1 # SPDX-License-Identifier: GPL-2.0-or-later
5 "name": "Bsurfaces GPL Edition",
6 "author": "Eclectiel, Vladimir Spivak (cwolf3d)",
9 "location": "View3D EditMode > Sidebar > Edit Tab",
10 "description": "Modeling and retopology tool",
11 "doc_url": "{BLENDER_MANUAL_URL}/addons/mesh/bsurfaces.html",
18 from bpy_extras
import object_utils
21 from mathutils
import Matrix
, Vector
22 from mathutils
.geometry
import (
31 from bpy
.props
import (
40 from bpy
.types
import (
47 # ----------------------------
49 global_shade_smooth
= False
50 global_mesh_object
= ""
51 global_gpencil_object
= ""
52 global_curve_object
= ""
54 # ----------------------------
56 class VIEW3D_PT_tools_SURFSK_mesh(Panel
):
57 bl_space_type
= 'VIEW_3D'
60 bl_label
= "Bsurfaces"
62 def draw(self
, context
):
64 bs
= context
.scene
.bsurfaces
66 col
= layout
.column(align
=True)
69 col
.operator("mesh.surfsk_init", text
="Initialize (Add BSurface mesh)")
70 col
.operator("mesh.surfsk_add_modifiers", text
="Add Mirror and others modifiers")
72 col
.label(text
="Mesh of BSurface:")
73 col
.prop(bs
, "SURFSK_mesh", text
="")
74 if bs
.SURFSK_mesh
!= None:
75 try: mesh_object
= bs
.SURFSK_mesh
77 try: col
.prop(mesh_object
.data
.materials
[0], "diffuse_color")
80 shrinkwrap
= next(mod
for mod
in mesh_object
.modifiers
81 if mod
.type == 'SHRINKWRAP')
82 col
.prop(shrinkwrap
, "offset")
85 try: col
.prop(mesh_object
, "show_in_front")
87 try: col
.prop(bs
, "SURFSK_shade_smooth")
89 try: col
.prop(mesh_object
, "show_wire")
92 col
.label(text
="Guide strokes:")
93 col
.row().prop(bs
, "SURFSK_guide", expand
=True)
94 if bs
.SURFSK_guide
== 'GPencil':
95 col
.prop(bs
, "SURFSK_gpencil", text
="")
97 if bs
.SURFSK_guide
== 'Curve':
98 col
.prop(bs
, "SURFSK_curve", text
="")
102 col
.operator("mesh.surfsk_add_surface", text
="Add Surface")
103 col
.operator("mesh.surfsk_edit_surface", text
="Edit Surface")
106 if bs
.SURFSK_guide
== 'GPencil':
107 col
.operator("gpencil.surfsk_add_strokes", text
="Add Strokes")
108 col
.operator("gpencil.surfsk_edit_strokes", text
="Edit Strokes")
110 col
.operator("gpencil.surfsk_strokes_to_curves", text
="Strokes to curves")
112 if bs
.SURFSK_guide
== 'Annotation':
113 col
.operator("gpencil.surfsk_add_annotation", text
="Add Annotation")
115 col
.operator("gpencil.surfsk_annotations_to_curves", text
="Annotation to curves")
117 if bs
.SURFSK_guide
== 'Curve':
118 col
.operator("curve.surfsk_edit_curve", text
="Edit curve")
121 col
.label(text
="Initial settings:")
122 col
.prop(bs
, "SURFSK_edges_U")
123 col
.prop(bs
, "SURFSK_edges_V")
124 col
.prop(bs
, "SURFSK_cyclic_cross")
125 col
.prop(bs
, "SURFSK_cyclic_follow")
126 col
.prop(bs
, "SURFSK_loops_on_strokes")
127 col
.prop(bs
, "SURFSK_automatic_join")
128 col
.prop(bs
, "SURFSK_keep_strokes")
130 class VIEW3D_PT_tools_SURFSK_curve(Panel
):
131 bl_space_type
= 'VIEW_3D'
132 bl_region_type
= 'UI'
133 bl_context
= "curve_edit"
135 bl_label
= "Bsurfaces"
138 def poll(cls
, context
):
139 return context
.active_object
141 def draw(self
, context
):
144 col
= layout
.column(align
=True)
147 col
.operator("curve.surfsk_first_points", text
="Set First Points")
148 col
.operator("curve.switch_direction", text
="Switch Direction")
149 col
.operator("curve.surfsk_reorder_splines", text
="Reorder Splines")
152 # ----------------------------
153 # Returns the type of strokes used
154 def get_strokes_type(context
):
155 strokes_type
= "NO_STROKES"
158 # Check if they are annotation
159 if context
.scene
.bsurfaces
.SURFSK_guide
== 'Annotation':
161 strokes
= bpy
.context
.annotation_data
.layers
.active
.active_frame
.strokes
163 strokes_num
= len(strokes
)
166 strokes_type
= "GP_ANNOTATION"
168 strokes_type
= "NO_STROKES"
170 # Check if they are grease pencil
171 if context
.scene
.bsurfaces
.SURFSK_guide
== 'GPencil':
173 global global_gpencil_object
174 gpencil
= bpy
.data
.objects
[global_gpencil_object
]
175 strokes
= gpencil
.data
.layers
.active
.active_frame
.strokes
177 strokes_num
= len(strokes
)
180 strokes_type
= "GP_STROKES"
182 strokes_type
= "NO_STROKES"
184 # Check if they are curves, if there aren't grease pencil strokes
185 if context
.scene
.bsurfaces
.SURFSK_guide
== 'Curve':
187 global global_curve_object
188 ob
= bpy
.data
.objects
[global_curve_object
]
189 if ob
.type == "CURVE":
190 strokes_type
= "EXTERNAL_CURVE"
191 strokes_num
= len(ob
.data
.splines
)
193 # Check if there is any non-bezier spline
194 for i
in range(len(ob
.data
.splines
)):
195 if ob
.data
.splines
[i
].type != "BEZIER":
196 strokes_type
= "CURVE_WITH_NON_BEZIER_SPLINES"
200 strokes_type
= "EXTERNAL_NO_CURVE"
202 strokes_type
= "NO_STROKES"
204 # Check if they are mesh
206 global global_mesh_object
207 self
.main_object
= bpy
.data
.objects
[global_mesh_object
]
208 total_vert_sel
= len([v
for v
in self
.main_object
.data
.vertices
if v
.select
])
210 # Check if there is a single stroke without any selection in the object
211 if strokes_num
== 1 and total_vert_sel
== 0:
212 if strokes_type
== "EXTERNAL_CURVE":
213 strokes_type
= "SINGLE_CURVE_STROKE_NO_SELECTION"
214 elif strokes_type
== "GP_STROKES":
215 strokes_type
= "SINGLE_GP_STROKE_NO_SELECTION"
217 if strokes_num
== 0 and total_vert_sel
> 0:
218 strokes_type
= "SELECTION_ALONE"
224 # ----------------------------
225 # Surface generator operator
226 class MESH_OT_SURFSK_add_surface(Operator
):
227 bl_idname
= "mesh.surfsk_add_surface"
228 bl_label
= "Bsurfaces add surface"
229 bl_description
= "Generates surfaces from grease pencil strokes, bezier curves or loose edges"
230 bl_options
= {'REGISTER', 'UNDO'}
232 is_crosshatch
: BoolProperty(
235 is_fill_faces
: BoolProperty(
238 selection_U_exists
: BoolProperty(
241 selection_V_exists
: BoolProperty(
244 selection_U2_exists
: BoolProperty(
247 selection_V2_exists
: BoolProperty(
250 selection_V_is_closed
: BoolProperty(
253 selection_U_is_closed
: BoolProperty(
256 selection_V2_is_closed
: BoolProperty(
259 selection_U2_is_closed
: BoolProperty(
263 edges_U
: IntProperty(
265 description
="Number of face-loops crossing the strokes",
270 edges_V
: IntProperty(
272 description
="Number of face-loops following the strokes",
277 cyclic_cross
: BoolProperty(
279 description
="Make cyclic the face-loops crossing the strokes",
282 cyclic_follow
: BoolProperty(
283 name
="Cyclic Follow",
284 description
="Make cyclic the face-loops following the strokes",
287 loops_on_strokes
: BoolProperty(
288 name
="Loops on strokes",
289 description
="Make the loops match the paths of the strokes",
292 automatic_join
: BoolProperty(
293 name
="Automatic join",
294 description
="Join automatically vertices of either surfaces generated "
295 "by crosshatching, or from the borders of closed shapes",
298 join_stretch_factor
: FloatProperty(
300 description
="Amount of stretching or shrinking allowed for "
301 "edges when joining vertices automatically",
307 keep_strokes
: BoolProperty(
309 description
="Keeps the sketched strokes or curves after adding the surface",
312 strokes_type
: StringProperty()
313 initial_global_undo_state
: BoolProperty()
316 def draw(self
, context
):
318 col
= layout
.column(align
=True)
321 if not self
.is_fill_faces
:
323 if not self
.is_crosshatch
:
324 if not self
.selection_U_exists
:
325 col
.prop(self
, "edges_U")
328 if not self
.selection_V_exists
:
329 col
.prop(self
, "edges_V")
334 if not self
.selection_U_exists
:
336 (self
.selection_V_exists
and not self
.selection_V_is_closed
) or
337 (self
.selection_V2_exists
and not self
.selection_V2_is_closed
)
339 col
.prop(self
, "cyclic_cross")
341 if not self
.selection_V_exists
:
343 (self
.selection_U_exists
and not self
.selection_U_is_closed
) or
344 (self
.selection_U2_exists
and not self
.selection_U2_is_closed
)
346 col
.prop(self
, "cyclic_follow")
348 col
.prop(self
, "loops_on_strokes")
350 col
.prop(self
, "automatic_join")
352 if self
.automatic_join
:
356 col
.prop(self
, "join_stretch_factor")
358 col
.prop(self
, "keep_strokes")
360 # Get an ordered list of a chain of vertices
361 def get_ordered_verts(self
, ob
, all_selected_edges_idx
, all_selected_verts_idx
,
362 first_vert_idx
, middle_vertex_idx
, closing_vert_idx
):
363 # Order selected vertices.
365 if closing_vert_idx
is not None:
366 verts_ordered
.append(ob
.data
.vertices
[closing_vert_idx
])
368 verts_ordered
.append(ob
.data
.vertices
[first_vert_idx
])
369 prev_v
= first_vert_idx
373 edges_non_matched
= 0
374 for i
in all_selected_edges_idx
:
375 if ob
.data
.edges
[i
] != prev_ed
and ob
.data
.edges
[i
].vertices
[0] == prev_v
and \
376 ob
.data
.edges
[i
].vertices
[1] in all_selected_verts_idx
:
378 verts_ordered
.append(ob
.data
.vertices
[ob
.data
.edges
[i
].vertices
[1]])
379 prev_v
= ob
.data
.edges
[i
].vertices
[1]
380 prev_ed
= ob
.data
.edges
[i
]
381 elif ob
.data
.edges
[i
] != prev_ed
and ob
.data
.edges
[i
].vertices
[1] == prev_v
and \
382 ob
.data
.edges
[i
].vertices
[0] in all_selected_verts_idx
:
384 verts_ordered
.append(ob
.data
.vertices
[ob
.data
.edges
[i
].vertices
[0]])
385 prev_v
= ob
.data
.edges
[i
].vertices
[0]
386 prev_ed
= ob
.data
.edges
[i
]
388 edges_non_matched
+= 1
390 if edges_non_matched
== len(all_selected_edges_idx
):
396 if closing_vert_idx
is not None:
397 verts_ordered
.append(ob
.data
.vertices
[closing_vert_idx
])
399 if middle_vertex_idx
is not None:
400 verts_ordered
.append(ob
.data
.vertices
[middle_vertex_idx
])
401 verts_ordered
.reverse()
403 return tuple(verts_ordered
)
405 # Calculates length of a chain of points.
406 def get_chain_length(self
, object, verts_ordered
):
407 matrix
= object.matrix_world
410 edges_lengths_sum
= 0
411 for i
in range(0, len(verts_ordered
)):
413 prev_v_co
= matrix
@ verts_ordered
[i
].co
415 v_co
= matrix
@ verts_ordered
[i
].co
417 v_difs
= [prev_v_co
[0] - v_co
[0], prev_v_co
[1] - v_co
[1], prev_v_co
[2] - v_co
[2]]
418 edge_length
= abs(sqrt(v_difs
[0] * v_difs
[0] + v_difs
[1] * v_difs
[1] + v_difs
[2] * v_difs
[2]))
420 edges_lengths
.append(edge_length
)
421 edges_lengths_sum
+= edge_length
425 return edges_lengths
, edges_lengths_sum
427 # Calculates the proportion of the edges of a chain of edges, relative to the full chain length.
428 def get_edges_proportions(self
, edges_lengths
, edges_lengths_sum
, use_boundaries
, fixed_edges_num
):
429 edges_proportions
= []
432 for l
in edges_lengths
:
433 edges_proportions
.append(l
/ edges_lengths_sum
)
437 for _n
in range(0, fixed_edges_num
):
438 edges_proportions
.append(1 / fixed_edges_num
)
441 return edges_proportions
443 # Calculates the angle between two pairs of points in space
444 def orientation_difference(self
, points_A_co
, points_B_co
):
445 # each parameter should be a list with two elements,
446 # and each element should be a x,y,z coordinate
447 vec_A
= points_A_co
[0] - points_A_co
[1]
448 vec_B
= points_B_co
[0] - points_B_co
[1]
450 angle
= vec_A
.angle(vec_B
)
453 angle
= abs(angle
- pi
)
457 # Calculate the which vert of verts_idx list is the nearest one
458 # to the point_co coordinates, and the distance
459 def shortest_distance(self
, object, point_co
, verts_idx
):
460 matrix
= object.matrix_world
462 for i
in range(0, len(verts_idx
)):
463 dist
= (point_co
- matrix
@ object.data
.vertices
[verts_idx
[i
]].co
).length
466 nearest_vert_idx
= verts_idx
[i
]
471 nearest_vert_idx
= verts_idx
[i
]
474 return nearest_vert_idx
, shortest_dist
476 # Returns the index of the opposite vert tip in a chain, given a vert tip index
477 # as parameter, and a multidimentional list with all pairs of tips
478 def opposite_tip(self
, vert_tip_idx
, all_chains_tips_idx
):
479 opposite_vert_tip_idx
= None
480 for i
in range(0, len(all_chains_tips_idx
)):
481 if vert_tip_idx
== all_chains_tips_idx
[i
][0]:
482 opposite_vert_tip_idx
= all_chains_tips_idx
[i
][1]
483 if vert_tip_idx
== all_chains_tips_idx
[i
][1]:
484 opposite_vert_tip_idx
= all_chains_tips_idx
[i
][0]
486 return opposite_vert_tip_idx
488 # Simplifies a spline and returns the new points coordinates
489 def simplify_spline(self
, spline_coords
, segments_num
):
490 simplified_spline
= []
491 points_between_segments
= round(len(spline_coords
) / segments_num
)
493 simplified_spline
.append(spline_coords
[0])
494 for i
in range(1, segments_num
):
495 simplified_spline
.append(spline_coords
[i
* points_between_segments
])
497 simplified_spline
.append(spline_coords
[len(spline_coords
) - 1])
499 return simplified_spline
501 # Returns a list with the coords of the points distributed over the splines
502 # passed to this method according to the proportions parameter
503 def distribute_pts(self
, surface_splines
, proportions
):
505 # Calculate the length of each final surface spline
506 surface_splines_lengths
= []
507 surface_splines_parsed
= []
509 for sp_idx
in range(0, len(surface_splines
)):
510 # Calculate spline length
511 surface_splines_lengths
.append(0)
513 for i
in range(0, len(surface_splines
[sp_idx
].bezier_points
)):
515 prev_p
= surface_splines
[sp_idx
].bezier_points
[i
]
517 p
= surface_splines
[sp_idx
].bezier_points
[i
]
518 edge_length
= (prev_p
.co
- p
.co
).length
519 surface_splines_lengths
[sp_idx
] += edge_length
523 # Calculate vertex positions with appropriate edge proportions, and ordered, for each spline
524 for sp_idx
in range(0, len(surface_splines
)):
525 surface_splines_parsed
.append([])
526 surface_splines_parsed
[sp_idx
].append(surface_splines
[sp_idx
].bezier_points
[0].co
)
528 prev_p_co
= surface_splines
[sp_idx
].bezier_points
[0].co
531 for prop_idx
in range(len(proportions
) - 1):
532 target_length
= surface_splines_lengths
[sp_idx
] * proportions
[prop_idx
]
533 partial_segment_length
= 0
537 # if not it'll pass the p_idx as an index below and crash
538 if p_idx
< len(surface_splines
[sp_idx
].bezier_points
):
539 p_co
= surface_splines
[sp_idx
].bezier_points
[p_idx
].co
540 new_dist
= (prev_p_co
- p_co
).length
542 # The new distance that could have the partial segment if
543 # it is still shorter than the target length
544 potential_segment_length
= partial_segment_length
+ new_dist
546 # If the potential is still shorter, keep adding
547 if potential_segment_length
< target_length
:
548 partial_segment_length
= potential_segment_length
553 # If the potential is longer than the target, calculate the target
554 # (a point between the last two points), and assign
555 elif potential_segment_length
> target_length
:
556 remaining_dist
= target_length
- partial_segment_length
557 vec
= p_co
- prev_p_co
559 intermediate_co
= prev_p_co
+ (vec
* remaining_dist
)
561 surface_splines_parsed
[sp_idx
].append(intermediate_co
)
563 partial_segment_length
+= remaining_dist
564 prev_p_co
= intermediate_co
568 # If the potential is equal to the target, assign
569 elif potential_segment_length
== target_length
:
570 surface_splines_parsed
[sp_idx
].append(p_co
)
578 # last point of the spline
579 surface_splines_parsed
[sp_idx
].append(
580 surface_splines
[sp_idx
].bezier_points
[len(surface_splines
[sp_idx
].bezier_points
) - 1].co
583 return surface_splines_parsed
585 # Counts the number of faces that belong to each edge
586 def edge_face_count(self
, ob
):
587 ed_keys_count_dict
= {}
589 for face
in ob
.data
.polygons
:
590 for ed_keys
in face
.edge_keys
:
591 if ed_keys
not in ed_keys_count_dict
:
592 ed_keys_count_dict
[ed_keys
] = 1
594 ed_keys_count_dict
[ed_keys
] += 1
597 for i
in range(len(ob
.data
.edges
)):
598 edge_face_count
.append(0)
600 for i
in range(len(ob
.data
.edges
)):
601 ed
= ob
.data
.edges
[i
]
606 if (v1
, v2
) in ed_keys_count_dict
:
607 edge_face_count
[i
] = ed_keys_count_dict
[(v1
, v2
)]
608 elif (v2
, v1
) in ed_keys_count_dict
:
609 edge_face_count
[i
] = ed_keys_count_dict
[(v2
, v1
)]
611 return edge_face_count
613 # Fills with faces all the selected vertices which form empty triangles or quads
614 def fill_with_faces(self
, object):
615 all_selected_verts_count
= self
.main_object_selected_verts_count
617 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
619 # Calculate average length of selected edges
620 all_selected_verts
= []
621 original_sel_edges_count
= 0
622 for ed
in object.data
.edges
:
623 if object.data
.vertices
[ed
.vertices
[0]].select
and object.data
.vertices
[ed
.vertices
[1]].select
:
625 coords
.append(object.data
.vertices
[ed
.vertices
[0]].co
)
626 coords
.append(object.data
.vertices
[ed
.vertices
[1]].co
)
628 original_sel_edges_count
+= 1
630 if not ed
.vertices
[0] in all_selected_verts
:
631 all_selected_verts
.append(ed
.vertices
[0])
633 if not ed
.vertices
[1] in all_selected_verts
:
634 all_selected_verts
.append(ed
.vertices
[1])
636 tuple(all_selected_verts
)
638 # Check if there is any edge selected. If not, interrupt the script
639 if original_sel_edges_count
== 0 and all_selected_verts_count
> 0:
642 # Get all edges connected to selected verts
643 all_edges_around_sel_verts
= []
644 edges_connected_to_sel_verts
= {}
645 verts_connected_to_every_vert
= {}
646 for ed_idx
in range(len(object.data
.edges
)):
647 ed
= object.data
.edges
[ed_idx
]
650 if ed
.vertices
[0] in all_selected_verts
:
651 if not ed
.vertices
[0] in edges_connected_to_sel_verts
:
652 edges_connected_to_sel_verts
[ed
.vertices
[0]] = []
654 edges_connected_to_sel_verts
[ed
.vertices
[0]].append(ed_idx
)
657 if ed
.vertices
[1] in all_selected_verts
:
658 if not ed
.vertices
[1] in edges_connected_to_sel_verts
:
659 edges_connected_to_sel_verts
[ed
.vertices
[1]] = []
661 edges_connected_to_sel_verts
[ed
.vertices
[1]].append(ed_idx
)
664 if include_edge
is True:
665 all_edges_around_sel_verts
.append(ed_idx
)
667 # Get all connected verts to each vert
668 if not ed
.vertices
[0] in verts_connected_to_every_vert
:
669 verts_connected_to_every_vert
[ed
.vertices
[0]] = []
671 if not ed
.vertices
[1] in verts_connected_to_every_vert
:
672 verts_connected_to_every_vert
[ed
.vertices
[1]] = []
674 verts_connected_to_every_vert
[ed
.vertices
[0]].append(ed
.vertices
[1])
675 verts_connected_to_every_vert
[ed
.vertices
[1]].append(ed
.vertices
[0])
677 # Get all verts connected to faces
678 all_verts_part_of_faces
= []
679 all_edges_faces_count
= []
680 all_edges_faces_count
+= self
.edge_face_count(object)
682 # Get only the selected edges that have faces attached.
683 count_faces_of_edges_around_sel_verts
= {}
684 selected_verts_with_faces
= []
685 for ed_idx
in all_edges_around_sel_verts
:
686 count_faces_of_edges_around_sel_verts
[ed_idx
] = all_edges_faces_count
[ed_idx
]
688 if all_edges_faces_count
[ed_idx
] > 0:
689 ed
= object.data
.edges
[ed_idx
]
691 if not ed
.vertices
[0] in selected_verts_with_faces
:
692 selected_verts_with_faces
.append(ed
.vertices
[0])
694 if not ed
.vertices
[1] in selected_verts_with_faces
:
695 selected_verts_with_faces
.append(ed
.vertices
[1])
697 all_verts_part_of_faces
.append(ed
.vertices
[0])
698 all_verts_part_of_faces
.append(ed
.vertices
[1])
700 tuple(selected_verts_with_faces
)
702 # Discard unneeded verts from calculations
703 participating_verts
= []
705 for v_idx
in all_selected_verts
:
706 vert_has_edges_with_one_face
= False
708 # Check if the actual vert has at least one edge connected to only one face
709 for ed_idx
in edges_connected_to_sel_verts
[v_idx
]:
710 if count_faces_of_edges_around_sel_verts
[ed_idx
] == 1:
711 vert_has_edges_with_one_face
= True
713 # If the vert has two or less edges connected and the vert is not part of any face.
714 # Or the vert is part of any face and at least one of
715 # the connected edges has only one face attached to it.
716 if (len(edges_connected_to_sel_verts
[v_idx
]) == 2 and
717 v_idx
not in all_verts_part_of_faces
) or \
718 len(edges_connected_to_sel_verts
[v_idx
]) == 1 or \
719 (v_idx
in all_verts_part_of_faces
and
720 vert_has_edges_with_one_face
):
722 participating_verts
.append(v_idx
)
724 if v_idx
not in all_verts_part_of_faces
:
725 movable_verts
.append(v_idx
)
727 # Remove from movable verts list those that are part of closed geometry (ie: triangles, quads)
728 for mv_idx
in movable_verts
:
730 mv_connected_verts
= verts_connected_to_every_vert
[mv_idx
]
732 for actual_v_idx
in all_selected_verts
:
733 count_shared_neighbors
= 0
736 for mv_conn_v_idx
in mv_connected_verts
:
737 if mv_idx
!= actual_v_idx
:
738 if mv_conn_v_idx
in verts_connected_to_every_vert
[actual_v_idx
] and \
739 mv_conn_v_idx
not in checked_verts
:
740 count_shared_neighbors
+= 1
741 checked_verts
.append(mv_conn_v_idx
)
743 if actual_v_idx
in mv_connected_verts
:
747 if count_shared_neighbors
== 2:
755 movable_verts
.remove(mv_idx
)
757 # Calculate merge distance for participating verts
758 shortest_edge_length
= None
759 for ed
in object.data
.edges
:
760 if ed
.vertices
[0] in movable_verts
and ed
.vertices
[1] in movable_verts
:
761 v1
= object.data
.vertices
[ed
.vertices
[0]]
762 v2
= object.data
.vertices
[ed
.vertices
[1]]
764 length
= (v1
.co
- v2
.co
).length
766 if shortest_edge_length
is None:
767 shortest_edge_length
= length
769 if length
< shortest_edge_length
:
770 shortest_edge_length
= length
772 if shortest_edge_length
is not None:
773 edges_merge_distance
= shortest_edge_length
* 0.5
775 edges_merge_distance
= 0
777 # Get together the verts near enough. They will be merged later
779 remaining_verts
+= participating_verts
780 for v1_idx
in participating_verts
:
781 if v1_idx
in remaining_verts
and v1_idx
in movable_verts
:
783 coords_verts_to_merge
= {}
785 verts_to_merge
.append(v1_idx
)
787 v1_co
= object.data
.vertices
[v1_idx
].co
788 coords_verts_to_merge
[v1_idx
] = (v1_co
[0], v1_co
[1], v1_co
[2])
790 for v2_idx
in remaining_verts
:
792 v2_co
= object.data
.vertices
[v2_idx
].co
794 dist
= (v1_co
- v2_co
).length
796 if dist
<= edges_merge_distance
: # Add the verts which are near enough
797 verts_to_merge
.append(v2_idx
)
799 coords_verts_to_merge
[v2_idx
] = (v2_co
[0], v2_co
[1], v2_co
[2])
801 for vm_idx
in verts_to_merge
:
802 remaining_verts
.remove(vm_idx
)
804 if len(verts_to_merge
) > 1:
805 # Calculate middle point of the verts to merge.
809 movable_verts_to_merge_count
= 0
810 for i
in range(len(verts_to_merge
)):
811 if verts_to_merge
[i
] in movable_verts
:
812 v_co
= object.data
.vertices
[verts_to_merge
[i
]].co
818 movable_verts_to_merge_count
+= 1
821 sum_x_co
/ movable_verts_to_merge_count
,
822 sum_y_co
/ movable_verts_to_merge_count
,
823 sum_z_co
/ movable_verts_to_merge_count
826 # Check if any vert to be merged is not movable
828 are_verts_not_movable
= False
829 verts_not_movable
= []
830 for v_merge_idx
in verts_to_merge
:
831 if v_merge_idx
in participating_verts
and v_merge_idx
not in movable_verts
:
832 are_verts_not_movable
= True
833 verts_not_movable
.append(v_merge_idx
)
835 if are_verts_not_movable
:
836 # Get the vert connected to faces, that is nearest to
837 # the middle point of the movable verts
839 for vcf_idx
in verts_not_movable
:
840 dist
= abs((object.data
.vertices
[vcf_idx
].co
-
841 Vector(middle_point_co
)).length
)
843 if shortest_dist
is None:
845 nearest_vert_idx
= vcf_idx
847 if dist
< shortest_dist
:
849 nearest_vert_idx
= vcf_idx
851 coords
= object.data
.vertices
[nearest_vert_idx
].co
852 target_point_co
= [coords
[0], coords
[1], coords
[2]]
854 target_point_co
= middle_point_co
856 # Move verts to merge to the middle position
857 for v_merge_idx
in verts_to_merge
:
858 if v_merge_idx
in movable_verts
: # Only move the verts that are not part of faces
859 object.data
.vertices
[v_merge_idx
].co
[0] = target_point_co
[0]
860 object.data
.vertices
[v_merge_idx
].co
[1] = target_point_co
[1]
861 object.data
.vertices
[v_merge_idx
].co
[2] = target_point_co
[2]
863 # Perform "Remove Doubles" to weld all the disconnected verts
864 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='EDIT')
865 bpy
.ops
.mesh
.remove_doubles(threshold
=0.0001)
867 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
869 # Get all the definitive selected edges, after weldding
871 edges_per_vert
= {} # Number of faces of each selected edge
872 for ed
in object.data
.edges
:
873 if object.data
.vertices
[ed
.vertices
[0]].select
and object.data
.vertices
[ed
.vertices
[1]].select
:
874 selected_edges
.append(ed
.index
)
876 # Save all the edges that belong to each vertex.
877 if not ed
.vertices
[0] in edges_per_vert
:
878 edges_per_vert
[ed
.vertices
[0]] = []
880 if not ed
.vertices
[1] in edges_per_vert
:
881 edges_per_vert
[ed
.vertices
[1]] = []
883 edges_per_vert
[ed
.vertices
[0]].append(ed
.index
)
884 edges_per_vert
[ed
.vertices
[1]].append(ed
.index
)
886 # Check if all the edges connected to each vert have two faces attached to them.
887 # To discard them later and make calculations faster
889 a
+= self
.edge_face_count(object)
891 verts_surrounded_by_faces
= {}
892 for v_idx
in edges_per_vert
:
893 edges_with_two_faces_count
= 0
895 for ed_idx
in edges_per_vert
[v_idx
]:
897 edges_with_two_faces_count
+= 1
899 if edges_with_two_faces_count
== len(edges_per_vert
[v_idx
]):
900 verts_surrounded_by_faces
[v_idx
] = True
902 verts_surrounded_by_faces
[v_idx
] = False
904 # Get all the selected vertices
905 selected_verts_idx
= []
906 for v
in object.data
.vertices
:
908 selected_verts_idx
.append(v
.index
)
910 # Get all the faces of the object
911 all_object_faces_verts_idx
= []
912 for face
in object.data
.polygons
:
914 face_verts
.append(face
.vertices
[0])
915 face_verts
.append(face
.vertices
[1])
916 face_verts
.append(face
.vertices
[2])
918 if len(face
.vertices
) == 4:
919 face_verts
.append(face
.vertices
[3])
921 all_object_faces_verts_idx
.append(face_verts
)
923 # Deselect all vertices
924 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='EDIT')
925 bpy
.ops
.mesh
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
926 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
928 # Make a dictionary with the verts related to each vert
929 related_key_verts
= {}
930 for ed_idx
in selected_edges
:
931 ed
= object.data
.edges
[ed_idx
]
933 if not verts_surrounded_by_faces
[ed
.vertices
[0]]:
934 if not ed
.vertices
[0] in related_key_verts
:
935 related_key_verts
[ed
.vertices
[0]] = []
937 if not ed
.vertices
[1] in related_key_verts
[ed
.vertices
[0]]:
938 related_key_verts
[ed
.vertices
[0]].append(ed
.vertices
[1])
940 if not verts_surrounded_by_faces
[ed
.vertices
[1]]:
941 if not ed
.vertices
[1] in related_key_verts
:
942 related_key_verts
[ed
.vertices
[1]] = []
944 if not ed
.vertices
[0] in related_key_verts
[ed
.vertices
[1]]:
945 related_key_verts
[ed
.vertices
[1]].append(ed
.vertices
[0])
947 # Get groups of verts forming each face
949 for v1
in related_key_verts
: # verts-1 ....
950 for v2
in related_key_verts
: # verts-2
952 related_verts_in_common
= []
955 for rel_v1
in related_key_verts
[v1
]:
956 # Check if related verts of verts-1 are related verts of verts-2
957 if rel_v1
in related_key_verts
[v2
]:
958 related_verts_in_common
.append(rel_v1
)
960 if v2
in related_key_verts
[v1
]:
963 if v1
in related_key_verts
[v2
]:
966 repeated_face
= False
967 # If two verts have two related verts in common, they form a quad
968 if len(related_verts_in_common
) == 2:
969 # Check if the face is already saved
970 all_faces_to_check_idx
= faces_verts_idx
+ all_object_faces_verts_idx
972 for f_verts
in all_faces_to_check_idx
:
975 if len(f_verts
) == 4:
980 if related_verts_in_common
[0] in f_verts
:
982 if related_verts_in_common
[1] in f_verts
:
985 if repeated_verts
== len(f_verts
):
989 if not repeated_face
:
990 faces_verts_idx
.append(
991 [v1
, related_verts_in_common
[0], v2
, related_verts_in_common
[1]]
994 # If Two verts have one related vert in common and
995 # they are related to each other, they form a triangle
996 elif v2_in_rel_v1
and v1_in_rel_v2
and len(related_verts_in_common
) == 1:
997 # Check if the face is already saved.
998 all_faces_to_check_idx
= faces_verts_idx
+ all_object_faces_verts_idx
1000 for f_verts
in all_faces_to_check_idx
:
1003 if len(f_verts
) == 3:
1008 if related_verts_in_common
[0] in f_verts
:
1011 if repeated_verts
== len(f_verts
):
1012 repeated_face
= True
1015 if not repeated_face
:
1016 faces_verts_idx
.append([v1
, related_verts_in_common
[0], v2
])
1018 # Keep only the faces that don't overlap by ignoring quads
1019 # that overlap with two adjacent triangles
1020 faces_to_not_include_idx
= [] # Indices of faces_verts_idx to eliminate
1021 all_faces_to_check_idx
= faces_verts_idx
+ all_object_faces_verts_idx
1022 for i
in range(len(faces_verts_idx
)):
1023 for t
in range(len(all_faces_to_check_idx
)):
1027 if len(faces_verts_idx
[i
]) == 4 and len(all_faces_to_check_idx
[t
]) == 3:
1028 for v_idx
in all_faces_to_check_idx
[t
]:
1029 if v_idx
in faces_verts_idx
[i
]:
1030 verts_in_common
+= 1
1031 # If it doesn't have all it's vertices repeated in the other face
1032 if verts_in_common
== 3:
1033 if i
not in faces_to_not_include_idx
:
1034 faces_to_not_include_idx
.append(i
)
1036 # Build faces discarding the ones in faces_to_not_include
1041 num_faces_created
= 0
1042 for i
in range(len(faces_verts_idx
)):
1043 if i
not in faces_to_not_include_idx
:
1044 bm
.faces
.new([bm
.verts
[v
] for v
in faces_verts_idx
[i
]])
1046 num_faces_created
+= 1
1051 for v_idx
in selected_verts_idx
:
1052 self
.main_object
.data
.vertices
[v_idx
].select
= True
1054 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='EDIT')
1055 bpy
.ops
.mesh
.normals_make_consistent(inside
=False)
1056 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
1060 return num_faces_created
1062 # Crosshatch skinning
1063 def crosshatch_surface_invoke(self
, ob_original_splines
):
1064 self
.is_crosshatch
= False
1065 self
.crosshatch_merge_distance
= 0
1067 objects_to_delete
= [] # duplicated strokes to be deleted.
1069 # If the main object uses modifiers deactivate them temporarily until the surface is joined
1070 # (without this the surface verts merging with the main object doesn't work well)
1071 self
.modifiers_prev_viewport_state
= []
1072 if len(self
.main_object
.modifiers
) > 0:
1073 for m_idx
in range(len(self
.main_object
.modifiers
)):
1074 self
.modifiers_prev_viewport_state
.append(
1075 self
.main_object
.modifiers
[m_idx
].show_viewport
1077 self
.main_object
.modifiers
[m_idx
].show_viewport
= False
1079 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
1080 ob_original_splines
.select_set(True)
1081 bpy
.context
.view_layer
.objects
.active
= ob_original_splines
1083 if len(ob_original_splines
.data
.splines
) >= 2:
1084 bpy
.ops
.object.duplicate('INVOKE_REGION_WIN')
1085 ob_splines
= bpy
.context
.object
1086 ob_splines
.name
= "SURFSKIO_NE_STR"
1088 # Get estimative merge distance (sum up the distances from the first point to
1089 # all other points, then average them and then divide them)
1090 first_point_dist_sum
= 0
1093 coords_first_pt
= ob_splines
.data
.splines
[0].bezier_points
[0].co
1094 for i
in range(len(ob_splines
.data
.splines
)):
1095 sp
= ob_splines
.data
.splines
[i
]
1097 if coords_first_pt
!= sp
.bezier_points
[0].co
:
1098 first_dist
= (coords_first_pt
- sp
.bezier_points
[0].co
).length
1100 if coords_first_pt
!= sp
.bezier_points
[len(sp
.bezier_points
) - 1].co
:
1101 second_dist
= (coords_first_pt
- sp
.bezier_points
[len(sp
.bezier_points
) - 1].co
).length
1103 first_point_dist_sum
+= first_dist
+ second_dist
1107 shortest_dist
= first_dist
1108 elif second_dist
!= 0:
1109 shortest_dist
= second_dist
1111 if shortest_dist
> first_dist
and first_dist
!= 0:
1112 shortest_dist
= first_dist
1114 if shortest_dist
> second_dist
and second_dist
!= 0:
1115 shortest_dist
= second_dist
1117 self
.crosshatch_merge_distance
= shortest_dist
/ 20
1119 # Recalculation of merge distance
1121 bpy
.ops
.object.duplicate('INVOKE_REGION_WIN')
1123 ob_calc_merge_dist
= bpy
.context
.object
1124 ob_calc_merge_dist
.name
= "SURFSKIO_CALC_TMP"
1126 objects_to_delete
.append(ob_calc_merge_dist
)
1128 # Smooth out strokes a little to improve crosshatch detection
1129 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1130 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='SELECT')
1133 bpy
.ops
.curve
.smooth('INVOKE_REGION_WIN')
1135 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
1136 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1138 # Convert curves into mesh
1139 ob_calc_merge_dist
.data
.resolution_u
= 12
1140 bpy
.ops
.object.convert(target
='MESH', keep_original
=False)
1142 # Find "intersection-nodes"
1143 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1144 bpy
.ops
.mesh
.select_all('INVOKE_REGION_WIN', action
='SELECT')
1145 bpy
.ops
.mesh
.remove_doubles('INVOKE_REGION_WIN',
1146 threshold
=self
.crosshatch_merge_distance
)
1147 bpy
.ops
.mesh
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
1148 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1150 # Remove verts with less than three edges
1151 verts_edges_count
= {}
1152 for ed
in ob_calc_merge_dist
.data
.edges
:
1155 if v
[0] not in verts_edges_count
:
1156 verts_edges_count
[v
[0]] = 0
1158 if v
[1] not in verts_edges_count
:
1159 verts_edges_count
[v
[1]] = 0
1161 verts_edges_count
[v
[0]] += 1
1162 verts_edges_count
[v
[1]] += 1
1164 nodes_verts_coords
= []
1165 for v_idx
in verts_edges_count
:
1166 v
= ob_calc_merge_dist
.data
.vertices
[v_idx
]
1168 if verts_edges_count
[v_idx
] < 3:
1172 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1173 bpy
.ops
.mesh
.delete('INVOKE_REGION_WIN', type='VERT')
1174 bpy
.ops
.mesh
.select_all('INVOKE_REGION_WIN', action
='SELECT')
1176 # Remove doubles to discard very near verts from calculations of distance
1177 bpy
.ops
.mesh
.remove_doubles(
1178 'INVOKE_REGION_WIN',
1179 threshold
=self
.crosshatch_merge_distance
* 4.0
1181 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1183 # Get all coords of the resulting nodes
1184 nodes_verts_coords
= [(v
.co
[0], v
.co
[1], v
.co
[2]) for
1185 v
in ob_calc_merge_dist
.data
.vertices
]
1187 # Check if the strokes are a crosshatch
1188 if len(nodes_verts_coords
) >= 3:
1189 self
.is_crosshatch
= True
1191 shortest_dist
= None
1192 for co_1
in nodes_verts_coords
:
1193 for co_2
in nodes_verts_coords
:
1195 dist
= (Vector(co_1
) - Vector(co_2
)).length
1197 if shortest_dist
is not None:
1198 if dist
< shortest_dist
:
1199 shortest_dist
= dist
1201 shortest_dist
= dist
1203 self
.crosshatch_merge_distance
= shortest_dist
/ 3
1205 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
1206 ob_splines
.select_set(True)
1207 bpy
.context
.view_layer
.objects
.active
= ob_splines
1209 # Deselect all points
1210 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1211 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
1212 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1214 # Smooth splines in a localized way, to eliminate "saw-teeth"
1215 # like shapes when there are many points
1216 for sp
in ob_splines
.data
.splines
:
1219 angle_limit
= 2 # Degrees
1220 for t
in range(len(sp
.bezier_points
)):
1221 # Because on each iteration it checks the "next two points"
1222 # of the actual. This way it doesn't go out of range
1223 if t
<= len(sp
.bezier_points
) - 3:
1224 p1
= sp
.bezier_points
[t
]
1225 p2
= sp
.bezier_points
[t
+ 1]
1226 p3
= sp
.bezier_points
[t
+ 2]
1228 vec_1
= p1
.co
- p2
.co
1229 vec_2
= p2
.co
- p3
.co
1231 if p2
.co
!= p1
.co
and p2
.co
!= p3
.co
:
1232 angle
= vec_1
.angle(vec_2
)
1233 angle_sum
+= degrees(angle
)
1235 if angle_sum
>= angle_limit
: # If sum of angles is grater than the limit
1236 if (p1
.co
- p2
.co
).length
<= self
.crosshatch_merge_distance
:
1237 p1
.select_control_point
= True
1238 p1
.select_left_handle
= True
1239 p1
.select_right_handle
= True
1241 p2
.select_control_point
= True
1242 p2
.select_left_handle
= True
1243 p2
.select_right_handle
= True
1245 if (p1
.co
- p2
.co
).length
<= self
.crosshatch_merge_distance
:
1246 p3
.select_control_point
= True
1247 p3
.select_left_handle
= True
1248 p3
.select_right_handle
= True
1252 sp
.bezier_points
[0].select_control_point
= False
1253 sp
.bezier_points
[0].select_left_handle
= False
1254 sp
.bezier_points
[0].select_right_handle
= False
1256 sp
.bezier_points
[len(sp
.bezier_points
) - 1].select_control_point
= False
1257 sp
.bezier_points
[len(sp
.bezier_points
) - 1].select_left_handle
= False
1258 sp
.bezier_points
[len(sp
.bezier_points
) - 1].select_right_handle
= False
1260 # Smooth out strokes a little to improve crosshatch detection
1261 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1264 bpy
.ops
.curve
.smooth('INVOKE_REGION_WIN')
1266 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
1267 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1269 # Simplify the splines
1270 for sp
in ob_splines
.data
.splines
:
1273 sp
.bezier_points
[0].select_control_point
= True
1274 sp
.bezier_points
[0].select_left_handle
= True
1275 sp
.bezier_points
[0].select_right_handle
= True
1277 sp
.bezier_points
[len(sp
.bezier_points
) - 1].select_control_point
= True
1278 sp
.bezier_points
[len(sp
.bezier_points
) - 1].select_left_handle
= True
1279 sp
.bezier_points
[len(sp
.bezier_points
) - 1].select_right_handle
= True
1281 angle_limit
= 15 # Degrees
1282 for t
in range(len(sp
.bezier_points
)):
1283 # Because on each iteration it checks the "next two points"
1284 # of the actual. This way it doesn't go out of range
1285 if t
<= len(sp
.bezier_points
) - 3:
1286 p1
= sp
.bezier_points
[t
]
1287 p2
= sp
.bezier_points
[t
+ 1]
1288 p3
= sp
.bezier_points
[t
+ 2]
1290 vec_1
= p1
.co
- p2
.co
1291 vec_2
= p2
.co
- p3
.co
1293 if p2
.co
!= p1
.co
and p2
.co
!= p3
.co
:
1294 angle
= vec_1
.angle(vec_2
)
1295 angle_sum
+= degrees(angle
)
1296 # If sum of angles is grater than the limit
1297 if angle_sum
>= angle_limit
:
1298 p1
.select_control_point
= True
1299 p1
.select_left_handle
= True
1300 p1
.select_right_handle
= True
1302 p2
.select_control_point
= True
1303 p2
.select_left_handle
= True
1304 p2
.select_right_handle
= True
1306 p3
.select_control_point
= True
1307 p3
.select_left_handle
= True
1308 p3
.select_right_handle
= True
1312 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1313 bpy
.ops
.curve
.select_all(action
='INVERT')
1315 bpy
.ops
.curve
.delete(type='VERT')
1316 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1318 objects_to_delete
.append(ob_splines
)
1320 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1321 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
1322 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1324 # Check if the strokes are a crosshatch
1325 if self
.is_crosshatch
:
1326 all_points_coords
= []
1327 for i
in range(len(ob_splines
.data
.splines
)):
1328 all_points_coords
.append([])
1330 all_points_coords
[i
] = [Vector((x
, y
, z
)) for
1331 x
, y
, z
in [bp
.co
for
1332 bp
in ob_splines
.data
.splines
[i
].bezier_points
]]
1334 all_intersections
= []
1335 checked_splines
= []
1336 for i
in range(len(all_points_coords
)):
1338 for t
in range(len(all_points_coords
[i
]) - 1):
1339 bp1_co
= all_points_coords
[i
][t
]
1340 bp2_co
= all_points_coords
[i
][t
+ 1]
1342 for i2
in range(len(all_points_coords
)):
1343 if i
!= i2
and i2
not in checked_splines
:
1344 for t2
in range(len(all_points_coords
[i2
]) - 1):
1345 bp3_co
= all_points_coords
[i2
][t2
]
1346 bp4_co
= all_points_coords
[i2
][t2
+ 1]
1348 intersec_coords
= intersect_line_line(
1349 bp1_co
, bp2_co
, bp3_co
, bp4_co
1351 if intersec_coords
is not None:
1352 dist
= (intersec_coords
[0] - intersec_coords
[1]).length
1354 if dist
<= self
.crosshatch_merge_distance
* 1.5:
1355 _temp_co
, percent1
= intersect_point_line(
1356 intersec_coords
[0], bp1_co
, bp2_co
1358 if (percent1
>= -0.02 and percent1
<= 1.02):
1359 _temp_co
, percent2
= intersect_point_line(
1360 intersec_coords
[1], bp3_co
, bp4_co
1362 if (percent2
>= -0.02 and percent2
<= 1.02):
1363 # Format: spline index, first point index from
1364 # corresponding segment, percentage from first point of
1365 # actual segment, coords of intersection point
1366 all_intersections
.append(
1368 ob_splines
.matrix_world
@ intersec_coords
[0])
1370 all_intersections
.append(
1372 ob_splines
.matrix_world
@ intersec_coords
[1])
1375 checked_splines
.append(i
)
1376 # Sort list by spline, then by corresponding first point index of segment,
1377 # and then by percentage from first point of segment: elements 0 and 1 respectively
1378 all_intersections
.sort(key
=operator
.itemgetter(0, 1, 2))
1380 self
.crosshatch_strokes_coords
= {}
1381 for i
in range(len(all_intersections
)):
1382 if not all_intersections
[i
][0] in self
.crosshatch_strokes_coords
:
1383 self
.crosshatch_strokes_coords
[all_intersections
[i
][0]] = []
1385 self
.crosshatch_strokes_coords
[all_intersections
[i
][0]].append(
1386 all_intersections
[i
][3]
1387 ) # Save intersection coords
1389 self
.is_crosshatch
= False
1391 # Delete all duplicates
1392 bpy
.ops
.object.delete({"selected_objects": objects_to_delete
})
1394 # If the main object has modifiers, turn their "viewport view status" to
1395 # what it was before the forced deactivation above
1396 if len(self
.main_object
.modifiers
) > 0:
1397 for m_idx
in range(len(self
.main_object
.modifiers
)):
1398 self
.main_object
.modifiers
[m_idx
].show_viewport
= self
.modifiers_prev_viewport_state
[m_idx
]
1404 # Part of the Crosshatch process that is repeated when the operator is tweaked
1405 def crosshatch_surface_execute(self
, context
):
1406 # If the main object uses modifiers deactivate them temporarily until the surface is joined
1407 # (without this the surface verts merging with the main object doesn't work well)
1408 self
.modifiers_prev_viewport_state
= []
1409 if len(self
.main_object
.modifiers
) > 0:
1410 for m_idx
in range(len(self
.main_object
.modifiers
)):
1411 self
.modifiers_prev_viewport_state
.append(self
.main_object
.modifiers
[m_idx
].show_viewport
)
1413 self
.main_object
.modifiers
[m_idx
].show_viewport
= False
1415 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1417 me_name
= "SURFSKIO_STK_TMP"
1418 me
= bpy
.data
.meshes
.new(me_name
)
1420 all_verts_coords
= []
1422 for st_idx
in self
.crosshatch_strokes_coords
:
1423 for co_idx
in range(len(self
.crosshatch_strokes_coords
[st_idx
])):
1424 coords
= self
.crosshatch_strokes_coords
[st_idx
][co_idx
]
1426 all_verts_coords
.append(coords
)
1429 all_edges
.append((len(all_verts_coords
) - 2, len(all_verts_coords
) - 1))
1431 me
.from_pydata(all_verts_coords
, all_edges
, [])
1432 ob
= object_utils
.object_data_add(context
, me
)
1433 ob
.location
= (0.0, 0.0, 0.0)
1434 ob
.rotation_euler
= (0.0, 0.0, 0.0)
1435 ob
.scale
= (1.0, 1.0, 1.0)
1437 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
1439 bpy
.context
.view_layer
.objects
.active
= ob
1441 # Get together each vert and its nearest, to the middle position
1442 verts
= ob
.data
.vertices
1444 for i
in range(len(verts
)):
1445 shortest_dist
= None
1447 if i
not in checked_verts
:
1448 for t
in range(len(verts
)):
1449 if i
!= t
and t
not in checked_verts
:
1450 dist
= (verts
[i
].co
- verts
[t
].co
).length
1452 if shortest_dist
is not None:
1453 if dist
< shortest_dist
:
1454 shortest_dist
= dist
1457 shortest_dist
= dist
1460 middle_location
= (verts
[i
].co
+ verts
[nearest_vert
].co
) / 2
1462 verts
[i
].co
= middle_location
1463 verts
[nearest_vert
].co
= middle_location
1465 checked_verts
.append(i
)
1466 checked_verts
.append(nearest_vert
)
1468 # Calculate average length between all the generated edges
1469 ob
= bpy
.context
.object
1471 for ed
in ob
.data
.edges
:
1472 v1
= ob
.data
.vertices
[ed
.vertices
[0]]
1473 v2
= ob
.data
.vertices
[ed
.vertices
[1]]
1475 lengths_sum
+= (v1
.co
- v2
.co
).length
1477 edges_count
= len(ob
.data
.edges
)
1478 # possible division by zero here
1479 average_edge_length
= lengths_sum
/ edges_count
if edges_count
!= 0 else 0.0001
1481 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1482 bpy
.ops
.mesh
.select_all('INVOKE_REGION_WIN', action
='SELECT')
1483 bpy
.ops
.mesh
.remove_doubles('INVOKE_REGION_WIN',
1484 threshold
=average_edge_length
/ 15.0)
1485 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1487 final_points_ob
= bpy
.context
.view_layer
.objects
.active
1489 # Make a dictionary with the verts related to each vert
1490 related_key_verts
= {}
1491 for ed
in final_points_ob
.data
.edges
:
1492 if not ed
.vertices
[0] in related_key_verts
:
1493 related_key_verts
[ed
.vertices
[0]] = []
1495 if not ed
.vertices
[1] in related_key_verts
:
1496 related_key_verts
[ed
.vertices
[1]] = []
1498 if not ed
.vertices
[1] in related_key_verts
[ed
.vertices
[0]]:
1499 related_key_verts
[ed
.vertices
[0]].append(ed
.vertices
[1])
1501 if not ed
.vertices
[0] in related_key_verts
[ed
.vertices
[1]]:
1502 related_key_verts
[ed
.vertices
[1]].append(ed
.vertices
[0])
1504 # Get groups of verts forming each face
1505 faces_verts_idx
= []
1506 for v1
in related_key_verts
: # verts-1 ....
1507 for v2
in related_key_verts
: # verts-2
1509 related_verts_in_common
= []
1510 v2_in_rel_v1
= False
1511 v1_in_rel_v2
= False
1512 for rel_v1
in related_key_verts
[v1
]:
1513 # Check if related verts of verts-1 are related verts of verts-2
1514 if rel_v1
in related_key_verts
[v2
]:
1515 related_verts_in_common
.append(rel_v1
)
1517 if v2
in related_key_verts
[v1
]:
1520 if v1
in related_key_verts
[v2
]:
1523 repeated_face
= False
1524 # If two verts have two related verts in common, they form a quad
1525 if len(related_verts_in_common
) == 2:
1526 # Check if the face is already saved
1527 for f_verts
in faces_verts_idx
:
1530 if len(f_verts
) == 4:
1535 if related_verts_in_common
[0] in f_verts
:
1537 if related_verts_in_common
[1] in f_verts
:
1540 if repeated_verts
== len(f_verts
):
1541 repeated_face
= True
1544 if not repeated_face
:
1545 faces_verts_idx
.append([v1
, related_verts_in_common
[0],
1546 v2
, related_verts_in_common
[1]])
1548 # If Two verts have one related vert in common and they are
1549 # related to each other, they form a triangle
1550 elif v2_in_rel_v1
and v1_in_rel_v2
and len(related_verts_in_common
) == 1:
1551 # Check if the face is already saved.
1552 for f_verts
in faces_verts_idx
:
1555 if len(f_verts
) == 3:
1560 if related_verts_in_common
[0] in f_verts
:
1563 if repeated_verts
== len(f_verts
):
1564 repeated_face
= True
1567 if not repeated_face
:
1568 faces_verts_idx
.append([v1
, related_verts_in_common
[0], v2
])
1570 # Keep only the faces that don't overlap by ignoring
1571 # quads that overlap with two adjacent triangles
1572 faces_to_not_include_idx
= [] # Indices of faces_verts_idx to eliminate
1573 for i
in range(len(faces_verts_idx
)):
1574 for t
in range(len(faces_verts_idx
)):
1578 if len(faces_verts_idx
[i
]) == 4 and len(faces_verts_idx
[t
]) == 3:
1579 for v_idx
in faces_verts_idx
[t
]:
1580 if v_idx
in faces_verts_idx
[i
]:
1581 verts_in_common
+= 1
1582 # If it doesn't have all it's vertices repeated in the other face
1583 if verts_in_common
== 3:
1584 if i
not in faces_to_not_include_idx
:
1585 faces_to_not_include_idx
.append(i
)
1588 all_surface_verts_co
= []
1589 for i
in range(len(final_points_ob
.data
.vertices
)):
1590 coords
= final_points_ob
.data
.vertices
[i
].co
1591 all_surface_verts_co
.append([coords
[0], coords
[1], coords
[2]])
1593 # Verts of each face.
1594 all_surface_faces
= []
1595 for i
in range(len(faces_verts_idx
)):
1596 if i
not in faces_to_not_include_idx
:
1598 for v_idx
in faces_verts_idx
[i
]:
1601 all_surface_faces
.append(face
)
1604 surf_me_name
= "SURFSKIO_surface"
1605 me_surf
= bpy
.data
.meshes
.new(surf_me_name
)
1606 me_surf
.from_pydata(all_surface_verts_co
, [], all_surface_faces
)
1607 ob_surface
= object_utils
.object_data_add(context
, me_surf
)
1608 ob_surface
.location
= (0.0, 0.0, 0.0)
1609 ob_surface
.rotation_euler
= (0.0, 0.0, 0.0)
1610 ob_surface
.scale
= (1.0, 1.0, 1.0)
1612 # Delete final points temporal object
1613 bpy
.ops
.object.delete({"selected_objects": [final_points_ob
]})
1615 # Delete isolated verts if there are any
1616 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
1617 ob_surface
.select_set(True)
1618 bpy
.context
.view_layer
.objects
.active
= ob_surface
1620 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1621 bpy
.ops
.mesh
.select_all(action
='DESELECT')
1622 bpy
.ops
.mesh
.select_face_by_sides(type='NOTEQUAL')
1623 bpy
.ops
.mesh
.delete()
1624 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1626 # Join crosshatch results with original mesh
1628 # Calculate a distance to merge the verts of the crosshatch surface to the main object
1629 edges_length_sum
= 0
1630 for ed
in ob_surface
.data
.edges
:
1631 edges_length_sum
+= (
1632 ob_surface
.data
.vertices
[ed
.vertices
[0]].co
-
1633 ob_surface
.data
.vertices
[ed
.vertices
[1]].co
1636 # Make dictionary with all the verts connected to each vert, on the new surface object.
1637 surface_connected_verts
= {}
1638 for ed
in ob_surface
.data
.edges
:
1639 if not ed
.vertices
[0] in surface_connected_verts
:
1640 surface_connected_verts
[ed
.vertices
[0]] = []
1642 surface_connected_verts
[ed
.vertices
[0]].append(ed
.vertices
[1])
1644 if ed
.vertices
[1] not in surface_connected_verts
:
1645 surface_connected_verts
[ed
.vertices
[1]] = []
1647 surface_connected_verts
[ed
.vertices
[1]].append(ed
.vertices
[0])
1649 # Duplicate the new surface object, and use shrinkwrap to
1650 # calculate later the nearest verts to the main object
1651 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1652 bpy
.ops
.mesh
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
1653 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1655 bpy
.ops
.object.duplicate('INVOKE_REGION_WIN')
1657 final_ob_duplicate
= bpy
.context
.view_layer
.objects
.active
1659 shrinkwrap_modifier
= context
.object.modifiers
.new("", 'SHRINKWRAP')
1660 shrinkwrap_modifier
.wrap_method
= "NEAREST_VERTEX"
1661 shrinkwrap_modifier
.target
= self
.main_object
1663 bpy
.ops
.object.modifier_apply('INVOKE_REGION_WIN', modifier
=shrinkwrap_modifier
.name
)
1665 # Make list with verts of original mesh as index and coords as value
1666 main_object_verts_coords
= []
1667 for v
in self
.main_object
.data
.vertices
:
1668 coords
= self
.main_object
.matrix_world
@ v
.co
1670 # To avoid problems when taking "-0.00" as a different value as "0.00"
1671 for c
in range(len(coords
)):
1672 if "%.3f" % coords
[c
] == "-0.00":
1675 main_object_verts_coords
.append(["%.3f" % coords
[0], "%.3f" % coords
[1], "%.3f" % coords
[2]])
1677 tuple(main_object_verts_coords
)
1679 # Determine which verts will be merged, snap them to the nearest verts
1680 # on the original verts, and get them selected
1681 crosshatch_verts_to_merge
= []
1682 if self
.automatic_join
:
1683 for i
in range(len(ob_surface
.data
.vertices
)-1):
1684 # Calculate the distance from each of the connected verts to the actual vert,
1685 # and compare it with the distance they would have if joined.
1686 # If they don't change much, that vert can be joined
1687 merge_actual_vert
= True
1689 if len(surface_connected_verts
[i
]) < 4:
1690 for c_v_idx
in surface_connected_verts
[i
]:
1691 points_original
= []
1692 points_original
.append(ob_surface
.data
.vertices
[c_v_idx
].co
)
1693 points_original
.append(ob_surface
.data
.vertices
[i
].co
)
1696 points_target
.append(ob_surface
.data
.vertices
[c_v_idx
].co
)
1697 points_target
.append(final_ob_duplicate
.data
.vertices
[i
].co
)
1699 vec_A
= points_original
[0] - points_original
[1]
1700 vec_B
= points_target
[0] - points_target
[1]
1702 dist_A
= (points_original
[0] - points_original
[1]).length
1703 dist_B
= (points_target
[0] - points_target
[1]).length
1706 points_original
[0] == points_original
[1] or
1707 points_target
[0] == points_target
[1]
1708 ): # If any vector's length is zero
1710 angle
= vec_A
.angle(vec_B
) / pi
1714 # Set a range of acceptable variation in the connected edges
1715 if dist_B
> dist_A
* 1.7 * self
.join_stretch_factor
or \
1716 dist_B
< dist_A
/ 2 / self
.join_stretch_factor
or \
1717 angle
>= 0.15 * self
.join_stretch_factor
:
1719 merge_actual_vert
= False
1722 merge_actual_vert
= False
1724 self
.report({'WARNING'},
1725 "Crosshatch set incorrectly")
1727 if merge_actual_vert
:
1728 coords
= final_ob_duplicate
.data
.vertices
[i
].co
1729 # To avoid problems when taking "-0.000" as a different value as "0.00"
1730 for c
in range(len(coords
)):
1731 if "%.3f" % coords
[c
] == "-0.00":
1734 comparison_coords
= ["%.3f" % coords
[0], "%.3f" % coords
[1], "%.3f" % coords
[2]]
1736 if comparison_coords
in main_object_verts_coords
:
1737 # Get the index of the vert with those coords in the main object
1738 main_object_related_vert_idx
= main_object_verts_coords
.index(comparison_coords
)
1740 if self
.main_object
.data
.vertices
[main_object_related_vert_idx
].select
is True or \
1741 self
.main_object_selected_verts_count
== 0:
1743 ob_surface
.data
.vertices
[i
].co
= final_ob_duplicate
.data
.vertices
[i
].co
1744 ob_surface
.data
.vertices
[i
].select
= True
1745 crosshatch_verts_to_merge
.append(i
)
1747 # Make sure the vert in the main object is selected,
1748 # in case it wasn't selected and the "join crosshatch" option is active
1749 self
.main_object
.data
.vertices
[main_object_related_vert_idx
].select
= True
1751 # Delete duplicated object
1752 bpy
.ops
.object.delete({"selected_objects": [final_ob_duplicate
]})
1754 # Join crosshatched surface and main object
1755 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
1756 ob_surface
.select_set(True)
1757 self
.main_object
.select_set(True)
1758 bpy
.context
.view_layer
.objects
.active
= self
.main_object
1760 bpy
.ops
.object.join('INVOKE_REGION_WIN')
1762 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1763 # Perform Remove doubles to merge verts
1764 if not (self
.automatic_join
is False and self
.main_object_selected_verts_count
== 0):
1765 bpy
.ops
.mesh
.remove_doubles(threshold
=0.0001)
1767 bpy
.ops
.mesh
.select_all(action
='DESELECT')
1769 # If the main object has modifiers, turn their "viewport view status"
1770 # to what it was before the forced deactivation above
1771 if len(self
.main_object
.modifiers
) > 0:
1772 for m_idx
in range(len(self
.main_object
.modifiers
)):
1773 self
.main_object
.modifiers
[m_idx
].show_viewport
= self
.modifiers_prev_viewport_state
[m_idx
]
1779 def rectangular_surface(self
, context
):
1781 all_selected_edges_idx
= []
1782 all_selected_verts
= []
1784 for ed
in self
.main_object
.data
.edges
:
1786 all_selected_edges_idx
.append(ed
.index
)
1789 if not ed
.vertices
[0] in all_selected_verts
:
1790 all_selected_verts
.append(self
.main_object
.data
.vertices
[ed
.vertices
[0]])
1791 if not ed
.vertices
[1] in all_selected_verts
:
1792 all_selected_verts
.append(self
.main_object
.data
.vertices
[ed
.vertices
[1]])
1794 # All verts (both from each edge) to determine later
1795 # which are at the tips (those not repeated twice)
1796 all_verts_idx
.append(ed
.vertices
[0])
1797 all_verts_idx
.append(ed
.vertices
[1])
1799 # Identify the tips and "middle-vertex" that separates U from V, if there is one
1800 all_chains_tips_idx
= []
1801 for v_idx
in all_verts_idx
:
1802 if all_verts_idx
.count(v_idx
) < 2:
1803 all_chains_tips_idx
.append(v_idx
)
1805 edges_connected_to_tips
= []
1806 for ed
in self
.main_object
.data
.edges
:
1807 if (ed
.vertices
[0] in all_chains_tips_idx
or ed
.vertices
[1] in all_chains_tips_idx
) and \
1808 not (ed
.vertices
[0] in all_verts_idx
and ed
.vertices
[1] in all_verts_idx
):
1810 edges_connected_to_tips
.append(ed
)
1812 # Check closed selections
1813 # List with groups of three verts, where the first element of the pair is
1814 # the unselected vert of a closed selection and the other two elements are the
1815 # selected neighbor verts (it will be useful to determine which selection chain
1816 # the unselected vert belongs to, and determine the "middle-vertex")
1817 single_unselected_verts_and_neighbors
= []
1819 # To identify a "closed" selection (a selection that is a closed chain except
1820 # for one vertex) find the vertex in common that have the edges connected to tips.
1821 # If there is a vertex in common, that one is the unselected vert that closes
1822 # the selection or is a "middle-vertex"
1823 single_unselected_verts
= []
1824 for ed
in edges_connected_to_tips
:
1825 for ed_b
in edges_connected_to_tips
:
1827 if ed
.vertices
[0] == ed_b
.vertices
[0] and \
1828 not self
.main_object
.data
.vertices
[ed
.vertices
[0]].select
and \
1829 ed
.vertices
[0] not in single_unselected_verts
:
1831 # The second element is one of the tips of the selected
1832 # vertices of the closed selection
1833 single_unselected_verts_and_neighbors
.append(
1834 [ed
.vertices
[0], ed
.vertices
[1], ed_b
.vertices
[1]]
1836 single_unselected_verts
.append(ed
.vertices
[0])
1838 elif ed
.vertices
[0] == ed_b
.vertices
[1] and \
1839 not self
.main_object
.data
.vertices
[ed
.vertices
[0]].select
and \
1840 ed
.vertices
[0] not in single_unselected_verts
:
1842 single_unselected_verts_and_neighbors
.append(
1843 [ed
.vertices
[0], ed
.vertices
[1], ed_b
.vertices
[0]]
1845 single_unselected_verts
.append(ed
.vertices
[0])
1847 elif ed
.vertices
[1] == ed_b
.vertices
[0] and \
1848 not self
.main_object
.data
.vertices
[ed
.vertices
[1]].select
and \
1849 ed
.vertices
[1] not in single_unselected_verts
:
1851 single_unselected_verts_and_neighbors
.append(
1852 [ed
.vertices
[1], ed
.vertices
[0], ed_b
.vertices
[1]]
1854 single_unselected_verts
.append(ed
.vertices
[1])
1856 elif ed
.vertices
[1] == ed_b
.vertices
[1] and \
1857 not self
.main_object
.data
.vertices
[ed
.vertices
[1]].select
and \
1858 ed
.vertices
[1] not in single_unselected_verts
:
1860 single_unselected_verts_and_neighbors
.append(
1861 [ed
.vertices
[1], ed
.vertices
[0], ed_b
.vertices
[0]]
1863 single_unselected_verts
.append(ed
.vertices
[1])
1866 middle_vertex_idx
= None
1867 tips_to_discard_idx
= []
1869 # Check if there is a "middle-vertex", and get its index
1870 for i
in range(0, len(single_unselected_verts_and_neighbors
)):
1871 actual_chain_verts
= self
.get_ordered_verts(
1872 self
.main_object
, all_selected_edges_idx
,
1873 all_verts_idx
, single_unselected_verts_and_neighbors
[i
][1],
1877 if single_unselected_verts_and_neighbors
[i
][2] != \
1878 actual_chain_verts
[len(actual_chain_verts
) - 1].index
:
1880 middle_vertex_idx
= single_unselected_verts_and_neighbors
[i
][0]
1881 tips_to_discard_idx
.append(single_unselected_verts_and_neighbors
[i
][1])
1882 tips_to_discard_idx
.append(single_unselected_verts_and_neighbors
[i
][2])
1884 # List with pairs of verts that belong to the tips of each selection chain (row)
1885 verts_tips_same_chain_idx
= []
1886 if len(all_chains_tips_idx
) >= 2:
1888 for i
in range(0, len(all_chains_tips_idx
)):
1889 if all_chains_tips_idx
[i
] not in checked_v
:
1890 v_chain
= self
.get_ordered_verts(
1891 self
.main_object
, all_selected_edges_idx
,
1892 all_verts_idx
, all_chains_tips_idx
[i
],
1893 middle_vertex_idx
, None
1896 verts_tips_same_chain_idx
.append([v_chain
[0].index
, v_chain
[len(v_chain
) - 1].index
])
1898 checked_v
.append(v_chain
[0].index
)
1899 checked_v
.append(v_chain
[len(v_chain
) - 1].index
)
1901 # Selection tips (vertices).
1902 verts_tips_parsed_idx
= []
1903 if len(all_chains_tips_idx
) >= 2:
1904 for spec_v_idx
in all_chains_tips_idx
:
1905 if (spec_v_idx
not in tips_to_discard_idx
):
1906 verts_tips_parsed_idx
.append(spec_v_idx
)
1908 # Identify the type of selection made by the user
1909 if middle_vertex_idx
is not None:
1910 # If there are 4 tips (two selection chains), and
1911 # there is only one single unselected vert (the middle vert)
1912 if len(all_chains_tips_idx
) == 4 and len(single_unselected_verts_and_neighbors
) == 1:
1913 selection_type
= "TWO_CONNECTED"
1915 # The type of the selection was not identified, the script stops.
1916 self
.report({'WARNING'}, "The selection isn't valid.")
1918 self
.stopping_errors
= True
1922 if len(all_chains_tips_idx
) == 2: # If there are 2 tips
1923 selection_type
= "SINGLE"
1924 elif len(all_chains_tips_idx
) == 4: # If there are 4 tips
1925 selection_type
= "TWO_NOT_CONNECTED"
1926 elif len(all_chains_tips_idx
) == 0:
1927 if len(self
.main_splines
.data
.splines
) > 1:
1928 selection_type
= "NO_SELECTION"
1930 # If the selection was not identified and there is only one stroke,
1931 # there's no possibility to build a surface, so the script is interrupted
1932 self
.report({'WARNING'}, "The selection isn't valid.")
1934 self
.stopping_errors
= True
1938 # The type of the selection was not identified, the script stops
1939 self
.report({'WARNING'}, "The selection isn't valid.")
1941 self
.stopping_errors
= True
1945 # If the selection type is TWO_NOT_CONNECTED and there is only one stroke, stop the script
1946 if selection_type
== "TWO_NOT_CONNECTED" and len(self
.main_splines
.data
.splines
) == 1:
1947 self
.report({'WARNING'},
1948 "At least two strokes are needed when there are two not connected selections")
1950 self
.stopping_errors
= True
1954 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1956 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
1957 self
.main_splines
.select_set(True)
1958 bpy
.context
.view_layer
.objects
.active
= self
.main_splines
1960 # Enter editmode for the new curve (converted from grease pencil strokes), to smooth it out
1961 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1962 bpy
.ops
.curve
.smooth('INVOKE_REGION_WIN')
1963 bpy
.ops
.curve
.smooth('INVOKE_REGION_WIN')
1964 bpy
.ops
.curve
.smooth('INVOKE_REGION_WIN')
1965 bpy
.ops
.curve
.smooth('INVOKE_REGION_WIN')
1966 bpy
.ops
.curve
.smooth('INVOKE_REGION_WIN')
1967 bpy
.ops
.curve
.smooth('INVOKE_REGION_WIN')
1968 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
1970 self
.selection_U_exists
= False
1971 self
.selection_U2_exists
= False
1972 self
.selection_V_exists
= False
1973 self
.selection_V2_exists
= False
1975 self
.selection_U_is_closed
= False
1976 self
.selection_U2_is_closed
= False
1977 self
.selection_V_is_closed
= False
1978 self
.selection_V2_is_closed
= False
1980 # Define what vertices are at the tips of each selection and are not the middle-vertex
1981 if selection_type
== "TWO_CONNECTED":
1982 self
.selection_U_exists
= True
1983 self
.selection_V_exists
= True
1985 closing_vert_U_idx
= None
1986 closing_vert_V_idx
= None
1987 closing_vert_U2_idx
= None
1988 closing_vert_V2_idx
= None
1990 # Determine which selection is Selection-U and which is Selection-V
1993 points_first_stroke_tips
= []
1996 self
.main_object
.matrix_world
@ self
.main_object
.data
.vertices
[verts_tips_parsed_idx
[0]].co
1999 self
.main_object
.matrix_world
@ self
.main_object
.data
.vertices
[middle_vertex_idx
].co
2002 self
.main_object
.matrix_world
@ self
.main_object
.data
.vertices
[verts_tips_parsed_idx
[1]].co
2005 self
.main_object
.matrix_world
@ self
.main_object
.data
.vertices
[middle_vertex_idx
].co
2007 points_first_stroke_tips
.append(
2008 self
.main_splines
.data
.splines
[0].bezier_points
[0].co
2010 points_first_stroke_tips
.append(
2011 self
.main_splines
.data
.splines
[0].bezier_points
[
2012 len(self
.main_splines
.data
.splines
[0].bezier_points
) - 1
2016 angle_A
= self
.orientation_difference(points_A
, points_first_stroke_tips
)
2017 angle_B
= self
.orientation_difference(points_B
, points_first_stroke_tips
)
2019 if angle_A
< angle_B
:
2020 first_vert_U_idx
= verts_tips_parsed_idx
[0]
2021 first_vert_V_idx
= verts_tips_parsed_idx
[1]
2023 first_vert_U_idx
= verts_tips_parsed_idx
[1]
2024 first_vert_V_idx
= verts_tips_parsed_idx
[0]
2026 elif selection_type
== "SINGLE" or selection_type
== "TWO_NOT_CONNECTED":
2027 first_sketched_point_first_stroke_co
= self
.main_splines
.data
.splines
[0].bezier_points
[0].co
2028 last_sketched_point_first_stroke_co
= \
2029 self
.main_splines
.data
.splines
[0].bezier_points
[
2030 len(self
.main_splines
.data
.splines
[0].bezier_points
) - 1
2032 first_sketched_point_last_stroke_co
= \
2033 self
.main_splines
.data
.splines
[
2034 len(self
.main_splines
.data
.splines
) - 1
2035 ].bezier_points
[0].co
2036 if len(self
.main_splines
.data
.splines
) > 1:
2037 first_sketched_point_second_stroke_co
= self
.main_splines
.data
.splines
[1].bezier_points
[0].co
2038 last_sketched_point_second_stroke_co
= \
2039 self
.main_splines
.data
.splines
[1].bezier_points
[
2040 len(self
.main_splines
.data
.splines
[1].bezier_points
) - 1
2043 single_unselected_neighbors
= [] # Only the neighbors of the single unselected verts
2044 for verts_neig_idx
in single_unselected_verts_and_neighbors
:
2045 single_unselected_neighbors
.append(verts_neig_idx
[1])
2046 single_unselected_neighbors
.append(verts_neig_idx
[2])
2048 all_chains_tips_and_middle_vert
= []
2049 for v_idx
in all_chains_tips_idx
:
2050 if v_idx
not in single_unselected_neighbors
:
2051 all_chains_tips_and_middle_vert
.append(v_idx
)
2053 all_chains_tips_and_middle_vert
+= single_unselected_verts
2055 all_participating_verts
= all_chains_tips_and_middle_vert
+ all_verts_idx
2057 # The tip of the selected vertices nearest to the first point of the first sketched stroke
2058 nearest_tip_to_first_st_first_pt_idx
, shortest_distance_to_first_stroke
= \
2059 self
.shortest_distance(
2061 first_sketched_point_first_stroke_co
,
2062 all_chains_tips_and_middle_vert
2064 # If the nearest tip is not from a closed selection, get the opposite tip vertex index
2065 if nearest_tip_to_first_st_first_pt_idx
not in single_unselected_verts
or \
2066 nearest_tip_to_first_st_first_pt_idx
== middle_vertex_idx
:
2068 nearest_tip_to_first_st_first_pt_opposite_idx
= \
2070 nearest_tip_to_first_st_first_pt_idx
,
2071 verts_tips_same_chain_idx
2073 # The tip of the selected vertices nearest to the last point of the first sketched stroke
2074 nearest_tip_to_first_st_last_pt_idx
, _temp_dist
= \
2075 self
.shortest_distance(
2077 last_sketched_point_first_stroke_co
,
2078 all_chains_tips_and_middle_vert
2080 # The tip of the selected vertices nearest to the first point of the last sketched stroke
2081 nearest_tip_to_last_st_first_pt_idx
, shortest_distance_to_last_stroke
= \
2082 self
.shortest_distance(
2084 first_sketched_point_last_stroke_co
,
2085 all_chains_tips_and_middle_vert
2087 if len(self
.main_splines
.data
.splines
) > 1:
2088 # The selected vertex nearest to the first point of the second sketched stroke
2089 # (This will be useful to determine the direction of the closed
2090 # selection V when extruding along strokes)
2091 nearest_vert_to_second_st_first_pt_idx
, _temp_dist
= \
2092 self
.shortest_distance(
2094 first_sketched_point_second_stroke_co
,
2097 # The selected vertex nearest to the first point of the second sketched stroke
2098 # (This will be useful to determine the direction of the closed
2099 # selection V2 when extruding along strokes)
2100 nearest_vert_to_second_st_last_pt_idx
, _temp_dist
= \
2101 self
.shortest_distance(
2103 last_sketched_point_second_stroke_co
,
2106 # Determine if the single selection will be treated as U or as V
2108 for i
in all_selected_edges_idx
:
2110 (self
.main_object
.matrix_world
@
2111 self
.main_object
.data
.vertices
[self
.main_object
.data
.edges
[i
].vertices
[0]].co
) -
2112 (self
.main_object
.matrix_world
@
2113 self
.main_object
.data
.vertices
[self
.main_object
.data
.edges
[i
].vertices
[1]].co
)
2116 average_edge_length
= edges_sum
/ len(all_selected_edges_idx
)
2118 # Get shortest distance from the first point of the last stroke to any participating vertex
2119 _temp_idx
, shortest_distance_to_last_stroke
= \
2120 self
.shortest_distance(
2122 first_sketched_point_last_stroke_co
,
2123 all_participating_verts
2125 # If the beginning of the first stroke is near enough, and its orientation
2126 # difference with the first edge of the nearest selection chain is not too high,
2127 # interpret things as an "extrude along strokes" instead of "extrude through strokes"
2128 if shortest_distance_to_first_stroke
< average_edge_length
/ 4 and \
2129 shortest_distance_to_last_stroke
< average_edge_length
and \
2130 len(self
.main_splines
.data
.splines
) > 1:
2132 self
.selection_U_exists
= False
2133 self
.selection_V_exists
= True
2134 # If the first selection is not closed
2135 if nearest_tip_to_first_st_first_pt_idx
not in single_unselected_verts
or \
2136 nearest_tip_to_first_st_first_pt_idx
== middle_vertex_idx
:
2137 self
.selection_V_is_closed
= False
2138 closing_vert_U_idx
= None
2139 closing_vert_U2_idx
= None
2140 closing_vert_V_idx
= None
2141 closing_vert_V2_idx
= None
2143 first_vert_V_idx
= nearest_tip_to_first_st_first_pt_idx
2145 if selection_type
== "TWO_NOT_CONNECTED":
2146 self
.selection_V2_exists
= True
2148 first_vert_V2_idx
= nearest_tip_to_first_st_last_pt_idx
2150 self
.selection_V_is_closed
= True
2151 closing_vert_V_idx
= nearest_tip_to_first_st_first_pt_idx
2153 # Get the neighbors of the first (unselected) vert of the closed selection U.
2155 for verts
in single_unselected_verts_and_neighbors
:
2156 if verts
[0] == nearest_tip_to_first_st_first_pt_idx
:
2157 vert_neighbors
.append(verts
[1])
2158 vert_neighbors
.append(verts
[2])
2161 verts_V
= self
.get_ordered_verts(
2162 self
.main_object
, all_selected_edges_idx
,
2163 all_verts_idx
, vert_neighbors
[0], middle_vertex_idx
, None
2166 for i
in range(0, len(verts_V
)):
2167 if verts_V
[i
].index
== nearest_vert_to_second_st_first_pt_idx
:
2168 # If the vertex nearest to the first point of the second stroke
2169 # is in the first half of the selected verts
2170 if i
>= len(verts_V
) / 2:
2171 first_vert_V_idx
= vert_neighbors
[1]
2174 first_vert_V_idx
= vert_neighbors
[0]
2177 if selection_type
== "TWO_NOT_CONNECTED":
2178 self
.selection_V2_exists
= True
2179 # If the second selection is not closed
2180 if nearest_tip_to_first_st_last_pt_idx
not in single_unselected_verts
or \
2181 nearest_tip_to_first_st_last_pt_idx
== middle_vertex_idx
:
2183 self
.selection_V2_is_closed
= False
2184 closing_vert_V2_idx
= None
2185 first_vert_V2_idx
= nearest_tip_to_first_st_last_pt_idx
2188 self
.selection_V2_is_closed
= True
2189 closing_vert_V2_idx
= nearest_tip_to_first_st_last_pt_idx
2191 # Get the neighbors of the first (unselected) vert of the closed selection U
2193 for verts
in single_unselected_verts_and_neighbors
:
2194 if verts
[0] == nearest_tip_to_first_st_last_pt_idx
:
2195 vert_neighbors
.append(verts
[1])
2196 vert_neighbors
.append(verts
[2])
2199 verts_V2
= self
.get_ordered_verts(
2200 self
.main_object
, all_selected_edges_idx
,
2201 all_verts_idx
, vert_neighbors
[0], middle_vertex_idx
, None
2204 for i
in range(0, len(verts_V2
)):
2205 if verts_V2
[i
].index
== nearest_vert_to_second_st_last_pt_idx
:
2206 # If the vertex nearest to the first point of the second stroke
2207 # is in the first half of the selected verts
2208 if i
>= len(verts_V2
) / 2:
2209 first_vert_V2_idx
= vert_neighbors
[1]
2212 first_vert_V2_idx
= vert_neighbors
[0]
2215 self
.selection_V2_exists
= False
2218 self
.selection_U_exists
= True
2219 self
.selection_V_exists
= False
2220 # If the first selection is not closed
2221 if nearest_tip_to_first_st_first_pt_idx
not in single_unselected_verts
or \
2222 nearest_tip_to_first_st_first_pt_idx
== middle_vertex_idx
:
2223 self
.selection_U_is_closed
= False
2224 closing_vert_U_idx
= None
2228 self
.main_object
.matrix_world
@
2229 self
.main_object
.data
.vertices
[nearest_tip_to_first_st_first_pt_idx
].co
2232 self
.main_object
.matrix_world
@
2233 self
.main_object
.data
.vertices
[nearest_tip_to_first_st_first_pt_opposite_idx
].co
2235 points_first_stroke_tips
= []
2236 points_first_stroke_tips
.append(self
.main_splines
.data
.splines
[0].bezier_points
[0].co
)
2237 points_first_stroke_tips
.append(
2238 self
.main_splines
.data
.splines
[0].bezier_points
[
2239 len(self
.main_splines
.data
.splines
[0].bezier_points
) - 1
2242 vec_A
= points_tips
[0] - points_tips
[1]
2243 vec_B
= points_first_stroke_tips
[0] - points_first_stroke_tips
[1]
2245 # Compare the direction of the selection and the first
2246 # grease pencil stroke to determine which is the "first" vertex of the selection
2247 if vec_A
.dot(vec_B
) < 0:
2248 first_vert_U_idx
= nearest_tip_to_first_st_first_pt_opposite_idx
2250 first_vert_U_idx
= nearest_tip_to_first_st_first_pt_idx
2253 self
.selection_U_is_closed
= True
2254 closing_vert_U_idx
= nearest_tip_to_first_st_first_pt_idx
2256 # Get the neighbors of the first (unselected) vert of the closed selection U
2258 for verts
in single_unselected_verts_and_neighbors
:
2259 if verts
[0] == nearest_tip_to_first_st_first_pt_idx
:
2260 vert_neighbors
.append(verts
[1])
2261 vert_neighbors
.append(verts
[2])
2264 points_first_and_neighbor
= []
2265 points_first_and_neighbor
.append(
2266 self
.main_object
.matrix_world
@
2267 self
.main_object
.data
.vertices
[nearest_tip_to_first_st_first_pt_idx
].co
2269 points_first_and_neighbor
.append(
2270 self
.main_object
.matrix_world
@
2271 self
.main_object
.data
.vertices
[vert_neighbors
[0]].co
2273 points_first_stroke_tips
= []
2274 points_first_stroke_tips
.append(self
.main_splines
.data
.splines
[0].bezier_points
[0].co
)
2275 points_first_stroke_tips
.append(self
.main_splines
.data
.splines
[0].bezier_points
[1].co
)
2277 vec_A
= points_first_and_neighbor
[0] - points_first_and_neighbor
[1]
2278 vec_B
= points_first_stroke_tips
[0] - points_first_stroke_tips
[1]
2280 # Compare the direction of the selection and the first grease pencil stroke to
2281 # determine which is the vertex neighbor to the first vertex (unselected) of
2282 # the closed selection. This will determine the direction of the closed selection
2283 if vec_A
.dot(vec_B
) < 0:
2284 first_vert_U_idx
= vert_neighbors
[1]
2286 first_vert_U_idx
= vert_neighbors
[0]
2288 if selection_type
== "TWO_NOT_CONNECTED":
2289 self
.selection_U2_exists
= True
2290 # If the second selection is not closed
2291 if nearest_tip_to_last_st_first_pt_idx
not in single_unselected_verts
or \
2292 nearest_tip_to_last_st_first_pt_idx
== middle_vertex_idx
:
2294 self
.selection_U2_is_closed
= False
2295 closing_vert_U2_idx
= None
2296 first_vert_U2_idx
= nearest_tip_to_last_st_first_pt_idx
2298 self
.selection_U2_is_closed
= True
2299 closing_vert_U2_idx
= nearest_tip_to_last_st_first_pt_idx
2301 # Get the neighbors of the first (unselected) vert of the closed selection U
2303 for verts
in single_unselected_verts_and_neighbors
:
2304 if verts
[0] == nearest_tip_to_last_st_first_pt_idx
:
2305 vert_neighbors
.append(verts
[1])
2306 vert_neighbors
.append(verts
[2])
2309 points_first_and_neighbor
= []
2310 points_first_and_neighbor
.append(
2311 self
.main_object
.matrix_world
@
2312 self
.main_object
.data
.vertices
[nearest_tip_to_last_st_first_pt_idx
].co
2314 points_first_and_neighbor
.append(
2315 self
.main_object
.matrix_world
@
2316 self
.main_object
.data
.vertices
[vert_neighbors
[0]].co
2318 points_last_stroke_tips
= []
2319 points_last_stroke_tips
.append(
2320 self
.main_splines
.data
.splines
[
2321 len(self
.main_splines
.data
.splines
) - 1
2322 ].bezier_points
[0].co
2324 points_last_stroke_tips
.append(
2325 self
.main_splines
.data
.splines
[
2326 len(self
.main_splines
.data
.splines
) - 1
2327 ].bezier_points
[1].co
2329 vec_A
= points_first_and_neighbor
[0] - points_first_and_neighbor
[1]
2330 vec_B
= points_last_stroke_tips
[0] - points_last_stroke_tips
[1]
2332 # Compare the direction of the selection and the last grease pencil stroke to
2333 # determine which is the vertex neighbor to the first vertex (unselected) of
2334 # the closed selection. This will determine the direction of the closed selection
2335 if vec_A
.dot(vec_B
) < 0:
2336 first_vert_U2_idx
= vert_neighbors
[1]
2338 first_vert_U2_idx
= vert_neighbors
[0]
2340 self
.selection_U2_exists
= False
2342 elif selection_type
== "NO_SELECTION":
2343 self
.selection_U_exists
= False
2344 self
.selection_V_exists
= False
2346 # Get an ordered list of the vertices of Selection-U
2347 verts_ordered_U
= []
2348 if self
.selection_U_exists
:
2349 verts_ordered_U
= self
.get_ordered_verts(
2350 self
.main_object
, all_selected_edges_idx
,
2351 all_verts_idx
, first_vert_U_idx
,
2352 middle_vertex_idx
, closing_vert_U_idx
2355 # Get an ordered list of the vertices of Selection-U2
2356 verts_ordered_U2
= []
2357 if self
.selection_U2_exists
:
2358 verts_ordered_U2
= self
.get_ordered_verts(
2359 self
.main_object
, all_selected_edges_idx
,
2360 all_verts_idx
, first_vert_U2_idx
,
2361 middle_vertex_idx
, closing_vert_U2_idx
2364 # Get an ordered list of the vertices of Selection-V
2365 verts_ordered_V
= []
2366 if self
.selection_V_exists
:
2367 verts_ordered_V
= self
.get_ordered_verts(
2368 self
.main_object
, all_selected_edges_idx
,
2369 all_verts_idx
, first_vert_V_idx
,
2370 middle_vertex_idx
, closing_vert_V_idx
2372 verts_ordered_V_indices
= [x
.index
for x
in verts_ordered_V
]
2374 # Get an ordered list of the vertices of Selection-V2
2375 verts_ordered_V2
= []
2376 if self
.selection_V2_exists
:
2377 verts_ordered_V2
= self
.get_ordered_verts(
2378 self
.main_object
, all_selected_edges_idx
,
2379 all_verts_idx
, first_vert_V2_idx
,
2380 middle_vertex_idx
, closing_vert_V2_idx
2383 # Check if when there are two-not-connected selections both have the same
2384 # number of verts. If not terminate the script
2385 if ((self
.selection_U2_exists
and len(verts_ordered_U
) != len(verts_ordered_U2
)) or
2386 (self
.selection_V2_exists
and len(verts_ordered_V
) != len(verts_ordered_V2
))):
2388 self
.report({'WARNING'}, "Both selections must have the same number of edges")
2390 self
.stopping_errors
= True
2394 # Calculate edges U proportions
2395 # Sum selected edges U lengths
2396 edges_lengths_U
= []
2397 edges_lengths_sum_U
= 0
2399 if self
.selection_U_exists
:
2400 edges_lengths_U
, edges_lengths_sum_U
= self
.get_chain_length(
2404 if self
.selection_U2_exists
:
2405 edges_lengths_U2
, edges_lengths_sum_U2
= self
.get_chain_length(
2409 # Sum selected edges V lengths
2410 edges_lengths_V
= []
2411 edges_lengths_sum_V
= 0
2413 if self
.selection_V_exists
:
2414 edges_lengths_V
, edges_lengths_sum_V
= self
.get_chain_length(
2418 if self
.selection_V2_exists
:
2419 edges_lengths_V2
, edges_lengths_sum_V2
= self
.get_chain_length(
2424 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
2425 bpy
.ops
.curve
.subdivide('INVOKE_REGION_WIN',
2426 number_cuts
=bpy
.context
.scene
.bsurfaces
.SURFSK_precision
)
2427 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
2430 edges_proportions_U
= []
2431 edges_proportions_U
= self
.get_edges_proportions(
2432 edges_lengths_U
, edges_lengths_sum_U
,
2433 self
.selection_U_exists
, self
.edges_U
2435 verts_count_U
= len(edges_proportions_U
) + 1
2437 if self
.selection_U2_exists
:
2438 edges_proportions_U2
= []
2439 edges_proportions_U2
= self
.get_edges_proportions(
2440 edges_lengths_U2
, edges_lengths_sum_U2
,
2441 self
.selection_U2_exists
, self
.edges_V
2445 edges_proportions_V
= []
2446 edges_proportions_V
= self
.get_edges_proportions(
2447 edges_lengths_V
, edges_lengths_sum_V
,
2448 self
.selection_V_exists
, self
.edges_V
2451 if self
.selection_V2_exists
:
2452 edges_proportions_V2
= []
2453 edges_proportions_V2
= self
.get_edges_proportions(
2454 edges_lengths_V2
, edges_lengths_sum_V2
,
2455 self
.selection_V2_exists
, self
.edges_V
2458 # Cyclic Follow: simplify sketched curves, make them Cyclic, and complete
2459 # the actual sketched curves with a "closing segment"
2460 if self
.cyclic_follow
and not self
.selection_V_exists
and not \
2461 ((self
.selection_U_exists
and not self
.selection_U_is_closed
) or
2462 (self
.selection_U2_exists
and not self
.selection_U2_is_closed
)):
2464 simplified_spline_coords
= []
2465 simplified_curve
= []
2466 ob_simplified_curve
= []
2467 splines_first_v_co
= []
2468 for i
in range(len(self
.main_splines
.data
.splines
)):
2469 # Create a curve object for the actual spline "cyclic extension"
2470 simplified_curve
.append(bpy
.data
.curves
.new('SURFSKIO_simpl_crv', 'CURVE'))
2471 ob_simplified_curve
.append(bpy
.data
.objects
.new('SURFSKIO_simpl_crv', simplified_curve
[i
]))
2472 bpy
.context
.collection
.objects
.link(ob_simplified_curve
[i
])
2474 simplified_curve
[i
].dimensions
= "3D"
2477 for bp
in self
.main_splines
.data
.splines
[i
].bezier_points
:
2478 spline_coords
.append(bp
.co
)
2481 simplified_spline_coords
.append(self
.simplify_spline(spline_coords
, 5))
2483 # Get the coordinates of the first vert of the actual spline
2484 splines_first_v_co
.append(simplified_spline_coords
[i
][0])
2486 # Generate the spline
2487 spline
= simplified_curve
[i
].splines
.new('BEZIER')
2488 # less one because one point is added when the spline is created
2489 spline
.bezier_points
.add(len(simplified_spline_coords
[i
]) - 1)
2490 for p
in range(0, len(simplified_spline_coords
[i
])):
2491 spline
.bezier_points
[p
].co
= simplified_spline_coords
[i
][p
]
2493 spline
.use_cyclic_u
= True
2495 spline_bp_count
= len(spline
.bezier_points
)
2497 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
2498 ob_simplified_curve
[i
].select_set(True)
2499 bpy
.context
.view_layer
.objects
.active
= ob_simplified_curve
[i
]
2501 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
2502 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='SELECT')
2503 bpy
.ops
.curve
.handle_type_set('INVOKE_REGION_WIN', type='AUTOMATIC')
2504 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
2505 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
2507 # Select the "closing segment", and subdivide it
2508 ob_simplified_curve
[i
].data
.splines
[0].bezier_points
[0].select_control_point
= True
2509 ob_simplified_curve
[i
].data
.splines
[0].bezier_points
[0].select_left_handle
= True
2510 ob_simplified_curve
[i
].data
.splines
[0].bezier_points
[0].select_right_handle
= True
2512 ob_simplified_curve
[i
].data
.splines
[0].bezier_points
[spline_bp_count
- 1].select_control_point
= True
2513 ob_simplified_curve
[i
].data
.splines
[0].bezier_points
[spline_bp_count
- 1].select_left_handle
= True
2514 ob_simplified_curve
[i
].data
.splines
[0].bezier_points
[spline_bp_count
- 1].select_right_handle
= True
2516 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
2518 (ob_simplified_curve
[i
].data
.splines
[0].bezier_points
[0].co
-
2519 ob_simplified_curve
[i
].data
.splines
[0].bezier_points
[spline_bp_count
- 1].co
).length
/
2520 self
.average_gp_segment_length
2523 bpy
.ops
.curve
.subdivide('INVOKE_REGION_WIN', number_cuts
=int(segments
))
2525 # Delete the other vertices and make it non-cyclic to
2526 # keep only the needed verts of the "closing segment"
2527 bpy
.ops
.curve
.select_all(action
='INVERT')
2528 bpy
.ops
.curve
.delete(type='VERT')
2529 ob_simplified_curve
[i
].data
.splines
[0].use_cyclic_u
= False
2530 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
2532 # Add the points of the "closing segment" to the original curve from grease pencil stroke
2533 first_new_index
= len(self
.main_splines
.data
.splines
[i
].bezier_points
)
2534 self
.main_splines
.data
.splines
[i
].bezier_points
.add(
2535 len(ob_simplified_curve
[i
].data
.splines
[0].bezier_points
) - 1
2537 for t
in range(1, len(ob_simplified_curve
[i
].data
.splines
[0].bezier_points
)):
2538 self
.main_splines
.data
.splines
[i
].bezier_points
[t
- 1 + first_new_index
].co
= \
2539 ob_simplified_curve
[i
].data
.splines
[0].bezier_points
[t
].co
2541 # Delete the temporal curve
2542 bpy
.ops
.object.delete({"selected_objects": [ob_simplified_curve
[i
]]})
2544 # Get the coords of the points distributed along the sketched strokes,
2545 # with proportions-U of the first selection
2546 pts_on_strokes_with_proportions_U
= self
.distribute_pts(
2547 self
.main_splines
.data
.splines
,
2550 sketched_splines_parsed
= []
2552 if self
.selection_U2_exists
:
2553 # Initialize the multidimensional list with the proportions of all the segments
2554 proportions_loops_crossing_strokes
= []
2555 for i
in range(len(pts_on_strokes_with_proportions_U
)):
2556 proportions_loops_crossing_strokes
.append([])
2558 for t
in range(len(pts_on_strokes_with_proportions_U
[0])):
2559 proportions_loops_crossing_strokes
[i
].append(None)
2561 # Calculate the proportions of each segment of the loops-U from pts_on_strokes_with_proportions_U
2562 for lp
in range(len(pts_on_strokes_with_proportions_U
[0])):
2563 loop_segments_lengths
= []
2565 for st
in range(len(pts_on_strokes_with_proportions_U
)):
2566 # When on the first stroke, add the segment from the selection to the first stroke
2568 loop_segments_lengths
.append(
2569 ((self
.main_object
.matrix_world
@ verts_ordered_U
[lp
].co
) -
2570 pts_on_strokes_with_proportions_U
[0][lp
]).length
2572 # For all strokes except for the last, calculate the distance
2573 # from the actual stroke to the next
2574 if st
!= len(pts_on_strokes_with_proportions_U
) - 1:
2575 loop_segments_lengths
.append(
2576 (pts_on_strokes_with_proportions_U
[st
][lp
] -
2577 pts_on_strokes_with_proportions_U
[st
+ 1][lp
]).length
2579 # When on the last stroke, add the segments
2580 # from the last stroke to the second selection
2581 if st
== len(pts_on_strokes_with_proportions_U
) - 1:
2582 loop_segments_lengths
.append(
2583 (pts_on_strokes_with_proportions_U
[st
][lp
] -
2584 (self
.main_object
.matrix_world
@ verts_ordered_U2
[lp
].co
)).length
2586 # Calculate full loop length
2587 loop_seg_lengths_sum
= 0
2588 for i
in range(len(loop_segments_lengths
)):
2589 loop_seg_lengths_sum
+= loop_segments_lengths
[i
]
2591 # Fill the multidimensional list with the proportions of all the segments
2592 for st
in range(len(pts_on_strokes_with_proportions_U
)):
2593 proportions_loops_crossing_strokes
[st
][lp
] = \
2594 loop_segments_lengths
[st
] / loop_seg_lengths_sum
2596 # Calculate proportions for each stroke
2597 for st
in range(len(pts_on_strokes_with_proportions_U
)):
2598 actual_stroke_spline
= []
2599 # Needs to be a list for the "distribute_pts" method
2600 actual_stroke_spline
.append(self
.main_splines
.data
.splines
[st
])
2602 # Calculate the proportions for the actual stroke.
2603 actual_edges_proportions_U
= []
2604 for i
in range(len(edges_proportions_U
)):
2607 # Sum the proportions of this loop up to the actual.
2608 for t
in range(0, st
+ 1):
2609 proportions_sum
+= proportions_loops_crossing_strokes
[t
][i
]
2610 # i + 1, because proportions_loops_crossing_strokes refers to loops,
2611 # and the proportions refer to edges, so we start at the element 1
2612 # of proportions_loops_crossing_strokes instead of element 0
2613 actual_edges_proportions_U
.append(
2614 edges_proportions_U
[i
] -
2615 ((edges_proportions_U
[i
] - edges_proportions_U2
[i
]) * proportions_sum
)
2617 points_actual_spline
= self
.distribute_pts(actual_stroke_spline
, actual_edges_proportions_U
)
2618 sketched_splines_parsed
.append(points_actual_spline
[0])
2620 sketched_splines_parsed
= pts_on_strokes_with_proportions_U
2622 # If the selection type is "TWO_NOT_CONNECTED" replace the
2623 # points of the last spline with the points in the "target" selection
2624 if selection_type
== "TWO_NOT_CONNECTED":
2625 if self
.selection_U2_exists
:
2626 for i
in range(0, len(sketched_splines_parsed
[len(sketched_splines_parsed
) - 1])):
2627 sketched_splines_parsed
[len(sketched_splines_parsed
) - 1][i
] = \
2628 self
.main_object
.matrix_world
@ verts_ordered_U2
[i
].co
2630 # Create temporary curves along the "control-points" found
2631 # on the sketched curves and the mesh selection
2632 mesh_ctrl_pts_name
= "SURFSKIO_ctrl_pts"
2633 me
= bpy
.data
.meshes
.new(mesh_ctrl_pts_name
)
2634 ob_ctrl_pts
= bpy
.data
.objects
.new(mesh_ctrl_pts_name
, me
)
2635 ob_ctrl_pts
.data
= me
2636 bpy
.context
.collection
.objects
.link(ob_ctrl_pts
)
2643 for i
in range(0, verts_count_U
):
2644 vert_num_in_spline
= 1
2646 if self
.selection_U_exists
:
2647 ob_ctrl_pts
.data
.vertices
.add(1)
2648 last_v
= ob_ctrl_pts
.data
.vertices
[len(ob_ctrl_pts
.data
.vertices
) - 1]
2649 last_v
.co
= self
.main_object
.matrix_world
@ verts_ordered_U
[i
].co
2651 vert_num_in_spline
+= 1
2653 for t
in range(0, len(sketched_splines_parsed
)):
2654 ob_ctrl_pts
.data
.vertices
.add(1)
2655 v
= ob_ctrl_pts
.data
.vertices
[len(ob_ctrl_pts
.data
.vertices
) - 1]
2656 v
.co
= sketched_splines_parsed
[t
][i
]
2658 if vert_num_in_spline
> 1:
2659 ob_ctrl_pts
.data
.edges
.add(1)
2660 ob_ctrl_pts
.data
.edges
[len(ob_ctrl_pts
.data
.edges
) - 1].vertices
[0] = \
2661 len(ob_ctrl_pts
.data
.vertices
) - 2
2662 ob_ctrl_pts
.data
.edges
[len(ob_ctrl_pts
.data
.edges
) - 1].vertices
[1] = \
2663 len(ob_ctrl_pts
.data
.vertices
) - 1
2666 first_verts
.append(v
.index
)
2669 second_verts
.append(v
.index
)
2671 if t
== len(sketched_splines_parsed
) - 1:
2672 last_verts
.append(v
.index
)
2675 vert_num_in_spline
+= 1
2677 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
2678 ob_ctrl_pts
.select_set(True)
2679 bpy
.context
.view_layer
.objects
.active
= ob_ctrl_pts
2681 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
2682 bpy
.ops
.mesh
.select_all(action
='DESELECT')
2683 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
2685 # Determine which loops-U will be "Cyclic"
2686 for i
in range(0, len(first_verts
)):
2687 # When there is Cyclic Cross there is no need of
2688 # Automatic Join, (and there are at least three strokes)
2689 if self
.automatic_join
and not self
.cyclic_cross
and \
2690 selection_type
!= "TWO_CONNECTED" and len(self
.main_splines
.data
.splines
) >= 3:
2692 v
= ob_ctrl_pts
.data
.vertices
2693 first_point_co
= v
[first_verts
[i
]].co
2694 second_point_co
= v
[second_verts
[i
]].co
2695 last_point_co
= v
[last_verts
[i
]].co
2697 # Coordinates of the point in the center of both the first and last verts.
2699 (first_point_co
[0] + last_point_co
[0]) / 2,
2700 (first_point_co
[1] + last_point_co
[1]) / 2,
2701 (first_point_co
[2] + last_point_co
[2]) / 2
2703 vec_A
= second_point_co
- first_point_co
2704 vec_B
= second_point_co
- Vector(verts_center_co
)
2706 # Calculate the length of the first segment of the loop,
2707 # and the length it would have after moving the first vert
2708 # to the middle position between first and last
2709 length_original
= (second_point_co
- first_point_co
).length
2710 length_target
= (second_point_co
- Vector(verts_center_co
)).length
2712 angle
= vec_A
.angle(vec_B
) / pi
2714 # If the target length doesn't stretch too much, and the
2715 # its angle doesn't change to much either
2716 if length_target
<= length_original
* 1.03 * self
.join_stretch_factor
and \
2717 angle
<= 0.008 * self
.join_stretch_factor
and not self
.selection_U_exists
:
2719 cyclic_loops_U
.append(True)
2720 # Move the first vert to the center coordinates
2721 ob_ctrl_pts
.data
.vertices
[first_verts
[i
]].co
= verts_center_co
2722 # Select the last verts from Cyclic loops, for later deletion all at once
2723 v
[last_verts
[i
]].select
= True
2725 cyclic_loops_U
.append(False)
2727 # If "Cyclic Cross" is active then "all" crossing curves become cyclic
2728 if self
.cyclic_cross
and not self
.selection_U_exists
and not \
2729 ((self
.selection_V_exists
and not self
.selection_V_is_closed
) or
2730 (self
.selection_V2_exists
and not self
.selection_V2_is_closed
)):
2732 cyclic_loops_U
.append(True)
2734 cyclic_loops_U
.append(False)
2736 # The cyclic_loops_U list needs to be reversed.
2737 cyclic_loops_U
.reverse()
2739 # Delete the previously selected (last_)verts.
2740 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
2741 bpy
.ops
.mesh
.delete('INVOKE_REGION_WIN', type='VERT')
2742 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
2744 # Create curves from control points.
2745 bpy
.ops
.object.convert('INVOKE_REGION_WIN', target
='CURVE', keep_original
=False)
2746 ob_curves_surf
= bpy
.context
.view_layer
.objects
.active
2747 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
2748 bpy
.ops
.curve
.spline_type_set('INVOKE_REGION_WIN', type='BEZIER')
2749 bpy
.ops
.curve
.handle_type_set('INVOKE_REGION_WIN', type='AUTOMATIC')
2751 # Make Cyclic the splines designated as Cyclic.
2752 for i
in range(0, len(cyclic_loops_U
)):
2753 ob_curves_surf
.data
.splines
[i
].use_cyclic_u
= cyclic_loops_U
[i
]
2755 # Get the coords of all points on first loop-U, for later comparison with its
2756 # subdivided version, to know which points of the loops-U are crossed by the
2757 # original strokes. The indices will be the same for the other loops-U
2758 if self
.loops_on_strokes
:
2759 coords_loops_U_control_points
= []
2760 for p
in ob_ctrl_pts
.data
.splines
[0].bezier_points
:
2761 coords_loops_U_control_points
.append(["%.4f" % p
.co
[0], "%.4f" % p
.co
[1], "%.4f" % p
.co
[2]])
2763 tuple(coords_loops_U_control_points
)
2765 # Calculate number of edges-V in case option "Loops on strokes" is active or inactive
2766 if self
.loops_on_strokes
and not self
.selection_V_exists
:
2767 edges_V_count
= len(self
.main_splines
.data
.splines
) * self
.edges_V
2769 edges_V_count
= len(edges_proportions_V
)
2771 # The Follow precision will vary depending on the number of Follow face-loops
2772 precision_multiplier
= round(2 + (edges_V_count
/ 15))
2773 curve_cuts
= bpy
.context
.scene
.bsurfaces
.SURFSK_precision
* precision_multiplier
2775 # Subdivide the curves
2776 bpy
.ops
.curve
.subdivide('INVOKE_REGION_WIN', number_cuts
=curve_cuts
)
2778 # The verts position shifting that happens with splines subdivision.
2779 # For later reorder splines points
2780 verts_position_shift
= curve_cuts
+ 1
2781 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
2783 # Reorder coordinates of the points of each spline to put the first point of
2784 # the spline starting at the position it was the first point before sudividing
2785 # the curve. And make a new curve object per spline (to handle memory better later)
2786 splines_U_objects
= []
2787 for i
in range(len(ob_curves_surf
.data
.splines
)):
2788 spline_U_curve
= bpy
.data
.curves
.new('SURFSKIO_spline_U_' + str(i
), 'CURVE')
2789 ob_spline_U
= bpy
.data
.objects
.new('SURFSKIO_spline_U_' + str(i
), spline_U_curve
)
2790 bpy
.context
.collection
.objects
.link(ob_spline_U
)
2792 spline_U_curve
.dimensions
= "3D"
2794 # Add points to the spline in the new curve object
2795 ob_spline_U
.data
.splines
.new('BEZIER')
2796 for t
in range(len(ob_curves_surf
.data
.splines
[i
].bezier_points
)):
2797 if cyclic_loops_U
[i
] is True and not self
.selection_U_exists
: # If the loop is cyclic
2798 if t
+ verts_position_shift
<= len(ob_curves_surf
.data
.splines
[i
].bezier_points
) - 1:
2799 point_index
= t
+ verts_position_shift
2801 point_index
= t
+ verts_position_shift
- len(ob_curves_surf
.data
.splines
[i
].bezier_points
)
2804 # to avoid adding the first point since it's added when the spline is created
2806 ob_spline_U
.data
.splines
[0].bezier_points
.add(1)
2807 ob_spline_U
.data
.splines
[0].bezier_points
[t
].co
= \
2808 ob_curves_surf
.data
.splines
[i
].bezier_points
[point_index
].co
2810 if cyclic_loops_U
[i
] is True and not self
.selection_U_exists
: # If the loop is cyclic
2811 # Add a last point at the same location as the first one
2812 ob_spline_U
.data
.splines
[0].bezier_points
.add(1)
2813 ob_spline_U
.data
.splines
[0].bezier_points
[len(ob_spline_U
.data
.splines
[0].bezier_points
) - 1].co
= \
2814 ob_spline_U
.data
.splines
[0].bezier_points
[0].co
2816 ob_spline_U
.data
.splines
[0].use_cyclic_u
= False
2818 splines_U_objects
.append(ob_spline_U
)
2819 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
2820 ob_spline_U
.select_set(True)
2821 bpy
.context
.view_layer
.objects
.active
= ob_spline_U
2823 # When option "Loops on strokes" is active each "Cross" loop will have
2824 # its own proportions according to where the original strokes "touch" them
2825 if self
.loops_on_strokes
:
2826 # Get the indices of points where the original strokes "touch" loops-U
2827 points_U_crossed_by_strokes
= []
2828 for i
in range(len(splines_U_objects
[0].data
.splines
[0].bezier_points
)):
2829 bp
= splines_U_objects
[0].data
.splines
[0].bezier_points
[i
]
2830 if ["%.4f" % bp
.co
[0], "%.4f" % bp
.co
[1], "%.4f" % bp
.co
[2]] in coords_loops_U_control_points
:
2831 points_U_crossed_by_strokes
.append(i
)
2833 # Make a dictionary with the number of the edge, in the selected chain V, corresponding to each stroke
2834 edge_order_number_for_splines
= {}
2835 if self
.selection_V_exists
:
2836 # For two-connected selections add a first hypothetic stroke at the beginning.
2837 if selection_type
== "TWO_CONNECTED":
2838 edge_order_number_for_splines
[0] = 0
2840 for i
in range(len(self
.main_splines
.data
.splines
)):
2841 sp
= self
.main_splines
.data
.splines
[i
]
2842 v_idx
, _dist_temp
= self
.shortest_distance(
2844 sp
.bezier_points
[0].co
,
2845 verts_ordered_V_indices
2847 # Get the position (edges count) of the vert v_idx in the selected chain V
2848 edge_idx_in_chain
= verts_ordered_V_indices
.index(v_idx
)
2850 # For two-connected selections the strokes go after the
2851 # hypothetic stroke added before, so the index adds one per spline
2852 if selection_type
== "TWO_CONNECTED":
2853 spline_number
= i
+ 1
2857 edge_order_number_for_splines
[spline_number
] = edge_idx_in_chain
2859 # Get the first and last verts indices for later comparison
2862 elif i
== len(self
.main_splines
.data
.splines
) - 1:
2865 if self
.selection_V_is_closed
:
2866 # If there is no last stroke on the last vertex (same as first vertex),
2867 # add a hypothetic spline at last vert order
2868 if first_v_idx
!= last_v_idx
:
2869 edge_order_number_for_splines
[(len(self
.main_splines
.data
.splines
) - 1) + 1] = \
2870 len(verts_ordered_V_indices
) - 1
2872 if self
.cyclic_cross
:
2873 edge_order_number_for_splines
[len(self
.main_splines
.data
.splines
) - 1] = \
2874 len(verts_ordered_V_indices
) - 2
2875 edge_order_number_for_splines
[(len(self
.main_splines
.data
.splines
) - 1) + 1] = \
2876 len(verts_ordered_V_indices
) - 1
2878 edge_order_number_for_splines
[len(self
.main_splines
.data
.splines
) - 1] = \
2879 len(verts_ordered_V_indices
) - 1
2881 # Get the coords of the points distributed along the
2882 # "crossing curves", with appropriate proportions-V
2883 surface_splines_parsed
= []
2884 for i
in range(len(splines_U_objects
)):
2885 sp_ob
= splines_U_objects
[i
]
2886 # If "Loops on strokes" option is active, calculate the proportions for each loop-U
2887 if self
.loops_on_strokes
:
2888 # Segments distances from stroke to stroke
2891 segments_distances
= []
2892 for t
in range(len(sp_ob
.data
.splines
[0].bezier_points
)):
2893 bp
= sp_ob
.data
.splines
[0].bezier_points
[t
]
2899 dist
+= (last_p
- actual_p
).length
2901 if t
in points_U_crossed_by_strokes
:
2902 segments_distances
.append(dist
)
2909 # Calculate Proportions.
2910 used_edges_proportions_V
= []
2911 for t
in range(len(segments_distances
)):
2912 if self
.selection_V_exists
:
2914 order_number_last_stroke
= 0
2916 segment_edges_length_V
= 0
2917 segment_edges_length_V2
= 0
2918 for order
in range(order_number_last_stroke
, edge_order_number_for_splines
[t
+ 1]):
2919 segment_edges_length_V
+= edges_lengths_V
[order
]
2920 if self
.selection_V2_exists
:
2921 segment_edges_length_V2
+= edges_lengths_V2
[order
]
2923 for order
in range(order_number_last_stroke
, edge_order_number_for_splines
[t
+ 1]):
2924 # Calculate each "sub-segment" (the ones between each stroke) length
2925 if self
.selection_V2_exists
:
2926 proportion_sub_seg
= (edges_lengths_V2
[order
] -
2927 ((edges_lengths_V2
[order
] - edges_lengths_V
[order
]) /
2928 len(splines_U_objects
) * i
)) / (segment_edges_length_V2
-
2929 (segment_edges_length_V2
- segment_edges_length_V
) /
2930 len(splines_U_objects
) * i
)
2932 sub_seg_dist
= segments_distances
[t
] * proportion_sub_seg
2934 proportion_sub_seg
= edges_lengths_V
[order
] / segment_edges_length_V
2935 sub_seg_dist
= segments_distances
[t
] * proportion_sub_seg
2937 used_edges_proportions_V
.append(sub_seg_dist
/ full_dist
)
2939 order_number_last_stroke
= edge_order_number_for_splines
[t
+ 1]
2942 for _c
in range(self
.edges_V
):
2943 # Calculate each "sub-segment" (the ones between each stroke) length
2944 sub_seg_dist
= segments_distances
[t
] / self
.edges_V
2945 used_edges_proportions_V
.append(sub_seg_dist
/ full_dist
)
2947 actual_spline
= self
.distribute_pts(sp_ob
.data
.splines
, used_edges_proportions_V
)
2948 surface_splines_parsed
.append(actual_spline
[0])
2951 if self
.selection_V2_exists
:
2952 used_edges_proportions_V
= []
2953 for p
in range(len(edges_proportions_V
)):
2954 used_edges_proportions_V
.append(
2955 edges_proportions_V2
[p
] -
2956 ((edges_proportions_V2
[p
] -
2957 edges_proportions_V
[p
]) / len(splines_U_objects
) * i
)
2960 used_edges_proportions_V
= edges_proportions_V
2962 actual_spline
= self
.distribute_pts(sp_ob
.data
.splines
, used_edges_proportions_V
)
2963 surface_splines_parsed
.append(actual_spline
[0])
2965 # Set the verts of the first and last splines to the locations
2966 # of the respective verts in the selections
2967 if self
.selection_V_exists
:
2968 for i
in range(0, len(surface_splines_parsed
[0])):
2969 surface_splines_parsed
[len(surface_splines_parsed
) - 1][i
] = \
2970 self
.main_object
.matrix_world
@ verts_ordered_V
[i
].co
2972 if selection_type
== "TWO_NOT_CONNECTED":
2973 if self
.selection_V2_exists
:
2974 for i
in range(0, len(surface_splines_parsed
[0])):
2975 surface_splines_parsed
[0][i
] = self
.main_object
.matrix_world
@ verts_ordered_V2
[i
].co
2977 # When "Automatic join" option is active (and the selection type != "TWO_CONNECTED"),
2978 # merge the verts of the tips of the loops when they are "near enough"
2979 if self
.automatic_join
and selection_type
!= "TWO_CONNECTED":
2980 # Join the tips of "Follow" loops that are near enough and must be "closed"
2981 if not self
.selection_V_exists
and len(edges_proportions_U
) >= 3:
2982 for i
in range(len(surface_splines_parsed
[0])):
2983 sp
= surface_splines_parsed
2984 loop_segment_dist
= (sp
[0][i
] - sp
[1][i
]).length
2986 verts_middle_position_co
= [
2987 (sp
[0][i
][0] + sp
[len(sp
) - 1][i
][0]) / 2,
2988 (sp
[0][i
][1] + sp
[len(sp
) - 1][i
][1]) / 2,
2989 (sp
[0][i
][2] + sp
[len(sp
) - 1][i
][2]) / 2
2991 points_original
= []
2992 points_original
.append(sp
[1][i
])
2993 points_original
.append(sp
[0][i
])
2996 points_target
.append(sp
[1][i
])
2997 points_target
.append(Vector(verts_middle_position_co
))
2999 vec_A
= points_original
[0] - points_original
[1]
3000 vec_B
= points_target
[0] - points_target
[1]
3001 # check for zero angles, not sure if it is a great fix
3002 if vec_A
.length
!= 0 and vec_B
.length
!= 0:
3003 angle
= vec_A
.angle(vec_B
) / pi
3004 edge_new_length
= (Vector(verts_middle_position_co
) - sp
[1][i
]).length
3009 # If after moving the verts to the middle point, the segment doesn't stretch too much
3010 if edge_new_length
<= loop_segment_dist
* 1.5 * \
3011 self
.join_stretch_factor
and angle
< 0.25 * self
.join_stretch_factor
:
3013 # Avoid joining when the actual loop must be merged with the original mesh
3014 if not (self
.selection_U_exists
and i
== 0) and \
3015 not (self
.selection_U2_exists
and i
== len(surface_splines_parsed
[0]) - 1):
3017 # Change the coords of both verts to the middle position
3018 surface_splines_parsed
[0][i
] = verts_middle_position_co
3019 surface_splines_parsed
[len(surface_splines_parsed
) - 1][i
] = verts_middle_position_co
3021 # Delete object with control points and object from grease pencil conversion
3022 bpy
.ops
.object.delete({"selected_objects": [ob_ctrl_pts
]})
3024 bpy
.ops
.object.delete({"selected_objects": splines_U_objects
})
3028 # Get all verts coords
3029 all_surface_verts_co
= []
3030 for i
in range(0, len(surface_splines_parsed
)):
3031 # Get coords of all verts and make a list with them
3032 for pt_co
in surface_splines_parsed
[i
]:
3033 all_surface_verts_co
.append(pt_co
)
3035 # Define verts for each face
3036 all_surface_faces
= []
3037 for i
in range(0, len(all_surface_verts_co
) - len(surface_splines_parsed
[0])):
3038 if ((i
+ 1) / len(surface_splines_parsed
[0]) != int((i
+ 1) / len(surface_splines_parsed
[0]))):
3039 all_surface_faces
.append(
3040 [i
+ 1, i
, i
+ len(surface_splines_parsed
[0]),
3041 i
+ len(surface_splines_parsed
[0]) + 1]
3044 surf_me_name
= "SURFSKIO_surface"
3045 me_surf
= bpy
.data
.meshes
.new(surf_me_name
)
3046 me_surf
.from_pydata(all_surface_verts_co
, [], all_surface_faces
)
3047 ob_surface
= object_utils
.object_data_add(context
, me_surf
)
3048 ob_surface
.location
= (0.0, 0.0, 0.0)
3049 ob_surface
.rotation_euler
= (0.0, 0.0, 0.0)
3050 ob_surface
.scale
= (1.0, 1.0, 1.0)
3052 # Select all the "unselected but participating" verts, from closed selection
3053 # or double selections with middle-vertex, for later join with remove doubles
3054 for v_idx
in single_unselected_verts
:
3055 self
.main_object
.data
.vertices
[v_idx
].select
= True
3057 # Join the new mesh to the main object
3058 ob_surface
.select_set(True)
3059 self
.main_object
.select_set(True)
3060 bpy
.context
.view_layer
.objects
.active
= self
.main_object
3062 bpy
.ops
.object.join('INVOKE_REGION_WIN')
3064 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3066 bpy
.ops
.mesh
.remove_doubles('INVOKE_REGION_WIN', threshold
=0.0001)
3067 bpy
.ops
.mesh
.normals_make_consistent('INVOKE_REGION_WIN', inside
=False)
3068 bpy
.ops
.mesh
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3076 global global_shade_smooth
3077 if global_shade_smooth
:
3078 bpy
.ops
.object.shade_smooth()
3080 bpy
.ops
.object.shade_flat()
3081 bpy
.context
.scene
.bsurfaces
.SURFSK_shade_smooth
= global_shade_smooth
3087 def execute(self
, context
):
3089 if bpy
.ops
.object.mode_set
.poll():
3090 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
3093 global global_mesh_object
3094 global_mesh_object
= bpy
.context
.scene
.bsurfaces
.SURFSK_mesh
.name
3095 bpy
.data
.objects
[global_mesh_object
].select_set(True)
3096 self
.main_object
= bpy
.data
.objects
[global_mesh_object
]
3097 bpy
.context
.view_layer
.objects
.active
= self
.main_object
3098 bsurfaces_props
= bpy
.context
.scene
.bsurfaces
3100 self
.report({'WARNING'}, "Specify the name of the object with retopology")
3102 bpy
.context
.view_layer
.objects
.active
= self
.main_object
3106 if not self
.is_fill_faces
:
3107 bpy
.ops
.wm
.context_set_value(data_path
='tool_settings.mesh_select_mode',
3108 value
='True, False, False')
3110 # Build splines from the "last saved splines".
3111 last_saved_curve
= bpy
.data
.curves
.new('SURFSKIO_last_crv', 'CURVE')
3112 self
.main_splines
= bpy
.data
.objects
.new('SURFSKIO_last_crv', last_saved_curve
)
3113 bpy
.context
.collection
.objects
.link(self
.main_splines
)
3115 last_saved_curve
.dimensions
= "3D"
3117 for sp
in self
.last_strokes_splines_coords
:
3118 spline
= self
.main_splines
.data
.splines
.new('BEZIER')
3119 # less one because one point is added when the spline is created
3120 spline
.bezier_points
.add(len(sp
) - 1)
3121 for p
in range(0, len(sp
)):
3122 spline
.bezier_points
[p
].co
= [sp
[p
][0], sp
[p
][1], sp
[p
][2]]
3124 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
3126 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3127 self
.main_splines
.select_set(True)
3128 bpy
.context
.view_layer
.objects
.active
= self
.main_splines
3130 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='EDIT')
3132 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='SELECT')
3133 # Important to make it vector first and then automatic, otherwise the
3134 # tips handles get too big and distort the shrinkwrap results later
3135 bpy
.ops
.curve
.handle_type_set(type='VECTOR')
3136 bpy
.ops
.curve
.handle_type_set('INVOKE_REGION_WIN', type='AUTOMATIC')
3137 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3138 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3140 self
.main_splines
.name
= "SURFSKIO_temp_strokes"
3142 if self
.is_crosshatch
:
3143 strokes_for_crosshatch
= True
3144 strokes_for_rectangular_surface
= False
3146 strokes_for_rectangular_surface
= True
3147 strokes_for_crosshatch
= False
3149 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3151 if strokes_for_rectangular_surface
:
3152 self
.rectangular_surface(context
)
3153 elif strokes_for_crosshatch
:
3154 self
.crosshatch_surface_execute(context
)
3156 #Set Shade smooth to new polygons
3157 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
3158 global global_shade_smooth
3159 if global_shade_smooth
:
3160 bpy
.ops
.object.shade_smooth()
3162 bpy
.ops
.object.shade_flat()
3164 # Delete main splines
3165 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
3166 if self
.keep_strokes
:
3167 self
.main_splines
.name
= "keep_strokes"
3168 self
.main_splines
.data
.bevel_depth
= 0.001
3169 if "keep_strokes_material" in bpy
.data
.materials
:
3170 self
.main_splines
.data
.materials
.append(bpy
.data
.materials
["keep_strokes_material"])
3172 mat
= bpy
.data
.materials
.new("keep_strokes_material")
3173 mat
.diffuse_color
= (1, 0, 0, 0)
3174 mat
.specular_color
= (1, 0, 0)
3175 mat
.specular_intensity
= 0.0
3177 self
.main_splines
.data
.materials
.append(mat
)
3179 bpy
.ops
.object.delete({"selected_objects": [self
.main_splines
]})
3181 # Delete grease pencil strokes
3182 if self
.strokes_type
== "GP_STROKES" and not self
.stopping_errors
:
3184 bpy
.context
.scene
.bsurfaces
.SURFSK_gpencil
.data
.layers
.active
.clear()
3188 # Delete annotations
3189 if self
.strokes_type
== "GP_ANNOTATION" and not self
.stopping_errors
:
3191 bpy
.context
.annotation_data
.layers
.active
.clear()
3195 bsurfaces_props
= bpy
.context
.scene
.bsurfaces
3196 bsurfaces_props
.SURFSK_edges_U
= self
.edges_U
3197 bsurfaces_props
.SURFSK_edges_V
= self
.edges_V
3198 bsurfaces_props
.SURFSK_cyclic_cross
= self
.cyclic_cross
3199 bsurfaces_props
.SURFSK_cyclic_follow
= self
.cyclic_follow
3200 bsurfaces_props
.SURFSK_automatic_join
= self
.automatic_join
3201 bsurfaces_props
.SURFSK_loops_on_strokes
= self
.loops_on_strokes
3202 bsurfaces_props
.SURFSK_keep_strokes
= self
.keep_strokes
3204 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3205 self
.main_object
.select_set(True)
3206 bpy
.context
.view_layer
.objects
.active
= self
.main_object
3208 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3214 def invoke(self
, context
, event
):
3216 if bpy
.ops
.object.mode_set
.poll():
3217 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
3219 bsurfaces_props
= bpy
.context
.scene
.bsurfaces
3220 self
.cyclic_cross
= bsurfaces_props
.SURFSK_cyclic_cross
3221 self
.cyclic_follow
= bsurfaces_props
.SURFSK_cyclic_follow
3222 self
.automatic_join
= bsurfaces_props
.SURFSK_automatic_join
3223 self
.loops_on_strokes
= bsurfaces_props
.SURFSK_loops_on_strokes
3224 self
.keep_strokes
= bsurfaces_props
.SURFSK_keep_strokes
3227 global global_mesh_object
3228 global_mesh_object
= bpy
.context
.scene
.bsurfaces
.SURFSK_mesh
.name
3229 bpy
.data
.objects
[global_mesh_object
].select_set(True)
3230 self
.main_object
= bpy
.data
.objects
[global_mesh_object
]
3231 bpy
.context
.view_layer
.objects
.active
= self
.main_object
3233 self
.report({'WARNING'}, "Specify the name of the object with retopology")
3238 self
.main_object_selected_verts_count
= len([v
for v
in self
.main_object
.data
.vertices
if v
.select
])
3240 bpy
.ops
.wm
.context_set_value(data_path
='tool_settings.mesh_select_mode',
3241 value
='True, False, False')
3243 self
.edges_U
= bsurfaces_props
.SURFSK_edges_U
3244 self
.edges_V
= bsurfaces_props
.SURFSK_edges_V
3246 self
.is_fill_faces
= False
3247 self
.stopping_errors
= False
3248 self
.last_strokes_splines_coords
= []
3250 # Determine the type of the strokes
3251 self
.strokes_type
= get_strokes_type(context
)
3253 # Check if it will be used grease pencil strokes or curves
3254 # If there are strokes to be used
3255 if self
.strokes_type
== "GP_STROKES" or self
.strokes_type
== "EXTERNAL_CURVE" or self
.strokes_type
== "GP_ANNOTATION":
3256 if self
.strokes_type
== "GP_STROKES":
3257 # Convert grease pencil strokes to curve
3258 global global_gpencil_object
3259 gp
= bpy
.data
.objects
[global_gpencil_object
]
3260 self
.original_curve
= conver_gpencil_to_curve(self
, context
, gp
, 'GPensil')
3261 self
.using_external_curves
= False
3263 elif self
.strokes_type
== "GP_ANNOTATION":
3264 # Convert grease pencil strokes to curve
3265 gp
= bpy
.context
.annotation_data
3266 self
.original_curve
= conver_gpencil_to_curve(self
, context
, gp
, 'Annotation')
3267 self
.using_external_curves
= False
3269 elif self
.strokes_type
== "EXTERNAL_CURVE":
3270 global global_curve_object
3271 self
.original_curve
= bpy
.data
.objects
[global_curve_object
]
3272 self
.using_external_curves
= True
3274 # Make sure there are no objects left from erroneous
3275 # executions of this operator, with the reserved names used here
3276 for o
in bpy
.data
.objects
:
3277 if o
.name
.find("SURFSKIO_") != -1:
3278 bpy
.ops
.object.delete({"selected_objects": [o
]})
3280 bpy
.context
.view_layer
.objects
.active
= self
.original_curve
3282 bpy
.ops
.object.duplicate('INVOKE_REGION_WIN')
3284 self
.temporary_curve
= bpy
.context
.view_layer
.objects
.active
3286 # Deselect all points of the curve
3287 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3288 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3289 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3291 # Delete splines with only a single isolated point
3292 for i
in range(len(self
.temporary_curve
.data
.splines
)):
3293 sp
= self
.temporary_curve
.data
.splines
[i
]
3295 if len(sp
.bezier_points
) == 1:
3296 sp
.bezier_points
[0].select_control_point
= True
3298 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3299 bpy
.ops
.curve
.delete(type='VERT')
3300 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3302 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3303 self
.temporary_curve
.select_set(True)
3304 bpy
.context
.view_layer
.objects
.active
= self
.temporary_curve
3306 # Set a minimum number of points for crosshatch
3307 minimum_points_num
= 15
3309 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3310 # Check if the number of points of each curve has at least the number of points
3311 # of minimum_points_num, which is a bit more than the face-loops limit.
3312 # If not, subdivide to reach at least that number of points
3313 for i
in range(len(self
.temporary_curve
.data
.splines
)):
3314 sp
= self
.temporary_curve
.data
.splines
[i
]
3316 if len(sp
.bezier_points
) < minimum_points_num
:
3317 for bp
in sp
.bezier_points
:
3318 bp
.select_control_point
= True
3320 if (len(sp
.bezier_points
) - 1) != 0:
3321 # Formula to get the number of cuts that will make a curve
3322 # of N number of points have near to "minimum_points_num"
3323 # points, when subdividing with this number of cuts
3324 subdivide_cuts
= int(
3325 (minimum_points_num
- len(sp
.bezier_points
)) /
3326 (len(sp
.bezier_points
) - 1)
3331 bpy
.ops
.curve
.subdivide('INVOKE_REGION_WIN', number_cuts
=subdivide_cuts
)
3332 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3334 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3336 # Detect if the strokes are a crosshatch and do it if it is
3337 self
.crosshatch_surface_invoke(self
.temporary_curve
)
3339 if not self
.is_crosshatch
:
3340 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3341 self
.temporary_curve
.select_set(True)
3342 bpy
.context
.view_layer
.objects
.active
= self
.temporary_curve
3344 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3346 # Set a minimum number of points for rectangular surfaces
3347 minimum_points_num
= 60
3349 # Check if the number of points of each curve has at least the number of points
3350 # of minimum_points_num, which is a bit more than the face-loops limit.
3351 # If not, subdivide to reach at least that number of points
3352 for i
in range(len(self
.temporary_curve
.data
.splines
)):
3353 sp
= self
.temporary_curve
.data
.splines
[i
]
3355 if len(sp
.bezier_points
) < minimum_points_num
:
3356 for bp
in sp
.bezier_points
:
3357 bp
.select_control_point
= True
3359 if (len(sp
.bezier_points
) - 1) != 0:
3360 # Formula to get the number of cuts that will make a curve of
3361 # N number of points have near to "minimum_points_num" points,
3362 # when subdividing with this number of cuts
3363 subdivide_cuts
= int(
3364 (minimum_points_num
- len(sp
.bezier_points
)) /
3365 (len(sp
.bezier_points
) - 1)
3370 bpy
.ops
.curve
.subdivide('INVOKE_REGION_WIN', number_cuts
=subdivide_cuts
)
3371 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3373 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3375 # Save coordinates of the actual strokes (as the "last saved splines")
3376 for sp_idx
in range(len(self
.temporary_curve
.data
.splines
)):
3377 self
.last_strokes_splines_coords
.append([])
3378 for bp_idx
in range(len(self
.temporary_curve
.data
.splines
[sp_idx
].bezier_points
)):
3379 coords
= self
.temporary_curve
.matrix_world
@ \
3380 self
.temporary_curve
.data
.splines
[sp_idx
].bezier_points
[bp_idx
].co
3381 self
.last_strokes_splines_coords
[sp_idx
].append([coords
[0], coords
[1], coords
[2]])
3383 # Check for cyclic splines, put the first and last points in the middle of their actual positions
3384 for sp_idx
in range(len(self
.temporary_curve
.data
.splines
)):
3385 if self
.temporary_curve
.data
.splines
[sp_idx
].use_cyclic_u
is True:
3386 first_p_co
= self
.last_strokes_splines_coords
[sp_idx
][0]
3387 last_p_co
= self
.last_strokes_splines_coords
[sp_idx
][
3388 len(self
.last_strokes_splines_coords
[sp_idx
]) - 1
3391 (first_p_co
[0] + last_p_co
[0]) / 2,
3392 (first_p_co
[1] + last_p_co
[1]) / 2,
3393 (first_p_co
[2] + last_p_co
[2]) / 2
3396 self
.last_strokes_splines_coords
[sp_idx
][0] = target_co
3397 self
.last_strokes_splines_coords
[sp_idx
][
3398 len(self
.last_strokes_splines_coords
[sp_idx
]) - 1
3400 tuple(self
.last_strokes_splines_coords
)
3402 # Estimation of the average length of the segments between
3403 # each point of the grease pencil strokes.
3404 # Will be useful to determine whether a curve should be made "Cyclic"
3405 segments_lengths_sum
= 0
3407 random_spline
= self
.temporary_curve
.data
.splines
[0].bezier_points
3408 for i
in range(0, len(random_spline
)):
3409 if i
!= 0 and len(random_spline
) - 1 >= i
:
3410 segments_lengths_sum
+= (random_spline
[i
- 1].co
- random_spline
[i
].co
).length
3413 self
.average_gp_segment_length
= segments_lengths_sum
/ segments_count
3415 # Delete temporary strokes curve object
3416 bpy
.ops
.object.delete({"selected_objects": [self
.temporary_curve
]})
3418 # Set again since "execute()" will turn it again to its initial value
3419 self
.execute(context
)
3421 if not self
.stopping_errors
:
3422 # Delete grease pencil strokes
3423 if self
.strokes_type
== "GP_STROKES":
3425 bpy
.context
.scene
.bsurfaces
.SURFSK_gpencil
.data
.layers
.active
.clear()
3429 # Delete annotation strokes
3430 elif self
.strokes_type
== "GP_ANNOTATION":
3432 bpy
.context
.annotation_data
.layers
.active
.clear()
3436 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3437 bpy
.ops
.object.delete({"selected_objects": [self
.original_curve
]})
3438 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3444 elif self
.strokes_type
== "SELECTION_ALONE":
3445 self
.is_fill_faces
= True
3446 created_faces_count
= self
.fill_with_faces(self
.main_object
)
3448 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3450 if created_faces_count
== 0:
3451 self
.report({'WARNING'}, "There aren't any strokes attached to the object")
3452 return {"CANCELLED"}
3456 if self
.strokes_type
== "EXTERNAL_NO_CURVE":
3457 self
.report({'WARNING'}, "The secondary object is not a Curve.")
3460 elif self
.strokes_type
== "MORE_THAN_ONE_EXTERNAL":
3461 self
.report({'WARNING'}, "There shouldn't be more than one secondary object selected.")
3464 elif self
.strokes_type
== "SINGLE_GP_STROKE_NO_SELECTION" or \
3465 self
.strokes_type
== "SINGLE_CURVE_STROKE_NO_SELECTION":
3467 self
.report({'WARNING'}, "It's needed at least one stroke and one selection, or two strokes.")
3470 elif self
.strokes_type
== "NO_STROKES":
3471 self
.report({'WARNING'}, "There aren't any strokes attached to the object")
3474 elif self
.strokes_type
== "CURVE_WITH_NON_BEZIER_SPLINES":
3475 self
.report({'WARNING'}, "All splines must be Bezier.")
3481 # ----------------------------
3483 class MESH_OT_SURFSK_init(Operator
):
3484 bl_idname
= "mesh.surfsk_init"
3485 bl_label
= "Bsurfaces initialize"
3486 bl_description
= "Add an empty mesh object with useful settings"
3487 bl_options
= {'REGISTER', 'UNDO'}
3489 def execute(self
, context
):
3491 bs
= bpy
.context
.scene
.bsurfaces
3493 if bpy
.ops
.object.mode_set
.poll():
3494 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
3496 global global_shade_smooth
3497 global global_mesh_object
3498 global global_gpencil_object
3500 if bs
.SURFSK_mesh
== None:
3501 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3502 mesh
= bpy
.data
.meshes
.new('BSurfaceMesh')
3503 mesh_object
= object_utils
.object_data_add(context
, mesh
)
3504 mesh_object
.select_set(True)
3505 bpy
.context
.view_layer
.objects
.active
= mesh_object
3507 mesh_object
.show_all_edges
= True
3508 mesh_object
.display_type
= 'SOLID'
3509 mesh_object
.show_wire
= True
3511 global_shade_smooth
= bpy
.context
.scene
.bsurfaces
.SURFSK_shade_smooth
3512 if global_shade_smooth
:
3513 bpy
.ops
.object.shade_smooth()
3515 bpy
.ops
.object.shade_flat()
3517 color_red
= [1.0, 0.0, 0.0, 0.3]
3518 material
= makeMaterial("BSurfaceMesh", color_red
)
3519 mesh_object
.data
.materials
.append(material
)
3520 modifier
= mesh_object
.modifiers
.new("", 'SHRINKWRAP')
3521 if self
.active_object
is not None:
3522 modifier
.target
= self
.active_object
3523 modifier
.wrap_method
= 'TARGET_PROJECT'
3524 modifier
.wrap_mode
= 'OUTSIDE_SURFACE'
3525 modifier
.show_on_cage
= True
3527 global_mesh_object
= mesh_object
.name
3528 bpy
.context
.scene
.bsurfaces
.SURFSK_mesh
= bpy
.data
.objects
[global_mesh_object
]
3530 bpy
.context
.scene
.tool_settings
.snap_elements
= {'FACE'}
3531 bpy
.context
.scene
.tool_settings
.use_snap
= True
3532 bpy
.context
.scene
.tool_settings
.use_snap_self
= False
3533 bpy
.context
.scene
.tool_settings
.use_snap_align_rotation
= True
3534 bpy
.context
.scene
.tool_settings
.use_snap_project
= True
3535 bpy
.context
.scene
.tool_settings
.use_snap_rotate
= True
3536 bpy
.context
.scene
.tool_settings
.use_snap_scale
= True
3538 bpy
.context
.scene
.tool_settings
.use_mesh_automerge
= True
3539 bpy
.context
.scene
.tool_settings
.double_threshold
= 0.01
3541 if context
.scene
.bsurfaces
.SURFSK_guide
== 'GPencil' and bs
.SURFSK_gpencil
== None:
3542 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3543 bpy
.ops
.object.gpencil_add(radius
=1.0, align
='WORLD', location
=(0.0, 0.0, 0.0), rotation
=(0.0, 0.0, 0.0), type='EMPTY')
3544 bpy
.context
.scene
.tool_settings
.gpencil_stroke_placement_view3d
= 'SURFACE'
3545 gpencil_object
= bpy
.context
.scene
.objects
[bpy
.context
.scene
.objects
[-1].name
]
3546 gpencil_object
.select_set(True)
3547 bpy
.context
.view_layer
.objects
.active
= gpencil_object
3548 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='PAINT_GPENCIL')
3549 global_gpencil_object
= gpencil_object
.name
3550 bpy
.context
.scene
.bsurfaces
.SURFSK_gpencil
= bpy
.data
.objects
[global_gpencil_object
]
3551 gpencil_object
.data
.stroke_depth_order
= '3D'
3552 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='PAINT_GPENCIL')
3553 bpy
.ops
.wm
.tool_set_by_id(name
="builtin_brush.Draw")
3555 if context
.scene
.bsurfaces
.SURFSK_guide
== 'Annotation':
3556 bpy
.ops
.wm
.tool_set_by_id(name
="builtin.annotate")
3557 bpy
.context
.scene
.tool_settings
.annotation_stroke_placement_view3d
= 'SURFACE'
3559 def invoke(self
, context
, event
):
3560 if bpy
.context
.active_object
:
3561 self
.active_object
= bpy
.context
.active_object
3563 self
.active_object
= None
3565 self
.execute(context
)
3569 # ----------------------------
3570 # Add modifiers operator
3571 class MESH_OT_SURFSK_add_modifiers(Operator
):
3572 bl_idname
= "mesh.surfsk_add_modifiers"
3573 bl_label
= "Add Mirror and others modifiers"
3574 bl_description
= "Add modifiers: Mirror, Shrinkwrap, Subdivision, Solidify"
3575 bl_options
= {'REGISTER', 'UNDO'}
3577 def execute(self
, context
):
3579 bs
= bpy
.context
.scene
.bsurfaces
3581 if bpy
.ops
.object.mode_set
.poll():
3582 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
3584 if bs
.SURFSK_mesh
== None:
3585 self
.report({'ERROR_INVALID_INPUT'}, "Please select Mesh of BSurface or click Initialize")
3587 mesh_object
= bs
.SURFSK_mesh
3590 mesh_object
.select_set(True)
3592 self
.report({'ERROR_INVALID_INPUT'}, "Mesh of BSurface does not exist")
3595 bpy
.context
.view_layer
.objects
.active
= mesh_object
3598 shrinkwrap
= next(mod
for mod
in mesh_object
.modifiers
3599 if mod
.type == 'SHRINKWRAP')
3601 shrinkwrap
= mesh_object
.modifiers
.new("", 'SHRINKWRAP')
3602 if self
.active_object
is not None and self
.active_object
!= mesh_object
:
3603 shrinkwrap
.target
= self
.active_object
3604 shrinkwrap
.wrap_method
= 'TARGET_PROJECT'
3605 shrinkwrap
.wrap_mode
= 'OUTSIDE_SURFACE'
3606 shrinkwrap
.show_on_cage
= True
3607 shrinkwrap
.offset
= bpy
.context
.scene
.bsurfaces
.SURFSK_Shrinkwrap_offset
3610 mirror
= next(mod
for mod
in mesh_object
.modifiers
3611 if mod
.type == 'MIRROR')
3613 mirror
= mesh_object
.modifiers
.new("", 'MIRROR')
3614 mirror
.use_clip
= True
3617 _subsurf
= next(mod
for mod
in mesh_object
.modifiers
3618 if mod
.type == 'SUBSURF')
3620 _subsurf
= mesh_object
.modifiers
.new("", 'SUBSURF')
3623 solidify
= next(mod
for mod
in mesh_object
.modifiers
3624 if mod
.type == 'SOLIDIFY')
3626 solidify
= mesh_object
.modifiers
.new("", 'SOLIDIFY')
3627 solidify
.thickness
= 0.01
3631 def invoke(self
, context
, event
):
3632 if bpy
.context
.active_object
:
3633 self
.active_object
= bpy
.context
.active_object
3635 self
.active_object
= None
3637 self
.execute(context
)
3641 # ----------------------------
3642 # Edit surface operator
3643 class MESH_OT_SURFSK_edit_surface(Operator
):
3644 bl_idname
= "mesh.surfsk_edit_surface"
3645 bl_label
= "Bsurfaces edit surface"
3646 bl_description
= "Edit surface mesh"
3647 bl_options
= {'REGISTER', 'UNDO'}
3649 def execute(self
, context
):
3650 if bpy
.ops
.object.mode_set
.poll():
3651 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
3652 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3653 bpy
.context
.scene
.bsurfaces
.SURFSK_mesh
.select_set(True)
3654 bpy
.context
.view_layer
.objects
.active
= bpy
.context
.scene
.bsurfaces
.SURFSK_mesh
3655 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='EDIT')
3656 bpy
.ops
.wm
.tool_set_by_id(name
="builtin.select")
3658 def invoke(self
, context
, event
):
3660 global_mesh_object
= bpy
.context
.scene
.bsurfaces
.SURFSK_mesh
.name
3661 bpy
.data
.objects
[global_mesh_object
].select_set(True)
3662 self
.main_object
= bpy
.data
.objects
[global_mesh_object
]
3663 bpy
.context
.view_layer
.objects
.active
= self
.main_object
3665 self
.report({'WARNING'}, "Specify the name of the object with retopology")
3668 self
.execute(context
)
3672 # ----------------------------
3673 # Add strokes operator
3674 class GPENCIL_OT_SURFSK_add_strokes(Operator
):
3675 bl_idname
= "gpencil.surfsk_add_strokes"
3676 bl_label
= "Bsurfaces add strokes"
3677 bl_description
= "Add the grease pencil strokes"
3678 bl_options
= {'REGISTER', 'UNDO'}
3680 def execute(self
, context
):
3681 if bpy
.ops
.object.mode_set
.poll():
3682 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
3683 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3685 bpy
.context
.scene
.bsurfaces
.SURFSK_gpencil
.select_set(True)
3686 bpy
.context
.view_layer
.objects
.active
= bpy
.context
.scene
.bsurfaces
.SURFSK_gpencil
3687 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='PAINT_GPENCIL')
3688 bpy
.ops
.wm
.tool_set_by_id(name
="builtin_brush.Draw")
3692 def invoke(self
, context
, event
):
3694 bpy
.context
.scene
.bsurfaces
.SURFSK_gpencil
.select_set(True)
3696 self
.report({'WARNING'}, "Specify the name of the object with strokes")
3699 self
.execute(context
)
3703 # ----------------------------
3704 # Edit strokes operator
3705 class GPENCIL_OT_SURFSK_edit_strokes(Operator
):
3706 bl_idname
= "gpencil.surfsk_edit_strokes"
3707 bl_label
= "Bsurfaces edit strokes"
3708 bl_description
= "Edit the grease pencil strokes"
3709 bl_options
= {'REGISTER', 'UNDO'}
3711 def execute(self
, context
):
3712 if bpy
.ops
.object.mode_set
.poll():
3713 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
3714 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3716 gpencil_object
= bpy
.context
.scene
.bsurfaces
.SURFSK_gpencil
3718 gpencil_object
.select_set(True)
3719 bpy
.context
.view_layer
.objects
.active
= gpencil_object
3721 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='EDIT_GPENCIL')
3723 bpy
.ops
.gpencil
.select_all(action
='SELECT')
3727 def invoke(self
, context
, event
):
3729 bpy
.context
.scene
.bsurfaces
.SURFSK_gpencil
.select_set(True)
3731 self
.report({'WARNING'}, "Specify the name of the object with strokes")
3734 self
.execute(context
)
3738 # ----------------------------
3739 # Convert annotation to curves operator
3740 class GPENCIL_OT_SURFSK_annotation_to_curves(Operator
):
3741 bl_idname
= "gpencil.surfsk_annotations_to_curves"
3742 bl_label
= "Convert annotation to curves"
3743 bl_description
= "Convert annotation to curves for editing"
3744 bl_options
= {'REGISTER', 'UNDO'}
3746 def execute(self
, context
):
3748 if bpy
.ops
.object.mode_set
.poll():
3749 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
3751 # Convert annotation to curve
3752 curve
= conver_gpencil_to_curve(self
, context
, None, 'Annotation')
3755 # Delete annotation strokes
3757 bpy
.context
.annotation_data
.layers
.active
.clear()
3762 curve
.select_set(True)
3763 bpy
.context
.view_layer
.objects
.active
= curve
3765 bpy
.ops
.wm
.tool_set_by_id(name
="builtin.select_box")
3769 def invoke(self
, context
, event
):
3771 strokes
= bpy
.context
.annotation_data
.layers
.active
.active_frame
.strokes
3773 _strokes_num
= len(strokes
)
3775 self
.report({'WARNING'}, "Not active annotation")
3778 self
.execute(context
)
3782 # ----------------------------
3783 # Convert strokes to curves operator
3784 class GPENCIL_OT_SURFSK_strokes_to_curves(Operator
):
3785 bl_idname
= "gpencil.surfsk_strokes_to_curves"
3786 bl_label
= "Convert strokes to curves"
3787 bl_description
= "Convert grease pencil strokes to curves for editing"
3788 bl_options
= {'REGISTER', 'UNDO'}
3790 def execute(self
, context
):
3792 if bpy
.ops
.object.mode_set
.poll():
3793 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
3795 # Convert grease pencil strokes to curve
3796 gp
= bpy
.context
.scene
.bsurfaces
.SURFSK_gpencil
3797 curve
= conver_gpencil_to_curve(self
, context
, gp
, 'GPensil')
3800 # Delete grease pencil strokes
3802 bpy
.context
.scene
.bsurfaces
.SURFSK_gpencil
.data
.layers
.active
.clear()
3808 curve
.select_set(True)
3809 bpy
.context
.view_layer
.objects
.active
= curve
3811 bpy
.ops
.wm
.tool_set_by_id(name
="builtin.select_box")
3815 def invoke(self
, context
, event
):
3817 bpy
.context
.scene
.bsurfaces
.SURFSK_gpencil
.select_set(True)
3819 self
.report({'WARNING'}, "Specify the name of the object with strokes")
3822 self
.execute(context
)
3826 # ----------------------------
3828 class GPENCIL_OT_SURFSK_add_annotation(Operator
):
3829 bl_idname
= "gpencil.surfsk_add_annotation"
3830 bl_label
= "Bsurfaces add annotation"
3831 bl_description
= "Add annotation"
3832 bl_options
= {'REGISTER', 'UNDO'}
3834 def execute(self
, context
):
3835 bpy
.ops
.wm
.tool_set_by_id(name
="builtin.annotate")
3836 bpy
.context
.scene
.tool_settings
.annotation_stroke_placement_view3d
= 'SURFACE'
3840 def invoke(self
, context
, event
):
3842 self
.execute(context
)
3847 # ----------------------------
3848 # Edit curve operator
3849 class CURVE_OT_SURFSK_edit_curve(Operator
):
3850 bl_idname
= "curve.surfsk_edit_curve"
3851 bl_label
= "Bsurfaces edit curve"
3852 bl_description
= "Edit curve"
3853 bl_options
= {'REGISTER', 'UNDO'}
3855 def execute(self
, context
):
3856 if bpy
.ops
.object.mode_set
.poll():
3857 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
3858 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3859 bpy
.context
.scene
.bsurfaces
.SURFSK_curve
.select_set(True)
3860 bpy
.context
.view_layer
.objects
.active
= bpy
.context
.scene
.bsurfaces
.SURFSK_curve
3861 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='EDIT')
3863 def invoke(self
, context
, event
):
3865 bpy
.context
.scene
.bsurfaces
.SURFSK_curve
.select_set(True)
3867 self
.report({'WARNING'}, "Specify the name of the object with curve")
3870 self
.execute(context
)
3874 # ----------------------------
3876 class CURVE_OT_SURFSK_reorder_splines(Operator
):
3877 bl_idname
= "curve.surfsk_reorder_splines"
3878 bl_label
= "Bsurfaces reorder splines"
3879 bl_description
= "Defines the order of the splines by using grease pencil strokes"
3880 bl_options
= {'REGISTER', 'UNDO'}
3882 def execute(self
, context
):
3883 objects_to_delete
= []
3884 # Convert grease pencil strokes to curve.
3885 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3886 bpy
.ops
.gpencil
.convert('INVOKE_REGION_WIN', type='CURVE', use_link_strokes
=False)
3887 for ob
in bpy
.context
.selected_objects
:
3888 if ob
!= bpy
.context
.view_layer
.objects
.active
and ob
.name
.startswith("GP_Layer"):
3889 GP_strokes_curve
= ob
3891 # GP_strokes_curve = bpy.context.object
3892 objects_to_delete
.append(GP_strokes_curve
)
3894 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3895 GP_strokes_curve
.select_set(True)
3896 bpy
.context
.view_layer
.objects
.active
= GP_strokes_curve
3898 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3899 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='SELECT')
3900 bpy
.ops
.curve
.subdivide('INVOKE_REGION_WIN', number_cuts
=100)
3901 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3903 bpy
.ops
.object.duplicate('INVOKE_REGION_WIN')
3904 GP_strokes_mesh
= bpy
.context
.object
3905 objects_to_delete
.append(GP_strokes_mesh
)
3907 GP_strokes_mesh
.data
.resolution_u
= 1
3908 bpy
.ops
.object.convert(target
='MESH', keep_original
=False)
3910 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3911 self
.main_curve
.select_set(True)
3912 bpy
.context
.view_layer
.objects
.active
= self
.main_curve
3914 bpy
.ops
.object.duplicate('INVOKE_REGION_WIN')
3915 curves_duplicate_1
= bpy
.context
.object
3916 objects_to_delete
.append(curves_duplicate_1
)
3918 minimum_points_num
= 500
3920 # Some iterations since the subdivision operator
3921 # has a limit of 100 subdivisions per iteration
3922 for x
in range(round(minimum_points_num
/ 100)):
3923 # Check if the number of points of each curve has at least the number of points
3924 # of minimum_points_num. If not, subdivide to reach at least that number of points
3925 for i
in range(len(curves_duplicate_1
.data
.splines
)):
3926 sp
= curves_duplicate_1
.data
.splines
[i
]
3928 if len(sp
.bezier_points
) < minimum_points_num
:
3929 for bp
in sp
.bezier_points
:
3930 bp
.select_control_point
= True
3932 if (len(sp
.bezier_points
) - 1) != 0:
3933 # Formula to get the number of cuts that will make a curve of N
3934 # number of points have near to "minimum_points_num" points,
3935 # when subdividing with this number of cuts
3936 subdivide_cuts
= int(
3937 (minimum_points_num
- len(sp
.bezier_points
)) /
3938 (len(sp
.bezier_points
) - 1)
3943 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3944 bpy
.ops
.curve
.subdivide('INVOKE_REGION_WIN', number_cuts
=subdivide_cuts
)
3945 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3946 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
3948 bpy
.ops
.object.duplicate('INVOKE_REGION_WIN')
3949 curves_duplicate_2
= bpy
.context
.object
3950 objects_to_delete
.append(curves_duplicate_2
)
3952 # Duplicate the duplicate and add Shrinkwrap to it, with the grease pencil strokes curve as target
3953 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
3954 curves_duplicate_2
.select_set(True)
3955 bpy
.context
.view_layer
.objects
.active
= curves_duplicate_2
3957 shrinkwrap
= curves_duplicate_2
.modifiers
.new("", 'SHRINKWRAP')
3958 shrinkwrap
.wrap_method
= "NEAREST_VERTEX"
3959 shrinkwrap
.target
= GP_strokes_mesh
3960 bpy
.ops
.object.modifier_apply('INVOKE_REGION_WIN', modifier
=shrinkwrap
.name
)
3962 # Get the distance of each vert from its original position to its position with Shrinkwrap
3963 nearest_points_coords
= {}
3964 for st_idx
in range(len(curves_duplicate_1
.data
.splines
)):
3965 for bp_idx
in range(len(curves_duplicate_1
.data
.splines
[st_idx
].bezier_points
)):
3966 bp_1_co
= curves_duplicate_1
.matrix_world
@ \
3967 curves_duplicate_1
.data
.splines
[st_idx
].bezier_points
[bp_idx
].co
3969 bp_2_co
= curves_duplicate_2
.matrix_world
@ \
3970 curves_duplicate_2
.data
.splines
[st_idx
].bezier_points
[bp_idx
].co
3973 shortest_dist
= (bp_1_co
- bp_2_co
).length
3974 nearest_points_coords
[st_idx
] = ("%.4f" % bp_2_co
[0],
3975 "%.4f" % bp_2_co
[1],
3976 "%.4f" % bp_2_co
[2])
3978 dist
= (bp_1_co
- bp_2_co
).length
3980 if dist
< shortest_dist
:
3981 nearest_points_coords
[st_idx
] = ("%.4f" % bp_2_co
[0],
3982 "%.4f" % bp_2_co
[1],
3983 "%.4f" % bp_2_co
[2])
3984 shortest_dist
= dist
3986 # Get all coords of GP strokes points, for comparison
3987 GP_strokes_coords
= []
3988 for st_idx
in range(len(GP_strokes_curve
.data
.splines
)):
3989 GP_strokes_coords
.append(
3990 [("%.4f" % x
if "%.4f" % x
!= "-0.00" else "0.00",
3991 "%.4f" % y
if "%.4f" % y
!= "-0.00" else "0.00",
3992 "%.4f" % z
if "%.4f" % z
!= "-0.00" else "0.00") for
3993 x
, y
, z
in [bp
.co
for bp
in GP_strokes_curve
.data
.splines
[st_idx
].bezier_points
]]
3996 # Check the point of the GP strokes with the same coords as
3997 # the nearest points of the curves (with shrinkwrap)
3999 # Dictionary with GP stroke index as index, and a list as value.
4000 # The list has as index the point index of the GP stroke
4001 # nearest to the spline, and as value the spline index
4002 GP_connection_points
= {}
4003 for gp_st_idx
in range(len(GP_strokes_coords
)):
4004 GPvert_spline_relationship
= {}
4006 for splines_st_idx
in range(len(nearest_points_coords
)):
4007 if nearest_points_coords
[splines_st_idx
] in GP_strokes_coords
[gp_st_idx
]:
4008 GPvert_spline_relationship
[
4009 GP_strokes_coords
[gp_st_idx
].index(nearest_points_coords
[splines_st_idx
])
4012 GP_connection_points
[gp_st_idx
] = GPvert_spline_relationship
4014 # Get the splines new order
4015 splines_new_order
= []
4016 for i
in GP_connection_points
:
4017 dict_keys
= sorted(GP_connection_points
[i
].keys()) # Sort dictionaries by key
4020 splines_new_order
.append(GP_connection_points
[i
][k
])
4023 curve_original_name
= self
.main_curve
.name
4025 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
4026 self
.main_curve
.select_set(True)
4027 bpy
.context
.view_layer
.objects
.active
= self
.main_curve
4029 self
.main_curve
.name
= "SURFSKIO_CRV_ORD"
4031 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
4032 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
4033 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
4035 for _sp_idx
in range(len(self
.main_curve
.data
.splines
)):
4036 self
.main_curve
.data
.splines
[0].bezier_points
[0].select_control_point
= True
4038 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
4039 bpy
.ops
.curve
.separate('EXEC_REGION_WIN')
4040 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
4042 # Get the names of the separated splines objects in the original order
4043 splines_unordered
= {}
4044 for o
in bpy
.data
.objects
:
4045 if o
.name
.find("SURFSKIO_CRV_ORD") != -1:
4046 spline_order_string
= o
.name
.partition(".")[2]
4048 if spline_order_string
!= "" and int(spline_order_string
) > 0:
4049 spline_order_index
= int(spline_order_string
) - 1
4050 splines_unordered
[spline_order_index
] = o
.name
4052 # Join all splines objects in final order
4053 for order_idx
in splines_new_order
:
4054 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
4055 bpy
.data
.objects
[splines_unordered
[order_idx
]].select_set(True)
4056 bpy
.data
.objects
["SURFSKIO_CRV_ORD"].select_set(True)
4057 bpy
.context
.view_layer
.objects
.active
= bpy
.data
.objects
["SURFSKIO_CRV_ORD"]
4059 bpy
.ops
.object.join('INVOKE_REGION_WIN')
4061 # Go back to the original name of the curves object.
4062 bpy
.context
.object.name
= curve_original_name
4064 # Delete all unused objects
4065 bpy
.ops
.object.delete({"selected_objects": objects_to_delete
})
4067 bpy
.ops
.object.select_all('INVOKE_REGION_WIN', action
='DESELECT')
4068 bpy
.data
.objects
[curve_original_name
].select_set(True)
4069 bpy
.context
.view_layer
.objects
.active
= bpy
.data
.objects
[curve_original_name
]
4071 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
4072 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
4075 bpy
.context
.scene
.bsurfaces
.SURFSK_gpencil
.data
.layers
.active
.clear()
4082 def invoke(self
, context
, event
):
4083 self
.main_curve
= bpy
.context
.object
4084 there_are_GP_strokes
= False
4087 # Get the active grease pencil layer
4088 strokes_num
= len(self
.main_curve
.grease_pencil
.layers
.active
.active_frame
.strokes
)
4091 there_are_GP_strokes
= True
4095 if there_are_GP_strokes
:
4096 self
.execute(context
)
4097 self
.report({'INFO'}, "Splines have been reordered")
4099 self
.report({'WARNING'}, "Draw grease pencil strokes to connect splines")
4103 # ----------------------------
4104 # Set first points operator
4105 class CURVE_OT_SURFSK_first_points(Operator
):
4106 bl_idname
= "curve.surfsk_first_points"
4107 bl_label
= "Bsurfaces set first points"
4108 bl_description
= "Set the selected points as the first point of each spline"
4109 bl_options
= {'REGISTER', 'UNDO'}
4111 def execute(self
, context
):
4112 splines_to_invert
= []
4114 # Check non-cyclic splines to invert
4115 for i
in range(len(self
.main_curve
.data
.splines
)):
4116 b_points
= self
.main_curve
.data
.splines
[i
].bezier_points
4118 if i
not in self
.cyclic_splines
: # Only for non-cyclic splines
4119 if b_points
[len(b_points
) - 1].select_control_point
:
4120 splines_to_invert
.append(i
)
4122 # Reorder points of cyclic splines, and set all handles to "Automatic"
4124 # Check first selected point
4125 cyclic_splines_new_first_pt
= {}
4126 for i
in self
.cyclic_splines
:
4127 sp
= self
.main_curve
.data
.splines
[i
]
4129 for t
in range(len(sp
.bezier_points
)):
4130 bp
= sp
.bezier_points
[t
]
4131 if bp
.select_control_point
or bp
.select_right_handle
or bp
.select_left_handle
:
4132 cyclic_splines_new_first_pt
[i
] = t
4133 break # To take only one if there are more
4136 for spline_idx
in cyclic_splines_new_first_pt
:
4137 sp
= self
.main_curve
.data
.splines
[spline_idx
]
4139 spline_old_coords
= []
4140 for bp_old
in sp
.bezier_points
:
4141 coords
= (bp_old
.co
[0], bp_old
.co
[1], bp_old
.co
[2])
4143 left_handle_type
= str(bp_old
.handle_left_type
)
4144 left_handle_length
= float(bp_old
.handle_left
.length
)
4146 float(bp_old
.handle_left
.x
),
4147 float(bp_old
.handle_left
.y
),
4148 float(bp_old
.handle_left
.z
)
4150 right_handle_type
= str(bp_old
.handle_right_type
)
4151 right_handle_length
= float(bp_old
.handle_right
.length
)
4152 right_handle_xyz
= (
4153 float(bp_old
.handle_right
.x
),
4154 float(bp_old
.handle_right
.y
),
4155 float(bp_old
.handle_right
.z
)
4157 spline_old_coords
.append(
4158 [coords
, left_handle_type
,
4159 right_handle_type
, left_handle_length
,
4160 right_handle_length
, left_handle_xyz
,
4164 for t
in range(len(sp
.bezier_points
)):
4165 bp
= sp
.bezier_points
4167 if t
+ cyclic_splines_new_first_pt
[spline_idx
] + 1 <= len(bp
) - 1:
4168 new_index
= t
+ cyclic_splines_new_first_pt
[spline_idx
] + 1
4170 new_index
= t
+ cyclic_splines_new_first_pt
[spline_idx
] + 1 - len(bp
)
4172 bp
[t
].co
= Vector(spline_old_coords
[new_index
][0])
4174 bp
[t
].handle_left
.length
= spline_old_coords
[new_index
][3]
4175 bp
[t
].handle_right
.length
= spline_old_coords
[new_index
][4]
4177 bp
[t
].handle_left_type
= "FREE"
4178 bp
[t
].handle_right_type
= "FREE"
4180 bp
[t
].handle_left
.x
= spline_old_coords
[new_index
][5][0]
4181 bp
[t
].handle_left
.y
= spline_old_coords
[new_index
][5][1]
4182 bp
[t
].handle_left
.z
= spline_old_coords
[new_index
][5][2]
4184 bp
[t
].handle_right
.x
= spline_old_coords
[new_index
][6][0]
4185 bp
[t
].handle_right
.y
= spline_old_coords
[new_index
][6][1]
4186 bp
[t
].handle_right
.z
= spline_old_coords
[new_index
][6][2]
4188 bp
[t
].handle_left_type
= spline_old_coords
[new_index
][1]
4189 bp
[t
].handle_right_type
= spline_old_coords
[new_index
][2]
4191 # Invert the non-cyclic splines designated above
4192 for i
in range(len(splines_to_invert
)):
4193 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
4195 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
4196 self
.main_curve
.data
.splines
[splines_to_invert
[i
]].bezier_points
[0].select_control_point
= True
4197 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
4199 bpy
.ops
.curve
.switch_direction()
4201 bpy
.ops
.curve
.select_all('INVOKE_REGION_WIN', action
='DESELECT')
4203 # Keep selected the first vert of each spline
4204 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
4205 for i
in range(len(self
.main_curve
.data
.splines
)):
4206 if not self
.main_curve
.data
.splines
[i
].use_cyclic_u
:
4207 bp
= self
.main_curve
.data
.splines
[i
].bezier_points
[0]
4209 bp
= self
.main_curve
.data
.splines
[i
].bezier_points
[
4210 len(self
.main_curve
.data
.splines
[i
].bezier_points
) - 1
4213 bp
.select_control_point
= True
4214 bp
.select_right_handle
= True
4215 bp
.select_left_handle
= True
4217 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
4221 def invoke(self
, context
, event
):
4222 self
.main_curve
= bpy
.context
.object
4224 # Check if all curves are Bezier, and detect which ones are cyclic
4225 self
.cyclic_splines
= []
4226 for i
in range(len(self
.main_curve
.data
.splines
)):
4227 if self
.main_curve
.data
.splines
[i
].type != "BEZIER":
4228 self
.report({'WARNING'}, "All splines must be Bezier type")
4230 return {'CANCELLED'}
4232 if self
.main_curve
.data
.splines
[i
].use_cyclic_u
:
4233 self
.cyclic_splines
.append(i
)
4235 self
.execute(context
)
4236 self
.report({'INFO'}, "First points have been set")
4241 # Add-ons Preferences Update Panel
4243 # Define Panel classes for updating
4245 VIEW3D_PT_tools_SURFSK_mesh
,
4246 VIEW3D_PT_tools_SURFSK_curve
4250 def conver_gpencil_to_curve(self
, context
, pencil
, type):
4251 newCurve
= bpy
.data
.curves
.new(type + '_curve', type='CURVE')
4252 newCurve
.dimensions
= '3D'
4253 CurveObject
= object_utils
.object_data_add(context
, newCurve
)
4256 if type == 'GPensil':
4258 strokes
= pencil
.data
.layers
.active
.active_frame
.strokes
4261 CurveObject
.location
= pencil
.location
4262 CurveObject
.rotation_euler
= pencil
.rotation_euler
4263 CurveObject
.scale
= pencil
.scale
4264 elif type == 'Annotation':
4266 strokes
= bpy
.context
.annotation_data
.layers
.active
.active_frame
.strokes
4269 CurveObject
.location
= (0.0, 0.0, 0.0)
4270 CurveObject
.rotation_euler
= (0.0, 0.0, 0.0)
4271 CurveObject
.scale
= (1.0, 1.0, 1.0)
4274 for i
, _stroke
in enumerate(strokes
):
4275 stroke_points
= strokes
[i
].points
4276 data_list
= [ (point
.co
.x
, point
.co
.y
, point
.co
.z
)
4277 for point
in stroke_points
]
4278 points_to_add
= len(data_list
)-1
4281 for point
in data_list
:
4282 flat_list
.extend(point
)
4284 spline
= newCurve
.splines
.new(type='BEZIER')
4285 spline
.bezier_points
.add(points_to_add
)
4286 spline
.bezier_points
.foreach_set("co", flat_list
)
4288 for point
in spline
.bezier_points
:
4289 point
.handle_left_type
="AUTO"
4290 point
.handle_right_type
="AUTO"
4297 def update_panel(self
, context
):
4298 message
= "Bsurfaces GPL Edition: Updating Panel locations has failed"
4300 for panel
in panels
:
4301 if "bl_rna" in panel
.__dict
__:
4302 bpy
.utils
.unregister_class(panel
)
4304 for panel
in panels
:
4305 category
= context
.preferences
.addons
[__name__
].preferences
.category
4306 if category
!= 'Tool':
4307 panel
.bl_category
= context
.preferences
.addons
[__name__
].preferences
.category
4309 context
.preferences
.addons
[__name__
].preferences
.category
= 'Edit'
4310 panel
.bl_category
= 'Edit'
4311 raise ValueError("You can not install add-ons in the Tool panel")
4312 bpy
.utils
.register_class(panel
)
4314 except Exception as e
:
4315 print("\n[{}]\n{}\n\nError:\n{}".format(__name__
, message
, e
))
4318 def makeMaterial(name
, diffuse
):
4320 if name
in bpy
.data
.materials
:
4321 material
= bpy
.data
.materials
[name
]
4322 material
.diffuse_color
= diffuse
4324 material
= bpy
.data
.materials
.new(name
)
4325 material
.diffuse_color
= diffuse
4329 def update_mesh(self
, context
):
4331 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
4332 bpy
.ops
.object.select_all(action
='DESELECT')
4333 bpy
.context
.view_layer
.update()
4334 global global_mesh_object
4335 global_mesh_object
= bpy
.context
.scene
.bsurfaces
.SURFSK_mesh
.name
4336 bpy
.data
.objects
[global_mesh_object
].select_set(True)
4337 bpy
.context
.view_layer
.objects
.active
= bpy
.data
.objects
[global_mesh_object
]
4339 print("Select mesh object")
4341 def update_gpencil(self
, context
):
4343 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
4344 bpy
.ops
.object.select_all(action
='DESELECT')
4345 bpy
.context
.view_layer
.update()
4346 global global_gpencil_object
4347 global_gpencil_object
= bpy
.context
.scene
.bsurfaces
.SURFSK_gpencil
.name
4348 bpy
.data
.objects
[global_gpencil_object
].select_set(True)
4349 bpy
.context
.view_layer
.objects
.active
= bpy
.data
.objects
[global_gpencil_object
]
4351 print("Select gpencil object")
4353 def update_curve(self
, context
):
4355 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
4356 bpy
.ops
.object.select_all(action
='DESELECT')
4357 bpy
.context
.view_layer
.update()
4358 global global_curve_object
4359 global_curve_object
= bpy
.context
.scene
.bsurfaces
.SURFSK_curve
.name
4360 bpy
.data
.objects
[global_curve_object
].select_set(True)
4361 bpy
.context
.view_layer
.objects
.active
= bpy
.data
.objects
[global_curve_object
]
4363 print("Select curve object")
4365 def update_shade_smooth(self
, context
):
4367 global global_shade_smooth
4368 global_shade_smooth
= bpy
.context
.scene
.bsurfaces
.SURFSK_shade_smooth
4370 contex_mode
= bpy
.context
.mode
4372 if bpy
.ops
.object.mode_set
.poll():
4373 bpy
.ops
.object.mode_set('INVOKE_REGION_WIN', mode
='OBJECT')
4375 bpy
.ops
.object.select_all(action
='DESELECT')
4376 global global_mesh_object
4377 global_mesh_object
= bpy
.context
.scene
.bsurfaces
.SURFSK_mesh
.name
4378 bpy
.data
.objects
[global_mesh_object
].select_set(True)
4380 if global_shade_smooth
:
4381 bpy
.ops
.object.shade_smooth()
4383 bpy
.ops
.object.shade_flat()
4385 if contex_mode
== "EDIT_MESH":
4386 bpy
.ops
.object.editmode_toggle('INVOKE_REGION_WIN')
4389 print("Select mesh object")
4392 class BsurfPreferences(AddonPreferences
):
4393 # this must match the addon name, use '__package__'
4394 # when defining this in a submodule of a python package.
4395 bl_idname
= __name__
4397 category
: StringProperty(
4398 name
="Tab Category",
4399 description
="Choose a name for the category of the panel",
4404 def draw(self
, context
):
4405 layout
= self
.layout
4409 col
.label(text
="Tab Category:")
4410 col
.prop(self
, "category", text
="")
4413 class BsurfacesProps(PropertyGroup
):
4414 SURFSK_guide
: EnumProperty(
4417 ('Annotation', 'Annotation', 'Annotation'),
4418 ('GPencil', 'GPencil', 'GPencil'),
4419 ('Curve', 'Curve', 'Curve')
4421 default
="Annotation"
4423 SURFSK_edges_U
: IntProperty(
4425 description
="Number of face-loops crossing the strokes",
4430 SURFSK_edges_V
: IntProperty(
4432 description
="Number of face-loops following the strokes",
4437 SURFSK_cyclic_cross
: BoolProperty(
4438 name
="Cyclic Cross",
4439 description
="Make cyclic the face-loops crossing the strokes",
4442 SURFSK_cyclic_follow
: BoolProperty(
4443 name
="Cyclic Follow",
4444 description
="Make cyclic the face-loops following the strokes",
4447 SURFSK_keep_strokes
: BoolProperty(
4448 name
="Keep strokes",
4449 description
="Keeps the sketched strokes or curves after adding the surface",
4452 SURFSK_automatic_join
: BoolProperty(
4453 name
="Automatic join",
4454 description
="Join automatically vertices of either surfaces "
4455 "generated by crosshatching, or from the borders of closed shapes",
4458 SURFSK_loops_on_strokes
: BoolProperty(
4459 name
="Loops on strokes",
4460 description
="Make the loops match the paths of the strokes",
4463 SURFSK_precision
: IntProperty(
4465 description
="Precision level of the surface calculation",
4470 SURFSK_mesh
: PointerProperty(
4471 name
="Mesh of BSurface",
4472 type=bpy
.types
.Object
,
4473 description
="Mesh of BSurface",
4476 SURFSK_gpencil
: PointerProperty(
4477 name
="GreasePencil object",
4478 type=bpy
.types
.Object
,
4479 description
="GreasePencil object",
4480 update
=update_gpencil
,
4482 SURFSK_curve
: PointerProperty(
4483 name
="Curve object",
4484 type=bpy
.types
.Object
,
4485 description
="Curve object",
4486 update
=update_curve
,
4488 SURFSK_shade_smooth
: BoolProperty(
4489 name
="Shade smooth",
4490 description
="Render and display faces smooth, using interpolated Vertex Normals",
4492 update
=update_shade_smooth
,
4496 MESH_OT_SURFSK_init
,
4497 MESH_OT_SURFSK_add_modifiers
,
4498 MESH_OT_SURFSK_add_surface
,
4499 MESH_OT_SURFSK_edit_surface
,
4500 GPENCIL_OT_SURFSK_add_strokes
,
4501 GPENCIL_OT_SURFSK_edit_strokes
,
4502 GPENCIL_OT_SURFSK_strokes_to_curves
,
4503 GPENCIL_OT_SURFSK_annotation_to_curves
,
4504 GPENCIL_OT_SURFSK_add_annotation
,
4505 CURVE_OT_SURFSK_edit_curve
,
4506 CURVE_OT_SURFSK_reorder_splines
,
4507 CURVE_OT_SURFSK_first_points
,
4514 bpy
.utils
.register_class(cls
)
4516 for panel
in panels
:
4517 bpy
.utils
.register_class(panel
)
4519 bpy
.types
.Scene
.bsurfaces
= PointerProperty(type=BsurfacesProps
)
4520 update_panel(None, bpy
.context
)
4523 for panel
in panels
:
4524 bpy
.utils
.unregister_class(panel
)
4527 bpy
.utils
.unregister_class(cls
)
4529 del bpy
.types
.Scene
.bsurfaces
4531 if __name__
== "__main__":