Extension building - sliders

Hello,

I’m trying to build a extension, following your UI documentation in Omniverse, I want to implement a slider widget.
But I do not know how should I access values that slider is showing.

The goal is to make slider change values of some joint properties of my UR10, but I have trouble figuring out the right code for accessing slider values. I tried to follow your Abstract Value Model tutorial in UI docs - but I couldn’t translate that to the slider problem.

I wondered if you can refer some .py files from which i can extrapolate the code or if you can give me some info how should I approach the problem.
I should mention that I am not an expert at python, but working with Omniverse has made me learn the basics of OOP and I wish to further that knowledge.

Thank you.

Hi @user102346! You could hold onto the model that is store in the slider object and get the value at any time or you could subscribe to the model for when the value changes. Here’s a the full code for a simple extension showing both:

import omni.ext
import omni.ui as ui


class MyWindow(ui.Window):
    def __init__(self, title: str = None, delegate=None, **kwargs):
        super().__init__(title, **kwargs)
        self.frame.set_build_fn(self._build_window)

    def _build_window(self):
        with ui.ScrollingFrame():
            with ui.VStack(height=0):
                ui.Label("My Label")
                self.proj_scale_model = ui.FloatSlider(min=0, max=100).model
                self.proj_scale_model_sub = self.proj_scale_model.subscribe_value_changed_fn( self.slider_value_changed )

                def clicked():
                    # Example showing how to retreive the value from the model.
                    print(f"Button Clicked! Slider Value: {self.proj_scale_model.as_float}")
                ui.Button("Click Me", clicked_fn=clicked)

    def slider_value_changed(self, m):
        # Example showing how to get the value when it changes.
        print("Slider Value:", m.as_float)


class MyExtension(omni.ext.IExt):
    def on_startup(self, ext_id):
        print("MyExtension startup")
        self._window = MyWindow("Name", width=300, height=300)

    def on_shutdown(self):
        print("MyExtension shutdown")
        if self._window:
            self._window.destroy()
            self._window = None

Thank you for the info, I appreciate it.