Trouble with classes within classes - Don't Animate!!!

Hi!

Well… i’m beginner, this is my “hello world” code:

import direct.directbase.DirectStart
from direct.actor.Actor import Actor
from direct.showbase.DirectObject import DirectObject

class main(DirectObject):
            
    def __init__(self):
            
        self.character = Actor("Models/CharStatic",{"run":"Models/CharMove"})
        self.character.reparentTo(render)
        self.character.loop("run")
        
    
launch = main()

run() 

Works fine! But, when i wank to “organize” the scenes putting classes within classes, in follow way:

import direct.directbase.DirectStart
from direct.actor.Actor import Actor
from direct.showbase.DirectObject import DirectObject

class main(DirectObject):
            
    def __init__(self):
        
        class room1(DirectObject):
            def __init__(self):
                self.character = Actor("Models/CharStatic",{"run":"Models/CharMove"})
                self.character.reparentTo(render)
                self.character.loop("run")
        
        t1 = room1()

    
launch = main()

run() 

The model appears, but do not run the animation.

¿What’s the problem?

Thanks!!!

Just from looking at the code (didn’t check): your variable “t1” is assigned within the init method of class “main”, but it is not stored anywhere. This means that it is released again when main.init finishes. This means that your instance of Actor is released too. The model itself is different: by reparenting it to another node a reference of the model is stored, thus it won’t be released even if the Python object is released.

Thank you very much, enn0x.

Well… understood almost all theory part, but i don’t know how to fix it in code (sorry, i’m also beginner for Python).

Can you show me an example?

Well, keep the variable “t1” around somewhere, for example as a member of class “main”. So change

t1 = room1()

to

self.t1 = room1()

Is there a reason why you want to declare class “room1” within the METHOD “main.init”? You could also declare it within the CLASS “main” or just alongside of class “main”.