1. Hello World

NVIDIA Omniverse™ Kit, the toolkit that Omniverse Isaac Sim uses to build its applications, provides a Python interpreter for scripting. This means every single GUI command, as well as many additional functions are available as Python APIs. However, the learning curve for interfacing with Omniverse Kit using Pixar’s USD Python API is steep and steps are frequently tedious. Therefore we’ve provided a set of APIs that are designed to be used in robotics applications, APIs that abstract away the complexity of USD APIs and merge multiple steps into one for frequently performed tasks.

In this tutorial, we will present the concepts of Core APIs and how to use them. We will start with adding a cube to an empty stage, and we’ll build upon it to create a scene with multiple robots executing mulitple tasks simultaneously, as seen below.

1.1. Learning Objectives

This tutorial series introduces the Core API. After this tutorial, you will learn

  • Creating a World and Scene as defined by the Core API.

  • How to add a rigid body to the Stage and simulate it using python in Omniverse Isaac Sim.

  • The difference between running python in an Extension Workflow vs a Standalone Workflow, and in Jupyter Notebook.

10-15 Minute Tutorial

1.2. Getting Started

Prerequisites

Begin by opening the Hello World example. Go to the top Menu Bar and Click Isaac Examples > Hello World.

The window for the Hello World example extension should now be visible in the workspace. Click the Open Source Code button to launch the source code for editing in Visual Studio Code.

Click the Open Containing Folder button to open the directory containing the example files. This folder contains three files: hello_world.py, hello_world_extension.py, and __init__.py.

The hello_world.py script is where the logic of the application will be added, while the UI elements of the application will be added in hello_world_extension.py script and thus linked to the logic.

  1. Click the LOAD button to load the World.

  2. click File > New From Stage Template > Empty to create a new stage.

  3. Click the LOAD button to load the World again.

  4. Open hello_world.py and press “Ctrl+S” to use the hot-reload feature. You will notice that the menu disappears from the workspace (because it was restarted).

  5. Open the example menu again and click the LOAD button.

Now you can begin adding to this example.

1.3. Code Overview

This example inherits from BaseSample, which is a boilerplate extension application that sets up the basics for every robotics extension application. The following are a few examples of the actions BaseSample performs:

  1. Loading the world with its corresponding assets using a button

  2. Clearing the world when a new stage is created.

  3. Resetting the world’s objects to their default states

  4. Handling hot reloading

World is the core class that enables you to interact with the simulator in an easy and modular way. It handles many time-related events such as adding callbacks, stepping physics, resetting the scene, adding tasks (this will be covered later in Adding a Manipulator Robot), etc.

A world contains an instance of a Scene. The Scene class manages simulation assets of interest in the USD Stage. It provides an easy API to add, manipulate, inspect, and reset different USD assets in the stage.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from omni.isaac.examples.base_sample import BaseSample #boiler plate of a robotics extension application

class HelloWorld(BaseSample):
    def __init__(self) -> None:
        super().__init__()
        return

    # This function is called to setup the assets in the scene for the first time
    # Class variables should not be assigned here, since this function is not called
    # after a hot-reload, its only called to load the world starting from an EMPTY stage
    def setup_scene(self):
        # A world is defined in the BaseSample, can be accessed everywhere EXCEPT __init__
        world = self.get_world()
        world.scene.add_default_ground_plane() # adds a default ground plane to the scene
        return

1.3.1. Singleton World

World is a Singleton, which means only one World can exist while running Omniverse Isaac Sim. The code below demonstrates how to retrieve the current instance of the World across different files and extensions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from omni.isaac.examples.base_sample import BaseSample
from omni.isaac.core import World

class HelloWorld(BaseSample):
    def __init__(self) -> None:
        super().__init__()
        return

    def setup_scene(self):
        world = World.instance()
        world.scene.add_default_ground_plane()
        return

1.4. Adding to the Scene

Next, use the Python API to add a cube as a rigid body to the scene.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from omni.isaac.examples.base_sample import BaseSample
import numpy as np
# Can be used to create a new cube or to point to an already existing cube in stage.
from omni.isaac.core.objects import DynamicCuboid

class HelloWorld(BaseSample):
    def __init__(self) -> None:
        super().__init__()
        return

    def setup_scene(self):
        world = self.get_world()
        world.scene.add_default_ground_plane()
        fancy_cube = world.scene.add(
            DynamicCuboid(
                prim_path="/World/random_cube", # The prim path of the cube in the USD stage
                name="fancy_cube", # The unique name used to retrieve the object from the scene later on
                position=np.array([0, 0, 1.0]), # Using the current stage units which is in meters by default.
                scale=np.array([0.5015, 0.5015, 0.5015]), # most arguments accept mainly numpy arrays.
                color=np.array([0, 0, 1.0]), # RGB channels, going from 0-1
            ))
        return
  1. Press Ctrl+S to save the code and hot-reload Omniverse Isaac Sim.

  2. Open the menu again.

  3. click File > New From Stage Template > Empty, then the LOAD button. You need to perform this action if you change anything in the setup_scene. Otherwise, you only need to press the LOAD button.

  4. Press the PLAY button to start simulating the dynamic cube and see it falling.

Note

Every time the code is edited/changed, press “Ctrl+S” to save the code and hot-reload Omniverse Isaac Sim.

1.5. Inspecting Object Properties

Next, print the world pose and velocity of the cube.

 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
from omni.isaac.examples.base_sample import BaseSample
import numpy as np
from omni.isaac.core.objects import DynamicCuboid

class HelloWorld(BaseSample):
    def __init__(self) -> None:
        super().__init__()
        return

    def setup_scene(self):
        world = self.get_world()
        world.scene.add_default_ground_plane()
        fancy_cube = world.scene.add(
            DynamicCuboid(
                prim_path="/World/random_cube",
                name="fancy_cube",
                position=np.array([0, 0, 1.0]),
                scale=np.array([0.5015, 0.5015, 0.5015]),
                color=np.array([0, 0, 1.0]),
            ))
        return

    # Here we assign the class's variables
    # this function is called after load button is pressed
    # regardless starting from an empty stage or not
    # this is called after setup_scene and after
    # one physics time step to propagate appropriate
    # physics handles which are needed to retrieve
    # many physical properties of the different objects
    async def setup_post_load(self):
        self._world = self.get_world()
        self._cube = self._world.scene.get_object("fancy_cube")
        position, orientation = self._cube.get_world_pose()
        linear_velocity = self._cube.get_linear_velocity()
        # will be shown on terminal
        print("Cube position is : " + str(position))
        print("Cube's orientation is : " + str(orientation))
        print("Cube's linear velocity is : " + str(linear_velocity))
        return

1.5.1. Continuously Inspecting the Object Properties during Simulation

Next, print the world pose and velocity of the cube during simulation at every physics step executed. As mentioned in Isaac Sim Workflows, in this workflow the application is running asynchronously and can’t control when to step physics. However, you can add callbacks to ensure certain things happen before certain events.

Add a physics callback as follows.

 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
from omni.isaac.examples.base_sample import BaseSample
import numpy as np
from omni.isaac.core.objects import DynamicCuboid

class HelloWorld(BaseSample):
    def __init__(self) -> None:
        super().__init__()
        return

    def setup_scene(self):
        world = self.get_world()
        world.scene.add_default_ground_plane()
        fancy_cube = world.scene.add(
            DynamicCuboid(
                prim_path="/World/random_cube",
                name="fancy_cube",
                position=np.array([0, 0, 1.0]),
                scale=np.array([0.5015, 0.5015, 0.5015]),
                color=np.array([0, 0, 1.0]),
            ))
        return

    async def setup_post_load(self):
        self._world = self.get_world()
        self._cube = self._world.scene.get_object("fancy_cube")
        self._world.add_physics_callback("sim_step", callback_fn=self.print_cube_info) #callback names have to be unique
        return

    # here we define the physics callback to be called before each physics step, all physics callbacks must take
    # step_size as an argument
    def print_cube_info(self, step_size):
        position, orientation = self._cube.get_world_pose()
        linear_velocity = self._cube.get_linear_velocity()
        # will be shown on terminal
        print("Cube position is : " + str(position))
        print("Cube's orientation is : " + str(orientation))
        print("Cube's linear velocity is : " + str(linear_velocity))

1.6. Adding a New Example in the Menu

So far, you have been editing the Hello World example. Next, you will create a new example under the Isaac Examples menu.

  1. Copy the current files to the user_examples folder under extension_examples

    cd extension_examples
    cp hello_world/hello_world* user_examples/
    
  2. Edit the extension_examples/user_examples/__init__.py file by adding the following lines.

    1
    2
    from omni.isaac.examples.user_examples.hello_world import HelloWorld
    from omni.isaac.examples.user_examples.hello_world_extension import HelloWorldExtension
    
  3. Edit the extension_examples/user_examples/hello_world_extension.py file as follows.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    import os
    from omni.isaac.examples.base_sample import BaseSampleExtension
    from omni.isaac.examples.user_examples import HelloWorld
    
    
    class HelloWorldExtension(BaseSampleExtension):
        def on_startup(self, ext_id: str):
            super().on_startup(ext_id)
            super().start_extension(
                menu_name="",
                submenu_name="",
                name="Awesome Example",
                title="My Awesome Example",
                doc_link="https://docs.omniverse.nvidia.com/app_isaacsim/app_isaacsim/tutorial_core_hello_world.html",
                overview="This Example introduces the user on how to do cool stuff with Isaac Sim through scripting in asynchronous mode.",
                file_path=os.path.abspath(__file__),
                sample=HelloWorld(),
            )
            return
    

Note

Every time the code is edited/changed, Press Ctrl+S to save the code and hot-reload Omniverse Isaac Sim.

1.7. Converting the Example to a Standalone Application

As mentioned in Isaac Sim Workflows, in this workflow, the robotics application is started when launched from Python right away, and you can control when to step physics and rendering.

  1. Open a new my_application.py file and add the following.

     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
    #launch Isaac Sim before any other imports
    #default first two lines in any standalone application
    from omni.isaac.kit import SimulationApp
    simulation_app = SimulationApp({"headless": False}) # we can also run as headless.
    
    from omni.isaac.core import World
    from omni.isaac.core.objects import DynamicCuboid
    import numpy as np
    
    world = World()
    world.scene.add_default_ground_plane()
    fancy_cube =  world.scene.add(
        DynamicCuboid(
            prim_path="/World/random_cube",
            name="fancy_cube",
            position=np.array([0, 0, 1.0]),
            scale=np.array([0.5015, 0.5015, 0.5015]),
            color=np.array([0, 0, 1.0]),
        ))
    # Resetting the world needs to be called before querying anything related to an articulation specifically.
    # Its recommended to always do a reset after adding your assets, for physics handles to be propagated properly
    world.reset()
    for i in range(500):
        position, orientation = fancy_cube.get_world_pose()
        linear_velocity = fancy_cube.get_linear_velocity()
        # will be shown on terminal
        print("Cube position is : " + str(position))
        print("Cube's orientation is : " + str(orientation))
        print("Cube's linear velocity is : " + str(linear_velocity))
        # we have control over stepping physics and rendering in this workflow
        # things run in sync
        world.step(render=True) # execute one physics step and one rendering step
    
    simulation_app.close() # close Isaac Sim
    
  2. Run it using ./python.sh /extension_examples/user_examples/my_application.py.

1.8. Converting the example to a Standalone Application using Jupyter Notebook

  1. Open a Jupyter notebook.

./jupyter_notebook.sh standalone_examples/notebooks/hello_world.ipynb
  1. Access the notebook through the browser as shown on the terminal.

  2. Execute the first cell in the notebook.

    1
    2
    3
    4
    5
    6
    import getpass
    user = getpass.getuser()
    from omni.isaac.kit import SimulationApp
    # Set the path below to your desired nucleus server
    # Make sure you installed a local nucleus server before this
    simulation_app = SimulationApp({"livesync_usd": f'omniverse://localhost/Users/{user}/temp_jupyter_stage.usd'})
    
  3. Open the same USD file after launching Isaac Sim through the launcher or the terminal and turn the live syncing option on.

    Note

    DON’T press play, pause, or stop in the GUI while in this workflow.

    ../_images/isaac_sim_live_sync.gif
  4. Run through the rest of the cells in the notebook, while viewing the stage directly in the Omniverse Isaac Sim GUI.

Note

A .render call is needed at the end of each cell to propagate the changes to the synced USD (i.e.`` World.render`` or SimulationContext.render)

Note

Use world.clear() or clear_stage at the beginning of a cell for iterative development.

1.9. Summary

This tutorial covered the following topics:

  1. Overview of the World and Scene classes.

  2. Adding content to the Scene via Python.

  3. Adding callbacks.

  4. Accessing dynamic properties for objects

  5. The main differences in a standalone application

  6. The main differences when developing an application using jupyter

1.9.1. Next Steps

Continue to Hello Robot to learn how to add a robot to the simulation.

Note

The next tutorials will be developed mainly using the extensions application workflow. However, conversion to other workflows should be straightforward given what was covered in this tutorial.