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
This commit is contained in:
rdb 2018-01-03 14:23:13 +01:00
parent 9065f2e16e
commit 4f50f6abd0
2 changed files with 27 additions and 7 deletions

View File

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

View File

@ -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__ = [] __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
# Create the showbase instance config = get_config_showbase()
# This should be created by the game specific "start" file
#ShowBase() vfs = VirtualFileSystem.getGlobalPtr()
# Instead of creating a show base, assert that one has already been created ostream = Notify.out()
assert base globalClock = ClockObject.getGlobalClock()
cpMgr = ConfigPageManager.getGlobalPtr()
cvMgr = ConfigVariableManager.getGlobalPtr()
pandaSystem = PandaSystem.getGlobalPtr()
# Set direct notify categories now that we have config # Set direct notify categories now that we have config
directNotify.setDconfigLevels() directNotify.setDconfigLevels()
@ -31,3 +43,4 @@ builtins.inspect = inspect
if (not __debug__) and __dev__: if (not __debug__) and __dev__:
notify = directNotify.newCategory('ShowBaseGlobal') notify = directNotify.newCategory('ShowBaseGlobal')
notify.error("You must set 'want-dev' to false in non-debug mode.") notify.error("You must set 'want-dev' to false in non-debug mode.")
del notify