Getting the active vertex, edge and face of a mesh using the Blender Python API

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.

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.

We can simply get a pointer to a polygon by index using the same list.

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.

We will refer to the selection history, if it exists, and get the last object of the required type in the list – a vertex.

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.

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.

This way we got all three pointers we need – to the active vertex, active edge and active face for the current mesh.

5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments