Friday, June 1, 2012

Extracting UV Maps

Someone asked on G+ "how blender stores the UV maps?" and i told him i would write about it, so this inspired me to begin writing this blog.

In fact, i'll try to dump the little knowledge i have acquired creating my own exporter (Blender Machete) in the coming weeks so everyone trying to use the Blender API can benefit from what i've learnt.

On to the question: How blender stores the UV maps?

It's easy: Each UV Layer is stored on an array called uv_textures in the Mesh class. So, for a given object Obj  you could do the following:


As you can see, we're obtaining the corresponding mesh of the object and then iterating over the UV layers. In each iteration we append to uvs the mapped values from the UV.

Enumerating each interesting line:

  • Line 6: for each face in the mesh
  • Line 7: use the index of that face to extract the UV from the layer data (layer.data)
  • Line 8: and then take each component of the UV coordinate (two floats) and store it on our array.
Finally, each element in the 'uvs' array is an array for each vertex that you can pass directly on a Vertex Buffer Object in OpenGL as the UVs.

Pretty simple, isn't it?

No comments:

Post a Comment