2017-07-22 127 views
0

你好我不会创建具有多个功能的类中的每个功能,我需要,所以我这样做是为了创建自己的公众成员,但它给了我一个错误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”对象有没有属性“文件”

+0

你的问题是什么?你认为'createShadingNode()._ FileTexture(name ='test',path ='test \ test')'返回什么? – Goyo

回答

2

两件事情:

首先,你不是从_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__中声明您的实例变量是一个好主意 - 如果没有其他说明,就会明白该类可能包含的内容。

+0

ok grate 所以如果我不想创建越来越多的函数,每个函数都必须返回它自己的成员,我必须在init函数中声明它们全部 然后返回每个函数返回的每个变量我不会在每个函数返回 这 'self.File = file self.P2d = place2dT return self.File,self.P2d' –

+0

函数可以返回任何你想要的。将实例成员(self.whatever)用于要在函数之间共享的事物 – theodox