Newer
Older
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
CoDEmanX
committed
"version": (1, 5),
"description": "Modeling and retopology tool.",
"wiki_url": "http://wiki.blender.org/index.php/Dev:Ref/Release_Notes/2.64/Bsurfaces_1.5",
CoDEmanX
committed
"tracker_url": "https://developer.blender.org/T26642",
CoDEmanX
committed
import mathutils
import operator
class VIEW3D_PT_tools_SURFSK_mesh(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
CoDEmanX
committed
CoDEmanX
committed
def draw(self, context):
layout = self.layout
CoDEmanX
committed
CoDEmanX
committed
row = layout.row()
row.separator()
col.operator("gpencil.surfsk_add_surface", text="Add Surface")
col.operator("gpencil.surfsk_edit_strokes", text="Edit Strokes")
col.prop(scn, "SURFSK_cyclic_cross")
col.prop(scn, "SURFSK_cyclic_follow")
col.prop(scn, "SURFSK_loops_on_strokes")
col.prop(scn, "SURFSK_automatic_join")
col.prop(scn, "SURFSK_keep_strokes")
CoDEmanX
committed
class VIEW3D_PT_tools_SURFSK_curve(bpy.types.Panel):
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_context = "curve_edit"
bl_label = "Bsurfaces"
CoDEmanX
committed
@classmethod
def poll(cls, context):
return context.active_object
CoDEmanX
committed
def draw(self, context):
layout = self.layout
CoDEmanX
committed
scn = context.scene
ob = context.object
CoDEmanX
committed
col = layout.column(align=True)
row = layout.row()
row.separator()
col.operator("curve.surfsk_first_points", text="Set First Points")
col.operator("curve.switch_direction", text="Switch Direction")
col.operator("curve.surfsk_reorder_splines", text="Reorder Splines")
CoDEmanX
committed
#### Returns the type of strokes used.
def get_strokes_type(main_object):
strokes_type = ""
strokes_num = 0
CoDEmanX
committed
# Check if they are grease pencil
try:
#### Get the active grease pencil layer.
strokes_num = len(main_object.grease_pencil.layers.active.active_frame.strokes)
CoDEmanX
committed
if strokes_num > 0:
strokes_type = "GP_STROKES"
except:
pass
CoDEmanX
committed
# Check if they are curves, if there aren't grease pencil strokes.
if strokes_type == "":
if len(bpy.context.selected_objects) == 2:
for ob in bpy.context.selected_objects:
if ob != bpy.context.scene.objects.active and ob.type == "CURVE":
strokes_type = "EXTERNAL_CURVE"
strokes_num = len(ob.data.splines)
CoDEmanX
committed
# Check if there is any non-bezier spline.
for i in range(len(ob.data.splines)):
if ob.data.splines[i].type != "BEZIER":
strokes_type = "CURVE_WITH_NON_BEZIER_SPLINES"
break
CoDEmanX
committed
elif ob != bpy.context.scene.objects.active and ob.type != "CURVE":
strokes_type = "EXTERNAL_NO_CURVE"
elif len(bpy.context.selected_objects) > 2:
strokes_type = "MORE_THAN_ONE_EXTERNAL"
CoDEmanX
committed
# Check if there is a single stroke without any selection in the object.
if strokes_num == 1 and main_object.data.total_vert_sel == 0:
if strokes_type == "EXTERNAL_CURVE":
strokes_type = "SINGLE_CURVE_STROKE_NO_SELECTION"
elif strokes_type == "GP_STROKES":
strokes_type = "SINGLE_GP_STROKE_NO_SELECTION"
CoDEmanX
committed
if strokes_num == 0 and main_object.data.total_vert_sel > 0:
strokes_type = "SELECTION_ALONE"
CoDEmanX
committed
if strokes_type == "":
strokes_type = "NO_STROKES"
CoDEmanX
committed
# Surface generator operator.
class GPENCIL_OT_SURFSK_add_surface(bpy.types.Operator):
bl_idname = "gpencil.surfsk_add_surface"
bl_description = "Generates surfaces from grease pencil strokes, bezier curves or loose edges."
CoDEmanX
committed
edges_U = bpy.props.IntProperty(name = "Cross",
description = "Number of face-loops crossing the strokes.",
default = 1,
min = 1,
max = 200)
CoDEmanX
committed
edges_V = bpy.props.IntProperty(name = "Follow",
description = "Number of face-loops following the strokes.",
default = 1,
min = 1,
max = 200)
CoDEmanX
committed
cyclic_cross = bpy.props.BoolProperty(name = "Cyclic Cross",
description = "Make cyclic the face-loops crossing the strokes.",
default = False)
CoDEmanX
committed
cyclic_follow = bpy.props.BoolProperty(name = "Cyclic Follow",
description = "Make cyclic the face-loops following the strokes.",
default = False)
CoDEmanX
committed
loops_on_strokes = bpy.props.BoolProperty(name = "Loops on strokes",
description = "Make the loops match the paths of the strokes.",
default = False)
CoDEmanX
committed
automatic_join = bpy.props.BoolProperty(name = "Automatic join",
description = "Join automatically vertices of either surfaces generated by crosshatching, or from the borders of closed shapes.",
default = False)
CoDEmanX
committed
join_stretch_factor = bpy.props.FloatProperty(name = "Stretch",
description = "Amount of stretching or shrinking allowed for edges when joining vertices automatically.",
default = 1,
min = 0,
max = 3,
subtype = 'FACTOR')
CoDEmanX
committed
def draw(self, context):
layout = self.layout
CoDEmanX
committed
scn = context.scene
ob = context.object
CoDEmanX
committed
col = layout.column(align=True)
row = layout.row()
CoDEmanX
committed
if not self.is_fill_faces:
row.separator()
if not self.is_crosshatch:
if not self.selection_U_exists:
col.prop(self, "edges_U")
row.separator()
CoDEmanX
committed
if not self.selection_V_exists:
col.prop(self, "edges_V")
row.separator()
CoDEmanX
committed
CoDEmanX
committed
if not self.selection_U_exists:
if not ((self.selection_V_exists and not self.selection_V_is_closed) or (self.selection_V2_exists and not self.selection_V2_is_closed)):
col.prop(self, "cyclic_cross")
CoDEmanX
committed
if not self.selection_V_exists:
if not ((self.selection_U_exists and not self.selection_U_is_closed) or (self.selection_U2_exists and not self.selection_U2_is_closed)):
col.prop(self, "cyclic_follow")
CoDEmanX
committed
col.prop(self, "loops_on_strokes")
CoDEmanX
committed
col.prop(self, "automatic_join")
if self.automatic_join:
row.separator()
col.separator()
row.separator()
col.prop(self, "join_stretch_factor")
CoDEmanX
committed
#### Get an ordered list of a chain of vertices.
def get_ordered_verts(self, ob, all_selected_edges_idx, all_selected_verts_idx, first_vert_idx, middle_vertex_idx, closing_vert_idx):
# Order selected vertices.
if closing_vert_idx != None:
verts_ordered.append(ob.data.vertices[closing_vert_idx])
CoDEmanX
committed
verts_ordered.append(ob.data.vertices[first_vert_idx])
prev_v = first_vert_idx
prev_ed = None
finish_while = False
while True:
edges_non_matched = 0
for i in all_selected_edges_idx:
if ob.data.edges[i] != prev_ed and ob.data.edges[i].vertices[0] == prev_v and ob.data.edges[i].vertices[1] in all_selected_verts_idx:
verts_ordered.append(ob.data.vertices[ob.data.edges[i].vertices[1]])
prev_v = ob.data.edges[i].vertices[1]
elif ob.data.edges[i] != prev_ed and ob.data.edges[i].vertices[1] == prev_v and ob.data.edges[i].vertices[0] in all_selected_verts_idx:
verts_ordered.append(ob.data.vertices[ob.data.edges[i].vertices[0]])
prev_v = ob.data.edges[i].vertices[0]
prev_ed = ob.data.edges[i]
else:
edges_non_matched += 1
CoDEmanX
committed
if edges_non_matched == len(all_selected_edges_idx):
finish_while = True
CoDEmanX
committed
CoDEmanX
committed
if closing_vert_idx != None:
verts_ordered.append(ob.data.vertices[closing_vert_idx])
CoDEmanX
committed
verts_ordered.append(ob.data.vertices[middle_vertex_idx])
CoDEmanX
committed
CoDEmanX
committed
#### Calculates length of a chain of points.
CoDEmanX
committed
edges_lengths = []
edges_lengths_sum = 0
for i in range(0, len(verts_ordered)):
if i == 0:
prev_v_co = matrix * verts_ordered[i].co
v_co = matrix * verts_ordered[i].co
CoDEmanX
committed
v_difs = [prev_v_co[0] - v_co[0], prev_v_co[1] - v_co[1], prev_v_co[2] - v_co[2]]
edge_length = abs(sqrt(v_difs[0] * v_difs[0] + v_difs[1] * v_difs[1] + v_difs[2] * v_difs[2]))
CoDEmanX
committed
edges_lengths.append(edge_length)
edges_lengths_sum += edge_length
CoDEmanX
committed
CoDEmanX
committed
return edges_lengths, edges_lengths_sum
CoDEmanX
committed
#### Calculates the proportion of the edges of a chain of edges, relative to the full chain length.
def get_edges_proportions(self, edges_lengths, edges_lengths_sum, use_boundaries, fixed_edges_num):
edges_proportions = []
if use_boundaries:
verts_count = 1
for l in edges_lengths:
edges_proportions.append(l / edges_lengths_sum)
verts_count += 1
else:
verts_count = 1
for n in range(0, fixed_edges_num):
edges_proportions.append(1 / fixed_edges_num)
verts_count += 1
CoDEmanX
committed
CoDEmanX
committed
#### Calculates the angle between two pairs of points in space.
def orientation_difference(self, points_A_co, points_B_co): # each parameter should be a list with two elements, and each element should be a x,y,z coordinate.
vec_A = points_A_co[0] - points_A_co[1]
vec_B = points_B_co[0] - points_B_co[1]
CoDEmanX
committed
CoDEmanX
committed
if angle > 0.5 * math.pi:
angle = abs(angle - math.pi)
CoDEmanX
committed
CoDEmanX
committed
#### Calculate the which vert of verts_idx list is the nearest one to the point_co coordinates, and the distance.
def shortest_distance(self, object, point_co, verts_idx):
matrix = object.matrix_world
CoDEmanX
committed
for i in range(0, len(verts_idx)):
dist = (point_co - matrix * object.data.vertices[verts_idx[i]].co).length
if i == 0:
prev_dist = dist
nearest_vert_idx = verts_idx[i]
shortest_dist = dist
CoDEmanX
committed
if dist < prev_dist:
prev_dist = dist
nearest_vert_idx = verts_idx[i]
shortest_dist = dist
CoDEmanX
committed
return nearest_vert_idx, shortest_dist
CoDEmanX
committed
#### Returns the index of the opposite vert tip in a chain, given a vert tip index as parameter, and a multidimentional list with all pairs of tips.
def opposite_tip(self, vert_tip_idx, all_chains_tips_idx):
opposite_vert_tip_idx = None
for i in range(0, len(all_chains_tips_idx)):
if vert_tip_idx == all_chains_tips_idx[i][0]:
opposite_vert_tip_idx = all_chains_tips_idx[i][1]
if vert_tip_idx == all_chains_tips_idx[i][1]:
opposite_vert_tip_idx = all_chains_tips_idx[i][0]
CoDEmanX
committed
CoDEmanX
committed
#### Simplifies a spline and returns the new points coordinates.
def simplify_spline(self, spline_coords, segments_num):
simplified_spline = []
points_between_segments = round(len(spline_coords) / segments_num)
CoDEmanX
committed
simplified_spline.append(spline_coords[0])
for i in range(1, segments_num):
simplified_spline.append(spline_coords[i * points_between_segments])
CoDEmanX
committed
simplified_spline.append(spline_coords[len(spline_coords) - 1])
CoDEmanX
committed
CoDEmanX
committed
#### Cleans up the scene and gets it the same it was at the beginning, in case the script is interrupted in the middle of the execution.
def cleanup_on_interruption(self):
# If the original strokes curve comes from conversion from grease pencil and wasn't made by hand, delete it.
if not self.using_external_curves:
try:
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.original_curve.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.original_curve.name]
CoDEmanX
committed
bpy.ops.object.delete()
except:
pass
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.main_object.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.main_object.name]
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.original_curve.name].select = True
bpy.data.objects[self.main_object.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.main_object.name]
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
#### Returns a list with the coords of the points distributed over the splines passed to this method according to the proportions parameter.
def distribute_pts(self, surface_splines, proportions):
# Calculate the length of each final surface spline.
surface_splines_lengths = []
surface_splines_parsed = []
for sp_idx in range(0, len(surface_splines)):
# Calculate spline length
surface_splines_lengths.append(0)
for i in range(0, len(surface_splines[sp_idx].bezier_points)):
if i == 0:
prev_p = surface_splines[sp_idx].bezier_points[i]
else:
p = surface_splines[sp_idx].bezier_points[i]
CoDEmanX
committed
edge_length = (prev_p.co - p.co).length
CoDEmanX
committed
surface_splines_lengths[sp_idx] += edge_length
CoDEmanX
committed
CoDEmanX
committed
# Calculate vertex positions with appropriate edge proportions, and ordered, for each spline.
for sp_idx in range(0, len(surface_splines)):
surface_splines_parsed.append([])
surface_splines_parsed[sp_idx].append(surface_splines[sp_idx].bezier_points[0].co)
CoDEmanX
committed
prev_p_co = surface_splines[sp_idx].bezier_points[0].co
p_idx = 0
for prop_idx in range(len(proportions) - 1):
target_length = surface_splines_lengths[sp_idx] * proportions[prop_idx]
CoDEmanX
committed
CoDEmanX
committed
finish_while = False
while True:
p_co = surface_splines[sp_idx].bezier_points[p_idx].co
CoDEmanX
committed
new_dist = (prev_p_co - p_co).length
CoDEmanX
committed
potential_segment_length = partial_segment_length + new_dist # The new distance that could have the partial segment if it is still shorter than the target length.
CoDEmanX
committed
if potential_segment_length < target_length: # If the potential is still shorter, keep adding.
partial_segment_length = potential_segment_length
CoDEmanX
committed
CoDEmanX
committed
elif potential_segment_length > target_length: # If the potential is longer than the target, calculate the target (a point between the last two points), and assign.
remaining_dist = target_length - partial_segment_length
vec = p_co - prev_p_co
vec.normalize()
intermediate_co = prev_p_co + (vec * remaining_dist)
CoDEmanX
committed
surface_splines_parsed[sp_idx].append(intermediate_co)
CoDEmanX
committed
partial_segment_length += remaining_dist
prev_p_co = intermediate_co
CoDEmanX
committed
CoDEmanX
committed
elif potential_segment_length == target_length: # If the potential is equal to the target, assign.
surface_splines_parsed[sp_idx].append(p_co)
CoDEmanX
committed
CoDEmanX
committed
CoDEmanX
committed
CoDEmanX
committed
# last point of the spline
surface_splines_parsed[sp_idx].append(surface_splines[sp_idx].bezier_points[len(surface_splines[sp_idx].bezier_points) - 1].co)
CoDEmanX
committed
CoDEmanX
committed
#### Counts the number of faces that belong to each edge.
def edge_face_count(self, ob):
ed_keys_count_dict = {}
CoDEmanX
committed
for face in ob.data.polygons:
for ed_keys in face.edge_keys:
if not ed_keys in ed_keys_count_dict:
ed_keys_count_dict[ed_keys] = 1
else:
ed_keys_count_dict[ed_keys] += 1
CoDEmanX
committed
edge_face_count = []
for i in range(len(ob.data.edges)):
edge_face_count.append(0)
CoDEmanX
committed
for i in range(len(ob.data.edges)):
ed = ob.data.edges[i]
CoDEmanX
committed
v1 = ed.vertices[0]
v2 = ed.vertices[1]
CoDEmanX
committed
if (v1, v2) in ed_keys_count_dict:
edge_face_count[i] = ed_keys_count_dict[(v1, v2)]
elif (v2, v1) in ed_keys_count_dict:
edge_face_count[i] = ed_keys_count_dict[(v2, v1)]
CoDEmanX
committed
CoDEmanX
committed
#### Fills with faces all the selected vertices which form empty triangles or quads.
def fill_with_faces(self, object):
all_selected_verts_count = self.main_object_selected_verts_count
CoDEmanX
committed
bpy.ops.object.mode_set('INVOKE_REGION_WIN', mode='OBJECT')
CoDEmanX
committed
#### Calculate average length of selected edges.
all_selected_verts = []
original_sel_edges_count = 0
for ed in object.data.edges:
if object.data.vertices[ed.vertices[0]].select and object.data.vertices[ed.vertices[1]].select:
coords = []
coords.append(object.data.vertices[ed.vertices[0]].co)
coords.append(object.data.vertices[ed.vertices[1]].co)
CoDEmanX
committed
CoDEmanX
committed
if not ed.vertices[0] in all_selected_verts:
all_selected_verts.append(ed.vertices[0])
CoDEmanX
committed
if not ed.vertices[1] in all_selected_verts:
all_selected_verts.append(ed.vertices[1])
CoDEmanX
committed
CoDEmanX
committed
#### Check if there is any edge selected. If not, interrupt the script.
if original_sel_edges_count == 0 and all_selected_verts_count > 0:
return 0
CoDEmanX
committed
#### Get all edges connected to selected verts.
all_edges_around_sel_verts = []
edges_connected_to_sel_verts = {}
verts_connected_to_every_vert = {}
for ed_idx in range(len(object.data.edges)):
ed = object.data.edges[ed_idx]
include_edge = False
CoDEmanX
committed
if ed.vertices[0] in all_selected_verts:
if not ed.vertices[0] in edges_connected_to_sel_verts:
edges_connected_to_sel_verts[ed.vertices[0]] = []
CoDEmanX
committed
edges_connected_to_sel_verts[ed.vertices[0]].append(ed_idx)
include_edge = True
CoDEmanX
committed
if ed.vertices[1] in all_selected_verts:
if not ed.vertices[1] in edges_connected_to_sel_verts:
edges_connected_to_sel_verts[ed.vertices[1]] = []
CoDEmanX
committed
edges_connected_to_sel_verts[ed.vertices[1]].append(ed_idx)
include_edge = True
CoDEmanX
committed
if include_edge == True:
all_edges_around_sel_verts.append(ed_idx)
CoDEmanX
committed
# Get all connected verts to each vert.
if not ed.vertices[0] in verts_connected_to_every_vert:
verts_connected_to_every_vert[ed.vertices[0]] = []
CoDEmanX
committed
if not ed.vertices[1] in verts_connected_to_every_vert:
verts_connected_to_every_vert[ed.vertices[1]] = []
CoDEmanX
committed
verts_connected_to_every_vert[ed.vertices[0]].append(ed.vertices[1])
verts_connected_to_every_vert[ed.vertices[1]].append(ed.vertices[0])
CoDEmanX
committed
#### Get all verts connected to faces.
all_verts_part_of_faces = []
all_edges_faces_count = []
all_edges_faces_count += self.edge_face_count(object)
CoDEmanX
committed
# Get only the selected edges that have faces attached.
count_faces_of_edges_around_sel_verts = {}
selected_verts_with_faces = []
for ed_idx in all_edges_around_sel_verts:
count_faces_of_edges_around_sel_verts[ed_idx] = all_edges_faces_count[ed_idx]
CoDEmanX
committed
if all_edges_faces_count[ed_idx] > 0:
ed = object.data.edges[ed_idx]
CoDEmanX
committed
if not ed.vertices[0] in selected_verts_with_faces:
selected_verts_with_faces.append(ed.vertices[0])
CoDEmanX
committed
if not ed.vertices[1] in selected_verts_with_faces:
selected_verts_with_faces.append(ed.vertices[1])
CoDEmanX
committed
all_verts_part_of_faces.append(ed.vertices[0])
all_verts_part_of_faces.append(ed.vertices[1])
CoDEmanX
committed
CoDEmanX
committed
#### Discard unneeded verts from calculations.
participating_verts = []
movable_verts = []
for v_idx in all_selected_verts:
vert_has_edges_with_one_face = False
CoDEmanX
committed
for ed_idx in edges_connected_to_sel_verts[v_idx]: # Check if the actual vert has at least one edge connected to only one face.
if count_faces_of_edges_around_sel_verts[ed_idx] == 1:
vert_has_edges_with_one_face = True
CoDEmanX
committed
# If the vert has two or less edges connected and the vert is not part of any face. Or the vert is part of any face and at least one of the connected edges has only one face attached to it.
if (len(edges_connected_to_sel_verts[v_idx]) == 2 and not v_idx in all_verts_part_of_faces) or len(edges_connected_to_sel_verts[v_idx]) == 1 or (v_idx in all_verts_part_of_faces and vert_has_edges_with_one_face):
participating_verts.append(v_idx)
CoDEmanX
committed
if not v_idx in all_verts_part_of_faces:
movable_verts.append(v_idx)
CoDEmanX
committed
#### Remove from movable verts list those that are part of closed geometry (ie: triangles, quads)
for mv_idx in movable_verts:
freeze_vert = False
mv_connected_verts = verts_connected_to_every_vert[mv_idx]
CoDEmanX
committed
for actual_v_idx in all_selected_verts:
count_shared_neighbors = 0
checked_verts = []
CoDEmanX
committed
for mv_conn_v_idx in mv_connected_verts:
if mv_idx != actual_v_idx:
if mv_conn_v_idx in verts_connected_to_every_vert[actual_v_idx] and not mv_conn_v_idx in checked_verts:
count_shared_neighbors += 1
checked_verts.append(mv_conn_v_idx)
CoDEmanX
committed
if actual_v_idx in mv_connected_verts:
freeze_vert = True
break
CoDEmanX
committed
if count_shared_neighbors == 2:
freeze_vert = True
break
CoDEmanX
committed
CoDEmanX
committed
if freeze_vert:
movable_verts.remove(mv_idx)
CoDEmanX
committed
#### Calculate merge distance for participating verts.
shortest_edge_length = None
for ed in object.data.edges:
if ed.vertices[0] in movable_verts and ed.vertices[1] in movable_verts:
v1 = object.data.vertices[ed.vertices[0]]
v2 = object.data.vertices[ed.vertices[1]]
CoDEmanX
committed
CoDEmanX
committed
if shortest_edge_length == None:
shortest_edge_length = length
else:
if length < shortest_edge_length:
shortest_edge_length = length
CoDEmanX
committed
if shortest_edge_length != None:
edges_merge_distance = shortest_edge_length * 0.5
else:
edges_merge_distance = 0
CoDEmanX
committed
#### Get together the verts near enough. They will be merged later.
remaining_verts = []
remaining_verts += participating_verts
for v1_idx in participating_verts:
if v1_idx in remaining_verts and v1_idx in movable_verts:
verts_to_merge = []
coords_verts_to_merge = {}
CoDEmanX
committed
CoDEmanX
committed
v1_co = object.data.vertices[v1_idx].co
coords_verts_to_merge[v1_idx] = (v1_co[0], v1_co[1], v1_co[2])
CoDEmanX
committed
for v2_idx in remaining_verts:
if v1_idx != v2_idx:
v2_co = object.data.vertices[v2_idx].co
CoDEmanX
committed
CoDEmanX
committed
if dist <= edges_merge_distance: # Add the verts which are near enough.
verts_to_merge.append(v2_idx)
CoDEmanX
committed
coords_verts_to_merge[v2_idx] = (v2_co[0], v2_co[1], v2_co[2])
CoDEmanX
committed
for vm_idx in verts_to_merge:
remaining_verts.remove(vm_idx)
CoDEmanX
committed
if len(verts_to_merge) > 1:
# Calculate middle point of the verts to merge.
sum_x_co = 0
sum_y_co = 0
sum_z_co = 0
movable_verts_to_merge_count = 0
for i in range(len(verts_to_merge)):
if verts_to_merge[i] in movable_verts:
v_co = object.data.vertices[verts_to_merge[i]].co
CoDEmanX
committed
sum_x_co += v_co[0]
sum_y_co += v_co[1]
sum_z_co += v_co[2]
CoDEmanX
committed
movable_verts_to_merge_count += 1
CoDEmanX
committed
middle_point_co = [sum_x_co / movable_verts_to_merge_count, sum_y_co / movable_verts_to_merge_count, sum_z_co / movable_verts_to_merge_count]
CoDEmanX
committed
# Check if any vert to be merged is not movable.
shortest_dist = None
are_verts_not_movable = False
verts_not_movable = []
for v_merge_idx in verts_to_merge:
if v_merge_idx in participating_verts and not v_merge_idx in movable_verts:
are_verts_not_movable = True
verts_not_movable.append(v_merge_idx)
CoDEmanX
committed
if are_verts_not_movable:
# Get the vert connected to faces, that is nearest to the middle point of the movable verts.
shortest_dist = None
for vcf_idx in verts_not_movable:
dist = abs((object.data.vertices[vcf_idx].co - mathutils.Vector(middle_point_co)).length)
CoDEmanX
committed
if shortest_dist == None:
shortest_dist = dist
nearest_vert_idx = vcf_idx
else:
if dist < shortest_dist:
shortest_dist = dist
nearest_vert_idx = vcf_idx
CoDEmanX
committed
coords = object.data.vertices[nearest_vert_idx].co
CoDEmanX
committed
target_point_co = [coords[0], coords[1], coords[2]]
else:
target_point_co = middle_point_co
CoDEmanX
committed
# Move verts to merge to the middle position.
for v_merge_idx in verts_to_merge:
if v_merge_idx in movable_verts: # Only move the verts that are not part of faces.
object.data.vertices[v_merge_idx].co[0] = target_point_co[0]
object.data.vertices[v_merge_idx].co[1] = target_point_co[1]
object.data.vertices[v_merge_idx].co[2] = target_point_co[2]
CoDEmanX
committed
#### Perform "Remove Doubles" to weld all the disconnected verts
bpy.ops.object.mode_set('INVOKE_REGION_WIN', mode='EDIT')
bpy.ops.mesh.remove_doubles(threshold=0.0001)
CoDEmanX
committed
bpy.ops.object.mode_set('INVOKE_REGION_WIN', mode='OBJECT')
CoDEmanX
committed
#### Get all the definitive selected edges, after weldding.
selected_edges = []
edges_per_vert = {} # Number of faces of each selected edge.
for ed in object.data.edges:
if object.data.vertices[ed.vertices[0]].select and object.data.vertices[ed.vertices[1]].select:
selected_edges.append(ed.index)
CoDEmanX
committed
# Save all the edges that belong to each vertex.
if not ed.vertices[0] in edges_per_vert:
edges_per_vert[ed.vertices[0]] = []
CoDEmanX
committed
if not ed.vertices[1] in edges_per_vert:
edges_per_vert[ed.vertices[1]] = []
CoDEmanX
committed
edges_per_vert[ed.vertices[0]].append(ed.index)
edges_per_vert[ed.vertices[1]].append(ed.index)
CoDEmanX
committed
# Check if all the edges connected to each vert have two faces attached to them. To discard them later and make calculations faster.
a = []
a += self.edge_face_count(object)
tuple(a)
verts_surrounded_by_faces = {}
for v_idx in edges_per_vert:
edges = edges_per_vert[v_idx]
CoDEmanX
committed
edges_with_two_faces_count = 0
for ed_idx in edges_per_vert[v_idx]:
if a[ed_idx] == 2:
edges_with_two_faces_count += 1
CoDEmanX
committed
if edges_with_two_faces_count == len(edges_per_vert[v_idx]):
verts_surrounded_by_faces[v_idx] = True
else:
verts_surrounded_by_faces[v_idx] = False
CoDEmanX
committed
#### Get all the selected vertices.
selected_verts_idx = []
for v in object.data.vertices:
if v.select:
selected_verts_idx.append(v.index)
CoDEmanX
committed
#### Get all the faces of the object.
all_object_faces_verts_idx = []
for face in object.data.polygons:
face_verts = []
face_verts.append(face.vertices[0])
face_verts.append(face.vertices[1])
face_verts.append(face.vertices[2])
CoDEmanX
committed
if len(face.vertices) == 4:
face_verts.append(face.vertices[3])
CoDEmanX
committed
all_object_faces_verts_idx.append(face_verts)
CoDEmanX
committed
#### Deselect all vertices.
bpy.ops.object.mode_set('INVOKE_REGION_WIN', mode='EDIT')
bpy.ops.mesh.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.ops.object.mode_set('INVOKE_REGION_WIN', mode='OBJECT')
CoDEmanX
committed
#### Make a dictionary with the verts related to each vert.
related_key_verts = {}
for ed_idx in selected_edges:
ed = object.data.edges[ed_idx]
CoDEmanX
committed
if not verts_surrounded_by_faces[ed.vertices[0]]:
if not ed.vertices[0] in related_key_verts:
related_key_verts[ed.vertices[0]] = []
CoDEmanX
committed
if not ed.vertices[1] in related_key_verts[ed.vertices[0]]:
related_key_verts[ed.vertices[0]].append(ed.vertices[1])
CoDEmanX
committed
if not verts_surrounded_by_faces[ed.vertices[1]]:
if not ed.vertices[1] in related_key_verts:
related_key_verts[ed.vertices[1]] = []
CoDEmanX
committed
if not ed.vertices[0] in related_key_verts[ed.vertices[1]]:
related_key_verts[ed.vertices[1]].append(ed.vertices[0])
CoDEmanX
committed
#### Get groups of verts forming each face.
CoDEmanX
committed
faces_verts_idx = []
for v1 in related_key_verts: # verts-1 ....
for v2 in related_key_verts: # verts-2
if v1 != v2:
related_verts_in_common = []
v2_in_rel_v1 = False
v1_in_rel_v2 = False
for rel_v1 in related_key_verts[v1]:
if rel_v1 in related_key_verts[v2]: # Check if related verts of verts-1 are related verts of verts-2.
related_verts_in_common.append(rel_v1)
CoDEmanX
committed
if v2 in related_key_verts[v1]:
v2_in_rel_v1 = True
CoDEmanX
committed
if v1 in related_key_verts[v2]:
v1_in_rel_v2 = True
CoDEmanX
committed
repeated_face = False
# If two verts have two related verts in common, they form a quad.
if len(related_verts_in_common) == 2:
# Check if the face is already saved.
all_faces_to_check_idx = faces_verts_idx + all_object_faces_verts_idx
CoDEmanX
committed
for f_verts in all_faces_to_check_idx:
repeated_verts = 0
CoDEmanX
committed
if len(f_verts) == 4:
if v1 in f_verts: repeated_verts += 1
if v2 in f_verts: repeated_verts += 1
if related_verts_in_common[0] in f_verts: repeated_verts += 1
if related_verts_in_common[1] in f_verts: repeated_verts += 1
CoDEmanX
committed
if repeated_verts == len(f_verts):
repeated_face = True
break
CoDEmanX
committed
if not repeated_face:
faces_verts_idx.append([v1, related_verts_in_common[0], v2, related_verts_in_common[1]])
CoDEmanX
committed
elif v2_in_rel_v1 and v1_in_rel_v2 and len(related_verts_in_common) == 1: # If Two verts have one related vert in common and they are related to each other, they form a triangle.
# Check if the face is already saved.
all_faces_to_check_idx = faces_verts_idx + all_object_faces_verts_idx
CoDEmanX
committed
for f_verts in all_faces_to_check_idx:
repeated_verts = 0
CoDEmanX
committed
if len(f_verts) == 3:
if v1 in f_verts: repeated_verts += 1
if v2 in f_verts: repeated_verts += 1
if related_verts_in_common[0] in f_verts: repeated_verts += 1
CoDEmanX
committed
if repeated_verts == len(f_verts):
repeated_face = True
break
CoDEmanX
committed
if not repeated_face:
faces_verts_idx.append([v1, related_verts_in_common[0], v2])
CoDEmanX
committed
#### Keep only the faces that don't overlap by ignoring quads that overlap with two adjacent triangles.
faces_to_not_include_idx = [] # Indices of faces_verts_idx to eliminate.
all_faces_to_check_idx = faces_verts_idx + all_object_faces_verts_idx
for i in range(len(faces_verts_idx)):
for t in range(len(all_faces_to_check_idx)):
if i != t:
verts_in_common = 0
CoDEmanX
committed
if len(faces_verts_idx[i]) == 4 and len(all_faces_to_check_idx[t]) == 3:
for v_idx in all_faces_to_check_idx[t]:
if v_idx in faces_verts_idx[i]:
verts_in_common += 1
CoDEmanX
committed
if verts_in_common == 3: # If it doesn't have all it's vertices repeated in the other face.
if not i in faces_to_not_include_idx:
faces_to_not_include_idx.append(i)
CoDEmanX
committed
#### Build faces discarding the ones in faces_to_not_include.
me = object.data
bm = bmesh.new()
bm.from_mesh(me)
CoDEmanX
committed
num_faces_created = 0
for i in range(len(faces_verts_idx)):
if not i in faces_to_not_include_idx:
bm.faces.new([ bm.verts[v] for v in faces_verts_idx[i] ])
CoDEmanX
committed
CoDEmanX
committed
CoDEmanX
committed
for v_idx in selected_verts_idx:
self.main_object.data.vertices[v_idx].select = True
CoDEmanX
committed
bpy.ops.object.mode_set('INVOKE_REGION_WIN', mode='EDIT')
bpy.ops.mesh.normals_make_consistent(inside=False)
bpy.ops.object.mode_set('INVOKE_REGION_WIN', mode='OBJECT')
CoDEmanX
committed
CoDEmanX
committed
#### Crosshatch skinning.
def crosshatch_surface_invoke(self, ob_original_splines):
self.is_crosshatch = False
self.crosshatch_merge_distance = 0
CoDEmanX
committed
objects_to_delete = [] # duplicated strokes to be deleted.
CoDEmanX
committed
# If the main object uses modifiers deactivate them temporarily until the surface is joined. (without this the surface verts merging with the main object doesn't work well)
self.modifiers_prev_viewport_state = []
if len(self.main_object.modifiers) > 0:
for m_idx in range(len(self.main_object.modifiers)):
self.modifiers_prev_viewport_state.append(self.main_object.modifiers[m_idx].show_viewport)
CoDEmanX
committed
self.main_object.modifiers[m_idx].show_viewport = False
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[ob_original_splines.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[ob_original_splines.name]
CoDEmanX
committed
if len(ob_original_splines.data.splines) >= 2:
bpy.ops.object.duplicate('INVOKE_REGION_WIN')
ob_splines = bpy.context.object
ob_splines.name = "SURFSKIO_NE_STR"
CoDEmanX
committed
#### Get estimative merge distance (sum up the distances from the first point to all other points, then average them and then divide them).
first_point_dist_sum = 0
first_dist = 0
second_dist = 0
coords_first_pt = ob_splines.data.splines[0].bezier_points[0].co
for i in range(len(ob_splines.data.splines)):
sp = ob_splines.data.splines[i]
CoDEmanX
committed
if coords_first_pt != sp.bezier_points[0].co:
first_dist = (coords_first_pt - sp.bezier_points[0].co).length
CoDEmanX
committed
if coords_first_pt != sp.bezier_points[len(sp.bezier_points) - 1].co:
second_dist = (coords_first_pt - sp.bezier_points[len(sp.bezier_points) - 1].co).length
CoDEmanX
committed
first_point_dist_sum += first_dist + second_dist
CoDEmanX
committed
if i == 0:
if first_dist != 0:
shortest_dist = first_dist
elif second_dist != 0:
shortest_dist = second_dist
CoDEmanX
committed
if shortest_dist > first_dist and first_dist != 0:
shortest_dist = first_dist
CoDEmanX
committed
if shortest_dist > second_dist and second_dist != 0:
shortest_dist = second_dist
CoDEmanX
committed
self.crosshatch_merge_distance = shortest_dist / 20
CoDEmanX
committed
#### Recalculation of merge distance.
CoDEmanX
committed
bpy.ops.object.duplicate('INVOKE_REGION_WIN')
CoDEmanX
committed
ob_calc_merge_dist = bpy.context.object
ob_calc_merge_dist.name = "SURFSKIO_CALC_TMP"
CoDEmanX
committed
objects_to_delete.append(ob_calc_merge_dist)
CoDEmanX
committed
#### Smooth out strokes a little to improve crosshatch detection.
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='SELECT')
CoDEmanX
committed
for i in range(4):
bpy.ops.curve.smooth('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
#### Convert curves into mesh.
ob_calc_merge_dist.data.resolution_u = 12
bpy.ops.object.convert(target='MESH', keep_original=False)
CoDEmanX
committed
# Find "intersection-nodes".
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.mesh.select_all('INVOKE_REGION_WIN', action='SELECT')
bpy.ops.mesh.remove_doubles('INVOKE_REGION_WIN', threshold=self.crosshatch_merge_distance)
bpy.ops.mesh.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
# Remove verts with less than three edges.
verts_edges_count = {}
for ed in ob_calc_merge_dist.data.edges:
v = ed.vertices
CoDEmanX
committed
if v[0] not in verts_edges_count:
verts_edges_count[v[0]] = 0
CoDEmanX
committed
if v[1] not in verts_edges_count:
verts_edges_count[v[1]] = 0
CoDEmanX
committed
verts_edges_count[v[0]] += 1
verts_edges_count[v[1]] += 1
CoDEmanX
committed
nodes_verts_coords = []
for v_idx in verts_edges_count:
v = ob_calc_merge_dist.data.vertices[v_idx]
CoDEmanX
committed
if verts_edges_count[v_idx] < 3:
v.select = True
CoDEmanX
committed
# Remove them.
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.mesh.delete('INVOKE_REGION_WIN', type='VERT')
bpy.ops.mesh.select_all('INVOKE_REGION_WIN', action='SELECT')
CoDEmanX
committed
# Remove doubles to discard very near verts from calculations of distance.
bpy.ops.mesh.remove_doubles('INVOKE_REGION_WIN', threshold=self.crosshatch_merge_distance * 4.0)
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
# Get all coords of the resulting nodes.
nodes_verts_coords = [(v.co[0], v.co[1], v.co[2]) for v in ob_calc_merge_dist.data.vertices]
CoDEmanX
committed
#### Check if the strokes are a crosshatch.
if len(nodes_verts_coords) >= 3:
self.is_crosshatch = True
CoDEmanX
committed
shortest_dist = None
for co_1 in nodes_verts_coords:
for co_2 in nodes_verts_coords:
if co_1 != co_2:
dist = (mathutils.Vector(co_1) - mathutils.Vector(co_2)).length
CoDEmanX
committed
if shortest_dist != None:
if dist < shortest_dist:
shortest_dist = dist
else:
shortest_dist = dist
CoDEmanX
committed
self.crosshatch_merge_distance = shortest_dist / 3
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[ob_splines.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[ob_splines.name]
CoDEmanX
committed
#### Deselect all points.
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
#### Smooth splines in a localized way, to eliminate "saw-teeth" like shapes when there are many points.
for sp in ob_splines.data.splines:
angle_sum = 0
CoDEmanX
committed
angle_limit = 2 # Degrees
for t in range(len(sp.bezier_points)):
if t <= len(sp.bezier_points) - 3: # Because on each iteration it checks the "next two points" of the actual. This way it doesn't go out of range.
p1 = sp.bezier_points[t]
p2 = sp.bezier_points[t + 1]
p3 = sp.bezier_points[t + 2]
CoDEmanX
committed
vec_1 = p1.co - p2.co
vec_2 = p2.co - p3.co
CoDEmanX
committed
if p2.co != p1.co and p2.co != p3.co:
angle = vec_1.angle(vec_2)
angle_sum += degrees(angle)
CoDEmanX
committed
if angle_sum >= angle_limit: # If sum of angles is grater than the limit.
if (p1.co - p2.co).length <= self.crosshatch_merge_distance:
p1.select_control_point = True; p1.select_left_handle = True; p1.select_right_handle = True
p2.select_control_point = True; p2.select_left_handle = True; p2.select_right_handle = True
CoDEmanX
committed
if (p1.co - p2.co).length <= self.crosshatch_merge_distance:
p3.select_control_point = True; p3.select_left_handle = True; p3.select_right_handle = True
CoDEmanX
committed
CoDEmanX
committed
sp.bezier_points[0].select_control_point = False
sp.bezier_points[0].select_left_handle = False
sp.bezier_points[0].select_right_handle = False
CoDEmanX
committed
sp.bezier_points[len(sp.bezier_points) - 1].select_control_point = False
sp.bezier_points[len(sp.bezier_points) - 1].select_left_handle = False
sp.bezier_points[len(sp.bezier_points) - 1].select_right_handle = False
CoDEmanX
committed
#### Smooth out strokes a little to improve crosshatch detection.
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
for i in range(15):
bpy.ops.curve.smooth('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
#### Simplify the splines.
for sp in ob_splines.data.splines:
angle_sum = 0
CoDEmanX
committed
sp.bezier_points[0].select_control_point = True
sp.bezier_points[0].select_left_handle = True
sp.bezier_points[0].select_right_handle = True
CoDEmanX
committed
sp.bezier_points[len(sp.bezier_points) - 1].select_control_point = True
sp.bezier_points[len(sp.bezier_points) - 1].select_left_handle = True
sp.bezier_points[len(sp.bezier_points) - 1].select_right_handle = True
CoDEmanX
committed
angle_limit = 15 # Degrees
for t in range(len(sp.bezier_points)):
if t <= len(sp.bezier_points) - 3: # Because on each iteration it checks the "next two points" of the actual. This way it doesn't go out of range.
p1 = sp.bezier_points[t]
p2 = sp.bezier_points[t + 1]
p3 = sp.bezier_points[t + 2]
CoDEmanX
committed
vec_1 = p1.co - p2.co
vec_2 = p2.co - p3.co
CoDEmanX
committed
if p2.co != p1.co and p2.co != p3.co:
angle = vec_1.angle(vec_2)
angle_sum += degrees(angle)
CoDEmanX
committed
if angle_sum >= angle_limit: # If sum of angles is grater than the limit.
p1.select_control_point = True; p1.select_left_handle = True; p1.select_right_handle = True
p2.select_control_point = True; p2.select_left_handle = True; p2.select_right_handle = True
p3.select_control_point = True; p3.select_left_handle = True; p3.select_right_handle = True
CoDEmanX
committed
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.curve.select_all(action = 'INVERT')
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
objects_to_delete.append(ob_splines)
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
#### Check if the strokes are a crosshatch.
if self.is_crosshatch:
all_points_coords = []
for i in range(len(ob_splines.data.splines)):
all_points_coords.append([])
CoDEmanX
committed
all_points_coords[i] = [mathutils.Vector((x, y, z)) for x, y, z in [bp.co for bp in ob_splines.data.splines[i].bezier_points]]
CoDEmanX
committed
all_intersections = []
checked_splines = []
for i in range(len(all_points_coords)):
CoDEmanX
committed
for t in range(len(all_points_coords[i]) - 1):
bp1_co = all_points_coords[i][t]
bp2_co = all_points_coords[i][t + 1]
CoDEmanX
committed
for i2 in range(len(all_points_coords)):
if i != i2 and not i2 in checked_splines:
for t2 in range(len(all_points_coords[i2]) - 1):
bp3_co = all_points_coords[i2][t2]
bp4_co = all_points_coords[i2][t2 + 1]
CoDEmanX
committed
intersec_coords = mathutils.geometry.intersect_line_line(bp1_co, bp2_co, bp3_co, bp4_co)
CoDEmanX
committed
if intersec_coords != None:
dist = (intersec_coords[0] - intersec_coords[1]).length
CoDEmanX
committed
if dist <= self.crosshatch_merge_distance * 1.5:
temp_co, percent1 = mathutils.geometry.intersect_point_line(intersec_coords[0], bp1_co, bp2_co)
CoDEmanX
committed
if (percent1 >= -0.02 and percent1 <= 1.02):
temp_co, percent2 = mathutils.geometry.intersect_point_line(intersec_coords[1], bp3_co, bp4_co)
if (percent2 >= -0.02 and percent2 <= 1.02):
all_intersections.append((i, t, percent1, ob_splines.matrix_world * intersec_coords[0])) # Format: spline index, first point index from corresponding segment, percentage from first point of actual segment, coords of intersection point.
all_intersections.append((i2, t2, percent2, ob_splines.matrix_world * intersec_coords[1]))
CoDEmanX
committed
checked_splines.append(i)
all_intersections.sort(key = operator.itemgetter(0,1,2)) # Sort list by spline, then by corresponding first point index of segment, and then by percentage from first point of segment: elements 0 and 1 respectively.
CoDEmanX
committed
self.crosshatch_strokes_coords = {}
for i in range(len(all_intersections)):
if not all_intersections[i][0] in self.crosshatch_strokes_coords:
self.crosshatch_strokes_coords[all_intersections[i][0]] = []
CoDEmanX
committed
self.crosshatch_strokes_coords[all_intersections[i][0]].append(all_intersections[i][3]) # Save intersection coords.
CoDEmanX
committed
else:
self.is_crosshatch = False
CoDEmanX
committed
#### Delete all duplicates.
for o in objects_to_delete:
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[o.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[o.name]
bpy.ops.object.delete()
CoDEmanX
committed
#### If the main object has modifiers, turn their "viewport view status" to what it was before the forced deactivation above.
if len(self.main_object.modifiers) > 0:
for m_idx in range(len(self.main_object.modifiers)):
self.main_object.modifiers[m_idx].show_viewport = self.modifiers_prev_viewport_state[m_idx]
CoDEmanX
committed
CoDEmanX
committed
#### Part of the Crosshatch process that is repeated when the operator is tweaked.
def crosshatch_surface_execute(self):
# If the main object uses modifiers deactivate them temporarily until the surface is joined. (without this the surface verts merging with the main object doesn't work well)
self.modifiers_prev_viewport_state = []
if len(self.main_object.modifiers) > 0:
for m_idx in range(len(self.main_object.modifiers)):
self.modifiers_prev_viewport_state.append(self.main_object.modifiers[m_idx].show_viewport)
CoDEmanX
committed
self.main_object.modifiers[m_idx].show_viewport = False
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
me_name = "SURFSKIO_STK_TMP"
me = bpy.data.meshes.new(me_name)
CoDEmanX
committed
all_verts_coords = []
all_edges = []
for st_idx in self.crosshatch_strokes_coords:
for co_idx in range(len(self.crosshatch_strokes_coords[st_idx])):
coords = self.crosshatch_strokes_coords[st_idx][co_idx]
CoDEmanX
committed
CoDEmanX
committed
if co_idx > 0:
all_edges.append((len(all_verts_coords) - 2, len(all_verts_coords) - 1))
CoDEmanX
committed
me.from_pydata(all_verts_coords, all_edges, [])
CoDEmanX
committed
CoDEmanX
committed
ob = bpy.data.objects.new(me_name, me)
ob.data = me
bpy.context.scene.objects.link(ob)
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[ob.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[ob.name]
CoDEmanX
committed
#### Get together each vert and its nearest, to the middle position.
verts = ob.data.vertices
checked_verts = []
for i in range(len(verts)):
shortest_dist = None
CoDEmanX
committed
if not i in checked_verts:
for t in range(len(verts)):
if i != t and not t in checked_verts:
dist = (verts[i].co - verts[t].co).length
CoDEmanX
committed
if shortest_dist != None:
if dist < shortest_dist:
shortest_dist = dist
nearest_vert = t
else:
shortest_dist = dist
nearest_vert = t
CoDEmanX
committed
middle_location = (verts[i].co + verts[nearest_vert].co) / 2
CoDEmanX
committed
verts[i].co = middle_location
verts[nearest_vert].co = middle_location
CoDEmanX
committed
checked_verts.append(i)
checked_verts.append(nearest_vert)
CoDEmanX
committed
#### Calculate average length between all the generated edges.
ob = bpy.context.object
lengths_sum = 0
for ed in ob.data.edges:
v1 = ob.data.vertices[ed.vertices[0]]
v2 = ob.data.vertices[ed.vertices[1]]
CoDEmanX
committed
lengths_sum += (v1.co - v2.co).length
CoDEmanX
committed
edges_count = len(ob.data.edges)
CoDEmanX
committed
average_edge_length = lengths_sum / edges_count
CoDEmanX
committed
bpy.ops.mesh.select_all('INVOKE_REGION_WIN', action='SELECT')
bpy.ops.mesh.remove_doubles('INVOKE_REGION_WIN', threshold=average_edge_length / 15.0)
CoDEmanX
committed
final_points_ob = bpy.context.scene.objects.active
CoDEmanX
committed
#### Make a dictionary with the verts related to each vert.
related_key_verts = {}
for ed in final_points_ob.data.edges:
if not ed.vertices[0] in related_key_verts:
related_key_verts[ed.vertices[0]] = []
CoDEmanX
committed
if not ed.vertices[1] in related_key_verts:
related_key_verts[ed.vertices[1]] = []
CoDEmanX
committed
if not ed.vertices[1] in related_key_verts[ed.vertices[0]]:
related_key_verts[ed.vertices[0]].append(ed.vertices[1])
CoDEmanX
committed
if not ed.vertices[0] in related_key_verts[ed.vertices[1]]:
related_key_verts[ed.vertices[1]].append(ed.vertices[0])
CoDEmanX
committed
#### Get groups of verts forming each face.
CoDEmanX
committed
faces_verts_idx = []
for v1 in related_key_verts: # verts-1 ....
for v2 in related_key_verts: # verts-2
if v1 != v2:
related_verts_in_common = []
v2_in_rel_v1 = False
v1_in_rel_v2 = False
for rel_v1 in related_key_verts[v1]:
if rel_v1 in related_key_verts[v2]: # Check if related verts of verts-1 are related verts of verts-2.
related_verts_in_common.append(rel_v1)
CoDEmanX
committed
if v2 in related_key_verts[v1]:
v2_in_rel_v1 = True
CoDEmanX
committed
if v1 in related_key_verts[v2]:
v1_in_rel_v2 = True
CoDEmanX
committed
repeated_face = False
# If two verts have two related verts in common, they form a quad.
if len(related_verts_in_common) == 2:
# Check if the face is already saved.
for f_verts in faces_verts_idx:
repeated_verts = 0
CoDEmanX
committed
if len(f_verts) == 4:
if v1 in f_verts: repeated_verts += 1
if v2 in f_verts: repeated_verts += 1
if related_verts_in_common[0] in f_verts: repeated_verts += 1
if related_verts_in_common[1] in f_verts: repeated_verts += 1
CoDEmanX
committed
if repeated_verts == len(f_verts):
repeated_face = True
break
CoDEmanX
committed
if not repeated_face:
faces_verts_idx.append([v1, related_verts_in_common[0], v2, related_verts_in_common[1]])
CoDEmanX
committed
elif v2_in_rel_v1 and v1_in_rel_v2 and len(related_verts_in_common) == 1: # If Two verts have one related vert in common and they are related to each other, they form a triangle.
# Check if the face is already saved.
for f_verts in faces_verts_idx:
repeated_verts = 0
CoDEmanX
committed
if len(f_verts) == 3:
if v1 in f_verts: repeated_verts += 1
if v2 in f_verts: repeated_verts += 1
if related_verts_in_common[0] in f_verts: repeated_verts += 1
CoDEmanX
committed
if repeated_verts == len(f_verts):
repeated_face = True
break
CoDEmanX
committed
if not repeated_face:
faces_verts_idx.append([v1, related_verts_in_common[0], v2])
CoDEmanX
committed
#### Keep only the faces that don't overlap by ignoring quads that overlap with two adjacent triangles.
faces_to_not_include_idx = [] # Indices of faces_verts_idx to eliminate.
for i in range(len(faces_verts_idx)):
for t in range(len(faces_verts_idx)):
if i != t:
verts_in_common = 0
CoDEmanX
committed
if len(faces_verts_idx[i]) == 4 and len(faces_verts_idx[t]) == 3:
for v_idx in faces_verts_idx[t]:
if v_idx in faces_verts_idx[i]:
verts_in_common += 1
CoDEmanX
committed
if verts_in_common == 3: # If it doesn't have all it's vertices repeated in the other face.
if not i in faces_to_not_include_idx:
faces_to_not_include_idx.append(i)
CoDEmanX
committed
#### Build surface.
all_surface_verts_co = []
verts_idx_translation = {}
for i in range(len(final_points_ob.data.vertices)):
coords = final_points_ob.data.vertices[i].co
all_surface_verts_co.append([coords[0], coords[1], coords[2]])
CoDEmanX
committed
# Verts of each face.
all_surface_faces = []
for i in range(len(faces_verts_idx)):
if not i in faces_to_not_include_idx:
face = []
for v_idx in faces_verts_idx[i]:
face.append(v_idx)
CoDEmanX
committed
CoDEmanX
committed
# Build the mesh.
surf_me_name = "SURFSKIO_surface"
me_surf = bpy.data.meshes.new(surf_me_name)
CoDEmanX
committed
me_surf.from_pydata(all_surface_verts_co, [], all_surface_faces)
CoDEmanX
committed
CoDEmanX
committed
ob_surface = bpy.data.objects.new(surf_me_name, me_surf)
bpy.context.scene.objects.link(ob_surface)
CoDEmanX
committed
# Delete final points temporal object
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[final_points_ob.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[final_points_ob.name]
CoDEmanX
committed
CoDEmanX
committed
# Delete isolated verts if there are any.
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[ob_surface.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[ob_surface.name]
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_face_by_sides(type='NOTEQUAL')
bpy.ops.mesh.delete()
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
#### Join crosshatch results with original mesh.
CoDEmanX
committed
# Calculate a distance to merge the verts of the crosshatch surface to the main object.
edges_length_sum = 0
for ed in ob_surface.data.edges:
edges_length_sum += (ob_surface.data.vertices[ed.vertices[0]].co - ob_surface.data.vertices[ed.vertices[1]].co).length
CoDEmanX
committed
if len(ob_surface.data.edges) > 0:
average_surface_edges_length = edges_length_sum / len(ob_surface.data.edges)
else:
average_surface_edges_length = 0.0001
CoDEmanX
committed
# Make dictionary with all the verts connected to each vert, on the new surface object.
surface_connected_verts = {}
for ed in ob_surface.data.edges:
if not ed.vertices[0] in surface_connected_verts:
surface_connected_verts[ed.vertices[0]] = []
CoDEmanX
committed
surface_connected_verts[ed.vertices[0]].append(ed.vertices[1])
CoDEmanX
committed
if not ed.vertices[1] in surface_connected_verts:
surface_connected_verts[ed.vertices[1]] = []
CoDEmanX
committed
surface_connected_verts[ed.vertices[1]].append(ed.vertices[0])
CoDEmanX
committed
# Duplicate the new surface object, and use shrinkwrap to calculate later the nearest verts to the main object.
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.mesh.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.object.duplicate('INVOKE_REGION_WIN')
CoDEmanX
committed
final_ob_duplicate = bpy.context.scene.objects.active
CoDEmanX
committed
bpy.ops.object.modifier_add('INVOKE_REGION_WIN', type='SHRINKWRAP')
final_ob_duplicate.modifiers["Shrinkwrap"].wrap_method = "NEAREST_VERTEX"
final_ob_duplicate.modifiers["Shrinkwrap"].target = self.main_object
CoDEmanX
committed
bpy.ops.object.modifier_apply('INVOKE_REGION_WIN', apply_as='DATA', modifier='Shrinkwrap')
CoDEmanX
committed
# Make list with verts of original mesh as index and coords as value.
main_object_verts_coords = []
for v in self.main_object.data.vertices:
coords = self.main_object.matrix_world * v.co
CoDEmanX
committed
for c in range(len(coords)): # To avoid problems when taking "-0.00" as a different value as "0.00".
if "%.3f" % coords[c] == "-0.00":
coords[c] = 0
CoDEmanX
committed
main_object_verts_coords.append(["%.3f" % coords[0], "%.3f" % coords[1], "%.3f" % coords[2]])
CoDEmanX
committed
CoDEmanX
committed
# Determine which verts will be merged, snap them to the nearest verts on the original verts, and get them selected.
crosshatch_verts_to_merge = []
if self.automatic_join:
for i in range(len(ob_surface.data.vertices)):
# Calculate the distance from each of the connected verts to the actual vert, and compare it with the distance they would have if joined. If they don't change much, that vert can be joined.
merge_actual_vert = True
if len(surface_connected_verts[i]) < 4:
for c_v_idx in surface_connected_verts[i]:
points_original = []
points_original.append(ob_surface.data.vertices[c_v_idx].co)
points_original.append(ob_surface.data.vertices[i].co)
CoDEmanX
committed
points_target = []
points_target.append(ob_surface.data.vertices[c_v_idx].co)
points_target.append(final_ob_duplicate.data.vertices[i].co)
CoDEmanX
committed
vec_A = points_original[0] - points_original[1]
vec_B = points_target[0] - points_target[1]
CoDEmanX
committed
dist_A = (points_original[0] - points_original[1]).length
dist_B = (points_target[0] - points_target[1]).length
CoDEmanX
committed
if not (points_original[0] == points_original[1] or points_target[0] == points_target[1]): # If any vector's length is zero.
angle = vec_A.angle(vec_B) / math.pi
else:
angle= 0
CoDEmanX
committed
if dist_B > dist_A * 1.7 * self.join_stretch_factor or dist_B < dist_A / 2 / self.join_stretch_factor or angle >= 0.15 * self.join_stretch_factor: # Set a range of acceptable variation in the connected edges.
merge_actual_vert = False
break
else:
merge_actual_vert = False
CoDEmanX
committed
if merge_actual_vert:
coords = final_ob_duplicate.data.vertices[i].co
CoDEmanX
committed
for c in range(len(coords)): # To avoid problems when taking "-0.000" as a different value as "0.00".
if "%.3f" % coords[c] == "-0.00":
coords[c] = 0
CoDEmanX
committed
comparison_coords = ["%.3f" % coords[0], "%.3f" % coords[1], "%.3f" % coords[2]]
CoDEmanX
committed
if comparison_coords in main_object_verts_coords:
main_object_related_vert_idx = main_object_verts_coords.index(comparison_coords) # Get the index of the vert with those coords in the main object.
CoDEmanX
committed
if self.main_object.data.vertices[main_object_related_vert_idx].select == True or self.main_object_selected_verts_count == 0:
ob_surface.data.vertices[i].co = final_ob_duplicate.data.vertices[i].co
ob_surface.data.vertices[i].select = True
crosshatch_verts_to_merge.append(i)
CoDEmanX
committed
# Make sure the vert in the main object is selected, in case it wasn't selected and the "join crosshatch" option is active.
self.main_object.data.vertices[main_object_related_vert_idx].select = True
CoDEmanX
committed
# Delete duplicated object.
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[final_ob_duplicate.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[final_ob_duplicate.name]
bpy.ops.object.delete()
CoDEmanX
committed
# Join crosshatched surface and main object.
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[ob_surface.name].select = True
bpy.data.objects[self.main_object.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.main_object.name]
CoDEmanX
committed
bpy.ops.object.join('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
# Perform Remove doubles to merge verts.
if not (self.automatic_join == False and self.main_object_selected_verts_count == 0):
bpy.ops.mesh.remove_doubles(threshold=0.0001)
CoDEmanX
committed
bpy.ops.mesh.select_all(action='DESELECT')
CoDEmanX
committed
#### If the main object has modifiers, turn their "viewport view status" to what it was before the forced deactivation above.
if len(self.main_object.modifiers) > 0:
for m_idx in range(len(self.main_object.modifiers)):
self.main_object.modifiers[m_idx].show_viewport = self.modifiers_prev_viewport_state[m_idx]
CoDEmanX
committed
CoDEmanX
committed
def rectangular_surface(self):
#### Selected edges.
all_selected_edges_idx = []
all_selected_verts = []
all_verts_idx = []
for ed in self.main_object.data.edges:
if ed.select:
all_selected_edges_idx.append(ed.index)
CoDEmanX
committed
# Selected vertices.
if not ed.vertices[0] in all_selected_verts:
all_selected_verts.append(self.main_object.data.vertices[ed.vertices[0]])
if not ed.vertices[1] in all_selected_verts:
all_selected_verts.append(self.main_object.data.vertices[ed.vertices[1]])
CoDEmanX
committed
# All verts (both from each edge) to determine later which are at the tips (those not repeated twice).
all_verts_idx.append(ed.vertices[0])
all_verts_idx.append(ed.vertices[1])
CoDEmanX
committed
#### Identify the tips and "middle-vertex" that separates U from V, if there is one.
all_chains_tips_idx = []
for v_idx in all_verts_idx:
if all_verts_idx.count(v_idx) < 2:
all_chains_tips_idx.append(v_idx)
CoDEmanX
committed
edges_connected_to_tips = []
for ed in self.main_object.data.edges:
if (ed.vertices[0] in all_chains_tips_idx or ed.vertices[1] in all_chains_tips_idx) and not (ed.vertices[0] in all_verts_idx and ed.vertices[1] in all_verts_idx):
edges_connected_to_tips.append(ed)
CoDEmanX
committed
#### Check closed selections.
single_unselected_verts_and_neighbors = [] # List with groups of three verts, where the first element of the pair is the unselected vert of a closed selection and the other two elements are the selected neighbor verts (it will be useful to determine which selection chain the unselected vert belongs to, and determine the "middle-vertex")
CoDEmanX
committed
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
# To identify a "closed" selection (a selection that is a closed chain except for one vertex) find the vertex in common that have the edges connected to tips. If there is a vertex in common, that one is the unselected vert that closes the selection or is a "middle-vertex".
single_unselected_verts = []
for ed in edges_connected_to_tips:
for ed_b in edges_connected_to_tips:
if ed != ed_b:
if ed.vertices[0] == ed_b.vertices[0] and not self.main_object.data.vertices[ed.vertices[0]].select and ed.vertices[0] not in single_unselected_verts:
single_unselected_verts_and_neighbors.append([ed.vertices[0], ed.vertices[1], ed_b.vertices[1]]) # The second element is one of the tips of the selected vertices of the closed selection.
single_unselected_verts.append(ed.vertices[0])
break
elif ed.vertices[0] == ed_b.vertices[1] and not self.main_object.data.vertices[ed.vertices[0]].select and ed.vertices[0] not in single_unselected_verts:
single_unselected_verts_and_neighbors.append([ed.vertices[0], ed.vertices[1], ed_b.vertices[0]])
single_unselected_verts.append(ed.vertices[0])
break
elif ed.vertices[1] == ed_b.vertices[0] and not self.main_object.data.vertices[ed.vertices[1]].select and ed.vertices[1] not in single_unselected_verts:
single_unselected_verts_and_neighbors.append([ed.vertices[1], ed.vertices[0], ed_b.vertices[1]])
single_unselected_verts.append(ed.vertices[1])
break
elif ed.vertices[1] == ed_b.vertices[1] and not self.main_object.data.vertices[ed.vertices[1]].select and ed.vertices[1] not in single_unselected_verts:
single_unselected_verts_and_neighbors.append([ed.vertices[1], ed.vertices[0], ed_b.vertices[0]])
single_unselected_verts.append(ed.vertices[1])
break
CoDEmanX
committed
middle_vertex_idx = None
tips_to_discard_idx = []
# Check if there is a "middle-vertex", and get its index.
for i in range(0, len(single_unselected_verts_and_neighbors)):
actual_chain_verts = self.get_ordered_verts(self.main_object, all_selected_edges_idx, all_verts_idx, single_unselected_verts_and_neighbors[i][1], None, None)
CoDEmanX
committed
if single_unselected_verts_and_neighbors[i][2] != actual_chain_verts[len(actual_chain_verts) - 1].index:
middle_vertex_idx = single_unselected_verts_and_neighbors[i][0]
tips_to_discard_idx.append(single_unselected_verts_and_neighbors[i][1])
tips_to_discard_idx.append(single_unselected_verts_and_neighbors[i][2])
CoDEmanX
committed
#### List with pairs of verts that belong to the tips of each selection chain (row).
verts_tips_same_chain_idx = []
if len(all_chains_tips_idx) >= 2:
checked_v = []
for i in range(0, len(all_chains_tips_idx)):
if all_chains_tips_idx[i] not in checked_v:
v_chain = self.get_ordered_verts(self.main_object, all_selected_edges_idx, all_verts_idx, all_chains_tips_idx[i], middle_vertex_idx, None)
CoDEmanX
committed
verts_tips_same_chain_idx.append([v_chain[0].index, v_chain[len(v_chain) - 1].index])
CoDEmanX
committed
checked_v.append(v_chain[0].index)
checked_v.append(v_chain[len(v_chain) - 1].index)
CoDEmanX
committed
#### Selection tips (vertices).
verts_tips_parsed_idx = []
if len(all_chains_tips_idx) >= 2:
for spec_v_idx in all_chains_tips_idx:
if (spec_v_idx not in tips_to_discard_idx):
verts_tips_parsed_idx.append(spec_v_idx)
CoDEmanX
committed
#### Identify the type of selection made by the user.
if middle_vertex_idx != None:
if len(all_chains_tips_idx) == 4 and len(single_unselected_verts_and_neighbors) == 1: # If there are 4 tips (two selection chains), and there is only one single unselected vert (the middle vert).
selection_type = "TWO_CONNECTED"
else:
# The type of the selection was not identified, the script stops.
self.report({'WARNING'}, "The selection isn't valid.")
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
self.cleanup_on_interruption()
self.stopping_errors = True
CoDEmanX
committed
return{'CANCELLED'}
else:
if len(all_chains_tips_idx) == 2: # If there are 2 tips
selection_type = "SINGLE"
elif len(all_chains_tips_idx) == 4: # If there are 4 tips
selection_type = "TWO_NOT_CONNECTED"
elif len(all_chains_tips_idx) == 0:
if len(self.main_splines.data.splines) > 1:
selection_type = "NO_SELECTION"
else:
# If the selection was not identified and there is only one stroke, there's no possibility to build a surface, so the script is interrupted.
self.report({'WARNING'}, "The selection isn't valid.")
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
self.cleanup_on_interruption()
self.stopping_errors = True
CoDEmanX
committed
return{'CANCELLED'}
else:
# The type of the selection was not identified, the script stops.
self.report({'WARNING'}, "The selection isn't valid.")
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
self.cleanup_on_interruption()
CoDEmanX
committed
CoDEmanX
committed
CoDEmanX
committed
#### If the selection type is TWO_NOT_CONNECTED and there is only one stroke, stop the script.
if selection_type == "TWO_NOT_CONNECTED" and len(self.main_splines.data.splines) == 1:
self.report({'WARNING'}, "At least two strokes are needed when there are two not connected selections.")
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
self.cleanup_on_interruption()
self.stopping_errors = True
CoDEmanX
committed
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.main_splines.name].select = True
bpy.context.scene.objects.active = bpy.context.scene.objects[self.main_splines.name]
CoDEmanX
committed
#### Enter editmode for the new curve (converted from grease pencil strokes), to smooth it out.
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.curve.smooth('INVOKE_REGION_WIN')
bpy.ops.curve.smooth('INVOKE_REGION_WIN')
bpy.ops.curve.smooth('INVOKE_REGION_WIN')
bpy.ops.curve.smooth('INVOKE_REGION_WIN')
bpy.ops.curve.smooth('INVOKE_REGION_WIN')
bpy.ops.curve.smooth('INVOKE_REGION_WIN')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
self.selection_U_exists = False
self.selection_U2_exists = False
self.selection_V_exists = False
self.selection_V2_exists = False
CoDEmanX
committed
self.selection_U_is_closed = False
self.selection_U2_is_closed = False
self.selection_V_is_closed = False
self.selection_V2_is_closed = False
CoDEmanX
committed
#### Define what vertices are at the tips of each selection and are not the middle-vertex.
if selection_type == "TWO_CONNECTED":
self.selection_U_exists = True
self.selection_V_exists = True
CoDEmanX
committed
closing_vert_U_idx = None
closing_vert_V_idx = None
closing_vert_U2_idx = None
closing_vert_V2_idx = None
CoDEmanX
committed
# Determine which selection is Selection-U and which is Selection-V.
points_A = []
points_B = []
points_first_stroke_tips = []
CoDEmanX
committed
points_A.append(self.main_object.matrix_world * self.main_object.data.vertices[verts_tips_parsed_idx[0]].co)
points_A.append(self.main_object.matrix_world * self.main_object.data.vertices[middle_vertex_idx].co)
CoDEmanX
committed
points_B.append(self.main_object.matrix_world * self.main_object.data.vertices[verts_tips_parsed_idx[1]].co)
points_B.append(self.main_object.matrix_world * self.main_object.data.vertices[middle_vertex_idx].co)
CoDEmanX
committed
points_first_stroke_tips.append(self.main_splines.data.splines[0].bezier_points[0].co)
points_first_stroke_tips.append(self.main_splines.data.splines[0].bezier_points[len(self.main_splines.data.splines[0].bezier_points) - 1].co)
CoDEmanX
committed
angle_A = self.orientation_difference(points_A, points_first_stroke_tips)
angle_B = self.orientation_difference(points_B, points_first_stroke_tips)
CoDEmanX
committed
if angle_A < angle_B:
first_vert_U_idx = verts_tips_parsed_idx[0]
first_vert_V_idx = verts_tips_parsed_idx[1]
else:
first_vert_U_idx = verts_tips_parsed_idx[1]
first_vert_V_idx = verts_tips_parsed_idx[0]
CoDEmanX
committed
elif selection_type == "SINGLE" or selection_type == "TWO_NOT_CONNECTED":
first_sketched_point_first_stroke_co = self.main_splines.data.splines[0].bezier_points[0].co
last_sketched_point_first_stroke_co = self.main_splines.data.splines[0].bezier_points[len(self.main_splines.data.splines[0].bezier_points) - 1].co
first_sketched_point_last_stroke_co = self.main_splines.data.splines[len(self.main_splines.data.splines) - 1].bezier_points[0].co
if len(self.main_splines.data.splines) > 1:
first_sketched_point_second_stroke_co = self.main_splines.data.splines[1].bezier_points[0].co
last_sketched_point_second_stroke_co = self.main_splines.data.splines[1].bezier_points[len(self.main_splines.data.splines[1].bezier_points) - 1].co
CoDEmanX
committed
single_unselected_neighbors = [] # Only the neighbors of the single unselected verts.
for verts_neig_idx in single_unselected_verts_and_neighbors:
single_unselected_neighbors.append(verts_neig_idx[1])
single_unselected_neighbors.append(verts_neig_idx[2])
CoDEmanX
committed
all_chains_tips_and_middle_vert = []
for v_idx in all_chains_tips_idx:
if v_idx not in single_unselected_neighbors:
all_chains_tips_and_middle_vert.append(v_idx)
CoDEmanX
committed
all_chains_tips_and_middle_vert += single_unselected_verts
CoDEmanX
committed
all_participating_verts = all_chains_tips_and_middle_vert + all_verts_idx
CoDEmanX
committed
# The tip of the selected vertices nearest to the first point of the first sketched stroke.
nearest_tip_to_first_st_first_pt_idx, shortest_distance_to_first_stroke = self.shortest_distance(self.main_object, first_sketched_point_first_stroke_co, all_chains_tips_and_middle_vert)
# If the nearest tip is not from a closed selection, get the opposite tip vertex index.
if nearest_tip_to_first_st_first_pt_idx not in single_unselected_verts or nearest_tip_to_first_st_first_pt_idx == middle_vertex_idx:
nearest_tip_to_first_st_first_pt_opposite_idx = self.opposite_tip(nearest_tip_to_first_st_first_pt_idx, verts_tips_same_chain_idx)
CoDEmanX
committed
# The tip of the selected vertices nearest to the last point of the first sketched stroke.
nearest_tip_to_first_st_last_pt_idx, temp_dist = self.shortest_distance(self.main_object, last_sketched_point_first_stroke_co, all_chains_tips_and_middle_vert)
CoDEmanX
committed
# The tip of the selected vertices nearest to the first point of the last sketched stroke.
nearest_tip_to_last_st_first_pt_idx, shortest_distance_to_last_stroke = self.shortest_distance(self.main_object, first_sketched_point_last_stroke_co, all_chains_tips_and_middle_vert)
CoDEmanX
committed
if len(self.main_splines.data.splines) > 1:
# The selected vertex nearest to the first point of the second sketched stroke. (This will be useful to determine the direction of the closed selection V when extruding along strokes)
nearest_vert_to_second_st_first_pt_idx, temp_dist = self.shortest_distance(self.main_object, first_sketched_point_second_stroke_co, all_verts_idx)
CoDEmanX
committed
# The selected vertex nearest to the first point of the second sketched stroke. (This will be useful to determine the direction of the closed selection V2 when extruding along strokes)
nearest_vert_to_second_st_last_pt_idx, temp_dist = self.shortest_distance(self.main_object, last_sketched_point_second_stroke_co, all_verts_idx)
CoDEmanX
committed
# Determine if the single selection will be treated as U or as V.
edges_sum = 0
for i in all_selected_edges_idx:
edges_sum += ((self.main_object.matrix_world * self.main_object.data.vertices[self.main_object.data.edges[i].vertices[0]].co) - (self.main_object.matrix_world * self.main_object.data.vertices[self.main_object.data.edges[i].vertices[1]].co)).length
CoDEmanX
committed
average_edge_length = edges_sum / len(all_selected_edges_idx)
CoDEmanX
committed
# Get shortest distance from the first point of the last stroke to any participating vertex.
temp_idx, shortest_distance_to_last_stroke = self.shortest_distance(self.main_object, first_sketched_point_last_stroke_co, all_participating_verts)
CoDEmanX
committed
if shortest_distance_to_first_stroke < average_edge_length / 4 and shortest_distance_to_last_stroke < average_edge_length and len(self.main_splines.data.splines) > 1: # If the beginning of the first stroke is near enough, and its orientation difference with the first edge of the nearest selection chain is not too high, interpret things as an "extrude along strokes" instead of "extrude through strokes"
self.selection_U_exists = False
self.selection_V_exists = True
if nearest_tip_to_first_st_first_pt_idx not in single_unselected_verts or nearest_tip_to_first_st_first_pt_idx == middle_vertex_idx: # If the first selection is not closed.
self.selection_V_is_closed = False
first_neighbor_V_idx = None
closing_vert_U_idx = None
closing_vert_U2_idx = None
closing_vert_V_idx = None
closing_vert_V2_idx = None
CoDEmanX
committed
first_vert_V_idx = nearest_tip_to_first_st_first_pt_idx
CoDEmanX
committed
if selection_type == "TWO_NOT_CONNECTED":
self.selection_V2_exists = True
CoDEmanX
committed
first_vert_V2_idx = nearest_tip_to_first_st_last_pt_idx
else:
self.selection_V_is_closed = True
closing_vert_V_idx = nearest_tip_to_first_st_first_pt_idx
CoDEmanX
committed
# Get the neighbors of the first (unselected) vert of the closed selection U.
vert_neighbors = []
for verts in single_unselected_verts_and_neighbors:
if verts[0] == nearest_tip_to_first_st_first_pt_idx:
vert_neighbors.append(verts[1])
vert_neighbors.append(verts[2])
break
CoDEmanX
committed
verts_V = self.get_ordered_verts(self.main_object, all_selected_edges_idx, all_verts_idx, vert_neighbors[0], middle_vertex_idx, None)
CoDEmanX
committed
for i in range(0, len(verts_V)):
if verts_V[i].index == nearest_vert_to_second_st_first_pt_idx:
if i >= len(verts_V) / 2: # If the vertex nearest to the first point of the second stroke is in the first half of the selected verts.
first_vert_V_idx = vert_neighbors[1]
break
else:
first_vert_V_idx = vert_neighbors[0]
break
CoDEmanX
committed
if selection_type == "TWO_NOT_CONNECTED":
self.selection_V2_exists = True
CoDEmanX
committed
if nearest_tip_to_first_st_last_pt_idx not in single_unselected_verts or nearest_tip_to_first_st_last_pt_idx == middle_vertex_idx: # If the second selection is not closed.
self.selection_V2_is_closed = False
first_neighbor_V2_idx = None
closing_vert_V2_idx = None
CoDEmanX
committed
first_vert_V2_idx = nearest_tip_to_first_st_last_pt_idx
CoDEmanX
committed
else:
self.selection_V2_is_closed = True
closing_vert_V2_idx = nearest_tip_to_first_st_last_pt_idx
CoDEmanX
committed
# Get the neighbors of the first (unselected) vert of the closed selection U.
vert_neighbors = []
for verts in single_unselected_verts_and_neighbors:
if verts[0] == nearest_tip_to_first_st_last_pt_idx:
vert_neighbors.append(verts[1])
vert_neighbors.append(verts[2])
break
CoDEmanX
committed
verts_V2 = self.get_ordered_verts(self.main_object, all_selected_edges_idx, all_verts_idx, vert_neighbors[0], middle_vertex_idx, None)
CoDEmanX
committed
for i in range(0, len(verts_V2)):
if verts_V2[i].index == nearest_vert_to_second_st_last_pt_idx:
if i >= len(verts_V2) / 2: # If the vertex nearest to the first point of the second stroke is in the first half of the selected verts.
first_vert_V2_idx = vert_neighbors[1]
break
else:
first_vert_V2_idx = vert_neighbors[0]
break
CoDEmanX
committed
else:
self.selection_V2_exists = False
CoDEmanX
committed
else:
self.selection_U_exists = True
self.selection_V_exists = False
if nearest_tip_to_first_st_first_pt_idx not in single_unselected_verts or nearest_tip_to_first_st_first_pt_idx == middle_vertex_idx: # If the first selection is not closed.
self.selection_U_is_closed = False
first_neighbor_U_idx = None
closing_vert_U_idx = None
CoDEmanX
committed
points_tips = []
points_tips.append(self.main_object.matrix_world * self.main_object.data.vertices[nearest_tip_to_first_st_first_pt_idx].co)
points_tips.append(self.main_object.matrix_world * self.main_object.data.vertices[nearest_tip_to_first_st_first_pt_opposite_idx].co)
CoDEmanX
committed
points_first_stroke_tips = []
points_first_stroke_tips.append(self.main_splines.data.splines[0].bezier_points[0].co)
points_first_stroke_tips.append(self.main_splines.data.splines[0].bezier_points[len(self.main_splines.data.splines[0].bezier_points) - 1].co)
CoDEmanX
committed
vec_A = points_tips[0] - points_tips[1]
vec_B = points_first_stroke_tips[0] - points_first_stroke_tips[1]
CoDEmanX
committed
# Compare the direction of the selection and the first grease pencil stroke to determine which is the "first" vertex of the selection.
if vec_A.dot(vec_B) < 0:
first_vert_U_idx = nearest_tip_to_first_st_first_pt_opposite_idx
else:
first_vert_U_idx = nearest_tip_to_first_st_first_pt_idx
CoDEmanX
committed
else:
self.selection_U_is_closed = True
closing_vert_U_idx = nearest_tip_to_first_st_first_pt_idx
CoDEmanX
committed
# Get the neighbors of the first (unselected) vert of the closed selection U.
vert_neighbors = []
for verts in single_unselected_verts_and_neighbors:
if verts[0] == nearest_tip_to_first_st_first_pt_idx:
vert_neighbors.append(verts[1])
vert_neighbors.append(verts[2])
break
CoDEmanX
committed
points_first_and_neighbor = []
points_first_and_neighbor.append(self.main_object.matrix_world * self.main_object.data.vertices[nearest_tip_to_first_st_first_pt_idx].co)
points_first_and_neighbor.append(self.main_object.matrix_world * self.main_object.data.vertices[vert_neighbors[0]].co)
CoDEmanX
committed
points_first_stroke_tips = []
points_first_stroke_tips.append(self.main_splines.data.splines[0].bezier_points[0].co)
points_first_stroke_tips.append(self.main_splines.data.splines[0].bezier_points[1].co)
CoDEmanX
committed
vec_A = points_first_and_neighbor[0] - points_first_and_neighbor[1]
vec_B = points_first_stroke_tips[0] - points_first_stroke_tips[1]
CoDEmanX
committed
# Compare the direction of the selection and the first grease pencil stroke to determine which is the vertex neighbor to the first vertex (unselected) of the closed selection. This will determine the direction of the closed selection.
if vec_A.dot(vec_B) < 0:
first_vert_U_idx = vert_neighbors[1]
else:
first_vert_U_idx = vert_neighbors[0]
CoDEmanX
committed
if selection_type == "TWO_NOT_CONNECTED":
self.selection_U2_exists = True
CoDEmanX
committed
if nearest_tip_to_last_st_first_pt_idx not in single_unselected_verts or nearest_tip_to_last_st_first_pt_idx == middle_vertex_idx: # If the second selection is not closed.
self.selection_U2_is_closed = False
first_neighbor_U2_idx = None
closing_vert_U2_idx = None
CoDEmanX
committed
first_vert_U2_idx = nearest_tip_to_last_st_first_pt_idx
CoDEmanX
committed
else:
self.selection_U2_is_closed = True
closing_vert_U2_idx = nearest_tip_to_last_st_first_pt_idx
CoDEmanX
committed
# Get the neighbors of the first (unselected) vert of the closed selection U.
vert_neighbors = []
for verts in single_unselected_verts_and_neighbors:
if verts[0] == nearest_tip_to_last_st_first_pt_idx:
vert_neighbors.append(verts[1])
vert_neighbors.append(verts[2])
break
CoDEmanX
committed
points_first_and_neighbor = []
points_first_and_neighbor.append(self.main_object.matrix_world * self.main_object.data.vertices[nearest_tip_to_last_st_first_pt_idx].co)
points_first_and_neighbor.append(self.main_object.matrix_world * self.main_object.data.vertices[vert_neighbors[0]].co)
CoDEmanX
committed
points_last_stroke_tips = []
points_last_stroke_tips.append(self.main_splines.data.splines[len(self.main_splines.data.splines) - 1].bezier_points[0].co)
points_last_stroke_tips.append(self.main_splines.data.splines[len(self.main_splines.data.splines) - 1].bezier_points[1].co)
CoDEmanX
committed
vec_A = points_first_and_neighbor[0] - points_first_and_neighbor[1]
vec_B = points_last_stroke_tips[0] - points_last_stroke_tips[1]
CoDEmanX
committed
# Compare the direction of the selection and the last grease pencil stroke to determine which is the vertex neighbor to the first vertex (unselected) of the closed selection. This will determine the direction of the closed selection.
if vec_A.dot(vec_B) < 0:
first_vert_U2_idx = vert_neighbors[1]
else:
first_vert_U2_idx = vert_neighbors[0]
CoDEmanX
committed
else:
self.selection_U2_exists = False
CoDEmanX
committed
elif selection_type == "NO_SELECTION":
self.selection_U_exists = False
self.selection_V_exists = False
CoDEmanX
committed
#### Get an ordered list of the vertices of Selection-U.
verts_ordered_U = []
if self.selection_U_exists:
verts_ordered_U = self.get_ordered_verts(self.main_object, all_selected_edges_idx, all_verts_idx, first_vert_U_idx, middle_vertex_idx, closing_vert_U_idx)
verts_ordered_U_indices = [x.index for x in verts_ordered_U]
CoDEmanX
committed
#### Get an ordered list of the vertices of Selection-U2.
verts_ordered_U2 = []
if self.selection_U2_exists:
verts_ordered_U2 = self.get_ordered_verts(self.main_object, all_selected_edges_idx, all_verts_idx, first_vert_U2_idx, middle_vertex_idx, closing_vert_U2_idx)
verts_ordered_U2_indices = [x.index for x in verts_ordered_U2]
CoDEmanX
committed
#### Get an ordered list of the vertices of Selection-V.
verts_ordered_V = []
if self.selection_V_exists:
verts_ordered_V = self.get_ordered_verts(self.main_object, all_selected_edges_idx, all_verts_idx, first_vert_V_idx, middle_vertex_idx, closing_vert_V_idx)
verts_ordered_V_indices = [x.index for x in verts_ordered_V]
CoDEmanX
committed
#### Get an ordered list of the vertices of Selection-V2.
verts_ordered_V2 = []
if self.selection_V2_exists:
verts_ordered_V2 = self.get_ordered_verts(self.main_object, all_selected_edges_idx, all_verts_idx, first_vert_V2_idx, middle_vertex_idx, closing_vert_V2_idx)
verts_ordered_V2_indices = [x.index for x in verts_ordered_V2]
CoDEmanX
committed
#### Check if when there are two-not-connected selections both have the same number of verts. If not terminate the script.
if ((self.selection_U2_exists and len(verts_ordered_U) != len(verts_ordered_U2)) or (self.selection_V2_exists and len(verts_ordered_V) != len(verts_ordered_V2))):
# Display a warning.
self.report({'WARNING'}, "Both selections must have the same number of edges")
CoDEmanX
committed
CoDEmanX
committed
CoDEmanX
committed
CoDEmanX
committed
#### Calculate edges U proportions.
CoDEmanX
committed
# Sum selected edges U lengths.
edges_lengths_U = []
edges_lengths_sum_U = 0
CoDEmanX
committed
if self.selection_U_exists:
edges_lengths_U, edges_lengths_sum_U = self.get_chain_length(self.main_object, verts_ordered_U)
CoDEmanX
committed
if self.selection_U2_exists:
edges_lengths_U2, edges_lengths_sum_U2 = self.get_chain_length(self.main_object, verts_ordered_U2)
CoDEmanX
committed
# Sum selected edges V lengths.
edges_lengths_V = []
edges_lengths_sum_V = 0
CoDEmanX
committed
if self.selection_V_exists:
edges_lengths_V, edges_lengths_sum_V = self.get_chain_length(self.main_object, verts_ordered_V)
CoDEmanX
committed
if self.selection_V2_exists:
edges_lengths_V2, edges_lengths_sum_V2 = self.get_chain_length(self.main_object, verts_ordered_V2)
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.curve.subdivide('INVOKE_REGION_WIN', number_cuts = bpy.context.scene.SURFSK_precision)
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
# Proportions U.
edges_proportions_U = []
edges_proportions_U = self.get_edges_proportions(edges_lengths_U, edges_lengths_sum_U, self.selection_U_exists, self.edges_U)
verts_count_U = len(edges_proportions_U) + 1
CoDEmanX
committed
if self.selection_U2_exists:
edges_proportions_U2 = []
edges_proportions_U2 = self.get_edges_proportions(edges_lengths_U2, edges_lengths_sum_U2, self.selection_U2_exists, self.edges_V)
verts_count_U2 = len(edges_proportions_U2) + 1
CoDEmanX
committed
# Proportions V.
edges_proportions_V = []
edges_proportions_V = self.get_edges_proportions(edges_lengths_V, edges_lengths_sum_V, self.selection_V_exists, self.edges_V)
verts_count_V = len(edges_proportions_V) + 1
CoDEmanX
committed
if self.selection_V2_exists:
edges_proportions_V2 = []
edges_proportions_V2 = self.get_edges_proportions(edges_lengths_V2, edges_lengths_sum_V2, self.selection_V2_exists, self.edges_V)
verts_count_V2 = len(edges_proportions_V2) + 1
CoDEmanX
committed
#### Cyclic Follow: simplify sketched curves, make them Cyclic, and complete the actual sketched curves with a "closing segment".
if self.cyclic_follow and not self.selection_V_exists and not ((self.selection_U_exists and not self.selection_U_is_closed) or (self.selection_U2_exists and not self.selection_U2_is_closed)):
simplified_spline_coords = []
simplified_curve = []
ob_simplified_curve = []
splines_first_v_co = []
for i in range(len(self.main_splines.data.splines)):
# Create a curve object for the actual spline "cyclic extension".
simplified_curve.append(bpy.data.curves.new('SURFSKIO_simpl_crv', 'CURVE'))
ob_simplified_curve.append(bpy.data.objects.new('SURFSKIO_simpl_crv', simplified_curve[i]))
bpy.context.scene.objects.link(ob_simplified_curve[i])
CoDEmanX
committed
simplified_curve[i].dimensions = "3D"
CoDEmanX
committed
spline_coords = []
for bp in self.main_splines.data.splines[i].bezier_points:
spline_coords.append(bp.co)
CoDEmanX
committed
# Simplification.
simplified_spline_coords.append(self.simplify_spline(spline_coords, 5))
CoDEmanX
committed
# Get the coordinates of the first vert of the actual spline.
splines_first_v_co.append(simplified_spline_coords[i][0])
CoDEmanX
committed
# Generate the spline.
spline = simplified_curve[i].splines.new('BEZIER')
spline.bezier_points.add(len(simplified_spline_coords[i]) - 1) # less one because one point is added when the spline is created.
for p in range(0, len(simplified_spline_coords[i])):
spline.bezier_points[p].co = simplified_spline_coords[i][p]
CoDEmanX
committed
CoDEmanX
committed
spline_bp_count = len(spline.bezier_points)
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[ob_simplified_curve[i].name].select = True
bpy.context.scene.objects.active = bpy.context.scene.objects[ob_simplified_curve[i].name]
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='SELECT')
bpy.ops.curve.handle_type_set('INVOKE_REGION_WIN', type='AUTOMATIC')
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
# Select the "closing segment", and subdivide it.
ob_simplified_curve[i].data.splines[0].bezier_points[0].select_control_point = True
ob_simplified_curve[i].data.splines[0].bezier_points[0].select_left_handle = True
ob_simplified_curve[i].data.splines[0].bezier_points[0].select_right_handle = True
CoDEmanX
committed
ob_simplified_curve[i].data.splines[0].bezier_points[spline_bp_count - 1].select_control_point = True
ob_simplified_curve[i].data.splines[0].bezier_points[spline_bp_count - 1].select_left_handle = True
ob_simplified_curve[i].data.splines[0].bezier_points[spline_bp_count - 1].select_right_handle = True
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
segments = sqrt((ob_simplified_curve[i].data.splines[0].bezier_points[0].co - ob_simplified_curve[i].data.splines[0].bezier_points[spline_bp_count - 1].co).length / self.average_gp_segment_length)
for t in range(2):
bpy.ops.curve.subdivide('INVOKE_REGION_WIN', number_cuts = segments)
CoDEmanX
committed
# Delete the other vertices and make it non-cyclic to keep only the needed verts of the "closing segment".
bpy.ops.curve.select_all(action = 'INVERT')
ob_simplified_curve[i].data.splines[0].use_cyclic_u = False
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
# Add the points of the "closing segment" to the original curve from grease pencil stroke.
first_new_index = len(self.main_splines.data.splines[i].bezier_points)
self.main_splines.data.splines[i].bezier_points.add(len(ob_simplified_curve[i].data.splines[0].bezier_points) - 1)
for t in range(1, len(ob_simplified_curve[i].data.splines[0].bezier_points)):
self.main_splines.data.splines[i].bezier_points[t - 1 + first_new_index].co = ob_simplified_curve[i].data.splines[0].bezier_points[t].co
CoDEmanX
committed
# Delete the temporal curve.
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[ob_simplified_curve[i].name].select = True
bpy.context.scene.objects.active = bpy.context.scene.objects[ob_simplified_curve[i].name]
CoDEmanX
committed
CoDEmanX
committed
#### Get the coords of the points distributed along the sketched strokes, with proportions-U of the first selection.
pts_on_strokes_with_proportions_U = self.distribute_pts(self.main_splines.data.splines, edges_proportions_U)
CoDEmanX
committed
CoDEmanX
committed
if self.selection_U2_exists:
# Initialize the multidimensional list with the proportions of all the segments.
proportions_loops_crossing_strokes = []
for i in range(len(pts_on_strokes_with_proportions_U)):
proportions_loops_crossing_strokes.append([])
CoDEmanX
committed
for t in range(len(pts_on_strokes_with_proportions_U[0])):
proportions_loops_crossing_strokes[i].append(None)
CoDEmanX
committed
# Calculate the proportions of each segment of the loops-U from pts_on_strokes_with_proportions_U.
for lp in range(len(pts_on_strokes_with_proportions_U[0])):
loop_segments_lengths = []
CoDEmanX
committed
for st in range(len(pts_on_strokes_with_proportions_U)):
if st == 0: # When on the first stroke, add the segment from the selection to the dirst stroke.
loop_segments_lengths.append(((self.main_object.matrix_world * verts_ordered_U[lp].co) - pts_on_strokes_with_proportions_U[0][lp]).length)
CoDEmanX
committed
if st != len(pts_on_strokes_with_proportions_U) - 1: # For all strokes except for the last, calculate the distance from the actual stroke to the next.
loop_segments_lengths.append((pts_on_strokes_with_proportions_U[st][lp] - pts_on_strokes_with_proportions_U[st + 1][lp]).length)
CoDEmanX
committed
if st == len(pts_on_strokes_with_proportions_U) - 1: # When on the last stroke, add the segments from the last stroke to the second selection.
loop_segments_lengths.append((pts_on_strokes_with_proportions_U[st][lp] - (self.main_object.matrix_world * verts_ordered_U2[lp].co)).length)
CoDEmanX
committed
# Calculate full loop length.
loop_seg_lengths_sum = 0
for i in range(len(loop_segments_lengths)):
loop_seg_lengths_sum += loop_segments_lengths[i]
CoDEmanX
committed
# Fill the multidimensional list with the proportions of all the segments.
for st in range(len(pts_on_strokes_with_proportions_U)):
proportions_loops_crossing_strokes[st][lp] = loop_segments_lengths[st] / loop_seg_lengths_sum
CoDEmanX
committed
# Calculate proportions for each stroke.
for st in range(len(pts_on_strokes_with_proportions_U)):
actual_stroke_spline = []
actual_stroke_spline.append(self.main_splines.data.splines[st]) # Needs to be a list for the "distribute_pts" method.
CoDEmanX
committed
# Calculate the proportions for the actual stroke.
actual_edges_proportions_U = []
for i in range(len(edges_proportions_U)):
proportions_sum = 0
CoDEmanX
committed
# Sum the proportions of this loop up to the actual.
for t in range(0, st + 1):
proportions_sum += proportions_loops_crossing_strokes[t][i]
CoDEmanX
committed
actual_edges_proportions_U.append(edges_proportions_U[i] - ((edges_proportions_U[i] - edges_proportions_U2[i]) * proportions_sum)) # i + 1, because proportions_loops_crossing_strokes refers to loops, and the proportions refer to edges, so we start at the element 1 of proportions_loops_crossing_strokes instead of element 0.
CoDEmanX
committed
points_actual_spline = self.distribute_pts(actual_stroke_spline, actual_edges_proportions_U)
sketched_splines_parsed.append(points_actual_spline[0])
CoDEmanX
committed
else:
sketched_splines_parsed = pts_on_strokes_with_proportions_U
CoDEmanX
committed
#### If the selection type is "TWO_NOT_CONNECTED" replace the points of the last spline with the points in the "target" selection.
if selection_type == "TWO_NOT_CONNECTED":
if self.selection_U2_exists:
for i in range(0, len(sketched_splines_parsed[len(sketched_splines_parsed) - 1])):
sketched_splines_parsed[len(sketched_splines_parsed) - 1][i] = self.main_object.matrix_world * verts_ordered_U2[i].co
CoDEmanX
committed
#### Create temporary curves along the "control-points" found on the sketched curves and the mesh selection.
mesh_ctrl_pts_name = "SURFSKIO_ctrl_pts"
me = bpy.data.meshes.new(mesh_ctrl_pts_name)
ob_ctrl_pts = bpy.data.objects.new(mesh_ctrl_pts_name, me)
ob_ctrl_pts.data = me
bpy.context.scene.objects.link(ob_ctrl_pts)
CoDEmanX
committed
cyclic_loops_U = []
first_verts = []
second_verts = []
last_verts = []
for i in range(0, verts_count_U):
vert_num_in_spline = 1
CoDEmanX
committed
if self.selection_U_exists:
ob_ctrl_pts.data.vertices.add(1)
last_v = ob_ctrl_pts.data.vertices[len(ob_ctrl_pts.data.vertices) - 1]
last_v.co = self.main_object.matrix_world * verts_ordered_U[i].co
CoDEmanX
committed
CoDEmanX
committed
for t in range(0, len(sketched_splines_parsed)):
ob_ctrl_pts.data.vertices.add(1)
v = ob_ctrl_pts.data.vertices[len(ob_ctrl_pts.data.vertices) - 1]
v.co = sketched_splines_parsed[t][i]
CoDEmanX
committed
if vert_num_in_spline > 1:
ob_ctrl_pts.data.edges.add(1)
ob_ctrl_pts.data.edges[len(ob_ctrl_pts.data.edges) - 1].vertices[0] = len(ob_ctrl_pts.data.vertices) - 2
ob_ctrl_pts.data.edges[len(ob_ctrl_pts.data.edges) - 1].vertices[1] = len(ob_ctrl_pts.data.vertices) - 1
CoDEmanX
committed
if t == 0:
first_verts.append(v.index)
CoDEmanX
committed
if t == 1:
second_verts.append(v.index)
CoDEmanX
committed
if t == len(sketched_splines_parsed) - 1:
last_verts.append(v.index)
CoDEmanX
committed
CoDEmanX
committed
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[ob_ctrl_pts.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[ob_ctrl_pts.name]
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
#### Determine which loops-U will be "Cyclic".
for i in range(0, len(first_verts)):
if self.automatic_join and not self.cyclic_cross and selection_type != "TWO_CONNECTED" and len(self.main_splines.data.splines) >= 3: # When there is Cyclic Cross there is no need of Automatic Join, (and there are at least three strokes).
v = ob_ctrl_pts.data.vertices
CoDEmanX
committed
first_point_co = v[first_verts[i]].co
second_point_co = v[second_verts[i]].co
last_point_co = v[last_verts[i]].co
CoDEmanX
committed
# Coordinates of the point in the center of both the first and last verts.
verts_center_co = [(first_point_co[0] + last_point_co[0]) / 2, (first_point_co[1] + last_point_co[1]) / 2, (first_point_co[2] + last_point_co[2]) / 2]
CoDEmanX
committed
vec_A = second_point_co - first_point_co
vec_B = second_point_co - mathutils.Vector(verts_center_co)
CoDEmanX
committed
# Calculate the length of the first segment of the loop, and the length it would have after moving the first vert to the middle position between first and last.
length_original = (second_point_co - first_point_co).length
length_target = (second_point_co - mathutils.Vector(verts_center_co)).length
CoDEmanX
committed
angle = vec_A.angle(vec_B) / math.pi
CoDEmanX
committed
if length_target <= length_original * 1.03 * self.join_stretch_factor and angle <= 0.008 * self.join_stretch_factor and not self.selection_U_exists: # If the target length doesn't stretch too much, and the its angle doesn't change to much either.
cyclic_loops_U.append(True)
CoDEmanX
committed
# Move the first vert to the center coordinates.
ob_ctrl_pts.data.vertices[first_verts[i]].co = verts_center_co
CoDEmanX
committed
# Select the last verts from Cyclic loops, for later deletion all at once.
v[last_verts[i]].select = True
CoDEmanX
committed
else:
cyclic_loops_U.append(False)
CoDEmanX
committed
else:
if self.cyclic_cross and not self.selection_U_exists and not ((self.selection_V_exists and not self.selection_V_is_closed) or (self.selection_V2_exists and not self.selection_V2_is_closed)): # If "Cyclic Cross" is active then "all" crossing curves become cyclic.
cyclic_loops_U.append(True)
else:
cyclic_loops_U.append(False)
CoDEmanX
committed
# The cyclic_loops_U list needs to be reversed.
cyclic_loops_U.reverse()
CoDEmanX
committed
# Delete the previously selected (last_)verts.
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.mesh.delete('INVOKE_REGION_WIN', type='VERT')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
# Create curves from control points.
bpy.ops.object.convert('INVOKE_REGION_WIN', target='CURVE', keep_original=False)
ob_curves_surf = bpy.context.scene.objects.active
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.curve.spline_type_set('INVOKE_REGION_WIN', type='BEZIER')
bpy.ops.curve.handle_type_set('INVOKE_REGION_WIN', type='AUTOMATIC')
CoDEmanX
committed
# Make Cyclic the splines designated as Cyclic.
for i in range(0, len(cyclic_loops_U)):
ob_curves_surf.data.splines[i].use_cyclic_u = cyclic_loops_U[i]
CoDEmanX
committed
#### Get the coords of all points on first loop-U, for later comparison with its subdivided version, to know which points of the loops-U are crossed by the original strokes. The indices wiil be the same for the other loops-U.
if self.loops_on_strokes:
coords_loops_U_control_points = []
for p in ob_ctrl_pts.data.splines[0].bezier_points:
coords_loops_U_control_points.append(["%.4f" % p.co[0], "%.4f" % p.co[1], "%.4f" % p.co[2]])
CoDEmanX
committed
tuple(coords_loops_U_control_points)
CoDEmanX
committed
# Calculate number of edges-V in case option "Loops on strokes" is active or inactive.
if self.loops_on_strokes and not self.selection_V_exists:
edges_V_count = len(self.main_splines.data.splines) * self.edges_V
else:
edges_V_count = len(edges_proportions_V)
CoDEmanX
committed
# The Follow precision will vary depending on the number of Follow face-loops.
precision_multiplier = round(2 + (edges_V_count / 15))
CoDEmanX
committed
curve_cuts = bpy.context.scene.SURFSK_precision * precision_multiplier
CoDEmanX
committed
# Subdivide the curves.
bpy.ops.curve.subdivide('INVOKE_REGION_WIN', number_cuts = curve_cuts)
CoDEmanX
committed
# The verts position shifting that happens with splines subdivision. For later reorder splines points.
verts_position_shift = curve_cuts + 1
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
# Reorder coordinates of the points of each spline to put the first point of the spline starting at the position it was the first point before sudividing the curve. And make a new curve object per spline (to handle memory better later).
splines_U_objects = []
for i in range(len(ob_curves_surf.data.splines)):
spline_U_curve = bpy.data.curves.new('SURFSKIO_spline_U_' + str(i), 'CURVE')
ob_spline_U = bpy.data.objects.new('SURFSKIO_spline_U_' + str(i), spline_U_curve)
bpy.context.scene.objects.link(ob_spline_U)
CoDEmanX
committed
spline_U_curve.dimensions = "3D"
CoDEmanX
committed
# Add points to the spline in the new curve object.
ob_spline_U.data.splines.new('BEZIER')
for t in range(len(ob_curves_surf.data.splines[i].bezier_points)):
if cyclic_loops_U[i] == True and not self.selection_U_exists: # If the loop is cyclic.
if t + verts_position_shift <= len(ob_curves_surf.data.splines[i].bezier_points) - 1:
point_index = t + verts_position_shift
else:
point_index = t + verts_position_shift - len(ob_curves_surf.data.splines[i].bezier_points)
else:
point_index = t
CoDEmanX
committed
if t > 0: # to avoid adding the first point since it's added when the spline is created.
ob_spline_U.data.splines[0].bezier_points.add(1)
ob_spline_U.data.splines[0].bezier_points[t].co = ob_curves_surf.data.splines[i].bezier_points[point_index].co
CoDEmanX
committed
if cyclic_loops_U[i] == True and not self.selection_U_exists: # If the loop is cyclic.
# Add a last point at the same location as the first one.
ob_spline_U.data.splines[0].bezier_points.add(1)
ob_spline_U.data.splines[0].bezier_points[len(ob_spline_U.data.splines[0].bezier_points) - 1].co = ob_spline_U.data.splines[0].bezier_points[0].co
else:
ob_spline_U.data.splines[0].use_cyclic_u = False
CoDEmanX
committed
splines_U_objects.append(ob_spline_U)
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[ob_spline_U.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[ob_spline_U.name]
CoDEmanX
committed
#### When option "Loops on strokes" is active each "Cross" loop will have its own proportions according to where the original strokes "touch" them.
if self.loops_on_strokes:
# Get the indices of points where the original strokes "touch" loops-U.
points_U_crossed_by_strokes = []
for i in range(len(splines_U_objects[0].data.splines[0].bezier_points)):
bp = splines_U_objects[0].data.splines[0].bezier_points[i]
if ["%.4f" % bp.co[0], "%.4f" % bp.co[1], "%.4f" % bp.co[2]] in coords_loops_U_control_points:
points_U_crossed_by_strokes.append(i)
CoDEmanX
committed
# Make a dictionary with the number of the edge, in the selected chain V, corresponding to each stroke.
edge_order_number_for_splines = {}
if self.selection_V_exists:
# For two-connected selections add a first hypothetic stroke at the begining.
if selection_type == "TWO_CONNECTED":
edge_order_number_for_splines[0] = 0
CoDEmanX
committed
for i in range(len(self.main_splines.data.splines)):
sp = self.main_splines.data.splines[i]
v_idx, dist_temp = self.shortest_distance(self.main_object, sp.bezier_points[0].co, verts_ordered_V_indices)
CoDEmanX
committed
edge_idx_in_chain = verts_ordered_V_indices.index(v_idx) # Get the position (edges count) of the vert v_idx in the selected chain V.
CoDEmanX
committed
# For two-connected selections the strokes go after the hypothetic stroke added before, so the index adds one per spline.
if selection_type == "TWO_CONNECTED":
spline_number = i + 1
else:
spline_number = i
CoDEmanX
committed
edge_order_number_for_splines[spline_number] = edge_idx_in_chain
CoDEmanX
committed
# Get the first and last verts indices for later comparison.
if i == 0:
first_v_idx = v_idx
elif i == len(self.main_splines.data.splines) - 1:
last_v_idx = v_idx
CoDEmanX
committed
if self.selection_V_is_closed:
# If there is no last stroke on the last vertex (same as first vertex), add a hypothetic spline at last vert order.
if first_v_idx != last_v_idx:
edge_order_number_for_splines[(len(self.main_splines.data.splines) - 1) + 1] = len(verts_ordered_V_indices) - 1
else:
if self.cyclic_cross:
edge_order_number_for_splines[len(self.main_splines.data.splines) - 1] = len(verts_ordered_V_indices) - 2
edge_order_number_for_splines[(len(self.main_splines.data.splines) - 1) + 1] = len(verts_ordered_V_indices) - 1
else:
edge_order_number_for_splines[len(self.main_splines.data.splines) - 1] = len(verts_ordered_V_indices) - 1
CoDEmanX
committed
#### Get the coords of the points distributed along the "crossing curves", with appropriate proportions-V.
surface_splines_parsed = []
for i in range(len(splines_U_objects)):
sp_ob = splines_U_objects[i]
# If "Loops on strokes" option is active, calculate the proportions for each loop-U.
if self.loops_on_strokes:
# Segments distances from stroke to stroke.
dist = 0
full_dist = 0
segments_distances = []
for t in range(len(sp_ob.data.splines[0].bezier_points)):
bp = sp_ob.data.splines[0].bezier_points[t]
CoDEmanX
committed
if t == 0:
last_p = bp.co
else:
actual_p = bp.co
dist += (last_p - actual_p).length
CoDEmanX
committed
if t in points_U_crossed_by_strokes:
segments_distances.append(dist)
full_dist += dist
CoDEmanX
committed
CoDEmanX
committed
CoDEmanX
committed
# Calculate Proportions.
used_edges_proportions_V = []
for t in range(len(segments_distances)):
if self.selection_V_exists:
if t == 0:
order_number_last_stroke = 0
CoDEmanX
committed
segment_edges_length_V = 0
segment_edges_length_V2 = 0
for order in range(order_number_last_stroke, edge_order_number_for_splines[t + 1]):
segment_edges_length_V += edges_lengths_V[order]
if self.selection_V2_exists:
segment_edges_length_V2 += edges_lengths_V2[order]
CoDEmanX
committed
for order in range(order_number_last_stroke, edge_order_number_for_splines[t + 1]):
# Calculate each "sub-segment" (the ones between each stroke) length.
if self.selection_V2_exists:
proportion_sub_seg = (edges_lengths_V2[order] - ((edges_lengths_V2[order] - edges_lengths_V[order]) / len(splines_U_objects) * i)) / (segment_edges_length_V2 - (segment_edges_length_V2 - segment_edges_length_V) / len(splines_U_objects) * i)
sub_seg_dist = segments_distances[t] * proportion_sub_seg
else:
proportion_sub_seg = edges_lengths_V[order] / segment_edges_length_V
sub_seg_dist = segments_distances[t] * proportion_sub_seg
CoDEmanX
committed
used_edges_proportions_V.append(sub_seg_dist / full_dist)
CoDEmanX
committed
order_number_last_stroke = edge_order_number_for_splines[t + 1]
CoDEmanX
committed
else:
for c in range(self.edges_V):
# Calculate each "sub-segment" (the ones between each stroke) length.
CoDEmanX
committed
sub_seg_dist = segments_distances[t] / self.edges_V
used_edges_proportions_V.append(sub_seg_dist / full_dist)
CoDEmanX
committed
actual_spline = self.distribute_pts(sp_ob.data.splines, used_edges_proportions_V)
surface_splines_parsed.append(actual_spline[0])
CoDEmanX
committed
else:
if self.selection_V2_exists:
used_edges_proportions_V = []
for p in range(len(edges_proportions_V)):
used_edges_proportions_V.append(edges_proportions_V2[p] - ((edges_proportions_V2[p] - edges_proportions_V[p]) / len(splines_U_objects) * i))
else:
used_edges_proportions_V = edges_proportions_V
CoDEmanX
committed
actual_spline = self.distribute_pts(sp_ob.data.splines, used_edges_proportions_V)
surface_splines_parsed.append(actual_spline[0])
CoDEmanX
committed
# Set the verts of the first and last splines to the locations of the respective verts in the selections.
if self.selection_V_exists:
for i in range(0, len(surface_splines_parsed[0])):
surface_splines_parsed[len(surface_splines_parsed) - 1][i] = self.main_object.matrix_world * verts_ordered_V[i].co
CoDEmanX
committed
if selection_type == "TWO_NOT_CONNECTED":
if self.selection_V2_exists:
for i in range(0, len(surface_splines_parsed[0])):
surface_splines_parsed[0][i] = self.main_object.matrix_world * verts_ordered_V2[i].co
CoDEmanX
committed
# When "Automatic join" option is active (and the selection type is not "TWO_CONNECTED"), merge the verts of the tips of the loops when they are "near enough".
if self.automatic_join and selection_type != "TWO_CONNECTED":
#### Join the tips of "Follow" loops that are near enough and must be "closed".
if not self.selection_V_exists and len(edges_proportions_U) >= 3:
for i in range(len(surface_splines_parsed[0])):
sp = surface_splines_parsed
loop_segment_dist = (sp[0][i] - sp[1][i]).length
full_loop_dist = loop_segment_dist * self.edges_U
CoDEmanX
committed
verts_middle_position_co = [(sp[0][i][0] + sp[len(sp) - 1][i][0]) / 2, (sp[0][i][1] + sp[len(sp) - 1][i][1]) / 2, (sp[0][i][2] + sp[len(sp) - 1][i][2]) / 2]
CoDEmanX
committed
points_original = []
points_original.append(sp[1][i])
points_original.append(sp[0][i])
CoDEmanX
committed
points_target = []
points_target.append(sp[1][i])
points_target.append(mathutils.Vector(verts_middle_position_co))
CoDEmanX
committed
vec_A = points_original[0] - points_original[1]
vec_B = points_target[0] - points_target[1]
CoDEmanX
committed
angle = vec_A.angle(vec_B) / math.pi
CoDEmanX
committed
edge_new_length = (mathutils.Vector(verts_middle_position_co) - sp[1][i]).length
CoDEmanX
committed
if edge_new_length <= loop_segment_dist * 1.5 * self.join_stretch_factor and angle < 0.25 * self.join_stretch_factor: # If after moving the verts to the middle point, the segment doesn't stretch too much.
if not (self.selection_U_exists and i == 0) and not (self.selection_U2_exists and i == len(surface_splines_parsed[0]) - 1): # Avoid joining when the actual loop must be merged with the original mesh.
# Change the coords of both verts to the middle position.
surface_splines_parsed[0][i] = verts_middle_position_co
surface_splines_parsed[len(surface_splines_parsed) - 1][i] = verts_middle_position_co
CoDEmanX
committed
#### Delete object with control points and object from grease pencil convertion.
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[ob_ctrl_pts.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[ob_ctrl_pts.name]
CoDEmanX
committed
CoDEmanX
committed
for sp_ob in splines_U_objects:
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[sp_ob.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[sp_ob.name]
CoDEmanX
committed
CoDEmanX
committed
CoDEmanX
committed
# Get all verts coords.
all_surface_verts_co = []
for i in range(0, len(surface_splines_parsed)):
# Get coords of all verts and make a list with them
for pt_co in surface_splines_parsed[i]:
all_surface_verts_co.append(pt_co)
CoDEmanX
committed
# Define verts for each face.
all_surface_faces = []
for i in range(0, len(all_surface_verts_co) - len(surface_splines_parsed[0])):
if ((i + 1) / len(surface_splines_parsed[0]) != int((i + 1) / len(surface_splines_parsed[0]))):
all_surface_faces.append([i+1, i , i + len(surface_splines_parsed[0]), i + len(surface_splines_parsed[0]) + 1])
CoDEmanX
committed
# Build the mesh.
surf_me_name = "SURFSKIO_surface"
me_surf = bpy.data.meshes.new(surf_me_name)
CoDEmanX
committed
me_surf.from_pydata(all_surface_verts_co, [], all_surface_faces)
CoDEmanX
committed
CoDEmanX
committed
ob_surface = bpy.data.objects.new(surf_me_name, me_surf)
bpy.context.scene.objects.link(ob_surface)
CoDEmanX
committed
# Select all the "unselected but participating" verts, from closed selection or double selections with middle-vertex, for later join with remove doubles.
for v_idx in single_unselected_verts:
self.main_object.data.vertices[v_idx].select = True
CoDEmanX
committed
#### Join the new mesh to the main object.
ob_surface.select = True
self.main_object.select = True
bpy.context.scene.objects.active = bpy.data.objects[self.main_object.name]
CoDEmanX
committed
bpy.ops.object.join('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.mesh.remove_doubles('INVOKE_REGION_WIN', threshold=0.0001)
bpy.ops.mesh.normals_make_consistent('INVOKE_REGION_WIN', inside=False)
bpy.ops.mesh.select_all('INVOKE_REGION_WIN', action='DESELECT')
CoDEmanX
committed
CoDEmanX
committed
def execute(self, context):
bpy.context.user_preferences.edit.use_global_undo = False
CoDEmanX
committed
if not self.is_fill_faces:
bpy.ops.wm.context_set_value(data_path='tool_settings.mesh_select_mode', value='True, False, False')
CoDEmanX
committed
# Build splines from the "last saved splines".
last_saved_curve = bpy.data.curves.new('SURFSKIO_last_crv', 'CURVE')
self.main_splines = bpy.data.objects.new('SURFSKIO_last_crv', last_saved_curve)
bpy.context.scene.objects.link(self.main_splines)
CoDEmanX
committed
last_saved_curve.dimensions = "3D"
CoDEmanX
committed
for sp in self.last_strokes_splines_coords:
spline = self.main_splines.data.splines.new('BEZIER')
spline.bezier_points.add(len(sp) - 1) # less one because one point is added when the spline is created.
for p in range(0, len(sp)):
spline.bezier_points[p].co = [sp[p][0], sp[p][1], sp[p][2]]
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.main_splines.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.main_splines.name]
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='SELECT')
bpy.ops.curve.handle_type_set(type='VECTOR') # Important to make it vector first and then automatic, otherwise the tips handles get too big and distort the shrinkwrap results later.
bpy.ops.curve.handle_type_set('INVOKE_REGION_WIN', type='AUTOMATIC')
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
self.main_splines.name = "SURFSKIO_temp_strokes"
CoDEmanX
committed
if self.is_crosshatch:
strokes_for_crosshatch = True
strokes_for_rectangular_surface = False
else:
strokes_for_rectangular_surface = True
strokes_for_crosshatch = False
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.main_object.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.main_object.name]
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
if strokes_for_rectangular_surface:
self.rectangular_surface()
elif strokes_for_crosshatch:
self.crosshatch_surface_execute()
CoDEmanX
committed
#### Delete main splines
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.main_splines.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.main_splines.name]
CoDEmanX
committed
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.main_object.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.main_object.name]
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.context.user_preferences.edit.use_global_undo = self.initial_global_undo_state
CoDEmanX
committed
CoDEmanX
committed
def invoke(self, context, event):
self.initial_global_undo_state = bpy.context.user_preferences.edit.use_global_undo
CoDEmanX
committed
self.main_object = bpy.context.scene.objects.active
self.main_object_selected_verts_count = int(self.main_object.data.total_vert_sel)
CoDEmanX
committed
bpy.context.user_preferences.edit.use_global_undo = False
CoDEmanX
committed
bpy.ops.wm.context_set_value(data_path='tool_settings.mesh_select_mode', value='True, False, False')
CoDEmanX
committed
# Out Edit mode and In again to make sure the actual mesh selections are being taken.
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
self.cyclic_cross = bpy.context.scene.SURFSK_cyclic_cross
self.cyclic_follow = bpy.context.scene.SURFSK_cyclic_follow
self.automatic_join = bpy.context.scene.SURFSK_automatic_join
self.loops_on_strokes = bpy.context.scene.SURFSK_loops_on_strokes
self.keep_strokes = bpy.context.scene.SURFSK_keep_strokes
CoDEmanX
committed
CoDEmanX
committed
if self.loops_on_strokes:
self.edges_V = 3
else:
self.edges_V = 10
CoDEmanX
committed
CoDEmanX
committed
CoDEmanX
committed
self.last_strokes_splines_coords = []
CoDEmanX
committed
#### Determine the type of the strokes.
self.strokes_type = get_strokes_type(self.main_object)
CoDEmanX
committed
#### Check if it will be used grease pencil strokes or curves.
if self.strokes_type == "GP_STROKES" or self.strokes_type == "EXTERNAL_CURVE": # If there are strokes to be used.
if self.strokes_type == "GP_STROKES":
# Convert grease pencil strokes to curve.
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
Bastien Montagne
committed
bpy.ops.gpencil.convert('INVOKE_REGION_WIN', type='CURVE', use_link_strokes=False)
# XXX gpencil.convert now keep org object as active/selected, *not* newly created curve!
# XXX This is far from perfect, but should work in most cases...
# self.original_curve = bpy.context.object
for ob in bpy.context.selected_objects:
if ob != bpy.context.scene.objects.active and ob.name.startswith("GP_Layer"):
self.original_curve = ob
self.using_external_curves = False
elif self.strokes_type == "EXTERNAL_CURVE":
for ob in bpy.context.selected_objects:
if ob != bpy.context.scene.objects.active:
self.original_curve = ob
self.using_external_curves = True
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
#### Make sure there are no objects left from erroneous executions of this operator, with the reserved names used here.
for o in bpy.data.objects:
if o.name.find("SURFSKIO_") != -1:
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[o.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[o.name]
CoDEmanX
committed
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.original_curve.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.original_curve.name]
CoDEmanX
committed
bpy.ops.object.duplicate('INVOKE_REGION_WIN')
CoDEmanX
committed
self.temporary_curve = bpy.context.scene.objects.active
CoDEmanX
committed
# Deselect all points of the curve
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
# Delete splines with only a single isolated point.
for i in range(len(self.temporary_curve.data.splines)):
sp = self.temporary_curve.data.splines[i]
CoDEmanX
committed
if len(sp.bezier_points) == 1:
sp.bezier_points[0].select_control_point = True
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.temporary_curve.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.temporary_curve.name]
CoDEmanX
committed
#### Set a minimum number of points for crosshatch
minimum_points_num = 15
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
# Check if the number of points of each curve has at least the number of points of minimum_points_num, which is a bit more than the face-loops limit. If not, subdivide to reach at least that number of ponts.
for i in range(len(self.temporary_curve.data.splines)):
sp = self.temporary_curve.data.splines[i]
CoDEmanX
committed
if len(sp.bezier_points) < minimum_points_num:
for bp in sp.bezier_points:
bp.select_control_point = True
CoDEmanX
committed
if (len(sp.bezier_points) - 1) != 0:
subdivide_cuts = int((minimum_points_num - len(sp.bezier_points)) / (len(sp.bezier_points) - 1)) + 1 # Formula to get the number of cuts that will make a curve of N number of points have near to "minimum_points_num" points, when subdividing with this number of cuts.
else:
subdivide_cuts = 0
CoDEmanX
committed
bpy.ops.curve.subdivide('INVOKE_REGION_WIN', number_cuts = subdivide_cuts)
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
# Detect if the strokes are a crosshatch and do it if it is.
self.crosshatch_surface_invoke(self.temporary_curve)
CoDEmanX
committed
if not self.is_crosshatch:
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.temporary_curve.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.temporary_curve.name]
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
#### Set a minimum number of points for rectangular surfaces.
minimum_points_num = 60
CoDEmanX
committed
# Check if the number of points of each curve has at least the number of points of minimum_points_num, which is a bit more than the face-loops limit. If not, subdivide to reach at least that number of ponts.
for i in range(len(self.temporary_curve.data.splines)):
sp = self.temporary_curve.data.splines[i]
CoDEmanX
committed
if len(sp.bezier_points) < minimum_points_num:
for bp in sp.bezier_points:
bp.select_control_point = True
CoDEmanX
committed
if (len(sp.bezier_points) - 1) != 0:
subdivide_cuts = int((minimum_points_num - len(sp.bezier_points)) / (len(sp.bezier_points) - 1)) + 1 # Formula to get the number of cuts that will make a curve of N number of points have near to "minimum_points_num" points, when subdividing with this number of cuts.
else:
subdivide_cuts = 0
CoDEmanX
committed
bpy.ops.curve.subdivide('INVOKE_REGION_WIN', number_cuts = subdivide_cuts)
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
# Save coordinates of the actual strokes (as the "last saved splines").
for sp_idx in range(len(self.temporary_curve.data.splines)):
self.last_strokes_splines_coords.append([])
for bp_idx in range(len(self.temporary_curve.data.splines[sp_idx].bezier_points)):
coords = self.temporary_curve.matrix_world * self.temporary_curve.data.splines[sp_idx].bezier_points[bp_idx].co
self.last_strokes_splines_coords[sp_idx].append([coords[0], coords[1], coords[2]])
CoDEmanX
committed
# Check for cyclic splines, put the first and last points in the middle of their actual positions.
for sp_idx in range(len(self.temporary_curve.data.splines)):
if self.temporary_curve.data.splines[sp_idx].use_cyclic_u == True:
first_p_co = self.last_strokes_splines_coords[sp_idx][0]
last_p_co = self.last_strokes_splines_coords[sp_idx][len(self.last_strokes_splines_coords[sp_idx]) - 1]
CoDEmanX
committed
target_co = [(first_p_co[0] + last_p_co[0]) / 2, (first_p_co[1] + last_p_co[1]) / 2, (first_p_co[2] + last_p_co[2]) / 2]
CoDEmanX
committed
self.last_strokes_splines_coords[sp_idx][0] = target_co
self.last_strokes_splines_coords[sp_idx][len(self.last_strokes_splines_coords[sp_idx]) - 1] = target_co
CoDEmanX
committed
tuple(self.last_strokes_splines_coords)
CoDEmanX
committed
# Estimation of the average length of the segments between each point of the grease pencil strokes. Will be useful to determine whether a curve should be made "Cyclic".
segments_lengths_sum = 0
segments_count = 0
random_spline = self.temporary_curve.data.splines[0].bezier_points
for i in range(0, len(random_spline)):
if i != 0 and len(random_spline) - 1 >= i:
segments_lengths_sum += (random_spline[i - 1].co - random_spline[i].co).length
segments_count += 1
CoDEmanX
committed
self.average_gp_segment_length = segments_lengths_sum / segments_count
CoDEmanX
committed
#### Delete temporary strokes curve object
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.temporary_curve.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.temporary_curve.name]
CoDEmanX
committed
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.main_object.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.main_object.name]
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
self.execute(context)
bpy.context.user_preferences.edit.use_global_undo = False # Set again since "execute()" will turn it again to its initial value.
CoDEmanX
committed
#### If "Keep strokes" option is not active, delete original strokes curve object.
if (not self.stopping_errors and not self.keep_strokes) or self.is_crosshatch:
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.original_curve.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.original_curve.name]
CoDEmanX
committed
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.main_object.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.main_object.name]
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
#### Delete grease pencil strokes.
if self.strokes_type == "GP_STROKES" and not self.stopping_errors:
bpy.ops.gpencil.active_frame_delete('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.context.user_preferences.edit.use_global_undo = self.initial_global_undo_state
CoDEmanX
committed
if not self.stopping_errors:
return {"FINISHED"}
else:
return{"CANCELLED"}
CoDEmanX
committed
elif self.strokes_type == "SELECTION_ALONE":
self.is_fill_faces = True
CoDEmanX
committed
created_faces_count = self.fill_with_faces(self.main_object)
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
if created_faces_count == 0:
self.report({'WARNING'}, "There aren't any strokes.")
return {"CANCELLED"}
else:
return {"FINISHED"}
CoDEmanX
committed
elif self.strokes_type == "EXTERNAL_NO_CURVE":
self.report({'WARNING'}, "The secondary object is not a Curve.")
return{"CANCELLED"}
CoDEmanX
committed
elif self.strokes_type == "MORE_THAN_ONE_EXTERNAL":
self.report({'WARNING'}, "There shouldn't be more than one secondary object selected.")
return{"CANCELLED"}
CoDEmanX
committed
elif self.strokes_type == "SINGLE_GP_STROKE_NO_SELECTION" or self.strokes_type == "SINGLE_CURVE_STROKE_NO_SELECTION":
self.report({'WARNING'}, "It's needed at least one stroke and one selection, or two strokes.")
return{"CANCELLED"}
CoDEmanX
committed
elif self.strokes_type == "NO_STROKES":
self.report({'WARNING'}, "There aren't any strokes.")
return{"CANCELLED"}
CoDEmanX
committed
elif self.strokes_type == "CURVE_WITH_NON_BEZIER_SPLINES":
self.report({'WARNING'}, "All splines must be Bezier.")
return{"CANCELLED"}
CoDEmanX
committed
else:
return{"CANCELLED"}
# Edit strokes operator.
class GPENCIL_OT_SURFSK_edit_strokes(bpy.types.Operator):
bl_idname = "gpencil.surfsk_edit_strokes"
bl_label = "Bsurfaces edit strokes"
bl_description = "Edit the grease pencil strokes or curves used."
CoDEmanX
committed
def execute(self, context):
#### Determine the type of the strokes.
self.strokes_type = get_strokes_type(self.main_object)
#### Check if strokes are grease pencil strokes or a curves object.
selected_objs = bpy.context.selected_objects
if self.strokes_type == "EXTERNAL_CURVE" or self.strokes_type == "SINGLE_CURVE_STROKE_NO_SELECTION":
for ob in selected_objs:
if ob != bpy.context.scene.objects.active:
curve_ob = ob
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[curve_ob.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[curve_ob.name]
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
elif self.strokes_type == "GP_STROKES" or self.strokes_type == "SINGLE_GP_STROKE_NO_SELECTION":
#### Convert grease pencil strokes to curve.
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.gpencil.convert('INVOKE_REGION_WIN', type='CURVE', use_link_strokes=False)
for ob in bpy.context.selected_objects:
if ob != bpy.context.scene.objects.active and ob.name.startswith("GP_Layer"):
ob_gp_strokes = ob
CoDEmanX
committed
#ob_gp_strokes = bpy.context.object
CoDEmanX
committed
#### Delete grease pencil strokes.
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.main_object.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.main_object.name]
CoDEmanX
committed
bpy.ops.gpencil.active_frame_delete('INVOKE_REGION_WIN')
CoDEmanX
committed
#### Clean up curves.
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[ob_gp_strokes.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[ob_gp_strokes.name]
CoDEmanX
committed
curve_crv = ob_gp_strokes.data
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.curve.spline_type_set('INVOKE_REGION_WIN', type="BEZIER")
bpy.ops.curve.handle_type_set('INVOKE_REGION_WIN', type="AUTOMATIC")
bpy.data.curves[curve_crv.name].show_handles = False
bpy.data.curves[curve_crv.name].show_normal_face = False
CoDEmanX
committed
elif self.strokes_type == "EXTERNAL_NO_CURVE":
self.report({'WARNING'}, "The secondary object is not a Curve.")
return{"CANCELLED"}
elif self.strokes_type == "MORE_THAN_ONE_EXTERNAL":
self.report({'WARNING'}, "There shouldn't be more than one secondary object selected.")
return{"CANCELLED"}
elif self.strokes_type == "NO_STROKES" or self.strokes_type == "SELECTION_ALONE":
self.report({'WARNING'}, "There aren't any strokes.")
return{"CANCELLED"}
else:
return{"CANCELLED"}
CoDEmanX
committed
def invoke (self, context, event):
self.main_object = bpy.context.object
CoDEmanX
committed
CoDEmanX
committed
return {"FINISHED"}
class CURVE_OT_SURFSK_reorder_splines(bpy.types.Operator):
bl_idname = "curve.surfsk_reorder_splines"
bl_label = "Bsurfaces reorder splines"
bl_description = "Defines the order of the splines by using grease pencil strokes."
bl_options = {'REGISTER', 'UNDO'}
CoDEmanX
committed
def execute(self, context):
objects_to_delete = []
#### Convert grease pencil strokes to curve.
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.gpencil.convert('INVOKE_REGION_WIN', type='CURVE', use_link_strokes=False)
for ob in bpy.context.selected_objects:
if ob != bpy.context.scene.objects.active and ob.name.startswith("GP_Layer"):
GP_strokes_curve = ob
CoDEmanX
committed
#GP_strokes_curve = bpy.context.object
objects_to_delete.append(GP_strokes_curve)
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[GP_strokes_curve.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[GP_strokes_curve.name]
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='SELECT')
bpy.ops.curve.subdivide('INVOKE_REGION_WIN', number_cuts = 100)
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.object.duplicate('INVOKE_REGION_WIN')
GP_strokes_mesh = bpy.context.object
objects_to_delete.append(GP_strokes_mesh)
CoDEmanX
committed
GP_strokes_mesh.data.resolution_u = 1
bpy.ops.object.convert(target='MESH', keep_original=False)
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.main_curve.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.main_curve.name]
CoDEmanX
committed
bpy.ops.object.duplicate('INVOKE_REGION_WIN')
curves_duplicate_1 = bpy.context.object
objects_to_delete.append(curves_duplicate_1)
CoDEmanX
committed
CoDEmanX
committed
for x in range(round(minimum_points_num / 100)): # Some iterations since the subdivision operator has a limit of 100 subdivisions per iteration.
#### Check if the number of points of each curve has at least the number of points of minimum_points_num. If not, subdivide to reach at least that number of ponts.
for i in range(len(curves_duplicate_1.data.splines)):
sp = curves_duplicate_1.data.splines[i]
CoDEmanX
committed
if len(sp.bezier_points) < minimum_points_num:
for bp in sp.bezier_points:
bp.select_control_point = True
CoDEmanX
committed
if (len(sp.bezier_points) - 1) != 0:
subdivide_cuts = int((minimum_points_num - len(sp.bezier_points)) / (len(sp.bezier_points) - 1)) + 1 # Formula to get the number of cuts that will make a curve of N number of points have near to "minimum_points_num" points, when subdividing with this number of cuts.
else:
subdivide_cuts = 0
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.curve.subdivide('INVOKE_REGION_WIN', number_cuts = subdivide_cuts)
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.object.duplicate('INVOKE_REGION_WIN')
curves_duplicate_2 = bpy.context.object
objects_to_delete.append(curves_duplicate_2)
CoDEmanX
committed
#### Duplicate the duplicate and add Shrinkwrap to it, with the grease pencil strokes curve as target.
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[curves_duplicate_2.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[curves_duplicate_2.name]
CoDEmanX
committed
bpy.ops.object.modifier_add('INVOKE_REGION_WIN', type='SHRINKWRAP')
curves_duplicate_2.modifiers["Shrinkwrap"].wrap_method = "NEAREST_VERTEX"
curves_duplicate_2.modifiers["Shrinkwrap"].target = GP_strokes_mesh
bpy.ops.object.modifier_apply('INVOKE_REGION_WIN', apply_as='DATA', modifier='Shrinkwrap')
CoDEmanX
committed
#### Get the distance of each vert from its original position to its position with Shrinkwrap.
nearest_points_coords = {}
for st_idx in range(len(curves_duplicate_1.data.splines)):
for bp_idx in range(len(curves_duplicate_1.data.splines[st_idx].bezier_points)):
bp_1_co = curves_duplicate_1.matrix_world * curves_duplicate_1.data.splines[st_idx].bezier_points[bp_idx].co
bp_2_co = curves_duplicate_2.matrix_world * curves_duplicate_2.data.splines[st_idx].bezier_points[bp_idx].co
CoDEmanX
committed
if bp_idx == 0:
shortest_dist = (bp_1_co - bp_2_co).length
nearest_points_coords[st_idx] = ("%.4f" % bp_2_co[0], "%.4f" % bp_2_co[1], "%.4f" % bp_2_co[2])
CoDEmanX
committed
dist = (bp_1_co - bp_2_co).length
CoDEmanX
committed
if dist < shortest_dist:
nearest_points_coords[st_idx] = ("%.4f" % bp_2_co[0], "%.4f" % bp_2_co[1], "%.4f" % bp_2_co[2])
shortest_dist = dist
CoDEmanX
committed
#### Get all coords of GP strokes points, for comparison.
GP_strokes_coords = []
for st_idx in range(len(GP_strokes_curve.data.splines)):
GP_strokes_coords.append([("%.4f" % x if "%.4f" % x != "-0.00" else "0.00", "%.4f" % y if "%.4f" % y != "-0.00" else "0.00", "%.4f" % z if "%.4f" % z != "-0.00" else "0.00") for x, y, z in [bp.co for bp in GP_strokes_curve.data.splines[st_idx].bezier_points]])
CoDEmanX
committed
#### Check the point of the GP strokes with the same coords as the nearest points of the curves (with shrinkwrap).
GP_connection_points = {} # Dictionary with GP stroke index as index, and a list as value. The list has as index the point index of the GP stroke nearest to the spline, and as value the spline index.
for gp_st_idx in range(len(GP_strokes_coords)):
GPvert_spline_relationship = {}
CoDEmanX
committed
for splines_st_idx in range(len(nearest_points_coords)):
if nearest_points_coords[splines_st_idx] in GP_strokes_coords[gp_st_idx]:
GPvert_spline_relationship[GP_strokes_coords[gp_st_idx].index(nearest_points_coords[splines_st_idx])] = splines_st_idx
CoDEmanX
committed
GP_connection_points[gp_st_idx] = GPvert_spline_relationship
CoDEmanX
committed
#### Get the splines new order.
splines_new_order = []
for i in GP_connection_points:
dict_keys = sorted(GP_connection_points[i].keys()) # Sort dictionaries by key
CoDEmanX
committed
for k in dict_keys:
splines_new_order.append(GP_connection_points[i][k])
CoDEmanX
committed
CoDEmanX
committed
curve_original_name = self.main_curve.name
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[self.main_curve.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[self.main_curve.name]
CoDEmanX
committed
self.main_curve.name = "SURFSKIO_CRV_ORD"
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
for sp_idx in range(len(self.main_curve.data.splines)):
self.main_curve.data.splines[0].bezier_points[0].select_control_point = True
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
bpy.ops.curve.separate('INVOKE_REGION_WIN')
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
#### Get the names of the separated splines objects in the original order.
splines_unordered = {}
for o in bpy.data.objects:
if o.name.find("SURFSKIO_CRV_ORD") != -1:
spline_order_string = o.name.partition(".")[2]
CoDEmanX
committed
if spline_order_string != "" and int(spline_order_string) > 0:
spline_order_index = int(spline_order_string) - 1
splines_unordered[spline_order_index] = o.name
CoDEmanX
committed
#### Join all splines objects in final order.
for order_idx in splines_new_order:
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[splines_unordered[order_idx]].select = True
bpy.data.objects["SURFSKIO_CRV_ORD"].select = True
bpy.context.scene.objects.active = bpy.data.objects["SURFSKIO_CRV_ORD"]
CoDEmanX
committed
bpy.ops.object.join('INVOKE_REGION_WIN')
CoDEmanX
committed
#### Go back to the original name of the curves object.
bpy.context.object.name = curve_original_name
CoDEmanX
committed
#### Delete all unused objects.
for o in objects_to_delete:
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[o.name].select = True
bpy.context.scene.objects.active = bpy.data.objects[o.name]
CoDEmanX
committed
CoDEmanX
committed
bpy.ops.object.select_all('INVOKE_REGION_WIN', action='DESELECT')
bpy.data.objects[curve_original_name].select = True
bpy.context.scene.objects.active = bpy.data.objects[curve_original_name]
CoDEmanX
committed
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
CoDEmanX
committed
bpy.ops.gpencil.active_frame_delete('INVOKE_REGION_WIN')
CoDEmanX
committed
CoDEmanX
committed
def invoke (self, context, event):
self.main_curve = bpy.context.object
CoDEmanX
committed
there_are_GP_strokes = False
try:
#### Get the active grease pencil layer.
strokes_num = len(self.main_curve.grease_pencil.layers.active.active_frame.strokes)
CoDEmanX
committed
if strokes_num > 0:
there_are_GP_strokes = True
except:
pass
CoDEmanX
committed
if there_are_GP_strokes:
self.execute(context)
self.report({'INFO'}, "Splines have been reordered.")
else:
self.report({'WARNING'}, "Draw grease pencil strokes to connect splines.")
CoDEmanX
committed
CoDEmanX
committed
class CURVE_OT_SURFSK_first_points(bpy.types.Operator):
bl_idname = "curve.surfsk_first_points"
bl_label = "Bsurfaces set first points"
bl_description = "Set the selected points as the first point of each spline."
bl_options = {'REGISTER', 'UNDO'}
CoDEmanX
committed
CoDEmanX
committed
#### Check non-cyclic splines to invert.
for i in range(len(self.main_curve.data.splines)):
b_points = self.main_curve.data.splines[i].bezier_points
CoDEmanX
committed
if not i in self.cyclic_splines: # Only for non-cyclic splines
if b_points[len(b_points) - 1].select_control_point:
splines_to_invert.append(i)
CoDEmanX
committed
#### Reorder points of cyclic splines, and set all handles to "Automatic".
CoDEmanX
committed
# Check first selected point.
cyclic_splines_new_first_pt = {}
for i in self.cyclic_splines:
sp = self.main_curve.data.splines[i]
CoDEmanX
committed
for t in range(len(sp.bezier_points)):
bp = sp.bezier_points[t]
if bp.select_control_point or bp.select_right_handle or bp.select_left_handle:
cyclic_splines_new_first_pt[i] = t
break # To take only one if there are more.
CoDEmanX
committed
# Reorder.
for spline_idx in cyclic_splines_new_first_pt:
sp = self.main_curve.data.splines[spline_idx]
CoDEmanX
committed
spline_old_coords = []
for bp_old in sp.bezier_points:
coords = (bp_old.co[0], bp_old.co[1], bp_old.co[2])
CoDEmanX
committed
left_handle_type = str(bp_old.handle_left_type)
left_handle_length = float(bp_old.handle_left.length)
left_handle_xyz = (float(bp_old.handle_left.x), float(bp_old.handle_left.y), float(bp_old.handle_left.z))
CoDEmanX
committed
right_handle_type = str(bp_old.handle_right_type)
right_handle_length = float(bp_old.handle_right.length)
right_handle_xyz = (float(bp_old.handle_right.x), float(bp_old.handle_right.y), float(bp_old.handle_right.z))
CoDEmanX
committed
spline_old_coords.append([coords, left_handle_type, right_handle_type, left_handle_length, right_handle_length, left_handle_xyz, right_handle_xyz])
CoDEmanX
committed
for t in range(len(sp.bezier_points)):
bp = sp.bezier_points
CoDEmanX
committed
if t + cyclic_splines_new_first_pt[spline_idx] + 1 <= len(bp) - 1:
new_index = t + cyclic_splines_new_first_pt[spline_idx] + 1
else:
new_index = t + cyclic_splines_new_first_pt[spline_idx] + 1 - len(bp)
CoDEmanX
committed
bp[t].co = mathutils.Vector(spline_old_coords[new_index][0])
CoDEmanX
committed
bp[t].handle_left.length = spline_old_coords[new_index][3]
bp[t].handle_right.length = spline_old_coords[new_index][4]
CoDEmanX
committed
bp[t].handle_left_type = "FREE"
bp[t].handle_right_type = "FREE"
CoDEmanX
committed
bp[t].handle_left.x = spline_old_coords[new_index][5][0]
bp[t].handle_left.y = spline_old_coords[new_index][5][1]
bp[t].handle_left.z = spline_old_coords[new_index][5][2]
CoDEmanX
committed
bp[t].handle_right.x = spline_old_coords[new_index][6][0]
bp[t].handle_right.y = spline_old_coords[new_index][6][1]
bp[t].handle_right.z = spline_old_coords[new_index][6][2]
CoDEmanX
committed
bp[t].handle_left_type = spline_old_coords[new_index][1]
bp[t].handle_right_type = spline_old_coords[new_index][2]
CoDEmanX
committed
#### Invert the non-cyclic splines designated above.
for i in range(len(splines_to_invert)):
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
self.main_curve.data.splines[splines_to_invert[i]].bezier_points[0].select_control_point = True
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.curve.switch_direction()
CoDEmanX
committed
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
CoDEmanX
committed
#### Keep selected the first vert of each spline.
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
for i in range(len(self.main_curve.data.splines)):
if not self.main_curve.data.splines[i].use_cyclic_u:
bp = self.main_curve.data.splines[i].bezier_points[0]
else:
bp = self.main_curve.data.splines[i].bezier_points[len(self.main_curve.data.splines[i].bezier_points) - 1]
CoDEmanX
committed
bp.select_control_point = True
bp.select_right_handle = True
bp.select_left_handle = True
CoDEmanX
committed
CoDEmanX
committed
def invoke (self, context, event):
self.main_curve = bpy.context.object
CoDEmanX
committed
# Check if all curves are Bezier, and detect which ones are cyclic.
self.cyclic_splines = []
for i in range(len(self.main_curve.data.splines)):
if self.main_curve.data.splines[i].type != "BEZIER":
self.report({'WARNING'}, 'All splines must be Bezier type.')
CoDEmanX
committed
return {'CANCELLED'}
else:
if self.main_curve.data.splines[i].use_cyclic_u:
self.cyclic_splines.append(i)
CoDEmanX
committed
self.report({'INFO'}, "First points have been set.")
CoDEmanX
committed
Campbell Barton
committed
return {'FINISHED'}
CoDEmanX
committed
bpy.utils.register_class(VIEW3D_PT_tools_SURFSK_mesh)
bpy.utils.register_class(VIEW3D_PT_tools_SURFSK_curve)
bpy.utils.register_class(GPENCIL_OT_SURFSK_add_surface)
bpy.utils.register_class(GPENCIL_OT_SURFSK_edit_strokes)
bpy.utils.register_class(CURVE_OT_SURFSK_reorder_splines)
bpy.utils.register_class(CURVE_OT_SURFSK_first_points)
CoDEmanX
committed
bpy.types.Scene.SURFSK_cyclic_cross = bpy.props.BoolProperty(
name="Cyclic Cross",
description="Make cyclic the face-loops crossing the strokes.",
default=False)
CoDEmanX
committed
bpy.types.Scene.SURFSK_cyclic_follow = bpy.props.BoolProperty(
name="Cyclic Follow",
description="Make cyclic the face-loops following the strokes.",
default=False)
CoDEmanX
committed
bpy.types.Scene.SURFSK_keep_strokes = bpy.props.BoolProperty(
name="Keep strokes",
description="Keeps the sketched strokes or curves after adding the surface.",
default=False)
CoDEmanX
committed
bpy.types.Scene.SURFSK_automatic_join = bpy.props.BoolProperty(
name="Automatic join",
description="Join automatically vertices of either surfaces generated by crosshatching, or from the borders of closed shapes.",
default=True)
CoDEmanX
committed
bpy.types.Scene.SURFSK_loops_on_strokes = bpy.props.BoolProperty(
name="Loops on strokes",
description="Make the loops match the paths of the strokes.",
default=True)
bpy.types.Scene.SURFSK_precision = bpy.props.IntProperty(
name="Precision",
description="Precision level of the surface calculation.",
default=2,
min=1,
max=100)
CoDEmanX
committed
bpy.utils.unregister_class(VIEW3D_PT_tools_SURFSK_mesh)
bpy.utils.unregister_class(VIEW3D_PT_tools_SURFSK_curve)
bpy.utils.unregister_class(GPENCIL_OT_SURFSK_add_surface)
bpy.utils.unregister_class(GPENCIL_OT_SURFSK_edit_strokes)
bpy.utils.unregister_class(CURVE_OT_SURFSK_reorder_splines)
bpy.utils.unregister_class(CURVE_OT_SURFSK_first_points)
CoDEmanX
committed
del bpy.types.Scene.SURFSK_precision
del bpy.types.Scene.SURFSK_keep_strokes
del bpy.types.Scene.SURFSK_automatic_join
del bpy.types.Scene.SURFSK_cyclic_cross
del bpy.types.Scene.SURFSK_cyclic_follow
del bpy.types.Scene.SURFSK_loops_on_strokes
CoDEmanX
committed
CoDEmanX
committed