To work with the objects geometry in Blender, we often need to know which vertex (edge or face) is currently active. It is not difficult to get the active elements of the mesh geometry using the Blender Python API.
Let’s see how we can get a pointer to the active vertex, active edge and active polygon for the current object in the Blender scene.
To work with the geometry of a mesh, we first need to switch to object mode.
|
1 |
bpy.ops.object.mode_set(mode='OBJECT') |
Active face
Getting the active polygon for the current object is the easiest. The list of mesh polygons has a property “active”, which stores the index of the currently active polygon.
|
1 2 |
active_face_index = bpy.context.object.data.polygons.active # 5 |
We can simply get a pointer to a polygon by index using the same list.
|
1 2 3 |
active_face = bpy.context.object.data.polygons[active_face_index] # <bpy_struct, MeshPolygon at 0x00000162C7A86D2C> |
Active vertex
We can get a pointer to the active vertex of the object using the bmesh object and its “select_history” list, which stores the entire history of geometry selections. The active vertex is always the last selected vertex in the selection history.
Create a bmesh object and dump the current mesh’s geometry into it. Also match the order of the indices in bmesh with the order of the indices in the source mesh geometry.
|
1 2 3 |
bm = bmesh.new() bm.from_mesh(bpy.context.active_object.data) bm.verts.ensure_lookup_table() |
We will refer to the selection history, if it exists, and get the last object of the required type in the list – a vertex.
|
1 2 3 4 |
active_vertex = None if bm.select_history: active_vertex_bm = next((_vertex for _vertex in reversed(bm.select_history) \ if isinstance(_vertex, bmesh.types.BMVert)), None) |
To speed up the search, we reverse the selection history list and take the first element of the required type. If there are no selected vertices in the history, we get None.
So far, we have received a pointer to an object of type BMVert – the vertex type in the bmesh object. But we need to get a pointer to the vertex itself in the original mesh geometry. We can get it by index, that’s why we previously brought the indices of the original geometry and bmesh into correspondence.
|
1 2 3 |
active_vertex = bpy.context.object.data.vertices[active_vertex_bm.index] if active_vertex_bm else None # <bpy_struct, MeshVertex at 0x00000162CB465DA4> |
Active edge
We can get the pointer to the active edge in the same way as to the active vertex. Through the bmesh object and the selection history list.
|
1 2 3 4 5 6 7 8 |
bm.edges.ensure_lookup_table() active_edge = None if bm.select_history: active_edge_bm = next((_edge for _edge in reversed(bm.select_history) \ if isinstance(_edge, bmesh.types.BMEdge)), None) active_edge = bpy.context.object.data.edges[active_edge_bm.index] if active_edge_bm else None # <bpy_struct, MeshEdge at 0x00000162CB185DF0> |
This way we got all three pointers we need – to the active vertex, active edge and active face for the current mesh.

.blend file on Patreon