簡體   English   中英

將在一個變量中創建的列表傳遞給類中的另一個變量(PYTHON)

[英]Passing a list created in one variable to another variable inside a class (PYTHON)

我不明白為什么在我調用第二個函數后,列表卻沒有返回到該類並留在那里,這是我的代碼:

class ShockAbsorber:
    '''create Proxy, based of 2 locators'''
    def createProxy(self):
        self.allLoc = {}
        self.allLoc["CylinderLoc"] = pm.spaceLocator (n = "Cylinder_Loc")
        self.allLoc["PistonLoc"] = pm.spaceLocator (n = "Piston_Loc", p =[0,30,0])
        pm.CenterPivot()

    '''create bones on locators'''
    def createRig(self):
        for name,loc in self.allLoc.items():
            print Loc

我在一個單獨的文件上有一個界面,該界面為每個功能創建2個按鈕。

    #define button Create Proxy
def CreateProxyPressed(self):
    csa = sa.ShockAbsorber() 
    csa.createProxy()

#define button Create Proxy
def CreateRigPressed(self):
    csa = sa.ShockAbsorber() 
    csa.createRig()

如果運行代碼,我會收到此錯誤消息:

AttributeError: ShockAbsorber instance has no attribute 'allLoc'

我希望這是足夠的信息,可以幫助您理解我的問題,我正在為“ Autodesk Maya”編寫工具。 我很確定自己的想法是正確的,所以我做錯了什么?

先感謝您!

PS:對不起,我很困惑,但是在您發現我的錯字后,我現在將代碼修改為正確的!

您需要將allLoc存儲在類實例中,而不是返回它:

def createProxy(self, *args):
    allLoc = {}
    allLoc["CylinderLoc"] = pm.spaceLocator (n = "Cylinder_Loc")
    allLoc["PistonLoc"] = pm.spaceLocator (n = "Piston_Loc", p =[0,30,0])
    pm.CenterPivot()
    self.allLoc = allLoc

createProxy需要設置self.allLoc 您僅設置本地名稱allLoc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM