Creating an empty scene#
This tutorial shows how to launch and control Isaac Sim simulator from a standalone Python script. It sets up an
empty scene in Isaac Lab and introduces the two main classes used in the framework, app.AppLauncher
and
sim.SimulationContext
.
Please review Isaac Sim Interface and Isaac Sim Workflows prior to beginning this tutorial to get an initial understanding of working with the simulator.
The Code#
The tutorial corresponds to the create_empty.py
script in the source/standalone/tutorials/00_sim
directory.
Code for create_empty.py
1# Copyright (c) 2022-2024, The Isaac Lab Project Developers.
2# All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5
6"""This script demonstrates how to create a simple stage in Isaac Sim.
7
8.. code-block:: bash
9
10 # Usage
11 ./isaaclab.sh -p source/standalone/tutorials/00_sim/create_empty.py
12
13"""
14
15"""Launch Isaac Sim Simulator first."""
16
17
18import argparse
19
20from omni.isaac.lab.app import AppLauncher
21
22# create argparser
23parser = argparse.ArgumentParser(description="Tutorial on creating an empty stage.")
24# append AppLauncher cli args
25AppLauncher.add_app_launcher_args(parser)
26# parse the arguments
27args_cli = parser.parse_args()
28# launch omniverse app
29app_launcher = AppLauncher(args_cli)
30simulation_app = app_launcher.app
31
32"""Rest everything follows."""
33
34from omni.isaac.lab.sim import SimulationCfg, SimulationContext
35
36
37def main():
38 """Main function."""
39
40 # Initialize the simulation context
41 sim_cfg = SimulationCfg(dt=0.01)
42 sim = SimulationContext(sim_cfg)
43 # Set main camera
44 sim.set_camera_view([2.5, 2.5, 2.5], [0.0, 0.0, 0.0])
45
46 # Play the simulator
47 sim.reset()
48 # Now we are ready!
49 print("[INFO]: Setup complete...")
50
51 # Simulate physics
52 while simulation_app.is_running():
53 # perform step
54 sim.step()
55
56
57if __name__ == "__main__":
58 # run the main function
59 main()
60 # close sim app
61 simulation_app.close()
The Code Explained#
Launching the simulator#
The first step when working with standalone Python scripts is to launch the simulation application. This is necessary to do at the start since various dependency modules of Isaac Sim are only available after the simulation app is running.
This can be done by importing the app.AppLauncher
class. This utility class wraps around
omni.isaac.kit.SimulationApp
class to launch the simulator. It provides mechanisms to
configure the simulator using command-line arguments and environment variables.
For this tutorial, we mainly look at adding the command-line options to a user-defined
argparse.ArgumentParser
. This is done by passing the parser instance to the
app.AppLauncher.add_app_launcher_args()
method, which appends different parameters
to it. These include launching the app headless, configuring different Livestream options,
and enabling off-screen rendering.
import argparse
from omni.isaac.lab.app import AppLauncher
# create argparser
parser = argparse.ArgumentParser(description="Tutorial on creating an empty stage.")
# append AppLauncher cli args
AppLauncher.add_app_launcher_args(parser)
# parse the arguments
args_cli = parser.parse_args()
# launch omniverse app
app_launcher = AppLauncher(args_cli)
simulation_app = app_launcher.app
Importing python modules#
Once the simulation app is running, it is possible to import different Python modules from Isaac Sim and other libraries. Here we import the following module:
omni.isaac.lab.sim
: A sub-package in Isaac Lab for all the core simulator-related operations.
from omni.isaac.lab.sim import SimulationCfg, SimulationContext
Configuring the simulation context#
When launching the simulator from a standalone script, the user has complete control over playing, pausing and stepping the simulator. All these operations are handled through the simulation context. It takes care of various timeline events and also configures the physics scene for simulation.
In Isaac Lab, the sim.SimulationContext
class inherits from Isaac Sim’s
omni.isaac.core.simulation_context.SimulationContext
to allow configuring the simulation
through Python’s dataclass
object and handle certain intricacies of the simulation stepping.
For this tutorial, we set the physics and rendering time step to 0.01 seconds. This is done
by passing these quantities to the sim.SimulationCfg
, which is then used to create an
instance of the simulation context.
# Initialize the simulation context
sim_cfg = SimulationCfg(dt=0.01)
sim = SimulationContext(sim_cfg)
# Set main camera
sim.set_camera_view([2.5, 2.5, 2.5], [0.0, 0.0, 0.0])
Following the creation of the simulation context, we have only configured the physics acting on the simulated scene. This includes the device to use for simulation, the gravity vector, and other advanced solver parameters. There are now two main steps remaining to run the simulation:
Designing the simulation scene: Adding sensors, robots and other simulated objects
Running the simulation loop: Stepping the simulator, and setting and getting data from the simulator
In this tutorial, we look at Step 2 first for an empty scene to focus on the simulation control first. In the following tutorials, we will look into Step 1 and working with simulation handles for interacting with the simulator.
Running the simulation#
The first thing, after setting up the simulation scene, is to call the sim.SimulationContext.reset()
method. This method plays the timeline and initializes the physics handles in the simulator. It must always
be called the first time before stepping the simulator. Otherwise, the simulation handles are not initialized
properly.
Note
sim.SimulationContext.reset()
is different from sim.SimulationContext.play()
method as the latter
only plays the timeline and does not initializes the physics handles.
After playing the simulation timeline, we set up a simple simulation loop where the simulator is stepped repeatedly
while the simulation app is running. The method sim.SimulationContext.step()
takes in as argument render
,
which dictates whether the step includes updating the rendering-related events or not. By default, this flag is
set to True.
# Play the simulator
sim.reset()
# Now we are ready!
print("[INFO]: Setup complete...")
# Simulate physics
while simulation_app.is_running():
# perform step
sim.step()
Exiting the simulation#
Lastly, the simulation application is stopped and its window is closed by calling
omni.isaac.kit.SimulationApp.close()
method.
# close sim app
simulation_app.close()
The Code Execution#
Now that we have gone through the code, let’s run the script and see the result:
./isaaclab.sh -p source/standalone/tutorials/00_sim/create_empty.py
The simulation should be playing, and the stage should be rendering. To stop the simulation,
you can either close the window, or press Ctrl+C
in the terminal.
Passing --help
to the above script will show the different command-line arguments added
earlier by the app.AppLauncher
class. To run the script headless, you can execute the
following:
./isaaclab.sh -p source/standalone/tutorials/00_sim/create_empty.py --headless
Now that we have a basic understanding of how to run a simulation, let’s move on to the following tutorial where we will learn how to add assets to the stage.