如何在 Maya 脚本的 Python 中的简单 class 中修复未定义的方法?

How to fix an undefined method in a simple class in Python for maya script?

为了更好地理解 classes 是如何调用和工作的,我一直在尝试在我编写的捕捉一个对象的简单脚本中使用一些对我有用的函数在玛雅的 3D space 中到另一个。

当我将它们放入 class 并尝试 运行 代码时,我收到的错误消息是:

Error: NameError: file line 10: global name 'runSelected' is not defined #

我认为这可能是因为我调用的方法前面没有 self.。我尝试这样做,但仍然收到错误消息:

Error: NameError: file line 35: global name 'self' is not defined #

脚本是 运行 在 maya 中选择两个 3D 对象 space 并由 运行ning 启动:

Align()

class 的代码如下:

#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(object): 应更改为 def runSelected(self, object):,然后才能在方法体中访问 self。您应该阅读 python self 和实例方法以获得一些直觉。

每个使用 self 的 class 方法必须在参数列表中有 self。其他方法,如 createWindow、runConstrainDelete 和 selectionCheck 应该是静态方法(或在 class 之外定义)。

创建 class 时,您必须包含 self 作为其中每个函数的第一个参数(除非您尝试使用 class 或静态方法)。 This has a pretty good explanation 关于如何将 self 与 class 一起使用。

您还忘记了在 __init__ 中传递 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 来对齐对象。尽量避免创建父约束来对齐,因为它会降低性能,然后你不得不担心清理场景。