I want to convert selected vertices/edges into curves. I can convert them using bpy.ops functions. But is there any alternative using lower level api preferable using bmesh. I can get all spline points to proper position but not in proper order so I get this mesh(middle) not the expected curve(left).
Here is the code.
import bpy
import bmesh
def create_curve(coords_list):
# make a new curve
crv = bpy.data.curves.new('crv', 'CURVE')
crv.dimensions = '3D'
# make a new spline in that curve
spline = crv.splines.new(type='POLY')
# a spline point for each point
spline.points.add(len(coords_list)-1) # theres already one point by default
# assign the point coordinates to the spline points
for p, new_co in zip(spline.points, coords_list):
x,y,z = new_co
p.co = (x,y,z,1)
# make a new object with the curve
obj = bpy.data.objects.new('object_name', crv)
bpy.context.scene.collection.objects.link(obj)
def main(context):
obj = context.active_object
if obj.mode == 'EDIT':
# this works only in edit mode,
bm = bmesh.from_edit_mesh(obj.data)
selected_verts = list(filter(lambda v: v.select, bm.verts))
pos = list(map(lambda v: v.co,selected_verts))
create_curve(pos)
class SimpleOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"
def execute(self, context):
main(context)
return {'FINISHED'}
def register():
bpy.utils.register_class(SimpleOperator)
def unregister():
bpy.utils.unregister_class(SimpleOperator)
if __name__ == "__main__":
register()
There is also this stack overflow thread. Solution in that thread works for closed loop. I tried to modify it to work for non-closed loop/selected edges but no success. Modified script works in some cases in other gives weird results.