Cube & Slider Control#

This example shows how to use the UI API. We will demonstrate how to create a cube and control with sliders.

First, some imports.

from fury import ui, window, actor
from import fetch_viz_icons
import numpy as np

First we need to fetch some icons that are included in FURY.

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': ('', 'BC1FEEA6F58BA3601D6A0B029EB8DFC5F352E21F2A16BA41099A96AA3F5A4735')}, '/Users/skoudoro/.fury/icons')

Cube and sliders#

Add a cube to the scene .

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]]))

Now we’ll add five sliders: 1 circular and 4 linear sliders. By default the alignments are ‘bottom’ for horizontal and ‘top’ for vertical.

ring_slider = ui.RingSlider2D(center=(630, 400), initial_value=0,

hor_line_slider_text_top = ui.LineSlider2D(center=(400, 230), initial_value=0,
                                           min_value=-10, max_value=10,

hor_line_slider_text_bottom = ui.LineSlider2D(center=(400, 200),
                                              min_value=-10, max_value=10,

ver_line_slider_text_left = ui.LineSlider2D(center=(100, 400), initial_value=0,
                                            min_value=-10, max_value=10,

ver_line_slider_text_right = ui.LineSlider2D(center=(150, 400),
                                             min_value=-10, max_value=10,

We can use a callback to rotate the cube with the ring slider.

def rotate_cube(slider):
    angle = slider.value
    previous_angle = slider.previous_value
    rotation_angle = angle - previous_angle

ring_slider.on_change = rotate_cube

Similarly, we can translate the cube with the line slider.

def translate_cube(slider):
    value = slider.value
    cube.SetPosition(value, 0, 0)

hor_line_slider_text_top.on_change = translate_cube
hor_line_slider_text_bottom.on_change = translate_cube
ver_line_slider_text_left.on_change = translate_cube
ver_line_slider_text_right.on_change = translate_cube

Show Manager#

Now that all the elements have been initialised, we add them to the show manager.

Visibility by default is True

Set camera for better visualization

viz ui slider

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

Gallery generated by Sphinx-Gallery