Search code examples
pythonmaya

Maya Python: call function from other function with GUI


so I'm kindof embarrassed to say: but my knowledge of calling functions from other functions is limited. I finished an auto rigging script that works perfectly: but I realized I can trim the fat according to another user by condensing alot of the repetitive commands by putting them into their own functions instead of using setAttr over and over again on different shapes. The problem is my knowledge on this trick is limited. It seems like if I screw with the *args in any of my function headers in anyway by adding any new function to it my script either ignores it or stops working outright.

The script itself is relatively simple: after hitting "build example curves" what I want it to do is upon hitting "Set Curve Color" I want the setAttributes function to source the setColor function and change the color of the example curves

if anyone can point me in the right path I would appreciate it, the instructions to install and run the script are at the end of the script:

'''
import exampleScriptTemplate
reload (exampleScriptTemplate)
exampleScriptTemplate.gui()
'''

import maya.cmds as cmds

if cmds.window("buildWin", exists =True):
    cmds.deleteUI("buildWin", window = True)

myWindow = cmds.window("buildWin",t='DS_colorChanger',rtf=1,w=100, h=100, toolbox=True)
column = cmds.columnLayout(adj=True)

def gui(*args):
    cmds.columnLayout()
    cmds.button(w=300,label='build example curves',c=exampleShapes)
    cmds.colorIndexSliderGrp('controlColor',
        label='Control Color',
        min=0,
        max=31,
        value=1,
        columnWidth=[( 1, 80 ),( 2, 40 ), ( 3, 150 )])
    cmds.button(w=300,label='Set Curve Color',c=setAttrbutes)
    cmds.showWindow(myWindow)
    
def exampleShapes(*args):
    cmds.circle(n='exampleCirc1',ch=False,nr = (0,0,0))
    cmds.circle(n='exampleCirc2',ch=False,nr = (0,0,0))
    cmds.circle(n='exampleCirc3',ch=False,nr = (0,0,0))
    
    cmds.setAttr('exampleCirc1.translateX',-2)
    cmds.setAttr('exampleCirc3.translateX',2)
    
def setAttrbutes(setColor,*args):
    
    cmds.setAttr('exampleCirc1',setColor)
    cmds.setAttr('exampleCirc2',setColor)
    cmds.setAttr('exampleCirc3',setColor)

def setColor(*args):
    colorPref = cmds.colorIndexSliderGrp('controlColor',query=True,value=True)
    colorPref = colorPref -1 
    
    cmds.setAttr('.overrideEnabled', 1)
    cmds.setAttr('.overrideColor', colorPref)
    
'''
To run this script:
   1.) Go to d:\Users\userName\Documents\maya\2020\scripts
   2.) Create a new python file named exampleScriptTemplate.py and paste the code in that file
   3.) open a python tab in the Maya script editor and run the following 3 lines:
        import exampleScriptTemplate
        reload (exampleScriptTemplate)
        exampleScriptTemplate.gui()
'''

Solution

  • You may need to read up on some basic function tutorials to get a fundamental understanding on how to pass values through function parameters. Also the cmds.setAttr commands in your setColor function don't have an object passed to it, so... I'm not sure how you were expecting that to magically work.

    The solution is pretty simple. Query the color index inside setAttributes, then call setColor for each of the objects, passing the object and color index to the function.

    import maya.cmds as cmds
    
    if cmds.window("buildWin", exists =True):
        cmds.deleteUI("buildWin", window = True)
    
    myWindow = cmds.window("buildWin",t='DS_colorChanger',rtf=1,w=100, h=100, toolbox=True)
    column = cmds.columnLayout(adj=True)
    
    def gui(*args):
        cmds.columnLayout()
        cmds.button(w=300,label='build example curves',c=exampleShapes)
        cmds.colorIndexSliderGrp('controlColor',
            label='Control Color',
            min=1,
            max=31,
            value=1,
            columnWidth=[( 1, 80 ),( 2, 40 ), ( 3, 150 )])
        cmds.button(w=300,label='Set Curve Color',c=setAttrbutes)
        cmds.showWindow(myWindow)
        
    def exampleShapes(*args):
        cmds.circle(n='exampleCirc1',ch=False,nr = (0,0,0))
        cmds.circle(n='exampleCirc2',ch=False,nr = (0,0,0))
        cmds.circle(n='exampleCirc3',ch=False,nr = (0,0,0))
        
        cmds.setAttr('exampleCirc1.translateX',-2)
        cmds.setAttr('exampleCirc3.translateX',2)
        
    def setAttrbutes(*args):
        colorPref = cmds.colorIndexSliderGrp('controlColor',query=True,value=True) - 1  # Query color index here.
        setColor('exampleCirc1', colorPref)  # Call set color here with the object and color index.
        setColor('exampleCirc2', colorPref)
        setColor('exampleCirc3', colorPref)
    
    def setColor(obj, colorPref):  # Requires an object, and the color index to set it as.
        cmds.setAttr(obj + '.overrideEnabled', 1)  # Need to pass an object!!
        cmds.setAttr(obj + '.overrideColor', colorPref)
    

    By the way you really should break that habit of hard-coding objects by their names and use variables 100% of the time. It will save you a lot of long-term grief and make your tools more bulletproof.