(Solved) Loading mesh with vertex colors

Official Python bindings with a focus on reinforcement learning and robotics.
Post Reply
zhsh
Posts: 7
Joined: Thu Oct 11, 2018 10:48 pm

(Solved) Loading mesh with vertex colors

Post by zhsh »

I'm trying to load a mesh created in open3d saved in the usual `.obj` format with vertex information into pybullet. The mesh vertices load fine, but don't have any color (resulting mesh in pybullet looks the same regardless if when saving write_vertex_colors=False or not).

I am loading the mesh in pybullet via

Code: Select all

                visId = p.createVisualShape(p.GEOM_MESH, fileName=fn)
                meshId = p.createMultiBody(0, baseVisualShapeIndex=visId, basePosition=[0, 0, 0])
First few lines of the saved mesh:

Code: Select all

# Created by Open3D 
# object name: test_mesh_2022_03_15_19_28_06_50
# number of vertices: 4700
# number of triangles: 9424
v -0.055 0.0346259 0.36 0.120638 0.625828 0.533488
v -0.055 0.035 0.358984 0.119699 0.61849 0.536347
v -0.0552418 0.035 0.36 0.120081 0.622161 0.534946
v -0.0564281 0.035 0.37 0.132268 0.655014 0.519661
v 0.095 0.065 0.311799 0.276022 0.044167 0.370164
v 0.095 0.0663959 0.22 0.252194 0.269783 0.531579
Does pybullet not support colored vertex obj convention (http://paulbourke.net/dataformats/obj/colour.html)?
Is there a way to get colored meshes working (converting to texture somehow and applying texture)?
Last edited by zhsh on Tue Mar 22, 2022 9:48 pm, edited 1 time in total.
zhsh
Posts: 7
Joined: Thu Oct 11, 2018 10:48 pm

Re: Loading mesh with vertex colors

Post by zhsh »

Ok found a way to bypass this. It's true that pybullet currently can't represent meshes with vertex colors, so I had to automatically generate a UV map and transfer vertex colors to a texture on the UV map. I was able to do this using MeshLab (pymeshlab) with the following (I actually have the vertices and faces as arrays, but if you just have the obj file you can load the mesh instead):

Code: Select all

                m = pymeshlab.Mesh(verts_xyz, faces, v_color_matrix=colrs)
                ms = pymeshlab.MeshSet()
                ms.add_mesh(m, "level_set")
                # UV map and turn vertex coloring into a texture
                base_name = f"{datetime.now().strftime('%Y_%m_%d_%H_%M_%S')}_{t}"
                ms.compute_texcoord_parametrization_triangle_trivial_per_wedge()
                ms.compute_texmap_from_color(textname=f"tex_{base_name}")
                ms.save_current_mesh(fn)

                visId = p.createVisualShape(p.GEOM_MESH, fileName=fn)
                meshId = p.createMultiBody(0, baseVisualShapeIndex=visId, basePosition=[0, 0, 0])
Post Reply