我真的很新,在瑪雅腳本。我一直在研究一個相對簡單的工具,該工具創建了一個簡單的用戶界面,用戶可以使用它來將一個對象約束到另一個對象,但仍然可以自由移動該對象一旦受到約束。這是使用嵌套定位器完成的。瑪雅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)