为了更好地了解类的调用方式和工作方式,我一直在尝试编写一些简单的脚本来为我工作的一些功能,这些脚本可以在Maya的3D空间中将一个对象捕捉到另一个对象。
当我将它们放在类中并尝试运行代码时,收到的错误消息是:
错误:NameError:文件第10行:全局名称'runSelected'未定义#
我认为这可能是因为我正在调用前面没有self.
的方法。我尝试这样做,但仍然收到错误:
错误:NameError:文件行35:全局名称“ self”为 未定义#
在maya的3D空间中选择两个对象后运行脚本,并通过运行开始:
Align()
该类的代码如下:
#Class for snapping one object to another in Maya.
import maya.cmds as mc
class Align(object):
def __init__(self):
#starts the runSelected Method
self.runSelected()
def selectionCheck(mySel):
#checks that 2 ojects are created, returns True if so, Flase if not.
if len(mySel) == 2:
print "Great! Two selected"
return True
elif len(mySel) == 0:
print "Nothing Selected to constrain!"
return False
def createWindow():
#This creates a simple dialogue window that gives a message.
mc.confirmDialog(title='Align Objects', m ="Instructions: You need to select two objects to constrain.")
def runConstrainDelete(mySel):
#Creates a parent constraint, does not maintain offset and then deletes the constraint when object is moved.Clears selection.
myParentConstraint = mc.parentConstraint(mySel[0], mySel[1], mo=False)
mc.delete(myParentConstraint)
mc.select (clear=True)
def runSelected(object):
#Creates a list of objects selected. Runs selection check
mySel = mc.ls(sl =True)
result_Sel_Check = self.selectionCheck(mySel)
#if statement handles if a warning window or the rest of the script should be run.
if result_Sel_Check == False:
self.createWindow()
else:
self.runConstrainDelete(mySel)
test_Align = Align()
答案 0 :(得分:1)
定义实例方法时,您需要显式传递self
作为方法的第一个参数。例如def runSelected(object):
应该更改为def runSelected(self, object):
,只有这样才能在方法主体中访问self
。您应该阅读python self
和实例方法以获得一些直觉。
答案 1 :(得分:1)
在创建类时,必须将self
作为其中每个函数的第一个参数(除非您尝试使用类或静态方法)。 This has a pretty good explanation关于如何在类中使用self
。
您还忘记了在self.runSelected
中的__init__
上传递参数!
这似乎按预期工作:
#Class for snapping one object to another in Maya.
import maya.cmds as mc
class Align(object):
def __init__(self):
#starts the runSelected Method
self.runSelected(cmds.ls(sl=True)) # Forgot to pass a parameter here.
def selectionCheck(self, mySel):
#checks that 2 ojects are created, returns True if so, Flase if not.
if len(mySel) == 2:
print "Great! Two selected"
return True
elif len(mySel) == 0:
print "Nothing Selected to constrain!"
return False
def createWindow(self):
#This creates a simple dialogue window that gives a message.
mc.confirmDialog(title='Align Objects', m ="Instructions: You need to select two objects to constrain.")
def runConstrainDelete(self, mySel):
#Creates a parent constraint, does not maintain offset and then deletes the constraint when object is moved.Clears selection.
myParentConstraint = mc.parentConstraint(mySel[0], mySel[1], mo=False)
mc.delete(myParentConstraint)
mc.select (clear=True)
def runSelected(self, object):
#Creates a list of objects selected. Runs selection check
mySel = mc.ls(sl =True)
result_Sel_Check = self.selectionCheck(mySel)
#if statement handles if a warning window or the rest of the script should be run.
if result_Sel_Check == False:
self.createWindow()
else:
self.runConstrainDelete(mySel)
test_Align = Align()
如果使用的是Maya 2016及更高版本,则可以使用cmds.matchTransform
来对齐对象。它还将考虑偏移枢轴。否则,您可以使用cmds.xform
来对齐对象。尝试避免创建父约束以使其对齐,因为这会降低性能,然后您不得不担心清理场景。
答案 2 :(得分:0)
每个使用self的类方法必须在参数列表中具有 self 。其他诸如createWindow,runConstrainDelete和selectionCheck之类的对象应为静态方法(或在类外部定义)。