Using Python-Ogre to embed in C++

David

15-06-2007 01:07:50

I am very, very new to Python and Python-Ogre. What I would like to do is to embed the Python interpreter in a C++ program. I import the Python-Ogre wrapper. What I want to do is pass a reference to the Ogre::Root object that is created in C++ to Python-Ogre and allow the Python-Ogre wrapper access to the same Ogre::Root object that C++ is using. Here is a code snippet of what I thought would work:

Ogre::Root& GetOgreRoot(void)
{
return Ogre::Root::getSingleton();
}

//This part is inside a Boost::Python module
class_<Ogre::Root>("Root")
;

def("GetOgreRoot", GetOgreRoot, return_value_policy<reference_existing_object>());

In Python, I can do the following:

theRoot = mymodule.GetOgreRoot()

which works fine. But when I try to do something like:

scenemgr = theRoot.getSceneManager("...")

I get an error that says object Root does not have an attribute getSceneManager.

Is there any way I can pass a reference back to Python and allow Python-Ogre to 'connect' to the same root I'm using in C++.

Thanks in advance for any help!

David

Game_Ender

15-06-2007 02:22:24

If all you want to do is share root there is nothing you have to do. Root is a singleton object, there is only one for the entire process. If your C++ code creates the Ogre::Root object your python code can access it through "ogre.renderer.OGRE.Root.getSingleton()", just like normal.

Oh, and please use: Code Tags: [code][/code]

David

15-06-2007 02:35:07

Thanks for the quick answer. That's what I tried the first time:

theRoot = ogre.renderer.OGRE.Root.getSingleton()
sceneMgr = theRoot.getSceneManager("RF2")

and then I get this error from Python:

Traceback (most recent call last):
File "c:/projects/rf2ogre/debug/scripts/camera.py", line 39, in <module>
main()
File "c:/projects/rf2ogre/debug/scripts/camera.py", line 9, in main
sceneMgr = theRoot.getSceneManager("RF2")
AttributeError: 'NoneType' object has no attribute 'getSceneManager'

It's like theRoot does not actually get set to reference the Ogre::Root object. Do you see anything wrong with it?

Thanks,
David

andy

15-06-2007 05:09:02

If you do (from python)
print dir(theRoot)
print theRoot

What do you get?

Cheers
Andy

Game_Ender

15-06-2007 05:18:22

Well the NoneType error is because the getSingleton() call is returning None. That is usually because the object was not created. You have called "new Ogre::Root()" before making that Python-Ogre call?

David

15-06-2007 12:28:20

Andy,

Here is the output I get from the print commands you requested I do:


print dir(theRoot)

['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__']

print theRoot

None



Game_Ender,

Yes, I have created the Ogre::Root object in C++ before this call is made.

Thanks guys, any other ideas?

David

andy

16-06-2007 03:58:29

If you don't mind lets move this to the google mailing list..

If you could post the issue to python-ogre-developers@googlegroups.com and attach your source files then I think we will be in a better position to help

Cheers

Andy