The length of edges (in a loop)

Hallo there…i’m a newbie in Python. I need help. :slight_smile:
I made a new method that will be return a total length of edges in a loop. the method looks like this :

 

def getCurrentLengthOf(name): 
obj_mesh = bpy.data.objects['nameOfObject'] 
# Store the mesh 
mesh = obj_mesh.data 
#BMESH import bmesh 
bm = bmesh.new() 
bm.from_mesh(mesh) 
#set active the object 
bpy.context.scene.objects.active = obj_mesh 
#set to edit mode 
opsobj.mode_set(mode='EDIT') 
#set mesh mode into edge mode (vertex, edge, face) 
bpy.context.tool_settings.mesh_select_mode = (False, True, False) 
#set to the object mode first 
opsobj.mode_set(mode='OBJECT') 
index = 0 
if name==some1: 
index = 24218 
elif name==some2: 
index = 20856  
#get an edge by index 
bpy.context.active_object.data.edges[index].select=True 
#back to edit mode 
opsobj.editmode_toggle() 
#make a loop from selected edge 
bpy.ops.mesh.loop_multi_select() 

bm.select_flush(True)

#get the sum 
total = sum(edge.calc_length() for edge in bm.edges if edge.select) 
return total 

 

the mistake is in the result.
in the first run of script, i just get the length of a single edge that i selected before (by index)…and then i run again the script, and finally i get the sum of the lengths which is that i hope as a result. Why did this happen?
assume, i have to get the result as ‘total’ 28. a single edge that selected by index is 1.032. i got the result like this :
first run : 28
2nd run : 1.032
3rd run and so on : 28…
thanks before for helping me. :’)

I would try going back into Object mode before counting the edges (After you have used ‘bpy.ops.mesh.loop_multi_select()’ to select the edge loop).

I’m not 100% on what bm.select_flush(…) does and I have never done anything with bmesh, but 2.62 requires leaving edit mode for the Python API to see selections made in Edit mode correctly.