Getting a vertices list in the order they follow each other

To work with the geometry of 3D objects in Blender, sometimes it is necessary to get not just a list of vertices, but a list of vertices in the order they follow one after another. This can be done with the Blender Python API.

When we get the list of mesh vertices in the common way:

We will get it in the order in which vertices are created, added or removed, but this order does not correspond to the order in which they follow each other in the geometry. We can check this by turning on the display of point indices for viewing in the 3D viewport area.

Such a list of vertices is not always convenient; for example, for a given geometry, it is impossible to build a correct polygon (face) that fills it.

We can get a list of vertices in the desired order using the bmesh.

Let’s create a bmesh and prepare the geometry to work with it:

Define a buffer list into which we will write the vertex indices in the following order:

Start from the first vertex (with index 0) and loop through the bmesh points, buffering their indices:

Each vertex in bmesh has pointers to the edges adjacent to this vertex. And each edge has pointers to the vertices at the end of this edge. Using this, we can get the index of the next point from one point through the edge.

The list of edges adjacent to a given vertex can be obtained through the link_edges pointer. The second vertex on the edge can be obtained through the edge.other_vert() method, passing in its parameter a pointer to the started vertex.

In the list of two edges adjacent to the original vertex, we choose one:

In the check, we look at both edges adjacent to the original vertex, get the index of the second point and check if it is already in our list of sorted indices. If the index is already in the list, it means we have turned in the opposite direction along the edges, and we need another edge. If there is no index, then we are moving in the right direction and the index of the other vertex is the next one we need.

Pointer to next vertex:

will be added to the list of sorted vertices at the next iteration of the while loop. The next step will be started from it.

If we need to “close” the chain, after the end of the while loop, we can add a vertex with a zero index to the end of the list.

Since we need the vertex indices of the original mesh, not bmesh, at the end of the check we need to translate the indices from bmesh to the original mesh:

Since the geometry of an object is not always ideal and there could be vertices that are adjacent to more than two edges, as well as vertices that are not connected by edges, we need to add a break-condition to our loop.

Before starting the loop, let’s count the total number of points, define a counter variable and increase it on each pass of the loop. If our loop became infinitive during operation, we will exit the loop after checking the maximum number of mesh points.

Let’s define all our code as a function:

In the function parameter, we will pass a pointer to an object (mesh).

Now we can get a list of vertices in the order we need:

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Inline Feedbacks
View all comments