This include file will start panda while not (permanently) populating the global builtins namespace. “base”, “loader”, “render” no longer are put in builtins. The first time it is imported it will start panda, but importing it multiple times simply gives you access to the direct globals.
I have only tested it on 1.5.4 but there is no reason for it not to work on 1.6.0.
It is completely backwards compatible; no changes to Showbase.py or anything in Panda itself is required; however I am not sure of the long term desirability–it is most definately a hack. Still, it gives a method of not polluting the namespace until something more involved come around.
edit: fixed the stdy.threading problem.
it seems that any time a module would have overwritten the panda globals it is messed up now as before the module globals were searched before the builtins but now they are the same and conflict. There should not be very many of these, but simply add any to the _excludedModules
import __builtin__,sys
_names = dir(__builtin__)
import direct.directbase.DirectStart
_moduleType = type(sys)
_excludedMods = ['direct.stdpy.threading']
_modules = []
for _n,_m in sys.modules.iteritems():
if _n[:_n.find('.')]=='direct' and _n not in _excludedMods:
if isinstance(_m,_moduleType): #grab all of the modules inside the direct tree--
_modules.append(_m) #they rely on some the panda globals actually being global
_modules.append(sys.modules[__name__]) #include self
_allNames = []
for _n in dir(__builtin__):
if _n not in _names: #it was added after we imported direct.directbase.DirectStart
_allNames.append(_n)
_attr = getattr(__builtin__,_n)
for _m in _modules: setattr(_m,_n,_attr) #add it to the direct modules and this module
delattr(__builtin__,_n) #take it out of the master globals list
__all__=_allNames #don't allow access to all the variables required to make this work, only the panda globals.
usage: (assuming the above is StartPanda.py)
#same result as before:
from StartPanda import *
n = loader.loadModel('panda')
n.reparentTo(render)
n.setPos(0,50,0)
base.setFrameRateMeter(True)
run()
#one way of importing
from StartPanda import base,render,loader,run
n = loader.loadModel('panda')
n.reparentTo(render)
n.setPos(0,50,0)
base.setFrameRateMeter(True)
#taskMgr.accept(..) #won't work, have to import it
run()
#another way of importing
import StartPanda as SP
n = SP.loader.loadModel('panda')
n.reparentTo(SP.render)
n.setPos(0,50,0)
SP.base.setFrameRateMeter(True)
SP.run()