5. Script Editor

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, a python interface that can be used to run snippets of python scripts. This is a convenient tool for debugging and experimenting. We will reproduce a few selected steps from previous GUI tutorials with Python snippets.

5.1. Learning Objectives

In this tutorial, we will cover

  • Script Editor window and python editing environment

  • Adding a Cube using USD API

  • Adding a Cube using Isaac Sim API

5.2. Getting Started

Prerequisites

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

  1. Let’s first open up the script editor window. Go to the Menu Bar and click Window > Script Editor. A window will pop up and you can dock with somewhere you find convenient.

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

5.3. USD APIs

The underlying format in NVIDIA Omniverse is USD. Below is the script to setup 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
import omni

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

# Setting up Physics Scene
gravity = 980
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", 1500, Gf.Vec3f(0,0,0), Gf.Vec3f(0.5))

# Adding a Cube
path = "/World/Cube"
cubeGeom = UsdGeom.Cube.Define(stage, path)
cubePrim = stage.GetPrimAtPath(path)
size = 50
offset = Gf.Vec3f(50,20,100)
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)

5.4. 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 setting. Here is 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
import numpy as np
from omni.isaac.core.objects import DynamicCuboid
from omni.isaac.core.objects.ground_plane import GroundPlane

PhysicsContext()
GroundPlane(prim_path="/World/groundPlane", size=1000, color=np.array([0.5, 0.5, 0.5]))
DynamicCuboid(prim_path="/World/cube",
    position=np.array([50, 20, 100.0]),
    size=np.array([50, 50, 50]),
    color=np.array([20,30,0]))

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

5.5. Summary

In this tutorial, we introduced

  • the Script Editor window

  • using Python APIs to replace GUI commands

  • difference between USD APIs and Isaac Sim Core APIs

5.5.1. Next Steps

Continue on to Working with USD to learn how to work with usd files inside Omniverse Isaac Sim.

5.5.2. Further Reading

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