The BMesh module provides users of the Blender Python API with far more options for editing meshes than the usual object data structure.
A BMesh object could be created from the currently active mesh as follows:
1 2 3 |
import bmesh bm = bmesh.from_edit_mesh(bpy.context.object.data) |
The source mesh must be in the Edit mode.
To create a new vertex, we can use the new() method of the “verts” collection of the BMesh object. We should pass the coordinates, in which place this point will be created, in the method parameter. Coordinates are considered in the object’s local coordinate system.
For example, let’s create copies of all selected mesh vertices, raising them by 3 units by the Z axis.
Loop through all the selected vertices, get their coordinates, add 3 to the Z-component of the coordinates and create new vertices in the required place.
1 2 3 4 5 |
for vert in bm.verts: if vert.select: co = vert.co.copy() co.z += 3 new_vert = bm.verts.new(co) |
To remove points in a BMesh, we can use the remove() method of the “verts” collection of the BMesh object.
Now let’s delete the initially selected points from which we made copies.
1 2 3 |
for vert in bm.verts: if vert.select: bm.verts.remove(vert) |
We should pass a pointer to the vertex to be removed through the parameter of the remove() method.
Now apply all the changes to the original mesh so that they are displayed in the viewport, and clear the BMesh object.
1 2 3 |
bmesh.update_edit_mesh(bpy.context.object.data) bm.free() |