Browse Source

showbase: use ShowBaseGlobal module as alternative to builtin scope

Allows accessing `base` object and several other builtins from the ShowBaseGlobal module.

It doesn't bother with builtins that are available as members of the base object such as `render` and `loader`.

Fixes #182
rdb 8 years ago
parent
commit
4f50f6abd0
2 changed files with 27 additions and 7 deletions
  1. 7 0
      direct/src/showbase/ShowBase.py
  2. 20 7
      direct/src/showbase/ShowBaseGlobal.py

+ 7 - 0
direct/src/showbase/ShowBase.py

@@ -389,6 +389,10 @@ class ShowBase(DirectObject.DirectObject):
             builtins.aspect2dp = self.aspect2dp
             builtins.pixel2dp = self.pixel2dp
 
+        # Now add this instance to the ShowBaseGlobal module scope.
+        from . import ShowBaseGlobal
+        ShowBaseGlobal.base = self
+
         if self.__dev__:
             ShowBase.notify.debug('__dev__ == %s' % self.__dev__)
         else:
@@ -514,6 +518,9 @@ class ShowBase(DirectObject.DirectObject):
             del builtins.base
             del builtins.loader
             del builtins.taskMgr
+            ShowBaseGlobal = sys.modules.get('direct.showbase.ShowBaseGlobal', None)
+            if ShowBaseGlobal:
+                del ShowBaseGlobal.base
 
         # [gjeon] restore sticky key settings
         if self.config.GetBool('disable-sticky-keys', 0):

+ 20 - 7
direct/src/showbase/ShowBaseGlobal.py

@@ -1,14 +1,26 @@
-"""instantiate global ShowBase object"""
+"""This module serves as a container to hold the global ShowBase instance, as
+an alternative to using the builtin scope.
+
+Note that you cannot directly import `base` from this module since ShowBase
+may not have been created yet; instead, ShowBase dynamically adds itself to
+this module's scope when instantiated."""
 
 __all__ = []
 
-from .ShowBase import *
+from .ShowBase import ShowBase, WindowControls
+from direct.directnotify.DirectNotifyGlobal import directNotify, giveNotify
+from panda3d.core import VirtualFileSystem, Notify, ClockObject, PandaSystem
+from panda3d.core import ConfigPageManager, ConfigVariableManager
+from panda3d.direct import get_config_showbase
+
+config = get_config_showbase()
 
-# Create the showbase instance
-# This should be created by the game specific "start" file
-#ShowBase()
-# Instead of creating a show base, assert that one has already been created
-assert base
+vfs = VirtualFileSystem.getGlobalPtr()
+ostream = Notify.out()
+globalClock = ClockObject.getGlobalClock()
+cpMgr = ConfigPageManager.getGlobalPtr()
+cvMgr = ConfigVariableManager.getGlobalPtr()
+pandaSystem = PandaSystem.getGlobalPtr()
 
 # Set direct notify categories now that we have config
 directNotify.setDconfigLevels()
@@ -31,3 +43,4 @@ builtins.inspect = inspect
 if (not __debug__) and __dev__:
     notify = directNotify.newCategory('ShowBaseGlobal')
     notify.error("You must set 'want-dev' to false in non-debug mode.")
+    del notify