Here’s my little proof of concept script that shows you how the UV, loops, and vertices are linked in Blender 2.62 and higher:
if bpy.app.version < 2 or bpy.app.version < 62:
raise Exception("Only for Blender 2.62 and above")
bpy.ops.object.mode_set(mode='OBJECT') # Can't access coordinate data in edit mode currently
me = bpy.data.meshes['Cube']
for f in me.polygons:
print("Polygon", f.index, "from loop index", f.loop_start, "and length", f.loop_total)
for i in f.loop_indices: # <-- python Range object with the proper indices already set
l = me.loops[i] # The loop entry this polygon point refers to
v = me.vertices[l.vertex_index] # The vertex data that loop entry refers to
print(" Loop index", l.index, "points to vertex index", l.vertex_index, \
"at position", v.co)
for j,ul in enumerate(me.uv_layers):
print(" UV Map", j, "has coordinates", ul.data[l.index].uv, \
"for this loop index")
The me.loops object is really a lookup list, and each entry is NOT a loop in and of itself. The “loops” are really polygons and are stored in the me.polygons structure. Using me.polygons, you can slice the me.loops list into chunks.
Running this on the default cube shows that me.polygons has six entries (the six faces of the cube). Each polygon refers to four loop indexes, making the me.loops list 24 entries long.
Every UV layer in me.uv_layers has a “data” attribute that is a list that is the same length as the me.loops list. Every me.loops entry relates to the same index entry in the me.uv_layers[n].data list.
GAH! But the me.uv_layers attribute is readonly! I’m trying to port a mesh importer to 2.63, and the mesh data being imported has UV data; how do I create those coordinates…?