Spaces:
Running
Running
Download demo/build_example_scene.py from ManiacWallnut/ManiTaskGen: direct link, hf CLI and curl.
- Browser
- Download file 12.8 kB
-
https://huggingface.co/spaces/ManiacWallnut/ManiTaskGen/resolve/main/demo/build_example_scene.py
- Command line
-
hf download hf://spaces/ManiacWallnut/ManiTaskGen/demo/build_example_scene.py
-
curl -L -o build_example_scene.py https://huggingface.co/spaces/ManiacWallnut/ManiTaskGen/resolve/main/demo/build_example_scene.py
12.8 kB
| """ | |
| Build the built-in example scene pack (`example_scene.zip`) for the | |
| ManiTaskGen Gradio demo. | |
| The pack mirrors the on-disk layout of a Habitat/ReplicaCAD dataset root, | |
| which is exactly what `ReplicaSceneParser` (src/preprocessing/maniskill_parser.py) | |
| consumes: | |
| example_scene.zip | |
| βββ configs/scenes/demo_studio.scene_instance.json # entry scene file | |
| βββ configs/objects/<template>.object_config.json # render asset config | |
| βββ objects/<template>.glb # object meshes | |
| βββ stages/demo_room.glb # background stage mesh | |
| All geometry is generated here from primitive boxes/cylinders, so the pack is | |
| tiny (<100 KB) and freely redistributable (no third-party assets, no license | |
| restrictions). | |
| Mesh up-axis note: the scene-graph pipeline converts loaded GLB geometry from | |
| Y-up to Z-up (`create_object_list` in scene_parser.py applies a fixed | |
| transform). We therefore design furniture in engine coordinates (Z-up) and | |
| pre-convert vertices to Y-up when saving, so objects stand upright after the | |
| pipeline's fixed transform. | |
| Run from the repo root: | |
| python demo/build_example_scene.py | |
| """ | |
| import json | |
| import os | |
| import sys | |
| import zipfile | |
| import numpy as np | |
| import trimesh | |
| REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| PACK_DIR = os.path.join(REPO_ROOT, "demo", "assets", "example_scene") | |
| ZIP_PATH = os.path.join(REPO_ROOT, "demo", "assets", "example_scene.zip") | |
| def eng_to_glb(vertices: np.ndarray) -> np.ndarray: | |
| """Engine coords (Z-up) -> GLB coords (Y-up), inverse of the pipeline's | |
| fixed Y-up->Z-up transform (new_x=x, new_y=-z, new_z=y).""" | |
| v = np.asarray(vertices, dtype=np.float64) | |
| out = np.empty_like(v) | |
| out[:, 0] = v[:, 0] | |
| out[:, 1] = v[:, 2] # glb Y (up) = engine Z (up) | |
| out[:, 2] = -v[:, 1] # glb Z = -engine Y | |
| return out | |
| def save_glb(mesh: trimesh.Trimesh, rel_path: str) -> None: | |
| path = os.path.join(PACK_DIR, rel_path) | |
| os.makedirs(os.path.dirname(path), exist_ok=True) | |
| mesh.vertices = eng_to_glb(mesh.vertices) | |
| mesh.export(path) | |
| print(f" wrote {rel_path} ({os.path.getsize(path)} bytes)") | |
| def box(length: float, width: float, height: float) -> trimesh.Trimesh: | |
| return trimesh.creation.box(extents=[length, width, height]) | |
| def box_at(center_eng, length, width, height) -> trimesh.Trimesh: | |
| m = box(length, width, height) | |
| m.apply_translation(np.asarray(center_eng, dtype=np.float64)) | |
| return m | |
| def cyl_at(center_eng, radius, height, sections=32) -> trimesh.Trimesh: | |
| m = trimesh.creation.cylinder(radius=radius, height=height, sections=sections) | |
| m.apply_translation(np.asarray(center_eng, dtype=np.float64)) | |
| return m | |
| def concat(meshes) -> trimesh.Trimesh: | |
| return trimesh.util.concatenate(list(meshes)) | |
| def build_stage() -> None: | |
| """A single floor slab, 8m x 8m, top surface at engine z = 0.""" | |
| slab = box_at([0.0, 0.0, -0.05], 8.0, 8.0, 0.10) | |
| save_glb(slab, "stages/demo_room.glb") | |
| # --------------------------------------------------------------------------- | |
| # Furniture (origin at bottom-center, engine z = 0 when placed on the floor) | |
| # --------------------------------------------------------------------------- | |
| def build_desk() -> None: | |
| """Desk: top surface at 0.75m, 1.2m x 0.7m.""" | |
| parts = [] | |
| top_z = 0.75 | |
| t = 0.04 | |
| parts.append(box_at([0, 0, top_z - t / 2], 1.2, 0.7, t)) | |
| leg_h = top_z - t | |
| for sx in (-1, 1): | |
| for sy in (-1, 1): | |
| parts.append( | |
| box_at([sx * 0.55, sy * 0.30, leg_h / 2], 0.05, 0.05, leg_h) | |
| ) | |
| save_glb(concat(parts), "objects/desk.glb") | |
| def build_table() -> None: | |
| """Low coffee table: top surface at 0.45m, 0.8m x 0.5m.""" | |
| parts = [] | |
| top_z = 0.45 | |
| t = 0.03 | |
| parts.append(box_at([0, 0, top_z - t / 2], 0.8, 0.5, t)) | |
| leg_h = top_z - t | |
| for sx in (-1, 1): | |
| for sy in (-1, 1): | |
| parts.append( | |
| box_at([sx * 0.35, sy * 0.20, leg_h / 2], 0.04, 0.04, leg_h) | |
| ) | |
| save_glb(concat(parts), "objects/table.glb") | |
| def build_shelf() -> None: | |
| """Bookshelf with 3 boards: a multilayer object. | |
| Board top surfaces at 0.30m / 0.75m / 1.20m, each 0.8m x 0.3m. | |
| """ | |
| parts = [] | |
| w, d = 0.8, 0.3 | |
| board_t = 0.03 | |
| board_tops = [0.30, 0.75, 1.20] | |
| side_h = board_tops[-1] + 0.02 | |
| for sy in (-1, 1): | |
| parts.append(box_at([0, sy * (d / 2 - 0.01), side_h / 2], w, 0.02, side_h)) | |
| for z_top in board_tops: | |
| parts.append(box_at([0, 0, z_top - board_t / 2], w, d, board_t)) | |
| save_glb(concat(parts), "objects/shelf.glb") | |
| def build_counter() -> None: | |
| """Solid counter block: 1.0m x 0.6m, top surface at 0.9m.""" | |
| save_glb(box_at([0, 0, 0.45], 1.0, 0.6, 0.9), "objects/counter.glb") | |
| # --------------------------------------------------------------------------- | |
| # Small items (origin at bottom-center; placed with centroid z = surface height) | |
| # --------------------------------------------------------------------------- | |
| def build_small_items() -> None: | |
| save_glb( | |
| box_at([0, 0, 0.011], 0.32, 0.22, 0.022), "objects/laptop.glb" | |
| ) | |
| # books | |
| save_glb(box_at([0, 0, 0.015], 0.21, 0.15, 0.03), "objects/book_red.glb") | |
| save_glb(box_at([0, 0, 0.015], 0.21, 0.15, 0.03), "objects/book_blue.glb") | |
| save_glb(box_at([0, 0, 0.015], 0.21, 0.15, 0.03), "objects/book_green.glb") | |
| # mug: cylinder, r=0.04, h=0.10 (z from 0 to 0.10) | |
| save_glb(cyl_at([0, 0, 0.05], 0.04, 0.10), "objects/mug.glb") | |
| # plant pot: r=0.07, h=0.14 | |
| save_glb(cyl_at([0, 0, 0.07], 0.07, 0.14), "objects/plant_pot.glb") | |
| # cereal box | |
| save_glb(box_at([0, 0, 0.15], 0.20, 0.08, 0.30), "objects/cereal_box.glb") | |
| # bowl: r=0.09, h=0.07 | |
| save_glb(cyl_at([0, 0, 0.035], 0.09, 0.07), "objects/bowl.glb") | |
| # remote | |
| save_glb(box_at([0, 0, 0.01], 0.15, 0.06, 0.02), "objects/remote.glb") | |
| # toaster | |
| save_glb(box_at([0, 0, 0.095], 0.26, 0.18, 0.19), "objects/toaster.glb") | |
| def write_object_configs() -> None: | |
| cfg_dir = os.path.join(PACK_DIR, "configs", "objects") | |
| os.makedirs(cfg_dir, exist_ok=True) | |
| for template in [ | |
| "desk", "table", "shelf", "counter", "laptop", | |
| "book_red", "book_blue", "book_green", "mug", | |
| "plant_pot", "cereal_box", "bowl", "remote", "toaster", | |
| ]: | |
| cfg = { | |
| "render_asset": f"../../objects/{template}.glb", | |
| "mass": 0.5, | |
| "COM": [0, 0, 0], | |
| "join_collision_meshes": False, | |
| } | |
| with open(os.path.join(cfg_dir, f"{template}.object_config.json"), "w") as f: | |
| json.dump(cfg, f, indent=2) | |
| def q_identity(): | |
| return [1.0, 0.0, 0.0, 0.0] # [w, x, y, z] | |
| def eng_to_hab(x, y, z): | |
| """Engine (Z-up) -> habitat (Y-up): hab = (x, z, -y).""" | |
| return [float(x), float(z), float(-y)] | |
| def obj(template: str, motion: str, eng_pos, eng_rot_z_deg: float = 0.0) -> dict: | |
| """Instance entry. eng_pos is the object-origin position in engine coords. | |
| The parser rotates habitat-space quaternions by +90Β° about X (then removes | |
| 90Β° from the euler x component), so a yaw rotation of `a` about engine Z | |
| corresponds to habitat quaternion (cosa/2, 0, 0, sina/2) -> corrected | |
| identity... We keep everything axis-aligned (yaw=0) for determinism. | |
| """ | |
| del eng_rot_z_deg | |
| return { | |
| "template_name": f"objects/{template}", | |
| "motion_type": motion, | |
| "translation": eng_to_hab(*eng_pos), | |
| "rotation": q_identity(), | |
| } | |
| def write_scene_instance() -> None: | |
| """Place furniture and items. Engine coords: +Z up. | |
| Desk at (-1.5, -1.0), table at (0, 0.9), shelf at (1.6, -1.2), | |
| counter at (2.0, 1.5). | |
| """ | |
| instances = [] | |
| desk_pos = (-1.5, -1.0, 0.0) | |
| desk_top = 0.75 | |
| table_pos = (0.0, 0.9, 0.0) | |
| table_top = 0.45 | |
| shelf_pos = (1.6, -1.2, 0.0) | |
| shelf_board_tops = [0.30, 0.75, 1.20] | |
| counter_pos = (2.0, 1.5, 0.0) | |
| counter_top = 0.9 | |
| # furniture | |
| instances.append(obj("desk", "STATIC", desk_pos)) | |
| instances.append(obj("table", "STATIC", table_pos)) | |
| instances.append(obj("shelf", "STATIC", shelf_pos)) | |
| instances.append(obj("counter", "STATIC", counter_pos)) | |
| # items on the desk (top at 0.75) | |
| instances.append(obj("laptop", "DYNAMIC", (desk_pos[0] - 0.25, desk_pos[1] + 0.0, desk_top))) | |
| instances.append(obj("book_red", "DYNAMIC", (desk_pos[0] + 0.30, desk_pos[1] - 0.15, desk_top))) | |
| instances.append(obj("book_red", "DYNAMIC", (desk_pos[0] + 0.30, desk_pos[1] + 0.15, desk_top))) | |
| instances.append(obj("mug", "DYNAMIC", (desk_pos[0] + 0.05, desk_pos[1] + 0.20, desk_top))) | |
| # items on the low table (top at 0.45) | |
| instances.append(obj("bowl", "DYNAMIC", (table_pos[0] - 0.15, table_pos[1], table_top))) | |
| instances.append(obj("remote", "DYNAMIC", (table_pos[0] + 0.20, table_pos[1] + 0.10, table_top))) | |
| # items on the shelf boards | |
| instances.append(obj("book_green", "DYNAMIC", (shelf_pos[0] - 0.20, shelf_pos[1], shelf_board_tops[0]))) | |
| instances.append(obj("cereal_box", "DYNAMIC", (shelf_pos[0] + 0.20, shelf_pos[1], shelf_board_tops[0]))) | |
| instances.append(obj("book_blue", "DYNAMIC", (shelf_pos[0] - 0.15, shelf_pos[1], shelf_board_tops[1]))) | |
| instances.append(obj("plant_pot", "DYNAMIC", (shelf_pos[0] + 0.20, shelf_pos[1], shelf_board_tops[1]))) | |
| instances.append(obj("mug", "DYNAMIC", (shelf_pos[0], shelf_pos[1], shelf_board_tops[2]))) | |
| # items on the counter (top at 0.9) | |
| instances.append(obj("toaster", "DYNAMIC", (counter_pos[0] - 0.20, counter_pos[1], counter_top))) | |
| instances.append(obj("book_red", "DYNAMIC", (counter_pos[0] + 0.30, counter_pos[1], counter_top))) | |
| scene = { | |
| "stage_instance": {"template_name": "stages/demo_room"}, | |
| "object_instances": instances, | |
| } | |
| path = os.path.join(PACK_DIR, "configs", "scenes", "demo_studio.scene_instance.json") | |
| os.makedirs(os.path.dirname(path), exist_ok=True) | |
| with open(path, "w") as f: | |
| json.dump(scene, f, indent=2) | |
| print(f" wrote scene instance with {len(instances)} objects") | |
| PACK_README = """# Demo Studio scene pack | |
| ManiTaskGen scene pack (Habitat/ReplicaCAD-style layout, primitive geometry, | |
| freely redistributable). | |
| - Entry scene file: `configs/scenes/demo_studio.scene_instance.json` | |
| - Stage/background mesh: `stages/demo_room.glb` (8m x 8m floor slab) | |
| - Object meshes: `objects/<template>.glb` | |
| - Per-template configs: `configs/objects/<template>.object_config.json` | |
| ## Coordinates | |
| Positions in `scene_instance.json` follow the Habitat convention (Y-up, | |
| rotation = quaternion [w, x, y, z]). Meshes are stored Y-up, matching | |
| ReplicaCAD assets. The engine pipeline converts both to Z-up internally. | |
| ## Structure | |
| | Template | Kind | Placed at (engine x, y) | Notes | | |
| | --- | --- | --- | --- | | |
| | desk | table | (-1.5, -1.0) | top surface 0.75 m | | |
| | table | low table | (0.0, 0.9) | top surface 0.45 m | | |
| | shelf | bookshelf (3 boards) | (1.6, -1.2) | multilayer: 0.30/0.75/1.20 m | | |
| | counter | solid block | (2.0, 1.5) | top surface 0.90 m | | |
| Small items (laptop, book_red x3, book_blue, book_green, mug x2, bowl, | |
| remote, cereal_box, plant_pot, toaster) are placed on top of the furniture. | |
| ## How to modify and re-upload | |
| 1. Unzip this archive. | |
| 2. Move / remove / rename an object: | |
| - edit its `translation` (Habitat coords) or delete its entry in | |
| `configs/scenes/demo_studio.scene_instance.json`; | |
| - if you delete a template, also delete `objects/<template>.glb` and | |
| `configs/objects/<template>.object_config.json`. | |
| 3. Re-zip the folder (keep the layout above) and upload the ZIP to the demo. | |
| Tasks referencing a removed object will disappear; the remaining results | |
| always reflect the uploaded scene. | |
| """ | |
| def write_pack_readme() -> None: | |
| with open(os.path.join(PACK_DIR, "README.md"), "w") as f: | |
| f.write(PACK_README) | |
| def main() -> None: | |
| print(f"Building example scene pack in {PACK_DIR}") | |
| os.makedirs(PACK_DIR, exist_ok=True) | |
| build_stage() | |
| build_desk() | |
| build_table() | |
| build_shelf() | |
| build_counter() | |
| build_small_items() | |
| write_object_configs() | |
| write_scene_instance() | |
| write_pack_readme() | |
| # zip it up (store small files with deflate) | |
| if os.path.exists(ZIP_PATH): | |
| os.remove(ZIP_PATH) | |
| with zipfile.ZipFile(ZIP_PATH, "w", zipfile.ZIP_DEFLATED) as zf: | |
| for root, _, files in os.walk(PACK_DIR): | |
| for name in sorted(files): | |
| full = os.path.join(root, name) | |
| rel = os.path.relpath(full, PACK_DIR) | |
| zf.write(full, arcname=rel) | |
| size = os.path.getsize(ZIP_PATH) | |
| print(f"Wrote {ZIP_PATH} ({size} bytes)") | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |