.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/10_animation/viz_bezier_interpolator.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_bezier_interpolator.py: =================== Bezier Interpolator =================== Keyframe animation using cubic Bezier interpolator. .. GENERATED FROM PYTHON SOURCE LINES 8-14 .. code-block:: Python import numpy as np from fury import actor, window from fury.animation import Animation, Timeline from fury.animation.interpolator import cubic_bezier_interpolator .. GENERATED FROM PYTHON SOURCE LINES 15-21 Position interpolation using cubic Bezier curve =============================================== Cubic bezier curve is a widely used method for interpolating motion paths. This can be achieved using positions and control points between those positions. .. GENERATED FROM PYTHON SOURCE LINES 21-28 .. code-block:: Python scene = window.Scene() showm = window.ShowManager( scene, size=(900, 768), reset_camera=False, order_transparent=True ) .. GENERATED FROM PYTHON SOURCE LINES 29-46 Cubic Bezier curve parameters ============================= In order to make a cubic bezier curve based animation, you need four values for every keyframe: 1- Timestamp: The time that the keyframe is assigned to. 2- value: The value of the keyframe. This might be position, quaternion, or scale value. 3- In control point: The control point used when the value is the destination value. 4- Out control point: The control point used when the value is the departure value:: keyframe 0 -----------------> keyframe 1 (time-0) (value-0) (out-cp-0) -----------------> (time-1) (value-1) (in-cp-1) keyframe 1 -----------------> keyframe 2 (time-1) (value-1) (out-cp-1) -----------------> (time-2) (value-2) (in-cp-2) .. GENERATED FROM PYTHON SOURCE LINES 46-50 .. code-block:: Python keyframe_1 = {'value': [-2, 0, 0], 'out_cp': [-15, 6, 0]} keyframe_2 = {'value': [18, 0, 0], 'in_cp': [27, 18, 0]} .. GENERATED FROM PYTHON SOURCE LINES 51-52 Visualizing points .. GENERATED FROM PYTHON SOURCE LINES 52-56 .. code-block:: Python pts_actor = actor.sphere( np.array([keyframe_1.get('value'), keyframe_2.get('value')]), (1, 0, 0), radii=0.3 ) .. GENERATED FROM PYTHON SOURCE LINES 57-58 Visualizing the control points .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: Python cps_actor = actor.sphere( np.array([keyframe_2.get('in_cp'), keyframe_1.get('out_cp')]), (0, 0, 1), radii=0.6 ) .. GENERATED FROM PYTHON SOURCE LINES 63-64 Visualizing the connection between the control points and the points .. GENERATED FROM PYTHON SOURCE LINES 64-69 .. code-block:: Python cline_actor = actor.line( np.array([list(keyframe_1.values()), list(keyframe_2.values())]), colors=np.array([0, 1, 0]), ) .. GENERATED FROM PYTHON SOURCE LINES 70-71 Initializing an ``Animation`` and adding sphere actor to it. .. GENERATED FROM PYTHON SOURCE LINES 71-75 .. code-block:: Python animation = Animation() sphere = actor.sphere(np.array([[0, 0, 0]]), (1, 0, 1)) animation.add_actor(sphere) .. GENERATED FROM PYTHON SOURCE LINES 76-87 Setting Cubic Bezier keyframes ============================== Cubic Bezier keyframes consists of 4 data per keyframe Timestamp, position, in control point, and out control point. - In control point is the cubic bezier control point for the associated position when this position is the destination position. - Out control point is the cubic bezier control point for the associated position when this position is the origin position or departing position. Note: If a control point is not provided or set `None`, this control point will be the same as the position itself. .. GENERATED FROM PYTHON SOURCE LINES 87-95 .. code-block:: Python animation.set_position( 0.0, np.array(keyframe_1.get('value')), out_cp=np.array(keyframe_1.get('out_cp')) ) animation.set_position( 5.0, np.array(keyframe_2.get('value')), in_cp=np.array(keyframe_2.get('in_cp')) ) .. GENERATED FROM PYTHON SOURCE LINES 96-97 Changing position interpolation into cubic bezier interpolation .. GENERATED FROM PYTHON SOURCE LINES 97-99 .. code-block:: Python animation.set_position_interpolator(cubic_bezier_interpolator) .. GENERATED FROM PYTHON SOURCE LINES 100-101 Adding the visualization actors to the scene. .. GENERATED FROM PYTHON SOURCE LINES 101-103 .. code-block:: Python scene.add(pts_actor, cps_actor, cline_actor) .. GENERATED FROM PYTHON SOURCE LINES 104-105 Adding the animation to the ``ShowManager`` .. GENERATED FROM PYTHON SOURCE LINES 105-114 .. code-block:: Python showm.add_animation(animation) interactive = False if interactive: showm.start() window.record(scene, out_path='viz_keyframe_animation_bezier_1.png', size=(900, 768)) .. image-sg:: /auto_examples/10_animation/images/sphx_glr_viz_bezier_interpolator_001.png :alt: viz bezier interpolator :srcset: /auto_examples/10_animation/images/sphx_glr_viz_bezier_interpolator_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 115-118 A more complex scene scene ========================== .. GENERATED FROM PYTHON SOURCE LINES 118-124 .. code-block:: Python scene = window.Scene() show_manager = window.ShowManager( scene, size=(900, 768), reset_camera=False, order_transparent=True ) .. GENERATED FROM PYTHON SOURCE LINES 125-127 Note: If a control point is set to `None`, it gets the value of the point it controls. .. GENERATED FROM PYTHON SOURCE LINES 127-134 .. code-block:: Python keyframes = { # time - position - in control point - out control point 0.0: {'value': [-2, 0, 0], 'out_cp': [-15, 6, 0]}, 5.0: {'value': [18, 0, 0], 'in_cp': [27, 18, 0], 'out_cp': [27, -18, 0]}, 9.0: {'value': [-5, -10, -10]}, } .. GENERATED FROM PYTHON SOURCE LINES 135-136 Create the sphere actor. .. GENERATED FROM PYTHON SOURCE LINES 136-138 .. code-block:: Python sphere = actor.sphere(np.array([[0, 0, 0]]), (1, 0, 1)) .. GENERATED FROM PYTHON SOURCE LINES 139-140 Create an ``Animation`` and adding the sphere actor to it. .. GENERATED FROM PYTHON SOURCE LINES 140-142 .. code-block:: Python animation = Animation(sphere) .. GENERATED FROM PYTHON SOURCE LINES 143-144 Setting Cubic Bezier keyframes .. GENERATED FROM PYTHON SOURCE LINES 144-146 .. code-block:: Python animation.set_position_keyframes(keyframes) .. GENERATED FROM PYTHON SOURCE LINES 147-148 changing position interpolation into cubic bezier interpolation .. GENERATED FROM PYTHON SOURCE LINES 148-150 .. code-block:: Python animation.set_position_interpolator(cubic_bezier_interpolator) .. GENERATED FROM PYTHON SOURCE LINES 151-152 visualizing the points and control points (only for demonstration) .. GENERATED FROM PYTHON SOURCE LINES 152-170 .. code-block:: Python for t, keyframe in keyframes.items(): pos = keyframe.get('value') in_control_point = keyframe.get('in_cp') out_control_point = keyframe.get('out_cp') ########################################################################### # visualizing position keyframe vis_point = actor.sphere(np.array([pos]), (1, 0, 0), radii=0.3) scene.add(vis_point) ########################################################################### # Visualizing the control points and their length (if exist) for cp in [in_control_point, out_control_point]: if cp is not None: vis_cps = actor.sphere(np.array([cp]), (0, 0, 1), radii=0.6) cline_actor = actor.line(np.array([[pos, cp]]), colors=np.array([0, 1, 0])) scene.add(vis_cps, cline_actor) .. GENERATED FROM PYTHON SOURCE LINES 171-173 Initializing the timeline to be able to control the playback of the animation. .. GENERATED FROM PYTHON SOURCE LINES 173-175 .. code-block:: Python timeline = Timeline(animation, playback_panel=True) .. GENERATED FROM PYTHON SOURCE LINES 176-177 We only need to add the ``Timeline`` to the ``ShowManager`` .. GENERATED FROM PYTHON SOURCE LINES 177-179 .. code-block:: Python show_manager.add_animation(timeline) .. GENERATED FROM PYTHON SOURCE LINES 180-181 Start the animation .. GENERATED FROM PYTHON SOURCE LINES 181-185 .. code-block:: Python if interactive: show_manager.start() window.record(scene, out_path='viz_keyframe_animation_bezier_2.png', size=(900, 768)) .. image-sg:: /auto_examples/10_animation/images/sphx_glr_viz_bezier_interpolator_002.png :alt: viz bezier interpolator :srcset: /auto_examples/10_animation/images/sphx_glr_viz_bezier_interpolator_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.212 seconds) .. _sphx_glr_download_auto_examples_10_animation_viz_bezier_interpolator.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: viz_bezier_interpolator.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_bezier_interpolator.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_