I decided to build a new spline generator based on selected mesh vertices and I ran into a problem. The order of the selected verts are not in the order by which I selected them. Does anyone know what might be happening here?
import bpy
from functools import reduce
from operator import concat
from pdb import set_trace
def make_curve( ob ):
# toggling to object mode ensures vert selection is up to date
if ob.mode == 'EDIT':
bpy.ops.object.mode_set( mode="OBJECT" )
bpy.ops.object.mode_set( mode = "EDIT" )
# filters down to only x,y,z, 0 tuples and then flattens the list.
vert_list = [ ( vert.co.x, vert.co.y, vert.co.z, 0 )
for vert in ob.data.vertices if vert.select ]
flat_list = reduce( concat, vert_list )
crv = bpy.data.curves.new( "curve", type="CURVE" )
crv_ob = bpy.data.objects.new( "curve_ob", crv )
spline = crv.splines.new( type="POLY" )
spline.points.add( len( vert_list ) - 1 )
spline.points.foreach_set( "co", flat_list )
return crv_ob
class MeshVertCurveOp(bpy.types.Operator):
'''Makes a polyline curve based on selected verts in an existing mesh'''
bl_idname = "curve.curve_from_mesh_verts"
bl_label = "Curve From Mesh Verts"
bl_options = { 'REGISTER', 'UNDO' }
@classmethod
def poll(cls, context):
return context.active_object != None
def execute(self, context):
scn = context.scene
ob = context.active_object
crv = make_curve( ob )
scn.objects.link( crv )
return {'FINISHED'}
def register():
bpy.utils.register_class(MeshVertCurveOp)
def unregister():
bpy.utils.unregister_class(MeshVertCurveOp)
if __name__ == "__main__":
register()