如何修复Python for maya脚本中简单类中未定义的方法?
为了更好地理解类是如何调用和工作的,我一直在尝试使用一些函数,这些函数在我编写的一个简单脚本中为我工作,该脚本在maya的3D空间中将一个对象捕捉到另一个对象 当我将它们放在一个类中并尝试运行代码时,得到的错误消息是: 错误:名称错误:文件第10行:未定义全局名称“runSelected”# 我认为这可能是因为我调用的方法前面没有self.。虽然我尝试过这样做,但仍然得到一个错误,即: 错误:名称错误:文件第35行:全局名称“self”为 未定义# 在maya中选择三维空间中的两个对象后运行脚本,并通过运行以下命令启动脚本:如何修复Python for maya脚本中简单类中未定义的方法?,python,maya,Python,Maya,为了更好地理解类是如何调用和工作的,我一直在尝试使用一些函数,这些函数在我编写的一个简单脚本中为我工作,该脚本在maya的3D空间中将一个对象捕捉到另一个对象 当我将它们放在一个类中并尝试运行代码时,得到的错误消息是: 错误:名称错误:文件第10行:未定义全局名称“runSelected”# 我认为这可能是因为我调用的方法前面没有self.。虽然我尝试过这样做,但仍然得到一个错误,即: 错误:名称错误:文件第35行:全局名称“self”为 未定义# 在maya中选择三维空间中的两个对象后运行脚本
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()
定义实例方法时,需要显式地将
self
作为方法的第一个参数传递。例如def runSelected(对象):
应更改为
def runSelected(self,object):
,只有这样才能在方法体中访问self
。您应该阅读pythonself
和实例方法以获得一些直觉。在定义实例方法时,您需要显式地将self
作为方法的第一个参数传递。例如def runSelected(对象):
应更改为
def runSelected(self,object):
,只有这样才能在方法体中访问self
。您应该阅读pythonself
和实例方法以获得一些直觉。每个使用self的类方法都必须在参数列表中包含self。其他方法,如createWindow、RunConstraintDelete和selectionCheck应该是静态方法(或在类外部定义)。每个使用self的类方法都必须在参数列表中包含self。其他,如createWindow、RunConstraintDelete和selectionCheck应该是静态方法(或在类外部定义)。创建类时,必须将self
作为其中每个函数的第一个参数(除非您尝试使用类或静态方法)。关于如何在类中使用self
您还忘记在\uuuu init\uuuu
中的self.runSelected
上传递参数
这似乎如预期的那样起作用:
#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
对齐对象。尽量避免创建要对齐的父约束,因为这样会降低性能,然后您必须担心清理场景。创建类时,必须将self
作为其中每个函数的第一个参数(除非您尝试使用类或静态方法)。关于如何在类中使用self
您还忘记在\uuuu init\uuuu
中的self.runSelected
上传递参数
这似乎如预期的那样起作用:
#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
对齐对象。尽量避免创建用于对齐的父约束,因为这样会降低性能,然后您必须担心清理场景。啊,我明白了,我没有意识到我需要将self作为类中所有方法的参数传递,但这是有意义的,脚本现在正在为我工作。将按照您的建议研究self和instance方法。谢谢你在这方面的帮助。啊,我明白了,我没有意识到我需要将self作为类中所有方法的参数传递,但这是有意义的,脚本现在正在为我工作。将按照您的建议研究self和instance方法。谢谢你的帮助。谢谢你的帮助!谢谢你的帮助!