(Ogre) Embedding Python in C++ Example

I decided that I want to find a good way to put Python in my C++ Ogre applications. Now, one method of doing this would be to write an interface for import game_namemy application at a high level. Lets say that Python is going to handle scripting in an RPG-type game. I would need it to do things like “move character to this position, do some spell particle effects, say this dialog”. To accomplish this, I’d manually create a Python module which, when used, would look like this:

def onSomeEvent( ):
  # setup some map locations
  loc1 = game_name.Vector2( x1, y1 )
  loc2 = game_name.Vector2( x2, y2 )

  # get the character
  character = game_name.getCharacter( "Billy Bad Ass" )

  # perform action with him
  character.moveTo( loc1 )
  character.cast( "Ice3", loc2 )
  character.dialog( "Dialog_47" )

When the character’s moveTo, cast, dialog, etc functions are called these events are added to a queue in C++ land, and they are performed in sequence. That is, each of the Python functions return instantly (they don’t wait on a result), and everything is kept in a nice clean interface.

I could do this with the Python/C API fairly simply. It would just take alot of work to define a clean interface to the C++ code and properly wrap every script method into a sequence of C++ calls to perform the action.

While I would eventually love to write a solid tutorial on how to accomplish this for a game, this is not my current goal. My current goal is to do something like this:

import ogre
import ogrecontroller as oc

sceneManager = oc.getSceneManager( )
fogColour = ogre.ColourValue( 0.8, 0.8, 0.8 )
sceneManager.setFog( ogre.FOG_LINEAR, fogColour, .001, 500, 1000);

someNode = oc.getSceneNode( "SomeSceneNode" )
someNode.translate( 1.0, -2.4, 1.5 )

Basically I want to be able to manipulate most parts of Ogre in a C++ program from embedded Python scripts. If I can get this working it will allow me to have a “Python Console” in any C++ Ogre program. You could pull up the console and change properties of anything on the fly without changing C++ code, or even without stopping the program.

There are many hurdles to overcome when doing this, and I’m not even sure that it’s as simple as I think it is (that is, just converting Ogre objects directly into PyOgre objects using Boost.Python). I hope to have something tangible by the end of the month, or at least know that what I’m trying to do is possible. I could always create a raw interface that allowed changing set things in Ogre land, but that’s not as powerful as what I have in mind.

Read More Post