Skip to content

Commit

Permalink
Add regex filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinzakka committed Jun 11, 2022
1 parent cde02f8 commit 4c5c17d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,27 @@ We also recommend installing [V-HACD v4.0](https://github.com/kmammou/v-hacd). I
## Usage

```bash
usage: obj2mjcf [-h] --obj-dir STR [--use-vhacd] [--save-mtl] [--save-mjcf] [--verbose] [--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] [--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]

required arguments:
--obj-dir STR path to a directory containing obj files
--obj-dir STR path to a directory containing obj files. All obj files in the directory will be
converted

optional arguments:
-h, --help show this help message and exit
--use-vhacd create a convex decomposition for the collision geom
--obj-filter STR only convert obj files matching this regex (default: None)
--save-mtl save the mtl files
--save-mjcf save an example MJCF file
--verbose print verbose output

optional vhacd args arguments:
arguments to pass to V-HACD

--vhacd-args.enable enable convex decomposition using V-HACD
--vhacd-args.max-output-convex-hulls INT
maximum number of output convex hulls (default: 64)
--vhacd-args.voxel-resolution INT
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.7"
__version__ = "0.0.8"
35 changes: 19 additions & 16 deletions obj2mjcf/_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import enum
import logging
import os
import re
import shutil
import subprocess
import tempfile
Expand Down Expand Up @@ -33,6 +34,8 @@ class FillMode(enum.Enum):

@dataclass(frozen=True)
class VhacdArgs:
enable: bool = False
"""enable convex decomposition using V-HACD"""
max_output_convex_hulls: int = 64
"""maximum number of output convex hulls"""
voxel_resolution: int = 400_000
Expand All @@ -58,9 +61,10 @@ class VhacdArgs:
@dataclass(frozen=True)
class Args:
obj_dir: str
"""path to a directory containing obj files"""
use_vhacd: bool = False
"""create a convex decomposition for the collision geom"""
"""path to a directory containing obj files. All obj files in the directory will be
converted"""
obj_filter: Optional[str] = None
"""only convert obj files matching this regex"""
save_mtl: bool = False
"""save the mtl files"""
save_mjcf: bool = False
Expand All @@ -71,17 +75,12 @@ class Args:
"""arguments to pass to V-HACD"""


def decompose_convex(
filename: Path, work_dir: Path, use_vhacd: bool, vhacd_args: VhacdArgs
) -> bool:
if not use_vhacd:
def decompose_convex(filename: Path, work_dir: Path, vhacd_args: VhacdArgs) -> bool:
if not vhacd_args.enable:
return False

if _VHACD_EXECUTABLE is None:
logging.info(
"`use_vhacd` was set but V-HACD was not found in the system path. "
"Skipping convex decomposition."
)
logging.info("`V-HACD was enabled but not found in the system path, skipping.")
return False

obj_file = filename.resolve()
Expand Down Expand Up @@ -162,9 +161,7 @@ def process_obj(filename: Path, args: Args) -> None:
logging.info(f"Saving processed meshes to {work_dir}")

# Decompose the mesh into convex pieces if V-HACD is available.
decomp_success = decompose_convex(
filename, work_dir, args.use_vhacd, args.vhacd_args
)
decomp_success = decompose_convex(filename, work_dir, args.vhacd_args)

# Read the MTL file from the OBJ file.
with open(filename, "r") as f:
Expand Down Expand Up @@ -390,7 +387,13 @@ def main() -> None:

# Get all obj files in the directory.
obj_files = list(Path(args.obj_dir).glob("*.obj"))
logging.info(f"Found {len(obj_files)} obj files.")

for obj_file in tqdm.tqdm(obj_files):
# Filter out the ones that don't match the regex filter.
if args.obj_filter is not None:
obj_files = [
x for x in obj_files if re.search(args.obj_filter, x.name) is not None
]
logging.info(f"Processing {len(obj_files)} obj files.")

for obj_file in tqdm.tqdm(obj_files, disable=args.verbose):
process_obj(obj_file, args)

0 comments on commit 4c5c17d

Please sign in to comment.