I’m trying to copy the UV map coordinates from one map to another on the same object- my end goal is to export these UV coordinates into a text file. I have three UV maps on my object, at the moment. Using code lifted directly from the documentation, I’m able to get data from all three maps, store it in three dictionaries with vertice indices as the keys, and replace data on Map 0 with data from Map 1. Kind of. Here’s a zoomed-in portion of Map 0:
Here's a zoomed-in portion of Map 1: The distortion is intentional, by the way.Here’s what I get when I copy the data from Map 1 to Map 0 by vertex index:
It’s close, it’s really close to accurate, but it’s not. Most of the vertices are in the right place, but quite a few are in the wrong place, and this is my problem.
Here’s my code- again, it’s almost word for word exactly from the documentation:
import bpy
class Globals:
me = bpy.context.object.data
uv_layer0 = me.uv_layers[0].data
uv_layer1 = me.uv_layers[1].data
uv_layer2 = me.uv_layers[2].data
uv_data0 = {}
uv_data1 = {}
uv_data2 = {}
g = Globals()
def SaveUVData():
for poly in g.me.polygons:
# range is used here to show how the polygons reference loops,
# for convenience 'poly.loop_indices' can be used instead.
for loop_index in range(poly.loop_start, poly.loop_start + poly.loop_total):
g.uv_data0[g.me.loops[loop_index].vertex_index] = g.uv_layer0[g.me.loops[loop_index].vertex_index].uv
g.uv_data1[g.me.loops[loop_index].vertex_index] = g.uv_layer1[g.me.loops[loop_index].vertex_index].uv
g.uv_data2[g.me.loops[loop_index].vertex_index] = g.uv_layer2[g.me.loops[loop_index].vertex_index].uv
SaveUVData()
def UVDataTransfer():
for poly in g.me.polygons:
for loop_index in range(poly.loop_start, poly.loop_start + poly.loop_total):
print(g.uv_layer0[g.me.loops[loop_index].vertex_index].uv == g.uv_data0[g.me.loops[loop_index].vertex_index])
g.uv_layer0[g.me.loops[loop_index].vertex_index].uv = g.uv_data1[g.me.loops[loop_index].vertex_index]
UVDataTransfer()
This line makes things especially puzzling:
print(g.uv_layer0[g.me.loops[loop_index].vertex_index].uv == g.uv_data0[g.me.loops[loop_index].vertex_index])
It verifies that the data in the uv_data dictionaries is being stored correctly- that is to say, it matches the data on the uv_layers correctly. My stored UV data for Map 0 is the same as the actual data on Map 0.
Running this code returns a long list of Trues and no Falses. If the data is being stored correctly, it must not be being transferred from map to map correctly, but I have no idea why. The relevant line that transfers is this one:
g.uv_layer0[g.me.loops[loop_index].vertex_index].uv = g.uv_data1[g.me.loops[loop_index].vertex_index]
Looking at the screenshots, it seems like some vertices are just being skipped, but I can’t imagine why this would be.
Any help is infinitely appreciated