.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/10_animation/viz_camera.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_camera.py: ====================================== Keyframe animation: Camera and opacity ====================================== Camera and opacity keyframe animation explained in this tutorial. .. GENERATED FROM PYTHON SOURCE LINES 8-15 .. code-block:: Python import numpy as np from fury import actor, window from fury.animation import Animation, CameraAnimation, Timeline from fury.animation.interpolator import cubic_spline_interpolator .. GENERATED FROM PYTHON SOURCE LINES 16-21 The Plan ======== The plan here is to animate (scale and translate) 50 spheres randomly, and show `FURY` text that appears at the end! .. GENERATED FROM PYTHON SOURCE LINES 21-29 .. code-block:: Python scene = window.Scene() showm = window.ShowManager( scene, size=(900, 768), reset_camera=False, order_transparent=True ) .. GENERATED FROM PYTHON SOURCE LINES 30-42 Creating the main ``Timeline`` and adding static actors to it ============================================================= Here we create a ``Timeline``. so that we can use it as a controller for the 50 animations we will create. So, Instead of updating and adding 50 Animations to the ``ShowManager``, we only need to update the main ``Timeline``. Also, a playback panel can be assigned to this main Timeline. But, why we need 50 ``Animations``, you may ask. -> A single ``Animation`` can handle each property once at a time. So we need 50 ``Animations`` to translate and scale our 50 spheres. .. GENERATED FROM PYTHON SOURCE LINES 44-46 ``playback_panel=True`` assigns a playback panel that can control the playback of its ``Animations`` .. GENERATED FROM PYTHON SOURCE LINES 46-49 .. code-block:: Python timeline = Timeline(playback_panel=True) .. GENERATED FROM PYTHON SOURCE LINES 50-51 Creating two actors for visualization, and to detect camera's animations. .. GENERATED FROM PYTHON SOURCE LINES 51-60 .. code-block:: Python arrow = actor.arrow( np.array([[0, 0, 0]]), np.array([[0, 1, 0]]), np.array([[1, 1, 0]]), scales=5 ) plan = 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 61-63 Creating "FURY" text ==================== .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: Python fury_text = actor.vector_text('FURY', pos=(-4.3, 15, 0), scale=(2, 2, 2)) .. GENERATED FROM PYTHON SOURCE LINES 66-67 Creating an ``Animation`` to animate the opacity of ``fury_text`` .. GENERATED FROM PYTHON SOURCE LINES 67-69 .. code-block:: Python text_anim = Animation(fury_text, loop=False) .. GENERATED FROM PYTHON SOURCE LINES 70-72 opacity is set to 0 at time 29 and set to one at time 35. Linear interpolator is always used by default. .. GENERATED FROM PYTHON SOURCE LINES 72-75 .. code-block:: Python text_anim.set_opacity(29, 0) text_anim.set_opacity(35, 1) .. GENERATED FROM PYTHON SOURCE LINES 76-77 ``text_anim`` contains the text actor is added to the Timeline. .. GENERATED FROM PYTHON SOURCE LINES 77-79 .. code-block:: Python timeline.add_animation(text_anim) .. GENERATED FROM PYTHON SOURCE LINES 80-83 Creating and animating 50 Spheres ================================= .. GENERATED FROM PYTHON SOURCE LINES 83-116 .. code-block:: Python for i in range(50): ########################################################################### # create a sphere actor that's centered at the origin and has random color # and radius. actors = [ actor.sphere( np.array([[0, 0, 0]]), np.random.random([1, 3]), np.random.random([1, 3]) ) ] ########################################################################### # create a timeline to animate this actor (single actor or list of actors) # Actors can be added later using `Timeline.add_actor(actor)` animation = Animation(actors) # We generate random position and scale values from time=0 to time=49 each # two seconds. for t in range(0, 50, 2): ####################################################################### # Position and scale are set to a random value at the timestamps # mentioned above. 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 interpolator. animation.set_position_interpolator(cubic_spline_interpolator) ########################################################################### # Finally, the ``Animation`` is added to the ``Timeline``. timeline.add_animation(animation) .. GENERATED FROM PYTHON SOURCE LINES 117-124 Animating the camera ==================== Since, only one camera is needed, camera animations are preferably done using a separate ``Animation``. Three properties can control the camera's animation: Position, focal position (referred to by `focal`), and up-view. .. GENERATED FROM PYTHON SOURCE LINES 124-128 .. code-block:: Python camera_anim = CameraAnimation(loop=False) timeline.add_animation(camera_anim) .. GENERATED FROM PYTHON SOURCE LINES 129-131 Multiple keyframes can be set at once as follows. camera focal positions .. GENERATED FROM PYTHON SOURCE LINES 131-152 .. code-block:: Python camera_positions = { # time: camera position 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 camera_focal_positions = { # time: focal position 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]), } .. GENERATED FROM PYTHON SOURCE LINES 153-155 ``set_camera_focal`` can only set one keyframe, but ``set_camera_focal_keyframes`` can set a dictionary of keyframes. .. GENERATED FROM PYTHON SOURCE LINES 155-158 .. code-block:: Python camera_anim.set_focal_keyframes(camera_focal_positions) camera_anim.set_position_keyframes(camera_positions) .. GENERATED FROM PYTHON SOURCE LINES 159-160 Change camera position and focal interpolators .. GENERATED FROM PYTHON SOURCE LINES 160-163 .. code-block:: Python camera_anim.set_position_interpolator(cubic_spline_interpolator) camera_anim.set_focal_interpolator(cubic_spline_interpolator) .. GENERATED FROM PYTHON SOURCE LINES 164-165 Adding non-animatable actors to the scene. .. GENERATED FROM PYTHON SOURCE LINES 165-167 .. code-block:: Python scene.add(arrow, plan) .. GENERATED FROM PYTHON SOURCE LINES 168-169 Adding the timeline to the ShowManager. .. GENERATED FROM PYTHON SOURCE LINES 169-171 .. code-block:: Python showm.add_animation(timeline) .. GENERATED FROM PYTHON SOURCE LINES 172-173 The ShowManager must go on! .. GENERATED FROM PYTHON SOURCE LINES 173-180 .. code-block:: Python interactive = False if interactive: showm.start() window.record(scene, out_path='viz_keyframe_animation_camera.png', size=(900, 768)) .. image-sg:: /auto_examples/10_animation/images/sphx_glr_viz_camera_001.png :alt: viz camera :srcset: /auto_examples/10_animation/images/sphx_glr_viz_camera_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.585 seconds) .. _sphx_glr_download_auto_examples_10_animation_viz_camera.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: viz_camera.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_camera.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_