3
$\begingroup$

is it possible to retrieve the selection history(the specific sequence) of vertices selected using "shortest path"?

Using bmesh, I can get the selection history by doing:

vertlist = [elem.index for elem in bm.select_history if isinstance(elem, bmesh.types.BMVert)]

But this unfortunately only works for verts, that have been manually selected(by hand and via "next active").

If anybody has any insight into this, I'd appreciate it.

$\endgroup$
1
  • 1
    $\begingroup$ The answer is correct? if is, please check as correct. thanks $\endgroup$
    – yhoyo
    Commented Jan 11, 2017 at 3:21

2 Answers 2

3
$\begingroup$

this script will help you

import bpy, bmesh

print("*"*50)
oa = bpy.context.active_object
obj = bpy.context.object

bpy.ops.mesh.shortest_path_select() #add for request

me = obj.data
bm = bmesh.from_edit_mesh(me)


v1,v2 = [elem for elem in bm.select_history if isinstance(elem, bmesh.types.BMVert)]

list = [v1]
verts = len([v.index for v in bm.verts if v.select])

cont=0

while cont< verts:
    v=list[cont]
    edges = v.link_edges

    for e in edges:
        if e.select:
            vn = e.other_vert(v)
            if vn not in list:
                list.append(vn)

    cont+=1

print(list)

for v in list:
    print(v.index)
bmesh.update_edit_mesh(me, True) 

selection history from shortest path

$\endgroup$
2
  • $\begingroup$ Have you forgotten to put in the shortest path operator call before you populate verts? $\endgroup$
    – batFINGER
    Commented Dec 28, 2016 at 18:50
  • $\begingroup$ @batFINGER the answer is ok? if is, please check as correct please. best $\endgroup$
    – yhoyo
    Commented Jan 11, 2017 at 3:20
1
$\begingroup$

the script is modified based on @yhoyo.In edit mode, given the start vertex index and the end vertex index, index_order store the shortest path

import bpy
import bmesh

ob = bpy.context.object
me = ob.data
bm = bmesh.from_edit_mesh(me)
bpy.ops.mesh.select_all(action='DESELECT')
start_index = 5017 # start points 
end_index = 2045 # end points
bm.verts[start_index].select = True  
bm.verts[end_index].select = True  
bpy.ops.mesh.shortest_path_select()
verts_list = [bm.verts[start_index]]
verts = [v for v in bm.verts if v.select]
cont=0
while cont< len(verts):
    v=verts_list[cont]
    edges = v.link_edges

    for e in edges:
        if e.select:
            vn = e.other_vert(v)
            if vn not in verts_list :
                verts_list.append(vn)
    cont+=1
index_order = [v.index for v in verts_list]
for v in verts_list:
    print(v.index)
$\endgroup$
1
  • 1
    $\begingroup$ Hi and Weclome! Can you please explain (at least a bit) what you've changed? $\endgroup$
    – brockmann
    Commented Apr 9, 2020 at 7:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .