.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/01_introductory/viz_timers.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_01_introductory_viz_timers.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_01_introductory_viz_timers.py:


===============
Using a timer
===============

This example shows how to create a simple animation using a timer callback.

We will use a sphere actor that generates many spheres of different colors,
radii and opacity. Then we will animate this actor by rotating and changing
global opacity levels from inside a user defined callback.

The timer will call this user defined callback every 200 milliseconds. The
application will exit after the callback has been called 100 times.

.. GENERATED FROM PYTHON SOURCE LINES 15-70



.. image-sg:: /auto_examples/01_introductory/images/sphx_glr_viz_timers_001.png
   :alt: viz timers
   :srcset: /auto_examples/01_introductory/images/sphx_glr_viz_timers_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import itertools

    import numpy as np

    import fury

    xyz = 10 * np.random.rand(100, 3)
    colors = np.random.rand(100, 4)
    radii = np.random.rand(100) + 0.5

    scene = fury.window.Scene()

    sphere_actor = fury.actor.sphere(centers=xyz, colors=colors, radii=radii)

    scene.add(sphere_actor)

    showm = fury.window.ShowManager(
        scene=scene, size=(900, 768), reset_camera=False, order_transparent=True
    )


    tb = fury.ui.TextBlock2D(bold=True)

    # use itertools to avoid global variables
    counter = itertools.count()


    def timer_callback(_obj, _event):
        global timer_id
        cnt = next(counter)
        tb.message = "Let's count up to 300 and exit :" + str(cnt)
        showm.scene.azimuth(0.05 * cnt)
        sphere_actor.GetProperty().SetOpacity(cnt / 100.0)
        showm.render()

        if cnt == 10:
            # destroy the first timer and replace it with another faster timer
            showm.destroy_timer(timer_id)
            timer_id = showm.add_timer_callback(True, 10, timer_callback)

        if cnt == 300:
            # destroy the second timer and exit
            showm.destroy_timer(timer_id)
            showm.exit()


    scene.add(tb)

    # Run every 200 milliseconds
    timer_id = showm.add_timer_callback(True, 200, timer_callback)

    showm.start()

    fury.window.record(scene=showm.scene, size=(900, 768), out_path="viz_timer.png")


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 6.232 seconds)


.. _sphx_glr_download_auto_examples_01_introductory_viz_timers.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: viz_timers.ipynb <viz_timers.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: viz_timers.py <viz_timers.py>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_