-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathimport_usd.py
More file actions
168 lines (145 loc) · 5 KB
/
import_usd.py
File metadata and controls
168 lines (145 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# ----------------------------------------------------------------------------
# Copyright (c) 2021-2025 DexForce Technology Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------
"""
This script demonstrates how to create a simulation scene using SimulationManager.
It shows the basic setup of simulation context, adding objects, and sensors.
"""
import argparse
import time
from embodichain.lab.sim import SimulationManager, SimulationManagerCfg
from embodichain.lab.sim.cfg import RigidBodyAttributesCfg
from embodichain.lab.sim.shapes import CubeCfg, MeshCfg
from embodichain.lab.sim.objects import (
RigidObject,
RigidObjectCfg,
ArticulationCfg,
Articulation,
)
from dexsim.utility.path import get_resources_data_path
def main():
"""Main function to create and run the simulation scene."""
# Parse command line arguments
parser = argparse.ArgumentParser(
description="Create a simulation scene with SimulationManager"
)
parser.add_argument(
"--headless",
action="store_true",
default=False,
help="Run simulation in headless mode",
)
parser.add_argument(
"--device", type=str, default="cpu", help="Simulation device (cuda or cpu)"
)
parser.add_argument(
"--enable_rt",
action="store_true",
default=True,
help="Enable ray tracing for better visuals",
)
args = parser.parse_args()
# Configure the simulation
sim_cfg = SimulationManagerCfg(
width=1920,
height=1080,
headless=True,
physics_dt=1.0 / 100.0, # Physics timestep (100 Hz)
sim_device=args.device,
enable_rt=args.enable_rt, # Enable ray tracing for better visuals
num_envs=1,
arena_space=3.0,
)
# Create the simulation instance
sim = SimulationManager(sim_cfg)
# Open window when the scene has been set up
if not args.headless:
sim.open_window()
cube: RigidObject = sim.add_rigid_object(
cfg=RigidObjectCfg(
uid="cube",
shape=CubeCfg(size=[0.1, 0.1, 0.1]),
body_type="dynamic",
attrs=RigidBodyAttributesCfg(
mass=1.0,
dynamic_friction=0.5,
static_friction=0.5,
restitution=0.1,
),
init_pos=[0.0, 0.0, 1.0],
)
)
usdpath = "/home/xiemh/model/004_sugar_box/004_sugar_box_xmh.usda"
sugar_box: RigidObject = sim.add_rigid_object(
cfg=RigidObjectCfg(
uid="sugar_box",
shape=MeshCfg(fpath=usdpath),
body_type="dynamic",
init_pos=[0.2, 0.2, 1.0],
use_usd_properties=True,
)
)
# Add objects to the scene
h1: Articulation = sim.add_articulation(
cfg=ArticulationCfg(
uid="h1",
# fpath="/home/xiemh/model/Collected_ur10/ur10.usd",
fpath="/home/xiemh/model/Collected_h1/h1.usda",
build_pk_chain=False,
init_pos=[-0.2, -0.2, 1.0],
use_usd_properties=False,
)
)
print("[INFO]: Scene setup complete!")
print("[INFO]: Press Ctrl+C to stop the simulation")
# Run the simulation
run_simulation(sim)
def run_simulation(sim: SimulationManager):
"""Run the simulation loop.
Args:
sim: The SimulationManager instance to run
"""
# Initialize GPU physics if using CUDA
if sim.is_use_gpu_physics:
sim.init_gpu_physics()
step_count = 0
try:
last_time = time.time()
last_step = 0
while True:
# Update physics simulation
sim.update(step=1)
time.sleep(0.03) # Sleep to limit update rate (optional)
step_count += 1
# Print FPS every second
if step_count % 100 == 0:
current_time = time.time()
elapsed = current_time - last_time
fps = (
sim.num_envs * (step_count - last_step) / elapsed
if elapsed > 0
else 0
)
# print(f"[INFO]: Simulation step: {step_count}, FPS: {fps:.2f}")
last_time = current_time
last_step = step_count
except KeyboardInterrupt:
print("\n[INFO]: Stopping simulation...")
finally:
# Clean up resources
sim.destroy()
print("[INFO]: Simulation terminated successfully")
if __name__ == "__main__":
main()