warp.render¶
The warp.render
module provides a set of renderers that can be used for visualizing scenes involving shapes of various types.
Built on top of these stand-alone renderers, the warp.sim.render
module provides renderers that can be used to
visualize scenes directly from warp.sim.ModelBuilder
objects and update them from warp.sim.State
objects.
Standalone Renderers¶
The OpenGLRenderer
provides an interactive renderer to play back animations in real time, the UsdRenderer
provides a renderer that exports the scene to a USD file that can be rendered in a renderer of your choice.
- class warp.render.UsdRenderer(stage, up_axis='Y', fps=60, scaling=1.0)[source]¶
A USD renderer
- __init__(stage, up_axis='Y', fps=60, scaling=1.0)[source]¶
Construct a UsdRenderer object
- Parameters:
model – A simulation model
stage (str/Usd.Stage) – A USD stage (either in memory or on disk)
up_axis (str) – The upfacing axis of the stage
fps – The number of frames per second to use in the USD file
scaling – Scaling factor to use for the entities in the scene
- add_shape_instance(name, shape, body, pos, rot, scale=(1.0, 1.0, 1.0), color=(1.0, 1.0, 1.0), custom_index=-1, visible=True)[source]¶
- render_plane(name, pos, rot, width, length, color=None, parent_body=None, is_template=False)[source]¶
Render a plane with the given dimensions.
- Parameters:
name (str) – Name of the plane
pos (tuple) – Position of the plane
rot (tuple) – Rotation of the plane
width (float) – Width of the plane
length (float) – Length of the plane
color (tuple | None) – Color of the plane
parent_body (str | None) – Name of the parent body
is_template (bool) – Whether the plane is a template
- render_sphere(name, pos, rot, radius, parent_body=None, is_template=False, color=None)[source]¶
Debug helper to add a sphere for visualization
- render_capsule(name, pos, rot, radius, half_height, parent_body=None, is_template=False, color=None)[source]¶
Debug helper to add a capsule for visualization
- Parameters:
- render_cylinder(name, pos, rot, radius, half_height, parent_body=None, is_template=False, color=None)[source]¶
Debug helper to add a cylinder for visualization
- Parameters:
- render_cone(name, pos, rot, radius, half_height, parent_body=None, is_template=False, color=None)[source]¶
Debug helper to add a cone for visualization
- render_box(name, pos, rot, extents, parent_body=None, is_template=False, color=None)[source]¶
Debug helper to add a box for visualization
- render_mesh(name, points, indices, colors=None, pos=(0.0, 0.0, 0.0), rot=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), update_topology=False, parent_body=None, is_template=False)[source]¶
- render_line_list(name, vertices, indices, color, radius)[source]¶
Debug helper to add a line list as a set of capsules
- Parameters:
vertices – The vertices of the line-strip
color – The color of the line
time – The time to update at
- class warp.render.OpenGLRenderer(title='Warp', scaling=1.0, fps=60, up_axis='Y', screen_width=1024, screen_height=768, near_plane=1.0, far_plane=100.0, camera_fov=45.0, camera_pos=(0.0, 2.0, 10.0), camera_front=(0.0, 0.0, -1.0), camera_up=(0.0, 1.0, 0.0), background_color=(0.53, 0.8, 0.92), draw_grid=True, draw_sky=True, draw_axis=True, show_info=True, render_wireframe=False, render_depth=False, axis_scale=1.0, vsync=False, headless=None, enable_backface_culling=True, enable_mouse_interaction=True, enable_keyboard_interaction=True)[source]¶
OpenGLRenderer is a simple OpenGL renderer for rendering 3D shapes and meshes.
- default_num_segments = 32¶
- __init__(title='Warp', scaling=1.0, fps=60, up_axis='Y', screen_width=1024, screen_height=768, near_plane=1.0, far_plane=100.0, camera_fov=45.0, camera_pos=(0.0, 2.0, 10.0), camera_front=(0.0, 0.0, -1.0), camera_up=(0.0, 1.0, 0.0), background_color=(0.53, 0.8, 0.92), draw_grid=True, draw_sky=True, draw_axis=True, show_info=True, render_wireframe=False, render_depth=False, axis_scale=1.0, vsync=False, headless=None, enable_backface_culling=True, enable_mouse_interaction=True, enable_keyboard_interaction=True)[source]¶
- Parameters:
title (str) – The window title.
scaling (float) – The scaling factor for the scene.
fps (int) – The target frames per second.
up_axis (str) – The up axis of the scene. Can be “X”, “Y”, or “Z”.
screen_width (int) – The width of the window.
screen_height (int) – The height of the window.
near_plane (float) – The near clipping plane.
far_plane (float) – The far clipping plane.
camera_fov (float) – The camera field of view in degrees.
camera_pos (tuple) – The initial camera position.
camera_front (tuple) – The initial camera front direction.
camera_up (tuple) – The initial camera up direction.
background_color (tuple) – The background color of the scene.
draw_grid (bool) – Whether to draw a grid indicating the ground plane.
draw_sky (bool) – Whether to draw a sky sphere.
draw_axis (bool) – Whether to draw the coordinate system axes.
show_info (bool) – Whether to overlay rendering information.
render_wireframe (bool) – Whether to render scene shapes as wireframes.
render_depth (bool) – Whether to show the depth buffer instead of the RGB image.
axis_scale (float) – The scale of the coordinate system axes being rendered (only if
draw_axis
is True).vsync (bool) – Whether to enable vertical synchronization.
headless (bool) – Whether to run in headless mode (no window is created). If None, the value is determined by the Pyglet configuration defined in
pyglet.options["headless"]
.enable_backface_culling (bool) – Whether to enable backface culling.
enable_mouse_interaction (bool) – Whether to enable mouse interaction.
enable_keyboard_interaction (bool) – Whether to enable keyboard interaction.
Note
OpenGLRenderer
requires Pyglet (version >= 2.0, known to work on 2.0.7) to be installed.Headless rendering is supported via EGL on UNIX operating systems. To enable headless rendering, set the following pyglet options before importing
warp.render
:import pyglet pyglet.options["headless"] = True import warp.render # OpenGLRenderer is instantiated with headless=True by default renderer = warp.render.OpenGLRenderer()
- setup_tiled_rendering(instances, rescale_window=False, tile_width=None, tile_height=None, tile_ncols=None, tile_nrows=None, tile_positions=None, tile_sizes=None, projection_matrices=None, view_matrices=None)[source]¶
Set up tiled rendering where the render buffer is split into multiple tiles that can visualize different shape instances of the scene with different view and projection matrices. See
get_pixels()
which allows to retrieve the pixels of for each tile. Seeupdate_tile()
which allows to update the shape instances, projection matrix, view matrix, tile size, or tile position for a given tile.- Parameters:
instances (List[List[int]]) – A list of lists of shape instance ids. Each list of shape instance ids will be rendered into a separate tile.
rescale_window (bool) – If True, the window will be resized to fit the tiles.
tile_width (int | None) – The width of each tile in pixels (optional).
tile_height (int | None) – The height of each tile in pixels (optional).
tile_ncols (int | None) – The number of tiles rendered horizontally (optional). Will be considered if tile_width is set to compute the tile positions, unless tile_positions is defined.
tile_positions (List[Tuple[int]] | None) – A list of (x, y) tuples specifying the position of each tile in pixels. If None, the tiles will be arranged in a square grid, or, if tile_ncols and tile_nrows is set, in a grid with the specified number of columns and rows.
tile_sizes (List[Tuple[int]] | None) – A list of (width, height) tuples specifying the size of each tile in pixels. If None, the tiles will have the same size as specified by tile_width and tile_height.
projection_matrices (List[List[float] | List[List[float]] | ndarray] | None) – A list of projection matrices for each tile (each view matrix is either a flattened 16-dimensional array or a 4x4 matrix). If the entire array is None, or only a view instances, the projection matrices for all, or these instances, respectively, will be derived from the current render settings.
view_matrices (List[List[float] | List[List[float]] | ndarray] | None) – A list of view matrices for each tile (each view matrix is either a flattened 16-dimensional array or a 4x4 matrix). If the entire array is None, or only a view instances, the view matrices for all, or these instances, respectively, will be derived from the current camera settings and be updated when the camera is moved.
tile_nrows (int | None)
- update_tile(tile_id, instances=None, projection_matrix=None, view_matrix=None, tile_size=None, tile_position=None)[source]¶
Update the shape instances, projection matrix, view matrix, tile size, or tile position for a given tile given its index.
- Parameters:
tile_id – The index of the tile to update.
instances (List[int] | None) – A list of shape instance ids (optional).
projection_matrix (List[float] | List[List[float]] | ndarray | None) – A projection matrix (optional).
view_matrix (List[float] | List[List[float]] | ndarray | None) – A view matrix (optional).
tile_size (Tuple[int] | None) – A (width, height) tuple specifying the size of the tile in pixels (optional).
tile_position (Tuple[int] | None) – A (x, y) tuple specifying the position of the tile in pixels (optional).
- static compute_projection_matrix(fov, aspect_ratio, near_plane, far_plane)[source]¶
Compute a projection matrix given the field of view, aspect ratio, near plane, and far plane.
- add_shape_instance(name, shape, body, pos, rot, scale=(1.0, 1.0, 1.0), color1=None, color2=None, custom_index=-1, visible=True)[source]¶
- update_shape_instance(name, pos=None, rot=None, color1=None, color2=None, visible=None)[source]¶
Update the instance transform of the shape
- Parameters:
name – The name of the shape
pos – The position of the shape
rot – The rotation of the shape
color1 – The first color of the checker pattern
color2 – The second color of the checker pattern
visible – Whether the shape is visible
- get_pixels(target_image, split_up_tiles=True, mode='rgb', use_uint8=False)[source]¶
Read the pixels from the frame buffer (RGB or depth are supported) into the given array.
If split_up_tiles is False, array must be of shape (screen_height, screen_width, 3) for RGB mode or (screen_height, screen_width, 1) for depth mode. If split_up_tiles is True, the pixels will be split up into tiles (see
tile_width
andtile_height
for dimensions): array must be of shape (num_tiles, tile_height, tile_width, 3) for RGB mode or (num_tiles, tile_height, tile_width, 1) for depth mode.- Parameters:
target_image (array) – The array to read the pixels into. Must have float32 as dtype and be on a CUDA device.
split_up_tiles (bool) – Whether to split up the viewport into tiles, see
setup_tiled_rendering()
.mode (str) – can be either “rgb” or “depth”
use_uint8 (bool) – Whether to use uint8 as dtype in RGB mode for the target_image array and return values in the range [0, 255]. Otherwise, float32 is assumed as dtype with values in the range [0, 1].
- Returns:
Whether the pixels were successfully read.
- Return type:
- render_plane(name, pos, rot, width, length, color=(1.0, 1.0, 1.0), color2=None, parent_body=None, is_template=False, u_scaling=1.0, v_scaling=1.0)[source]¶
Add a plane for visualization
- Parameters:
name (str) – The name of the plane
pos (tuple) – The position of the plane
rot (tuple) – The rotation of the plane
width (float) – The width of the plane
length (float) – The length of the plane
color (tuple) – The color of the plane
texture – The texture of the plane (optional)
parent_body (str | None)
is_template (bool)
- render_ground(size=1000.0, plane=None)[source]¶
Add a ground plane for visualization
- Parameters:
size (float) – The size of the ground plane
- render_sphere(name, pos, rot, radius, parent_body=None, is_template=False, color=None)[source]¶
Add a sphere for visualization
- render_capsule(name, pos, rot, radius, half_height, parent_body=None, is_template=False, up_axis=1, color=None)[source]¶
Add a capsule for visualization
- Parameters:
pos (tuple) – The position of the capsule
radius (float) – The radius of the capsule
half_height (float) – The half height of the capsule
name (str) – A name for the USD prim on the stage
up_axis (int) – The axis of the capsule that points up (0: x, 1: y, 2: z)
color (tuple | None) – The color of the capsule
rot (tuple)
parent_body (str | None)
is_template (bool)
- render_cylinder(name, pos, rot, radius, half_height, parent_body=None, is_template=False, up_axis=1, color=None)[source]¶
Add a cylinder for visualization
- Parameters:
pos (tuple) – The position of the cylinder
radius (float) – The radius of the cylinder
half_height (float) – The half height of the cylinder
name (str) – A name for the USD prim on the stage
up_axis (int) – The axis of the cylinder that points up (0: x, 1: y, 2: z)
color (tuple | None) – The color of the capsule
rot (tuple)
parent_body (str | None)
is_template (bool)
- render_cone(name, pos, rot, radius, half_height, parent_body=None, is_template=False, up_axis=1, color=None)[source]¶
Add a cone for visualization
- Parameters:
pos (tuple) – The position of the cone
radius (float) – The radius of the cone
half_height (float) – The half height of the cone
name (str) – A name for the USD prim on the stage
up_axis (int) – The axis of the cone that points up (0: x, 1: y, 2: z)
color (tuple | None) – The color of the cone
rot (tuple)
parent_body (str | None)
is_template (bool)
- render_box(name, pos, rot, extents, parent_body=None, is_template=False, color=None)[source]¶
Add a box for visualization
- render_mesh(name, points, indices, colors=None, pos=(0.0, 0.0, 0.0), rot=(0.0, 0.0, 0.0, 1.0), scale=(1.0, 1.0, 1.0), update_topology=False, parent_body=None, is_template=False, smooth_shading=True)[source]¶
Add a mesh for visualization
- Parameters:
points – The points of the mesh
indices – The indices of the mesh
colors – The colors of the mesh
pos – The position of the mesh
rot – The rotation of the mesh
scale – The scale of the mesh
name (str) – A name for the USD prim on the stage
smooth_shading (bool) – Whether to average face normals at each vertex or introduce additional vertices for each face
parent_body (str | None)
is_template (bool)
- render_arrow(name, pos, rot, base_radius, base_height, cap_radius=None, cap_height=None, parent_body=None, is_template=False, up_axis=1, color=None)[source]¶
Add a arrow for visualization
- Parameters:
pos (tuple) – The position of the arrow
base_radius (float) – The radius of the cylindrical base of the arrow
base_height (float) – The height of the cylindrical base of the arrow
cap_radius (float | None) – The radius of the conical cap of the arrow
cap_height (float | None) – The height of the conical cap of the arrow
name (str) – A name for the USD prim on the stage
up_axis (int) – The axis of the arrow that points up (0: x, 1: y, 2: z)
rot (tuple)
parent_body (str | None)
is_template (bool)
- render_ref(name, path, pos, rot, scale, color=None)[source]¶
Create a reference (instance) with the given name to the given path.
- render_points(name, points, radius, colors=None)[source]¶
Add a set of points
- Parameters:
points – The points to render
radius – The radius of the points (scalar or list)
colors – The colors of the points
name (str) – A name for the USD prim on the stage
- render_line_list(name, vertices, indices, color=None, radius=0.01)[source]¶
Add a line list as a set of capsules
Simulation Renderers¶
Based on these renderers from warp.render
, the SimRendererUsd
(which equals SimRenderer
) and
SimRendererOpenGL
classes from warp.sim.render
are derived to populate the renderers directly from
warp.sim.ModelBuilder
scenes and update them from warp.sim.State
objects.
CUDA Graphics Interface¶
Warp provides a CUDA graphics interface that allows you to access OpenGL buffers from CUDA kernels. This is useful for manipulating OpenGL array buffers without having to copy them back and forth between the CPU and GPU.
See the CUDA documentation on OpenGL Interoperability for more information.
- class warp.context.RegisteredGLBuffer(*args, **kwargs)[source]¶
Helper class to register a GL buffer with CUDA so that it can be mapped to a Warp array.
Example usage:
import warp as wp import numpy as np from pyglet.gl import * wp.init() # create a GL buffer gl_buffer_id = GLuint() glGenBuffers(1, gl_buffer_id) # copy some data to the GL buffer glBindBuffer(GL_ARRAY_BUFFER, gl_buffer_id) gl_data = np.arange(1024, dtype=np.float32) glBufferData(GL_ARRAY_BUFFER, gl_data.nbytes, gl_data.ctypes.data, GL_DYNAMIC_DRAW) glBindBuffer(GL_ARRAY_BUFFER, 0) # register the GL buffer with CUDA cuda_gl_buffer = wp.RegisteredGLBuffer(gl_buffer_id) # map the GL buffer to a Warp array arr = cuda_gl_buffer.map(dtype=wp.float32, shape=(1024,)) # launch a Warp kernel to manipulate or read the array wp.launch(my_kernel, dim=1024, inputs=[arr]) # unmap the GL buffer cuda_gl_buffer.unmap()
- NONE = 0¶
Flag that specifies no hints about how this resource will be used. It is therefore assumed that this resource will be read from and written to by CUDA. This is the default value.
- READ_ONLY = 1¶
Flag that specifies that CUDA will not write to this resource.
- WRITE_DISCARD = 2¶
Flag that specifies that CUDA will not read from this resource and will write over the entire contents of the resource, so none of the data previously stored in the resource will be preserved.
- __init__(gl_buffer_id, device=None, flags=NONE, fallback_to_copy=True)[source]¶
- Parameters:
gl_buffer_id (int) – The OpenGL buffer id (GLuint).
device (Device | str | None) – The device to register the buffer with. If None, the current device will be used.
flags (int) – A combination of the flags constants
NONE
,READ_ONLY
, andWRITE_DISCARD
.fallback_to_copy (bool) – If True and CUDA/OpenGL interop is not available, fall back to copy operations between the Warp array and the OpenGL buffer. Otherwise, a
RuntimeError
will be raised.
Note
The
fallback_to_copy
option (to use copy operations if CUDA graphics interop functionality is not available) requires pyglet version 2.0 or later. Install viapip install pyglet==2.*
.