#!/usr/bin/env python3 """Batch export scenesmith outputs -> self-contained MuJoCo scene dirs. Mirrors export_full.py (house_state build + export_scene_to_mujoco.py) for every scene that batch generation produced. Runs inside the scenesmith docker image so pydrake/mujoco/trimesh are available. Usage (on server, scenesmith conda env): python export_batch.py # all scenes in batch_logs python export_batch.py # single scene """ import json import subprocess import sys from pathlib import Path REPO = Path("/home/zhanghao/scene/scenesmith_full") LOG_DIR = Path("/home/zhanghao/scene/batch_logs") def export_one(scene_dir: Path, name: str) -> str: # The scene generator already wrote scene_000/combined_house/house_state.json # with all asset paths relative to scene_000 - pass scene_000 as the scene # path so every reference (SDF, floor gltf, furniture assets) resolves. scene_dir = Path(scene_dir) if not scene_dir.exists(): return "dir-missing" s000 = scene_dir / "scene_000" if not (s000 / "combined_house" / "house_state.json").exists(): return "no-house-state" out_name = f"mujoco_scene_{name}" rel = s000.relative_to(REPO / "outputs") cmd = [ "docker", "run", "--rm", "--network", "host", "-v", f"{REPO}:/app", "scenesmith:latest", "python", "scripts/export_scene_to_mujoco.py", f"/app/outputs/{rel}", "--output", f"/app/outputs/{out_name}", ] print(f"[{name}] exporting scene_000 -> outputs/{out_name} ...") proc = subprocess.run(cmd, capture_output=True, text=True, timeout=2400) combined_out = proc.stdout + proc.stderr out_dir = REPO / "outputs" / out_name ok = (out_dir / "scene.xml").exists() and (out_dir / "meshes").is_dir() if not ok: print(combined_out[-1800:]) return "OK" if ok else f"FAIL rc={proc.returncode}" def main(): if len(sys.argv) >= 3: print(f"[{sys.argv[2]}] -> {export_one(Path(sys.argv[1]), sys.argv[2])}") return names = sorted(p.stem for p in LOG_DIR.glob("home_*.dir")) print(f"{len(names)} scenes from batch_logs: {names}") results = {} for name in names: dirfile = LOG_DIR / f"{name}.dir" scene_dir = Path(dirfile.read_text().strip()) if not scene_dir.exists() or "FAILED" in dirfile.read_text(): results[name] = "skipped(failed)" print(f"[{name}] -> {results[name]}") continue results[name] = export_one(scene_dir, name) print(f"[{name}] -> {results[name]}") print("\n=== SUMMARY ===") for n, r in results.items(): print(f" {n}: {r}") if __name__ == "__main__": main()