将类中的变量用于另一个类

时间:2014-09-26 10:15:22

标签: python variables maya

我正在尝试使用类中的变量 - CustomNodeTranslator并将该变量用于另一个类 - fileImporter

但是我被提示错误# AttributeError: 'CustomNodeTranslator' object has no attribute 'camName' #我在另外两个类中使用了类似的方法,除了这两个类之外它还在工作。

可能是因为CustomNodeTranslator是一个特例吗?看到它用于插件?

请给我建议。

谢谢。

class CustomNodeTranslator(OpenMayaMPx.MPxFileTranslator):
    def __init__(self):
        OpenMayaMPx.MPxFileTranslator.__init__(self)
    ...
    ...

    def reader(self, fileObject, optionString, accessMode):
        try:           
            fullPath = fileObject.fullName()

            self.fileHandle = open(fullPath,"r")
            camHandle = self.fileHandle

            camBaseName = os.path.basename(camHandle.name)
            camName = os.path.splitext(camBaseName)[0]
            self.camName = camName

class fileImporter():
    def __init__(self, order):

        test = CustomNodeTranslator()

        cameraName, cameraShape =  cmds.camera(n=str(test.camName))
        camSel.extend((cameraName, cameraShape))

        cmds.scale(0.5, 0.5, 0.5)


        camBaseName = os.path.basename(camHandle.name)
        camName = os.path.splitext(camBaseName)[0]
        self.camName = camName

2 个答案:

答案 0 :(得分:1)

您不会在__init__方法中创建class属性,当然 - 此处 -

test = CustomNodeTranslator()
# -- here --
cameraName, cameraShape =  cmds.camera(n=str(test.camName))

目前还不可见。在init中分配它或在类接口上放置属性引用。 More in the doc.

更新:您应该在CustomNodeTranslator

中声明它
class CustomNodeTranslator(OpenMayaMPx.MPxFileTranslator):
    def __init__(self):
        self.camName = ""
        ....

答案 1 :(得分:0)

字段仅在您创建后才存在。

test = CustomNodeTranslator()
# at this point test only has fields that are either introduced in 
# the __init__
# or in the class body (like methods)
# calling reader with proper arguments will create the field

test.reader(fileObject, optionString, accessMode)
print test.camName