【问题标题】:Global variable defining fails全局变量定义失败
【发布时间】:2014-05-12 03:12:13
【问题描述】:

我创建了一个简单的重命名脚本,但我想请教一些建议,以便我可以改进编码以及磨练我的 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')

【问题讨论】:

  • 您好!如果我正确理解您的帖子,这是您开始使用的代码,这很棒。您能否也发布一下您尝试过的导致全局名称错误的方法?另外,您是否考虑过使用类变量?
  • @Nacho 我试图在class mainWindow(QDialog): 行之前添加global objectsobjects = cmds.ls(selection=True),所有其他功能(不包括选择和清除层次结构),我一直得到# RuntimeError: No object matches name ,假设我正在选择 2 个项目(有一个子项的多维数据集 1 - 多维数据集 2)。此外,它只编辑/重命名父级而不是子级......顺便问一下,你如何使用类变量?老实说,我对def/class test(xxx)的理解还是很薄弱的

标签: python global-variables rename maya


【解决方案1】:

好吧,我想我明白你的尝试了,我打算试一试。

首先,看看下面的帖子,应该可以让您快速了解全局变量:

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 mainWindow(QDialog):
    objects = cmds.ls(selection=True)

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

请记住,mainWindow 的所有实例的对象都是相同的,并且对一个实例中的对象的任何更新都会影响所有其他实例。据我所知,这应该没问题,但您绝对应该熟悉实例、类和模块。

希望有帮助!

更新: 哎呀,在一个地方更改了类属性,但在上一个示例中没有更改另一个。更新了示例,现在应该更有意义了。

【讨论】:

  • 请原谅迟到的回复。你的帖子肯定对我有帮助,我现在可以看到更清晰的画面。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多