File size: 4,205 Bytes
9ee6f68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""
Build a `scene.zip` pack for ReplicaCAD `apt_0` from the local dataset
downloaded by `scripts/download_habitat_datasets.py`.

The resulting pack can be uploaded to the ManiTaskGen demo exactly like the
built-in `example_scene.zip`. The pack is NOT shipped in the repository
(~80 MB, ReplicaCAD assets are CC-BY-NC 4.0 by Meta).

Usage (from the repository root, after downloading the dataset):
    python scripts/build_apt0_scene_zip.py [--out output/apt_0_scene.zip]
"""

import argparse
import json
import os
import sys
import zipfile

REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATASET_ROOT = os.path.join(REPO_ROOT, "data", "datasets", "replica_cad_dataset")
SCENE_INSTANCE = os.path.join(
    DATASET_ROOT, "configs", "scenes", "apt_0.scene_instance.json"
)

# Objects excluded by config/default_config.yml (keep the pack consistent with
# the paper's default preprocessing).
NOT_DESIRED_OBJECTS = {
    "frl_apartment_handbag", "frl_apartment_cushion_01",
    "frl_apartment_monitor", "frl_apartment_cloth_01",
    "frl_apartment_cloth_02", "frl_apartment_cloth_03",
    "frl_apartment_cloth", "frl_apartment_umbrella",
    "frl_apartment_tv_screen", "frl_apartment_indoor_plant_01",
    "frl_apartment_monitor_stand", "frl_apartment_setupbox",
    "frl_apartment_beanbag", "frl_apartment_bike_01",
    "frl_apartment_bike_02", "frl_apartment_indoor_plant_02",
    "frl_apartment_picture_01", "frl_apartment_towel",
    "frl_apartment_rug_01", "frl_apartment_rug_02",
    "frl_apartment_rug_03", "frl_apartment_mat", "frl_apartment_tv_object",
}


def main(out_path: str) -> None:
    with open(SCENE_INSTANCE) as f:
        scene = json.load(f)

    scene["object_instances"] = [
        o
        for o in scene.get("object_instances", [])
        if o.get("template_name", "").split("/")[-1] not in NOT_DESIRED_OBJECTS
    ]
    stage_template = scene["stage_instance"]["template_name"]

    collected = set()
    missing = []

    def add(path: str) -> None:
        rel = os.path.relpath(path, DATASET_ROOT)
        if rel in collected:
            return
        if os.path.isfile(path):
            collected.add(rel)
        else:
            missing.append(rel)

    add(os.path.join(DATASET_ROOT, f"{stage_template}.glb"))

    for obj in scene["object_instances"]:
        template = obj["template_name"].split("/")[-1]
        cfg_path = os.path.join(
            DATASET_ROOT, "configs", "objects", f"{template}.object_config.json"
        )
        if not os.path.isfile(cfg_path):
            missing.append(f"configs/objects/{template}.object_config.json")
            continue
        add(cfg_path)
        with open(cfg_path) as f:
            cfg = json.load(f)
        for key in ("render_asset", "collision_asset"):
            asset = cfg.get(key)
            if asset:
                add(os.path.normpath(os.path.join(os.path.dirname(cfg_path), asset)))

    for art in scene.get("articulated_object_instances", []):
        base = os.path.basename(art.get("template_name", ""))
        urdf_dir = os.path.join(DATASET_ROOT, "urdf", base)
        if os.path.isdir(urdf_dir):
            for root, _, files in os.walk(urdf_dir):
                for name in files:
                    add(os.path.join(root, name))

    if missing:
        print("Missing files (not in dataset?):")
        for m in missing[:20]:
            print(f"  - {m}")
        sys.exit(1)

    os.makedirs(os.path.dirname(os.path.abspath(out_path)), exist_ok=True)
    with open(SCENE_INSTANCE) as f:
        scene_json = json.dumps(scene, indent=2)
    with zipfile.ZipFile(out_path, "w", zipfile.ZIP_DEFLATED) as zf:
        zf.writestr(
            "configs/scenes/apt_0.scene_instance.json", scene_json
        )
        for rel in sorted(collected):
            zf.write(os.path.join(DATASET_ROOT, rel), arcname=rel)

    size_mb = os.path.getsize(out_path) / 1e6
    print(f"Wrote {out_path} ({size_mb:.1f} MB, {len(collected)} files)")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--out", default=os.path.join(REPO_ROOT, "output", "apt_0_scene.zip")
    )
    args = parser.parse_args()
    main(args.out)