From 3a322b168029993a5fc0f5dbe685cb32e8958f20 Mon Sep 17 00:00:00 2001 From: Kevin Zakka Date: Sun, 25 Sep 2022 19:58:33 -0700 Subject: [PATCH] Handle extraneous material edge case. --- obj2mjcf/__init__.py | 2 +- obj2mjcf/_cli.py | 18 ++++++++++++++++++ setup.py | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/obj2mjcf/__init__.py b/obj2mjcf/__init__.py index c35625a..479dad0 100644 --- a/obj2mjcf/__init__.py +++ b/obj2mjcf/__init__.py @@ -1 +1 @@ -__version__ = "0.0.21" +__version__ = "0.0.22" diff --git a/obj2mjcf/_cli.py b/obj2mjcf/_cli.py index 1da3240..dc7562f 100644 --- a/obj2mjcf/_cli.py +++ b/obj2mjcf/_cli.py @@ -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 diff --git a/setup.py b/setup.py index fb3d080..1179272 100644 --- a/setup.py +++ b/setup.py @@ -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",