I'm currently working on my custom format exporter form blender and have hit some kind of a speed bump... Through tons of experimentation I have finally found that UV data is stored in:
mesh.uv_layers[layer].data
But I don't think that the internal blender data format agrees with the way I'm going about exporting. First off, my exporter assumes that all faces are either triangles or quads. If a face is a quad then it outputs it as two triangles:
t1 = 0 1 2
t2 = 2 3 0
Now, my exporter just iterates over mesh.polygons and outputs per vertex per triangle, but uv_layers.data obviously doesn't store it this way. So how could I go about exporting it? I need a way to lookup the uv coordinates for a specific vertex and output it in the same way as I do this:
for vert in fac.vertices:
file.write("+v %g %g %g\n" % (obj.vertices[vert].co.x, obj.vertices[vert].co.y, obj.vertices[vert].co.z))
But using the vertex's UV instead.
Any suggestions are greatly appreciated! Thanks guys!!!
EDIT:
For anyone who runs into a similar problem, here is my working code:
for fac in obj.polygons:
if len(fac.vertices) == 3:
for li in fac.loop_indices:
file.write("+t %g %g\n" % (uv_layer[li].uv.x, uv_layer[li].uv.y))
if len(fac.vertices) == 4:
file.write("+t %g %g\n" % (uv_layer[fac.loop_indices[0]].uv.x, uv_layer[fac.loop_indices[0]].uv.y))
file.write("+t %g %g\n" % (uv_layer[fac.loop_indices[1]].uv.x, uv_layer[fac.loop_indices[1]].uv.y))
file.write("+t %g %g\n" % (uv_layer[fac.loop_indices[2]].uv.x, uv_layer[fac.loop_indices[2]].uv.y))
file.write("+t %g %g\n" % (uv_layer[fac.loop_indices[2]].uv.x, uv_layer[fac.loop_indices[2]].uv.y))
file.write("+t %g %g\n" % (uv_layer[fac.loop_indices[3]].uv.x, uv_layer[fac.loop_indices[3]].uv.y))
file.write("+t %g %g\n" % (uv_layer[fac.loop_indices[0]].uv.x, uv_layer[fac.loop_indices[0]].uv.y))
This code will export the uv coordinates for any triangle or quad in the mesh (Quads are broken down into two triangles). The result is a list of the vertex uv coordinates in order of the vertices. So every 3 lines represents a triangle.
obj
is the mesh data of the current mesh, which is accessed by bpy.data.meshes['NameOfMesh']
.