Integrating Python-Ogre into a C++ Ogre program?


10-07-2007 03:30:24

Has anyone attempted to integrate Python-Ogre into a C++ Ogre program?

That is, in a C++ ogre application you could load an execute a python script (in the C++ program) and it would be able to interact with the C++ ogre objects.

I've done this before with the Boost::Python and Swig versions of PyOgre...has anyone attempted to do this with Python-Ogre?


10-07-2007 07:19:03

If you mean "embed Python into a C++ Ogre application and import the Python-Ogre module", yes, I did it.


12-07-2007 17:03:44

Ah very cool, do you have the code for this sitting around somewhere I could take a peak at? ;)


13-07-2007 10:32:41

Well, it's pretty simple:

First include the Python header:

#ifdef _DEBUG
#undef _DEBUG
#include <Python.h>
#define _DEBUG
#include <Python.h>

(The mess with the _DEBUG macro allows me to link with the release version of Python while in Debug mode).

Next in my ScriptingManager class I initialize Python and import Python-Ogre:

executeString("import sys, os");
executeString("sys.path.insert(0, os.getcwd() + '\\Modules')");

executeString("import ogre");

(I'm only using the Ogre module of Python-Ogre, which is put into the folder '$(OutputDir)/Modules/ogre').

With the executeString method:

bool ScriptingManager::executeString(const Ogre::String& strCommand)
return (PyRun_SimpleString(strCommand.c_str()) == 0);
catch (Ogre::Exception& ex)
ATHENA_LOG_ERROR("Failed to execute the command '" + strCommand + "', exception: " + ex.getDescription());
catch (...)
ATHENA_LOG_ERROR("Failed to execute the command '" + strCommand + "', unknown exception");

return false;

As long as the same Ogre DLLs are used by your application and Python-Ogre, it should work without any trouble (which means compiling Python-Ogre yourself in Debug mode too).

Note: in fact, this is done in my framework, which is also wrapped using Py++, and so can also be used either from C++ and Python. In that latter case, the second code snippet above isn't executed.


14-07-2007 05:53:28

Ahh fair enough, thanks.


16-07-2007 20:29:58

What is the purpose/gain of embedding Python in C++ if you're gonna use it to access wrapped C++ libraries?

I'm sure there are good reasons, I'm just not sure what they are. :)


18-07-2007 13:00:24

I'm using Python as a scripting language. Giving the scripts an access to Ogre, my framework and the application on top of it, or a "limited version" of them is application-dependent.

An editor can use the full control offered by having full access to Ogre for instance, where a game could only use some high-level specific commands.

The goal isn't to create a C++ string containing a Python-Ogre command each time I want to use an Ogre feature of course ^^

During development, it's sometimes useful to be able to open an in-game console to quickly do some tests.


18-07-2007 13:09:07

I see, yeah I can see how that makes sense. :)


25-07-2007 21:20:58

Hmm, now that I am actually sitting down to do this, I should have been more specific.

The following is something I used to do with the Boost.Python version of PyOgre and the swig version of PyOgre. I'd like to know if something similar will work with Python-Ogre:

First, define a method in a python script which takes in an ogre object:

def foo(camera):
camera.Position = 1, 2, 3 #or whatever here!

What I need to do is to call this function from C++ land. To do this, you need to use the Python-C api to get the function you have created as an object, then use one of the call-a-function Python-C calls. No problem, except for one small detail: how do I covert an Ogre object to a PyObject which the python interpreter will understand?

In both Boost and Swig you would call one of the converter functions which would translate your C++ object into a wrapped PyObject which the Python-C interpreter could understand. How do you do that with Python-Ogre?



25-07-2007 21:24:19

Edit: Digging further into the source, I see the library contains boost... Is this boost-based?


26-07-2007 00:11:13

Yes, Python-Ogre is built using Boost...

You may want to have a browse through the google group history as there was a thread on intergating with C along with a sample set of code.




26-07-2007 18:51:59

Ahh perfect. That solves everything then. =)