newton.viewer.ViewerGL#

class newton.viewer.ViewerGL(width=1920, height=1080, vsync=False, headless=False)[source]#

Bases: ViewerBase

OpenGL-based interactive viewer for Newton physics models.

This class provides a graphical interface for visualizing and interacting with Newton models using OpenGL rendering. It supports real-time simulation control, camera navigation, object picking, wind effects, and a rich ImGui-based UI for model introspection and visualization options.

Key Features:
  • Real-time 3D rendering of Newton models and simulation states.

  • Camera navigation with WASD and mouse controls.

  • Object picking and manipulation via mouse.

  • Visualization toggles for joints, contacts, particles, springs, etc.

  • Wind force controls and visualization.

  • Performance statistics overlay (FPS, object counts, etc.).

  • Selection panel for introspecting and filtering model attributes.

  • Extensible logging of meshes, lines, points, and arrays for custom visualization.

__init__(width=1920, height=1080, vsync=False, headless=False)#

Initialize the OpenGL viewer and UI.

Parameters:
  • width (int) – Window width in pixels.

  • height (int) – Window height in pixels.

  • vsync (bool) – Enable vertical sync.

  • headless (bool) – Run in headless mode (no window).

apply_forces(state)#

Apply viewer-driven forces (picking, wind) to the model.

Parameters:

state – The current simulation state.

begin_frame(time)#

Begin a new frame (calls parent implementation).

Parameters:

time – Current simulation time.

close()#

Close the viewer and clean up resources.

end_frame()#

Finish rendering the current frame and process window events.

This method first updates the renderer which will poll and process window events. It is possible that the user closes the window during this event processing step, which would invalidate the underlying OpenGL context. Trying to issue GL calls after the context has been destroyed results in a crash (access violation). Therefore we check whether an exit was requested and early-out before touching GL if so.

is_key_down(key)#

Check if a key is currently pressed.

Parameters:

key

Either a string representing a character/key name, or an int representing a pyglet key constant.

String examples: ‘w’, ‘a’, ‘s’, ‘d’, ‘space’, ‘escape’, ‘enter’ Int examples: pyglet.window.key.W, pyglet.window.key.SPACE

Returns:

True if the key is currently pressed, False otherwise.

Return type:

bool

is_paused()#

Check if the simulation is paused.

Returns:

True if paused, False otherwise.

Return type:

bool

is_running()#

Check if the viewer is still running.

Returns:

True if the window is open, False if closed.

Return type:

bool

log_array(name, array)#

Log a generic array for visualization (not implemented).

log_gizmo(name, transform)#
log_instances(name, mesh, xforms, scales, colors, materials, hidden=False)#

Log a batch of mesh instances for rendering.

Parameters:
  • name (str) – Unique name for the instancer.

  • mesh (str) – Name of the base mesh.

  • xforms – Array of transforms.

  • scales – Array of scales.

  • colors – Array of colors.

  • materials – Array of materials.

  • hidden – Whether the instances are hidden.

log_lines(name, starts, ends, colors, width=0.01, hidden=False)#

Log line data for rendering.

Parameters:
  • name (str) – Unique identifier for the line batch.

  • starts (wp.array) – Array of line start positions (shape: [N, 3]) or None for empty.

  • ends (wp.array) – Array of line end positions (shape: [N, 3]) or None for empty.

  • colors – Array of line colors (shape: [N, 3]) or tuple/list of RGB or None for empty.

  • width (float) – The width of the lines (float)

  • hidden (bool) – Whether the lines are initially hidden.

log_mesh(name, points, indices, normals=None, uvs=None, hidden=False, backface_culling=True)#

Log a mesh for rendering.

Parameters:
  • name (str) – Unique name for the mesh.

  • points (wp.array) – Vertex positions.

  • indices (wp.array) – Triangle indices.

  • normals (wp.array, optional) – Vertex normals.

  • uvs (wp.array, optional) – Vertex UVs.

  • hidden (bool) – Whether the mesh is hidden.

  • backface_culling (bool) – Enable backface culling.

log_points(name, points, radii, colors, hidden=False)#

Log a batch of points for rendering as spheres.

Parameters:
  • name (str) – Unique name for the point batch.

  • points – Array of point positions.

  • radii – Array of point radius values.

  • colors – Array of point colors.

  • hidden (bool) – Whether the points are hidden.

log_scalar(name, value)#

Log a scalar value for visualization (not implemented).

log_state(state)#

Cache the simulation state for UI panels and call parent log_state.

Parameters:

state – The current simulation state.

on_key_press(symbol, modifiers)#

Handle key press events for UI and simulation control.

Parameters:
  • symbol – Key symbol.

  • modifiers – Modifier keys.

on_key_release(symbol, modifiers)#

Handle key release events (not used).

on_mouse_drag(x, y, dx, dy, buttons, modifiers)#

Handle mouse drag events for camera and picking.

Parameters:
  • x – Mouse position.

  • y – Mouse position.

  • dx – Mouse movement deltas.

  • dy – Mouse movement deltas.

  • buttons – Mouse buttons pressed.

  • modifiers – Modifier keys.

on_mouse_motion(x, y, dx, dy)#

Handle mouse motion events (not used).

on_mouse_press(x, y, button, modifiers)#

Handle mouse press events (object picking).

Parameters:
  • x – Mouse position.

  • y – Mouse position.

  • button – Mouse button pressed.

  • modifiers – Modifier keys.

on_mouse_release(x, y, button, modifiers)#

Handle mouse release events to stop dragging.

Parameters:
  • x – Mouse position.

  • y – Mouse position.

  • button – Mouse button released.

  • modifiers – Modifier keys.

on_mouse_scroll(x, y, scroll_x, scroll_y)#

Handle mouse scroll for zooming (FOV adjustment).

Parameters:
  • x – Mouse position.

  • y – Mouse position.

  • scroll_x – Scroll deltas.

  • scroll_y – Scroll deltas.

on_resize(width, height)#

Handle window resize events.

Parameters:
  • width (int) – New window width.

  • height (int) – New window height.

register_ui_callback(callback, position='side')#

Register a UI callback to be rendered during the UI phase.

Parameters:
  • callback – Function to be called during UI rendering

  • position – Position where the UI should be rendered. One of: “side” - Side callback (default) “stats” - Stats/metrics area “free” - Free-floating UI elements

set_camera(pos, pitch, yaw)#
set_model(model)#

Set the Newton model to visualize.

Parameters:

model – The Newton model instance.

property vsync: bool#

Get the current vsync state.

Returns:

True if vsync is enabled, False otherwise.

Return type:

bool