2
$\begingroup$

How can i retrieve the list of selected verts/edges/faces after selecting the shortest_path_select() ?

I can see in the viewport that the correct faces are being selected, but from code i cannot see them selected, neither from the bmesh neither from the mesh itself.

enter image description here

As you can see in the image, after the call to bpy.ops.mesh.shortest_path_select() i see the faces selected in the viewport, but from code only the face 655 of the mesh results selected.

After calling the function i DO call bmesh.update_edit_mesh(me) but still it seems not to work as wanted.

Here a snippet of code driving this example

                bpy.ops.mesh.shortest_path_select()

                bmesh.update_edit_mesh(me)

                self.report({"INFO"}, f"me 655: {me.polygons[655].select}")
                self.report({"INFO"}, f"me 380: {me.polygons[380].select}")
                self.report({"INFO"}, f"me 372: {me.polygons[372].select}")
                self.report({"INFO"}, f"me 947: {me.polygons[947].select}")
                self.report({"INFO"}, f"me 2200: {me.polygons[2200].select}")
                self.report({"INFO"}, f"")

                self.report({"INFO"}, f"bm 655: {bm.faces[655].select}")
                self.report({"INFO"}, f"bm 380: {bm.faces[380].select}")
                self.report({"INFO"}, f"bm 372: {bm.faces[372].select}")
                self.report({"INFO"}, f"bm 947: {bm.faces[947].select}")
                self.report({"INFO"}, f"bm 2200: {bm.faces[2200].select}")
                self.report({"INFO"}, f"")

I don't know how to get the list of selected faces after the shortest path calculation. Any hep?

$\endgroup$
2
$\begingroup$

Unclear what the issue is.

Will mention that when a mesh is put into edit mode, the edit mode mesh is a "snapshot" of the data when mode toggled. It is not written back to the mesh datablock until mode is toggled back. (Or other methods are called to implicitly save edit mode changes)

>>> C.object.update_from_editmode(
update_from_editmode()
Object.update_from_editmode()
Load the objects edit-mode data into the object data

Hence looking at bpy.data.meshes["Foo"] data while "Foo" is being edited will not show updates.

Little test script.

import bpy
import bmesh

ob = bpy.context.edit_object
me = ob.data
bm = bmesh.from_edit_mesh(me)

def print_selection(element_type):
    selection = [e.index for e in getattr(bm, element_type)
                 if e.select]
    print(f"{len(selection)} {element_type} selected")
    if selection:
        print(f"    {selection}")



print("Before")
for sel in ("verts", "edges", "faces"):
    print_selection(sel)
bpy.ops.mesh.shortest_path_select()
#bmesh.update_edit_mesh(me) # Not required.
print("After")
for sel in ("verts", "edges", "faces"):
    print_selection(sel)

Run on default grid.

enter image description here

Output to console

Before
8 verts selected
    [11, 12, 21, 22, 77, 78, 87, 88]
8 edges selected
    [6, 29, 31, 32, 139, 155, 157, 158]
2 faces selected
    [10, 70]
After
28 verts selected
    [11, 12, 21, 22, 23, 31, 32, 33, 42, 43, 52, 53, 54, 55, 62, 63, 64, 65, 74, 75, 76, 77, 78, 84, 85, 86, 87, 88]
40 edges selected
    [6, 29, 31, 32, 34, 48, 50, 51, 52, 53, 69, 71, 72, 88, 90, 91, 93, 95, 107, 109, 110, 111, 112, 113, 114, 130, 132, 133, 135, 137, 139, 149, 151, 152, 153, 154, 155, 156, 157, 158]
13 faces selected
    [10, 19, 20, 29, 38, 47, 48, 49, 58, 67, 68, 69, 70]

Again with two initial verts selected

enter image description here

Before
2 verts selected
    [11, 88]
0 edges selected
0 faces selected
After
15 verts selected
    [11, 12, 13, 14, 24, 34, 44, 45, 46, 56, 66, 76, 86, 87, 88]
14 edges selected
    [6, 9, 12, 35, 54, 73, 76, 78, 96, 115, 134, 153, 156, 158]
0 faces selected

And again, but modifying to also show the mesh selection, deselect all verts, toggle into object mode and back to edit mode, select two verts and run script

def print_selection(element_type):
    lut = {"verts" : me.vertices,
           "edges" : me.edges,
           "faces" : me.polygons,
           }
    selection = [e.index for e in getattr(bm, element_type)
                 if e.select]
    print(f"{len(selection)} {element_type} selected")
    if selection:
        print(f"    {selection}")
        
        print([lut[element_type][i].select for i in selection])

Result.

Before
2 verts selected
    [11, 88]
[False, False]
0 edges selected
0 faces selected
After
15 verts selected
    [11, 12, 13, 14, 24, 34, 44, 45, 46, 56, 66, 76, 86, 87, 88]
[False, False, False, False, False, False, False, False, False, False, False, False, False, False, False]
14 edges selected
    [6, 9, 12, 35, 54, 73, 76, 78, 96, 115, 134, 153, 156, 158]
[False, False, False, False, False, False, False, False, False, False, False, False, False, False]
0 faces selected

What is the bmesh equivalent to bpy.ops.mesh.shortest_path_select()?

$\endgroup$
1
  • $\begingroup$ Thank you very much for the explanation. My key problem was the way i was getting my bmesh. I setted the active object into edit mode and then created a new bmesh from the contex.active_object In your code you get reference with bm = bmesh.from_edit_mesh(me) which actually solves my problem entirely. Thank you! $\endgroup$ – Daniele S DShot92 Feb 25 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.