Creating a Modular Environment#

This tutorial demonstrates how to create sophisticated robotic environments using EmbodiChain’s modular architecture. You’ll learn how to use the advanced envs.EmbodiedEnv class with configuration-driven setup, event managers, observation managers, and randomization systems.

The Code#

The tutorial corresponds to the modular_env.py script in the scripts/tutorials/gym directory.

Code for modular_env.py
  1# ----------------------------------------------------------------------------
  2# Copyright (c) 2021-2026 DexForce Technology Co., Ltd.
  3#
  4# Licensed under the Apache License, Version 2.0 (the "License");
  5# you may not use this file except in compliance with the License.
  6# You may obtain a copy of the License at
  7#
  8#     http://www.apache.org/licenses/LICENSE-2.0
  9#
 10# Unless required by applicable law or agreed to in writing, software
 11# distributed under the License is distributed on an "AS IS" BASIS,
 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13# See the License for the specific language governing permissions and
 14# limitations under the License.
 15# ----------------------------------------------------------------------------
 16
 17from __future__ import annotations
 18
 19import torch
 20
 21from typing import List, Dict, Any
 22
 23import embodichain.lab.gym.envs.managers.randomization as rand
 24import embodichain.lab.gym.envs.managers.events as events
 25import embodichain.lab.gym.envs.managers.observations as obs
 26
 27from embodichain.lab.gym.envs.managers import (
 28    EventCfg,
 29    SceneEntityCfg,
 30    ObservationCfg,
 31)
 32from embodichain.lab.gym.envs import EmbodiedEnv, EmbodiedEnvCfg
 33from embodichain.lab.gym.utils.registration import register_env
 34from embodichain.lab.sim.robots import DexforceW1Cfg
 35from embodichain.lab.sim.sensors import StereoCameraCfg, SensorCfg
 36from embodichain.lab.sim.shapes import MeshCfg
 37from embodichain.lab.sim.cfg import (
 38    RenderCfg,
 39    LightCfg,
 40    ArticulationCfg,
 41    RobotCfg,
 42    RigidObjectCfg,
 43    RigidBodyAttributesCfg,
 44)
 45from embodichain.data import get_data_path
 46from embodichain.utils import configclass
 47
 48
 49@configclass
 50class ExampleEventCfg:
 51
 52    replace_obj: EventCfg = EventCfg(
 53        func=events.replace_assets_from_group,
 54        mode="reset",
 55        params={
 56            "entity_cfg": SceneEntityCfg(
 57                uid="fork",
 58            ),
 59            "folder_path": get_data_path("TableWare/tableware/fork/"),
 60        },
 61    )
 62
 63    randomize_fork_mass: EventCfg = EventCfg(
 64        func=rand.randomize_rigid_object_mass,
 65        mode="reset",
 66        params={
 67            "entity_cfg": SceneEntityCfg(
 68                uid="fork",
 69            ),
 70            "mass_range": (0.1, 2.0),
 71        },
 72    )
 73
 74    randomize_table_mat: EventCfg = EventCfg(
 75        func=rand.randomize_visual_material,
 76        mode="interval",
 77        interval_step=25,
 78        params={
 79            "entity_cfg": SceneEntityCfg(
 80                uid="table",
 81            ),
 82            "random_texture_prob": 0.5,
 83            "texture_path": get_data_path("CocoBackground/coco"),
 84            "base_color_range": [[0.2, 0.2, 0.2], [1.0, 1.0, 1.0]],
 85        },
 86    )
 87
 88
 89@configclass
 90class ObsCfg:
 91
 92    obj_pose: ObservationCfg = ObservationCfg(
 93        func=obs.get_rigid_object_pose,
 94        mode="add",
 95        name="fork_pose",
 96        params={"entity_cfg": SceneEntityCfg(uid="fork")},
 97    )
 98
 99
100@configclass
101class ExampleCfg(EmbodiedEnvCfg):
102
103    # Define the robot configuration using DexforceW1Cfg
104    robot: RobotCfg = DexforceW1Cfg.from_dict(
105        {
106            "uid": "dexforce_w1",
107            "version": "v021",
108            "init_pos": [0.0, 0, 0.0],
109        }
110    )
111
112    # Define the sensor configuration using StereoCameraCfg
113    sensor: List[SensorCfg] = [
114        StereoCameraCfg(
115            uid="eye_in_head",
116            width=960,
117            height=540,
118            enable_mask=True,
119            enable_depth=True,
120            left_to_right_pos=(0.06, 0, 0),
121            intrinsics=(450, 450, 480, 270),
122            intrinsics_right=(450, 450, 480, 270),
123            extrinsics=StereoCameraCfg.ExtrinsicsCfg(
124                parent="eyes",
125            ),
126        )
127    ]
128
129    background: List[RigidObjectCfg] = [
130        RigidObjectCfg(
131            uid="table",
132            shape=MeshCfg(
133                fpath=get_data_path("CircleTableSimple/circle_table_simple.ply"),
134                compute_uv=True,
135            ),
136            attrs=RigidBodyAttributesCfg(
137                mass=10.0,
138                static_friction=0.95,
139                dynamic_friction=0.85,
140                restitution=0.01,
141            ),
142            body_type="kinematic",
143            init_pos=(0.80, 0, 0.8),
144            init_rot=(0, 90, 0),
145        ),
146    ]
147
148    rigid_object: List[RigidObjectCfg] = [
149        RigidObjectCfg(
150            uid="fork",
151            shape=MeshCfg(
152                fpath=get_data_path("TableWare/tableware/fork/standard_fork_scale.ply"),
153            ),
154            body_scale=(0.75, 0.75, 1.0),
155            init_pos=(0.8, 0, 1.0),
156        ),
157    ]
158
159    articulation_cfg: List[ArticulationCfg] = [
160        ArticulationCfg(
161            uid="drawer",
162            fpath="SlidingBoxDrawer/SlidingBoxDrawer.urdf",
163            init_pos=(0.5, 0.0, 0.85),
164        )
165    ]
166
167    events = ExampleEventCfg()
168
169    observations = ObsCfg()
170
171
172@register_env("ModularEnv-v1", max_episode_steps=100, override=True)
173class ModularEnv(EmbodiedEnv):
174    """
175    An example of a modular environment that inherits from EmbodiedEnv
176    and uses custom event and observation managers.
177    """
178
179    def __init__(self, cfg: EmbodiedEnvCfg, **kwargs):
180        super().__init__(cfg, **kwargs)
181
182
183if __name__ == "__main__":
184    import gymnasium as gym
185    import argparse
186
187    from embodichain.lab.sim import SimulationManagerCfg
188    from embodichain.lab.gym.utils.gym_utils import add_env_launcher_args_to_parser
189    from embodichain.lab.visualization import visualization_cfg_from_args
190
191    parser = argparse.ArgumentParser()
192    add_env_launcher_args_to_parser(parser)
193    args = parser.parse_args()
194
195    env_cfg = ExampleCfg(
196        sim_cfg=SimulationManagerCfg(
197            render_cfg=RenderCfg(renderer=args.renderer),
198            headless=args.headless,
199            sim_device=args.device,
200            visualization=visualization_cfg_from_args(args),
201        ),
202        num_envs=args.num_envs,
203    )
204
205    # Create the Gym environment
206    env = gym.make("ModularEnv-v1", cfg=env_cfg)
207
208    for i in range(5):
209        obs, info = env.reset()
210
211        for i in range(100):
212            action = torch.zeros(env.action_space.shape, dtype=torch.float32)
213            obs, reward, done, truncated, info = env.step(action)

The Code Explained#

This tutorial showcases EmbodiChain’s most powerful environment creation approach using the envs.EmbodiedEnv class. Unlike the basic environment tutorial, this approach uses declarative configuration classes and manager systems for maximum flexibility and reusability.

Event Configuration#

Events define automated behaviors that occur during simulation. There are three types of supported modes:

  • startup: triggers once when the environment is initialized

  • reset: triggers every time the environment is reset

  • interval: triggers at fixed step intervals during simulation

The ExampleEventCfg demonstrates three types of events:

from embodichain.lab.sim.shapes import MeshCfg
from embodichain.lab.sim.cfg import (
    RenderCfg,
    LightCfg,
    ArticulationCfg,
    RobotCfg,
    RigidObjectCfg,
    RigidBodyAttributesCfg,
)
from embodichain.data import get_data_path
from embodichain.utils import configclass


@configclass
class ExampleEventCfg:

    replace_obj: EventCfg = EventCfg(
        func=events.replace_assets_from_group,
        mode="reset",
        params={
            "entity_cfg": SceneEntityCfg(
                uid="fork",
            ),
            "folder_path": get_data_path("TableWare/tableware/fork/"),
        },
    )

    randomize_fork_mass: EventCfg = EventCfg(
        func=rand.randomize_rigid_object_mass,
        mode="reset",
        params={
            "entity_cfg": SceneEntityCfg(
                uid="fork",
            ),
            "mass_range": (0.1, 2.0),
        },
    )

    randomize_table_mat: EventCfg = EventCfg(
        func=rand.randomize_visual_material,
        mode="interval",

Asset Replacement Event

The replace_obj event demonstrates dynamic asset swapping:

Light Randomization Event

The randomize_light event creates dynamic lighting conditions:

  • Function: envs.managers.randomization.rendering.randomize_light()

  • Mode: "interval" - triggers every 5 steps

  • Parameters: Randomizes position, color, and intensity within specified ranges

Material Randomization Event

The randomize_table_mat event varies visual appearance:

  • Function: envs.managers.randomization.rendering.randomize_visual_material()

  • Mode: "interval" - triggers every 10 steps

  • Features: Random textures from COCO dataset and base color variations

For more randomization events, please refer to Event Functors.

Observation Configuration#

The default observation from envs.EmbodiedEnv includes: - robot: robot proprioceptive data (joint positions, velocities, efforts) - sensor: all available sensor data (images, depth, segmentation, etc.)

However, users always need to define some custom observation for specified learning tasks. To handle this, the observation manager system allows users to declaratively specify additional observations.

            "entity_cfg": SceneEntityCfg(
                uid="table",
            ),
            "random_texture_prob": 0.5,
            "texture_path": get_data_path("CocoBackground/coco"),
            "base_color_range": [[0.2, 0.2, 0.2], [1.0, 1.0, 1.0]],
        },
    )

This configuration:

For details documentation, see envs.managers.cfg.ObservationCfg.

Environment Configuration#

The main environment configuration inherits from envs.EmbodiedEnvCfg and defines all scene components:

Robot Configuration

    robot: RobotCfg = DexforceW1Cfg.from_dict(
        {
            "uid": "dexforce_w1",
            "version": "v021",
            "init_pos": [0.0, 0, 0.0],
        }
    )

Uses the pre-configured DexforceW1Cfg with customizations:

  • Version: Specific robot variant (v021)

  • Position: Initial placement in the scene

Sensor Configuration

    robot: RobotCfg = DexforceW1Cfg.from_dict(
        {
            "uid": "dexforce_w1",
            "version": "v021",
            "init_pos": [0.0, 0, 0.0],
        }
    )

    # Define the sensor configuration using StereoCameraCfg
    sensor: List[SensorCfg] = [
        StereoCameraCfg(
            uid="eye_in_head",
            width=960,
            height=540,
            enable_mask=True,

Configures a stereo camera system using StereoCameraCfg:

  • Resolution: 960x540 pixels for realistic visual input

  • Features: Depth sensing and segmentation masks enabled

  • Stereo Setup: 6cm baseline between left and right cameras

  • Mounting: Attached to robot’s “eyes” frame

Lighting Configuration

            left_to_right_pos=(0.06, 0, 0),
            intrinsics=(450, 450, 480, 270),
            intrinsics_right=(450, 450, 480, 270),
            extrinsics=StereoCameraCfg.ExtrinsicsCfg(
                parent="eyes",
            ),
        )
    ]

    background: List[RigidObjectCfg] = [
        RigidObjectCfg(

Defines scene illumination with configurable lights:

  • Types: Supports "point", "sun", "direction", "spot", "rect", and "mesh".

  • Global lights: "sun" and "direction" are global scene lights (single instance, infinite distance).

  • Properties: Configurable color, intensity, position, and direction.

  • UID: Named reference for event system manipulation.

Rigid Objects

            shape=MeshCfg(
                fpath=get_data_path("CircleTableSimple/circle_table_simple.ply"),
                compute_uv=True,
            ),
            attrs=RigidBodyAttributesCfg(
                mass=10.0,
                static_friction=0.95,
                dynamic_friction=0.85,
                restitution=0.01,
            ),
            body_type="kinematic",
            init_pos=(0.80, 0, 0.8),
            init_rot=(0, 90, 0),
        ),
    ]

    rigid_object: List[RigidObjectCfg] = [
        RigidObjectCfg(
            uid="fork",
            shape=MeshCfg(
                fpath=get_data_path("TableWare/tableware/fork/standard_fork_scale.ply"),
            ),
            body_scale=(0.75, 0.75, 1.0),
            init_pos=(0.8, 0, 1.0),
        ),
    ]

Multiple objects demonstrate different physics properties:

Table Configuration:

  • Shape: Custom PLY mesh with UV mapping

  • Physics: Kinematic body (movable but not affected by forces)

  • Material: Friction and restitution properties for realistic contact

Fork Configuration:

  • Shape: Detailed mesh from asset library

  • Scale: Proportionally scaled for scene consistency

  • Physics: Dynamic body affected by gravity and collisions

Articulated Objects

    articulation_cfg: List[ArticulationCfg] = [
        ArticulationCfg(
            uid="drawer",
            fpath="SlidingBoxDrawer/SlidingBoxDrawer.urdf",
            init_pos=(0.5, 0.0, 0.85),
        )
    ]

    events = ExampleEventCfg()

    observations = ObsCfg()

Demonstrates complex mechanisms with moving parts:

  • URDF: Sliding drawer with joints and constraints

  • Positioning: Placed on table surface for interaction

Environment Implementation#

The actual environment class is remarkably simple due to the configuration-driven approach:

@register_env("ModularEnv-v1", max_episode_steps=100, override=True)
class ModularEnv(EmbodiedEnv):
    """
    An example of a modular environment that inherits from EmbodiedEnv
    and uses custom event and observation managers.
    """

    def __init__(self, cfg: EmbodiedEnvCfg, **kwargs):
        super().__init__(cfg, **kwargs)

The envs.EmbodiedEnv base class automatically:

  • Loads all configured scene components

  • Sets up observation and action spaces

  • Initializes event and observation managers

  • Handles environment lifecycle (reset, step, etc.)

The Code Execution#

To run the modular environment:

cd /path/to/embodichain
python scripts/tutorials/gym/modular_env.py

The script demonstrates the complete workflow:

  1. Configuration: Creates an instance of ExampleCfg

  2. Registration: Uses the registered environment ID

  3. Execution: Runs episodes with zero actions to observe automatic behaviors

Manager System Benefits#

The manager-based architecture provides several key advantages:

Event Managers

  • Modularity: Reusable event functions across environments

  • Timing Control: Flexible scheduling (reset, interval, condition-based)

  • Parameter Binding: Type-safe configuration with validation

  • Extensibility: Easy to add custom event behaviors

Observation Managers

  • Flexible Data: Any simulation data can become an observation

  • Processing Pipeline: Built-in normalization and transformation

  • Dynamic Composition: Runtime observation space modification

  • Performance: Efficient data collection and GPU acceleration

Key Features Demonstrated#

This tutorial showcases the most advanced features of EmbodiChain environments:

  1. Configuration-Driven Design: Declarative environment specification

  2. Manager Systems: Modular event and observation handling

  3. Asset Management: Dynamic loading and randomization

  4. Sensor Integration: Realistic camera systems with stereo vision

  5. Physics Simulation: Complex articulated and rigid body dynamics

  6. Visual Randomization: Automated domain randomization

  7. Extensible Architecture: Easy customization and extension points

This tutorial demonstrates the full power of EmbodiChain’s modular environment system, providing the foundation for creating sophisticated robotic learning scenarios.

Tip

Using an AI coding agent? These skills can help you build on this tutorial:

  • /add-task-env — Scaffold a new task environment with the correct file structure, @register_env decorator, base class methods, __init__.py update, and test stub.

  • /add-functor — Add observation, reward, event, or randomization functors with the correct signature and module placement.

Next Steps#