Panda3D Manual: DirectStart

Overview of DirectStart

If you are already up to speed with Python, the following code may look like a black box:

import direct.directbase.DirectStart
run()

This section provide some key details on the function of this import statement.

DirectStart creates an instance of the class ShowBase, which itself inherits DirectObject. Under Linux, the relevant file can be found in:

/usr/share/panda3d/direct/showbase/

On Windows, the code is (by default) located in:

C:\Panda3D-1.X.X\direct\src\showbase

An important item created meanwhile is a base task manager as taskMgr. The function run() is in fact a single call to launch this task manager.

Apparent global variables created

Some key variables used in all Panda3D scripts are actually attributes of the ShowBase instance. Here is the relevant code making these attribute global in scope:

__builtin__.base = self
__builtin__.render2d = self.render2d
__builtin__.aspect2d = self.aspect2d
__builtin__.render = self.render
__builtin__.hidden = self.hidden
__builtin__.camera = self.camera
__builtin__.loader = self.loader
__builtin__.taskMgr = self.taskMgr
__builtin__.jobMgr = self.jobMgr
__builtin__.eventMgr = self.eventMgr
__builtin__.messenger = self.messenger
__builtin__.bboard = self.bboard
__builtin__.run = self.run
__builtin__.ostream = Notify.out()
__builtin__.directNotify = directNotify
__builtin__.giveNotify = giveNotify
__builtin__.globalClock = globalClock
__builtin__.vfs = vfs
__builtin__.cpMgr = ConfigPageManager.getGlobalPtr()
__builtin__.cvMgr = ConfigVariableManager.getGlobalPtr()
__builtin__.pandaSystem = PandaSystem.getGlobalPtr()

This is where the commonly used objects base, render, render2d, camera, messenger, and taskMgr are created. There are more such variables added to __builtin__. Consult the code if you want to find them all.

Note that this way of exposing these attributes make them available globally even though these will not be returned by a call to the built-in function dir().

Cleaner Code with ShowBase

DirectStart is sometimes seen as a sloppy concept. Use of it can be avoided entirely by manually instancing ShowBase instead. In many cases, this may actually be the preferred method of initializing the scene.

To do this, simply replace the DirectStart import line with:

from direct.showbase.ShowBase import ShowBase

Then create the instance of ShowBase when desired. The two-line code sample at the top of this page can thus be changed to this:

from direct.showbase.ShowBase import ShowBase
ShowBase()
run()

The obvious benefit of this is the ability to instance the ShowBase whenever it is needed rather than immediately before all other imports. Using DirectStart in this case would call for the import line to be placed further into the code separate from all the other imports or functions being called between import lines, both of which are often seen as poor coding practice.