如何在Python Maya中存储floatSliderGrp的值 [英] How to store the value of floatSliderGrp in Python Maya

查看:232
本文介绍了如何在Python Maya中存储floatSliderGrp的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从1到10的滑块,我想存储用户每次使用滑块选择的值.我希望该值随着滑块的移动而更新.这是我到目前为止所拥有的

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

我想传递PASSVALUE参数所在的值,有没有办法从滑块获取值?

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

推荐答案

'value'是floatSliderGrp的标志,而不是变量.这就是为什么您不能直接打印它的原因.

'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. 您可以存储已创建的控件的名称,然后在其他函数中要求的值.您可以使用partial将控件名称绑定到函数,或者可以仅重新排序创建它们的方式,以便在知道它们所关心的UI元素的名称的情况下创建函数.在这种情况下,您无需在滑块上输入命令,而可以在需要知道滑块值的其他东西上输入命令
  2. 您可以创建一个变量,并在滑块值更改时对其进行更新.如果许多其他工具需要知道该值,那么一个不错的选择是创建一个类,然后将滑块值戳入其中.然后其他代码就可以在不关心UI的情况下获取值.如果要在更改滑块时在场景中执行某些操作,可以更改update_slider_value()函数以在滑块移动时执行操作.
  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.

这里是方法1.它不会尝试将dc事件挂在滑块上,它只是记住滑块对象的名称,并在需要知道时查询值:

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)

这是方法2所完成的事情:

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

任何知道example的人都可以索要example.value并获取当前的滑块值.这对较复杂的案例很有用,但对简单的东西就算过分了.

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.

相关信息: Maya回调速查表

这篇关于如何在Python Maya中存储floatSliderGrp的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆