Upload scripts/run_convex_decomposition.py with huggingface_hub
Browse files
scripts/run_convex_decomposition.py
CHANGED
|
@@ -185,7 +185,24 @@ def filter_coacd_params(coacd, params):
|
|
| 185 |
def run_coacd_for_mesh(mesh_path: Path, output_dir: Path, params):
|
| 186 |
coacd, trimesh = import_deps()
|
| 187 |
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
if mesh.is_empty:
|
| 190 |
raise RuntimeError(f"empty mesh: {mesh_path}")
|
| 191 |
|
|
@@ -330,13 +347,18 @@ def main():
|
|
| 330 |
|
| 331 |
if derived_dir.exists() and not args.overwrite:
|
| 332 |
raise SystemExit(f"destination already exists: {derived_dir}. Use --overwrite only if you intend to replace files inside it.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
# Fail before creating derived files if the real decomposition backend is unavailable.
|
| 335 |
import_deps()
|
| 336 |
|
| 337 |
-
(
|
| 338 |
-
(
|
| 339 |
-
(
|
| 340 |
|
| 341 |
coacd_params = {
|
| 342 |
"threshold": args.threshold,
|
|
@@ -353,14 +375,14 @@ def main():
|
|
| 353 |
|
| 354 |
for mesh_path in meshes:
|
| 355 |
mesh_stem = safe_name(mesh_path.stem)
|
| 356 |
-
out_dir =
|
| 357 |
part_paths, actual_tool_version, actual_params = run_coacd_for_mesh(mesh_path, out_dir, coacd_params)
|
| 358 |
inputs_log.append({
|
| 359 |
"path": rel_to_root(mesh_path),
|
| 360 |
"sha256": sha256_file(mesh_path),
|
| 361 |
})
|
| 362 |
for part_path in part_paths:
|
| 363 |
-
part_rel = part_path.relative_to(
|
| 364 |
part_index = int(part_path.stem.split("_")[-1])
|
| 365 |
mesh_name = safe_name(f"{source}_{source_asset_id}_{mesh_stem}_convex_{part_index:03d}")
|
| 366 |
part_records.append({
|
|
@@ -377,7 +399,7 @@ def main():
|
|
| 377 |
})
|
| 378 |
|
| 379 |
write_xml_files(
|
| 380 |
-
|
| 381 |
source_asset_id,
|
| 382 |
part_records,
|
| 383 |
args.class_name,
|
|
@@ -414,7 +436,7 @@ def main():
|
|
| 414 |
metadata["readiness_level"] = raw_meta["readiness_level"]
|
| 415 |
if "source_commit" in raw_meta:
|
| 416 |
metadata["source_commit"] = raw_meta["source_commit"]
|
| 417 |
-
(
|
| 418 |
|
| 419 |
source_refs = {
|
| 420 |
"raw_asset": rel_to_root(raw_asset_dir),
|
|
@@ -423,7 +445,7 @@ def main():
|
|
| 423 |
else "",
|
| 424 |
"raw_meshes": [rel_to_root(p) for p in meshes],
|
| 425 |
}
|
| 426 |
-
(
|
| 427 |
|
| 428 |
log = {
|
| 429 |
"tool": "coacd",
|
|
@@ -440,7 +462,7 @@ def main():
|
|
| 440 |
"standalone_include": "mjcf/convex_collision_include.xml",
|
| 441 |
},
|
| 442 |
}
|
| 443 |
-
(
|
| 444 |
|
| 445 |
readme = f"""# Convex decomposition: {source_asset_id}
|
| 446 |
|
|
@@ -464,7 +486,10 @@ MuJoCo integration:
|
|
| 464 |
|
| 465 |
Do not edit the raw source asset in place.
|
| 466 |
"""
|
| 467 |
-
(
|
|
|
|
|
|
|
|
|
|
| 468 |
|
| 469 |
if args.register_manifest:
|
| 470 |
row = {
|
|
|
|
| 185 |
def run_coacd_for_mesh(mesh_path: Path, output_dir: Path, params):
|
| 186 |
coacd, trimesh = import_deps()
|
| 187 |
|
| 188 |
+
# Convex decomposition only needs geometry. Skipping materials avoids a
|
| 189 |
+
# Pillow dependency for textured OBJ files from sources such as RoboCasa.
|
| 190 |
+
loaded = trimesh.load(mesh_path, skip_materials=True)
|
| 191 |
+
if isinstance(loaded, trimesh.Scene):
|
| 192 |
+
meshes = []
|
| 193 |
+
for node_name in loaded.graph.nodes_geometry:
|
| 194 |
+
transform, geometry_name = loaded.graph[node_name]
|
| 195 |
+
geom = loaded.geometry[geometry_name].copy()
|
| 196 |
+
geom.visual = trimesh.visual.ColorVisuals(geom)
|
| 197 |
+
geom.apply_transform(transform)
|
| 198 |
+
meshes.append(geom)
|
| 199 |
+
if not meshes:
|
| 200 |
+
raise RuntimeError(f"no mesh geometry in scene: {mesh_path}")
|
| 201 |
+
mesh = trimesh.util.concatenate(meshes)
|
| 202 |
+
else:
|
| 203 |
+
mesh = loaded
|
| 204 |
+
mesh.visual = trimesh.visual.ColorVisuals(mesh)
|
| 205 |
+
|
| 206 |
if mesh.is_empty:
|
| 207 |
raise RuntimeError(f"empty mesh: {mesh_path}")
|
| 208 |
|
|
|
|
| 347 |
|
| 348 |
if derived_dir.exists() and not args.overwrite:
|
| 349 |
raise SystemExit(f"destination already exists: {derived_dir}. Use --overwrite only if you intend to replace files inside it.")
|
| 350 |
+
work_dir = derived_dir
|
| 351 |
+
if not args.overwrite:
|
| 352 |
+
work_dir = derived_dir.parent / f".{derived_dir.name}.tmp"
|
| 353 |
+
if work_dir.exists():
|
| 354 |
+
raise SystemExit(f"temporary output already exists from a previous failed run: {work_dir}")
|
| 355 |
|
| 356 |
# Fail before creating derived files if the real decomposition backend is unavailable.
|
| 357 |
import_deps()
|
| 358 |
|
| 359 |
+
(work_dir / "meshes").mkdir(parents=True, exist_ok=True)
|
| 360 |
+
(work_dir / "mjcf").mkdir(parents=True, exist_ok=True)
|
| 361 |
+
(work_dir / "logs").mkdir(parents=True, exist_ok=True)
|
| 362 |
|
| 363 |
coacd_params = {
|
| 364 |
"threshold": args.threshold,
|
|
|
|
| 375 |
|
| 376 |
for mesh_path in meshes:
|
| 377 |
mesh_stem = safe_name(mesh_path.stem)
|
| 378 |
+
out_dir = work_dir / "meshes" / mesh_stem
|
| 379 |
part_paths, actual_tool_version, actual_params = run_coacd_for_mesh(mesh_path, out_dir, coacd_params)
|
| 380 |
inputs_log.append({
|
| 381 |
"path": rel_to_root(mesh_path),
|
| 382 |
"sha256": sha256_file(mesh_path),
|
| 383 |
})
|
| 384 |
for part_path in part_paths:
|
| 385 |
+
part_rel = part_path.relative_to(work_dir).as_posix()
|
| 386 |
part_index = int(part_path.stem.split("_")[-1])
|
| 387 |
mesh_name = safe_name(f"{source}_{source_asset_id}_{mesh_stem}_convex_{part_index:03d}")
|
| 388 |
part_records.append({
|
|
|
|
| 399 |
})
|
| 400 |
|
| 401 |
write_xml_files(
|
| 402 |
+
work_dir / "mjcf",
|
| 403 |
source_asset_id,
|
| 404 |
part_records,
|
| 405 |
args.class_name,
|
|
|
|
| 436 |
metadata["readiness_level"] = raw_meta["readiness_level"]
|
| 437 |
if "source_commit" in raw_meta:
|
| 438 |
metadata["source_commit"] = raw_meta["source_commit"]
|
| 439 |
+
(work_dir / "metadata.yaml").write_text("\n".join(dump_yaml(metadata)) + "\n")
|
| 440 |
|
| 441 |
source_refs = {
|
| 442 |
"raw_asset": rel_to_root(raw_asset_dir),
|
|
|
|
| 445 |
else "",
|
| 446 |
"raw_meshes": [rel_to_root(p) for p in meshes],
|
| 447 |
}
|
| 448 |
+
(work_dir / "source_refs.yaml").write_text("\n".join(dump_yaml(source_refs)) + "\n")
|
| 449 |
|
| 450 |
log = {
|
| 451 |
"tool": "coacd",
|
|
|
|
| 462 |
"standalone_include": "mjcf/convex_collision_include.xml",
|
| 463 |
},
|
| 464 |
}
|
| 465 |
+
(work_dir / "logs" / "decomposition.json").write_text(json.dumps(log, indent=2, ensure_ascii=False) + "\n")
|
| 466 |
|
| 467 |
readme = f"""# Convex decomposition: {source_asset_id}
|
| 468 |
|
|
|
|
| 486 |
|
| 487 |
Do not edit the raw source asset in place.
|
| 488 |
"""
|
| 489 |
+
(work_dir / "README.md").write_text(readme)
|
| 490 |
+
|
| 491 |
+
if work_dir != derived_dir:
|
| 492 |
+
work_dir.rename(derived_dir)
|
| 493 |
|
| 494 |
if args.register_manifest:
|
| 495 |
row = {
|