Interactive Scripting

Every action in the GUI has its corresponding Python APIs. Therefore everything that’s done in the GUI can be done with a Python command (but not the reverse, not all Python APIs have corresponding GUI). In this tutorial, you learn about the Script Editor inside the GUI environment and the Isaac Python REPL extension as two interactive scripting options for running Python while the stage is open. They are convenient tools for debugging and experimenting. You are also guided through reproducing a few selected steps from previous GUI tutorials with Python snippets.

Learning Objectives

This tutorial covers:

  • Script Editor window and Python editing environment

  • Isaac Python REPL extension

  • Adding a Cube using USD API

  • Adding a Cube using Isaac Sim API

Getting Started

Prerequisites

  • Complete Add Simple Objects to have a basic understanding of the process adding an object onto the stage.

Script Editor

Script Editor is a Python editing environment internal to Omniverse Kit. It can be used to run snippets of python code to interact with the stage.

  1. To open the script editor window, go to the Menu Bar and click Window > Script Editor. A window appears and you can dock it somewhere you find convenient.

  2. Open multiple tabs by going to the Tab Menu under Script Editor. All the tabs share the same environment, so libraries that are imported or variables defined in one environment can be accessed and used in other environments.

REPL Extension

REPL, which stands for “Read–Evaluate–Print loop”, is a programming shell that can read and evaluate small snippets of code and allows you to interactively query the state of the variables inside the environment. IPython, or Jupyter Notebook, is an example of a REPL environment. Access to a REPL environment is provided through the Isaac REPL Extension (Linux only).

  1. Make sure there is an instance of Isaac Sim already running.

  2. Enable the REPL Extension. Go to Windows > Extensions, search for Isaac Sim REPL, and enable it if it’s not already. Check Autoload if you wish to have the extensions always enabled when you start Isaac Sim.

  3. Open a new terminal, and on the command line run telnet localhost 8223. A Python shell starts in the terminal, and you are all set to start interacting with the stage opened in Isaac Sim via Python.

  4. You can copy-paste the snippets below to test it by executing the whole block immediately, or line-by-line.

  5. Exit the shell environment by Ctrl+D.

USD APIs

The underlying format in NVIDIA Omniverse is USD. Below is a script to set up a ground plane, a default light, and a cuboid with physics and collision presets using raw USD APIs.

Start with a fresh stage, copy and paste the code into the Script Editor window, run it, and press Play to simulate.

Note

Only run the following scripts on an empty new stage and only run it once.

 1from pxr import UsdPhysics, PhysxSchema, Gf, PhysicsSchemaTools, UsdGeom
 2import omni
 3
 4stage = omni.usd.get_context().get_stage()
 5
 6# Setting up Physics Scene
 7gravity = 9.8
 8scene = UsdPhysics.Scene.Define(stage, "/World/physics")
 9scene.CreateGravityDirectionAttr().Set(Gf.Vec3f(0.0, 0.0, -1.0))
10scene.CreateGravityMagnitudeAttr().Set(gravity)
11PhysxSchema.PhysxSceneAPI.Apply(stage.GetPrimAtPath("/World/physics"))
12physxSceneAPI = PhysxSchema.PhysxSceneAPI.Get(stage, "/World/physics")
13physxSceneAPI.CreateEnableCCDAttr(True)
14physxSceneAPI.CreateEnableStabilizationAttr(True)
15physxSceneAPI.CreateEnableGPUDynamicsAttr(False)
16physxSceneAPI.CreateBroadphaseTypeAttr("MBP")
17physxSceneAPI.CreateSolverTypeAttr("TGS")
18
19# Setting up Ground Plane
20PhysicsSchemaTools.addGroundPlane(stage, "/World/groundPlane", "Z", 15, Gf.Vec3f(0,0,0), Gf.Vec3f(0.7))
21
22# Adding a Cube
23path = "/World/Cube"
24cubeGeom = UsdGeom.Cube.Define(stage, path)
25cubePrim = stage.GetPrimAtPath(path)
26size = 0.5
27offset = Gf.Vec3f(0.5,0.2,1.0)
28cubeGeom.CreateSizeAttr(size)
29cubeGeom.AddTranslateOp().Set(offset)
30
31# Attach Rigid Body and Collision Preset
32rigid_api = UsdPhysics.RigidBodyAPI.Apply(cubePrim)
33rigid_api.CreateRigidBodyEnabledAttr(True)
34UsdPhysics.CollisionAPI.Apply(cubePrim)

Isaac Sim Core APIs

Raw USD APIs are versatile and detailed but complex, especially for beginners. Isaac Sim has a set of core APIs that simplify some of the frequently used actions for robotics simulators. These APIs abstract away default parameter settings. The following APIs:

  • Set up the stage

  • Add a cuboid with physics and collision presets

  • Set physics and visual material properties

Note

Only run the following scripts on an empty new stage and only run it once.

 1import numpy as np
 2from omni.isaac.core.objects import DynamicCuboid
 3from omni.isaac.core.objects.ground_plane import GroundPlane
 4from omni.isaac.core.physics_context import PhysicsContext
 5
 6PhysicsContext()
 7GroundPlane(prim_path="/World/groundPlane", size=10, color=np.array([0.5, 0.5, 0.5]))
 8DynamicCuboid(prim_path="/World/cube",
 9    position=np.array([-.5, -.2, 1.0]),
10    scale=np.array([.5, .5, .5]),
11    color=np.array([.2,.3,0.]))

Using Isaac Sim Core APIs produces code that is lightweight and readable. When necessary you can always use USD APIs to direct the stage.

Summary

In this tutorial, you learned about the:

  • Script Editor window

  • REPL extension

  • Python APIs used to replace GUI commands

  • Difference between USD APIs and Isaac Sim Core APIs

Next Steps

Continue on to OmniGraph to learn about OmniGraph and how to use it for controlling a robot.

Further Reading

List of all the API documentation at API Documentation.