Toggle / Switch between scenes

Working code 1.10
Simply switch between two scenes with space bar example

#!/usr/bin/env python3

from direct.showbase.ShowBase import ShowBase
from direct.gui.OnscreenText import OnscreenText
from panda3d.core import TextNode

class MyApp(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)

        # Create scene 1
        self.scene1 = self.render.attachNewNode("Scene 1")
        self.scene1_text = OnscreenText(text="Scene 1", pos=(0, 0), scale=0.1, align=TextNode.ACenter)

        # Create scene 2
        self.scene2 = self.render.attachNewNode("Scene 2")
        self.scene2_text = OnscreenText(text="Scene 2", pos=(0, 0), scale=0.1, align=TextNode.ACenter)
        self.scene2_text.hide()

        # Create toggle text
        self.toggle_text = OnscreenText(text="Toggle Scene: [SPACE]", pos=(0, -0.9), scale=0.08, align=TextNode.ACenter)

        # Set initial scene
        self.current_scene = 1

        # Set key bindings
        self.accept('space', self.toggle_scene)
        self.accept('escape', self.quit)

        # Position and orient the camera
        self.camera.setPos(0, -10, 0)  # Set camera position
        self.camera.lookAt(0, 0, 0)  # Point camera towards the origin

    def toggle_scene(self):
        if self.current_scene == 1:
            self.scene1_text.hide()
            self.scene2_text.show()
            self.current_scene = 2
        else:
            self.scene2_text.hide()
            self.scene1_text.show()
            self.current_scene = 1

    def quit(self):
        self.destroy()

app = MyApp()
app.run()

I like it! This could work for complex sub trees, making seen switching a breeze, just add everyting to “scene” nodes and mix in a FSM and you have pretty neat little system. Thanks.

1 Like