Help understanding what's happenning in this code

I have the following script (mostly copied from a sample I can’t remember where I took it from)


import bpy
import bmesh



############################################
def print_vert_details(selected_verts):
    num_verts = len(selected_verts)
    print("number of verts: {}".format(num_verts))
    print("vert indices: {}".format([id.index for id in selected_verts]))


        
############################################
def getSelectedVertices (object_reference):
    bm = bmesh.from_edit_mesh(object_reference.data)
    selected_verts = [vert for vert in bm.verts if vert.select]
    print_vert_details(selected_verts)
    return selected_verts



object_reference = bpy.context.active_object

print ("run1")
selected_verts=getSelectedVertices (object_reference)

print ("run2")
print_vert_details (selected_verts)

print ("final")



Basically I call getSelectedVertices and print the results inside and outside the function.

The output of this code is this:



run1
number of verts: 96
vert indices: [57, 58, 59, 60, ....some numbers more..... ]
run2
number of verts: 96
Traceback (most recent call last):
  File "o:\projects\voxel\assets\models\mayaProject	estChar2.blend\voxelSkinFix.py", line 29, in <module>
  File "o:\projects\voxel\assets\models\mayaProject	estChar2.blend\voxelSkinFix.py", line 10, in print_vert_details
  File "o:\projects\voxel\assets\models\mayaProject	estChar2.blend\voxelSkinFix.py", line 10, in <listcomp>
ReferenceError: BMesh data of type BMVert has been removed
Error: Python script fail, look in the console for now...


I fail to understand why the message “BMesh data of type BMVert has been removed” happens. It looks like somehow the list is kept intact when returning the function, but the contents of the list (the vertexs) are deleted somehow.
Printing the vertexs inside the list give me things like

<BMVert dead at 0x000001F9317B56E8>

Any clues?
Thanks.

never mind, I found out that I’m better using vertex indexes than references to objects in blender/python, so I’ve rewritten my code to handle them.

1 Like