From 39e9f2b0c84441353d0bbeca8d933b8647600a65 Mon Sep 17 00:00:00 2001 From: Kevin Zakka Date: Tue, 5 Jul 2022 16:50:52 -0400 Subject: [PATCH] Add ability to resize textures via a command line flag. --- README.md | 16 ++++++++++++---- obj2mjcf/__init__.py | 2 +- obj2mjcf/_cli.py | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1589e64..86b2379 100644 --- a/README.md +++ b/README.md @@ -44,9 +44,13 @@ bash install_vhacd.sh ## Usage ```bash -usage: obj2mjcf [-h] --obj-dir STR [--obj-filter STR] [--save-mtl] [--save-mjcf] [--compile-model] [--verbose] [--vhacd-args.enable] [--vhacd-args.max-output-convex-hulls INT] [--vhacd-args.voxel-resolution INT] - [--vhacd-args.volume-error-percent FLOAT] [--vhacd-args.max-recursion-depth INT] [--vhacd-args.disable-shrink-wrap] [--vhacd-args.fill-mode {FLOOD,SURFACE,RAYCAST}] [--vhacd-args.max-hull-vert-count INT] - [--vhacd-args.disable-async] [--vhacd-args.min-edge-length INT] [--vhacd-args.split-hull] +usage: obj2mjcf [-h] --obj-dir STR [--obj-filter STR] [--save-mtl] [--save-mjcf] [--compile-model] [--verbose] [--vhacd-args.enable] + [--vhacd-args.max-output-convex-hulls INT] [--vhacd-args.voxel-resolution INT] [--vhacd-args.volume-error-percent FLOAT] + [--vhacd-args.max-recursion-depth INT] [--vhacd-args.disable-shrink-wrap] [--vhacd-args.fill-mode {FLOOD,SURFACE,RAYCAST}] + [--vhacd-args.max-hull-vert-count INT] [--vhacd-args.disable-async] [--vhacd-args.min-edge-length INT] [--vhacd-args.split-hull] + [--texture-args.resize-percent FLOAT] + +A CLI for processing composite Wavefront OBJ files into a MuJoCo-conducive format. required arguments: --obj-dir STR path to a directory containing obj files. All obj files in the directory will be @@ -60,7 +64,7 @@ optional arguments: --compile-model compile the MJCF file to check for errors --verbose print verbose output -optional vhacd args arguments: +optional vhacd_args arguments: arguments to pass to V-HACD --vhacd-args.enable enable convex decomposition using V-HACD @@ -84,6 +88,10 @@ optional vhacd args arguments: minimum size of a voxel edge (default: 2) --vhacd-args.split-hull try to find optimal split plane location + +optional texture_args arguments: + --texture-args.resize-percent FLOAT + resize the texture to this percentage of the original size (default: 1.0) ``` [OBJ]: https://en.wikipedia.org/wiki/Wavefront_.obj_file diff --git a/obj2mjcf/__init__.py b/obj2mjcf/__init__.py index 1f658a4..f18e5d0 100644 --- a/obj2mjcf/__init__.py +++ b/obj2mjcf/__init__.py @@ -1 +1 @@ -__version__ = "0.0.17" +__version__ = "0.0.18" diff --git a/obj2mjcf/_cli.py b/obj2mjcf/_cli.py index 060f722..1976eef 100644 --- a/obj2mjcf/_cli.py +++ b/obj2mjcf/_cli.py @@ -78,6 +78,12 @@ class VhacdArgs: """try to find optimal split plane location""" +@dataclass(frozen=True) +class TextureArgs: + resize_percent: float = 1.0 + """resize the texture to this percentage of the original size""" + + @dataclass(frozen=True) class Args: obj_dir: str @@ -95,6 +101,7 @@ class Args: """print verbose output""" vhacd_args: VhacdArgs = field(default_factory=VhacdArgs) """arguments to pass to V-HACD""" + texture_args: TextureArgs = field(default_factory=TextureArgs) @dataclass @@ -149,6 +156,18 @@ def mjcf_specular(self) -> str: return f"{Ks}" +def resize_texture(filename: Path, texture_args: TextureArgs) -> None: + """Resize a texture to a percentage of its original size.""" + if texture_args.resize_percent == 1.0: + return + image = Image.open(filename) + new_width = int(image.size[0] * texture_args.resize_percent) + new_height = int(image.size[1] * texture_args.resize_percent) + logging.info(f"Resizing {filename} to {new_width}x{new_height}") + image = image.resize((new_width, new_height), Image.LANCZOS) + image.save(filename) + + def decompose_convex(filename: Path, work_dir: Path, vhacd_args: VhacdArgs) -> bool: if not vhacd_args.enable: return False @@ -302,6 +321,7 @@ def process_obj(filename: Path, args: Args) -> None: image.save(dst_filename) texture_name = dst_filename.name mtl.map_Kd = texture_name + resize_texture(dst_filename, args.texture_args) logging.info("Done processing MTL file") logging.info("Processing OBJ file with trimesh")