Search code examples
pythonfunctionuser-interfacewindowmaya

How to pass different values to different functions at once


I have a question...I am making a script in Python...I have some sliders to insert the values..but i would like some values to go to one function, some others to another function...Here is the script that allow me to pass all the functions in another one single function(working)...and in the comment the line that i thought it would have worked for what I need, but it does not.

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

Solution

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