.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/10_animation/viz_timeline.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_10_animation_viz_timeline.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_10_animation_viz_timeline.py:


==============================
Timeline and setting keyframes
==============================

In his tutorial, you will learn how to use Fury ``Timeline`` for playing the
animations.

.. GENERATED FROM PYTHON SOURCE LINES 11-18

What is ``Timeline``?
=====================

``Timeline`` is responsible for handling the playback of Fury Animations.

``Timeline`` has playback methods such as ``play``, ``pause``, ``stop``, ...
which can be used to control the animation.

.. GENERATED FROM PYTHON SOURCE LINES 18-23

.. code-block:: Python


    import numpy as np

    import fury








.. GENERATED FROM PYTHON SOURCE LINES 24-25

We create our ``Scene`` and ``ShowManager`` as usual.

.. GENERATED FROM PYTHON SOURCE LINES 25-30

.. code-block:: Python

    scene = fury.window.Scene()

    showm = fury.window.ShowManager(scene=scene, size=(900, 768))
    showm.initialize()








.. GENERATED FROM PYTHON SOURCE LINES 31-36

Creating a ``Timeline``
=======================

FURY ``Timeline`` has the option to attaches a very useful panel for
controlling the animation by setting ``playback_panel=True``.

.. GENERATED FROM PYTHON SOURCE LINES 38-39

Creating a ``Timeline`` with a PlaybackPanel.

.. GENERATED FROM PYTHON SOURCE LINES 39-41

.. code-block:: Python

    timeline = fury.animation.Timeline(playback_panel=True)








.. GENERATED FROM PYTHON SOURCE LINES 42-43

Creating a Fury Animation as usual

.. GENERATED FROM PYTHON SOURCE LINES 43-50

.. code-block:: Python

    anim = fury.animation.Animation()
    sphere = fury.actor.sphere(np.zeros([1, 3]), np.ones([1, 3]))
    anim.add_actor(sphere)
    # Now that the actor is add to the ``Animation``, setting keyframes to the
    # Animation will animate the actor accordingly.









.. GENERATED FROM PYTHON SOURCE LINES 51-59

Setting Keyframes
=================

There are multiple ways to set keyframes:

1- To set a single keyframe, you may use ``animation.set_<property>(t, k)``,
where <property> is the name of the property to be set. I.e. setting position
to (1, 2, 3) at time 0.0 would be as following:

.. GENERATED FROM PYTHON SOURCE LINES 59-61

.. code-block:: Python

    anim.set_position(0.0, np.array([1, 2, 3]))








.. GENERATED FROM PYTHON SOURCE LINES 62-66

Supported properties are: **position, rotation, scale, color, and opacity**.

2- To set multiple keyframes at once, you may use
``animation.set_<property>_keyframes(keyframes)``.

.. GENERATED FROM PYTHON SOURCE LINES 66-70

.. code-block:: Python

    keyframes = {1.0: np.array([0, 0, 0]), 3.0: np.array([-2, 0, 0])}

    anim.set_position_keyframes(keyframes)








.. GENERATED FROM PYTHON SOURCE LINES 71-72

That's it! Now we are done setting keyframes.

.. GENERATED FROM PYTHON SOURCE LINES 74-76

In order to control this animation by the timeline we created earlier, this
animation must be added to the timeline.

.. GENERATED FROM PYTHON SOURCE LINES 76-78

.. code-block:: Python

    timeline.add_animation(anim)








.. GENERATED FROM PYTHON SOURCE LINES 79-81

Now we add only the ``Timeline`` to the ``ShowManager`` the same way we add
``Animation`` to the ``ShowManager``.

.. GENERATED FROM PYTHON SOURCE LINES 81-93

.. code-block:: Python

    showm.add_animation(timeline)

    scene.set_camera(position=(0, 0, -10))

    interactive = False

    if interactive:
        showm.start()

    fury.window.record(
        scene=scene, out_path="viz_keyframe_animation_timeline.png", size=(900, 768)
    )



.. image-sg:: /auto_examples/10_animation/images/sphx_glr_viz_timeline_001.png
   :alt: viz timeline
   :srcset: /auto_examples/10_animation/images/sphx_glr_viz_timeline_001.png
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.121 seconds)


.. _sphx_glr_download_auto_examples_10_animation_viz_timeline.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: viz_timeline.ipynb <viz_timeline.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: viz_timeline.py <viz_timeline.py>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_