【问题标题】:Python in Maya: main function calls other function while passing variables?Maya中的Python:主函数在传递变量时调用其他函数?
【发布时间】:2017-09-16 01:02:41
【问题描述】:

我是一名动画师,是编码初学者...感谢您的帮助!

我正在编写一个 Python 脚本来自动复制 Maya 中的控制器对象。它为角色手中的每个骨骼创建控制器。

它作为单个长函数工作得很好,但我想要一个主函数“handCtrls()”,在其中调用函数以执行各种任务。我还需要将变量传递给子函数,因为我正在迭代变量“i”。

我在调用函数“nullGrouper(side, boney, i, ctrl)”的行上出现错误,并且错误说“全局变量 'ctrl' 未定义”。但它是在“ctrlDup”函数中定义的,我使用了“return ctrl”,虽然我会将该变量发送回“handCtrls()”。

代码如下:

import maya.cmds as c

def ctrlDup(side, boney, i, ctrlOrig):
    #Select and duplicate the original Control, and place it at the appropiate boney
    c.select(ctrlOrig)
    ctrl = c.duplicate(ctrlOrig, n=side + boney + str(i) + '_CTRL')
    print(ctrl)
    print('ctrlDup passed')
    return ctrl

def nullGrouper(side, boney, i, ctrl):
    #Create null group at same position as Control
    print(ctrl)
    print(ctrl[0])
    nullGrp = c.group(n=side + boney + str(i) + '_GRP', empty=1, world=1)
    print(nullGrp)
    c.parentConstraint(ctrl, nullGrp, n='nullGrp_parentConstr_temp')
    c.delete('nullGrp_parentConstr_temp')
    print('nullGrouper passed')
    return ctrl, nullGrp
def handCtrls():
    #First select the Controller to be duplicated
    sel = c.ls(sl=1)
    ctrlOrig = sel[0]
    #List sides
    sideList = ['L_', 'R_']
    #List bones of part
    boneyList = ['thumb', 'index', 'middle', 'ring', 'pinky']
    #Now, iterate across finger joints, up to 3, group controls, and parent them
    for side in sideList:
        for boney in boneyList:
            for i in range(1, 4, 1):
                #Check if boney is thumb, and joint number is 3
                if (boney!='thumb') or (i!=3):
                    ctrlDup(side, boney, i, ctrlOrig)
                    nullGrouper(side, boney, i, ctrl)
                else:
                    pass

    print("It's done.")

handCtrls()

有几个“打印”命令只是为了检查变量是否被传递。谢谢!

【问题讨论】:

    标签: python animation maya


    【解决方案1】:

    这是因为你没有存储返回

    ...
    ctrl = ctrlDup(side, boney, i, ctrlOrig)
    nullGrouper(side, boney, i, ctrl)
    

    【讨论】:

    • 感谢您的帮助!如果我可能会问:我将如何传递两个变量?像这样:ctrl, var2 = ctrlDup(side, boney, i, ctrlOrig)?
    • 你必须返回一个列表或一个元组:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 2016-05-27
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    相关资源
    最近更新 更多