"""Procedural tabletop scenes for testing success verifiers (CPU, no physics needed). Each scenario places objects so that a task is either *completed* or a *near-miss* failure (object next to / on the rim of the target), renders an RGB image, and records the ground-truth label. This is a cheap stand-in for real rollout frames until GR00T/SO-101 rollouts exist. Rendering needs MUJOCO_GL=egl (set automatically below if unset). """ from __future__ import annotations import math import os import random from dataclasses import dataclass os.environ.setdefault("MUJOCO_GL", "egl") import mujoco # noqa: E402 import numpy as np # noqa: E402 COLORS = { "red": "0.85 0.1 0.1 1", "green": "0.1 0.7 0.2 1", "blue": "0.15 0.3 0.9 1", "yellow": "0.95 0.85 0.1 1", "purple": "0.55 0.2 0.7 1", "orange": "1 0.5 0.05 1", } CUBE = 0.025 # half-size (5 cm cube) BOWL_R, BOWL_H = 0.075, 0.04 def _bowl_xml(name: str, rgba: str) -> str: """Smooth round bowl: base disc + 48 overlapping wall segments + rim.""" walls = [] n = 48 half_w = BOWL_R * math.pi / n * 1.6 # >1 so neighbouring segments overlap (no gaps) for i in range(n): a = 2 * math.pi * i / n walls.append( f'') return (f'{"".join(walls)}') def _scene_xml(cubes: list[str], bowl: str | None) -> str: bodies = [f'' for c in cubes] if bowl: bodies.append(_bowl_xml(f"{bowl}_bowl", COLORS[bowl])) return f""" {''.join(bodies)} """ @dataclass class Scenario: task: str # natural-language instruction success: bool # ground truth kind: str # e.g. "in_bowl", "near_miss_rim" image: np.ndarray # HxWx3 uint8 (front camera) views: dict[str, np.ndarray] | None = None # camera name -> image, when multi-view CAMERAS = ("front", "left", "right", "back", "top") _CAMS: tuple[str, ...] = ("front",) # set by make_suite _RENDER = True # set by make_suite; False = layout only (fast, no GL context) _LOG: list | None = None # when a list, every scene's (xml, poses) is appended (used for videos) def _render(xml: str, poses: dict[str, tuple[float, float, float]], size: int): if _LOG is not None: _LOG.append((xml, dict(poses))) if not _RENDER: return {cam: np.zeros((size, size, 3), np.uint8) for cam in _CAMS} m = mujoco.MjModel.from_xml_string(xml) d = mujoco.MjData(m) for body, pos in poses.items(): d.mocap_pos[m.body_mocapid[m.body(body).id]] = pos mujoco.mj_forward(m, d) r = mujoco.Renderer(m, size, size) views = {} for cam in _CAMS: r.update_scene(d, camera=cam) views[cam] = r.render().copy() r.close() return views def _rand_xy(rng: random.Random, avoid: list[tuple[float, float]], min_d: float = 0.14): """Random table position at least min_d from `avoid`; relaxes min_d if the table is crowded.""" d = min_d while d > 0.09: for _ in range(200): p = (rng.uniform(-0.22, 0.22), rng.uniform(-0.12, 0.18)) if all(math.dist(p, a) > d for a in avoid): return p d *= 0.9 raise RuntimeError("could not place object") def put_in_bowl(rng: random.Random, success: bool, size: int = 448) -> Scenario: cube, bowl, distract = rng.sample(["red", "green", "yellow", "purple", "orange"], 2) + [None] bowl = rng.choice(["blue", "green", "purple"]) if bowl in ("red",) else bowl if bowl == cube: bowl = "blue" other = rng.choice([c for c in COLORS if c not in (cube, bowl)]) bxy = _rand_xy(rng, []) oxy = _rand_xy(rng, [bxy]) if success: r, a = rng.uniform(0, 0.03), rng.uniform(0, 2 * math.pi) cxy, kind = (bxy[0] + r * math.cos(a), bxy[1] + r * math.sin(a)), "in_bowl" cz = 0.008 + CUBE else: kind = rng.choice(["beside_bowl", "far_away", "other_object_in_bowl"]) a = rng.uniform(0, 2 * math.pi) if kind == "beside_bowl": cxy = (bxy[0] + 0.12 * math.cos(a), bxy[1] + 0.12 * math.sin(a)) elif kind == "far_away": cxy = _rand_xy(rng, [bxy, oxy], 0.2) else: # the distractor is in the bowl, the target cube is not cxy = _rand_xy(rng, [bxy, oxy], 0.16) oxy = bxy cz = CUBE oz = 0.008 + CUBE if oxy == bxy else CUBE xml = _scene_xml([cube, other], bowl) views = _render(xml, {f"{cube}_cube": (*cxy, cz), f"{other}_cube": (*oxy, oz), f"{bowl}_bowl": (*bxy, 0)}, size) return Scenario(f"put the {cube} cube in the {bowl} bowl", success, kind, views["front"], views) def stack(rng: random.Random, success: bool, size: int = 448) -> Scenario: top, base, other = rng.sample(["red", "green", "blue", "yellow", "orange"], 3) bxy = _rand_xy(rng, []) oxy = _rand_xy(rng, [bxy]) if success: txy, tz, kind = (bxy[0] + rng.uniform(-0.008, 0.008), bxy[1] + rng.uniform(-0.008, 0.008)), 3 * CUBE, "stacked" poses = {f"{top}_cube": (*txy, tz), f"{base}_cube": (*bxy, CUBE), f"{other}_cube": (*oxy, CUBE)} else: kind = rng.choice(["side_by_side", "wrong_base", "reversed"]) if kind == "side_by_side": a = rng.uniform(0, 2 * math.pi) poses = {f"{top}_cube": (bxy[0] + 0.06 * math.cos(a), bxy[1] + 0.06 * math.sin(a), CUBE), f"{base}_cube": (*bxy, CUBE), f"{other}_cube": (*oxy, CUBE)} elif kind == "wrong_base": # top cube is stacked, but on the wrong cube poses = {f"{top}_cube": (*oxy, 3 * CUBE), f"{other}_cube": (*oxy, CUBE), f"{base}_cube": (*bxy, CUBE)} else: # base is on top of top poses = {f"{base}_cube": (*bxy, 3 * CUBE), f"{top}_cube": (*bxy, CUBE), f"{other}_cube": (*oxy, CUBE)} views = _render(_scene_xml([top, base, other], None), poses, size) return Scenario(f"stack the {top} cube on the {base} cube", success, kind, views["front"], views) GENERATORS = {"put_in_bowl": put_in_bowl, "stack": stack} def make_suite(n_per_task: int = 10, seed: int = 0, size: int = 448, cameras: tuple[str, ...] = ("front",), render: bool = True) -> list[Scenario]: """Balanced suite: n_per_task scenarios per task family, half successes. Scene layouts depend only on `seed`, not on which cameras are rendered.""" global _CAMS, _RENDER _CAMS, _RENDER = tuple(cameras), render rng = random.Random(seed) out = [] for gen in GENERATORS.values(): for i in range(n_per_task): out.append(gen(rng, success=(i % 2 == 0), size=size)) return out def render_poses(xml: str, poses: dict, cameras=CAMERAS, size: int = 192) -> dict: """Render an arbitrary pose dict (e.g. one animation frame) from the given cameras.""" global _CAMS, _RENDER, _LOG saved = (_CAMS, _RENDER, _LOG) _CAMS, _RENDER, _LOG = tuple(cameras), True, None try: return _render(xml, poses, size) finally: _CAMS, _RENDER, _LOG = saved