How to store the value of floatSliderGrp in Python Maya

拈花ヽ惹草 提交于 2020-01-11 12:20:20

问题


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?


回答1:


'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




回答2:


use the module : partial

form functools import partial

#update the query of the slider
def getSliderValue(sliderName):
    return cmds.floatSliderGrp(sliderName, q=1, v=1)

#funciton for the ui
def PrintValue(sliderName, *args):
    value = getSliderValue(sliderName)
    print value

#define slider into var
Slider = cmds.floatSliderGrp(label='Angle', field=True, minValue=0.0, maxValue=10.0, 
value=0, dc='placeholder' )
#now that we have the name of the slider, edit the function to pass it throught
cmds.floatSliderGrp(Slider , e=True, dc= partial(PrintValue, Slider))

---EDIT---- Here is another post with the same question

How to create Maya sliders to move objects in interface



来源:https://stackoverflow.com/questions/43569064/how-to-store-the-value-of-floatslidergrp-in-python-maya

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!