Upload scripts/run_convex_decomposition.py with huggingface_hub
Browse files
scripts/run_convex_decomposition.py
CHANGED
|
@@ -14,6 +14,7 @@ The generated MJCF files are intentionally split:
|
|
| 14 |
"""
|
| 15 |
|
| 16 |
import argparse
|
|
|
|
| 17 |
import hashlib
|
| 18 |
import inspect
|
| 19 |
import json
|
|
@@ -97,6 +98,35 @@ def sha256_file(path: Path):
|
|
| 97 |
return h.hexdigest()
|
| 98 |
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
def safe_name(text: str):
|
| 101 |
text = Path(text).stem if "/" in text else text
|
| 102 |
text = re.sub(r"[^0-9A-Za-z_]+", "_", text)
|
|
@@ -217,7 +247,7 @@ def run_coacd_for_mesh(mesh_path: Path, output_dir: Path, params):
|
|
| 217 |
part_mesh = trimesh.Trimesh(vertices, faces)
|
| 218 |
part_path = output_dir / f"part_{i:03d}.obj"
|
| 219 |
part_mesh.export(part_path)
|
| 220 |
-
outputs.append(part_path)
|
| 221 |
|
| 222 |
return outputs, coacd_version(coacd), kwargs
|
| 223 |
|
|
@@ -376,12 +406,12 @@ def main():
|
|
| 376 |
for mesh_path in meshes:
|
| 377 |
mesh_stem = safe_name(mesh_path.stem)
|
| 378 |
out_dir = work_dir / "meshes" / mesh_stem
|
| 379 |
-
|
| 380 |
inputs_log.append({
|
| 381 |
"path": rel_to_root(mesh_path),
|
| 382 |
"sha256": sha256_file(mesh_path),
|
| 383 |
})
|
| 384 |
-
for part_path in
|
| 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}")
|
|
@@ -391,11 +421,13 @@ def main():
|
|
| 391 |
"sha256": sha256_file(part_path),
|
| 392 |
"mesh_name": mesh_name,
|
| 393 |
"file_from_mjcf": f"../{part_rel}",
|
|
|
|
| 394 |
})
|
| 395 |
outputs_log.append({
|
| 396 |
"path": part_rel,
|
| 397 |
"sha256": sha256_file(part_path),
|
| 398 |
"mesh_name": mesh_name,
|
|
|
|
| 399 |
})
|
| 400 |
|
| 401 |
write_xml_files(
|
|
@@ -464,6 +496,50 @@ def main():
|
|
| 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 |
|
| 469 |
Source asset:
|
|
|
|
| 14 |
"""
|
| 15 |
|
| 16 |
import argparse
|
| 17 |
+
import csv
|
| 18 |
import hashlib
|
| 19 |
import inspect
|
| 20 |
import json
|
|
|
|
| 98 |
return h.hexdigest()
|
| 99 |
|
| 100 |
|
| 101 |
+
def rounded_list(values, digits=8):
|
| 102 |
+
return [round(float(v), digits) for v in values]
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def mesh_stats(mesh):
|
| 106 |
+
extents = rounded_list(mesh.bounding_box.extents)
|
| 107 |
+
center = rounded_list(mesh.bounding_box.centroid)
|
| 108 |
+
bbox_volume = float(extents[0] * extents[1] * extents[2])
|
| 109 |
+
volume = None
|
| 110 |
+
try:
|
| 111 |
+
volume = float(mesh.volume)
|
| 112 |
+
except Exception:
|
| 113 |
+
volume = None
|
| 114 |
+
area = None
|
| 115 |
+
try:
|
| 116 |
+
area = float(mesh.area)
|
| 117 |
+
except Exception:
|
| 118 |
+
area = None
|
| 119 |
+
return {
|
| 120 |
+
"bbox_extents": extents,
|
| 121 |
+
"bbox_center": center,
|
| 122 |
+
"bbox_volume": round(bbox_volume, 12),
|
| 123 |
+
"mesh_volume": round(volume, 12) if volume is not None else None,
|
| 124 |
+
"surface_area": round(area, 12) if area is not None else None,
|
| 125 |
+
"num_vertices": int(len(mesh.vertices)),
|
| 126 |
+
"num_faces": int(len(mesh.faces)),
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
|
| 130 |
def safe_name(text: str):
|
| 131 |
text = Path(text).stem if "/" in text else text
|
| 132 |
text = re.sub(r"[^0-9A-Za-z_]+", "_", text)
|
|
|
|
| 247 |
part_mesh = trimesh.Trimesh(vertices, faces)
|
| 248 |
part_path = output_dir / f"part_{i:03d}.obj"
|
| 249 |
part_mesh.export(part_path)
|
| 250 |
+
outputs.append((part_path, mesh_stats(part_mesh)))
|
| 251 |
|
| 252 |
return outputs, coacd_version(coacd), kwargs
|
| 253 |
|
|
|
|
| 406 |
for mesh_path in meshes:
|
| 407 |
mesh_stem = safe_name(mesh_path.stem)
|
| 408 |
out_dir = work_dir / "meshes" / mesh_stem
|
| 409 |
+
part_outputs, actual_tool_version, actual_params = run_coacd_for_mesh(mesh_path, out_dir, coacd_params)
|
| 410 |
inputs_log.append({
|
| 411 |
"path": rel_to_root(mesh_path),
|
| 412 |
"sha256": sha256_file(mesh_path),
|
| 413 |
})
|
| 414 |
+
for part_path, part_stats in part_outputs:
|
| 415 |
part_rel = part_path.relative_to(work_dir).as_posix()
|
| 416 |
part_index = int(part_path.stem.split("_")[-1])
|
| 417 |
mesh_name = safe_name(f"{source}_{source_asset_id}_{mesh_stem}_convex_{part_index:03d}")
|
|
|
|
| 421 |
"sha256": sha256_file(part_path),
|
| 422 |
"mesh_name": mesh_name,
|
| 423 |
"file_from_mjcf": f"../{part_rel}",
|
| 424 |
+
**part_stats,
|
| 425 |
})
|
| 426 |
outputs_log.append({
|
| 427 |
"path": part_rel,
|
| 428 |
"sha256": sha256_file(part_path),
|
| 429 |
"mesh_name": mesh_name,
|
| 430 |
+
**part_stats,
|
| 431 |
})
|
| 432 |
|
| 433 |
write_xml_files(
|
|
|
|
| 496 |
}
|
| 497 |
(work_dir / "logs" / "decomposition.json").write_text(json.dumps(log, indent=2, ensure_ascii=False) + "\n")
|
| 498 |
|
| 499 |
+
summary_path = work_dir / "logs" / "parts_summary.csv"
|
| 500 |
+
with summary_path.open("w", newline="") as f:
|
| 501 |
+
writer = csv.DictWriter(
|
| 502 |
+
f,
|
| 503 |
+
fieldnames=[
|
| 504 |
+
"source_mesh",
|
| 505 |
+
"part_path",
|
| 506 |
+
"mesh_name",
|
| 507 |
+
"bbox_x",
|
| 508 |
+
"bbox_y",
|
| 509 |
+
"bbox_z",
|
| 510 |
+
"bbox_center_x",
|
| 511 |
+
"bbox_center_y",
|
| 512 |
+
"bbox_center_z",
|
| 513 |
+
"bbox_volume",
|
| 514 |
+
"mesh_volume",
|
| 515 |
+
"surface_area",
|
| 516 |
+
"num_vertices",
|
| 517 |
+
"num_faces",
|
| 518 |
+
"sha256",
|
| 519 |
+
],
|
| 520 |
+
)
|
| 521 |
+
writer.writeheader()
|
| 522 |
+
for rec in part_records:
|
| 523 |
+
bbox = rec["bbox_extents"]
|
| 524 |
+
center = rec["bbox_center"]
|
| 525 |
+
writer.writerow({
|
| 526 |
+
"source_mesh": rec["source_mesh"],
|
| 527 |
+
"part_path": rec["path"],
|
| 528 |
+
"mesh_name": rec["mesh_name"],
|
| 529 |
+
"bbox_x": bbox[0],
|
| 530 |
+
"bbox_y": bbox[1],
|
| 531 |
+
"bbox_z": bbox[2],
|
| 532 |
+
"bbox_center_x": center[0],
|
| 533 |
+
"bbox_center_y": center[1],
|
| 534 |
+
"bbox_center_z": center[2],
|
| 535 |
+
"bbox_volume": rec["bbox_volume"],
|
| 536 |
+
"mesh_volume": rec["mesh_volume"],
|
| 537 |
+
"surface_area": rec["surface_area"],
|
| 538 |
+
"num_vertices": rec["num_vertices"],
|
| 539 |
+
"num_faces": rec["num_faces"],
|
| 540 |
+
"sha256": rec["sha256"],
|
| 541 |
+
})
|
| 542 |
+
|
| 543 |
readme = f"""# Convex decomposition: {source_asset_id}
|
| 544 |
|
| 545 |
Source asset:
|