Implementation:ARISE Initiative Robosuite OpenCVRenderer
| Knowledge Sources | |
|---|---|
| Domains | Robotics, Rendering, Visualization |
| Last Updated | 2026-02-15 07:00 GMT |
Overview
The OpenCVViewer class provides a lightweight viewer that displays MuJoCo offscreen-rendered frames in an OpenCV window, supporting multiple cameras and keyboard callbacks.
Description
OpenCVViewer is a simple rendering viewer that uses OpenCV's imshow to display frames rendered by MuJoCo's offscreen renderer. Unlike the native MuJoCo viewer, it does not provide interactive scene manipulation but offers a lightweight way to visualize simulations, particularly useful on remote machines or in environments where the full MuJoCo viewer is not available.
The viewer supports displaying multiple cameras simultaneously by concatenating their rendered images horizontally. Camera views can be specified by either camera ID or camera name via the set_camera method. The window resolution defaults to 1280x800 but can be customized when setting cameras.
The render method retrieves frames from the offscreen renderer for each configured camera, concatenates them horizontally, flips the image vertically (to correct for coordinate system differences), and displays the result using OpenCV. On non-macOS systems, the window is positioned at the top-left corner of the screen upon first creation. An optional keypress callback can be registered via add_keypress_callback to handle keyboard input events from the OpenCV window.
The close method cleans up by removing the sim reference and destroying the OpenCV window. The close_window helper method handles just the window destruction.
Usage
Use this viewer when you need a simple, lightweight way to display simulation frames without the full MuJoCo viewer. It is particularly useful for headless setups that have a display forwarded via X11, or when you want to display offscreen renders from multiple cameras side by side.
Code Reference
Source Location
- Repository: ARISE_Initiative_Robosuite
- File: robosuite/renderers/viewer/opencv_renderer.py
Signature
class OpenCVViewer:
def __init__(self, sim)
def set_camera(self, camera_id=None, camera_name=None,
width=None, height=None)
def render(self)
def add_keypress_callback(self, keypress_callback)
def close_window(self)
def close(self)
Import
from robosuite.renderers.viewer.opencv_renderer import OpenCVViewer
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| sim | MjSim | Yes | Active MuJoCo simulation instance with offscreen renderer |
| camera_id | int or list | No (set_camera) | Camera ID(s) to render (mutually exclusive with camera_name) |
| camera_name | str or list | No (set_camera) | Camera name(s) to render (mutually exclusive with camera_id) |
| width | int | No (set_camera) | Custom render width in pixels |
| height | int | No (set_camera) | Custom render height in pixels |
| keypress_callback | callable | No | Function called with key code on each frame |
Outputs
| Name | Type | Description |
|---|---|---|
| render() | None | Displays the current frame in an OpenCV window |
| Displayed window | OpenCV window | Visual display of horizontally concatenated camera views |
Usage Examples
from robosuite.renderers.viewer.opencv_renderer import OpenCVViewer
# Create an OpenCV viewer
viewer = OpenCVViewer(sim=env.sim)
# Set to display a specific camera
viewer.set_camera(camera_name="agentview")
# Set multiple cameras for side-by-side display
viewer.set_camera(camera_name=["agentview", "robot0_eye_in_hand"])
# Render in a loop
for _ in range(1000):
env.step(action)
viewer.render()
# Add a keypress handler
def on_key(key):
if key == ord('q'):
print("Quit requested")
viewer.add_keypress_callback(on_key)
# Clean up
viewer.close()