Skip to content

Commit 14b8231

Browse files
committed
added randomizing rigid object material to tensor demo
1 parent a9257d1 commit 14b8231

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

scripts/demos/amz_tensor_demo.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
AppLauncher.add_app_launcher_args(parser)
3232
# parse the arguments
3333
args_cli = parser.parse_args()
34-
34+
args_cli.headless = True
3535
# launch omniverse app
3636
app_launcher = AppLauncher(args_cli)
3737
simulation_app = app_launcher.app
@@ -67,6 +67,11 @@
6767

6868
CACHE_HEIGHT = 2.0
6969

70+
STATIC_FRICTION_RANGE = (0.2, 2.0)
71+
DYNAMIC_FRICTION_RANGE = (0.2, 2.0)
72+
RESTITUTION_RANGE = (0.0, 0.0)
73+
NUM_BUCKETS = 25
74+
7075
def setup_object_cfg(max_num_objects: int, spacing: float = 0.5) -> RigidObjectCollectionCfg:
7176
object_cfg_list = []
7277
grid_size = math.ceil(math.sqrt(max_num_objects))
@@ -187,6 +192,11 @@ def __init__(self,
187192
self.current_grocery_poses = torch.zeros((self.num_envs, MAX_NUM_OBJECTS, 7), device=self.sim.device)
188193
self.current_grocery_velocities = torch.zeros((self.num_envs, MAX_NUM_OBJECTS, 6), device=self.sim.device)
189194

195+
range_list = [STATIC_FRICTION_RANGE, DYNAMIC_FRICTION_RANGE, RESTITUTION_RANGE]
196+
ranges = torch.tensor(range_list, device="cpu")
197+
self.material_buckets = math_utils.sample_uniform(ranges[:, 0], ranges[:, 1], (NUM_BUCKETS, 3), device="cpu")
198+
199+
190200
self.ALL_OBJ_INDICES = torch.arange(MAX_NUM_OBJECTS, dtype=torch.long, device=self.sim.device)
191201
self.ALL_ENV_INDICES = torch.arange(self.num_envs, dtype=torch.long, device=self.sim.device)
192202

@@ -287,6 +297,21 @@ def set_mass_tensor(self, mass, env_ids: list[int] | None = None):
287297
reworked_indices = (self.ALL_OBJ_INDICES.unsqueeze(1) * num_envs + env_ids).flatten()
288298
self.groceries_view.set_masses(mass.flatten().cpu(), indices=reworked_indices.cpu())
289299

300+
def set_rigid_body_material_tensor(self, materials: torch.Tensor, env_ids: list[int] | None = None):
301+
with Timer("[INFO] Time to set the rigid_object_material"):
302+
# Set the rigid body material of the groceries in the view
303+
# Doc here: https://docs.omniverse.nvidia.com/kit/docs/omni_physics/latest/extensions/runtime/source/omni.physics.tensors/docs/api/python.html#omni.physics.tensors.impl.api.ArticulationView.set_material_properties
304+
305+
# Note: We need to convert to CPU as this property can only be set on CPU,
306+
# we need to rework the indices to match the shape of the view.
307+
if env_ids is None:
308+
num_envs = self.num_envs
309+
else:
310+
num_envs = len(env_ids)
311+
312+
reworked_indices = (self.ALL_OBJ_INDICES.unsqueeze(1) * num_envs + env_ids).flatten()
313+
self.groceries_view.set_material_properties(materials, indices=reworked_indices.cpu())
314+
290315
def set_disable_gravity_tensor(self, disable_gravity: torch.Tensor, env_ids: list[int] | None = None):
291316
with Timer("[INFO] Time to set the disable gravity"):
292317
# Disables the gravity for some of the inactive groceries.
@@ -411,9 +436,19 @@ def reset(self, env_ids: list[int] | None = None):
411436

412437
# Randomize the mass of the groceries
413438
new_masses = torch.rand((len(env_ids), MAX_NUM_OBJECTS), device=self.sim.device) * 0.2 + 0.2
439+
440+
# Randomize the grocery materials
441+
total_num_shapes = self.groceries_view.max_shapes
442+
bucket_ids = torch.randint(0, len(self.material_buckets), (len(env_ids), total_num_shapes), device="cpu")
443+
material_samples = self.material_buckets[bucket_ids]
444+
445+
# retrieve material buffer from the physics simulation
446+
materials = self.groceries_view.get_material_properties()
447+
materials[env_ids] = material_samples[:]
448+
414449
# Apply the new masses to the simulation
415450
self.set_mass_tensor(new_masses, env_ids)
416-
451+
self.set_rigid_body_material_tensor(materials, env_ids)
417452
# Disable the gravity for the cached groceries
418453
self.set_disable_gravity_tensor(~self.groceries_mask[env_ids], env_ids)
419454

scripts/demos/bin_packing.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ class MultiObjectSceneCfg(InteractiveSceneCfg):
153153
}
154154
)
155155

156-
157156
def reset_object_collections(scene: InteractiveScene, view_ids: torch.Tensor):
158157
if len(view_ids) == 0:
159158
return
@@ -165,6 +164,7 @@ def reset_object_collections(scene: InteractiveScene, view_ids: torch.Tensor):
165164
ranges = torch.tensor(range_list, device=scene.device)
166165
samples = math_utils.sample_uniform(ranges[:, 0], ranges[:, 1], (len(view_ids), 6), device=scene.device)
167166

167+
# poses
168168
positions = default_state_w_view[:, :3] + samples[..., 0:3]
169169
orientations_delta = math_utils.quat_from_euler_xyz(samples[..., 3], samples[..., 4], samples[..., 5])
170170
orientations = math_utils.quat_mul(default_state_w_view[:, 3:7], orientations_delta)
@@ -173,13 +173,24 @@ def reset_object_collections(scene: InteractiveScene, view_ids: torch.Tensor):
173173
ranges = torch.tensor(range_list, device=scene.device)
174174
samples = math_utils.sample_uniform(ranges[:, 0], ranges[:, 1], (len(view_ids), 6), device=scene.device)
175175

176-
velocities = default_state_w_view[:, 7:13] + samples
176+
new_velocities = default_state_w_view[:, 7:13] + samples
177177
new_poses = torch.concat((positions, orientations), dim=-1)
178178

179179
new_poses[..., 3:] = math_utils.convert_quat(new_poses[..., 3:], to="xyzw")
180-
rigid_object_collection.root_physx_view.set_transforms(new_poses, indices=view_ids.view(-1, 1))
181-
rigid_object_collection.root_physx_view.set_velocities(velocities, indices=view_ids.view(-1, 1))
182180

181+
num_objects = rigid_object_collection.num_instances * rigid_object_collection.num_objects
182+
183+
if len(view_ids) != num_objects:
184+
poses = torch.zeros((num_objects, 7), device=scene.device)
185+
poses[view_ids, :] = new_poses
186+
velocities = torch.zeros((num_objects, 6), device=scene.device)
187+
velocities[view_ids, :] = new_velocities
188+
else:
189+
poses = new_poses
190+
velocities = new_velocities
191+
192+
rigid_object_collection.root_physx_view.set_transforms(poses, indices=view_ids.view(-1, 1))
193+
rigid_object_collection.root_physx_view.set_velocities(velocities, indices=view_ids.view(-1, 1))
183194

184195

185196
##
@@ -207,8 +218,6 @@ def run_simulator(sim: SimulationContext, scene: InteractiveScene):
207218
# object
208219
root_state = rigid_object.data.default_root_state.clone()
209220
root_state[:, :3] += scene.env_origins
210-
rigid_object.write_root_pose_to_sim(root_state[:, :7])
211-
rigid_object.write_root_velocity_to_sim(root_state[:, 7:])
212221
# object collection
213222
reset_object_collections(scene, view_indices)
214223
scene.reset()
@@ -220,8 +229,8 @@ def run_simulator(sim: SimulationContext, scene: InteractiveScene):
220229
sim.step()
221230
object_pos_b = rigid_object_collection.data.object_pos_w - scene.env_origins.unsqueeze(1)
222231
object_pos_b_view = rigid_object_collection.reshape_data_to_view(object_pos_b)
223-
inbound_mask = (-1.0 < object_pos_b_view[:, 0]) & (object_pos_b_view[:, 0] < 1.0)
224-
inbound_mask &= (-1.0 < object_pos_b_view[:, 1]) & (object_pos_b_view[:, 1] < 1.0)
232+
inbound_mask = (-0.2 < object_pos_b_view[:, 0]) & (object_pos_b_view[:, 0] < 0.2)
233+
inbound_mask &= (-0.3 < object_pos_b_view[:, 1]) & (object_pos_b_view[:, 1] < 0.3)
225234
reset_object_collections(scene, view_indices[~inbound_mask])
226235
# Increment counter
227236
count += 1

0 commit comments

Comments
 (0)