3. 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, we introduce the Script Editor inside the GUI environment and the Isaac Python REPL extension as the two interactive scripting options for running python while the stage is open. They are convenient tools for debugging and experimenting. We will reproduce a few selected steps from previous GUI tutorials with Python snippets.

3.1. Learning Objectives

In this tutorial, we will cover

  • Script Editor window and python editing environment

  • Isaac Python REPL extension

  • Adding a Cube using USD API

  • Adding a Cube using Isaac Sim API

3.2. Getting Started

Prerequisites

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

3.3. 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 will pop up, and you can dock somewhere you find convenient.

  2. You can 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.

3.4. REPL Extension

REPL, which stands for “Read–Evaluate–Print loop”, is a programming shell that can read and evaluate small snippets of code and allow users to interactively query the state of the variables inside the environment. IPython, or Jupyter Notebook, is an example of a REPL environment. We provide access to a REPL environment through the Isaac REPL Extension.

  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 with 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 will start in the terminal, and you are all set to start interacting with the stage opened in Isaac Sim via Python. You can copy-paste the snippets below to test it out. You can execute the whole block at once, or line-by-line.

  4. Exit the shell environment by Ctrl+D.

3.5. USD APIs

The underlying format in NVIDIA Omniverse is USD. Below is the 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 and run it, then press Play to simulate.

Note

The following scripts should only be run on an empty new stage and only once.

 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
from pxr import UsdPhysics, PhysxSchema, Gf, PhysicsSchemaTools, UsdGeom
import omni

stage = omni.usd.get_context().get_stage()

# Setting up Physics Scene
gravity = 9.8
scene = UsdPhysics.Scene.Define(stage, "/World/physics")
scene.CreateGravityDirectionAttr().Set(Gf.Vec3f(0.0, 0.0, -1.0))
scene.CreateGravityMagnitudeAttr().Set(gravity)
PhysxSchema.PhysxSceneAPI.Apply(stage.GetPrimAtPath("/World/physics"))
physxSceneAPI = PhysxSchema.PhysxSceneAPI.Get(stage, "/World/physics")
physxSceneAPI.CreateEnableCCDAttr(True)
physxSceneAPI.CreateEnableStabilizationAttr(True)
physxSceneAPI.CreateEnableGPUDynamicsAttr(False)
physxSceneAPI.CreateBroadphaseTypeAttr("MBP")
physxSceneAPI.CreateSolverTypeAttr("TGS")

# Setting up Ground Plane
PhysicsSchemaTools.addGroundPlane(stage, "/World/groundPlane", "Z", 15, Gf.Vec3f(0,0,0), Gf.Vec3f(0.7))

# Adding a Cube
path = "/World/Cube"
cubeGeom = UsdGeom.Cube.Define(stage, path)
cubePrim = stage.GetPrimAtPath(path)
size = 0.5
offset = Gf.Vec3f(0.5,0.2,1.0)
cubeGeom.CreateSizeAttr(size)
cubeGeom.AddTranslateOp().Set(offset)

# Attach Rigid Body and Collision Preset
rigid_api = UsdPhysics.RigidBodyAPI.Apply(cubePrim)
rigid_api.CreateRigidBodyEnabledAttr(True)
UsdPhysics.CollisionAPI.Apply(cubePrim)

3.6. Isaac Sim Core APIs

Raw USD APIs are versatile and detailed but complex, especially for beginners. Omniverse Isaac Sim has a set of core APIs that simplifies some of the frequently used actions for robotics simulators and abstracts away default parameter settings. Here are the same actions of setting up the stage and adding a cuboid with physics and collision presets, as well as setting physics and visual material properties.

Note

The following scripts should only be run on an empty new stage and only once.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import numpy as np
from omni.isaac.core.objects import DynamicCuboid
from omni.isaac.core.objects.ground_plane import GroundPlane
from omni.isaac.core.physics_context import PhysicsContext

PhysicsContext()
GroundPlane(prim_path="/World/groundPlane", size=10, color=np.array([0.5, 0.5, 0.5]))
DynamicCuboid(prim_path="/World/cube",
    position=np.array([-.5, -.2, 1.0]),
    size=np.array([.5, .5, .5]),
    color=np.array([.2,.3,0.]))

Using Isaac Sim Core APIs produces code that is much more lightweight and readable, though you can always resort to using USD APIs to direct the stage whenever necessary.

3.7. Summary

In this tutorial, we introduced

  • the Script Editor window

  • the REPL extension

  • using Python APIs to replace GUI commands

  • the difference between USD APIs and Isaac Sim Core APIs

3.7.1. Next Steps

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

3.7.2. Further Reading

List of all the API documentation at Isaac Sim API Documentation.