Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/roboticstoolbox/backends/PyPlot/PyPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ def getframe(self):

# render the frame and save as a PIL image in the list
canvas = self.fig.canvas
return _pil("RGB", canvas.get_width_height(), canvas.tostring_rgb())
image = _pil("RGBA", canvas.get_width_height(), bytes(canvas.buffer_rgba()))
return image.convert("RGB")

def _push_inline_frame(self):
# Push a snapshot into notebook output for inline animation.
Expand Down
3 changes: 3 additions & 0 deletions src/roboticstoolbox/robot/BaseRobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,9 @@ def plot(

env = self._get_graphical_backend(backend)

if movie is not None:
from roboticstoolbox.backends.PyPlot import PyPlot

launch_kwargs = {}
for key in ("render_mode", "inline_every_n", "inline_format", "inline_dpi"):
if key in kwargs:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_PyPlot.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,36 @@ def test_launch_rejects_2d_axes(self):
env.launch(fig=fig, ax=ax)
plt.close(fig)

def test_plot_movie(self):
# robot.plot(..., movie=...) used to crash outright: BaseRobot.plot()
# referenced PyPlot in an isinstance check with no import anywhere in
# the module (NameError), and getframe() called the long-removed
# matplotlib Agg canvas method tostring_rgb() (AttributeError on
# modern matplotlib). Covers both bugs end-to-end via a real saved
# GIF, not just "no exception raised".
import tempfile
import os
from PIL import Image

panda = rp.models.Panda()
qt = rp.jtraj(panda.qr, panda.qz, 3)

with tempfile.TemporaryDirectory() as tmpdir:
path = os.path.join(tmpdir, "movie.gif")
env = panda.plot(qt.q, backend="pyplot", movie=path)
env.close()

self.assertTrue(os.path.exists(path))
with Image.open(path) as img:
n_frames = 0
try:
while True:
img.seek(n_frames)
n_frames += 1
except EOFError:
pass
self.assertEqual(n_frames, 3)

def test_options_scalar_override(self):
# Issue #418: options={"jointaxislength": ...} (a plain scalar
# default, unlike the dict-valued color/linewidth options) used to
Expand Down
Loading