starting panda without __builtins__

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()

In 1.6, an item in direct.stdpy.threading, pm (PandaModules), is overwritten by pdb’s pm() function. So, I have to exclude that module.

This is mine :

import sys, __builtin__
__bd__ = __builtin__.__dict__
b = __bd__.copy()
bk = b.keys()

import direct.directbase.DirectStart
__all__ = list( set(__bd__.keys()).symmetric_difference(bk) )
excludedMods = ['stdpy.threading']
mods = [m for m in sys.modules.keys() \
  if sys.modules[m] and m.find('direct.')==0 and m[7:] not in excludedMods]
for md in [globals()] + [sys.modules[m].__dict__ for m in mods]:
   for k in __all__:
      md[k]=__bd__[k]
__bd__.clear()
for k in bk:
   __bd__[k]=b[k]

note : there might be typos in it.