0
$\begingroup$

I need to create a mesh by reading a custom file format.
I have a list of vertices, faces and uvs.

So far I am able to create a mesh with vertices and faces with the following script:

import bpy
import bmesh

# verts = [...]
# uvs = [...]
# faces = [...]

mesh_data = bpy.data.meshes.new("cube_mesh_data")
mesh_data.from_pydata(verts, [], faces)
mesh_data.update()

obj = bpy.data.objects.new("My_Object", mesh_data)

scene = bpy.context.scene
scene.collection.objects.link(obj)
obj.select_set(state=True)

However, I cannot find a way to assign the UV array to the vertices. UV is a list of pair of floats with the same length as verts. I have looked online but couldn't find any solution.

Any tips or resources I could read? Thank you very much!

$\endgroup$
  • $\begingroup$ UV coordinates are generally per face/vertex (not only per vertex as a 3D vertex could be used several times in the UV map). Could you describe a bit more the input data? $\endgroup$ – lemon Oct 14 at 11:15
  • $\begingroup$ You're right, however in this case each vertex is used exactly once. The input data is an xml that is parsed to a list of vertices (list of tuples of 3 floats each), a list of uvs (list of pairs of 2 floats each with same size as the vertices) and a list of faces (tuples of 3 int each) $\endgroup$ – simog Oct 14 at 11:50
  • $\begingroup$ OK, so I imagine that 3D vertices are eventually doubled in your case. Anyway, you can set uv coordinates to "o.data.uv_layers['UVMap'].data[x].uv" where "x" is given inside "o.data.polygons[p].loop_indices". $\endgroup$ – lemon Oct 14 at 12:04

Your Answer

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

Browse other questions tagged or ask your own question.