简体   繁体   中英

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

I don´t understand why my list dosn´t get returned to the class and stay there after i call my second function, here is my code:

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

I have an interface on a separete file that create 2 buttons one for each function.

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

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

If I run my code I recive this error message:

AttributeError: ShockAbsorber instance has no attribute 'allLoc'

I hope this is enought information for you to understand my problem, I´m besically writing a tool For "Autodesk Maya". I´m pretty sure my concept is correct, so what I´m I doing wrong?

Thank you in advance!

PS I´m sorry guys for the confusion but I now edited my code to be correct after you spotted my typo!

You need to store allLoc in the class instance, instead of returning it:

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 needs to set self.allLoc . You are setting the local name allLoc only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM