Spaces:
Running
Running
Download scripts/build_ai2thor_scene_zip.py from ManiacWallnut/ManiTaskGen: direct link, hf CLI and curl.
- Browser
- Download file 5.54 kB
-
https://huggingface.co/spaces/ManiacWallnut/ManiTaskGen/resolve/main/scripts/build_ai2thor_scene_zip.py
- Command line
-
hf download hf://spaces/ManiacWallnut/ManiTaskGen/scripts/build_ai2thor_scene_zip.py
-
curl -L -o build_ai2thor_scene_zip.py https://huggingface.co/spaces/ManiacWallnut/ManiTaskGen/resolve/main/scripts/build_ai2thor_scene_zip.py
5.54 kB
| """ | |
| Build an uploadable scene pack (scene.zip) for an AI2THOR (HSSD) scene from the | |
| local ManiSkill-style dataset: | |
| data/datasets/ai2thor/ai2thor-hab # stages + scene configs | |
| data/datasets/ai2thor/ai2thorhab-uncompressed # uncompressed object GLBs + configs | |
| The pack uses the same layout as the demo's example_scene.zip / ReplicaCAD | |
| packs (which is what the demo's upload pipeline expects): | |
| <scene>.zip | |
| βββ configs/scenes/<scene>.scene_instance.json | |
| βββ configs/objects/<template>.object_config.json (render_asset rewritten) | |
| βββ objects/<template>.glb (uncompressed GLBs) | |
| βββ stages/iTHOR/<scene>.glb (from ai2thor-hab) | |
| Usage (from the repository root): | |
| python scripts/build_ai2thor_scene_zip.py --scene iTHOR/FloorPlan1_physics | |
| python scripts/build_ai2thor_scene_zip.py --list | |
| """ | |
| import argparse | |
| import json | |
| import os | |
| import sys | |
| import zipfile | |
| REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| HAB_ROOT = os.path.join(REPO_ROOT, "data", "datasets", "ai2thor", "ai2thor-hab") | |
| UNCOMP_ROOT = os.path.join( | |
| REPO_ROOT, "data", "datasets", "ai2thor", "ai2thorhab-uncompressed" | |
| ) | |
| SCENES_DIR = os.path.join(HAB_ROOT, "configs", "scenes") | |
| DEFAULT_OUT_DIR = os.path.join(REPO_ROOT, "output") | |
| def list_scenes(): | |
| found = [] | |
| for series in sorted(os.listdir(SCENES_DIR)): | |
| sdir = os.path.join(SCENES_DIR, series) | |
| if not os.path.isdir(sdir): | |
| continue | |
| for name in sorted(os.listdir(sdir)): | |
| if name.endswith(".scene_instance.json"): | |
| found.append(f"{series}/{name[: -len('.scene_instance.json')]}") | |
| return found | |
| def build(scene_id: str, out_path: str) -> None: | |
| entry_rel = f"configs/scenes/{scene_id}.scene_instance.json" | |
| entry_path = os.path.join(HAB_ROOT, entry_rel) | |
| if not os.path.isfile(entry_path): | |
| raise SystemExit(f"Scene instance not found: {entry_path}") | |
| with open(entry_path) as f: | |
| scene = json.load(f) | |
| stage_template = scene["stage_instance"]["template_name"] | |
| stage_glb_src = os.path.join(HAB_ROOT, "assets", f"{stage_template}.glb") | |
| if not os.path.isfile(stage_glb_src): | |
| raise SystemExit(f"Stage GLB not found: {stage_glb_src}") | |
| objects = scene.get("object_instances", []) | |
| print(f"scene {scene_id}: stage='{stage_template}', {len(objects)} objects") | |
| collected = {} # arcname -> src path | |
| missing = [] | |
| seen = set() | |
| for obj in objects: | |
| template = (obj.get("template_name") or "").split("/")[-1] | |
| if template in seen: | |
| continue | |
| seen.add(template) | |
| cfg_src = os.path.join( | |
| UNCOMP_ROOT, "configs", "objects", f"{template}.object_config.json" | |
| ) | |
| if not os.path.isfile(cfg_src): | |
| missing.append(f"configs/objects/{template}.object_config.json") | |
| continue | |
| with open(cfg_src) as f: | |
| cfg = json.load(f) | |
| asset = cfg.get("render_asset") | |
| if not asset: | |
| missing.append(f"{template}: no render_asset") | |
| continue | |
| glb_src = os.path.normpath( | |
| os.path.join(os.path.dirname(cfg_src), asset) | |
| ) | |
| if not os.path.isfile(glb_src): | |
| missing.append(f"{template}: GLB {asset}") | |
| continue | |
| collected[f"configs/objects/{template}.object_config.json"] = ("cfg", cfg_src) | |
| collected[f"objects/{template}.glb"] = ("glb", glb_src) | |
| if missing: | |
| print("Missing (skipped):") | |
| for m in missing[:15]: | |
| print(f" - {m}") | |
| if len(missing) > 15: | |
| print(f" ... and {len(missing) - 15} more") | |
| # zip layout: stage keeps its habitat-relative path under the pack root, | |
| # objects go under objects/, configs rewritten to ../../objects/<t>.glb | |
| os.makedirs(os.path.dirname(os.path.abspath(out_path)), exist_ok=True) | |
| with zipfile.ZipFile(out_path, "w", zipfile.ZIP_DEFLATED) as zf: | |
| zf.writestr(f"configs/scenes/{scene_id}.scene_instance.json", | |
| json.dumps(scene, indent=2)) | |
| zf.write(stage_glb_src, arcname=f"{stage_template}.glb") | |
| for arcname, (kind, src) in sorted(collected.items()): | |
| if kind == "glb": | |
| zf.write(src, arcname=arcname) | |
| else: | |
| template = os.path.basename(arcname)[: -len(".object_config.json")] | |
| cfg = json.load(open(src)) | |
| cfg["render_asset"] = f"../../objects/{template}.glb" | |
| cfg.pop("collision_asset", None) | |
| zf.writestr(arcname, json.dumps(cfg, indent=2)) | |
| size_mb = os.path.getsize(out_path) / 1e6 | |
| n = 1 + len(collected) // 2 | |
| print(f"Wrote {out_path} ({size_mb:.1f} MB, stage + {n} objects)") | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--scene", help="e.g. iTHOR/FloorPlan1_physics") | |
| parser.add_argument( | |
| "--out", default=None, | |
| help="output zip path (default output/ai2thor_<name>.zip)", | |
| ) | |
| parser.add_argument("--list", action="store_true", help="list local scenes") | |
| args = parser.parse_args() | |
| if args.list: | |
| for s in list_scenes(): | |
| print(s) | |
| return | |
| if not args.scene: | |
| parser.error("--scene or --list required") | |
| out = args.out or os.path.join( | |
| DEFAULT_OUT_DIR, f"ai2thor_{os.path.basename(args.scene)}.zip" | |
| ) | |
| build(args.scene, out) | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |