Vertex Order?

I made a script that analyse a poly line and measures the distances between each vertex and save them in a list.
Then, takes a copy of that polyline that i converted into a curve, add a Follow_Path constraint and put empties for each vertex of that poly line.

In my tests it seemed to work. I made a Curve with at least 5 vertices, converted into a poly line and after running the script, each path empty was pretty much at the position of the analysed poly line vertices.

The problem is, when i convert a poly loop to a poly line from a character mesh (eyes or mouth for example) the script works but the empties dont match up with the vertex postion of that poly line anymore. I wonder if this has something to do with the vertex order.

Here is the script, probably awkwardly done since i’m a bloody python rook, no doubt. So, in order for it to work you need a poly line (named U.Lip) and a copy converted into a Curve (named U.Lip.Crv).

This script is based on an idea by Marco Giordano for Maya and can be found on Vimeo and probably explains it better what i am trying to do here.
https://vimeo.com/66583205
It would be awesome if somone could find the time to look into it and tell me why it wont work as expected.


import bpy

import os

os.system("cls")
bpy.context.scene.objects.active = bpy.data.objects['U.Lip']

def findlengthofcurve(obj):
    mesh = obj.data
    edges = mesh.edges.values()
    vertices = mesh.vertices.values()
    
    TotalSegLength = 0      #Total Length
    NumSeg = 0              #Total Number of Segments
    SegList =[]             #List of Segment Lengths
    
    for i in edges:
        print (vertices)
        vtx1, vtx2 = i.vertices
        segLength = (vertices[vtx1].co-vertices[vtx2].co).length
        
        TotalSegLength += segLength       
        SegList.append(TotalSegLength)   
        
        NumSeg+=1       
    return NumSeg,TotalSegLength,SegList    
  


list = findlengthofcurve(bpy.context.active_object)

print (list[2])         #List of Segment Lengths
print (list[1])         #Total Length
print (list[0])         #Total Number of Segments

# This part places an empty at the start of the path
bpy.context.scene.objects.active = bpy.data.objects['U.Lip.Crv']
name = 'Empty.00'
bpy.ops.object.empty_add(type='PLAIN_AXES', location=(0,0,0))
bpy.context.active_object.name = name
bpy.ops.object.constraint_add(type='FOLLOW_PATH')
bpy.data.objects['Empty.00'].constraints["Follow Path"].target = bpy.data.objects["U.Lip.Crv"]
bpy.data.objects['Empty.00'].constraints["Follow Path"].use_fixed_location = True
bpy.data.objects['Empty.00'].constraints["Follow Path"].offset_factor = 0.0
bpy.context.active_object.empty_draw_size = bpy.context.active_object.empty_draw_size / 50

#This part goes through the List of Segment Length and calculates the empty positions for the path
for x in range(list[0]):
    name = 'Empty.0{0}'.format(x+1)
    bpy.ops.object.empty_add(type='PLAIN_AXES', location=(0,0,0))
    bpy.context.active_object.name = name
    bpy.context.active_object.empty_draw_size = bpy.context.active_object.empty_draw_size / 50
    bpy.ops.object.constraint_add(type='FOLLOW_PATH')
    
    bpy.data.objects[name].constraints["Follow Path"].target = bpy.data.objects["U.Lip.Crv"]
    bpy.data.objects[name].constraints["Follow Path"].use_fixed_location = True

    ppos = ((list[2][x]*100)/(list[1])/100) #calculates the percentage position for the path
    print (ppos)
       
    bpy.data.objects[name].constraints["Follow Path"].offset_factor = ppos
    bpy.data.objects[name].constraints["Follow Path"].use_curve_follow = False