0
$\begingroup$

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):
        vertices.append([self.ob.data.vertices[uv_polygon.vertices[i]],
                        self.ob.data.uv[uv_polygon.vertices[i]]])
    try:
        Face(vertices, uv_polygon.material_index)
    except:
        Face(vertices, -1)

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

Thanks

$\endgroup$
1

1 Answer 1

2
$\begingroup$

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.

http://www.blender.org/api/blender_python_api_2_73_release/info_gotcha.html#ngons-and-tessellation-faces

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:

https://code.google.com/p/blender-cod/source/browse/blender_26/export_xmodel.py#518

$\endgroup$
4
  • $\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
    Commented Mar 11, 2015 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
    Commented Mar 11, 2015 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
    Commented Mar 11, 2015 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
    Commented Mar 12, 2015 at 8:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .