2014-02-05 116 views
0

我真的很新,在瑪雅腳本。我一直在研究一個相對簡單的工具,該工具創建了一個簡單的用戶界面,用戶可以使用它來將一個對象約束到另一個對象,但仍然可以自由移動該對象一旦受到約束。這是使用嵌套定位器完成的。瑪雅Python:只需要1個參數(給出2個)

如果我運行下面的代碼我得到的錯誤信息:

「#錯誤:attachObject()恰恰1參數(2給出)#」

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方法...代碼按預期工作。

#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) 

回答

2

您的def attachObject被寫爲無參數。在這種情況下,'self'會自動作爲類調用的一部分傳入,因此這是第一個參數。當你將它掛到按鈕回調函數中時,它也會得到一個自動參數 - 第二個參數。你可以通過打印你的'huh'變量進行驗證,它總是會以False的形式出現。

很多人使用應該以單下劃線(_),因此可以忽略命名值的約定: 高清attachObject(個體經營,_): #....

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

他人使用忽視或忽略*:

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

TLDR:這是預期的行爲

1

該按鈕被賦予你的功能attachObject作爲回調,當按鈕被按下時它將調用。瑪雅會通過它認爲可能有用的各種論點,並且沒有辦法告訴它你想要哪個。所以你只需要準備好任何可能引發你的事情。在這種情況下,似乎按鈕使用一個參數調用它們的command回調。爲了讓你的函數應付不管多少參數給出,使用Python的變量參數列表語法:

def attachObject(*args, **kw): 

現在args是包含所有被傳遞的位置參數的元組,而kw與所有字典關鍵字參數。

相關問題