2.6. 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.

2.6.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

2.6.2. Getting Started

Prerequisites

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

2.6.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.

2.6.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 (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 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.

2.6.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.

 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)

2.6.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.

 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 much more lightweight and readable, though you can always resort to using USD APIs to direct the stage whenever necessary.

2.6.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

2.6.7.1. Next Steps

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

2.6.7.2. Further Reading

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