如何一次将不同的值传递给不同的函数

时间:2017-03-22 17:49:26

标签: python function user-interface window maya

我有一个问题......我正在用Python创建一个脚本...我有一些滑块来插入值..但我想要一些值去一个函数,一些其他函数去另一个函数...这是允许我在另一个单一函数(工作)中传递所有函数的脚本......并且在注释中我认为它可以用于我需要的行,但事实并非如此。

import maya.cmds as cmds

#def function1(value):
def function1(value1,value2):
    print(value1)

def function2(value):
    print(value)

def createUI(): 
    cmds.window("Window")
    cmds.columnLayout( adjustableColumn=True )
    value1 = cmds.intSliderGrp(label='number of moons', minValue=4, maxValue=20, value=12, field=True)
    value2 = cmds.intSliderGrp(label='Distance from parent to child', minValue=5, maxValue=40, value=20, field=True)

    #cmds.button(label = "OK", command = lambda *args: function1(cmds.intSliderGrp(value1, query=True, value=True)),function2(cmds.intSliderGrp(value2, query=True, value=True)))
    cmds.button(label = "OK", command = lambda *args: function1(cmds.intSliderGrp(value1, query=True, value=True), cmds.intSliderGrp(value2, query=True, value=True)))
    cmds.showWindow()

createUI()

1 个答案:

答案 0 :(得分:1)

import maya.cmds as cmds
from functools import partial

# Dictionnary to store the ui important variables
uiDic = {}

#def function1(value):
def function1(value1,value2):
    print(value1)
    print(value2)
# another normal function
def function2(value):
    print(value)

# save the slider value to the dic
def setSliderUiDic(sliderName, *args):
    entryName = '{}_value'.format(sliderName)
    uiDic[entryName] = getSlider(sliderName)

# get any slider value (in order to refresh)
def getSlider(nameCtrl):
    value = cmds.intSliderGrp(nameCtrl, query=True, value=True)
    return value

# create a def that combine multiple function to ui purposes
def ui_func(*args):
    slider1Value = uiDic['{0}_value'.format(uiDic['value1'])]
    slider2Value = uiDic['{0}_value'.format(uiDic['value2'])]
    function1(slider1Value, slider2Value)
    function2(slider1Value)

def createUI(): 
    #windowtest
    if cmds.window("wintest", q=True, ex=True):
        cmds.deleteUI("wintest")
    cmds.window("wintest")
    #layout
    cmds.columnLayout( adjustableColumn=True )
    #create the slider and store it to dic
    uiDic['value1'] = cmds.intSliderGrp(label='number of moons', minValue=4, maxValue=20, value=12, field=True)
    # Add a function to update the value of the slider
    cmds.intSliderGrp(uiDic['value1'], e=True, cc=partial(setSliderUiDic, uiDic['value1']))
    # Store the default value
    setSliderUiDic(uiDic['value1'])

    #Do the same thing to slider 2
    uiDic['value2'] = cmds.intSliderGrp(label='Distance from parent to child', minValue=5, maxValue=40, value=20, field=True)
    cmds.intSliderGrp(uiDic['value2'], e=True, cc=partial(setSliderUiDic, uiDic['value2']))
    setSliderUiDic(uiDic['value2'])

    # create a UI function that group multiple functions
    cmds.button(label = "OK", command = ui_func)
    cmds.showWindow()

createUI()