Visualization

moro provides visualization tools for inspecting robot configurations and animations.

The main interface is RobotVisualizer, which takes a Robot model and evaluates its symbolic kinematics at numerical configurations before passing the resulting scene to one of the available rendering backends.

Two backends are currently available:

  • Three.js (default), for interactive browser-based visualization in Jupyter and Colab;

  • Matplotlib (explicit), for standard Python figures and animations.

This section focuses on the recommended high-level workflow. Lower-level visualization objects such as SceneData, FrameData, and the backend classes are documented in the API Reference.

Creating a visualizer

Start by defining a robot:

from moro import Robot
from moro.abc import q1, q2, l1, l2

robot = Robot(
    (l1, 0, 0, q1, "r"),
    (l2, 0, 0, q2, "r"),
)

Then create a visualizer:

from moro.visualization import RobotVisualizer

viz = RobotVisualizer(robot)

A RobotVisualizer is associated with a single Robot instance.

The same visualizer can then be reused for different configurations, backends, and animations.

Plotting a robot configuration

A single robot configuration is displayed with:

viz.plot(...)

For example:

values = {
    l1: 1.0,
    l2: 0.8,
    q1: 0.5,
    q2: 0.8,
}

viz.plot(values)

A full symbolic substitution dictionary like values remains supported. In Moro 0.7.0, the preferred form separates the joint configuration from the fixed DH parameters:

parameters = {l1: 1.0, l2: 0.8}

viz.plot(
    configuration=[0.5, 0.8],
    parameters=parameters,
)

The numerical joint vector follows robot.qs order. This is especially convenient when animating many configurations of the same robot.

Because Three.js is the default backend, the previous call is equivalent to:

viz.plot(
    values,
    backend="threejs",
)

The result is an IPython.display.HTML viewer. Code expecting fig, ax = ... must request backend="matplotlib" explicitly.

Providing numerical values

Visualization requires numerical frame transformations.

For a fully numerical robot model, only the joint variables may need to be supplied:

robot = Robot(
    (1.0, 0, 0, q1, "r"),
    (0.8, 0, 0, q2, "r"),
)

values = {
    q1: 0.5,
    q2: 0.8,
}

For a symbolic robot model:

robot = Robot(
    (l1, 0, 0, q1, "r"),
    (l2, 0, 0, q2, "r"),
)

the geometric parameters must also be provided, preferably separately from the joint configuration:

parameters = {l1: 1.0, l2: 0.8}
configuration = [0.5, 0.8]

viz.plot(configuration, parameters=parameters)

Alternatively, the original complete mapping still works:

values = {l1: 1.0, l2: 0.8, q1: 0.5, q2: 0.8}
viz.plot(values)

The keyword-only parameters mapping accepts constant DH symbols needed to evaluate the robot’s kinematic geometry, not joint coordinates or dynamics-only quantities such as masses or inertia tensors. Values must be finite real scalars; SymPy numeric constants are accepted. For an expression such as l1 / 2 in the DH table, provide l1 as the key.

These inputs are invalid and raise ValueError:

  • missing required DH parameters or joint coordinates;

  • unknown keys in the explicit parameters mapping;

  • joint variables in parameters;

  • the same key in both configuration and parameters, even when the values are identical;

  • complex, nonfinite or otherwise invalid numerical values.

A model with a composite joint entry, such as q1 + theta0, cannot be assigned unambiguously from a numerical joint vector using the current Robot.qs representation. Use a symbolic substitution dictionary in that case. For the exact contracts, see Symbolic Parameters for Visualization.

Internally, moro evaluates the transformation of each robot frame with respect to the base frame and converts the resulting matrices to numerical scene data.

Therefore, all symbolic quantities required by the robot transformations must have numerical values before rendering.

Choosing a backend

The backend is selected with:

backend=...

The available values are:

"threejs"     # default
"matplotlib"  # explicit alternative

For example:

viz.plot(
    values,
    backend="matplotlib",
)

or:

viz.plot(
    values,
    backend="threejs",
)

Both backends use the same evaluated robot geometry and the same VisualizationStyle interface, but their rendering capabilities differ.

Matplotlib visualization

The Matplotlib backend creates a standard 3D Matplotlib figure.

Use:

fig, ax = viz.plot(
    values,
    backend="matplotlib",
)

The result contains:

fig → Matplotlib Figure
ax  → 3D Matplotlib Axes

This makes it possible to continue customizing the plot using normal Matplotlib operations.

For example:

ax.set_title("Planar 2R robot")

The backend does not call plt.show() automatically, so display remains under user control.

In scripts, the figure can be shown with:

import matplotlib.pyplot as plt

plt.show()

Figure size

The default figure size is:

figsize=(10, 8)

and can be changed with:

fig, ax = viz.plot(
    values,
    backend="matplotlib",
    figsize=(8, 6),
)

Camera orientation

The initial camera orientation can be configured with:

view_init=(elevation, azimuth)

For example:

fig, ax = viz.plot(
    values,
    backend="matplotlib",
    view_init=(40, 25),
)

This follows the usual Matplotlib 3D view convention.

Interactive Three.js visualization

The Three.js backend produces an interactive view intended primarily for notebook environments.

Use:

viz.plot(
    values,
    backend="threejs",
)

The result is returned as an IPython.display.HTML object and can be displayed directly in environments such as Jupyter Notebook or JupyterLab.

The browser viewer loads Three.js and OrbitControls from a CDN; the browser needs network access to those resources. For standalone Python scripts or offline rendering, explicitly choose Matplotlib.

The default rendering area is:

width=800
height=600

and can be changed with:

viz.plot(
    values,
    backend="threejs",
    width=900,
    height=650,
)

Preset views

The interactive viewer includes buttons for:

Front
Right
Top
Isometric

These controls change the camera directly inside the viewer.

The initial view is isometric.

Camera type

The viewer supports both:

Orthographic
Perspective

projection.

The orthographic camera is selected initially.

The camera type can be changed interactively without recomputing the robot model.

Interactive navigation

The Three.js viewer also provides free camera navigation through orbit controls.

The user can rotate, zoom, and inspect the robot from arbitrary directions.

Use left-drag or a touchpad primary drag to orbit, right-drag to pan, and the wheel or pinch gesture to zoom. On touchscreens, one finger orbits and two fingers zoom/pan.

The Fit View button restores the scene center and fitting scale without changing the current viewing orientation or perspective/orthographic projection. Use Isometric to restore that camera orientation.

The preset view and camera controls belong to the Three.js interface itself; they are not arguments of RobotVisualizer.plot().

Default joint representation

By default, VisualizationStyle(joint_representation="auto") selects joint representations according to the chosen backend:

  • Three.js renders solid blue revolute cylinders and orange prismatic boxes when the scene contains evaluated R/P joint metadata.

  • Matplotlib retains simple spherical joint markers.

  • Manually assembled legacy scenes without joint metadata use simple spheres, including in Three.js.

This does not modify the caller’s style object. To force spherical markers, use joint_representation="simple". To select schematic joint motion lines in either backend:

style = VisualizationStyle(
    joint_representation="kinematic",
    joint_appearance="schematic",
)
viz.plot(values, style=style)

The solid kinematic appearance currently requires Three.js. Matplotlib raises an explanatory error if explicitly asked to render solid joints.

Animation playback interval

Both backends accept interval, measured in milliseconds per animation frame, with a default of 100 ms:

viz.animate(configurations, interval=50)

For Three.js this is a positive finite number and controls the browser’s playback timer, not the physical time coordinates of a computed trajectory.

Physical-time playback in Three.js (T-06)

The default Three.js backend can reproduce joint configurations at explicit physical timestamps, including nonuniform intervals:

# For a trajectory with matching time-major q and physical times t:
viz.animate(traj.q, times=traj.t, playback_speed=1.0)

times has one strictly increasing, finite real timestamp in seconds per frame, with at least two samples. Its first value need not be zero: Three.js uses the elapsed time relative to the first timestamp. playback_speed is a strictly positive finite multiplier (1.0 = nominal physical speed, 2.0 = twice as fast, 0.5 = half speed). No intermediate joint configurations are interpolated inside the visualizer.

For example, a minimal one-joint animation that holds unevenly spaced samples can be created as follows:

from moro import Robot
from moro.abc import q1
from moro.visualization import RobotVisualizer

robot = Robot((1.0, 0, 0, q1, "r"))
viz = RobotVisualizer(robot)

viz.animate(
    [[0.0], [0.2], [0.5], [0.8], [1.0]],
    times=[0.0, 0.1, 0.7, 0.8, 1.5],
)

Playback follows a monotonic browser clock and selects the most recent supplied frame at or before the current physical time. The frame slider snaps the playback clock to the selected timestamp. Pause preserves the elapsed time, Play resumes, and playback stops at the final configuration. Pressing Play after completion restarts at the beginning. Camera operations and Fit View do not reset playback.

If the browser is slow or a tab has been throttled, frames may be skipped to catch up to the physical clock. This is visual playback, not a real-time control guarantee. The time indicator displays elapsed physical time and the playback-speed factor; the slider remains indexed by frames.

Do not combine explicit times with interval. A non-default playback_speed without times is also invalid. The original interval-based mode remains unchanged and continues looping frames uniformly by default.

Matplotlib limitation: physical-time playback via times= is not supported in this version. Passing it with backend="matplotlib" raises a clear ValueError. Matplotlib continues to support the existing interval-based FuncAnimation workflow; setting a fixed interval cannot accurately reproduce arbitrary nonuniform physical times.

Backend capability summary

Animation capability

Three.js (default)

Matplotlib

Fixed interval playback

Yes; loops

Yes; FuncAnimation

Explicit times in seconds

Yes; including nonuniform grids

Not supported (raises ValueError)

playback_speed with times

Yes; finite positive multiplier

Not supported

Pause / resume / frame seek

Yes; synchronized with physical time

Unchanged Matplotlib controls

Physical-time end behavior

Stops at the final configuration

Not applicable

Interpolation of missing samples

No

No T-06 feature

Hard real-time scheduling

Not guaranteed

Not guaranteed

For an executable two-joint example (including a nonuniform multi-waypoint trajectory, a trapezoidal trajectory and a visual verification checklist), see Physical-Time Animation Playback. The behavior is based on a browser monotonic clock; if a notebook viewer does not render correctly, check browser console output and whether Three.js dependencies loaded, rather than assuming times was converted into a fixed interval.

Customizing the visualization

Visual appearance is configured through:

from moro.visualization import VisualizationStyle

For example:

style = VisualizationStyle(
    show_frames=False,
    show_grid=True,
    link_linewidth=5,
)

Then pass the same style object to either backend:

viz.plot(
    values,
    backend="matplotlib",
    style=style,
)

or:

viz.plot(
    values,
    backend="threejs",
    style=style,
)

Visibility options

The main visibility controls are:

VisualizationStyle(
    show_frames=True,
    show_links=True,
    show_joints=True,
    show_base=True,
    show_grid=True,
)

For example, to display only the robot links and joints:

style = VisualizationStyle(
    show_frames=False,
    show_grid=False,
)

Colors

The default colors can also be changed:

style = VisualizationStyle(
    link_color="#444444",
    joint_color="#d62728",
    base_color="#000000",
)

Object sizes

The following properties control the size of the displayed elements:

frame_scale
joint_size
base_size
link_linewidth

When:

frame_scale=None
joint_size=None
base_size=None

their values are derived automatically from the dimensions of the robot scene.

This makes the default visualization scale appropriately for robots of different sizes.

Animating robot configurations

A sequence of robot configurations can be animated with:

viz.animate(...)

The input is a sequence of substitution dictionaries.

For example:

configurations = [
    {
        l1: 1.0,
        l2: 0.8,
        q1: 0.0,
        q2: 0.0,
    },
    {
        l1: 1.0,
        l2: 0.8,
        q1: 0.2,
        q2: 0.1,
    },
    {
        l1: 1.0,
        l2: 0.8,
        q1: 0.4,
        q2: 0.2,
    },
]

Then:

animation = viz.animate(
    configurations,
)

Three.js is the default backend. The result is an interactive IPython.display.HTML viewer. Select Matplotlib explicitly when you need a FuncAnimation object.

An empty configuration sequence is not valid.

Matplotlib animations

Use:

animation = viz.animate(
    configurations,
    backend="matplotlib",
)

The result is a:

matplotlib.animation.FuncAnimation

Keep a reference to the returned animation object until it has been displayed or saved.

For example:

animation = viz.animate(
    configurations,
    backend="matplotlib",
)

rather than calling viz.animate(...) without assigning the result.

Frame interval

The default interval is:

interval=100

milliseconds.

It can be changed with:

animation = viz.animate(
    configurations,
    backend="matplotlib",
    interval=50,
)

The spatial limits of the animation are computed from the complete sequence, so the camera scale remains fixed while the robot moves.

Three.js animations

Use:

viz.animate(
    configurations,
    backend="threejs",
)

The result is an interactive HTML animation.

The viewer includes controls for:

  • Play and Pause;

  • direct frame selection;

  • a frame slider;

  • Front, Right, Top, and Isometric views;

  • orthographic and perspective cameras;

  • Fit View (framing only; leaves playback and orientation unchanged);

  • free orbit navigation.

The animation uses one global spatial scale for the complete sequence, so the apparent sizes of links, joints, and coordinate frames remain consistent throughout the motion.

Showing the end-effector trajectory

Animations can optionally display the Cartesian path followed by the end-effector.

Create a style with:

style = VisualizationStyle(
    show_trajectory=True,
)

and use it in the animation:

viz.animate(
    configurations,
    backend="threejs",
    style=style,
)

The same option is supported by the Matplotlib backend.

The displayed trajectory corresponds to the positions of the final robot frame throughout the animation sequence.

Trajectory display modes

Two display modes are available:

"full"
"trace"

The default is:

trajectory_mode="full"

With "full", the complete end-effector trajectory is displayed.

With:

style = VisualizationStyle(
    show_trajectory=True,
    trajectory_mode="trace",
)

the displayed path grows as the animation advances.

Trajectory appearance can also be customized with:

trajectory_color
trajectory_linewidth

For example:

style = VisualizationStyle(
    show_trajectory=True,
    trajectory_color="#222222",
    trajectory_linewidth=3,
)

Animating an inverse-kinematics trajectory

solve_position_trajectory() returns joint configurations through its qs field. There is no need to convert every row to a full substitution dictionary: RobotVisualizer.animate() accepts the numeric array directly.

Suppose Cartesian targets targets have already been prepared for the symbolic RR robot defined above:

from moro.inverse_kinematics import solve_position_trajectory

parameters = {l1: 1.0, l2: 0.8}
trajectory = solve_position_trajectory(
    robot,
    targets,
    q0=[0.1, 0.1],
    parameters=parameters,
)

After checking convergence, pass the joint solutions to the viewer:

if not trajectory.converged:
    raise ValueError("The IK trajectory did not converge.")

style = VisualizationStyle(
    show_trajectory=True,
    trajectory_mode="trace",
)

viz.animate(
    trajectory.qs,
    parameters=parameters,
    style=style,
)

The geometry constants are shared across all frames and are not embedded in each joint vector. This IK result does not itself prescribe physical sample times; use times= only when you have a valid matching time vector, or use the legacy fixed interval player.

A worked example

Consider a planar RR manipulator with symbolic link lengths:

import numpy as np

from moro import Robot
from moro.abc import q1, q2, l1, l2
from moro.visualization import RobotVisualizer, VisualizationStyle

robot = Robot(
    (l1, 0, 0, q1, "r"),
    (l2, 0, 0, q2, "r"),
)
viz = RobotVisualizer(robot)
parameters = {l1: 1.0, l2: 0.8}
configuration = [np.pi / 4, -np.pi / 6]

A static Matplotlib plot and the equivalent interactive Three.js view share exactly the same model values:

fig, ax = viz.plot(
    configuration,
    backend="matplotlib",
    parameters=parameters,
    view_init=(35, 35),
)

viz.plot(configuration, parameters=parameters)

Now generate a sequence of joint configurations, with one row per frame:

t = np.linspace(0.0, 2.0, 40)
theta = np.linspace(0.0, np.pi / 2, t.size)
configurations = np.column_stack((theta, -theta / 2))

style = VisualizationStyle(
    show_trajectory=True,
    trajectory_mode="trace",
)

Animate it in physical time using the default Three.js backend:

viz.animate(
    configurations,
    times=t,
    parameters=parameters,
    style=style,
)

The same configurations can be rendered with Matplotlib’s fixed frame interval, without times:

animation = viz.animate(
    configurations,
    backend="matplotlib",
    parameters=parameters,
    interval=75,
    style=style,
)

The viewer accepts traditional complete substitution dictionaries as well, but repeating geometric parameters in each frame is no longer necessary. For an independent notebook-ready exercise, see Symbolic Parameters for Visualization.

Return types and migration from previous versions

Moro 0.7.0 keyword rename: the first argument of plot() is configuration (formerly num_vals); the first argument of animate() is configurations (formerly num_vals_list). Old named calls such as viz.plot(num_vals=values) and viz.animate(num_vals_list=frames) must be updated; no legacy keyword aliases are provided. Positional calls such as viz.plot(values) and viz.animate(frames) are unchanged. The optional parameters argument is keyword-only, and backend remains the second positional argument.

A complete symbolic mapping is still valid, but joint vectors plus parameters= avoid repeating fixed model geometry during animation. The public low-level evaluate_robot(robot, substitutions) API is not renamed or extended by API-001. See the Moro 0.7.0 migration and release notes for the migration summary.

The default backend changed in Moro 0.6.0. A bare viz.plot(values) returns HTML rather than (fig, ax), and bare viz.animate(configurations) returns HTML rather than FuncAnimation. Older notebooks that manipulate Matplotlib axes or animation objects should request backend="matplotlib" explicitly. Arguments ax, figsize, and view_init remain specific to Matplotlib; width and height remain specific to Three.js. These are not converted automatically.

Notes and limitations

The visualization module is intended primarily for inspecting kinematic robot models and computed joint trajectories.

Keep the following points in mind:

  • visualization operates on the Robot kinematic model;

  • all symbolic quantities required by the robot transformations must be assigned numerical values;

  • RobotVisualizer.plot() renders one configuration;

  • RobotVisualizer.animate() renders a sequence of configurations;

  • Three.js is the default backend for plot() and animate() and returns interactive HTML, especially convenient in notebook environments;

  • Matplotlib returns normal figures or FuncAnimation when explicitly selected;

  • Three.js preset views and camera types are controlled from the viewer interface rather than through RobotVisualizer.plot() arguments;

  • visualization styles can be shared between both backends;

  • automatic scene scaling is used when explicit object sizes are not provided;

  • animation scaling remains fixed across the complete sequence;

  • the optional trajectory display corresponds to the Cartesian path of the final robot frame.

The current visualization module represents robots using kinematic links, joints, and coordinate frames. It is not intended to provide detailed CAD geometry or physically realistic rendering.

The visualization system does not currently provide:

  • collision detection;

  • contact visualization;

  • physics simulation;

  • URDF or mesh-based robot geometry;

  • automatic trajectory generation;

  • interactive joint sliders;

  • dynamic-force or torque visualization.

These capabilities may require additional modeling or visualization infrastructure beyond the current scope.

See also

  • Robot Modeling — define the serial robot to be visualized.

  • Forward Kinematics — compute the frame transformations used by the visualization system.

  • Inverse Kinematics — generate joint configurations for Cartesian targets and trajectories.

  • Jacobians — inspect differential kinematic quantities associated with the robot motion.

  • API Reference → Visualization — complete reference for RobotVisualizer, VisualizationStyle, backend classes, and scene-data types.