Maya Python: 使用 GUI 从其他函数调用函数

Maya Python: call function from other function with GUI

所以我有点不好意思说:但是我对从其他函数调用函数的知识是有限的。我完成了一个完美运行的自动装配脚本:但我意识到我可以 trim 根据另一个用户的要求,通过将大量重复命令压缩到它们自己的函数中,而不是在不同的地方一遍又一遍地使用 setAttr形状。问题是我对这个技巧的了解有限。似乎如果我通过向它添加任何新函数来搞砸我的任何函数头文件中的 *args 我的脚本要么忽略它要么完全停止工作。

脚本本身相对简单:点击“构建示例曲线”后,我希望它做的是点击“设置曲线颜色”后,我希望 setAttributes 函数获取 setColor 函数并更改示例的颜色曲线

如果有人能指出正确的路径,我将不胜感激,安装说明和 运行 脚本位于脚本末尾:

'''
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\maya20\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()
'''

您可能需要阅读一些基本的函数教程,以基本了解如何通过函数参数传递值。此外,setColor 函数中的 cmds.setAttr 命令没有传递给它的对象,所以......我不确定你期望它如何神奇地工作。

解决方法很简单。查询 setAttributes 中的颜色索引,然后为每个对象调用 setColor,将对象和颜色索引传递给函数。

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)

顺便说一下,您确实应该打破 hard-coding 对象按名称命名的习惯,并在 100% 的时间使用变量。它将为您省去很多 long-term 痛苦,并使您的工具更安全。