Skip to content

Commit

Permalink
Add ability to resize textures via a command line flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinzakka committed Jul 5, 2022
1 parent c185b0b commit 39e9f2b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion obj2mjcf/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.17"
__version__ = "0.0.18"
20 changes: 20 additions & 0 deletions obj2mjcf/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 39e9f2b

Please sign in to comment.