Search code examples
pythonmayauislidergetvalue

How to store the value of floatSliderGrp in Python Maya


I have a slider that goes from 1 to 10 and I want to store the value that the user chose with the slider every time. I want the value to get updated with the movement of the slider. This is what I have so far

def PrintValue():
    print value

cmds.floatSliderGrp(label='Angle', field=True, minValue=0.0, maxValue=10.0, 
value=0 ,dc=PrintValue(PASSVALUE) )

I want to pass the value where the PASSVALUE argument is, is there a way to get the value from the slider?


Solution

  • 'value' is a flag for the floatSliderGrp -- not a variable. That's why you can't print it directly.

    Depending on what you want to do, you've got two ways to move forward.

    1. You can store the name of the control you've created and then ask the for the value in other functions. You can use partial to bind control names to functions, or you can just re-order the way you create them so that the functions are created knowing the names of the UI elements they care about. You don't put a command on the slider in this case, but on something else that needs to know the value of the slider
    2. you can create a variable and update it whenever slider value changes. If lots of different tools need to know the value, a good option is to create a class and poke the slider value into it. Then other code can just grab the value without caring about the UI. If you wanted to do something in the scene when the slider changes, you could change the update_slider_value() function to do things as the slider is moved.

    Here's approach #1. It doesn't try to hook the dc event on the slider - it just remembers the name of the slider objects and queries the value when it needs to know:

     wnd = cmds.window()
     col = cmds.columnLayout()
     slider = cmds.floatSliderGrp(label='Angle', field=True, minValue=0.0, maxValue=10.0, value=0)
     b = cmds.button('print value')
     # since this is defined after `slider`, it knows the value 
     def print_value(*_):
         print cmds.floatSliderGrp(slider, q=True, v=True)
     cmds.button(b, e=True, c=print_value)
     cmds.showWindow(wnd)
    

    Here's the same thing accomplished with approach #2:

    class SliderWindow(object):
    
        def __init__(self):
            self.value = 0
            self.window = cmds.window(title ="SliderWindow")
            self.column = cmds.columnLayout()
            self.slider = cmds.floatSliderGrp(label='Angle', field=True, minValue=0.0, maxValue=10.0, value=0, dc= self.update_slider_value)
            cmds.button('print value', c= self.print_value)
            cmds.showWindow(self.window)
    
        def update_slider_value(self, *_):
            self.value = cmds.floatSliderGrp(self.slider, q=True, v=True)
    
    
        def print_value(self, *_):
            print self.value
    
    example = SliderWindow()
    

    Anybody who knows example can just ask for example.value and get the current slider value. This is good for more complex cases, but overkill for simple stuff.

    related info: Maya callbacks cheatsheet