Skip to content

Commit cd516d8

Browse files
authored
[BUG FIX] Raise exception if trying to load PointCloud as Mesh. (#2042)
1 parent 4547b8c commit cd516d8

File tree

5 files changed

+24
-18
lines changed

5 files changed

+24
-18
lines changed

‎genesis/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def _custom_excepthook(exctype, value, tb):
411411
from pygel3d import graph, hmesh
412412
except OSError as e:
413413
# Import may fail because of missing system dependencies (libGLU.so.1).
414-
# This is not blocking because it is only an issue for hybrig entities.
414+
# This is not blocking because it is only an issue for hybrid entities.
415415
pass
416416

417417
try:

‎genesis/engine/mesh.py‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def copy(self):
195195
Copy the mesh.
196196
"""
197197
return Mesh(
198-
mesh=self._mesh.copy(include_cache=True),
198+
mesh=self._mesh.copy(**(dict(include_cache=True) if isinstance(self._mesh, trimesh.Trimesh) else {})),
199199
surface=self._surface.copy(),
200200
uvs=self._uvs.copy() if self._uvs is not None else None,
201201
metadata=self._metadata.copy(),
@@ -221,7 +221,7 @@ def from_trimesh(
221221
surface.update_texture()
222222
else:
223223
surface = surface.copy()
224-
mesh = mesh.copy(include_cache=True)
224+
mesh = mesh.copy(**(dict(include_cache=True) if isinstance(mesh, trimesh.Trimesh) else {}))
225225

226226
try: # always parse uvs because roughness and normal map also need uvs
227227
uvs = mesh.visual.uv.copy()
@@ -234,9 +234,10 @@ def from_trimesh(
234234
color_factor = None
235235
opacity = 1.0
236236

237-
if mesh.visual.defined:
238-
if mesh.visual.kind == "texture":
239-
material = mesh.visual.material
237+
visual = mesh.visual
238+
if isinstance(visual, trimesh.visual.texture.TextureVisuals) and visual.defined:
239+
if visual.kind == "texture":
240+
material = visual.material
240241

241242
# TODO: Parsing PBR in obj or not
242243
# trimesh from .obj file will never use PBR material, but that from .glb file will
@@ -267,12 +268,15 @@ def from_trimesh(
267268
else:
268269
color_factor = (*color_factor[:3], color_factor[3] * opacity)
269270
else:
270-
gs.raise_exception()
271-
271+
gs.raise_exception(f"Unsupported Trimesh material type '{type(material)}'.")
272272
else:
273273
# TODO: support vertex/face colors in luisa
274-
color_factor = tuple(np.array(mesh.visual.main_color, dtype=np.float32) / 255.0)
275-
274+
color_factor = tuple(np.array(visual.main_color, dtype=np.float32) / 255.0)
275+
elif isinstance(visual, trimesh.visual.color.VertexColor) and visual.vertex_colors.size > 0:
276+
color = np.unique(visual.vertex_colors, axis=0)
277+
if len(color) > 1:
278+
gs.raise_exception("Loading point clouds with heterogeneous colors is not supported.")
279+
color_factor = tuple(np.array(color, dtype=np.float32) / 255.0)
276280
else:
277281
# use white color as default
278282
color_factor = (1.0, 1.0, 1.0, 1.0)
@@ -328,7 +332,7 @@ def from_attrs(cls, verts, faces, normals=None, surface=None, uvs=None, scale=No
328332
def from_morph_surface(cls, morph, surface=None):
329333
"""
330334
Create a genesis.Mesh from morph and surface options.
331-
If the morph is a Mesh morph (morphs.Mesh), it could contain multiple submeshes, so we return a list.
335+
If the morph is a Mesh morph (morphs.Mesh), it could contain multiple sub-meshes, so we return a list.
332336
"""
333337
if isinstance(morph, gs.options.morphs.Mesh):
334338
if morph.is_format(gs.options.morphs.MESH_FORMATS):
@@ -355,9 +359,7 @@ def from_morph_surface(cls, morph, surface=None):
355359
assert all(isinstance(mesh, trimesh.Trimesh) for mesh in morph.files)
356360
meshes = [mu.trimesh_to_mesh(mesh, morph.scale, surface) for mesh in morph.files]
357361
else:
358-
gs.raise_exception(
359-
f"File type not supported (yet). Submit a feature request if you need this: {morph.file}."
360-
)
362+
gs.raise_exception(f"File type not supported: {morph.file}")
361363

362364
return meshes
363365

‎genesis/options/morphs.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
URDF_FORMAT = ".urdf"
2020
MJCF_FORMAT = ".xml"
21-
MESH_FORMATS = (".obj", ".ply", ".stl")
21+
MESH_FORMATS = (".obj", ".stl")
2222
GLTF_FORMATS = (".glb", ".gltf")
2323
USD_FORMATS = (".usd", ".usda", ".usdc", ".usdz")
2424

‎genesis/utils/mesh.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,12 @@ def postprocess_collision_geoms(
473473

474474
def parse_mesh_trimesh(path, group_by_material, scale, surface):
475475
meshes = []
476-
for _, mesh in trimesh.load(path, force="scene", group_material=group_by_material, process=False).geometry.items():
477-
meshes.append(gs.Mesh.from_trimesh(mesh=mesh, scale=scale, surface=surface, metadata={"mesh_path": path}))
476+
scene = trimesh.load(path, force="scene", group_material=group_by_material, process=False)
477+
for tmesh in scene.geometry.values():
478+
if not isinstance(tmesh, trimesh.Trimesh):
479+
gs.raise_exception(f"Mesh type not supported: {path}")
480+
mesh = gs.Mesh.from_trimesh(mesh=tmesh, scale=scale, surface=surface, metadata={"mesh_path": path})
481+
meshes.append(mesh)
478482
return meshes
479483

480484

‎tests/test_render.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ def test_render_planes(tmp_path, png_snapshot, renderer_type, renderer):
11491149
@pytest.mark.required
11501150
@pytest.mark.parametrize("renderer_type", [RENDERER_TYPE.RASTERIZER])
11511151
@pytest.mark.skipif(not IS_INTERACTIVE_VIEWER_AVAILABLE, reason="Interactive viewer not supported on this platform.")
1152-
def test_batch_deformable_render(tmp_path, monkeypatch, png_snapshot):
1152+
def test_batch_deformable_render(monkeypatch, png_snapshot):
11531153
CAM_RES = (640, 480)
11541154

11551155
# Disable text rendering as it is messing up with pixel matching when using old CPU-based Mesa driver

0 commit comments

Comments
 (0)