Skip to content

Commit

Permalink
Handle extraneous material edge case.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinzakka committed Sep 26, 2022
1 parent 86320e2 commit 3a322b1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion obj2mjcf/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.21"
__version__ = "0.0.22"
18 changes: 18 additions & 0 deletions obj2mjcf/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,24 @@ def process_obj(filename: Path, args: Args) -> None:
logging.info(f"Saving submesh {savename}")
geom.export(savename, include_texture=True, header=None)

# Edge case handling where the material file can have many materials but the OBJ
# itself only references one. In that case, we trim out the extra materials and
# only keep the one that is referenced.
if isinstance(mesh, trimesh.base.Trimesh) and len(mtls) > 1:
# Find the material that is referenced.
with open(filename, "r") as f:
lines = f.readlines()
for i, line in enumerate(lines):
if line.startswith("usemtl"):
break
mat_name = line.split()[1]
# Trim out the extra materials.
for smtl in sub_mtls:
if smtl[0].split()[1] == mat_name:
break
sub_mtls = [smtl]
mtls = [Material.from_string(smtl)]

# Delete any MTL files that were created during trimesh processing, if any.
for file in [
x for x in work_dir.glob("**/*") if x.is_file() and "material_0" in x.name
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
readme = f.read()

core_requirements = [
"trimesh>=3.12.5",
"trimesh>=3.15.2",
"Pillow>=9.1.1",
"mujoco>=2.2.0",
"dcargs>=0.1.2",
Expand Down

0 comments on commit 3a322b1

Please sign in to comment.