Skip to content

Commit 407651e

Browse files
committed
allow generator to be passed to tranimate
1 parent e238570 commit 407651e

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

‎spatialmath/base/animate.py‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import matplotlib.pyplot as plt
1414
from matplotlib import animation
1515
from spatialmath import base
16-
from collections.abc import Iterable
16+
from collections.abc import Iterable, Generator
1717

1818

1919
class Animate:
@@ -107,9 +107,13 @@ def trplot(self, end, start=None, **kwargs):
107107
108108
"""
109109
if not isinstance(end, (np.ndarray, np.generic) ) and isinstance(end, Iterable):
110-
if len(end) == 1:
111-
end = end[0]
112-
elif len(end) >= 2:
110+
try:
111+
if len(end) == 1:
112+
end = end[0]
113+
elif len(end) >= 2:
114+
self.trajectory = end
115+
except TypeError:
116+
# a generator has no len()
113117
self.trajectory = end
114118

115119
# stash the final value
@@ -157,7 +161,7 @@ def run(self, movie=None, axes=None, repeat=False, interval=50, nframes=100, pau
157161
def update(frame, a):
158162
# if contains trajectory:
159163
if self.trajectory is not None:
160-
T = self.trajectory[frame]
164+
T = next(self.trajectory)
161165
else:
162166
T = base.trinterp(start=self.start, end=self.end, s=frame / nframes)
163167
a._draw(T)
@@ -171,8 +175,11 @@ def update(frame, a):
171175

172176
self.done = False
173177
if self.trajectory is not None:
174-
nframes = len(self.trajectory)
175-
ani = animation.FuncAnimation(fig=plt.gcf(), func=update, frames=range(0, nframes), fargs=(self,), blit=False, interval=interval, repeat=repeat)
178+
if not isinstance(self.trajectory, Iterable):
179+
self.trajectory = iter(self.trajectory)
180+
181+
# ani = animation.FuncAnimation(fig=plt.gcf(), func=update, frames=range(0, nframes), fargs=(self,), blit=False, interval=interval, repeat=repeat)
182+
ani = animation.FuncAnimation(fig=plt.gcf(), func=update, fargs=(self,), blit=False, interval=interval, repeat=repeat)
176183
if movie is None:
177184
while repeat or not self.done:
178185
plt.pause(0.1)

‎spatialmath/base/transforms3d.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,8 +1828,8 @@ def tranimate(T, **kwargs):
18281828
"""
18291829
Animate a 3D coordinate frame
18301830
1831-
:param R: SE(3) or SO(3) matrix
1832-
:type R: ndarray(4,4) or ndarray(3,3)
1831+
:param T: SE(3) or SO(3) matrix
1832+
:type T: ndarray(4,4) or ndarray(3,3)
18331833
:param nframes: number of steps in the animation [defaault 100]
18341834
:type nframes: int
18351835
:param repeat: animate in endless loop [default False]
@@ -1850,6 +1850,10 @@ def tranimate(T, **kwargs):
18501850
>>> tranimate(transl(1,2,3)@trotx(1), frame='A', arrow=False, dims=[0, 5])
18511851
>>> tranimate(transl(1,2,3)@trotx(1), frame='A', arrow=False, dims=[0, 5], movie='spin.mp4')
18521852
1853+
.. note::
1854+
- If ``T`` is a list, the poses are taken from that list
1855+
- If ``T`` is a generator, the poses are taken from its ``next()`` values
1856+
18531857
:SymPy: not supported
18541858
"""
18551859
if not _matplotlib_exists:

0 commit comments

Comments
 (0)