Thursday, April 24, 2008

Command-Line Panda3D

Here's a neat tip worth pointing out: because Panda3D works in Python, and Python has a relatively robust command-line mode, you have a powerful realtime debugging tool in the form of command-line Panda. It's not really hard, you just may not realize it's possible. Open up a Python prompt, then run these lines:

from pandac.PandaModules import *
from direct.showbase.DirectObject import DirectObject
import direct.directbase.DirectStart

That should be enough to get a window up and running. As you test more and more things out, remember you'll probably have to call the run() function every now and then to get tasks to run, do some extended interaction or that sort of thing. While Panda3D is actually "running," you won't have the command-line available.

When you want to pause the system and do more debugging, simply raise a KeyboardInterrupt exception, and Panda3D will freeze and return control to the command-line. You may want to write a module that does all of this for you, like, say:

from pandac.PandaModules import *
from direct.showbase.DirectObject import DirectObject
import direct.directbase.DirectStart
class debugObj(DirectObject):
 def __init__(self):
  self.accept("space",self.interruptPanda) #any unused event, really
 def interruptPanda(self):
  raise KeyboardInterrupt()


Woohoo, now you can load modules and run Panda3D stuff to your heart's content, with the ability to freeze execution and evaluate variables at any moment.

3 comments:

A Reasonable Atheist said...

For even more command line python fun, install ipython :)

http://ipython.scipy.org/moin/

Thibaud said...

Nice tip.
Do you know how python actually communicates with scripting widgets? I'm writing a code editor and doing some Panda3D stuff as well so it would be great if I could interface the editor as a python interpreter...

Khakionion said...

Not sure what you mean by "scripting widgets," but most Panda3D objects are written in C, and have their interfaces exposed to Python using Panda3D's interrogate system. If you're wanting to integrate Panda3D into an interactive editor, I highly recommend giving the source a quick skim. It will illuminate a lot about how Panda3D actually works.