Finding the active polygon in a BMesh object

The BMesh object certainly offers greater functionality and capabilities for working with mesh data compared to the standard data set stored in the object’s “data” property. However, some aspects of managing mesh data in a BMesh object aren’t always obvious. For example, consider the rather trivial operation of getting the currently active polygon.

For example we have the currently active mesh is in edit mode. If it’s in object mode, we’ll switch it.

Switch to polygon selection mode.

Create a BMesh object, transfer data from the currently active mesh into it, and rebalance the polygon indices.

We can obtain the currently active polygon in a BMesh through the “bm.select_history” list. This list stores the entire selection history for the current BMesh object. The last selected polygon will always be considered active, but it’s better to access it through the “active” pointer of the list.

It seems simple, but there are a few subtle points.

If we select another polygon in BMesh, it is not automatically added to the history list and, therefore, does not become active.

This must be done manually by removing it from the history list and adding it again.

Keep this in mind when manipulating the selection in a BMesh object.

Also, keep in mind that when the user switches the polygon-edge-vertex selection mode, the selection history in the BMesh object is reset.

By switching the selection mode, for example, to edges and back to polygons, we get an empty selection list in BMesh.

We get an empty list, while selections are still present on the mesh. This should also be taken into account.

There is another way to get the active polygon in BMesh: through the active polygon index in the mesh’s main data block.

First, we simply take the index of the currently active polygon in the mesh, and then get the polygon in the BMesh with that index. This, therefore, should be the active polygon.

However, this method also has a catch: if we intentionally change the active polygon while working with the BMesh, as shown above, this method will still return the polygon that was active in the original mesh data block, not the one we set. On the other hand, this will always be the polygon the user sees as active in the 3D viewport.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted