.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/10_animation/viz_interpolators.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_10_animation_viz_interpolators.py: ===================== Keyframe animation ===================== Minimal tutorial of making keyframe-based animation in FURY. .. GENERATED FROM PYTHON SOURCE LINES 10-15 What is an ``Animation`` ======================== ``Animation`` is responsible for animating FURY actors using a set of keyframes by interpolating values between timestamps of these keyframes. .. GENERATED FROM PYTHON SOURCE LINES 15-29 .. code-block:: Python import numpy as np from fury import actor, window from fury.animation import Animation from fury.animation.interpolator import cubic_spline_interpolator keyframes = { 1.0: {'value': np.array([0, 0, 0])}, 2.0: {'value': np.array([-4, 1, 0])}, 5.0: {'value': np.array([0, 0, 12])}, 6.0: {'value': np.array([25, 0, 12])}, } .. GENERATED FROM PYTHON SOURCE LINES 30-34 Why keyframes data are also a dictionary ``{'value': np.array([0, 0, 0])})``? -> Since some keyframes data can only be defined by a set of data i.e. a single position keyframe could consist of a position, in control point, and out control point or any other data that helps to define this keyframe. .. GENERATED FROM PYTHON SOURCE LINES 37-45 What are the interpolators ========================== The keyframes interpolators are functions that takes a set of keyframes and returns a function that calculates an interpolated value between these keyframes. Below there is an example on how to use interpolators manually to interpolate the above defined ``keyframes``. .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: Python interpolation_function = cubic_spline_interpolator(keyframes) .. GENERATED FROM PYTHON SOURCE LINES 49-51 Now, if we feed any time to this function it would return the cubic interpolated position at that time. .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. code-block:: Python position = interpolation_function(1.44434) .. GENERATED FROM PYTHON SOURCE LINES 55-56 ``position`` would contain an interpolated position at time equals 1.44434 .. GENERATED FROM PYTHON SOURCE LINES 58-63 Creating the environment ======================== In order to make any animations in FURY, a `ShowManager` is needed to handle updating the animation and rendering the scene. .. GENERATED FROM PYTHON SOURCE LINES 63-73 .. code-block:: Python scene = window.Scene() showm = window.ShowManager( scene, size=(900, 768), reset_camera=False, order_transparent=True ) showm.initialize() arrow = actor.arrow(np.array([[0, 0, 0]]), (0, 0, 0), (1, 0, 1), scales=6) .. GENERATED FROM PYTHON SOURCE LINES 74-78 Creating an ``Animation`` ========================= First step is creating the Animation. .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: Python animation = Animation() .. GENERATED FROM PYTHON SOURCE LINES 81-83 Adding the sphere actor to the timeline This could've been done during initialization. .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: Python animation.add_actor(arrow) .. GENERATED FROM PYTHON SOURCE LINES 86-90 Setting position keyframes ========================== Adding some position keyframes .. GENERATED FROM PYTHON SOURCE LINES 90-95 .. code-block:: Python animation.set_position(0.0, np.array([0, 0, 0])) animation.set_position(2.0, np.array([10, 10, 10])) animation.set_position(5.0, np.array([-10, -3, -6])) animation.set_position(9.0, np.array([10, 6, 20])) .. GENERATED FROM PYTHON SOURCE LINES 96-107 Changing the default interpolator for a single property ======================================================= For all properties except **rotation**, linear interpolator is used by default. In order to change the default interpolator and set another interpolator, call ``animation.set__interpolator(interpolator)`` FURY already has some interpolators located at: ``fury.animation.interpolator``. Below we set the interpolator for position keyframes to be **cubic spline interpolator**. .. GENERATED FROM PYTHON SOURCE LINES 107-109 .. code-block:: Python animation.set_position_interpolator(cubic_spline_interpolator) .. GENERATED FROM PYTHON SOURCE LINES 110-111 Adding some rotation keyframes. .. GENERATED FROM PYTHON SOURCE LINES 111-114 .. code-block:: Python animation.set_rotation(0.0, np.array([160, 50, 0])) animation.set_rotation(8.0, np.array([60, 160, 0])) .. GENERATED FROM PYTHON SOURCE LINES 115-120 For Rotation keyframes, Slerp is used as the default interpolator. What is Slerp? Slerp (spherical linear interpolation) of quaternions results in a constant speed rotation in keyframe animation. Reed more about Slerp: https://en.wikipedia.org/wiki/Slerp .. GENERATED FROM PYTHON SOURCE LINES 122-123 Setting camera position to see the animation better. .. GENERATED FROM PYTHON SOURCE LINES 123-125 .. code-block:: Python scene.set_camera(position=(0, 0, 90)) .. GENERATED FROM PYTHON SOURCE LINES 126-127 Adding main animation to the ``ShowManager``. .. GENERATED FROM PYTHON SOURCE LINES 127-129 .. code-block:: Python showm.add_animation(animation) .. GENERATED FROM PYTHON SOURCE LINES 130-131 Start the ``ShowManager`` to start playing the animation .. GENERATED FROM PYTHON SOURCE LINES 131-137 .. code-block:: Python interactive = False if interactive: showm.start() window.record(scene, out_path='viz_keyframe_interpolator.png', size=(900, 768)) .. image-sg:: /auto_examples/10_animation/images/sphx_glr_viz_interpolators_001.png :alt: viz interpolators :srcset: /auto_examples/10_animation/images/sphx_glr_viz_interpolators_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.101 seconds) .. _sphx_glr_download_auto_examples_10_animation_viz_interpolators.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: viz_interpolators.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_interpolators.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_