2017-07-22 131 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