Hello,
this is just a little experiment I made the last 15 minutes. I’m afraid there is nothing really useful in here right now, but it is rather nice from the technical side.
from direct.showbase.DirectObject import DirectObject
import sys
class World( DirectObject ):
def __init__( self ):
base.setBackgroundColor( 0.0, 0.0, 0.3, 1.0 )
self.accept( 'escape', self.exit )
self.np = loader.loadModel( 'models/camera' )
self.np.reparentTo( render )
self.np.setPos( 0, 10, 0 )
taskMgr.add( self.update, 'update' )
def exit( self ):
sys.exit( )
def update( self, task ):
self.np.setH( task.time * 90.0 )
return task.cont
import direct.directbase.DirectStart
world = World( )
run( )
Not the most complex Panda3D script I ever wrote, but it has some Panda3D stuff in it (open a window, register a key for closing the window, load a model, and rotate the model using a task). And it is a plain Python file.
Now I add some lines and rename the file to “PandaTest.pyx”:
from direct.showbase.DirectObject import DirectObject
import sys
class World( DirectObject ):
def __init__( self ):
base.setBackgroundColor( 0.0, 0.0, 0.3, 1.0 )
self.accept( 'escape', self.exit )
self.np = loader.loadModel( 'models/camera' )
self.np.reparentTo( render )
self.np.setPos( 0, 10, 0 )
taskMgr.add( self.update, 'update' )
def exit( self ):
sys.exit( )
def update( self, task ):
self.np.setH( task.time * 90.0 )
return task.cont
cdef extern from 'Python.h':
void Py_Initialize( )
void Py_Finalize( )
void PySys_SetArgv( int argc, char **argv )
cdef public void initPandaTest( )
cdef public int main( int argc, char **argv ):
Py_Initialize( )
PySys_SetArgv( argc, argv )
initPandaTest( )
import direct.directbase.DirectStart
world = World( )
run( )
Py_Finalize( )
This is no longer Python code, but Pyrex code. Running Pyrex generates C/C++ code. Compile the cpp file, and you get a binary. “.exe” in my case, since I’m on windows.
I have used Pyrex mainly for extending and speeding up Python so far, but this short code shows it is also easy to embed Python. This script has no benefit from Pyrex (no speed gain), except that your own code is well hidden now.
enn0x