Rigid object group tutorial#
This tutorial shows how to create and use a RigidObjectGroup in SimulationManager.
It follows the style used in the create_scene tutorial and references the
example script located in scripts/tutorials/sim/create_rigid_object_group.py.
The Code#
The tutorial corresponds to the create_rigid_object_group.py script in the
scripts/tutorials/sim directory.
Code for create_rigid_object_group.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
17"""
18This script demonstrates how to create a rigid object group using SimulationManager.
19"""
20
21import argparse
22import time
23
24from embodichain.lab.sim import SimulationManager, SimulationManagerCfg
25from embodichain.lab.gym.utils.gym_utils import add_env_launcher_args_to_parser
26from embodichain.lab.sim.cfg import RigidBodyAttributesCfg, RenderCfg
27from embodichain.lab.sim.shapes import CubeCfg
28from embodichain.lab.sim.objects import (
29 RigidObjectGroup,
30 RigidObjectGroupCfg,
31 RigidObjectCfg,
32)
33
34
35def main():
36 """Main function to create and run the simulation scene."""
37
38 # Parse command line arguments
39 parser = argparse.ArgumentParser(
40 description="Create a simulation scene with SimulationManager"
41 )
42 add_env_launcher_args_to_parser(parser)
43 args = parser.parse_args()
44
45 # Configure the simulation
46 sim_cfg = SimulationManagerCfg(
47 width=1920,
48 height=1080,
49 headless=True,
50 physics_dt=1.0 / 100.0, # Physics timestep (100 Hz)
51 sim_device=args.device,
52 render_cfg=RenderCfg(
53 renderer=args.renderer
54 ), # Enable ray tracing for better visuals
55 num_envs=args.num_envs,
56 arena_space=3.0,
57 )
58
59 # Create the simulation instance
60 sim = SimulationManager(sim_cfg)
61
62 physics_attrs = RigidBodyAttributesCfg(
63 mass=1.0,
64 dynamic_friction=0.5,
65 static_friction=0.5,
66 restitution=0.1,
67 )
68
69 # Add objects to the scene
70 obj_group: RigidObjectGroup = sim.add_rigid_object_group(
71 cfg=RigidObjectGroupCfg(
72 uid="obj_group",
73 rigid_objects={
74 "cube_1": RigidObjectCfg(
75 uid="cube_1",
76 shape=CubeCfg(size=[0.1, 0.1, 0.1]),
77 attrs=physics_attrs,
78 init_pos=[0.0, 0.0, 1.0],
79 ),
80 "cube_2": RigidObjectCfg(
81 uid="cube_2",
82 shape=CubeCfg(size=[0.2, 0.2, 0.2]),
83 attrs=physics_attrs,
84 init_pos=[0.5, 0.0, 1.0],
85 ),
86 "cube_3": RigidObjectCfg(
87 uid="cube_3",
88 shape=CubeCfg(size=[0.3, 0.3, 0.3]),
89 attrs=physics_attrs,
90 init_pos=[-0.5, 0.0, 1.0],
91 ),
92 },
93 )
94 )
95
96 print("[INFO]: Scene setup complete!")
97 print(f"[INFO]: Running simulation with {args.num_envs} environment(s)")
98 print("[INFO]: Press Ctrl+C to stop the simulation")
99
100 # Open window when the scene has been set up
101 if not args.headless:
102 sim.open_window()
103
104 # Run the simulation
105 run_simulation(sim)
106
107
108def run_simulation(sim: SimulationManager):
109 """Run the simulation loop.
110
111 Args:
112 sim: The SimulationManager instance to run
113 """
114
115 # Initialize GPU physics if using CUDA
116 if sim.is_use_gpu_physics:
117 sim.init_gpu_physics()
118
119 step_count = 0
120
121 try:
122 last_time = time.time()
123 last_step = 0
124 while True:
125 # Update physics simulation
126 sim.update(step=1)
127 step_count += 1
128
129 # Print FPS every second
130 if step_count % 100 == 0:
131 current_time = time.time()
132 elapsed = current_time - last_time
133 fps = (
134 sim.num_envs * (step_count - last_step) / elapsed
135 if elapsed > 0
136 else 0
137 )
138 print(f"[INFO]: Simulation step: {step_count}, FPS: {fps:.2f}")
139 last_time = current_time
140 last_step = step_count
141
142 except KeyboardInterrupt:
143 print("\n[INFO]: Stopping simulation...")
144 finally:
145 # Clean up resources
146 sim.destroy()
147 print("[INFO]: Simulation terminated successfully")
148
149
150if __name__ == "__main__":
151 main()
The Code Explained#
Adding a RigidObjectGroup#
The key part of the tutorial demonstrates creating a RigidObjectGroup via
sim.add_rigid_object_group. The group is configured with a mapping of
object UIDs to RigidObjectCfg entries. Each entry defines a shape
(here CubeCfg), physics attributes, and initial pose.
obj_group: RigidObjectGroup = sim.add_rigid_object_group(
cfg=RigidObjectGroupCfg(
uid="obj_group",
rigid_objects={
"cube_1": RigidObjectCfg(
uid="cube_1",
shape=CubeCfg(size=[0.1, 0.1, 0.1]),
attrs=physics_attrs,
init_pos=[0.0, 0.0, 1.0],
),
"cube_2": RigidObjectCfg(
uid="cube_2",
shape=CubeCfg(size=[0.2, 0.2, 0.2]),
attrs=physics_attrs,
init_pos=[0.5, 0.0, 1.0],
),
"cube_3": RigidObjectCfg(
uid="cube_3",
shape=CubeCfg(size=[0.3, 0.3, 0.3]),
attrs=physics_attrs,
init_pos=[-0.5, 0.0, 1.0],
),
},
)
)
print("[INFO]: Scene setup complete!")
Running the tutorial#
To run the script from the repository root:
python scripts/tutorials/sim/create_rigid_object_group.py
You can pass flags such as --headless, --num_envs <n>, and
--device <cpu|cuda> to customize the run.