.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/07_ui/viz_check_boxes.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_07_ui_viz_check_boxes.py: ============================================================ Figure and Color Control using Check boxes and Radio Buttons ============================================================ This example shows how to use the CheckBox UI API. We will demonstrate how to create a cube, sphere, cone and arrow and control its color and visibility using checkboxes. First, some imports. .. GENERATED FROM PYTHON SOURCE LINES 12-18 .. code-block:: Python import numpy as np from fury import actor, ui, utils, window from fury.data import fetch_viz_icons .. GENERATED FROM PYTHON SOURCE LINES 19-20 First we need to fetch some icons that are included in FURY. .. GENERATED FROM PYTHON SOURCE LINES 20-23 .. code-block:: Python fetch_viz_icons() .. rst-class:: sphx-glr-script-out .. code-block:: none Data size is approximately 12KB Dataset is already in place. If you want to fetch it again please first remove the folder /Users/skoudoro/.fury/icons ({'icomoon.tar.gz': ('https://digital.lib.washington.edu/researchworks/bitstream/handle/1773/38478/icomoon.tar.gz', 'BC1FEEA6F58BA3601D6A0B029EB8DFC5F352E21F2A16BA41099A96AA3F5A4735')}, '/Users/skoudoro/.fury/icons') .. GENERATED FROM PYTHON SOURCE LINES 24-25 We create the corresponding object actors for cube, sphere, cone and arrow. .. GENERATED FROM PYTHON SOURCE LINES 25-57 .. code-block:: Python cube = actor.cube( centers=np.array([[15, 0, 0]]), colors=np.array([[0, 0, 1]]), scales=np.array([[20, 20, 20]]), directions=np.array([[0, 0, 1]]), ) sphere = actor.sphere( centers=np.array([[50, 0, 0]]), colors=np.array([[0, 0, 1]]), radii=11.0, theta=360, phi=360, ) cone = actor.cone( centers=np.array([[-20, -0.5, 0]]), directions=np.array([[0, 1, 0]]), colors=np.array([[0, 0, 1]]), heights=20, resolution=100, ) arrow = actor.arrow( centers=np.array([[0, 25, 0]]), colors=np.array([[0, 0, 1]]), directions=np.array([[1, 0, 0]]), heights=40, resolution=100, ) .. GENERATED FROM PYTHON SOURCE LINES 58-60 We perform symmetric difference to determine the unchecked options. We also define methods to render visibility and color. .. GENERATED FROM PYTHON SOURCE LINES 60-103 .. code-block:: Python # Get difference between two lists. def sym_diff(l1, l2): return list(set(l1).symmetric_difference(set(l2))) # Set Visibility of the figures def set_figure_visiblity(checkboxes): checked = checkboxes.checked_labels unchecked = sym_diff(list(figure_dict), checked) for visible in checked: figure_dict[visible].SetVisibility(True) for invisible in unchecked: figure_dict[invisible].SetVisibility(False) def update_colors(color_array): for _, figure in figure_dict.items(): vcolors = utils.colors_from_actor(figure) vcolors[:] = color_array utils.update_actor(figure) # Toggle colors of the figures def toggle_color(checkboxes): colors = checkboxes.checked_labels color_array = np.array([0, 0, 0]) for col in colors: if col == 'Red': color_array[0] = 255 elif col == 'Green': color_array[1] = 255 else: color_array[2] = 255 update_colors(color_array) .. GENERATED FROM PYTHON SOURCE LINES 104-106 We define a dictionary to store the actors with their names as keys. A checkbox is created with actor names as it's options. .. GENERATED FROM PYTHON SOURCE LINES 106-117 .. code-block:: Python figure_dict = {'cube': cube, 'sphere': sphere, 'cone': cone, 'arrow': arrow} check_box = ui.Checkbox( list(figure_dict), list(figure_dict), padding=1, font_size=18, font_family='Arial', position=(400, 85), ) .. GENERATED FROM PYTHON SOURCE LINES 118-119 A similar checkbox is created for changing colors. .. GENERATED FROM PYTHON SOURCE LINES 119-135 .. code-block:: Python options = {'Blue': (0, 0, 1), 'Red': (1, 0, 0), 'Green': (0, 1, 0)} color_toggler = ui.Checkbox( list(options), checked_labels=['Blue'], padding=1, font_size=16, font_family='Arial', position=(600, 120), ) check_box.on_change = set_figure_visiblity color_toggler.on_change = toggle_color .. GENERATED FROM PYTHON SOURCE LINES 136-141 Show Manager ================================== Now that all the elements have been initialised, we add them to the show manager. .. GENERATED FROM PYTHON SOURCE LINES 141-152 .. code-block:: Python current_size = (1000, 1000) show_manager = window.ShowManager(size=current_size, title='FURY Checkbox Example') show_manager.scene.add(cube) show_manager.scene.add(sphere) show_manager.scene.add(cone) show_manager.scene.add(arrow) show_manager.scene.add(check_box) show_manager.scene.add(color_toggler) .. GENERATED FROM PYTHON SOURCE LINES 153-154 Set camera for better visualization .. GENERATED FROM PYTHON SOURCE LINES 154-165 .. code-block:: Python show_manager.scene.reset_camera() show_manager.scene.set_camera(position=(0, 0, 150)) show_manager.scene.reset_clipping_range() show_manager.scene.azimuth(30) interactive = False if interactive: show_manager.start() window.record(show_manager.scene, size=current_size, out_path='viz_checkbox.png') .. image-sg:: /auto_examples/07_ui/images/sphx_glr_viz_check_boxes_001.png :alt: viz check boxes :srcset: /auto_examples/07_ui/images/sphx_glr_viz_check_boxes_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.140 seconds) .. _sphx_glr_download_auto_examples_07_ui_viz_check_boxes.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: viz_check_boxes.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_check_boxes.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_