mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
3.01k stars 581 forks source link

How to apply Vertex colors from .obj file? #448

Closed mowshon closed 5 years ago

mowshon commented 5 years ago

pip list:

PyOpenGL (3.1.0)
pyrender (0.1.24)
Shapely (1.6.4.post2)
trimesh (2.38.24)

I have a obj file from http://cvl-demos.cs.nott.ac.uk/vrn/ Bill You can download and try this tool by yourself, after that you can download the .obj file Source: https://specials-images.forbesimg.com/imageserve/5c76b4b84bbe6f24ad99c370/416x416.jpg?background=000000&cropX1=0&cropX2=4000&cropY1=0&cropY2=4000

bill-gates.obj (with VERTEX COLORS) contains something like that:

v 61.00 117.00 22.00 0.49 0.31 0.16
v 60.00 118.00 22.00 0.44 0.28 0.13
v 62.00 118.00 22.00 0.49 0.31 0.18
v 61.00 119.00 22.00 0.45 0.28 0.15
v 61.00 117.00 22.00 0.49 0.31 0.16
v 61.00 116.00 22.50 0.52 0.33 0.17
v 60.00 117.00 22.50 0.46 0.29 0.14
.... ....
.... ....
f 3630 2580 3628
f 2582 2580 3630
f 3632 2582 3630
f 3632 3635 2582
f 3635 2584 2582
f 3636 2585 2584
f 3636 2584 3637
f 3637 2584 3635
f 3637 3639 3638
f 3636 3637 3638
f 3639 3641 3640
f 3638 3639 3640
f 3641 3643 3642
f 3640 3641 3642

I have tried this code:

import trimesh

bill_trimesh = trimesh.load('bill-gates.obj')
bill_trimesh.show()

The face color is always gray.

  1. How to extract vertex colors?
  2. How to apply this vertex colors?
jackd commented 5 years ago

Mike can correct me if I'm wrong, but it seems vertex_colors aren't currently supported by the obj loader. It's currently undergoing a rewrite, and while it seems to be a consideration the implementation is commented out.

In terms of rendering, it seems vertex colors aren't currently supported for mesh rendering either.

If you're happy hacking the source code, the following worked for me:

  1. Implement vertex color loading on the feature/obj branch load_obj by uncommenting this section and adding 'vertex_colors': vc[mask_v] here and 'vertex_colors': vc here
  2. There seems to be an issue with vertex counts/face indices based on the one example I downloaded (1 too few vertices). This might be because of trimesh simplifying something incorrectly, or could be the file itself. I'll leave you to do the investigations, but for a seriously dodgy "get it working just once" method, I added a single vertex/color at the beginning of the Trimesh constructor.
    vertices = np.concatenate([vertices, [[0, 0, 0]]], axis=0)
    vertex_colors = np.concatenate([vertex_colors, [[0, 0, 0]]], axis=0)
  3. Converting the mesh to a point cloud
    
    import trimesh
    obj_scene = trimesh.load('face.obj')
    geom = obj_scene.geometry.values()
    scene = trimesh.scene.Scene()
    for g in geom:
    pc = trimesh.PointCloud(g.vertices, g.visual.vertex_colors)
    scene.add_geometry(pc)

scene.show()


![face](https://user-images.githubusercontent.com/659115/58931930-fe290c00-87a4-11e9-9550-41f2b99fc768.png)
mowshon commented 5 years ago

@jackd, I did everything according to your instructions and it worked for me. It is a pity that this is not in the stable version. I'll try to figure it out. You helped me a lot.

@mikedh do you plan to support vertex colors from a .obj file for the future?

mikedh commented 5 years ago

Unfortunately all the OBJ files I downloaded from that side didn't open in meshlab or trimesh and appeared to have index issues.

Yep, supporting vertex colors is quite straightforward and we'll support it in the new OBJ loader.

mikedh commented 5 years ago

Hey, these now load cleanly in the release/major branch (scheduled to be released 7/15/2019), including vertex colors. The main problem was an index issue which is now fixed. It's a shame these files are so large, otherwise it would be great to put them in unit tests as they have several edge cases :).

MarkCEagar commented 1 year ago

Mike truly struggling to convert 200 obj files from Texture to Vertex Colors...Reason being there is bleed in the obj files which are all textured pink (HEX #ffc0cb)...The idea is to convert the texture to vertex colors (Faces more optimal) and delete all the pink faces/vertices from the mesh...

Not to fazed about retexturing the updated mesh as I can easily accomplish this in the photogrammetry software. Any chance there is a workflow like this for Trimesh ?? I have tried to follow the above yet coming up dry every time...

Example of OBJ