如何修复Python for maya脚本中简单类中未定义的方法?

2024-10-02 00:33:51 发布

您现在位置:Python中文网/ 问答频道 /正文

为了更好地理解类是如何调用和工作的,我一直在尝试使用一些函数,这些函数在我编写的一个简单脚本中为我工作,该脚本在maya的3D空间中将一个对象捕捉到另一个对象。你知道吗

当我将它们放入一个类中并尝试运行代码时,得到的错误消息是:

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

我认为这可能是因为我调用的方法前面没有self.。我试过这样做,但仍然得到一个错误是:

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

在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()

Tags: theto对象self脚本trueifobject
3条回答

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

您还忘了在__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对齐对象。尽量避免创建要对齐的父约束,因为这样会降低性能,然后需要担心清理场景。你知道吗

定义实例方法时,需要显式地传递self作为方法的第一个参数。例如def runSelected(object): 应该更改为def runSelected(self, object):,只有这样才能访问方法体中的self。您应该阅读pythonself和实例方法以获得一些直觉。你知道吗

每个使用self的类方法在参数列表中必须有self。其他方法,如createWindow、RunConstraintDelete和selectionCheck应该是静态方法(或在类外部定义)。你知道吗

相关问题 更多 >

    热门问题