Skip to content
Snippets Groups Projects
mesh_bsurfaces.py 195 KiB
Newer Older
  • Learn to ignore specific revisions
  •             self.report({'WARNING'}, "Draw grease pencil strokes to connect splines")
    
            return {"FINISHED"}
    
    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'}
    
        def execute(self, context):
    
            splines_to_invert = []
    
            # 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
    
                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)
    
            # Reorder points of cyclic splines, and set all handles to "Automatic"
    
            # Check first selected point
    
            cyclic_splines_new_first_pt = {}
            for i in self.cyclic_splines:
                sp = self.main_curve.data.splines[i]
    
                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
    
            for spline_idx in cyclic_splines_new_first_pt:
                sp = self.main_curve.data.splines[spline_idx]
    
                spline_old_coords = []
                for bp_old in sp.bezier_points:
                    coords = (bp_old.co[0], bp_old.co[1], bp_old.co[2])
    
                    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]
                            )
    
                for t in range(len(sp.bezier_points)):
                    bp = sp.bezier_points
    
                    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)
    
                    bp[t].co = Vector(spline_old_coords[new_index][0])
    
                    bp[t].handle_left.length = spline_old_coords[new_index][3]
                    bp[t].handle_right.length = spline_old_coords[new_index][4]
    
                    bp[t].handle_left_type = "FREE"
                    bp[t].handle_right_type = "FREE"
    
                    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]
    
                    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]
    
                    bp[t].handle_left_type = spline_old_coords[new_index][1]
                    bp[t].handle_right_type = spline_old_coords[new_index][2]
    
            # 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')
    
                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')
    
                bpy.ops.curve.switch_direction()
    
            bpy.ops.curve.select_all('INVOKE_REGION_WIN', action='DESELECT')
    
            # 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
                                                            ]
    
                bp.select_control_point = True
                bp.select_right_handle = True
                bp.select_left_handle = True
    
            bpy.ops.object.editmode_toggle('INVOKE_REGION_WIN')
    
            return {'FINISHED'}
    
        def invoke(self, context, event):
    
            self.main_curve = bpy.context.object
    
            # 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")
    
                    return {'CANCELLED'}
                else:
                    if self.main_curve.data.splines[i].use_cyclic_u:
                        self.cyclic_splines.append(i)
    
            self.execute(context)
    
            self.report({'INFO'}, "First points have been set")
    
    
    # Add-ons Preferences Update Panel
    
    # Define Panel classes for updating
    panels = (
            VIEW3D_PT_tools_SURFSK_mesh,
    
            VIEW3D_PT_tools_SURFSK_curve
    
    def update_panel(self, context):
    
        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')
    
        newCurve.dimensions = '3D'
    
        CurveObject = object_utils.object_data_add(context, newCurve)
    
            strokes = pencil.data.layers.active.active_frame.strokes
            CurveObject.location = pencil.location
            CurveObject.rotation_euler = pencil.rotation_euler
            CurveObject.scale = pencil.scale
    
            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__
    
        category: StringProperty(
    
                name="Tab Category",
                description="Choose a name for the category of the panel",
    
                update=update_panel
                )
    
    
        def draw(self, context):
            layout = self.layout
    
            row = layout.row()
            col = row.column()
            col.label(text="Tab Category:")
            col.prop(self, "category", text="")
    
    # Properties
    class BsurfacesProps(PropertyGroup):
    
        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
                    )
    
        SURFSK_precision: IntProperty(
    
                    name="Precision",
                    description="Precision level of the surface calculation",
                    default=2,
                    min=1,
                    max=100
                    )
    
        SURFSK_object_with_retopology: PointerProperty(
    
                    type=bpy.types.Object
                    )
        SURFSK_object_with_strokes: PointerProperty(
    
                    type=bpy.types.Object
                    )
    
        GPENCIL_OT_SURFSK_add_modifiers,
    
        GPENCIL_OT_SURFSK_add_surface,
    
        GPENCIL_OT_SURFSK_add_strokes,
    
        GPENCIL_OT_SURFSK_edit_strokes,
    
        GPENCIL_OT_SURFSK_add_annotation,
    
        CURVE_OT_SURFSK_reorder_splines,
        CURVE_OT_SURFSK_first_points,
        BsurfPreferences,
    
    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)
    
    def unregister():
    
        for panel in panels:
            bpy.utils.unregister_class(panel)
        
    
        for cls in classes:
            bpy.utils.unregister_class(cls)
    
        del bpy.types.Scene.bsurfaces
    
    
    if __name__ == "__main__":
    
    Guillermo S. Romero's avatar
    Guillermo S. Romero committed
        register()