# Bmesh: running bmesh ops after deleting geometry often results in an error

I've uploaded a blend file that demonstrates the problem, please have a look and test it for yourself:

If you delete geometry from a bmesh, but immediately then run another some more bmesh operations with reference to your resultant geometry, you will often encounter this error:

ValueError: geom: found the same (BMVert/BMEdge/BMFace) used multiple times

The only way I've found to fix this is to run the following after a deletion:

bmesh.update_edit_mesh(mesh, True)
bpy.ops.object.editmode_toggle()
bpy.ops.object.editmode_toggle()
bm = bmesh.from_edit_mesh(mesh)


But that seems a bit hacky. Is there not a more appropriate way of dealing with this problem?

edit: in the example blend file uncomment 'bmesh.update_edit_mesh(mesh, True)' to stop the entire application from crashing.

I'm inclinded to think this is a bug and I have reported it.

The context paramater you pass to bmesh.ops.delete would appear to correspond to deleting faces (DEL_FACES), this isn't documented clearly but appears to match with the enum defined in bmesh_operator.h. Ideally this enum should be defined in python for use with this operator.

By passing a geom list as well as a context, there are times (not every time) when an item can be deleted multiple times. It may be expecting to use one of these paramaters - not both.

You can verify this by deselecting all geometry before running the script. Removing the context paramater or changing it to 1,2 or 4 also works. It would also appear that if the selected items matches the geom paramater there is no problem, you can test this by deselecting the faces in your loop and selecting the faces as you add them to the list.

So this also works -

delete_first = []
for f in bm.faces:
f.select = False
if f.calc_center_bounds().y < 0:
delete_first.append(f)
f.select = True

bmesh.ops.delete(bm, geom=delete_first, context=5)


Edit: From the response to the bug report it seems that passing duplicate geom to bisect_plane is the cause of the duplicate error message, though I seem to have trouble creating another scenario that causes a crash. The bmesh data created by the bisect isn't guaranteed to be appended to the end of the geom list so copying the geom based on the index relative to previous list lengths is creating duplicates. You will need to look at what you want to do after what you have so far to see how to work out another way to update the geom list.

• I've just seen that and commented on it myself. As I suspected, Germano confirmed: Remember that the new elements created are not necessarily at the end of the array., and that explains the bug. But it would seem the only resolution currently is the editmode_toggle trick, which finalizes geometry and leaves no hidden gaps in the bmelemseqs, so I can trust that all new geometry is appended. I think it would be a good idea to make more mention of this in the documentation's Gotcha section, and possibly have a new op that ensures the expected behaviour, so new elements are appended. – hedgehog90 Mar 12 '17 at 12:25