.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_introductory/viz_earth_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_01_introductory_viz_earth_animation.py: ========================= Texture Sphere Animation ========================= In this tutorial, we will show how to animate a textured sphere. .. GENERATED FROM PYTHON SOURCE LINES 7-20 .. code-block:: Python import itertools import numpy as np from fury import actor, io, utils, window from fury.data import ( fetch_viz_models, fetch_viz_textures, read_viz_models, read_viz_textures, ) .. GENERATED FROM PYTHON SOURCE LINES 21-22 Create a scene to start. .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: Python scene = window.Scene() .. GENERATED FROM PYTHON SOURCE LINES 26-31 Next, load in a texture for each of the actors. For this tutorial, we will be creating one textured sphere for the Earth, and another for the moon. Collect the Earth texture from the FURY github using ``fetch_viz_textures`` and ``read_viz_textures``, then use ``io.load_image`` to load in the image. .. GENERATED FROM PYTHON SOURCE LINES 31-36 .. code-block:: Python fetch_viz_textures() earth_filename = read_viz_textures('1_earth_8k.jpg') earth_image = io.load_image(earth_filename) .. rst-class:: sphx-glr-script-out .. code-block:: none Dataset is already in place. If you want to fetch it again please first remove the folder /Users/skoudoro/.fury/textures .. GENERATED FROM PYTHON SOURCE LINES 37-39 Using ``actor.texture_on_sphere()``, create an earth_actor with your newly loaded texture. .. GENERATED FROM PYTHON SOURCE LINES 39-42 .. code-block:: Python earth_actor = actor.texture_on_sphere(earth_image) .. GENERATED FROM PYTHON SOURCE LINES 43-44 Then, do the same for the moon. .. GENERATED FROM PYTHON SOURCE LINES 44-50 .. code-block:: Python moon_filename = read_viz_textures('moon-8k.jpg') moon_image = io.load_image(moon_filename) moon_actor = actor.texture_on_sphere(moon_image) .. GENERATED FROM PYTHON SOURCE LINES 51-52 Add both actors to the already existing scene. .. GENERATED FROM PYTHON SOURCE LINES 52-56 .. code-block:: Python scene.add(earth_actor) scene.add(moon_actor) .. GENERATED FROM PYTHON SOURCE LINES 57-61 Next, alter the position and scale of the moon to correctly size it in comparison to the Earth using ``actor.SetPosition()`` and ``actor.SetScale()``, and rotate the Earth using ``utils.rotate`` to correctly align the texture. .. GENERATED FROM PYTHON SOURCE LINES 61-66 .. code-block:: Python moon_actor.SetPosition(1, 0.1, 0.5) moon_actor.SetScale(0.25, 0.25, 0.25) utils.rotate(earth_actor, (-90, 1, 0, 0)) .. GENERATED FROM PYTHON SOURCE LINES 67-69 The ShowManager class is the interface between the scene, the window and the interactor. .. GENERATED FROM PYTHON SOURCE LINES 69-74 .. code-block:: Python showm = window.ShowManager( scene, size=(900, 768), reset_camera=False, order_transparent=True ) .. GENERATED FROM PYTHON SOURCE LINES 75-78 Next, let's focus on creating the animation. We can determine the duration of animation with using the ``counter``. Use itertools to avoid global variables. .. GENERATED FROM PYTHON SOURCE LINES 78-81 .. code-block:: Python counter = itertools.count() .. GENERATED FROM PYTHON SOURCE LINES 82-84 Use ``set_camera`` to ensure the camera is in the optimal position for the scene. .. GENERATED FROM PYTHON SOURCE LINES 84-91 .. code-block:: Python scene.set_camera( position=(0.24, 0.00, 4.34), focal_point=(0.00, 0.00, 0.00), view_up=(0.00, 1.00, 0.00), ) .. GENERATED FROM PYTHON SOURCE LINES 92-94 Let's create a sphere actor to add to the Earth. We will place this sphere on the Earth's surface on Bloomington, IN, home of FURY's headquarters! .. GENERATED FROM PYTHON SOURCE LINES 94-99 .. code-block:: Python center = np.array([[-0.39, 0.3175, 0.025]]) radius = 0.002 sphere_actor = actor.sphere(center, window.colors.blue_medium, radius) .. GENERATED FROM PYTHON SOURCE LINES 100-101 Also creating a text actor to add below the sphere. .. GENERATED FROM PYTHON SOURCE LINES 101-107 .. code-block:: Python text_actor = actor.text_3d( 'Bloomington, Indiana', (-0.42, 0.31, 0.03), window.colors.white, 0.004 ) utils.rotate(text_actor, (-90, 0, 1, 0)) .. GENERATED FROM PYTHON SOURCE LINES 108-109 Let's also import a model of a satellite to visualize circling the moon. .. GENERATED FROM PYTHON SOURCE LINES 109-119 .. code-block:: Python fetch_viz_models() satellite_filename = read_viz_models('satellite_obj.obj') satellite = io.load_polydata(satellite_filename) satellite_actor = utils.get_actor_from_polydata(satellite) satellite_actor.SetPosition(-0.75, 0.1, 0.4) satellite_actor.SetScale(0.005, 0.005, 0.005) .. rst-class:: sphx-glr-script-out .. code-block:: none Dataset is already in place. If you want to fetch it again please first remove the folder /Users/skoudoro/.fury/models .. GENERATED FROM PYTHON SOURCE LINES 120-124 In the ``timer_callback`` function, use if statements to specify when certain events will happen in the animation, based on the position that the counter is at. So, for example, the earth actor will continue to rotate while the count is less than 450. .. GENERATED FROM PYTHON SOURCE LINES 124-169 .. code-block:: Python def timer_callback(_obj, _event): cnt = next(counter) showm.render() if cnt < 450: utils.rotate(earth_actor, (1, 0, 1, 0)) if cnt % 5 == 0 and cnt < 450: showm.scene.azimuth(-1) if cnt == 300: scene.set_camera( position=(-3.679, 0.00, 2.314), focal_point=(0.0, 0.35, 0.00), view_up=(0.00, 1.00, 0.00), ) if cnt > 300 and cnt < 450: scene.zoom(1.01) if cnt >= 450 and cnt < 1500: scene.add(sphere_actor) scene.add(text_actor) if cnt >= 450 and cnt < 550: scene.zoom(1.01) if cnt == 575: moon_actor.SetPosition(-1, 0.1, 0.5) scene.set_camera( position=(-0.5, 0.1, 0.00), focal_point=(-1, 0.1, 0.5), view_up=(0.00, 1.00, 0.00), ) scene.zoom(0.03) scene.add(satellite_actor) utils.rotate(satellite_actor, (180, 0, 1, 0)) scene.rm(earth_actor) if cnt > 575 and cnt < 750: showm.scene.azimuth(-2) utils.rotate(moon_actor, (-2, 0, 1, 0)) satellite_actor.SetPosition(-0.8, 0.1 - cnt / 10000, 0.4) if cnt >= 750 and cnt < 1100: showm.scene.azimuth(-2) utils.rotate(moon_actor, (-2, 0, 1, 0)) satellite_actor.SetPosition(-0.8, -0.07 + cnt / 10000, 0.4) if cnt == 1100: showm.exit() .. GENERATED FROM PYTHON SOURCE LINES 170-171 Watch your new animation take place! .. GENERATED FROM PYTHON SOURCE LINES 171-176 .. code-block:: Python showm.add_timer_callback(True, 35, timer_callback) showm.start() window.record(showm.scene, size=(900, 768), out_path='viz_earth_animation.png') .. image-sg:: /auto_examples/01_introductory/images/sphx_glr_viz_earth_animation_001.png :alt: viz earth animation :srcset: /auto_examples/01_introductory/images/sphx_glr_viz_earth_animation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 39.436 seconds) .. _sphx_glr_download_auto_examples_01_introductory_viz_earth_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_earth_animation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_earth_animation.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_