I'm trying to update an old script and I'm near to achieve my goal but I'm locking at getting uv coordinates because the old script used the texture assigned using the UV image editor and the new one need to use the texture assigned with material.

Then the thing I don't find is how to get the uv as easy as the vertex coordinates. This is the code I have to get faces:

face_count = len(self.ob.data.polygons)
print("face count: "+str(face_count))

for f_index in range(face_count):
    uv_polygon = self.ob.data.polygons[f_index]

    vertices = []
    # export the triangle
    for i in range(3):
        Face(vertices, uv_polygon.material_index)
        Face(vertices, -1)

The error appears here: self.ob.data.uv[ uv_polygon.vertices[i] ] because uv is not an attribute of mesh.



UV coordinates are stored in ob.data.uv_layers.active.data[loop_index].uv since the mesh system update (NGons are supported from 2.63 on). Note that a loop index is required, not a vertex index.

UV textures are stored in ob.data.uv_textures.active.data[face_index].image.


You might wanna use the tessface mesh (triangles and quads) for your export. Don't forget to triangulate quads, your original code doesn't seem to handle them at all. Here's an example:


  • $\begingroup$ (About the quad it is handled in the full code) Is loop_index equivalent to uv_polygon.vertices[i] in my code ? $\endgroup$
    – Entretoize
    Mar 11 '15 at 10:08
  • $\begingroup$ Every face used to have 3 or 4 vertices, and the uv data was stored on face-level (fixed-size array of 4 elements, each storing a pair of UV coordinates). Since the amount of vertices per face is now unbounded, it's no longer stored there. It's rather stored per-face vertex (=loop). You can get all loop UV pairs like: [ob.data.uv_layers.active.data[li].uv for face in ob.data.polygons for li in face.loop_indices]. Note that .tessface_uv_textures has an interface like the old mesh system (no loops, stored on face-level). $\endgroup$
    – CodeManX
    Mar 11 '15 at 14:12
  • $\begingroup$ I'm nosure to understand this notation, I'm not advanced in python (as I need the list of uv corresponding to the vertices I listed) About tessface_uv_textures I first updated my old code using that but I had no data, like if it's not getting material... $\endgroup$
    – Entretoize
    Mar 11 '15 at 14:23
  • $\begingroup$ I found the solution, for thus who will look for, as I'm getting vertice indices with uv_polygon.vertices[i] the uv indices are gotten with uv_polygon.loop_indices[i] which gives self.ob.data.uv_layers.active.data[ uv_polygon.loop_indices[i] ].uv to get the uv's. $\endgroup$
    – Entretoize
    Mar 12 '15 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.