.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/01_introductory/viz_multithread.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_multithread.py>`
        to download the full example code.

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

.. _sphx_glr_auto_examples_01_introductory_viz_multithread.py:


=======================================================
Multithreading Example
=======================================================

The goal of this demo is to show how to use different threads
to interact with FURY. In particular, the main thread is used
to update interactions and render the scene, while thread A
rotates the camera, thread B prints a counter, and thread C
adds and removes elements from the scene.

.. GENERATED FROM PYTHON SOURCE LINES 12-114

.. code-block:: Python


    from threading import Thread
    import time

    import numpy as np

    import fury

    # Preparing to draw some spheres
    xyz = 10 * (np.random.random((100, 3)) - 0.5)
    colors = np.random.random((100, 4))
    radii = np.random.random(100) + 0.5

    scene = fury.window.Scene()
    sphere_actor = fury.actor.sphere(
        centers=xyz, colors=colors, radii=radii, use_primitive=False
    )
    scene.add(sphere_actor)


    # Preparing the show manager as usual
    showm = fury.window.ShowManager(
        scene=scene, size=(900, 768), reset_camera=False, order_transparent=True
    )

    # showm.initialize()

    # Creating a text block to show a message and reset the camera
    tb = fury.ui.TextBlock2D(bold=True)
    scene.add(tb)
    scene.ResetCamera()


    # Create a function to print a counter to the console
    def print_counter():
        print("")
        for i in range(100):
            print("\rCounter: %d" % i, end="")
            message = "Let's count up to 100 and exit :" + str(i + 1)
            tb.message = message
            time.sleep(0.05)
            if showm.is_done():
                break
        showm.exit()
        print("")


    # Create a function to rotate the camera


    def rotate_camera():
        for i in range(100):
            if showm.lock_current():
                scene.azimuth(0.01 * i)
                showm.release_current()
                time.sleep(0.05)
            else:
                break


    # Create a function to add or remove the axes and increase its scale


    def add_remove_axes():
        current_axes = None
        for i in range(100):
            if showm.lock_current():
                if current_axes is None:
                    current_axes = fury.actor.axes(scale=(0.20 * i, 0.20 * i, 0.20 * i))
                    scene.add(current_axes)
                    pass
                else:
                    scene.rm(current_axes)
                    current_axes = None
                    pass
                showm.release_current()
                time.sleep(0.1)
            else:
                break


    # Start the threads
    # Multiple threads can be started here
    # First, one to rotate the camera
    thread_a = Thread(target=rotate_camera)
    thread_a.start()

    # Now let's start a thread that will print a counter
    thread_b = Thread(target=print_counter)
    thread_b.start()

    # Now let's start a thread that will add or remove axes
    thread_c = Thread(target=add_remove_axes)
    thread_c.start()

    # Let's start the show manager loop with multithreading option
    showm.start(multithreaded=True)

    # Wait for the threads to finish
    thread_a.join()
    thread_b.join()
    thread_c.join()


.. _sphx_glr_download_auto_examples_01_introductory_viz_multithread.py:

.. only:: html

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

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

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

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

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

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: viz_multithread.zip <viz_multithread.zip>`


.. only:: html

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

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