Making selected edges in a mesh sequentially active

Many operators in the Blender Python API work based on the currently active element—a vertex, edge, or polygon. But very often, we need to execute such an operator multiple times for several selected, rather than active, elements. To do this, we need to sequentially make each of the selected elements active in a loop and call the desired operator.

Let’s consider this situation using mesh edges as an example. Suppose we have several selected edges, and we want to make each of them active in order to perform an action on them.

We can’t make the desired edge active in the base mesh, so we’ll use a BMesh object.

Create a BMesh object and load the current mesh’s geometry into it. The mesh is in edit mode.

Get a list of selected edges.

The active edge in a BMesh is the last selected edge. However, simply selecting an edge using its “select” property isn’t enough. To make it active, we need to add it to the selection history.

Loop through all the selected edges.

First, clear the selection history. Next, make the next edge selected. And then add this edge to the history.

Now we have the next edge in a loop active in the BMesh.

Flush the changes from the BMesh object back to the base mesh.

Now the new edge is considered active in the base mesh. Here we can call the required operator. Then, continuing the loop, move on to the next edge.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted