:orphan:

.. _tutorial-interactive-scene:

Using the Interactive Scene
===========================

.. currentmodule:: isaaclab

:class:`scene.InteractiveScene` spawns and manages the assets and sensors in an
environment. It groups ground planes, lights, robots, rigid objects, and sensors in
one configuration and handles cloning them across parallel environments.

This tutorial replaces the ``design_scene`` function from
:ref:`tutorial-interact-articulation` with an ``InteractiveScene``. The same pattern
extends to scenes with more assets and sensors.

The Code
~~~~~~~~

This tutorial corresponds to the ``create_scene.py`` script within
``scripts/tutorials/02_scene``.

.. dropdown:: Code for create_scene.py
   :icon: code

   .. literalinclude:: ../../../scripts/tutorials/02_scene/create_scene.py
      :language: python
      :emphasize-lines: 50-63, 68-70, 97-98, 105-106, 111-112, 125-127
      :linenos:


The Code Explained
~~~~~~~~~~~~~~~~~~

While the code is similar to the previous tutorial, there are a few key differences
that we will go over in detail.

Scene configuration
-------------------

The scene is composed of a collection of entities, each with their own configuration.
These are specified in a configuration class that inherits from :class:`scene.InteractiveSceneCfg`.
The configuration class is then passed to the :class:`scene.InteractiveScene` constructor
to create the scene.

For the cartpole example, we specify the same scene as in the previous tutorial, but list
them now in the configuration class :class:`CartpoleSceneCfg` instead of manually spawning them.

.. literalinclude:: ../../../scripts/tutorials/02_scene/create_scene.py
   :language: python
   :pyobject: CartpoleSceneCfg

The variable names in the configuration class are used as keys to access the corresponding
entity from the :class:`scene.InteractiveScene` object. For example, the cartpole can
be accessed via ``scene["cartpole"]``. However, we will get to that later. First, let's
look at how individual scene entities are configured.

Similar to how a rigid object and articulation were configured in the previous tutorials,
the configurations are specified using a configuration class. However, there is a key
difference between the configurations for the ground plane and light source and the
configuration for the cartpole. The ground plane and light source are non-interactive
prims, while the cartpole is an interactive prim. This distinction is reflected in the
configuration classes used to specify them. The configurations for the ground plane and
light source are specified using an instance of the :class:`assets.AssetBaseCfg` class
while the cartpole is configured using an instance of the :class:`assets.ArticulationCfg`.
Anything that is not an interactive prim (i.e., neither an asset nor a sensor) is not
*handled* by the scene during simulation steps.

Another key difference to note is in the specification of the prim paths for the
different prims:

* Ground plane: ``/World/defaultGroundPlane``
* Light source: ``/World/Light``
* Cartpole: ``{ENV_REGEX_NS}/Robot``

As we learned earlier, Omniverse creates a graph of prims in the USD stage. The prim
paths are used to specify the location of the prim in the graph. The ground plane and
light source are specified using absolute paths, while the cartpole is specified using
a relative path. The relative path is specified using the ``ENV_REGEX_NS`` variable,
which is a special variable that is replaced with the environment name during scene creation.
Any entity that has the ``ENV_REGEX_NS`` variable in its prim path will be  cloned for each
environment. This path is replaced by the scene object with ``/World/envs/env_{i}`` where
``i`` is the environment index.

Scene instantiation
-------------------

Unlike before where we called the ``design_scene`` function to create the scene, we now
create an instance of the :class:`scene.InteractiveScene` class from the configuration object.
The configuration names the class it builds in its :attr:`~scene.InteractiveSceneCfg.class_type`
attribute, so :func:`~isaaclab.utils.instantiate` constructs the scene from it. While creating the
configuration instance of ``CartpoleSceneCfg`` we specify how many environment copies we want to
create using the ``num_envs`` argument. This will be used to clone the scene for each environment.

.. literalinclude:: ../../../scripts/tutorials/02_scene/create_scene.py
   :language: python
   :start-at: # Design scene
   :end-at: scene = instantiate(scene_cfg)

The configuration class ``CartpoleSceneCfg`` can be defined at the top of the script, but the
:class:`scene.InteractiveScene` class works on the USD stage of the running simulator. The
``class_type`` attribute resolves the class lazily, so the scene class is only imported when it is
constructed inside the ``with launch_simulation(...)`` block, once the simulator runtime has been launched.

Accessing scene elements
------------------------

Similar to how entities were accessed from a dictionary in the previous tutorials, the
scene elements can be accessed from the :class:`InteractiveScene` object using the
``[]`` operator. The operator takes in a string key and returns the corresponding
entity. The key is specified through the configuration class for each entity. For example,
the cartpole is specified using the key ``"cartpole"`` in the configuration class.

.. literalinclude:: ../../../scripts/tutorials/02_scene/create_scene.py
   :language: python
   :start-at: # Extract scene entities
   :end-at: robot = scene["cartpole"]

Running the simulation loop
---------------------------

The rest of the script looks similar to previous scripts that interfaced with :class:`assets.Articulation`,
with a few small differences in the methods called:

* :meth:`assets.Articulation.reset` ⟶ :meth:`scene.InteractiveScene.reset`
* :meth:`assets.Articulation.write_data_to_sim` ⟶ :meth:`scene.InteractiveScene.write_data_to_sim`
* :meth:`assets.Articulation.update` ⟶ :meth:`scene.InteractiveScene.update`

Under the hood, the methods of :class:`scene.InteractiveScene` call the corresponding
methods of the entities in the scene.


The Code Execution
~~~~~~~~~~~~~~~~~~



Let's run the script to simulate 32 cartpoles in the scene. We can do this by passing
the ``--num_envs`` argument to the script.

.. tab-set::

   .. tab-item:: uv (Recommended)

      .. code-block:: bash

         uv run python scripts/tutorials/02_scene/create_scene.py --num_envs 32

This should open a stage with 32 cartpoles swinging around randomly. You can use the
mouse to rotate the camera and the arrow keys to move around the scene.


.. figure:: ../_static/tutorials/tutorial_creating_a_scene.jpg
    :align: center
    :figwidth: 100%
    :alt: result of create_scene.py

In this tutorial, we saw how to use :class:`scene.InteractiveScene` to create a
scene with multiple assets. We also saw how to use the ``num_envs`` argument
to clone the scene for multiple environments.

There are many more example usages of the :class:`scene.InteractiveSceneCfg` in the tasks found
under the ``isaaclab_tasks`` extension. Please check out the source code to see
how they are used for more complex scenes.
