File size: 3,364 Bytes
406662d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause

"""This test has a lot of duplication with ``test_build_simulation_context_headless.py``.

This is intentional to ensure that the tests are run in both headless and non-headless modes,
and we currently can't re-build the simulation app in a script.

If you need to make a change to this test, please make sure to also make the same change to
``test_build_simulation_context_headless.py``.
"""

"""Launch Isaac Sim Simulator first."""

from isaaclab.app import AppLauncher

# launch omniverse app
simulation_app = AppLauncher(headless=True).app

"""Rest everything follows."""

import pytest

from isaaclab.sim.simulation_cfg import SimulationCfg
from isaaclab.sim.simulation_context import build_simulation_context


@pytest.mark.parametrize("gravity_enabled", [True, False])
@pytest.mark.parametrize("device", ["cuda:0", "cpu"])
@pytest.mark.parametrize("dt", [0.01, 0.1])
def test_build_simulation_context_no_cfg(gravity_enabled, device, dt):
    """Test that the simulation context is built when no simulation cfg is passed in."""
    with build_simulation_context(gravity_enabled=gravity_enabled, device=device, dt=dt) as sim:
        if gravity_enabled:
            assert sim.cfg.gravity == (0.0, 0.0, -9.81)
        else:
            assert sim.cfg.gravity == (0.0, 0.0, 0.0)

        assert sim.cfg.device == device
        assert sim.cfg.dt == dt


@pytest.mark.parametrize("add_ground_plane", [True, False])
def test_build_simulation_context_ground_plane(add_ground_plane):
    """Test that the simulation context is built with the correct ground plane."""
    with build_simulation_context(add_ground_plane=add_ground_plane) as sim:
        # Ensure that ground plane got added
        if add_ground_plane:
            assert sim.stage.GetPrimAtPath("/World/defaultGroundPlane").IsValid()
        else:
            assert not sim.stage.GetPrimAtPath("/World/defaultGroundPlane").IsValid()


@pytest.mark.parametrize("add_lighting", [True, False])
@pytest.mark.parametrize("auto_add_lighting", [True, False])
def test_build_simulation_context_auto_add_lighting(add_lighting, auto_add_lighting):
    """Test that the simulation context is built with the correct lighting."""
    with build_simulation_context(add_lighting=add_lighting, auto_add_lighting=auto_add_lighting) as sim:
        if auto_add_lighting or add_lighting:
            # Ensure that dome light got added
            assert sim.stage.GetPrimAtPath("/World/defaultDomeLight").IsValid()
        else:
            # Ensure that dome light didn't get added
            assert not sim.stage.GetPrimAtPath("/World/defaultDomeLight").IsValid()


def test_build_simulation_context_cfg():
    """Test that the simulation context is built with the correct cfg and values don't get overridden."""
    dt = 0.001
    # Non-standard gravity
    gravity = (0.0, 0.0, -1.81)
    device = "cuda:0"

    cfg = SimulationCfg(
        gravity=gravity,
        device=device,
        dt=dt,
    )

    with build_simulation_context(sim_cfg=cfg, gravity_enabled=False, dt=0.01, device="cpu") as sim:
        assert sim.cfg.gravity == gravity
        assert sim.cfg.device == device
        assert sim.cfg.dt == dt