Exporting UV coordinates I'm having massive trouble exporting UV coordinates from Blender. I'm using the following code:

"""
Name: 'PMDL'
Blender: 263
Group: 'Export'
Tooltip: 'Exports to ESTL format'
"""

bl_info={
        "name":             "ESTL",
        "description":      "Exports to estl format",
        "author":           "Doxin",
        "version":          (0,2),
        "blender":          (2,60,0),
        "location":         "File &gt; Import-Export",
        "category":         "Import-Export"
        }

import bpy
from bpy_extras.io_utils import ExportHelper
import sys
import struct

class ExportMyFormat(bpy.types.Operator, ExportHelper):
    bl_idname       = "model.estl";
    bl_label        = "ESTL Exporter";
    bl_options      = {'PRESET'};

    filename_ext    = ".estl";

    def execute(self, context):
        with open(self.filepath,"wb") as fid:
            fid.write(b"ExtendedStl")
            fid.write(struct.pack("I",0))

            count=0
            for ob in bpy.context.scene.objects:
                try:
                    ob.data.polygons
                except AttributeError:
                    continue

                bpy.context.scene.objects.active=ob
                bpy.ops.object.mode_set(mode='EDIT')
                bpy.ops.mesh.quads_convert_to_tris()
                bpy.ops.object.mode_set(mode='OBJECT')

                count+=len(ob.data.polygons)

            fid.write(struct.pack("I",count))
            bpy.ops.object.mode_set(mode='OBJECT')
            for ob in bpy.context.scene.objects:
                try:
                    ob.data.polygons
                except AttributeError:
                    continue

                for face in ob.data.polygons:
                    for vert, loop in zip(face.vertices, face.loop_indices):
                        for item in ob.data.vertices[vert].normal:#normal
                            fid.write(struct.pack("f",item))
                        for item in ob.data.vertices[vert].co:#vertex
                            fid.write(struct.pack("f",item))
                        for item in (ob.data.uv_layers.active.data[loop].uv if ob.data.uv_layers.active!=None else (0,0)):#uv
                            fid.write(struct.pack("f",item))

        return {'FINISHED'};

def menu_func(self, context):
    self.layout.operator(ExportMyFormat.bl_idname, text="ESTL (.estl)");

def register():
    bpy.utils.register_module(__name__);
    bpy.types.INFO_MT_file_export.append(menu_func);

def unregister():
    bpy.utils.unregister_module(__name__);
    bpy.types.INFO_MT_file_export.remove(menu_func);

if __name__ == "__main__":
    register()


It seems like almost every triangle gets the wrong UV coordinates, causing stretching and smearing of my textures. I've been stuck on this issue for a couple days now.

Does anyone know if I'm doing something obvious wrong?

EDIT: it seems that somehow the triangulation is messing up my uvs, if I triangulate the mesh before unwrapping the uvs seem to be mostly correct.
This issue is that you are accessing UV's by the vertex index,

ob.data.uv_layers.active.data[vert].uv


This is incorrect, you need to iterate over the face loops and access UV's by the loop index.

This is how you could access loops.

            for face in ob.data.polygons:
                for vert, loop in zip(face.vertices, face.loop_indices):
                    for item in ob.data.vertices[vert].normal:  # normal
                        fid.write(struct.pack("f",item))
                    for item in ob.data.vertices[vert].co:  # vertex
                        fid.write(struct.pack("f",item))
                    for item in (ob.data.uv_layers.active.data[loop].uv if ob.data.uv_layers.active is not None else (0.0, 0.0)):  # uv
                        fid.write(struct.pack("f",item))


Note that ob.data.uv_layers.active is not None should probably be moved outside of the loop as assigned a var has_uv for eg, its quite inefficient to check this all the time.