How to assign UV coordinates to faces in export script.

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
Posts: 0
Joined: Sat Dec 29, 2012 1:56 pm

How to assign UV coordinates to faces in export script.

Post by hybridflat » Sat Dec 29, 2012 4:01 pm

Hello guys! I'm writing an export script in python and I need some help. First of all I have never worked before with Blender so maybe I will have lame questions but I try to explain my problem. It will be a task for a university lesson so I got a fixed xml format for the export. It looks something like this:

Code: Select all

<texture u0="1.000000" v0="0.000000" />
		<texture u0="1.000000" v0="1.000000" />
		<texture u0="0.000000" v0="1.000000" />
		<texture u0="0.000000" v0="0.000000" />

		<face a="0" b="1" c="2" ta="0" tb="1" tc="2" />
		<face a="2" b="3" c="0" ta="2" tb="3" tc="0" />
		<face a="4" b="5" c="6" ta="3" tb="0" tc="1" />
		<face a="6" b="7" c="4" ta="1" tb="2" tc="3" />
		<face a="0" b="3" c="5" ta="3" tb="0" tc="1" />
		<face a="5" b="4" c="0" ta="1" tb="2" tc="3" />
		<face a="3" b="2" c="6" ta="3" tb="0" tc="1" />
		<face a="6" b="5" c="3" ta="1" tb="2" tc="3" />
		<face a="2" b="1" c="7" ta="3" tb="0" tc="1" />
		<face a="7" b="6" c="2" ta="1" tb="2" tc="3" />
		<face a="1" b="0" c="4" ta="3" tb="0" tc="1" />
		<face a="4" b="7" c="1" ta="1" tb="2" tc="3" />
Here the texture tag contains the UV coordinates. The a,b,c attributes of the face tag contain the vertices of the face. The ta, tb, tc attributes represent the index of the belonging UV coordinate. So, in our example if we are looking at the first line from the face tags, the ta="0" belongs to a="0" vertex. This means that the corresponding line from the texture tags is the first one (0 th programmatically). So my question is simple, how can I assign these UV coordinates to face vertices? I'm looping through the mesh.tessfaces to get the face information and the[face_index] to get the UV coordinates, but I can't see the connection between them programmatically. I would appreciate any help!

Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Tue Jan 01, 2013 6:48 pm

Actually, there isn't a connection between blender vertices and uv coordiantes. The reason is, that vertices can be shared among many faces in blender, but each individual face can still have its own uv texture (sharp edges).

This is achieved by another data structure, called loops. A loop is a per-face vertex (=not shared with any other faces). Each loop has a corresponding UV record.

If you wanna export ngons, then you need to deal with loops. But if you use tessfaces, you get tris and quads. You may wanna turn quads into tris, to get triangulated data only. But either way, you can map the uv1 to uv4 properties to the tessface vertices.

Here's an example: ...
I'm sitting, waiting, wishing, building Blender in superstition...

Post Reply