.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/10_animation/viz_fury_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_fury_animation.py: ====================================== Keyframe animation: Camera and opacity ====================================== Camera and opacity keyframe animation explained in this tutorial. .. GENERATED FROM PYTHON SOURCE LINES 8-17 .. code-block:: Python import numpy as np import fury from fury.animation import Animation, CameraAnimation, Timeline from fury.animation.interpolator import cubic_spline_interpolator from fury.data import fetch_viz_cubemaps, read_viz_cubemap from fury.io import load_cube_map_texture .. GENERATED FROM PYTHON SOURCE LINES 18-23 The Plan ======== The plan here is to animate, scale, and translate 50 spheres randomly, then show ``FURY`` text at the end. .. GENERATED FROM PYTHON SOURCE LINES 25-26 Let's fetch and load a skybox texture. .. GENERATED FROM PYTHON SOURCE LINES 26-39 .. code-block:: Python fetch_viz_cubemaps() texture_files = read_viz_cubemap("skybox") cube_map = load_cube_map_texture(texture_files) scene = fury.window.Scene(skybox=cube_map) showm = fury.window.ShowManager( scene=scene, size=(900, 768), pixel_ratio=2, ) .. GENERATED FROM PYTHON SOURCE LINES 40-45 Creating the main ``Timeline`` ============================== Here we create a ``Timeline`` so that it can control all animations from one place. A playback panel is attached to provide interactive playback controls. .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: Python timeline = Timeline(playback_panel=True, loop=True) .. GENERATED FROM PYTHON SOURCE LINES 49-50 Creating an actor for visualization. .. GENERATED FROM PYTHON SOURCE LINES 50-57 .. code-block:: Python plane = fury.actor.box( np.array([[0, 0, 0]]), colors=np.array([[1, 1, 1]]), scales=np.array([[20, 0.2, 20]]), ) .. GENERATED FROM PYTHON SOURCE LINES 58-60 Creating ``FURY`` text ====================== .. GENERATED FROM PYTHON SOURCE LINES 60-65 .. code-block:: Python fury_text = fury.actor.text( text="FURY", position=(0, 15, 0), font_size=5, anchor="middle-center" ) .. GENERATED FROM PYTHON SOURCE LINES 66-67 Creating an ``Animation`` to animate the opacity of ``fury_text``. .. GENERATED FROM PYTHON SOURCE LINES 67-70 .. code-block:: Python text_anim = Animation(actors=fury_text, loop=False) .. GENERATED FROM PYTHON SOURCE LINES 71-73 Opacity is set to 0 at time 29 and to 1 at time 35. Linear interpolation is used by default. .. GENERATED FROM PYTHON SOURCE LINES 73-78 .. code-block:: Python text_anim.set_opacity(29, 0.0) text_anim.set_opacity(35, 1.0) timeline.add_animation(text_anim) .. GENERATED FROM PYTHON SOURCE LINES 79-81 Creating and animating 50 spheres ================================= .. GENERATED FROM PYTHON SOURCE LINES 81-106 .. code-block:: Python for _ in range(50): ########################################################################### # Create a sphere actor centered at the origin with random color and radius. sphere_actor = fury.actor.sphere( np.array([[0, 0, 0]]), colors=np.random.random([1, 3]), radii=np.random.random(1) * 0.5 + 0.1, ) ########################################################################### # Create one animation for this sphere. animation = Animation(actors=sphere_actor) ########################################################################### # Generate random position and scale values every two seconds. for t in range(0, 50, 2): animation.set_position(t, np.random.random(3) * 30 - np.array([15, 0, 15])) animation.set_scale(t, np.repeat(np.random.random(1), 3)) ########################################################################### # Change the position interpolator to cubic spline interpolation. animation.set_position_interpolator(cubic_spline_interpolator) timeline.add_animation(animation) .. GENERATED FROM PYTHON SOURCE LINES 107-112 Animating the camera ==================== Camera animations are represented with ``CameraAnimation``. Three properties can control the camera: position, focal position, and view-up direction. .. GENERATED FROM PYTHON SOURCE LINES 112-137 .. code-block:: Python camera_anim = CameraAnimation(loop=False) timeline.add_animation(camera_anim) camera_positions = { 0: np.array([3, 3, 3]), 4: np.array([50, 25, -40]), 7: np.array([-50, 50, -40]), 10: np.array([-25, 25, 20]), 14: np.array([0, 16, 25]), 20: np.array([0, 14.5, 20]), } camera_focal_positions = { 15: np.array([0, 0, 0]), 20: np.array([3, 9, 5]), 23: np.array([7, 5, 3]), 25: np.array([-2, 9, -6]), 27: np.array([0, 16, 0]), 31: np.array([0, 14.5, 0]), } camera_anim.set_position_keyframes(camera_positions) camera_anim.set_focal_keyframes(camera_focal_positions) .. GENERATED FROM PYTHON SOURCE LINES 138-139 Change camera position and focal interpolators. .. GENERATED FROM PYTHON SOURCE LINES 139-143 .. code-block:: Python camera_anim.set_position_interpolator(cubic_spline_interpolator) camera_anim.set_focal_interpolator(cubic_spline_interpolator) .. GENERATED FROM PYTHON SOURCE LINES 144-145 Add non-animated actors to the scene. .. GENERATED FROM PYTHON SOURCE LINES 145-148 .. code-block:: Python scene.add(plane) .. GENERATED FROM PYTHON SOURCE LINES 149-150 Add the timeline to the ``ShowManager`` for interactive playback. .. GENERATED FROM PYTHON SOURCE LINES 150-153 .. code-block:: Python showm.add_animation(timeline) .. GENERATED FROM PYTHON SOURCE LINES 154-155 The ShowManager must go on! .. GENERATED FROM PYTHON SOURCE LINES 155-157 .. code-block:: Python showm.start() .. _sphx_glr_download_auto_examples_10_animation_viz_fury_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_fury_animation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_fury_animation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: viz_fury_animation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_