Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用变量从一个类转换到另一个类_Python_Variables_Maya - Fatal编程技术网

Python 使用变量从一个类转换到另一个类

Python 使用变量从一个类转换到另一个类,python,variables,maya,Python,Variables,Maya,我试图使用一个类中的变量-CustomNodeTranslator,并将该变量用于另一个类-fileImporter 然而,我被提示出现错误#AttributeError:'CustomNodeTranslator'对象没有属性'camName'#,我在我的另外两个类上使用了类似的方法,除了这两个类之外,它都在工作 可能是因为CustomNodeTranslator是一个特例吗?看到它被用于插件了吗 请给我一些建议 多谢各位 class CustomNodeTranslator(OpenMaya

我试图使用一个类中的变量-
CustomNodeTranslator
,并将该变量用于另一个类-
fileImporter

然而,我被提示出现错误
#AttributeError:'CustomNodeTranslator'对象没有属性'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

您不需要在
\uuuu init\uuuu
方法中创建class属性,因此--

现在还看不见。要么在init中分配它,要么在类接口上放置属性引用

更新:您应该在
CustomNodeTranslator
类中声明它

class CustomNodeTranslator(OpenMayaMPx.MPxFileTranslator):
    def __init__(self):
        self.camName = ""
        ....

字段仅在创建后才存在

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

test.camName
只在
test.reader(…)
被调用后才被赋值,因为它不是在
\uuuuuu init\uuuuu
@jornsharpe中赋值的,所以我认为,除非我在
\uuu init\uuuuu
中赋值,否则不可能调用任何其他函数。不清楚你在问什么。任何需要在
reader
中首次分配的两个属性(
fileHandle
camName
)的情况下,如果尚未调用该方法,则将失败。@jonrsharpe我想我理解你的意思。我确实设法让它工作,但过了一段时间,它再次失败,因为一些Maya干预,它喜欢在后面添加一个数字后缀。即便如此,我的代码还是得到了多米诺效应(尽管如此,我还是找到了一个解决办法。您好,请原谅我的不知道,但您真正的意思是什么?
camName
CustomNodeTranslator
中第一次声明时就变为“可用”。因此在实例化(
CustomNodeTranslator()
)还没有。只有在您第一次调用
reader
时,它才可用。我的建议是在
\uuuu init\uuu
方法中声明它,就像
self.camName=“
。所以我尝试声明
self.camName=“”
在任何一个
\uuuu init\uuuuuuu
中,我仍然得到这个错误:
\AttributeError:type object'CustomNodeTranslator'没有我在更新答案时指定的属性'camName'
@disdia?因为在这种情况下,至少根据语言规范,它应该可以工作,我尝试过它,但它不工作g也是。我仍然遇到同样的错误。我插入了一些打印语句,出于某些原因,
self.camName
返回的是空白,而不是任何字符串,尽管它能够捕获
try
语句中的信息。它似乎给了我更多的问题,例如
名称错误:全局名称“fileObject”I没有定义35;
但是谢谢anyway@dissidia:是-您必须在之前的某个地方定义它们。
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