1
$\begingroup$

I've been trying to set the UV's of a mesh i created via code, and i have an array of vectros with the U and V coordinates, the problem is, the way to set up the UV's via the API seems to be via MeshLoops, which confuses me because, in this case, my mesh has 128 vertices, and on the UV Layer there's 384 Mesh UV Loops, which from what i understand, consist of one edge and 2 vertices in it.

This confuses me because this means there are repeated vertices in it, regardless of wether those verts are broken or not, and i do not know how to assign the texture coordinates to the proper vertices.

here's my code: (obj is the object that contains the mesh in it, and msh is the mesh itself)

            msh.from_pydata(data[i].vertices, edges, data[i].faces)
            uv_layer = msh.uv_layers.new()
            print(obj.data.uv_layers.active.data[0])
            #for j in range(len(obj.data.uv_layers.active.data)):
            for j in range(len(data[i].uvs)-1):
                obj.data.uv_layers.active.data[j].uv = (data[i].uvs[j][0], 1-data[i].uvs[j][1])

This obviously doesn't work, because i'm not assigning data[i].uvs array to the proper uvs on the uv_layer. How could i approach properly adding the texture coordinates to my mesh with my array? I'm not sure how to apply this with this API, and the meshloop doesn't seem to favor the way i stored my coordinates.

Thanks for reading!

$\endgroup$
5
  • $\begingroup$ Did you try looking at this thread? blender.stackexchange.com/questions/30677/… You can cycle through loops (as per second example in the accepted answer). Each loop has a vertex index. From there you should be able to map your vertex data to loops. See docs.blender.org/api/current/bpy.types.MeshLoop.html for loop api $\endgroup$ – uvnoob Aug 28 '20 at 19:57
  • $\begingroup$ I've tried that, but the problem is, my model only has 129 vertices (which is the same number of texture coordinates i have), and there's 384 Mesh loops, which is the number of my faces (128) times three, so i'm not sure how to position each texture coordinate on each vertex that i have, is my issue. $\endgroup$ – Seyren Windsor Aug 28 '20 at 20:43
  • $\begingroup$ Does the order of UVs in your array match the order of vertices in the mesh? If so, for every loop you assign it's uv based on the its vertex index. Not tested code: ob.data.uv_layers.active.data[loop.index].uv =( data[loop.vertex_index][0], data[loop.vertex_index][1]). To clarify, as far as I remember, each face has a loop per vertex - this allows you to have multiple normals/uvs per vertex. In your case you only need a single uv per vertex, which can be achieved by setting the same uv value to each loop that corresponds to the same vertex $\endgroup$ – uvnoob Aug 28 '20 at 20:56
  • $\begingroup$ Okay, that actually worked! seems like the same vertices are grouped up together in groups of 3? Should i write my own answer or what should i do with the thread? $\endgroup$ – Seyren Windsor Aug 28 '20 at 21:13
  • $\begingroup$ Glad it worked. I'm not sure about the customs as well, so I just posted the comment as answer. Accept it ant it will be it I suppose :) $\endgroup$ – uvnoob Aug 28 '20 at 21:16
1
$\begingroup$

Does the order of UVs in your array match the order of vertices in the mesh? If so, for every loop you assign it's uv based on the its vertex index.

ob.data.uv_layers.active.data[loop.index].uv =( data[loop.vertex_index][0], data[loop.vertex_index][1])

To clarify, as far as I remember, each face has a loop per vertex - this allows you to have multiple normals/uvs per vertex. In your case you only need a single uv per vertex, which can be achieved by setting the same uv value to each loop that corresponds to the same vertex.

Answer based on Get/Set coordinates for UV vertices using Python

$\endgroup$

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.