1313import matplotlib .pyplot as plt
1414from matplotlib import animation
1515from spatialmath import base
16- from collections .abc import Iterable
16+ from collections .abc import Iterable , Generator
1717
1818
1919class 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 )
0 commit comments