.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/10_animation/viz_hierarchical_animation.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_hierarchical_animation.py>`
        to download the full example code

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

.. _sphx_glr_auto_examples_10_animation_viz_hierarchical_animation.py:


===============================
Keyframe hierarchical Animation
===============================

Creating hierarchical keyframes animation in fury

.. GENERATED FROM PYTHON SOURCE LINES 8-20

.. code-block:: Python


    import numpy as np

    import fury

    scene = fury.window.Scene()

    showm = fury.window.ShowManager(
        scene=scene, size=(900, 768), reset_camera=False, order_transparent=True
    )
    showm.initialize()








.. GENERATED FROM PYTHON SOURCE LINES 21-22

Creating the road

.. GENERATED FROM PYTHON SOURCE LINES 22-26

.. code-block:: Python

    road = fury.actor.box(
        np.array([[0, 0, 0]]), colors=np.array([[1, 1, 1]]), scales=np.array([[22, 0.1, 5]])
    )








.. GENERATED FROM PYTHON SOURCE LINES 27-28

Constructing the car geometry

.. GENERATED FROM PYTHON SOURCE LINES 28-35

.. code-block:: Python


    body_actor = fury.actor.box(
        np.array([[0, 0.5, 0], [-0.2, 1, 0]]),
        scales=((4, 1, 2), (2.5, 1.5, 1.8)),
        colors=(0.6, 0.3, 0.1),
    )








.. GENERATED FROM PYTHON SOURCE LINES 36-37

Adding the the car's body to an Animation to be able to animate it later.

.. GENERATED FROM PYTHON SOURCE LINES 37-39

.. code-block:: Python

    car_anim = fury.animation.Animation(actors=body_actor)








.. GENERATED FROM PYTHON SOURCE LINES 40-41

Creating the wheels of the car

.. GENERATED FROM PYTHON SOURCE LINES 41-64

.. code-block:: Python

    wheel_center = np.array([[0, 0, 0]])

    wheel_direction = np.array([[0, 0, 1]])
    wheel_positions = [
        [1.2, 0, 1.1],
        [-1.2, 0, 1.1],
        [1.2, 0, -1.1],
        [-1.2, 0, -1.1],
    ]

    wheels = [
        fury.actor.cylinder(
            wheel_center,
            wheel_direction,
            (0.1, 0.7, 0.3),
            radius=1.7,
            heights=0.3,
            resolution=10,
            capped=True,
        )
        for _ in range(4)
    ]








.. GENERATED FROM PYTHON SOURCE LINES 65-67

Animating each wheel and setting its position to the right position using a
single keyframe that will not change.

.. GENERATED FROM PYTHON SOURCE LINES 67-75

.. code-block:: Python


    wheels_animations = [fury.animation.Animation(actors=wheel) for wheel in wheels]

    for wheel_anim in wheels_animations:
        wheel_anim.set_position(0.0, wheel_positions.pop())
        wheel_anim.set_rotation(0.0, [0, 0, 1, 1])
        wheel_anim.set_rotation(1.0, [0, 0, 1, -1])








.. GENERATED FROM PYTHON SOURCE LINES 76-77

Creating a radar on top of the car

.. GENERATED FROM PYTHON SOURCE LINES 79-80

First we create the shaft holding and rotating the radar

.. GENERATED FROM PYTHON SOURCE LINES 80-84

.. code-block:: Python

    radar_shaft = fury.actor.cylinder(
        np.array([[0, 0, 0]]), np.array([[0, 1, 0]]), (0, 1, 0), heights=1
    )








.. GENERATED FROM PYTHON SOURCE LINES 85-86

In order to animate the shaft actor we have to add it to an Animation

.. GENERATED FROM PYTHON SOURCE LINES 86-88

.. code-block:: Python

    radar_shaft_anim = fury.animation.Animation(actors=radar_shaft)








.. GENERATED FROM PYTHON SOURCE LINES 89-91

Setting a single position keyframe will make sure the actor will be placed at
that position

.. GENERATED FROM PYTHON SOURCE LINES 91-93

.. code-block:: Python

    radar_shaft_anim.set_position(0.0, [0, 2, 0])








.. GENERATED FROM PYTHON SOURCE LINES 94-95

Rotating the shaft around Y axis

.. GENERATED FROM PYTHON SOURCE LINES 95-99

.. code-block:: Python

    radar_shaft_anim.set_rotation(0.0, [0, -250, 0])
    radar_shaft_anim.set_rotation(1.0, [0, 250, 0])
    radar_shaft_anim.set_rotation(2.0, [0, -250, 0])








.. GENERATED FROM PYTHON SOURCE LINES 100-101

Now we create the radar itself

.. GENERATED FROM PYTHON SOURCE LINES 101-105

.. code-block:: Python

    radar = fury.actor.cone(
        np.array([[0, 0, 0]]), directions=(0, 0, 0), colors=(0.2, 0.2, 0.9)
    )








.. GENERATED FROM PYTHON SOURCE LINES 106-107

Then add it to an animation in order to rotate it

.. GENERATED FROM PYTHON SOURCE LINES 107-109

.. code-block:: Python

    radar_animation = fury.animation.Animation(actors=radar)








.. GENERATED FROM PYTHON SOURCE LINES 110-111

Set position and rotation as done above with the shaft.

.. GENERATED FROM PYTHON SOURCE LINES 111-116

.. code-block:: Python

    radar_animation.set_position(0, [-0.4, 0.5, 0])
    radar_animation.set_rotation(0.0, [0, 0, 0])
    radar_animation.set_rotation(1.0, [180, 0, 0])
    radar_animation.set_rotation(2.0, [0, 0, 0])








.. GENERATED FROM PYTHON SOURCE LINES 117-120

Now, we want the radar to rotate when the shaft rotates in hierarchical way.
To do that we must add the radar animation as a child animation of the shaft
animation as below:

.. GENERATED FROM PYTHON SOURCE LINES 120-122

.. code-block:: Python

    radar_shaft_anim.add_child_animation(radar_animation)








.. GENERATED FROM PYTHON SOURCE LINES 123-126

After that we want everything to animate related to the car.
The wheels should always be attached to the car no matter where it moves.
we do that by adding them as child animations of the car's body animation

.. GENERATED FROM PYTHON SOURCE LINES 126-128

.. code-block:: Python

    car_anim.add_child_animation([wheels_animations, radar_shaft_anim])








.. GENERATED FROM PYTHON SOURCE LINES 129-130

Moving the car

.. GENERATED FROM PYTHON SOURCE LINES 130-133

.. code-block:: Python

    car_anim.set_position(0.0, [-10, 0.5, 0])
    car_anim.set_position(6.0, [10, 0.5, 0])








.. GENERATED FROM PYTHON SOURCE LINES 134-135

Adding the car Animation to the show manager

.. GENERATED FROM PYTHON SOURCE LINES 135-147

.. code-block:: Python

    showm.add_animation(car_anim)
    scene.add(road)
    scene.camera().SetPosition(0, 20, 30)

    interactive = False

    if interactive:
        showm.start()

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



.. image-sg:: /auto_examples/10_animation/images/sphx_glr_viz_hierarchical_animation_001.png
   :alt: viz hierarchical animation
   :srcset: /auto_examples/10_animation/images/sphx_glr_viz_hierarchical_animation_001.png
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_auto_examples_10_animation_viz_hierarchical_animation.py:

.. only:: html

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

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

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

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

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


.. only:: html

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

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