From 09c0e7181b5a2a96322bc8e5d43e1bae5facfeb5 Mon Sep 17 00:00:00 2001 From: Kevin Zakka Date: Fri, 23 Sep 2022 13:00:52 -0700 Subject: [PATCH] Fix MTL save bug. --- obj2mjcf/__init__.py | 2 +- obj2mjcf/_cli.py | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/obj2mjcf/__init__.py b/obj2mjcf/__init__.py index a11f0b4..040835a 100644 --- a/obj2mjcf/__init__.py +++ b/obj2mjcf/__init__.py @@ -1 +1 @@ -__version__ = "0.0.19" +__version__ = "0.0.20" diff --git a/obj2mjcf/_cli.py b/obj2mjcf/_cli.py index c4e153c..f78add7 100644 --- a/obj2mjcf/_cli.py +++ b/obj2mjcf/_cli.py @@ -45,6 +45,9 @@ "map_Kd", ) +# Character used to denote a comment in an MTL file. +_MTL_COMMENT_CHAR = "#" + class FillMode(enum.Enum): FLOOD = enum.auto() @@ -290,9 +293,12 @@ def process_obj(filename: Path, args: Args) -> None: # Parse the MTL file into separate materials. with open(mtl_filename, "r") as f: lines = f.readlines() - lines = [ - line.strip() for line in lines if not line.startswith("#") and line.strip() - ] + # Remove comments. + lines = [line for line in lines if not line.startswith(_MTL_COMMENT_CHAR)] + # Remove empty lines. + lines = [line for line in lines if line.strip()] + # Remove trailing whitespace. + lines = [line.strip() for line in lines] # Split at each new material definition. for line in lines: if line.startswith("newmtl"): @@ -366,7 +372,7 @@ def process_obj(filename: Path, args: Args) -> None: break # Save the MTL file. with open(work_dir / f"{mtl_name}.mtl", "w") as f: - f.write("".join(smtl)) + f.write("\n".join(smtl)) # Edit the mtllib line to point to the new MTL file. if len(sub_mtls) > 1: savename = str(work_dir / f"{filename.stem}_{i}.obj")