全局变量定义失败

时间:2014-04-01 11:19:19

标签: python global-variables rename maya

我创建了一个简单的重命名脚本,但我想请一些建议,以便我可以改进编码以及磨练我的python脚本。以下是现在代码的一小部分......

虽然这在我看来可能不是问题,但除了我在下面说过的两个功能之外,我已经意识到几乎所有的功能,它们都包含objects = cmds.ls(selection=True)虽然我不介意一遍又一遍地重新输入,但我相信有更好的方法可以解决这个问题。

但是,当我尝试在类函数之前将它们设置为全局时,它能够运行直到我厌倦执行其中一个函数时,它会提示错误,指出global name 'objects' is not defined或'对象未定义'等等。

与此有关,是否有任何建议?

class mainWindow(QDialog):
    def __init__(self, parent=None):
        super(mainWindow, self).__init__(parent)
        self.resize(300,225)
        self.initUI()
        self.createConnections()

    def searchReplace(self):
        wordSearch = str(self.searchTxt.text())
        wordReplace = str(self.replaceTxt.text())

        objCnt = cmds.ls(sl=True, sn=True)

        if len(objCnt) == 0:
            self.searchTxt.clear()
            self.replaceTxt.clear()
            cmds.warning('Nothing is selected')
        else:
            for wordString in sorted(objCnt):
                if wordSearch in wordString:
                    newWordString = wordString.replace(wordSearch, wordReplace)
                    cmds.rename(wordString, newWordString)
                    self.searchTxt.clear()
                    self.replaceTxt.clear()
                    print '%s' %wordString + " has changed to : " + "%s" %newWordString

    def addPrefix(self):
        objects = cmds.ls(selection=True)
        pfx = str(self.prefixTxt.text())

        for item in objects:
            if pfx == "":
                cmds.warning('No prefix values in the field')
            else:
                cmds.rename(item, pfx + "_" + item)
                self.prefixTxt.clear()
                print 'Prefix added: %s_' %pfx

    def addSuffix(self):
        objects = cmds.ls(selection=True)
        sfx = str(self.suffixTxt.text())

        for item in objects:
            cmds.rename(item, item + "_" + sfx)
            self.suffixTxt.clear()
            print 'Suffix added: _%s' %sfx

    def numPadding(self):
        objects = pm.ls(selection=True)
        num = self.numTxt.text()
        padding = self.paddingTxt.text()

        if num != "" and padding !="":
            try:
                for currentWordStr in objects:
                pad = ("%%0%ii" % int(padding)) % int(num)
                newWordStr = currentWordStr.rename(currentWordStr.name() + "_" + pad)

            except Exception:
                self.numTxt.clear()
                self.paddingTxt.clear()
                cmds.warning('Input numerical values only')
        else:
            cmds.warning('Entries of Num or Padding are empty')

    def selectHierarchy(self):
        sel = cmds.ls(selection = True)
        selCnt = len(sel)

        if int(selCnt) == 0:
            cmds.warning('Nothing is selected')
        else:
            objHierarchy = cmds.listRelatives(ad=True, type='transform', fullPath=True)          
            cmds.select(sel, objHierarchy)

    def clearHierarchy(self):
        sel = cmds.ls(selection = True)
        selCnt = len(sel)

        if int(selCnt) != 0 :
            objHierarchy = cmds.select(clear=True)
        else:
            cmds.warning('Selection is empty. Nothing to be cleared')

1 个答案:

答案 0 :(得分:3)

好吧,我想我明白你的所作所为,想要回答一下。

首先,看看以下帖子,应该让你快速了解全局:

Using global variables in a function other than the one that created them(很棒,简洁的摘要)

Variable scope outside of classes(类的示例)

因此,首先,在首次声明类定义之外的对象时,您不需要使用global关键字。所以,而不是:

global objects
objects = cmds.ls(selection=True)
class mainWindow(QDialog):
    ...

你会这样做:

objects = cmds.ls(selection=True)
class mainWindow(QDialog):
    ...

然后,您的功能可以引用"对象"。如果需要在类中的函数内写入对象,则需要首先使用global关键字(此代码假定对象是在类之前定义的):

def my_method(self):
    global objects
    objects = some_function()

那就是说,我不能100%确定上面的代码是如何被调用的,所以其他东西可能会导致"对象"未定义。

这里可能更适合使用class属性。你可以这样做:

class mainWindow(QDialog):
    objects = cmds.ls(selection=True)

    def my_func(self):
        for item in self.objects:
            do_stuff()

请记住,对于mainWindow的所有实例,对象都是相同的,并且对一个实例中的对象的任何更新都将影响所有其他实例。从我所知道的情况来看应该没问题,但你应该熟悉实例与类对比模块。

希望有所帮助!

UPDATE:糟糕,在一个地方更改了类属性,但在最后一个示例中没有更改。更新了示例,它现在应该更有意义。