Isaac Sim Setup Code Examples
1. Basic Isaac Sim Environment Setup
# Example: Initialize Isaac Sim environment
import omni
from pxr import Gf, UsdGeom, Sdf
import carb
def setup_isaac_sim_environment():
"""Initialize Isaac Sim environment with physics and rendering"""
# Get the USD stage
stage = omni.usd.get_context().get_stage()
# Create a physics scene
scene_path = Sdf.Path("/World/physicsScene")
physics_scene = UsdGeom.Xform.Define(stage, scene_path)
# Configure physics scene properties
from omni.physx.scripts import physicsUtils
physicsUtils.add_physics_scene_to_stage(stage, scene_path)
# Set gravity
gravity_path = scene_path.AppendChild("gravity")
stage.GetPrimAtPath(scene_path).GetAttribute("physxScene:gravity").Set(
Gf.Vec3f(0.0, 0.0, -9.81)
)
# Create ground plane
plane_path = Sdf.Path("/World/groundPlane")
plane = UsdGeom.Mesh.Define(stage, plane_path)
UsdGeom.SetStageMetersPerUnit(stage, 1.0)
carb.log_info("Isaac Sim environment initialized with physics scene")
return stage
# Example: Create a simple environment
def create_simple_environment():
"""Create a simple environment with basic objects"""
stage = omni.usd.get_context().get_stage()
# Create some objects in the scene
from pxr import Gf, UsdGeom
# Create a cube
cube_path = Sdf.Path("/World/Cube")
cube = UsdGeom.Cube.Define(stage, cube_path)
cube.GetSizeAttr().Set(0.5)
# Position the cube
xform = UsdGeom.Xformable(stage.GetPrimAtPath(cube_path))
xform.AddTranslateOp().Set(Gf.Vec3f(1.0, 0.0, 0.25))
# Create a sphere
sphere_path = Sdf.Path("/World/Sphere")
sphere = UsdGeom.Sphere.Define(stage, sphere_path)
sphere.GetRadiusAttr().Set(0.25)
# Position the sphere
xform_sphere = UsdGeom.Xformable(stage.GetPrimAtPath(sphere_path))
xform_sphere.AddTranslateOp().Set(Gf.Vec3f(-1.0, 0.0, 0.25))
carb.log_info("Simple environment created with cube and sphere")
2. Robot Model Creation in Isaac Sim
# Example: Create a differential drive robot
def create_differential_drive_robot(robot_name, position=(0, 0, 0.3)):
"""Create a differential drive robot in Isaac Sim"""
stage = omni.usd.get_context().get_stage()
# Create robot root
robot_path = f"/World/{robot_name}"
robot_prim = stage.DefinePrim(robot_path, "Xform")
# Create chassis
chassis_path = f"{robot_path}/Chassis"
chassis_xform = UsdGeom.Xform.Define(stage, chassis_path)
chassis_mesh = UsdGeom.Cube.Define(stage, f"{chassis_path}/Mesh")
chassis_mesh.GetSizeAttr().Set(0.3)
# Position chassis
chassis_xform.AddTranslateOp().Set(Gf.Vec3f(position[0], position[1], position[2]))
# Create left wheel
left_wheel_path = f"{robot_path}/LeftWheel"
left_wheel_xform = UsdGeom.Xform.Define(stage, left_wheel_path)
left_wheel_mesh = UsdGeom.Cylinder.Define(stage, f"{left_wheel_path}/Mesh")
left_wheel_mesh.GetRadiusAttr().Set(0.1)
left_wheel_mesh.GetHeightAttr().Set(0.05)
left_wheel_xform.AddTranslateOp().Set(Gf.Vec3f(0, 0.15, 0))
# Create right wheel
right_wheel_path = f"{robot_path}/RightWheel"
right_wheel_xform = UsdGeom.Xform.Define(stage, right_wheel_path)
right_wheel_mesh = UsdGeom.Cylinder.Define(stage, f"{right_wheel_path}/Mesh")
right_wheel_mesh.GetRadiusAttr().Set(0.1)
right_wheel_mesh.GetHeightAttr().Set(0.05)
right_wheel_xform.AddTranslateOp().Set(Gf.Vec3f(0, -0.15, 0))
# Add rigid body properties
from omni.physx.scripts import physicsUtils
# Add rigid body to chassis
chassis_rigid_body = physicsUtils.add_rigidBody(
stage.GetPrimAtPath(f"{chassis_path}/Mesh"),
name="chassis_body",
density=1000
)
# Add rigid body to wheels
left_wheel_rigid_body = physicsUtils.add_rigidBody(
stage.GetPrimAtPath(f"{left_wheel_path}/Mesh"),
name="left_wheel_body",
density=800
)
right_wheel_rigid_body = physicsUtils.add_rigidBody(
stage.GetPrimAtPath(f"{right_wheel_path}/Mesh"),
name="right_wheel_body",
density=800
)
carb.log_info(f"Differential drive robot '{robot_name}' created at position {position}")
return robot_path
# Example usage
robot_path = create_differential_drive_robot("MyRobot", (0, 0, 0.3))
3. Physics Configuration
# Example: Configure physics properties for objects
def configure_physics_properties(prim_path, mass=1.0, friction=0.5, restitution=0.2):
"""Configure physics properties for a USD prim"""
stage = omni.usd.get_context().get_stage()
prim = stage.GetPrimAtPath(prim_path)
# Apply rigid body API
from pxr import PhysxSchema
rigid_body_api = PhysxSchema.PhysxRigidBodyAPI.Apply(prim)
# Apply mass API
from pxr import UsdPhysics
mass_api = UsdPhysics.MassAPI.Apply(prim)
mass_api.GetMassAttr().Set(mass)
# Apply material properties
material_api = PhysxSchema.PhysxMaterialAPI.Apply(prim)
material_api.GetStaticFrictionAttr().Set(friction)
material_api.GetDynamicFrictionAttr().Set(friction)
material_api.GetRestitutionAttr().Set(restitution)
carb.log_info(f"Physics properties configured for {prim_path}")
# Example usage
configure_physics_properties("/World/MyRobot/Chassis/Mesh", mass=2.0, friction=0.8)
4. Sensor Integration
# Example: Add sensors to the robot
def add_camera_sensor_to_robot(robot_path, sensor_name, position=(0.1, 0, 0.1), rotation=(0, 0, 0)):
"""Add a camera sensor to a robot"""
stage = omni.usd.get_context().get_stage()
# Create sensor prim
sensor_path = f"{robot_path}/{sensor_name}"
sensor_xform = UsdGeom.Xform.Define(stage, sensor_path)
sensor_xform.AddTranslateOp().Set(Gf.Vec3f(*position))
sensor_xform.AddRotateXYZOp().Set(Gf.Vec3f(*rotation))
# Configure camera properties
from omni.isaac.core.utils.prims import create_prim
from omni.isaac.sensor import Camera
# Create the camera sensor
camera = Camera(
prim_path=sensor_path,
translation=position,
orientation=(1, 0, 0, 0) # Default orientation
)
# Set camera properties
camera.focal_length = 24.0
camera.horizontal_aperture = 20.955
camera.vertical_aperture = 15.2908
camera.resolution = (640, 480)
carb.log_info(f"Camera sensor '{sensor_name}' added to robot at {robot_path}")
return sensor_path
def add_lidar_sensor_to_robot(robot_path, sensor_name, position=(0, 0, 0.2)):
"""Add a LiDAR sensor to a robot"""
stage = omni.usd.get_context().get_stage()
# Create LiDAR prim
lidar_path = f"{robot_path}/{sensor_name}"
# Import the range sensor extension
from omni.isaac.range_sensor import _range_sensor
import omni.isaac.core.utils.prims as prim_utils
# Create the LiDAR prim
lidar_prim = prim_utils.create_prim(
prim_path=lidar_path,
prim_type="RotatingLidarSensor",
translation=position
)
# Configure LiDAR properties
lidar_sensor = _range_sensor.acquire_lidar_sensor_interface()
carb.log_info(f"LiDAR sensor '{sensor_name}' added to robot at {robot_path}")
return lidar_path
# Example usage
camera_path = add_camera_sensor_to_robot("/World/MyRobot", "FrontCamera", (0.15, 0, 0.1))
lidar_path = add_lidar_sensor_to_robot("/World/MyRobot", "FrontLidar", (0, 0, 0.2))
5. ROS Bridge Setup
# Example: Configure ROS bridge for Isaac Sim
def setup_ros_bridge(robot_path):
"""Set up ROS bridge for the robot"""
import omni
from omni.isaac.core.utils.extensions import enable_extension
# Enable ROS bridge extension
enable_extension("omni.isaac.ros_bridge")
# Import ROS bridge utilities
from omni.isaac.ros_bridge.scripts import ros_bridge_utils
# Create ROS publishers and subscribers for the robot
robot_name = robot_path.split('/')[-1]
# Set up TF publishing
tf_publisher = ros_bridge_utils.create_tf_publisher(
topic_name="/tf",
frame_id=f"{robot_name}/base_link"
)
# Set up joint state publisher
joint_state_publisher = ros_bridge_utils.create_joint_state_publisher(
topic_name=f"/{robot_name}/joint_states"
)
# Set up robot state publisher
robot_state_publisher = ros_bridge_utils.create_robot_state_publisher(
topic_name=f"/{robot_name}/robot_description",
urdf_path=f"/robots/{robot_name}.urdf"
)
carb.log_info(f"ROS bridge configured for robot {robot_name}")
return True
# Example usage
# setup_ros_bridge("/World/MyRobot")
6. Complete Isaac Sim Configuration Script
# Complete Isaac Sim setup script
import omni
from pxr import Gf, UsdGeom, Sdf
import carb
def complete_isaac_sim_setup():
"""Complete Isaac Sim setup with environment, robot, and sensors"""
carb.log_info("Starting complete Isaac Sim setup...")
# Initialize environment
stage = setup_isaac_sim_environment()
# Create simple environment
create_simple_environment()
# Create robot
robot_path = create_differential_drive_robot("MyRobot", (0, 0, 0.3))
# Configure physics properties
configure_physics_properties("/World/MyRobot/Chassis/Mesh", mass=2.0, friction=0.8)
configure_physics_properties("/World/MyRobot/LeftWheel/Mesh", mass=0.5, friction=0.9)
configure_physics_properties("/World/MyRobot/RightWheel/Mesh", mass=0.5, friction=0.9)
# Add sensors
camera_path = add_camera_sensor_to_robot(
"/World/MyRobot",
"FrontCamera",
position=(0.15, 0, 0.1)
)
lidar_path = add_lidar_sensor_to_robot(
"/World/MyRobot",
"FrontLidar",
position=(0, 0, 0.2)
)
carb.log_info("Complete Isaac Sim setup finished successfully!")
# Print summary
print(f"Environment setup with robot at: {robot_path}")
print(f"Camera sensor at: {camera_path}")
print(f"LiDAR sensor at: {lidar_path}")
return robot_path, camera_path, lidar_path
# Run the complete setup
if __name__ == "__main__":
robot_path, camera_path, lidar_path = complete_isaac_sim_setup()
7. Isaac Sim Extension Example
# Example: Isaac Sim extension for robot control
import omni.ext
import omni.ui as ui
import omni.kit.ui
from omni.kit.menu.utils import set_menu_enabled
import omni.usd
from pxr import Gf
class IsaacSimRobotControlExtension(omni.ext.IExt):
def on_startup(self, ext_id):
print("[isaac_sim_robot_control] Isaac Sim Robot Control Extension Startup")
# Create menu entry
self._window = ui.Window("Isaac Sim Robot Control", width=300, height=300)
self._build_ui()
def _build_ui(self):
with self._window.frame:
with ui.VStack():
ui.Label("Isaac Sim Robot Control Panel")
# Robot selection
with ui.HStack():
ui.Label("Robot:")
self._robot_selector = ui.ComboBox(0, "MyRobot", "Robot2", "Robot3")
# Velocity controls
with ui.HStack():
ui.Label("Linear Vel:")
self._linear_vel_field = ui.FloatField()
self._linear_vel_field.model.set_value(0.5)
with ui.HStack():
ui.Label("Angular Vel:")
self._angular_vel_field = ui.FloatField()
self._angular_vel_field.model.set_value(0.0)
# Control button
self._control_btn = ui.Button("Send Command")
self._control_btn.set_clicked_fn(self._send_command)
# Status label
self._status_label = ui.Label("Ready")
def _send_command(self):
robot_idx = self._robot_selector.model.get_item_value_model().get_value_as_int()
robot_names = ["MyRobot", "Robot2", "Robot3"]
selected_robot = robot_names[robot_idx] if robot_idx < len(robot_names) else robot_names[0]
linear_vel = self._linear_vel_field.model.get_value_as_float()
angular_vel = self._angular_vel_field.model.get_value_as_float()
# Update status
self._status_label.text = f"Command sent to {selected_robot}: linear={linear_vel}, angular={angular_vel}"
# In a real implementation, this would send the command to the robot in Isaac Sim
print(f"Sending command to {selected_robot}: linear={linear_vel}, angular={angular_vel}")
def on_shutdown(self):
print("[isaac_sim_robot_control] Isaac Sim Robot Control Extension Shutdown")
self._window = None
These examples provide a comprehensive set of code examples for Isaac Sim setup, covering:
- Basic environment initialization
- Robot model creation with physics properties
- Sensor integration (camera and LiDAR)
- ROS bridge configuration
- A complete setup script
- An Isaac Sim extension example
The code follows Isaac Sim best practices and provides a solid foundation for setting up robotics simulation environments.