When we work with the bmesh object through the Blender Python API to create new geometry, all created vertices receive an index equal to -1. Although we can fully interact and control the created vertices, the lack of a numbered index can make it somewhat difficult to perceive the geometry and debug the code.
To assign sequential numbers to vertices in bmesh, the index_update() function is used.
Let’s see how it works. To do this, create a bmesh object from the currently active mesh.
1 2 3 |
mesh = bpy.context.object.data bm = bmesh.from_edit_mesh(mesh) bm.verts.ensure_lookup_table() |
And check the numbering of the current vertices indices.
1 2 3 |
print([_vert.index for _vert in bm.verts]) # [0, 1, 2, 3, 4, 5, 6, 7] |
Now create a new vertex with (0.0, 0.0, 2.0) coordinates.
1 |
new_vert = bm.verts.new((0.0, 0.0, 2.0)) |
And check the index numbers again.
1 2 3 |
print([_vert.index for _vert in bm.verts]) # [0, 1, 2, 3, 4, 5, 6, 7, -1] |
As we see, a new vertex with index equal to -1 has appeared. And this is a new vertex created by us.
1 2 3 |
print(new_vert) # <BMVert(0x000002BF03FAFA90), index=-1> |
Now convert the indices to sequential order using the index_update() function.
1 |
bm.verts.index_update() |
And check the indices again.
1 2 3 4 5 |
print([_vert.index for _vert in bm.verts]) print(new_vert) # [0, 1, 2, 3, 4, 5, 6, 7, 8] # <BMVert(0x000002BF03FAF150), index=8> |
After calling the index_update() function, all indices received ordinal numbers. This is also reflected in the data of the vertex we created.
In fact, the index_update() function consists of only two lines.
1 2 |
for i, vertex in enumerate(bm.verts): vertex.index = i |
Important
Remember that calling index_update() does not guarantee that the resulting indices will exactly follow the order in which the vertices are created or the order in which the vertices follow each other.