Maya Python:恰好接受1个参数(给定2个) [英] Maya Python: takes exactly 1 argument (2 given)

查看:97
本文介绍了Maya Python:恰好接受1个参数(给定2个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的对使用Maya编写脚本非常陌生.我一直在开发一个相对简单的工具,该工具创建一个简单的UI,用户可以使用该UI将一个对象约束到另一个对象,但是一旦对象受约束,仍然可以自由移动.这是使用嵌套定位器完成的.

I'm really quite new to scripting in Maya. I've been working on a relatively simple tool that creates a simple UI that a user can use to constrain one object to another, but still have the freedom to move that object once its constrained. This is done using nested locators.

如果运行以下代码,则会收到错误消息:

If I run the following code I get the error msg:

#错误:attachObject()恰好接受1个参数(给定2个)#"

"# Error: attachObject() takes exactly 1 argument (2 given) # "

import maya.cmds as mc
from functools import partial

#Class to hold UI methods
class UI():

    def __init__(self):
        self.windowName = "Object Attachment Tool"
        self.target = None
        self.constraintTarget = None
        self.constraintTargetName = ""

    def buildUI(self):
        #Add code to check if Window already exists

        #Window is not re-sizeable
        objAttachTool = mc.window( self.windowName, title = "Object Attachment Tool", sizeable = False, widthHeight = ( 1024, 750 ) )
        mc.columnLayout( width = 1024 )

        mc.text( "\nTarget:\n" )

        self.targetTextFieldButtonGrp = mc.textFieldButtonGrp( buttonLabel = "Update", buttonCommand = partial( self.updateTarget ) )

        mc.text( "\nObject or Control to be Constrained:\n" )

        self.constraintTargetTextFieldButtonGrp = mc.textFieldButtonGrp( buttonLabel = "Update", buttonCommand = partial( self.updateConstraintTarget ) )

        test = mc.button( label = "Attach Object", command = self.attachObject )

        mc.showWindow()

    #This method sets the currently selected object as the target object.  
    #It also updates the Target textFieldButtonGrp to display the name of the current target object.
    def updateTarget(self):
        #Get name of selected object
        #Assign selected object to a variable to keep track of it
        selectedObj = mc.ls( selection = True )
        #Check that only one object is selected
        if len( selectedObj ) != 1:
            print "Select exactly ONE object or control!"
        else:
            #Update text field with name of selected object
            mc.textFieldButtonGrp( self.targetTextFieldButtonGrp, edit = True, text = selectedObj[0] )

            #Set selected object as target
            self.target = selectedObj

    #This method sets the currently selected object as the object to be constrained.
    #It also updates the object's textFieldButtonGrp to display the name of the current constraint target
    def updateConstraintTarget(self):
        #Get the name of the currently selected object
        selectedObj = mc.ls( selection = True )
        #Make sure exactly ONE object is selected
        if len ( selectedObj ) != 1:
            print "Select exactly ONE object or control!"
        else:
            self.constraintTargetName = selectedObj[0]
            #Update text field with name of constraint target
            mc.textFieldButtonGrp( self.constraintTargetTextFieldButtonGrp, edit = True, text = self.constraintTargetName )

            #Update constraintTarget
            self.constraintTarget = selectedObj


    #This method attaches the constraint target to the target using locators.        
    def attachObject(self):
        #Check that a target and constraint target have been specified
        if ( self.target == None ) | ( self.constraintTarget == None ):
            print "Make sure you have selected a *Target* and an *Object or Control to be Constrained*" 
            print "self.target = " + self.target + "\n"
            print "self.constraintTarget = " + self.constraintTarget + "\n"
        else:
            #Create locator named after constraint target
            targetLoc = mc.spaceLocator( name = self.constraintTargetName )
            #Create locator named constraintTargetName + _MANIP
            targetManipLoc = mc.spaceLocator( name = ( self.constraintTargetName + "_MANIP" ) )
            #Parent MANIP to constraint target locator
            mc.parent ( targetManipLoc, targetLoc )

            #Parent Constrain constraint target to _MANIP locator with maintain offset OFF
            mc.parentConstraint( targetManipLoc, self.constraintTarget, maintainOffset = False )

            #Parent Constrain parent locator to target
            mc.parentConstraint( self.target, targetLoc, maintainOffset = False ) 

UI()

#Create an instance of the UI
userInterface = UI()
userInterface.buildUI()

但是,如果我像下面这样简单地向def attachObject方法中添加另一个参数,则该代码将按预期工作.

But if I simply add another argument to the def attachObject method like below...the code works as expected.

#This method attaches the constraint target to the target using locators.        
def attachObject(self, huh):
    #Check that a target and constraint target have been specified
    if ( self.target == None ) | ( self.constraintTarget == None ):
        print "Make sure you have selected a *Target* and an *Object or Control to be Constrained*" 
        print "self.target = " + self.target + "\n"
        print "self.constraintTarget = " + self.constraintTarget + "\n"
    else:
        #Create locator named after constraint target
        targetLoc = mc.spaceLocator( name = self.constraintTargetName )
        #Create locator named constraintTargetName + _MANIP
        targetManipLoc = mc.spaceLocator( name = ( self.constraintTargetName + "_MANIP" ) )
        #Parent MANIP to constraint target locator
        mc.parent ( targetManipLoc, targetLoc )

        #Parent Constrain constraint target to _MANIP locator with maintain offset OFF
        mc.parentConstraint( targetManipLoc, self.constraintTarget, maintainOffset = False )

        #Parent Constrain parent locator to target
        mc.parentConstraint( self.target, targetLoc, maintainOffset = False ) 

推荐答案

您的def attachObject编写为不带参数.在这种情况下,"self"将作为类调用的一部分自动传递,因此这是第一个参数.当您将其挂接到按钮回调时,它也会自动传入一个参数-那就是第二个参数.您可以通过打印'huh'变量进行验证,该变量将始终显示为False.

Your def attachObject is written to take no args. In this case, 'self' is automatically passed in as part of the class call so that's the first argument . When you hook it to the button callback it gets an automatic argument passed in as well - thats argument number two. You can verify by printing your 'huh' variable, it will always come out as False.

许多人使用命名值的惯例,应该使用单个下划线(_)来忽略它们,因此: def attachObject(self,_): #....

A lot of people use the convention of naming values that should be ignored with a single underscore (_) thus: def attachObject(self, _): #....

 #or if you might get multiple ignore arguments:
def attachObject(self, *_):
    #....

其他人使用忽略或*忽略:

others use ignore or *ignore:

def attachObject(self, *ignore):
    #....

TLDR:这是预期的行为

TLDR: this is expected behavior

这篇关于Maya Python:恰好接受1个参数(给定2个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆