为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Qyouu 2023-07-11 16:39:05
所以我有点不好意思地说:但是我从其他函数调用函数的知识是有限的。我完成了一个完美运行的自动装配脚本:但我意识到我可以根据另一个用户的需要来修剪脂肪,方法是将大量重复命令放入自己的函数中,而不是在不同的形状上一遍又一遍地使用 setAttr。问题是我对这个技巧的了解有限。看起来,如果我通过向其中添加任何新函数来修改任何函数头中的 *args,我的脚本要么忽略它,要么直接停止工作。脚本本身相对简单:点击“构建示例曲线”后,我想要它做的是点击“设置曲线颜色”后,我希望 setAttributes 函数获取 setColor 函数并更改示例曲线的颜色如果有人能指出我正确的路径,我将不胜感激,安装和运行脚本的说明位于脚本的末尾:'''import exampleScriptTemplatereload (exampleScriptTemplate)exampleScriptTemplate.gui()'''import maya.cmds as cmdsif 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)    '''
查看完整描述

1 回答

?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

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


解决方案非常简单。查询内部的颜色索引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)

顺便说一下,你确实应该打破按名称对对象进行硬编码的习惯,并 100% 地使用变量。它将为您省去很多长期的痛苦,并使您的工具更加防弹。


查看完整回答
反对 回复 2023-07-11
  • 1 回答
  • 0 关注
  • 86 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信