Exporting UV coordinates by face elements

I'm trying to write my own export script, and I'm not sure how to reference the uv coordinates as an element array, similar to how .obj format works (ie. you have a list of all uv coordinates "vt s t" and each face selects the correct index). I looked at previous threads on similar things, but there doesn't seem to be anything similar to my issue. Do I need to hash all uv coordinates and compute the index for each face myself? Or is there a built-in way to do this? How does the .obj exporter work? Also, for further reference, is it possible to see the python source for the .obj exporter?

• It would be nice if you could post your edit as an answer. – stacker Mar 22 '14 at 10:28
• @stacker And done! – spearmunkie Mar 22 '14 at 10:40

I found the .obj export source in /usr/share/blender It's probably different on your computer, the best place to see where you can find it is to go into user preference under Addons. It seems like the official exporter just creates a hash map of uv coordinates. This seems to be where the magic happens.

# Make our own list so it can be sorted to reduce context switching
face_index_pairs = [(face, index) for index, face in enumerate(me.polygons)]
uv = f_index = uv_index = uv_key = uv_val = uv_ls = None

uv_face_mapping = [None] * len(face_index_pairs)

uv_dict = {}
uv_get = uv_dict.get
for f, f_index in face_index_pairs:
uv_ls = uv_face_mapping[f_index] = []
for uv_index, l_index in enumerate(f.loop_indices):
uv = uv_layer[l_index].uv
uv_key = veckey2d(uv)
uv_val = uv_get(uv_key)
if uv_val is None:
uv_val = uv_dict[uv_key] = uv_unique_count
fw('vt %.6f %.6f\n' % uv[:])
uv_unique_count += 1
uv_ls.append(uv_val)


Since I answered my own question, should this be deleted? I feel like this has some value to other people who may want to do something similar, as this is a specific and non-trivial task.

• Answering your own questions is fine, you even have the option to answer as you post the question, it is a way to share your knowledge with others. – sambler Mar 23 '14 at 1:48