# Accessing data in the UV layout in Python?

I learned how to export the UV layout from here,

but am still unclear how to access the data in Python.

By "access the data," I mean getting an array that tells me where each vertex ends up in the UV map (in other words, UV and vertex ID for each of the vertex above). This is why exporting the UV layout as a rasterized .png, as above, is not acceptable.

# The hacky way in my mind...

I can imagine exporting the UV layout to a vector image (.svg and .eps supported by the above-referenced addon), and then extracting the coordinates from the vector image, but this sounds like a hack that requires parsing .svg or .eps. I believe since we can export it, there must be a way for us to directly access the data.

# What I tried

I've fiddled with tessface_uv_textures, uv_layers, uv_textures, but they are all either empty lists or None, probably because my material is just a diffuse shader of a color. But this should have nothing to do with the UV layout though, right?

• I'm not familiar with the python apis but I'm prettysure based on the C side it should be uv_layers, and a quick glance at the docs suggests I'm right. Check that the correct mesh actually is selected and the UVs are defined in the mesh data tab. Also be aware that a single vert may have multiple locations in the UV map if it appears on a seam. UV maps are defined in terms or faces. – Sazerac Jun 21 '19 at 0:03
• Does this help?: blender.stackexchange.com/questions/30677/… – Jaroslav Jerryno Novotny Jun 21 '19 at 11:14
• Thanks both @Sazerac and Jaroslav for the helpful comments! I've found the solution and added below. – Sibbs Gambling Jun 22 '19 at 0:44

Turns out I need to get into the EDIT mode and select all vertices, and then perform the projection.

bpy.ops.object.mode_set(mode='OBJECT')
bpy.context.scene.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.uv.smart_project()
bpy.ops.object.mode_set(mode='OBJECT')


After that, accessing data is as easy as the solution given in Get/Set coordinates for UV vertices using Python.