File size: 2,749 Bytes
327ef01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/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 <scene_000_path> <name>    # 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()