Problem animating an Actor that is part of a class I made.

I’m experimenting a little bit with panda3d and I’m trying to let a class load some models during it’s initialization. And then I could access these models using the object.

This is an example of one of these classes.

from panda3d.core import NodePath
from panda3d.core import PandaNode
from direct.actor.Actor import Actor
class HWscene_Class:
    def __init__(self, name="HWscene"):
        self.root = NodePath(PandaNode(name))        
        
        #self = self.attachNewNode(name)
        self.environ = loader.loadModel("models/environment")
        self.environ.setScale(0.25, 0.25, 0.25)
        self.environ.setPos(-8, 42, 0)
        self.environ.reparentTo(self.root)
        
        self.pandaActor = Actor("models/panda-model",{"walk": "models/panda-walk4"})
        self.pandaActor.setScale(0.005, 0.005, 0.005)
        self.pandaActor.reparentTo(self.root)

The code in the main file is this

HWscene = HWscene_Class()
HWscene.root.reparentTo(self.render)
HWscene.pandaActor.loop('walk')

This opens the the hello world scene. And for wath I think it should also animate the panda. But It doesn’t. It only stands still.

Did I mis anithing. I am very new.

This is probably because you are not keeping a reference to the class and it is destructing (and thus the Actor destructing). This is mentioned briefly in the manual: http://www.panda3d.org/manual/index.php/Loading_Actors_and_Animations
Something like this might work:

class Game(DirectObject):
    def __init__(self):
        self.HWscene = HWscene_Class()
        self.HWscene.root.reparentTo(render)
        self.HWscene.pandaActor.loop('walk')

Game()
run()

Thanks. It solved my problem. I didn’t know I had to put the self. in front of the variable. In my last project I didn’t use a OOP language. I need to remember some things again.