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-2025 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.sim.cfg import RigidBodyAttributesCfg
26from embodichain.lab.sim.shapes import CubeCfg
27from embodichain.lab.sim.objects import (
28 RigidObjectGroup,
29 RigidObjectGroupCfg,
30 RigidObjectCfg,
31)
32
33
34def main():
35 """Main function to create and run the simulation scene."""
36
37 # Parse command line arguments
38 parser = argparse.ArgumentParser(
39 description="Create a simulation scene with SimulationManager"
40 )
41 parser.add_argument(
42 "--headless",
43 action="store_true",
44 default=False,
45 help="Run simulation in headless mode",
46 )
47 parser.add_argument(
48 "--num_envs", type=int, default=1, help="Number of parallel environments"
49 )
50 parser.add_argument(
51 "--device", type=str, default="cpu", help="Simulation device (cuda or cpu)"
52 )
53 parser.add_argument(
54 "--enable_rt",
55 action="store_true",
56 default=False,
57 help="Enable ray tracing for better visuals",
58 )
59 args = parser.parse_args()
60
61 # Configure the simulation
62 sim_cfg = SimulationManagerCfg(
63 width=1920,
64 height=1080,
65 headless=True,
66 physics_dt=1.0 / 100.0, # Physics timestep (100 Hz)
67 sim_device=args.device,
68 enable_rt=args.enable_rt, # Enable ray tracing for better visuals
69 num_envs=args.num_envs,
70 arena_space=3.0,
71 )
72
73 # Create the simulation instance
74 sim = SimulationManager(sim_cfg)
75
76 physics_attrs = RigidBodyAttributesCfg(
77 mass=1.0,
78 dynamic_friction=0.5,
79 static_friction=0.5,
80 restitution=0.1,
81 )
82
83 # Add objects to the scene
84 obj_group: RigidObjectGroup = sim.add_rigid_object_group(
85 cfg=RigidObjectGroupCfg(
86 uid="obj_group",
87 rigid_objects={
88 "cube_1": RigidObjectCfg(
89 uid="cube_1",
90 shape=CubeCfg(size=[0.1, 0.1, 0.1]),
91 attrs=physics_attrs,
92 init_pos=[0.0, 0.0, 1.0],
93 ),
94 "cube_2": RigidObjectCfg(
95 uid="cube_2",
96 shape=CubeCfg(size=[0.2, 0.2, 0.2]),
97 attrs=physics_attrs,
98 init_pos=[0.5, 0.0, 1.0],
99 ),
100 "cube_3": RigidObjectCfg(
101 uid="cube_3",
102 shape=CubeCfg(size=[0.3, 0.3, 0.3]),
103 attrs=physics_attrs,
104 init_pos=[-0.5, 0.0, 1.0],
105 ),
106 },
107 )
108 )
109
110 print("[INFO]: Scene setup complete!")
111 print(f"[INFO]: Running simulation with {args.num_envs} environment(s)")
112 print("[INFO]: Press Ctrl+C to stop the simulation")
113
114 # Open window when the scene has been set up
115 if not args.headless:
116 sim.open_window()
117
118 # Run the simulation
119 run_simulation(sim)
120
121
122def run_simulation(sim: SimulationManager):
123 """Run the simulation loop.
124
125 Args:
126 sim: The SimulationManager instance to run
127 """
128
129 # Initialize GPU physics if using CUDA
130 if sim.is_use_gpu_physics:
131 sim.init_gpu_physics()
132
133 step_count = 0
134
135 try:
136 last_time = time.time()
137 last_step = 0
138 while True:
139 # Update physics simulation
140 sim.update(step=1)
141 step_count += 1
142
143 # Print FPS every second
144 if step_count % 100 == 0:
145 current_time = time.time()
146 elapsed = current_time - last_time
147 fps = (
148 sim.num_envs * (step_count - last_step) / elapsed
149 if elapsed > 0
150 else 0
151 )
152 print(f"[INFO]: Simulation step: {step_count}, FPS: {fps:.2f}")
153 last_time = current_time
154 last_step = step_count
155
156 except KeyboardInterrupt:
157 print("\n[INFO]: Stopping simulation...")
158 finally:
159 # Clean up resources
160 sim.destroy()
161 print("[INFO]: Simulation terminated successfully")
162
163
164if __name__ == "__main__":
165 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.