Controlling Vertex Order

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()


there is no stack recording which vertices you did select.
The vertices are only set to state selected.
You have to record on your own the process of selecting the vertices.

There are tools to do a re-ordering of the veritces (according to their 3d-location),
you may check those, whether there are any ideas to solve the ordering you need.

don’t think there is any way to have order in a complex mesh
so when you select some vertices on a mesh there is no guarantie to get order for the selected vertices

so just make a copy to a new ly created object line of your selected vertices then apply the mesh to curve and the vertices will be in order !

hope it helps

happy 2.5

There is an order to this somehow. You can select the same 5 vertices in several different orders and it’ll give you the same spline shape every time. Pretty interesting.

Thanks for the response test-dr. I’ll take a look at these options. In the meantime, it’s back to the drawing board.

Cheers!