Skip to content

Commit 3eb881e

Browse files
authored
Refactor env launcher scripts (#126)
1 parent 4013ed1 commit 3eb881e

File tree

5 files changed

+187
-301
lines changed

5 files changed

+187
-301
lines changed

configs/gym/pour_water/gym_config_simple.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
"max_episodes": 5,
44
"env": {
55
"events": {
6+
"record_camera": {
7+
"func": "record_camera_data",
8+
"mode": "interval",
9+
"interval_step": 1,
10+
"params": {
11+
"name": "cam1",
12+
"resolution": [320, 240],
13+
"eye": [2, 0, 2],
14+
"target": [0.5, 0, 1]
15+
}
16+
},
617
"random_light": {
718
"func": "randomize_light",
819
"mode": "interval",

embodichain/lab/gym/utils/gym_utils.py

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import numpy as np
1818
import torch
1919
import dexsim
20+
import argparse
21+
import gymnasium
2022

2123
from typing import Dict, Any, List, Tuple, Union, Sequence
2224
from gymnasium import spaces
@@ -374,8 +376,8 @@ def config_to_cfg(config: dict, manager_modules: list = None) -> "EmbodiedEnvCfg
374376
ArticulationCfg,
375377
LightCfg,
376378
)
377-
from embodichain.lab.gym.envs import EmbodiedEnvCfg
378379
from embodichain.lab.sim.sensors import SensorCfg
380+
from embodichain.lab.gym.envs import EmbodiedEnvCfg
379381
from embodichain.lab.gym.envs.managers import (
380382
SceneEntityCfg,
381383
EventCfg,
@@ -701,3 +703,115 @@ def assign_data_to_dict(
701703

702704
last_key = keys[-1]
703705
current_data[last_key] = value
706+
707+
708+
def add_env_launcher_args_to_parser(parser: argparse.ArgumentParser) -> None:
709+
"""Add common environment launcher arguments to an existing argparse parser.
710+
711+
This function adds the following arguments to the provided parser:
712+
- --num_envs: Number of environments to run in parallel (default: 1)
713+
- --device: Device to run the environment on (default: 'cpu')
714+
- --headless: Whether to perform the simulation in headless mode (default: False)
715+
- --enable_rt: Whether to use RTX rendering backend for the simulation (default: False)
716+
- --gpu_id: The GPU ID to use for the simulation (default: 0)
717+
- --filter_visual_rand: Whether to filter out visual randomization (default: False)
718+
- --gym_config: Path to gym config file (default: '')
719+
- --action_config: Path to action config file (default: None)
720+
- --preview: Whether to preview the environment after launching (default: False)
721+
722+
Note:
723+
1. In preview mode, the environment will be launched and keep running in a loop for user interaction.
724+
725+
Args:
726+
parser (argparse.ArgumentParser): The parser to which arguments will be added.
727+
"""
728+
parser.add_argument(
729+
"--num_envs",
730+
help="The number of environments to run in parallel.",
731+
default=1,
732+
type=int,
733+
)
734+
parser.add_argument(
735+
"--device",
736+
type=str,
737+
default="cpu",
738+
help="Device to run the environment on, e.g., 'cpu' or 'cuda'.",
739+
)
740+
parser.add_argument(
741+
"--headless",
742+
help="Whether to perform the simulation in headless mode.",
743+
default=False,
744+
action="store_true",
745+
)
746+
parser.add_argument(
747+
"--enable_rt",
748+
help="Whether to use RTX rendering backend for the simulation.",
749+
default=False,
750+
action="store_true",
751+
)
752+
parser.add_argument(
753+
"--gpu_id",
754+
help="The GPU ID to use for the simulation.",
755+
default=0,
756+
type=int,
757+
)
758+
parser.add_argument(
759+
"--filter_visual_rand",
760+
help="Whether to filter out visual randomization.",
761+
default=False,
762+
action="store_true",
763+
)
764+
parser.add_argument(
765+
"--gym_config",
766+
type=str,
767+
help="Path to gym config file.",
768+
default="",
769+
required=True,
770+
)
771+
parser.add_argument(
772+
"--action_config", type=str, help="Path to action config file.", default=None
773+
)
774+
parser.add_argument(
775+
"--preview",
776+
help="Whether to preview the environment after launching.",
777+
default=False,
778+
action="store_true",
779+
)
780+
781+
782+
def build_env_cfg_from_args(
783+
args: argparse.Namespace,
784+
) -> tuple["EmbodiedEnvCfg", dict, dict]:
785+
"""Build environment configuration from command-line arguments.
786+
787+
Args:
788+
args (argparse.Namespace): The parsed command-line arguments.
789+
790+
Returns:
791+
tuple[EmbodiedEnvCfg, dict, dict]: A tuple containing the environment configuration object,
792+
the original gym configuration dictionary, and the action configuration dictionary.
793+
"""
794+
from embodichain.utils.utility import load_json
795+
from embodichain.lab.gym.envs import EmbodiedEnvCfg
796+
from embodichain.lab.sim import SimulationManagerCfg
797+
798+
gym_config = load_json(args.gym_config)
799+
cfg: EmbodiedEnvCfg = config_to_cfg(
800+
gym_config, manager_modules=DEFAULT_MANAGER_MODULES
801+
)
802+
cfg.filter_visual_rand = args.filter_visual_rand
803+
804+
action_config = {}
805+
if args.action_config is not None:
806+
action_config = load_json(args.action_config)
807+
action_config["action_config"] = action_config
808+
809+
cfg.num_envs = args.num_envs
810+
cfg.sim_cfg = SimulationManagerCfg(
811+
headless=args.headless,
812+
sim_device=args.device,
813+
enable_rt=args.enable_rt,
814+
gpu_id=args.gpu_id,
815+
)
816+
817+
return cfg, gym_config, action_config

embodichain/lab/scripts/preview_env.py

Lines changed: 0 additions & 143 deletions
This file was deleted.

0 commit comments

Comments
 (0)