# Exporting UV coords from Blender

So I have searched on google and various other websites but I've not found an answer. The only ones I did find did not work.

So my question is how do I get UV coords from blender (2.63)?

Currently I'm writing my own custom file exporter, and so far have managed to export vertices and their normals. Is there a way to export the UV coords?

N.B. I'm currently try to figure it out using a simple cube that is unwrapped and has a texture applied to it.

• The .obj export script that comes with blender can do texture coordinates, you can find it in the scripts folder. Jun 10 '12 at 1:13

Yep, you can export those. This is a bit of code I was using when I wrote a custom exporter:

 if len(mesh.uv_textures)>0:
uvLayer=mesh.uv_textures.active
uvLayer=uvLayer.data
uv=uvLayer[faceIdx]
uv=uv.uv1, uv.uv2, uv.uv3, uv.uv4
uvCoord=uv[vertIndex][0], 1.0-uv[vertIndex][1]
uvCoord=roundVec2(uvCoord)
else:
uvCoord=v.uvco[0], 1.0-v.uvco[1]
uvCoord=roundVec2(uvCoord)


Where vertIndex is enumerating over all the vertices in a face. And faceIdx is enumerating over all the faces in a mesh.

Then when writing:

file.write('u %.6f %.6f \n' %uvCoord) # uv


I should mention that I eventually abandoned the whole writing my own exporter. It's far easier to just write an importer that imports from a common export standard. This also has the additional benefit of your exporter being updated by someone else if Blender ever changes things.

Additionally, if you still want to write your own. Reading how it's done in the "official" exporters is a pretty big help.

For a larger picture of how this code is used:

class processVerts():
def __init__(self, mesh, vert, face, faceIdx, vertIdx, file):
...
def roundVec3(v):
return round(v[0], 6), round(v[1], 6), round(v[2], 6)

v=roundVec3(tuple(vert.co))
...
if len(mesh.uv_textures)>0:
uvLayer=mesh.uv_textures.active
uvLayer=uvLayer.data
uv=uvLayer[faceIdx]
uv=uv.uv1, uv.uv2, uv.uv3, uv.uv4
uvCoord=uv[vertIdx][0], 1.0-uv[vertIdx][1]
uvCoord=roundVec2(uvCoord)
else:
uvCoord=v.uvco[0], 1.0-v.uvco[1]
uvCoord=roundVec2(uvCoord)
...
file.write('u %.6f %.6f \n' %uvCoord) # uv

class processFace():
def __init__(self, mesh, face, faceIdx, file):
...
for vertIdx, vert in enumerate(face.vertices):
processVerts(mesh, meshVerts[vert], face, faceIdx, vertIdx, file)

class processMesh():
def __init__(self, mesh, file):
for faceIdx, face in enumerate(mesh.faces):
processFace(mesh, face, faceIdx, file)

• Couple of questions... 1. What is faceIdx? You say it enumerates over all the faces in the mesh, but how? 2. What is v? (inside the else: statement). Jun 10 '12 at 2:07
– House
Jun 10 '12 at 3:33
• Not quite. Also to point out that mesh.faces does not work in Blender 2.63 but is mesh.tessfaces instead. So I'm trying to work around that. Jun 10 '12 at 3:45
• Just one more reason to go with my alternative suggestion and just write an importer to work with one of the standard export formats. Also, the code I posted is old, yes, but that was just for the context. I thought you already had code that read vertices and normals?
– House
Jun 10 '12 at 3:48
• Yes I do, but i don't understand why something like uv coords are readily available, like how normals are accessible through vertices, why not uv's through vertices too? Also in the file.write part of your code, why does it have .6f? (Python is not my strong point). Jun 10 '12 at 3:54