Newer
Older
self.report({'WARNING'}, "Draw grease pencil strokes to connect splines")
CoDEmanX
committed
CoDEmanX
committed
class CURVE_OT_SURFSK_first_points(Operator):
bl_idname = "curve.surfsk_first_points"
bl_label = "Bsurfaces set first points"
bl_description = "Set the selected points as the first point of each spline"
bl_options = {'REGISTER', 'UNDO'}
CoDEmanX
committed
CoDEmanX
committed
# Check non-cyclic splines to invert
for i in range(len(self.main_curve.data.splines)):
b_points = self.main_curve.data.splines[i].bezier_points
CoDEmanX
committed
if i not in self.cyclic_splines: # Only for non-cyclic splines
if b_points[len(b_points) - 1].select_control_point:
splines_to_invert.append(i)
CoDEmanX
committed
# Reorder points of cyclic splines, and set all handles to "Automatic"
CoDEmanX
committed
cyclic_splines_new_first_pt = {}
for i in self.cyclic_splines:
sp = self.main_curve.data.splines[i]
CoDEmanX
committed
for t in range(len(sp.bezier_points)):
bp = sp.bezier_points[t]
if bp.select_control_point or bp.select_right_handle or bp.select_left_handle:
cyclic_splines_new_first_pt[i] = t
break # To take only one if there are more
CoDEmanX
committed
for spline_idx in cyclic_splines_new_first_pt:
sp = self.main_curve.data.splines[spline_idx]
CoDEmanX
committed
spline_old_coords = []
for bp_old in sp.bezier_points:
coords = (bp_old.co[0], bp_old.co[1], bp_old.co[2])
CoDEmanX
committed
left_handle_type = str(bp_old.handle_left_type)
left_handle_length = float(bp_old.handle_left.length)
left_handle_xyz = (
float(bp_old.handle_left.x),
float(bp_old.handle_left.y),
float(bp_old.handle_left.z)
)
right_handle_type = str(bp_old.handle_right_type)
right_handle_length = float(bp_old.handle_right.length)
right_handle_xyz = (
float(bp_old.handle_right.x),
float(bp_old.handle_right.y),
float(bp_old.handle_right.z)
)
spline_old_coords.append(
[coords, left_handle_type,
right_handle_type, left_handle_length,
right_handle_length, left_handle_xyz,
right_handle_xyz]
)
CoDEmanX
committed
for t in range(len(sp.bezier_points)):
bp = sp.bezier_points
CoDEmanX
committed
if t + cyclic_splines_new_first_pt[spline_idx] + 1 <= len(bp) - 1:
new_index = t + cyclic_splines_new_first_pt[spline_idx] + 1
else:
new_index = t + cyclic_splines_new_first_pt[spline_idx] + 1 - len(bp)
CoDEmanX
committed
bp[t].co = Vector(spline_old_coords[new_index][0])
CoDEmanX
committed
bp[t].handle_left.length = spline_old_coords[new_index][3]
bp[t].handle_right.length = spline_old_coords[new_index][4]
CoDEmanX
committed
bp[t].handle_left_type = "FREE"
bp[t].handle_right_type = "FREE"
CoDEmanX
committed
bp[t].handle_left.x = spline_old_coords[new_index][5][0]
bp[t].handle_left.y = spline_old_coords[new_index][5][1]
bp[t].handle_left.z = spline_old_coords[new_index][5][2]
CoDEmanX
committed
bp[t].handle_right.x = spline_old_coords[new_index][6][0]
bp[t].handle_right.y = spline_old_coords[new_index][6][1]
bp[t].handle_right.z = spline_old_coords[new_index][6][2]
CoDEmanX
committed
bp[t].handle_left_type = spline_old_coords[new_index][1]
bp[t].handle_right_type = spline_old_coords[new_index][2]
CoDEmanX
committed
# Invert the non-cyclic splines designated above
for i in range(len(splines_to_invert)):
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
self.main_curve.data.splines[splines_to_invert[i]].bezier_points[0].select_control_point = True
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
bpy.ops.curve.switch_direction()
CoDEmanX
committed
bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
CoDEmanX
committed
# Keep selected the first vert of each spline
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
for i in range(len(self.main_curve.data.splines)):
if not self.main_curve.data.splines[i].use_cyclic_u:
bp = self.main_curve.data.splines[i].bezier_points[0]
else:
bp = self.main_curve.data.splines[i].bezier_points[
len(self.main_curve.data.splines[i].bezier_points) - 1
]
CoDEmanX
committed
bp.select_control_point = True
bp.select_right_handle = True
bp.select_left_handle = True
CoDEmanX
committed
bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
CoDEmanX
committed
CoDEmanX
committed
self.main_curve = bpy.context.object
CoDEmanX
committed
# Check if all curves are Bezier, and detect which ones are cyclic
self.cyclic_splines = []
for i in range(len(self.main_curve.data.splines)):
if self.main_curve.data.splines[i].type != "BEZIER":
self.report({'WARNING'}, "All splines must be Bezier type")
CoDEmanX
committed
return {'CANCELLED'}
else:
if self.main_curve.data.splines[i].use_cyclic_u:
self.cyclic_splines.append(i)
CoDEmanX
committed
self.report({'INFO'}, "First points have been set")
CoDEmanX
committed
Campbell Barton
committed
return {'FINISHED'}
CoDEmanX
committed
# Add-ons Preferences Update Panel
# Define Panel classes for updating
panels = (
VIEW3D_PT_tools_SURFSK_mesh,
VIEW3D_PT_tools_SURFSK_curve
message = "Bsurfaces GPL Edition: Updating Panel locations has failed"
for panel in panels:
if "bl_rna" in panel.__dict__:
bpy.utils.unregister_class(panel)
for panel in panels:
panel.bl_category = context.preferences.addons[__name__].preferences.category
bpy.utils.register_class(panel)
except Exception as e:
print("\n[{}]\n{}\n\nError:\n{}".format(__name__, message, e))
def conver_gpencil_to_curve(self, context, pencil, type):
newCurve = bpy.data.curves.new('gpencil_curve', type='CURVE')
CurveObject = object_utils.object_data_add(context, newCurve)
if type == 'GPensil':
strokes = pencil.data.layers.active.active_frame.strokes
CurveObject.location = pencil.location
CurveObject.rotation_euler = pencil.rotation_euler
CurveObject.scale = pencil.scale
elif type == 'Annotation':
grease_pencil = bpy.data.grease_pencils[0]
strokes = grease_pencil.layers.active.active_frame.strokes
CurveObject.location = (0.0, 0.0, 0.0)
CurveObject.rotation_euler = (0.0, 0.0, 0.0)
CurveObject.scale = (1.0, 1.0, 1.0)
for i, stroke in enumerate(strokes):
stroke_points = strokes[i].points
data_list = [ (point.co.x, point.co.y, point.co.z)
for point in stroke_points ]
points_to_add = len(data_list)-1
flat_list = []
for point in data_list:
flat_list.extend(point)
spline = newCurve.splines.new(type='BEZIER')
spline.bezier_points.add(points_to_add)
spline.bezier_points.foreach_set("co", flat_list)
for point in spline.bezier_points:
point.handle_left_type="AUTO"
point.handle_right_type="AUTO"
return CurveObject
class BsurfPreferences(AddonPreferences):
# this must match the addon name, use '__package__'
# when defining this in a submodule of a python package.
bl_idname = __name__
CoDEmanX
committed
name="Tab Category",
description="Choose a name for the category of the panel",
def draw(self, context):
layout = self.layout
row = layout.row()
col = row.column()
col.label(text="Tab Category:")
col.prop(self, "category", text="")
CoDEmanX
committed
# Properties
class BsurfacesProps(PropertyGroup):
Spivak Vladimir (cwolf3d)
committed
SURFSK_guide: EnumProperty(
name="Guide:",
items=[
('Annotation', 'Annotation', 'Annotation'),
('GPencil', 'GPencil', 'GPencil'),
('Curve', 'Curve', 'Curve')
],
default="Annotation"
)
SURFSK_edges_U: IntProperty(
name="Cross",
description="Number of face-loops crossing the strokes",
default=5,
min=1,
max=200
)
SURFSK_edges_V: IntProperty(
name="Follow",
description="Number of face-loops following the strokes",
default=1,
min=1,
max=200
)
SURFSK_cyclic_cross: BoolProperty(
name="Cyclic Cross",
description="Make cyclic the face-loops crossing the strokes",
default=False
)
SURFSK_cyclic_follow: BoolProperty(
name="Cyclic Follow",
description="Make cyclic the face-loops following the strokes",
default=False
)
SURFSK_keep_strokes: BoolProperty(
name="Keep strokes",
description="Keeps the sketched strokes or curves after adding the surface",
default=False
)
SURFSK_automatic_join: BoolProperty(
name="Automatic join",
description="Join automatically vertices of either surfaces "
"generated by crosshatching, or from the borders of closed shapes",
default=True
)
SURFSK_loops_on_strokes: BoolProperty(
name="Loops on strokes",
description="Make the loops match the paths of the strokes",
default=True
)
name="Precision",
description="Precision level of the surface calculation",
default=2,
min=1,
max=100
)
SURFSK_object_with_retopology: PointerProperty(
name="Mesh of BSurface",
type=bpy.types.Object
)
SURFSK_object_with_strokes: PointerProperty(
name="GPensil or Curve object",
GPENCIL_OT_SURFSK_init,
GPENCIL_OT_SURFSK_add_modifiers,
GPENCIL_OT_SURFSK_edit_surface,
GPENCIL_OT_SURFSK_add_strokes,
GPENCIL_OT_SURFSK_add_annotation,
CURVE_OT_SURFSK_reorder_splines,
CURVE_OT_SURFSK_first_points,
BsurfPreferences,
BsurfacesProps
CoDEmanX
committed
def register():
for cls in classes:
bpy.utils.register_class(cls)
for panel in panels:
bpy.utils.register_class(panel)
bpy.types.Scene.bsurfaces = PointerProperty(type=BsurfacesProps)
update_panel(None, bpy.context)
CoDEmanX
committed
for panel in panels:
bpy.utils.unregister_class(panel)
for cls in classes:
bpy.utils.unregister_class(cls)
CoDEmanX
committed
if __name__ == "__main__":