Working with UV-Maps through the Blender API

Active (selected) UV-Map:

Access to the active UV-Map by its name:

One mesh polygon always corresponds to one UV-Map polygon. However, this is not true for edges and vertices. Mesh edge with a seam divides into two separate edges in a UV-Map. Mesh vertex divides into as many points in a UV-Map as seams cross it. Therefore, in order to relate the UV-Map and mesh point, the concept of “MeshLoop” is used.

“MeshLoop” is a logical combination of one edge and one vertex. Two adjacent polygons have one common edge and two common vertices, but each polygon has its own separate “MeshLoops” located at the site of the common edge. So the separate quad polygon has 4 vertexes, 4 edges and 4 “MeshLoops”. But the default cube has 8 vertexes, 12 edges and 24 “MeshLoops” – 6 polygons with 4 “MeshLoops” on each of them.

To distinguish “MeshLoops” on the mesh and “MeshLoops” on the UV-Map they are differently typed. MeshLoop – for the mesh, MeshUVLoop – for the UV-Map.

The “MeshLoop” data is only available in the “Object” mode. It is one of the Blender API features. Before you start working, you need to check or force the required mode:

We can access all the mesh and the UV-Map “MeshLoops” through the polygons indices. Let’s get the UV-Map “MeshLoops” list with the coordinates of their points:

If the mesh edge was not marked seam when constructing the UV-Map, the UV-Map “MeshLoops” for this edge is located at one point and when you select this point with a mouse click all “MeshLoops” together selected. All manipulations (for example – movement) are made with all the matching “MeshLoops” at once. This is convenient for stitching – it’s enough to combine the separate UV-Map “MeshLoops” that corresponds to the same edge of the mesh at one point to sew the seam. But that concept makes difficult to divide UV-Map “MeshLoops”.

The following code allows to separate the selected UV-Map “MeshLoops”:

The “centroid” function calculates the coordinates of the polygon center from the coordinates of its vertices.

The script scans all selected UV-Map points. All the “MeshLoops” for the selected point move slightly towards the center of the polygon they belong.