Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Changed
Fixed
^^^^^

- Unfilled per-world mesh variant slots (``geom_dataid = -1``) no longer produce
phantom collisions at the origin when ``num_envs=1`` :issue:`1231`.
- YAM RGB and depth cube-lifting tasks now use separate experiment directories,
``yam_lift_cube_rgb`` and ``yam_lift_cube_depth``.
- ``EntityData.gravity_vec_w`` now initializes from normalized model gravity, so
Expand Down
15 changes: 14 additions & 1 deletion src/mjlab/entity/variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -1426,10 +1426,23 @@ def build_variant_model(
dataid_table[:, slot_geom_ids] = variant_slot_mesh_ids[w2v]
matid_table[:, slot_geom_ids] = variant_slot_matids[w2v]

# MuJoCo Warp compiles out its missing-mesh broadphase guard when
# geom_dataid.shape[0] == 1, causing unfilled mesh slots (dataid == -1) to
# collide at the origin. Pad a duplicate row when nworld == 1 has unfilled
# mesh slots; world 0 still indexes row 0 % 2 == 0.
mesh_mask = model.geom_type == mujoco.mjtGeom.mjGEOM_MESH
wp_dataid_table = (
np.repeat(dataid_table, 2, axis=0)
if nworld == 1 and bool(np.any(dataid_table[0, mesh_mask] < 0))
else dataid_table
)

# Build warp model.
m = mjwarp.put_model(model)
m.geom_dataid = wp.array(dataid_table, dtype=int)
m.geom_dataid = wp.array(wp_dataid_table, dtype=int)
m.geom_dataid._is_batched = True # ty: ignore[missing-attribute]
m.geom_matid = wp.array(matid_table, dtype=int)
m.geom_matid._is_batched = True # ty: ignore[missing-attribute]

# Populate dependent per-world fields.
_populate_dependent_fields(m, model, nworld, variant_info, world_to_variant)
Expand Down
7 changes: 7 additions & 0 deletions src/mjlab/sim/sim_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def __init__(self, wp_array: wp.array, nworld: int | None = None) -> None:
):
new_shape = (nworld,) + self._tensor.shape[1:]
self._tensor = self._tensor.expand(new_shape)
elif (
nworld is not None
and len(self._tensor.shape) > 0
and self._tensor.shape[0] > nworld
and getattr(wp_array, "_is_batched", False)
):
self._tensor = self._tensor[:nworld]

self._is_cuda = not wp_array.device.is_cpu
self._torch_stream = self._setup_stream()
Expand Down
67 changes: 67 additions & 0 deletions tests/test_variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -1780,3 +1780,70 @@ class _SimModel:

np.testing.assert_allclose(model.geom_rgba, [[0.5, 0.6, 0.7, 0.8]])
np.testing.assert_allclose(model.geom_pos, original_geom_pos)


@pytest.mark.parametrize("broadphase", ["nxn", "sap_tile"])
@pytest.mark.parametrize("num_envs", [1, 2])
def test_unfilled_mesh_slots_do_not_collide_in_single_or_multi_world(
broadphase: str, num_envs: int
) -> None:
"""Unfilled variant mesh slots (dataid == -1) produce no phantom contacts,
including when num_envs == 1."""

def _one_high_mesh_spec() -> mujoco.MjSpec:
spec = mujoco.MjSpec()
m0 = spec.add_mesh(name="m0")
m0.make_sphere(subdivision=1)
body = spec.worldbody.add_body(name="prop")
body.add_freejoint()
body.add_geom(
name="col_0",
type=mujoco.mjtGeom.mjGEOM_MESH,
meshname="m0",
pos=(0.0, 0.0, 3.0),
)
return spec

def _two_mesh_spec() -> mujoco.MjSpec:
spec = _one_high_mesh_spec()
m1 = spec.add_mesh(name="m1")
m1.make_sphere(subdivision=1)
spec.body("prop").add_geom(
name="col_1",
type=mujoco.mjtGeom.mjGEOM_MESH,
meshname="m1",
pos=(0.0, 0.0, 0.0),
)
return spec

scene_spec, vi = _build_scene_with_variants(
_one_high_mesh_spec,
_two_mesh_spec,
weight_a=1.0,
weight_b=0.0,
)
# Floor at z = -0.5 overlaps the unfilled col_1 slot at z = 0, but not col_0 at z = 3.
scene_spec.worldbody.add_geom(
name="floor",
type=mujoco.mjtGeom.mjGEOM_PLANE,
pos=(0.0, 0.0, -0.5),
size=(5.0, 5.0, 0.1),
)

sim = Simulation(
num_envs=num_envs,
cfg=SimulationCfg(broadphase=cast(Any, broadphase)),
spec=scene_spec,
variant_info=vi,
device="cpu",
)
assert sim.model.geom_dataid.shape == (num_envs, sim.mj_model.ngeom)

# Unfilled col_1 slot must not collide with the floor.
sim.forward()
assert int(sim.wp_data.nacon.numpy()[0]) == 0

# Lowering the body so filled col_0 reaches the floor must register contacts.
sim.data.qpos[:, 2] = -3.0
sim.forward()
assert int(sim.wp_data.nacon.numpy()[0]) > 0
Loading