Skip to content

Commit 8b128d3

Browse files
authored
add animation func getter and clear_animations() method (#896)
* add animation func getter and clear() method * requested changes * fix kwarg name * requested changes * update api docs
1 parent 522c296 commit 8b128d3

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

docs/source/api/layouts/figure.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Properties
2020
.. autosummary::
2121
:toctree: Figure_api
2222

23+
Figure.animations
2324
Figure.cameras
2425
Figure.canvas
2526
Figure.controllers
@@ -38,6 +39,7 @@ Methods
3839
Figure.add_animations
3940
Figure.add_subplot
4041
Figure.clear
42+
Figure.clear_animations
4143
Figure.close
4244
Figure.export
4345
Figure.export_numpy

docs/source/api/layouts/imgui_figure.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Properties
2020
.. autosummary::
2121
:toctree: ImguiFigure_api
2222

23+
ImguiFigure.animations
2324
ImguiFigure.cameras
2425
ImguiFigure.canvas
2526
ImguiFigure.controllers
@@ -42,6 +43,7 @@ Methods
4243
ImguiFigure.add_gui
4344
ImguiFigure.add_subplot
4445
ImguiFigure.clear
46+
ImguiFigure.clear_animations
4547
ImguiFigure.close
4648
ImguiFigure.export
4749
ImguiFigure.export_numpy

docs/source/api/layouts/subplot.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Properties
2020
.. autosummary::
2121
:toctree: Subplot_api
2222

23+
Subplot.animations
2324
Subplot.axes
2425
Subplot.background_color
2526
Subplot.camera
@@ -57,6 +58,7 @@ Methods
5758
Subplot.center_graphic
5859
Subplot.center_scene
5960
Subplot.clear
61+
Subplot.clear_animations
6062
Subplot.delete_graphic
6163
Subplot.get_figure
6264
Subplot.insert_graphic

fastplotlib/layouts/_figure.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,11 @@ def show_tooltips(self) -> bool:
543543
"""show/hide tooltips for all graphics"""
544544
return self._show_tooltips
545545

546+
@property
547+
def animations(self) -> dict[str, list[callable]]:
548+
"""Returns a dictionary of 'pre' and 'post' animation functions."""
549+
return {"pre": self._animate_funcs_pre, "post": self._animate_funcs_post}
550+
546551
@show_tooltips.setter
547552
def show_tooltips(self, val: bool):
548553
self._show_tooltips = val
@@ -765,6 +770,37 @@ def remove_animation(self, func):
765770
if func in self._animate_funcs_post:
766771
self._animate_funcs_post.remove(func)
767772

773+
def clear_animations(self, removal: str = None):
774+
"""
775+
Remove animation functions.
776+
777+
Parameters
778+
----------
779+
removal: str, default ``None``
780+
The type of animation functions to clear. One of 'pre' or 'post'. If `None`, removes all animation
781+
functions.
782+
"""
783+
if removal is None:
784+
# remove all
785+
for func in self._animate_funcs_pre:
786+
self._animate_funcs_pre.remove(func)
787+
788+
for func in self._animate_funcs_post:
789+
self._animate_funcs_post.remove(func)
790+
elif removal == "pre":
791+
# only pre
792+
for func in self._animate_funcs_pre:
793+
self._animate_funcs_pre.remove(func)
794+
elif removal == "post":
795+
# only post
796+
for func in self._animate_funcs_post:
797+
self._animate_funcs_post.remove(func)
798+
else:
799+
raise ValueError(
800+
f"Animation type: {removal} must be one of 'pre' or 'post'. To remove all animation "
801+
f"functions, pass `type=None`"
802+
)
803+
768804
def clear(self):
769805
"""Clear all Subplots"""
770806
for subplot in self:

fastplotlib/layouts/_plot_area.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ def background_color(self, colors: str | tuple[float]):
274274
"""1, 2, or 4 colors, each color must be acceptable by pygfx.Color"""
275275
self._background_material.set_colors(*colors)
276276

277+
@property
278+
def animations(self) -> dict[str, list[callable]]:
279+
"""Returns a dictionary of 'pre' and 'post' animation functions."""
280+
return {"pre": self._animate_funcs_pre, "post": self._animate_funcs_post}
281+
277282
def map_screen_to_world(
278283
self, pos: tuple[float, float] | pygfx.PointerEvent, allow_outside: bool = False
279284
) -> np.ndarray | None:
@@ -395,6 +400,37 @@ def remove_animation(self, func):
395400
if func in self._animate_funcs_post:
396401
self._animate_funcs_post.remove(func)
397402

403+
def clear_animations(self, removal: str = None):
404+
"""
405+
Remove animation functions.
406+
407+
Parameters
408+
----------
409+
removal: str, default ``None``
410+
The type of animation functions to clear. One of 'pre' or 'post'. If `None`, removes all animation
411+
functions.
412+
"""
413+
if removal is None:
414+
# remove all
415+
for func in self._animate_funcs_pre:
416+
self._animate_funcs_pre.remove(func)
417+
418+
for func in self._animate_funcs_post:
419+
self._animate_funcs_post.remove(func)
420+
elif removal == "pre":
421+
# only pre
422+
for func in self._animate_funcs_pre:
423+
self._animate_funcs_pre.remove(func)
424+
elif removal == "post":
425+
# only post
426+
for func in self._animate_funcs_post:
427+
self._animate_funcs_post.remove(func)
428+
else:
429+
raise ValueError(
430+
f"Animation type: {removal} must be one of 'pre' or 'post'. To remove all animation "
431+
f"functions, pass `type=None`"
432+
)
433+
398434
def _sort_images_by_depth(self):
399435
"""
400436
In general, we want to avoid setting the offset of a graphic, because the

0 commit comments

Comments
 (0)