mikedh / trimesh

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

Error when loading .obj where only some objects have colors #2085

Open domef opened 9 months ago

domef commented 9 months ago

If I create a scene with two objects and only one has verteces/triangles colors (one of them or both), it can be exported correctly but crashes when loading it:

import trimesh
from trimesh.visual import ColorVisuals
import numpy as np

scene = trimesh.Scene()
box = trimesh.creation.box(extents=[1, 2, 3])
box.visual = ColorVisuals()
box.visual.vertex_colors = np.random.uniform(size=(box.vertices.shape[0], 3))
box.visual.face_colors = np.random.uniform(size=(box.faces.shape[0], 3))
scene.add_geometry(box, geom_name="BOX")
scene.add_geometry(trimesh.creation.cone(1.0, 1.0), geom_name="CONE")
scene.export("myobjects.obj", file_type="obj")
loaded_scene = trimesh.load("myobjects.obj", file_type="obj")

The error is:

Traceback (most recent call last):
  File "/home/federico/Desktop/trimesh_bug.py", line 13, in <module>
    loaded_scene = trimesh.load("myobjects.obj", file_type="obj")
  File "/home/federico/myenv/lib/python3.9/site-packages/trimesh/exchange/load.py", line 120, in load
    loaded = load_mesh(file_obj, file_type=file_type, resolver=resolver, **kwargs)
  File "/home/federico/myenv/lib/python3.9/site-packages/trimesh/exchange/load.py", line 191, in load_mesh
    results = loader(file_obj, file_type=file_type, resolver=resolver, **kwargs)
  File "/home/federico/myenv/lib/python3.9/site-packages/trimesh/exchange/obj.py", line 223, in load_obj
    mask_v = np.zeros(len(v), dtype=bool)
TypeError: object of type 'NoneType' has no len()
mikedh commented 9 months ago

Thanks for the report and reproducible example! Yeah it's definitely the flat-array shenanigans on import causing this. The export looks pretty nice, but I noticed meshlab also hard-crashes on "some vertices have color, some don't."

rahulswa08 commented 7 months ago

Any update on this? I'm facing the same issue when trying to import mesh file in trimesh which is generated using blender.