I am using Blender 2.78. I have found 2 vertices that I want to automate measuring the shortest path between them on the surface. I have written the following function that I give the object that I found the vertices on and the vertices themselves, it looks like this.
def surf_len(ob, path):
# first select the object in edit mode
# and select the vertex path to measure
ob.select = True
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
for vert in path:
vert.select=True
bpy.context.object.update_from_editmode()
# select the shortest path
r = bpy.ops.mesh.shortest_path_select()
bpy.ops.object.mode_set(mode='OBJECT')
ob.select = False
if 'CANCELLED' in r:
raise ValueError('Cannot build shortest path: {}'.format(r))
However when I run this code I always get back CANCELLED and get the exception thrown even though I think I have selected valid vertices.
I also read somewhere that I might need to use the bmesh module for this so I did modify the code like this
def surf_len(ob, path):
# first select the object in edit mode
# and select the vertex path to measure
ob.select = True
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
me = ob.data
bm = bmesh.from_edit_mesh(me)
bm.verts.ensure_lookup_table()
for vert in path:
bm.verts[vert.index].select_set(True)
# select the shortest path
r = bpy.ops.mesh.shortest_path_select()
bpy.ops.object.mode_set(mode='OBJECT')
ob.select = False
if 'CANCELLED' in r:
raise ValueError('Cannot build shortest path: {}'.format(r))
But this always raises ValueError: The mesh must be in editmode
which is weird because when I run this from the console within blender, rather than as a start up script, I can see it working
I would really appreciate any help.
Cheers