Learning loops

In general, the “loop” is usually a sequential selection of several points, edges or polygons of a mesh.

However, there is an element in the mesh structure, which is also called a “loop”. It is a combination of one vertex with one edge of the mesh. Let’s try to learn what these “loops” are for.

Let’s consider a separate polygon with 4 vertices and 4 edges.

The polygon has 4 loops:

  1. Vertex 0 + edge 1
  2. Vertex 1 + edge 2
  3. Vertex 3 + edge 3
  4. Vertex 2 + edge 0

Loops are not sorted according to indices of vertexes or edges. What loop is considered to be conditionally first is possible to identify using the polygon property “loop_start”, which returns the index of the edge which takes as the starting point:

Loops are always counterclockwise directed in the right-sided coordinate system (the polygon normal is directed upwards). The loop always comes out of the vertex and runs along the edge.

In our example, the first loop leaves the vertex with the index 2 and goes along the edge with the index 0.

The total number of polygon loops can be obtained using the “loop_total” polygon property:

And the list of the polygon loop indices – through the range property “loop_indices”:

The mesh object has the list of all meshes loops:

If the polygon loop is just a vertex – edge combination, the loop in the mesh structure has its own type:

From “MeshLoop”, we can get the vertex and the edge indices owned by this loop:

It seems that the loops in the mesh structure do not carry much value, but it’s not. For example, in order to match the mesh polygons with the polygons of its unwrap (UV) structure, loops mechanisms is used.

From the polygon loop we can access to the corresponding loop on the mesh UV:

And from the UV-loop – get the vertex UV-coordinates:

So we can correlate the mesh vertex with its coordinates on the mesh UV, which makes us possible to manipulate the texture superimposition.

The loops mechanism is also implemented in the internal Blender mesh format – “BMesh”. Here it provides match more opportunities.

The BMesh loop has its own type:

and a large set of properties and methods. But the principle remains unchanged: the loop is a combination of a vertex and an edge.

To get all polygon loops, let’s execute the following code:

For each loop, it is easy to get the polygon to which the loop owns, the edge along which it goes, and the vertex from which the loop starts.

The second vertex (the loop comes in) we can get using the “other_vert()” method of the BMesh edge:

For BMesh edges and vertexes, it is also available to get the loops owned to them with the “link_loops” property.

The method:

is called for each set of BMesh polygons, edges, and vertexes to rebuild the index tables for these elements.

In addition to simple loops access, BMesh provides a simple mechanism for switching between neighboring loops.

For a separate polygon loop the:

property always return the previous loop of this polygon.

The

property returns the next polygon loop.

Calling the same properties “link_loop_prev” or “link_loop_next” from the received loop we can, for example, get the polygon opposite side loop:

or

Other useful properties for switching between BMesh loops are:

and

Calling them allows us to move from the current loop to the loop on the same edge but on the neighboring polygon and directed in the opposite direction.

In most cases, for the usual mesh geometry, the “link_loop_radial_next” and “link_loop_radial_prev” properties points to the same loop.

They will point to different loops only if more than two polygons converge into one edge. This case, each next call “link_loop_radial_next” will point to the loop owned by the next polygon with the same edge, and “link_loop_radial_prev” – to the previous one.

To demonstrate this let’s mark selected several edges one by one (similar by clicking with the alt key pressed) by code. We will start from one selected edge of the mesh and add to the selection another 3 edges, lying the same direction.

To move from the current edge to the next one, we need to execute a chain of loops transitions:

and mark selected the edge owned to the received loop.

Repeating that transitions between loops and marking received edges as selected let’s make the required selection.

The complete script code that selects the next 3 edges, starting with the selected one:

The loops mechanism is clear, but sometimes not predictably.

For example, on a mesh cut, if the polygon does not have a neighboring polygon, the properties “link_loop_radial_next” (“link_loop_radial_prev”) and “link_loop_next” (“link_loop_prev”) will point to the same loop.

Therefore, executing the following code:

in the case of a mesh cut will point to the same polygon opposite edge loop, but not to the perpendicular edge of the neighboring polygon, as might be expected.

At the same time, if the cut is filled with an n-gone, the same combination of loop transitions will return the expected perpendicular edge loop of the neighboring polygon.