Python类和公共成员

2024-04-24 06:02:05 发布

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

你好,我习惯于创建一个有多个函数的类每个函数我需要创建自己的公共成员,所以我这样做了,但它给了我一个错误

import maya.cmds as cmds

class creatingShadingNode():

    def _FileTexture( self, name = 'new' , path = '' , place2dT = None ):

        # craeting file texture

        mapping = [

              ['coverage', 'coverage'],
              ['translateFrame', 'translateFrame'],
              ['rotateFrame', 'rotateFrame'],
              ['mirrorU', 'mirrorU'],
              ['mirrorV', 'mirrorV']

              ]

        file = cmds.shadingNode ( 'file' , asTexture = 1 , isColorManaged = 1 , n = name + '_file' )

        if not place2dT:
            place2dT = cmds.shadingNode ( 'place2dTexture' , asUtility = 1 , n = name + '_p2d' )

        for con in mapping:

            cmds.connectAttr( place2dT + '.' + con[0] , file + '.' + con[1] , f = 1 )

        if path:
            cmds.setAttr( file + '.fileTextureName' , path, type = 'string' )

        self.File = file
        self.P2d = place2dT

test  = creatingShadingNode()._FileTexture(name = 'test' , path = 'test\test' )
print test.File

我得到第1行:“NoneType”对象没有属性“File”


Tags: path函数nametestselfcoverageconmapping
1条回答
网友
1楼 · 发布于 2024-04-24 06:02:05

两件事:

首先,您没有从_FileTexture()返回任何内容,而是创建了一个实例并调用了它的方法而没有返回。如果要设置所需的实例成员

instance = creatingShadingNode()
instance._FileTexture(name = 'test' , path = 'test\test' )
print instance.File

第二,您没有以常见的Python方式创建类。大多数人会这样做:

class ShadingNodeCreator(object):
      def __init__(self):
          self.file = None
          self.p2d = None

      def create_file(name, path, p2d):
          # your code here

大多数差异是表面的,但是如果使用Python约定,您将有一个更轻松的时间。从object继承给您一个bunch of useful abilities,最好在__init__中声明您的实例变量,如果没有其他东西可以让您清楚地知道类可能包含什么。你知道吗

相关问题 更多 >