.. 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 ` 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-21 .. code-block:: Python import numpy as np from fury import actor, window from fury.animation import Animation scene = window.Scene() showm = window.ShowManager( scene, size=(900, 768), reset_camera=False, order_transparent=True ) showm.initialize() .. GENERATED FROM PYTHON SOURCE LINES 22-23 Creating the road .. GENERATED FROM PYTHON SOURCE LINES 23-27 .. code-block:: Python road = 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 28-29 Constructing the car geometry .. GENERATED FROM PYTHON SOURCE LINES 29-36 .. code-block:: Python body_actor = 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 37-38 Adding the the car's body to an Animation to be able to animate it later. .. GENERATED FROM PYTHON SOURCE LINES 38-40 .. code-block:: Python car_anim = Animation(body_actor) .. GENERATED FROM PYTHON SOURCE LINES 41-42 Creating the wheels of the car .. GENERATED FROM PYTHON SOURCE LINES 42-65 .. 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 = [ 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 66-68 Animating each wheel and setting its position to the right position using a single keyframe that will not change. .. GENERATED FROM PYTHON SOURCE LINES 68-76 .. code-block:: Python wheels_animations = [Animation(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 77-78 Creating a radar on top of the car .. GENERATED FROM PYTHON SOURCE LINES 80-81 First we create the shaft holding and rotating the radar .. GENERATED FROM PYTHON SOURCE LINES 81-85 .. code-block:: Python radar_shaft = actor.cylinder( np.array([[0, 0, 0]]), np.array([[0, 1, 0]]), (0, 1, 0), heights=1 ) .. GENERATED FROM PYTHON SOURCE LINES 86-87 In order to animate the shaft actor we have to add it to an Animation .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: Python radar_shaft_anim = Animation(radar_shaft) .. GENERATED FROM PYTHON SOURCE LINES 90-92 Setting a single position keyframe will make sure the actor will be placed at that position .. GENERATED FROM PYTHON SOURCE LINES 92-94 .. code-block:: Python radar_shaft_anim.set_position(0.0, [0, 2, 0]) .. GENERATED FROM PYTHON SOURCE LINES 95-96 Rotating the shaft around Y axis .. GENERATED FROM PYTHON SOURCE LINES 96-100 .. 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 101-102 Now we create the radar itself .. GENERATED FROM PYTHON SOURCE LINES 102-104 .. code-block:: Python radar = actor.cone(np.array([[0, 0, 0]]), directions=(0, 0, 0), colors=(0.2, 0.2, 0.9)) .. GENERATED FROM PYTHON SOURCE LINES 105-106 Then add it to an animation in order to rotate it .. GENERATED FROM PYTHON SOURCE LINES 106-108 .. code-block:: Python radar_animation = Animation(radar) .. GENERATED FROM PYTHON SOURCE LINES 109-110 Set position and rotation as done above with the shaft. .. GENERATED FROM PYTHON SOURCE LINES 110-115 .. 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 116-119 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 119-121 .. code-block:: Python radar_shaft_anim.add_child_animation(radar_animation) .. GENERATED FROM PYTHON SOURCE LINES 122-125 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 125-127 .. code-block:: Python car_anim.add_child_animation([wheels_animations, radar_shaft_anim]) .. GENERATED FROM PYTHON SOURCE LINES 128-129 Moving the car .. GENERATED FROM PYTHON SOURCE LINES 129-132 .. 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 133-134 Adding the car Animation to the show manager .. GENERATED FROM PYTHON SOURCE LINES 134-146 .. code-block:: Python showm.add_animation(car_anim) scene.add(road) scene.camera().SetPosition(0, 20, 30) interactive = False if interactive: showm.start() window.record( scene, out_path="viz_keyframe_hierarchical_animation.png", size=(900, 768) ) .. _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 ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_hierarchical_animation.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_