Help me export vertex UV coordinates properly

Hello everyone.
I need help with accessing and properly exporting vertex UV data via Python API.

I’m developing own C+±based 3D game engine and decided to create an open 3d model data format for it ( with mesh/skeleton/animation data within ). Now I’m in a process of creating an addon for Blender to export models to my JSON-based format variant.

Disregarding how exactly I am accessing UV data( via mesh.tesselated_uv_textures or face.loop_indices ), in result it’s seems to be broken: my 3d model in Blender is unwrapped and textured just fine, and after I export it, it’s texture coordinates are distorted.

Here’s my blender model:


And here’s how my model looks like, when it’s loaded within my engine:


That grey bg is another model exported from Blender in obj format with no UV coords problems.

Here’s my conversion code:


def convert_obj_mesh(mesh_obj):
    obj = triangulate_obj(mesh_obj)
    gff_mesh = gff.Mesh()
    triangulate_obj(mesh_obj)
    mesh = obj.to_mesh(bpy.context.scene, True, 'PREVIEW')
    mesh.calc_normals_split()
    export_uv = len(mesh.uv_textures) > 0
    uv_layer = mesh.uv_layers.active.data[:]

    for f in mesh.polygons:
        if len(f.vertices) != 3:
            raise gff.Error("Error: face is not a triangle!")
        gff_face = gff.Face()
        # new way of export - through loop indices
        # loop is basically an index of vertex decoupled from other faces
        # yet, loop index corresponds to actual mesh vertex
        for i in f.loop_indices:
            loop = mesh.loops[i]
            vert = mesh.vertices[loop.vertex_index]
            uv = None
            if export_uv:
                for j, ul in enumerate(mesh.uv_layers):
                    uv = ul.data[loop.index].uv
            gff_vert = gff.Vertex()
            gff_vert.position.set3(vert.co.x, vert.co.y, vert.co.z)
            gff_vert.normal.set3(vert.normal.x, vert.normal.y, vert.normal.z)
            gff_vert.index = loop.vertex_index
            if export_uv:
                gff_vert.uv.set2(uv[0], uv[1])
            gff_face.vertices.append(gff_vert)
        if export_uv:
            gff_face.calc_tangent_space()
        gff_mesh.faces.append(gff_face)
        for v in gff_face.vertices:
            gff_mesh.add_vertex(v)
    return gff_mesh

Please help, or point me somewhere, where I should look for solution.
I’ve already watched several threads on the forum and other addons sources, and still I can’t find what is wrong.

Thanks in advance.

you might need to flip the V in UV:
https://code.google.com/p/blender-cod/source/browse/blender_26/export_xmodel.py#538

Note that Blender supports “sharp” texturing, every face has its own UV coords (every loop), it does not share UVs among vertices. It usually means that a game engine needs to split faces / duplicate vertices in order to assign different UV coordinates to vertices.


Well, duplicating or splitting vertices means a huge performance overhead - usually you don’t want to do that, and better solutions are possible.
Thank you for the reply, I think I know where to head next. I will either provide support for split UVs in my engine, or ( more likely ) will dig shared UVs from blender.