|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import copy |
| 4 | +import importlib |
3 | 5 | import time |
4 | 6 | from dataclasses import dataclass |
5 | 7 | from pathlib import Path |
|
12 | 14 | import typer |
13 | 15 | from PIL import Image |
14 | 16 | from rcs.envs.base import ControlMode |
| 17 | +from rcs.envs.scenes import SimEnvCreator |
15 | 18 | from rcs.envs.sim import SimStateObservationWrapper |
16 | 19 |
|
17 | 20 | import rcs # noqa: F401 |
|
20 | 23 |
|
21 | 24 | DATASET_ARGUMENT = typer.Argument(..., exists=True, file_okay=False, dir_okay=True) |
22 | 25 |
|
23 | | -ENV_ID_OPTION = typer.Option(help="Gymnasium env id used for replay.") |
| 26 | +SCENE_CONFIG_OPTION = typer.Option( |
| 27 | + help="Scene creator class name or import path used to build the replay env, for example EmptyWorldFR3." |
| 28 | +) |
24 | 29 | TRAJECTORY_UUID_OPTION = typer.Option(help="UUID of the recorded trajectory to replay.") |
25 | | -CAMERA_OPTION = typer.Option("--camera", help="Camera names to enable on the replay env.") |
26 | | -RESOLUTION_OPTION = typer.Option(help="Replay camera resolution as WIDTH HEIGHT.") |
27 | | -FRAME_RATE_OPTION = typer.Option(help="Replay camera frame rate.") |
28 | | -RENDER_MODE_OPTION = typer.Option(help="Gym render mode for the replay env.") |
29 | | -CONTROL_MODE_OPTION = typer.Option(help="Control mode name for env creation.") |
| 30 | +CAMERA_OPTION = typer.Option("--camera", help="Optional camera names to enable on the replay env.") |
| 31 | +RESOLUTION_OPTION = typer.Option(help="Optional replay camera resolution as WIDTH HEIGHT.") |
| 32 | +FRAME_RATE_OPTION = typer.Option(help="Optional replay camera frame rate override.") |
| 33 | +RENDER_MODE_OPTION = typer.Option( |
| 34 | + help="Replay viewer mode. Use 'human' to open the GUI, anything else keeps it headless." |
| 35 | +) |
| 36 | +CONTROL_MODE_OPTION = typer.Option(help="Optional control mode name override for env creation.") |
30 | 37 | SLEEP_OPTION = typer.Option(help="Optional delay between restored states.") |
31 | 38 | OUTPUT_DIR_OPTION = typer.Option(help="Optional directory for re-rendered RGB frames.") |
32 | 39 | PREFER_DUCKDB_OPTION = typer.Option(help="Use duckdb for parquet loading when it is available.") |
@@ -202,30 +209,89 @@ def replay_trajectory( |
202 | 209 | time.sleep(sleep_s) |
203 | 210 |
|
204 | 211 |
|
| 212 | +def resolve_scene_creator(scene_config: str) -> SimEnvCreator[Any]: |
| 213 | + module_name, _, class_name = scene_config.rpartition(".") |
| 214 | + if not module_name: |
| 215 | + module_name = "rcs.envs.configs" |
| 216 | + class_name = scene_config |
| 217 | + module = importlib.import_module(module_name) |
| 218 | + scene_creator_cls = getattr(module, class_name, None) |
| 219 | + if scene_creator_cls is None: |
| 220 | + msg = f"Could not find scene creator '{class_name}' in module '{module_name}'." |
| 221 | + raise ValueError(msg) |
| 222 | + scene_creator = scene_creator_cls() |
| 223 | + if not isinstance(scene_creator, SimEnvCreator): |
| 224 | + msg = f"{module_name}.{class_name} is not an RCS SimEnvCreator." |
| 225 | + raise TypeError(msg) |
| 226 | + return scene_creator |
| 227 | + |
| 228 | + |
| 229 | +def create_replay_env( |
| 230 | + scene_config: str, |
| 231 | + *, |
| 232 | + camera: list[str] | None = None, |
| 233 | + resolution: tuple[int, int] | None = None, |
| 234 | + frame_rate: int | None = None, |
| 235 | + render_mode: str = "human", |
| 236 | + control_mode: str | None = None, |
| 237 | +) -> gym.Env: |
| 238 | + scene_creator = resolve_scene_creator(scene_config) |
| 239 | + cfg = scene_creator.config() |
| 240 | + |
| 241 | + if control_mode is not None: |
| 242 | + cfg.control_mode = ControlMode[control_mode] |
| 243 | + cfg.sim_cfg.async_control = False |
| 244 | + cfg.sim_cfg.realtime = False |
| 245 | + cfg.headless = render_mode != "human" |
| 246 | + |
| 247 | + if camera is not None: |
| 248 | + if cfg.camera_cfgs is None: |
| 249 | + msg = f"Scene config '{scene_config}' does not define any cameras to replay." |
| 250 | + raise ValueError(msg) |
| 251 | + missing_cameras = [name for name in camera if name not in cfg.camera_cfgs] |
| 252 | + if missing_cameras: |
| 253 | + available_cameras = sorted(cfg.camera_cfgs) |
| 254 | + msg = ( |
| 255 | + f"Unknown replay cameras {missing_cameras} for scene config '{scene_config}'. " |
| 256 | + f"Available cameras are {available_cameras}." |
| 257 | + ) |
| 258 | + raise ValueError(msg) |
| 259 | + cfg.camera_cfgs = {name: copy.deepcopy(cfg.camera_cfgs[name]) for name in camera} |
| 260 | + elif cfg.camera_cfgs is not None: |
| 261 | + cfg.camera_cfgs = {name: copy.deepcopy(camera_cfg) for name, camera_cfg in cfg.camera_cfgs.items()} |
| 262 | + |
| 263 | + if cfg.camera_cfgs is not None: |
| 264 | + for camera_cfg in cfg.camera_cfgs.values(): |
| 265 | + if resolution is not None: |
| 266 | + camera_cfg.resolution_width, camera_cfg.resolution_height = resolution |
| 267 | + if frame_rate is not None: |
| 268 | + camera_cfg.frame_rate = frame_rate |
| 269 | + |
| 270 | + return scene_creator.create_env(cfg) |
| 271 | + |
| 272 | + |
205 | 273 | @app.command() |
206 | 274 | def replay( |
207 | 275 | dataset: Annotated[Path, DATASET_ARGUMENT], |
208 | | - env_id: Annotated[str, ENV_ID_OPTION] = "rcs/FR3SimplePickUpSim-v0", |
| 276 | + scene_config: Annotated[str, SCENE_CONFIG_OPTION] = "EmptyWorldFR3", |
209 | 277 | trajectory_uuid: Annotated[str | None, TRAJECTORY_UUID_OPTION] = None, |
210 | 278 | camera: Annotated[list[str] | None, CAMERA_OPTION] = None, |
211 | | - resolution: Annotated[tuple[int, int], RESOLUTION_OPTION] = (256, 256), |
212 | | - frame_rate: Annotated[int, FRAME_RATE_OPTION] = 0, |
| 279 | + resolution: Annotated[tuple[int, int] | None, RESOLUTION_OPTION] = None, |
| 280 | + frame_rate: Annotated[int | None, FRAME_RATE_OPTION] = None, |
213 | 281 | render_mode: Annotated[str, RENDER_MODE_OPTION] = "human", |
214 | | - control_mode: Annotated[str, CONTROL_MODE_OPTION] = ControlMode.CARTESIAN_TRPY.name, |
| 282 | + control_mode: Annotated[str | None, CONTROL_MODE_OPTION] = None, |
215 | 283 | sleep_s: Annotated[float, SLEEP_OPTION] = 0.0, |
216 | 284 | output_dir: Annotated[Path | None, OUTPUT_DIR_OPTION] = None, |
217 | 285 | prefer_duckdb: Annotated[bool, PREFER_DUCKDB_OPTION] = True, |
218 | 286 | ): |
219 | | - if camera is None: |
220 | | - camera = [] |
221 | 287 | resolved_uuid = resolve_trajectory_uuid(dataset, trajectory_uuid, prefer_duckdb=prefer_duckdb) |
222 | | - env = gym.make( |
223 | | - env_id, |
224 | | - render_mode=render_mode, |
225 | | - control_mode=ControlMode[control_mode], |
| 288 | + env = create_replay_env( |
| 289 | + scene_config, |
| 290 | + camera=camera, |
226 | 291 | resolution=resolution, |
227 | 292 | frame_rate=frame_rate, |
228 | | - cam_list=camera, |
| 293 | + render_mode=render_mode, |
| 294 | + control_mode=control_mode, |
229 | 295 | ) |
230 | 296 | try: |
231 | 297 | recorded_steps = load_trajectory(dataset, resolved_uuid, prefer_duckdb=prefer_duckdb) |
|
0 commit comments