.. 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-16 .. code-block:: Python import numpy as np import fury from fury.motion import Animation, CameraAnimation, Timeline, 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 17-22 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 24-25 Let's fetch and load a skybox texture. .. GENERATED FROM PYTHON SOURCE LINES 25-38 .. 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 39-44 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 44-47 .. code-block:: Python timeline = Timeline(playback_panel=True, loop=True) .. GENERATED FROM PYTHON SOURCE LINES 48-49 Creating an actor for visualization. .. GENERATED FROM PYTHON SOURCE LINES 49-56 .. 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 57-59 Creating ``FURY`` text ====================== .. GENERATED FROM PYTHON SOURCE LINES 59-64 .. 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 65-66 Creating an ``Animation`` to animate the opacity of ``fury_text``. .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: Python text_anim = Animation(actors=fury_text, loop=False) .. GENERATED FROM PYTHON SOURCE LINES 70-72 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 72-77 .. 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 78-80 Creating and animating 50 spheres ================================= .. GENERATED FROM PYTHON SOURCE LINES 80-105 .. 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 106-111 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 111-136 .. 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 137-138 Change camera position and focal interpolators. .. GENERATED FROM PYTHON SOURCE LINES 138-142 .. code-block:: Python camera_anim.set_position_interpolator(cubic_spline_interpolator) camera_anim.set_focal_interpolator(cubic_spline_interpolator) .. GENERATED FROM PYTHON SOURCE LINES 143-144 Add non-animated actors to the scene. .. GENERATED FROM PYTHON SOURCE LINES 144-147 .. code-block:: Python scene.add(plane) .. GENERATED FROM PYTHON SOURCE LINES 148-149 Add the timeline to the ``ShowManager`` for interactive playback. .. GENERATED FROM PYTHON SOURCE LINES 149-152 .. code-block:: Python showm.add_animation(timeline) .. GENERATED FROM PYTHON SOURCE LINES 153-156 Recording the animation ======================= .. GENERATED FROM PYTHON SOURCE LINES 156-166 .. code-block:: Python record_animation = False if record_animation: timeline.record("viz_fury_animation.mp4", fps=30) else: ########################################################################### # The ShowManager must go on! 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 `_