Fix problem with saving texture coordinates in obj with OpenMesh
There is a great library OpenMesh for working with 3D meshes which has Python version.
I had a problem with saving texture coordinates in obj file. I use Python version of OpenMesh for manipulating with texture coordinates in 3D mesh stored in obj file and save the result mesh using method write_mesh. When I open the resultant obj file there is no texture coordinates information, only list of vertices and faces.
Let’s say we have following code
mesh = om.TriMesh()
# create vertices
vh0 = mesh.add_vertex([0, 1, 0])
vh1 = mesh.add_vertex([1, 0, 0])
...
mesh.set_texcoord2D(vh1, [0.24, 0.4])
And we write mesh to obj file
om.write_mesh('test_tex.obj', mesh)
Open test_tex.obj and its content is following
# 5 vertices, 1 faces v 0.000000 1.000000 0.000000 v 1.000000 0.000000 0.000000 v 2.000000 1.000000 0.000000 v 0.000000 -1.000000 0.000000 v 2.000000 -1.000000 0.000000 f 1 2 3
To solve this problem pass argument vertex_tex_coord in the method write_mesh (source):
om.write_mesh('test1_tex.obj', mesh, vertex_tex_coord=True)
Now rerun the script and open the obj file
# 5 vertices, 1 faces vt 0.000000 0.000000 vt 0.240000 0.400000 vt inf inf v 0.000000 1.000000 0.000000 v 1.000000 0.000000 0.000000 v 2.000000 1.000000 0.000000 v 0.000000 -1.000000 0.000000 v 2.000000 -1.000000 0.000000 f 1/1 2/2 3/3
That’s it. Good luck in working with OpenMesh.