class NewtonViewerRenderBackend(RenderBackend):
"""
Shared base for render backends built on a Newton viewer. Only supports ``rgb``, and requires Newton physics.
"""
supports_camera_capture = True
# Whether the viewer's FOV is fixed when it is built, so viewers can only be shared between cameras with one FOV
fov_fixed_at_build = False
def __init__(self, sim=None):
super().__init__(sim=sim)
# Offscreen viewers shared between camera resources, keyed by _get_viewer()'s viewer key.
self._shared_viewers = {}
def _get_viewer(self, width, height, fov=None):
"""
The headless viewer shared by every camera resource of this size (and, under RTX, this FOV), built on
first use and rebound when the physics model is rebuilt.
Shared rather than one-per-camera because a ViewerGL owns its own GL context and uploads a
private copy of every visual mesh in the scene into it: up to ~5 GB on house_double_floor_lower).
Every caller re-points the camera at its own prim (_sync_camera) immediately before rendering.
Args:
width (int): Viewer width in pixels
height (int): Viewer height in pixels
fov (None or float): Vertical FOV in degrees. Fixed at build time under RTX, so part of the key there
Returns:
ViewerGL or ViewerRTX: Shared viewer for this key, bound to the current Newton model
"""
key = (width, height, None if fov is None else round(fov, 3)) if self.fov_fixed_at_build else (width, height)
model = self.sim.physics_backend._model
viewer = self._shared_viewers.get(key)
if viewer is not None and viewer.model is not model:
# Model was rebuilt (dynamic object add/remove): a viewer still bound to the
# old one renders stale geometry.
viewer = self._rebind_viewer(viewer, model)
if viewer is None:
# _sync_camera() poses the camera from the tracked prim before every capture
viewer = self._create_viewer(width=width, height=height)
viewer.set_model(model)
# Render the model's baked-in visual shapes (physics_backends/newton_visuals.py), not its collision shapes
viewer.show_collision = False
viewer.show_visual = True
if fov is not None:
# ViewerRTX reads this once, when it initializes on the first end_frame()
viewer.camera.fov = fov
self._shared_viewers[key] = viewer
return viewer
def _create_viewer(self, width, height):
"""
Args:
width (int): Viewer width in pixels
height (int): Viewer height in pixels
Returns:
ViewerGL or ViewerRTX: New headless viewer, not yet bound to a model
"""
raise NotImplementedError()
def _rebind_viewer(self, viewer, model):
"""
Args:
viewer (ViewerGL or ViewerRTX): Shared viewer still bound to an old Newton model
model (newton.Model): The rebuilt model
Returns:
None or ViewerGL or ViewerRTX: @viewer bound to @model, or None if it was closed and must be rebuilt
"""
raise NotImplementedError()
def render(self):
# Nothing to tick globally: each camera resource renders on-demand in get_modality_data().
pass
def create_camera_resource(self, prim_path, resolution, force_new=False):
return _NewtonCameraResource(self, prim_path, resolution)
def destroy_camera_resource(self, camera_resource):
camera_resource.destroy()
def create_annotator(self, raw_modality_name):
if raw_modality_name not in _SUPPORTED_RAW_MODALITIES:
raise NotImplementedError(
f"{type(self).__name__} only supports {sorted(_SUPPORTED_RAW_MODALITIES)} modalities, got "
f"{raw_modality_name!r}."
)
return _NewtonAnnotator(raw_modality_name)
def attach_modality(self, annotator, camera_resource):
annotator.camera_resource = camera_resource
def detach_modality(self, annotator, camera_resource):
annotator.camera_resource = None
def get_modality_data(self, annotator, device=None):
if annotator.camera_resource is None:
raise RuntimeError("Annotator is not attached to a camera resource.")
return annotator.camera_resource.capture_rgb(device)