Skip to content
Snippets Groups Projects
operators.py 43.2 KiB
Newer Older
  • Learn to ignore specific revisions
  •                            
                                point.co = spline_points[key][j][0]
                            point = spline.points[-1]
                            point.co = spline_points[key][i][0]
                            num=i
       
            return {'FINISHED'}
    
            
    class SeparateOutline(bpy.types.Operator):
        bl_idname = "curvetools.sep_outline"
        bl_label = "Separate Outline"
        bl_options = {'REGISTER', 'UNDO'}
        bl_description = "Makes 'Outline' separate mesh"
    
        @classmethod
        def poll(cls, context):
            return util.Selected1OrMoreCurves()
    
        def execute(self, context):
            bpy.ops.object.mode_set(mode = 'EDIT')
            bpy.ops.curve.separate()
    
            return {'FINISHED'}
    
            
    class CurveBoolean(bpy.types.Operator):
        bl_idname = "curvetools.bezier_curve_boolean"
        bl_description = "Curve Boolean"
        bl_label = "Curve Boolean"
        bl_options = {'REGISTER', 'UNDO'}
    
        operation: bpy.props.EnumProperty(name='Type', items=[
            ('UNION', 'Union', 'Boolean OR', 0),
            ('INTERSECTION', 'Intersection', 'Boolean AND', 1),
            ('DIFFERENCE AB', 'Difference AB', 'Active minus Selected', 2),
            ('DIFFERENCE BA', 'Difference BA', 'Selected minus Active', 3),
        ])
    
        @classmethod
        def poll(cls, context):
            return util.Selected1OrMoreCurves()
    
        def execute(self, context):
            current_mode = bpy.context.object.mode
            
            if bpy.ops.object.mode_set.poll():
                bpy.ops.object.mode_set(mode = 'OBJECT')
            
            selected_Curves = util.GetSelectedCurves()
            len_selected_curves = len(selected_Curves)
            
            bpy.ops.object.select_all(action='DESELECT')
            
            if len_selected_curves < 2:
                return {'FINISHED'}
            
            spline1 = selected_Curves[0].data.splines[0]
            matrix_world1 = selected_Curves[0].matrix_world
            
            len_spline1 = len(spline1.bezier_points)
                    
            dataCurve = bpy.data.curves.new(self.operation, type='CURVE')
            dataCurve.dimensions = '2D'
            newSpline1 = dataCurve.splines.new(type='BEZIER')
            newSpline1.use_cyclic_u = True
            newSpline1.bezier_points.add(len_spline1 - 1)
            for n in range(0, len_spline1):
                newSpline1.bezier_points[n].co = matrix_world1 @ spline1.bezier_points[n].co
                newSpline1.bezier_points[n].handle_left_type = spline1.bezier_points[n].handle_left_type
                newSpline1.bezier_points[n].handle_left = matrix_world1 @ spline1.bezier_points[n].handle_left
                newSpline1.bezier_points[n].handle_right_type = spline1.bezier_points[n].handle_right_type
                newSpline1.bezier_points[n].handle_right = matrix_world1 @ spline1.bezier_points[n].handle_right
                
            Curve = object_utils.object_data_add(context, dataCurve)
            bpy.context.view_layer.objects.active = Curve
            Curve.select_set(True)
    
    
            for iCurve in range(0, len_selected_curves):
                matrix_world = selected_Curves[iCurve].matrix_world
                len_splines = len(selected_Curves[iCurve].data.splines)
                first = 0
                if iCurve == 0:
                    first = 1
                for ispline in range(first, len_splines):
                    spline = selected_Curves[iCurve].data.splines[ispline]
                    len_spline = len(spline.bezier_points)
                    newSpline = dataCurve.splines.new(type='BEZIER')
                    newSpline.use_cyclic_u = True
                    newSpline.bezier_points.add(len_spline - 1)
                    for n in range(0, len_spline):
                        newSpline.bezier_points[n].co = matrix_world @ spline.bezier_points[n].co
                        newSpline.bezier_points[n].handle_left_type = spline.bezier_points[n].handle_left_type
                        newSpline.bezier_points[n].handle_left = matrix_world @ spline.bezier_points[n].handle_left
                        newSpline.bezier_points[n].handle_right_type = spline.bezier_points[n].handle_right_type
                        newSpline.bezier_points[n].handle_right = matrix_world @ spline.bezier_points[n].handle_right
                    
                    bpy.ops.object.mode_set(mode = 'EDIT')
                    bpy.ops.curve.select_all(action='SELECT')
                    splines = internal.getSelectedSplines(True, True)
    
                    if len(splines) < 2:
                       continue
    
                    splineA = splines[0]
                    splineB = splines[1]
                    operation = self.operation
                    dataCurve.splines.active = newSpline1
                    if self.operation == 'DIFFERENCE AB':
                        operation = 'DIFFERENCE'
                    if self.operation == 'DIFFERENCE BA':
                        dataCurve.splines.active = newSpline
                        operation = 'DIFFERENCE'
                        splineA = splines[1]
                        splineB = splines[0]
                    
                    if not internal.bezierBooleanGeometry(splineA, splineB, operation):
                        self.report({'WARNING'}, 'Invalid selection.')
                        return {'CANCELLED'}
                        
            bpy.ops.object.mode_set (mode = current_mode)
            
            return {'FINISHED'}
    
    
    def register():
        for cls in classes:
            bpy.utils.register_class(operators)
    
    def unregister():
        for cls in classes:
            bpy.utils.unregister_class(operators)
    
    if __name__ == "__main__":
        register()
    
    operators = [
        OperatorCurveInfo,
        OperatorCurveLength,
        OperatorSplinesInfo,
        OperatorSegmentsInfo,
        OperatorOriginToSpline0Start,
        OperatorIntersectCurves,
        OperatorLoftCurves,
        OperatorSweepCurves,
        OperatorBirail,
        OperatorSplinesSetResolution,
        OperatorSplinesRemoveZeroSegment,
        OperatorSplinesRemoveShort,
        OperatorSplinesJoinNeighbouring,
        ConvertSelectedFacesToBezier,
        ConvertBezierToSurface,
        BezierPointsFillet,
        BezierDivide,
        CurveScaleReset,