When working with scene objects in Blender using the Python API, we often need to manipulate both the base object structure (Mesh) and the more advanced version (BMesh). The method for deselecting vertices, edges, and polygons differs slightly between the two structures, but we can create a simple, universal deselection function that correctly handles both cases.
For this example, let’s assume we have two objects in the scene. We will deselect the geometry of both objects: the first via its base mesh, and the second via BMesh.
Pointers to both objects:
|
1 2 |
obj = bpy.context.scene.objects['Suzanne_obj'].data obj_bm = bpy.context.scene.objects['Suzanne_bm'].data |
Switch to the object mode so we don’t have to manually manage mesh structure updates while working.
|
1 |
bpy.ops.object.mode_set(mode='OBJECT') |
Define a universal geometry deselection function:
|
1 |
def deselect_all(obj): |
We will pass a pointer to the object whose geometry needs to be deselected in the function parameter.
Set up a simple conditional: depending on the type of object passed to the function, we will treat it either as a base mesh or as a BMesh.
The first part handles the base mesh:
|
1 2 3 4 5 6 7 |
if isinstance(obj, bpy.types.Mesh): for face in obj.polygons: face.select = False for edge in obj.edges: edge.select = False for vertex in obj.vertices: vertex.select = False |
And the second – for BMesh.
|
1 2 3 4 5 6 7 |
elif isinstance(obj, bmesh.types.BMesh): for face in obj.faces: face.select = False for edge in obj.edges: edge.select = False for vertex in obj.verts: vertex.select = False |
To test this, let’s call our function, passing in the first mesh simply as a base mesh.
|
1 |
deselect_all(obj) |
Now, let’s create a BMesh object, load the geometry from our second object into it, and call our function again—this time passing the BMesh object in the parameter.
|
1 2 3 4 5 |
bm = bmesh.new() bm.from_mesh(obj_bm) deselect_all(bm) bm.to_mesh(obj_bm) bm.free() |
To ensure the changes are reflected on the original mesh, we transfer the geometry back from the BMesh and delete the BMesh object, as it is no longer needed.
After switching back to edit mode,
|
1 |
bpy.ops.object.mode_set(mode='EDIT') |
we can clearly see that all selected edges, vertices, and faces on both meshes have been deselected.
This allows us to use the function we wrote universally, regardless of whether we are currently working with a base Mesh or a BMesh object.

.blend file on Patreon