Skip to content

Commit

Permalink
RF: Add keyword arguments to animation (#899)
Browse files Browse the repository at this point in the history
  • Loading branch information
WassCodeur authored Jul 14, 2024
1 parent dd26181 commit a1f635a
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 34 deletions.
54 changes: 35 additions & 19 deletions fury/animation/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
spline_interpolator,
step_interpolator,
)
from fury.decorators import warn_on_args_to_kwargs
from fury.lib import Actor, Camera, Transform


Expand Down Expand Up @@ -42,7 +43,8 @@ class Animation:
"""

def __init__(self, actors=None, length=None, loop=True, motion_path_res=None):
@warn_on_args_to_kwargs()
def __init__(self, *, actors=None, length=None, loop=True, motion_path_res=None):
super().__init__()
self._data = defaultdict(dict)
self._animations = []
Expand Down Expand Up @@ -191,7 +193,8 @@ def _get_attribute_data(self, attrib):
}
return data.get(attrib)

def get_keyframes(self, attrib=None):
@warn_on_args_to_kwargs()
def get_keyframes(self, *, attrib=None):
"""Get a keyframe for a specific or all attributes.
Parameters
Expand All @@ -209,8 +212,9 @@ def get_keyframes(self, attrib=None):
}
return data.get(attrib, {}).get("keyframes", {})

@warn_on_args_to_kwargs()
def set_keyframe(
self, attrib, timestamp, value, update_interpolator=True, **kwargs
self, attrib, timestamp, value, *, update_interpolator=True, **kwargs
):
"""Set a keyframe for a certain attribute.
Expand Down Expand Up @@ -263,7 +267,7 @@ def set_keyframe(
self._timeline.update_duration()
else:
self.update_duration()
self.update_animation(0)
self.update_animation(time=0)
self.update_motion_path()

def set_keyframes(self, attrib, keyframes):
Expand Down Expand Up @@ -359,7 +363,8 @@ def _handle_scene_event(self, timestamp):
self._scene.rm(*self._actors)
self._added_to_scene = False

def set_interpolator(self, attrib, interpolator, is_evaluator=False, **kwargs):
@warn_on_args_to_kwargs()
def set_interpolator(self, attrib, interpolator, *, is_evaluator=False, **kwargs):
"""Set keyframes interpolator for a certain property
Parameters
Expand Down Expand Up @@ -437,7 +442,8 @@ def is_interpolatable(self, attrib):
data = self._data
return bool(data.get(attrib, {}).get("interpolator", {}).get("func"))

def set_position_interpolator(self, interpolator, is_evaluator=False, **kwargs):
@warn_on_args_to_kwargs()
def set_position_interpolator(self, interpolator, *, is_evaluator=False, **kwargs):
"""Set the position interpolator.
Parameters
Expand All @@ -464,7 +470,8 @@ def set_position_interpolator(self, interpolator, is_evaluator=False, **kwargs):
"position", interpolator, is_evaluator=is_evaluator, **kwargs
)

def set_scale_interpolator(self, interpolator, is_evaluator=False):
@warn_on_args_to_kwargs()
def set_scale_interpolator(self, interpolator, *, is_evaluator=False):
"""Set the scale interpolator.
Parameters
Expand All @@ -483,7 +490,8 @@ def set_scale_interpolator(self, interpolator, is_evaluator=False):
"""
self.set_interpolator("scale", interpolator, is_evaluator=is_evaluator)

def set_rotation_interpolator(self, interpolator, is_evaluator=False):
@warn_on_args_to_kwargs()
def set_rotation_interpolator(self, interpolator, *, is_evaluator=False):
"""Set the rotation interpolator .
Parameters
Expand All @@ -502,7 +510,8 @@ def set_rotation_interpolator(self, interpolator, is_evaluator=False):
"""
self.set_interpolator("rotation", interpolator, is_evaluator=is_evaluator)

def set_color_interpolator(self, interpolator, is_evaluator=False):
@warn_on_args_to_kwargs()
def set_color_interpolator(self, interpolator, *, is_evaluator=False):
"""Set the color interpolator.
Parameters
Expand All @@ -521,7 +530,8 @@ def set_color_interpolator(self, interpolator, is_evaluator=False):
"""
self.set_interpolator("color", interpolator, is_evaluator=is_evaluator)

def set_opacity_interpolator(self, interpolator, is_evaluator=False):
@warn_on_args_to_kwargs()
def set_opacity_interpolator(self, interpolator, *, is_evaluator=False):
"""Set the opacity interpolator.
Parameters
Expand Down Expand Up @@ -899,7 +909,8 @@ def add_child_animation(self, animation):
self._animations.append(animation)
self.update_duration()

def add_actor(self, actor, static=False):
@warn_on_args_to_kwargs()
def add_actor(self, actor, *, static=False):
"""Add an actor or list of actors to the Animation.
Parameters
Expand Down Expand Up @@ -1094,7 +1105,8 @@ def add_update_callback(self, callback, prop=None):
attrib = self._get_attribute_data(prop)
attrib.get("callbacks", []).append(callback)

def update_animation(self, time=None):
@warn_on_args_to_kwargs()
def update_animation(self, *, time=None):
"""Update the animation.
Update the animation at a certain time. This will make sure all
Expand Down Expand Up @@ -1168,7 +1180,7 @@ def update_animation(self, time=None):
[callback(time) for callback in self._general_callbacks]

# Also update all child Animations.
[animation.update_animation(time) for animation in self._animations]
[animation.update_animation(time=time) for animation in self._animations]

if self._scene and not has_handler:
self._scene.reset_clipping_range()
Expand All @@ -1184,7 +1196,7 @@ def add_to_scene(self, scene):
self._scene = scene
self._added_to_scene = True
self._start_time = perf_counter()
self.update_animation(0)
self.update_animation(time=0)

def remove_from_scene(self, scene):
"""Remove Animation, its actors and sub Animations from the scene"""
Expand Down Expand Up @@ -1217,7 +1229,8 @@ class CameraAnimation(Animation):
"""

def __init__(self, camera=None, length=None, loop=True, motion_path_res=None):
@warn_on_args_to_kwargs()
def __init__(self, *, camera=None, length=None, loop=True, motion_path_res=None):
super(CameraAnimation, self).__init__(
length=length, loop=loop, motion_path_res=motion_path_res
)
Expand Down Expand Up @@ -1353,7 +1366,8 @@ def get_view_up(self, t):
"""
return self.get_value("view_up", t)

def set_focal_interpolator(self, interpolator, is_evaluator=False):
@warn_on_args_to_kwargs()
def set_focal_interpolator(self, interpolator, *, is_evaluator=False):
"""Set the camera focal position interpolator.
Parameters
Expand All @@ -1368,7 +1382,8 @@ def set_focal_interpolator(self, interpolator, is_evaluator=False):
"""
self.set_interpolator("focal", interpolator, is_evaluator=is_evaluator)

def set_view_up_interpolator(self, interpolator, is_evaluator=False):
@warn_on_args_to_kwargs()
def set_view_up_interpolator(self, interpolator, *, is_evaluator=False):
"""Set the camera up-view vector animation interpolator.
Parameters
Expand All @@ -1383,7 +1398,8 @@ def set_view_up_interpolator(self, interpolator, is_evaluator=False):
"""
self.set_interpolator("view_up", interpolator, is_evaluator=is_evaluator)

def update_animation(self, time=None):
@warn_on_args_to_kwargs()
def update_animation(self, *, time=None):
"""Update the camera animation.
Parameters
Expand All @@ -1396,7 +1412,7 @@ def update_animation(self, time=None):
if self._camera is None:
if self._scene:
self._camera = self._scene.camera()
self.update_animation(time)
self.update_animation(tile=time)
return
else:
if self.is_interpolatable("rotation"):
Expand Down
8 changes: 6 additions & 2 deletions fury/animation/helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import numpy as np

from fury.decorators import warn_on_args_to_kwargs

def get_previous_timestamp(timestamps, current_time, include_last=False):

@warn_on_args_to_kwargs()
def get_previous_timestamp(timestamps, current_time, *, include_last=False):
"""Return the maximum previous timestamp of a given time.
Parameters
Expand All @@ -26,7 +29,8 @@ def get_previous_timestamp(timestamps, current_time, include_last=False):
return timestamps[0]


def get_next_timestamp(timestamps, current_time, include_first=False):
@warn_on_args_to_kwargs()
def get_next_timestamp(timestamps, current_time, *, include_first=False):
"""Return the minimum next timestamp of a given time.
Parameters
Expand Down
14 changes: 7 additions & 7 deletions fury/animation/tests/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def test_animation():
anim.add_static_actor(cube_actor)
assert cube_actor in anim.static_actors

anim = Animation(cube_actor)
anim = Animation(actors=cube_actor)
assert cube_actor in anim.actors

anim_main = Animation()
anim_main.add_child_animation(anim)
assert anim in anim_main.child_animations

anim = Animation(cube_actor)
anim = Animation(actors=cube_actor)
anim.set_position(0, np.array([1, 1, 1]))
# overriding a keyframe
anim.set_position(0, np.array([0, 0, 0]))
Expand Down Expand Up @@ -79,7 +79,7 @@ def test_animation():

cube = actor.cube(np.array([[0, 0, 0]]))
anim.add_actor(cube)
anim.update_animation(0)
anim.update_animation(time=0)
if not shaders:
transform = cube.GetUserTransform()
npt.assert_almost_equal(anim.get_position(0), transform.GetPosition())
Expand All @@ -89,7 +89,7 @@ def test_animation():

def test_camera_animation():
cam = Camera()
anim = CameraAnimation(cam)
anim = CameraAnimation(camera=cam)

assert anim.camera is cam

Expand All @@ -101,13 +101,13 @@ def test_camera_animation():

anim.set_rotation(0, np.array([180, 0, 0]))

anim.update_animation(0)
anim.update_animation(time=0)
npt.assert_almost_equal(cam.GetPosition(), np.array([1, 2, 3]))
npt.assert_almost_equal(cam.GetFocalPoint(), np.array([10, 20, 30]))
anim.update_animation(3)
anim.update_animation(time=3)
npt.assert_almost_equal(cam.GetPosition(), np.array([3, 2, 1]))
npt.assert_almost_equal(cam.GetFocalPoint(), np.array([30, 20, 10]))
anim.update_animation(1.5)
anim.update_animation(time=1.5)
npt.assert_almost_equal(cam.GetPosition(), np.array([2, 2, 2]))
npt.assert_almost_equal(cam.GetFocalPoint(), np.array([20, 20, 20]))
rot = np.zeros(16)
Expand Down
2 changes: 1 addition & 1 deletion fury/animation/tests/test_timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_timeline():
anim.set_position(12, [1, 2, 1])
assert tl_2.duration == length

tl_2 = Timeline(anim, length=11)
tl_2 = Timeline(animations=anim, length=11)
assert tl_2.duration == 11

tl = Timeline(playback_panel=True)
Expand Down
13 changes: 10 additions & 3 deletions fury/animation/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from fury import window
from fury.animation.animation import Animation
from fury.decorators import warn_on_args_to_kwargs
from fury.lib import RenderWindow, WindowToImageFilter, numpy_support
from fury.ui.elements import PlaybackPanel

Expand Down Expand Up @@ -33,7 +34,10 @@ class Timeline:
"""

def __init__(self, animations=None, playback_panel=False, loop=True, length=None):
@warn_on_args_to_kwargs()
def __init__(
self, *, animations=None, playback_panel=False, loop=True, length=None
):
self._scene = None
self.playback_panel = None
self._current_timestamp = 0
Expand Down Expand Up @@ -284,8 +288,10 @@ def has_playback_panel(self):
"""
return self.playback_panel is not None

@warn_on_args_to_kwargs()
def record(
self,
*,
fname=None,
fps=30,
speed=1.0,
Expand Down Expand Up @@ -450,7 +456,8 @@ def animations(self) -> "list[Animation]":
"""
return self._animations

def update(self, force=False):
@warn_on_args_to_kwargs()
def update(self, *, force=False):
"""Update the timeline.
Update the Timeline and all the animations that it controls. As well as
Expand All @@ -477,7 +484,7 @@ def update(self, force=False):
else:
self.pause()
if self.playing or force:
[anim.update_animation(time) for anim in self._animations]
[anim.update_animation(time=time) for anim in self._animations]

def add_to_scene(self, scene):
"""Add Timeline and all of its Animations to the scene"""
Expand Down
2 changes: 1 addition & 1 deletion fury/tests/test_gltf.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def test_skinning():
file = read_viz_gltf("SimpleSkin")
gltf_obj = glTF(file)
animation = gltf_obj.skin_animation()["anim_0"]
timeline = Timeline(animation)
timeline = Timeline(animations=animation)
# checking weights and joints
weights = np.array(
[
Expand Down
2 changes: 1 addition & 1 deletion fury/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def test_add_animation_to_show_manager():
cube = actor.cube(np.array([[2, 2, 3]]))

timeline = Timeline(playback_panel=True)
animation = Animation(cube)
animation = Animation(actors=cube)
timeline.add_animation(animation)
showm.add_animation(timeline)

Expand Down

0 comments on commit a1f635a

Please sign in to comment.