|
17 | 17 | import numpy as np |
18 | 18 | import torch |
19 | 19 | import dexsim |
| 20 | +import argparse |
| 21 | +import gymnasium |
20 | 22 |
|
21 | 23 | from typing import Dict, Any, List, Tuple, Union, Sequence |
22 | 24 | from gymnasium import spaces |
@@ -374,8 +376,8 @@ def config_to_cfg(config: dict, manager_modules: list = None) -> "EmbodiedEnvCfg |
374 | 376 | ArticulationCfg, |
375 | 377 | LightCfg, |
376 | 378 | ) |
377 | | - from embodichain.lab.gym.envs import EmbodiedEnvCfg |
378 | 379 | from embodichain.lab.sim.sensors import SensorCfg |
| 380 | + from embodichain.lab.gym.envs import EmbodiedEnvCfg |
379 | 381 | from embodichain.lab.gym.envs.managers import ( |
380 | 382 | SceneEntityCfg, |
381 | 383 | EventCfg, |
@@ -701,3 +703,115 @@ def assign_data_to_dict( |
701 | 703 |
|
702 | 704 | last_key = keys[-1] |
703 | 705 | 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 |
0 commit comments