.. 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-15 .. 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 16-22 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 22-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-47 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 47-51 .. 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 52-53 Visualizing points .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. 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 58-59 Visualizing the control points .. GENERATED FROM PYTHON SOURCE LINES 59-63 .. 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 64-65 Visualizing the connection between the control points and the points .. GENERATED FROM PYTHON SOURCE LINES 65-70 .. 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 71-72 Initializing an ``Animation`` and adding sphere actor to it. .. GENERATED FROM PYTHON SOURCE LINES 72-76 .. 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 77-88 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 88-96 .. 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 97-98 Changing position interpolation into cubic bezier interpolation .. GENERATED FROM PYTHON SOURCE LINES 98-100 .. code-block:: Python animation.set_position_interpolator(cubic_bezier_interpolator) .. GENERATED FROM PYTHON SOURCE LINES 101-102 Adding the visualization actors to the scene. .. GENERATED FROM PYTHON SOURCE LINES 102-104 .. code-block:: Python scene.add(pts_actor, cps_actor, cline_actor) .. GENERATED FROM PYTHON SOURCE LINES 105-106 Adding the animation to the ``ShowManager`` .. GENERATED FROM PYTHON SOURCE LINES 106-115 .. 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)) .. GENERATED FROM PYTHON SOURCE LINES 116-119 A more complex scene scene ========================== .. GENERATED FROM PYTHON SOURCE LINES 119-125 .. 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 126-128 Note: If a control point is set to `None`, it gets the value of the point it controls. .. GENERATED FROM PYTHON SOURCE LINES 128-135 .. 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 136-137 Create the sphere actor. .. GENERATED FROM PYTHON SOURCE LINES 137-139 .. code-block:: Python sphere = actor.sphere(np.array([[0, 0, 0]]), (1, 0, 1)) .. GENERATED FROM PYTHON SOURCE LINES 140-141 Create an ``Animation`` and adding the sphere actor to it. .. GENERATED FROM PYTHON SOURCE LINES 141-143 .. code-block:: Python animation = Animation(sphere) .. GENERATED FROM PYTHON SOURCE LINES 144-145 Setting Cubic Bezier keyframes .. GENERATED FROM PYTHON SOURCE LINES 145-147 .. code-block:: Python animation.set_position_keyframes(keyframes) .. GENERATED FROM PYTHON SOURCE LINES 148-149 changing position interpolation into cubic bezier interpolation .. GENERATED FROM PYTHON SOURCE LINES 149-151 .. code-block:: Python animation.set_position_interpolator(cubic_bezier_interpolator) .. GENERATED FROM PYTHON SOURCE LINES 152-153 visualizing the points and control points (only for demonstration) .. GENERATED FROM PYTHON SOURCE LINES 153-171 .. code-block:: Python for keyframe in keyframes.values(): 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 172-174 Initializing the timeline to be able to control the playback of the animation. .. GENERATED FROM PYTHON SOURCE LINES 174-176 .. code-block:: Python timeline = Timeline(animation, playback_panel=True) .. GENERATED FROM PYTHON SOURCE LINES 177-178 We only need to add the ``Timeline`` to the ``ShowManager`` .. GENERATED FROM PYTHON SOURCE LINES 178-180 .. code-block:: Python show_manager.add_animation(timeline) .. GENERATED FROM PYTHON SOURCE LINES 181-182 Start the animation .. GENERATED FROM PYTHON SOURCE LINES 182-186 .. code-block:: Python if interactive: show_manager.start() window.record(scene, out_path="viz_keyframe_animation_bezier_2.png", size=(900, 768)) .. _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 `_