Kaynağa Gözat

new operator to convert Bezier curve to NURBs

Joseph Brandenburg 5 ay önce
ebeveyn
işleme
7ad38c8622
2 değiştirilmiş dosya ile 45 ekleme ve 0 silme
  1. 24 0
      ops_nodegroup.py
  2. 21 0
      utilities.py

+ 24 - 0
ops_nodegroup.py

@@ -812,6 +812,28 @@ class B4_4_0_Workaround_NodeTree_Interface_Update(Operator):
         return {"FINISHED"}
 
 
+class ConvertBezierCurveToNURBS(Operator):
+    """Converts all bezier splines of curve to NURBS."""
+    bl_idname = "mantis.convert_bezcrv_to_nurbs"
+    bl_label = "Convert Bezier Curve to NURBS"
+    bl_options = {'REGISTER', 'UNDO'}
+
+    @classmethod
+    def poll(cls, context):
+        return (context.active_object is not None) and (context.active_object.type=='CURVE')
+
+    def execute(self, context):
+        from .utilities import nurbs_copy_bez_spline
+        curve = context.active_object
+        bez=[]
+        for spl in curve.data.splines:
+            if spl.type=='BEZIER':
+                bez.append(spl)
+        for bez_spline in bez:
+            new_spline=nurbs_copy_bez_spline(curve, bez_spline)
+            
+            curve.data.splines.remove(bez_spline)
+        return {"FINISHED"}
 
 
 # this has to be down here for some reason. what a pain
@@ -840,6 +862,8 @@ classes = [
         # Armature Link Node
         LinkArmatureAddTargetInput,
         LinkArmatureRemoveTargetInput,
+        # rigging utilities
+        ConvertBezierCurveToNURBS,
         ]
 if (bpy.app.version >= (4, 4, 0)):
     classes.append(B4_4_0_Workaround_NodeTree_Interface_Update)

+ 21 - 0
utilities.py

@@ -745,6 +745,27 @@ def get_extracted_spline_object(proto_curve, spline_index, mContext):
         mContext.b_objects[curve.name] = curve
     return curve
 
+def nurbs_copy_bez_spline(curve, bez_spline, do_setup=True):
+    other_spline= curve.data.splines.new('NURBS')
+    other_spline.use_endpoint_u=True
+    other_spline.use_bezier_u=True
+    bez_pts = bez_spline.bezier_points
+    bez_data=[]
+    for i, bez_pt in enumerate(bez_pts):
+        if i > 0:
+            bez_data.append(bez_pt.handle_left.copy())
+        bez_data.append(bez_pt.co.copy())
+        if i != len(bez_pts)-1:
+            bez_data.append(bez_pt.handle_right.copy())
+    print(bez_data)
+    other_spline.points.add(len(bez_data)-1)
+    for i, pt in enumerate(bez_data):
+        other_spline.points[i].co=(*pt,1.0) # add the W value here
+    if do_setup: # do the stuff that makes it behave the same as a bez spline
+        other_spline.use_endpoint_u = True; other_spline.use_bezier_u = True
+        other_spline.order_u=4 # set to 1 for poly
+    return other_spline
+
 def RibbonMeshEdgeLengths(m, ribbon):
     tE = ribbon[0]; bE = ribbon[1]; c = ribbon[2]
     lengths = []