To remove vertices, edges or polygons from a bmesh object, it is most convenient to use the remove() method for the corresponding set of elements. In addition, the bmesh object has a bmesh.ops.delete() operator, which allows us to delete elements using different modes (context).
There are seven contexts for the bmesh.ops.delete() delete operator:
- VERTS – removing vertices
- EDGES – removing edges
- FACES_ONLY – removing only faces
- EDGES_FACES – removing only faces and edges
- FACES – removing faces
- FACES_KEEP_BOUNDARY – removing faces, saving their boundaries
- TAGGED_ONLY – only tagged (undocumented)
The required context is specified in the third parameter of the deleting operator.
Let’s add Suzanne to the scene.
To work with bmesh correctly, we need to switch to editing mode.
1 2 |
if bpy.context.active_object.mode == 'OBJECT': bpy.ops.object.mode_set(mode='EDIT') |
Create a bmesh object and transfer data from the current active mesh (Suzanne) to it.
1 |
bm = bmesh.from_edit_mesh(bpy.context.object.data) |
Create a list of selected polygons.
1 |
selection = [face for face in bm.faces if face.select] |
This list is passed as the second parameter to the delete() operator. If we use deletion contexts for points or edges, we will need to form the selection list from points or edges, respectively. When calling the operator, only elements from this list will be deleted.
Now we can execute the operator
1 |
bmesh.ops.delete(bm, geom=selection, context='FACES_KEEP_BOUNDARY') |
In the first parameter of the operator we passed a pointer to the bmesh object, in the second – we passed the created selection list, and in the third we specified the context.
When calling the operator, the selected polygons will be deleted, and since the context for deleting polygons was used while preserving their boundaries, the edges around the deleted polygons will not be deleted.
Once the removal is done, we can return the geometry to the active mesh.
1 |
bmesh.update_edit_mesh(bpy.context.object.data) |