Pygame and Pyogre

deficite

19-03-2006 22:21:40

NOTE: This is just an archive left over for questions, etc. Look at the wiki for the actual code and explanation.
-------------------------------------------------------------------------------------------------------------------------------------------
Well, I've been working on getting pyogre and pygame to work together and after reading the wxPython+PyOgre post I finally found how to do it through the renderParameters thing:
import pygame
from pyogre import ogre

pygame.init()
screen = pygame.display.set_mode((640,480))

root = ogre.Root("plugins.cfg")
root.showConfigDialog()

root.initialise(False)

renderparams = ogre._StringStringMap()
renderparams['externalWindowHandle'] = str(pygame.display.get_wm_info()['window'])
renderw = root.createRenderWindow('PyOgre through PyGame demo', 640, 480, False, renderparams)
renderw.active = True

sceneman = root.getSceneManager(ogre.ST_GENERIC)
camera = sceneman.createCamera("PlayerCam")
camera.position = (0,0,500)
camera.lookAt((0,0,-300))
camera.nearClipDistance = 5
vp = renderw.addViewport(camera)
vp.backgroundColor = (0,0,0)
camera.aspectRatio = vp.actualWidth / vp.actualHeight

pygame.quit()

Sorry about it being messy and uncommented, I WILL fix that soon. This was about as hacky as it gets. I will keep you guys updated as I continue my efforts.

edit: Of course, that's just getting the two to run side-by-side, I need to work on it more. I haven't even tested drawing yet

deficite

20-03-2006 00:46:16

It works! I got a robot to render and everything. Now I need to test input and sound, etc.

It's a rough test, and you'll only see it for a split second. If your computer is too fast to see the robot, take out the pygame.quit() at the end and press CTRL-D in the python window (or CTRL-Z and then ENTER for windows python shell users)

import pygame
from pygame.locals import *
from pyogre import ogre

pygame.init()
screen = pygame.display.set_mode((640,480))

root = ogre.Root("plugins.cfg")
root.showConfigDialog()

root.initialise(False)

renderparams = ogre._StringStringMap()
renderparams['externalWindowHandle'] = str(pygame.display.get_wm_info()['window'])
renderw = root.createRenderWindow('PyOgre through PyGame demo', 640, 480, False, renderparams)
renderw.active = True

sceneman = root.getSceneManager(ogre.ST_GENERIC)
camera = sceneman.createCamera("PlayerCam")
camera.position = (0,0,500)
camera.lookAt((0,0,-300))
camera.nearClipDistance = 5
vp = renderw.addViewport(camera)
vp.backgroundColor = (0,0,0)
camera.aspectRatio = vp.actualWidth / vp.actualHeight
sceneman.ambientLight = (1,1,1)

rc = ogre.ConfigFile()
rc.loadFromFile("resources.cfg")
for section,key,path in rc.values:
ogre.ResourceGroupManager.getSingleton().addResourceLocation(path,key,section)
ogre.ResourceGroupManager.getSingleton().initialiseAllResourceGroups()

ent1 = sceneman.createEntity("Robot", "robot.mesh")
node1 = sceneman.rootSceneNode.createChildSceneNode("RobotNode")
node1.attachObject(ent1)

root.renderOneFrame()

pygame.quit()


Oh, and I do code better than that normally, it's just a really rough hack :)

pkdawson

20-03-2006 20:16:15

Nice work. Input works too, if you add a loop like:
while 1:
for event in pygame.event.get():
print event
root.renderOneFrame()


You should clean it up a little and add it to the Wiki.

deficite

21-03-2006 01:41:00

Okay, I'm writing the wiki page right now. I just wrote the application class I'm going to use in it:
# Written by: Josh Taylor (deficite) <joshtaylor.mail@gmail.com>
import pygame
from pygame.locals import *
from pyogre import ogre

class PyGameOGREApp:
"Provides a base for an application using PyGame and PyOgre"
def __init__(self, width=640, height=480, fullscreen=False):
self._initPyGame()
self._initPyOgre()
self._createWindow(width, height, fullscreen)
self._createViewport()
self._loadResources("resources.cfg")
self._createEntities()
def _initPyGame(self):
"Starts up PyGame"
pygame.init()
def _initPyOgre(self):
"Instantiates the PyOgre root and sceneManager objects and initialises"
self.root = ogre.Root("plugins.cfg")
self.root.showConfigDialog()
self.root.initialise(False)
self.sceneManager = self.root.getSceneManager(ogre.ST_GENERIC)
def _createWindow(self, width, height, fullscreen):
"Creates a PyGame window and sets PyOgre to render onto it"
self.screen = pygame.display.set_mode((width,height))
renderParameters = ogre._StringStringMap()
renderParameters['externalWindowHandle'] = str(pygame.display.get_wm_info()['window'])
self.renderWindow = self.root.createRenderWindow('PyOgre through PyGame', width, height, \
fullscreen, renderParameters)
self.renderWindow.active = True
def _createViewport(self):
"Creates the user's viewport and camera"
self.camera = self.sceneManager.createCamera("camera")
self.camera.position = (0, 0, 500)
self.camera.lookAt((0, 0, -300))
self.camera.nearClipDistance = 5
self.viewport = self.renderWindow.addViewport(self.camera)
self.viewport.backgroundColour = (0, 0, 0)
def _loadResources(self, rcfile):
"Loads the resource paths from specified resource config file"
rc = ogre.ConfigFile()
rc.loadFromFile(rcfile)
for section, key, path in rc.values:
ogre.ResourceGroupManager.getSingleton().addResourceLocation(path, key, section)
ogre.ResourceGroupManager.getSingleton().initialiseAllResourceGroups()
def _createEntities(self):
"For simple demo purposes this can be used to create entities and attach them to sceneNode's"
self.entities = [self.sceneManager.createEntity("robot", "robot.mesh")]
self.sceneNodes = [self.sceneManager.rootSceneNode.createChildSceneNode("robotNode")]
for i in range(len(self.sceneNodes)):
self.sceneNodes[i].attachObject(self.entities[i])
def _createScene(self):
"Prepare the scene. All logic should go here"
pass
def _presentScene(self):
"Render the scene and anything else needed done at the time of presentation"
self.root.renderOneFrame()
def run(self):
"Brings the application to life"
while self._processEvents():
self._createScene()
self._presentScene()
pygame.quit()
def _processEvents(self):
"Process events and take appropriate action"
for event in pygame.event.get():
if event.type is QUIT:
return False
elif event.type is KEYDOWN and event.key is K_ESCAPE:
return False
return True

# Instantiate and run!
app = PyGameOGREApp()
app.run()


Very minimalistic but customizable (which is how I like code examples to be)

deficite

21-03-2006 02:21:21

Okay, it's now on the wiki under the tutorial & examples section on the PyOgre page. http://www.ogre3d.org/wiki/index.php/Us ... ith_PyOgre

DeGauss

23-03-2006 18:03:48

Just one question... I'm following anything that looks tutorial-like in nature, and am very interested in getting Ogre into a wx or pygame window, but there's one line in the code above that confuses me.

What is the elusive plugins.cfg?

deficite

25-03-2006 04:36:56

plugins.cfg is a configuration file that specifies what OGRE plugins to load. I'm using the basic one that OGRE's examples use:
# Defines plugins to load

# Define plugin folder
PluginFolder=.

# Define plugins
Plugin=RenderSystem_Direct3D9
Plugin=RenderSystem_GL
Plugin=Plugin_ParticleFX
Plugin=Plugin_BSPSceneManager
Plugin=Plugin_OctreeSceneManager
Plugin=Plugin_CgProgramManager

Just copy the "plugins.cfg" out of the examples directory and stick it in the same directory as your program

You need to find the .dll files that have the same name as the plugins listed in the file (such as RenderSystem_Direct3D9.dll) and stick them in the directory you specify as "PluginFolder" above. (which happens to be the same directory)

deficite

27-03-2006 00:22:15

If anybody used the code up until now and noticed some weird garbage when the window opened up, I forgot to put the viewport and camera creation in there. For some reason it was working without when I made it originally and I tried to run it today and got some weird results. It's fixed now.

Brad

26-08-2006 10:25:27

For anyone that happens to be using this PyGame integration example along with PyOgre 1.2, change the following line if you have an issue with the sceneManager expression :

self.sceneManager = self.root.getSceneManager(ogre.ST_GENERIC)
to
self.sceneManager = self.root.createSceneManager(ogre.ST_GENERIC,"DefaultSceneManager")

It seemed to resolve the issue for me at least.

Brendon

08-10-2006 19:47:52

I've been trying to get this working using my own Boost::Python wrapper -- I'm hoping to wrap my game stuff at a higher level than PyOgre does.

So far, nothing but segfaults when calling createRenderWindow in C with a handle from pygame.

Are you guys all using Windows? I've been looking at http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fSysWMInfo and noticed that in Windows you get a simple HWND matching what createRenderWindow wants, but in Linux it wants "poslong:posint:poslong (display*:screen:windowHandle) or poslong:posint:poslong:poslong (display*:screen:windowHandle:XVisualInfo*)", which I can't figure out how to construct from what get_wm_info() gives me.

Any suggestions?

Thanks,
Brendon

Derelict

10-11-2006 00:43:51

Newbie here, sorry!

Using Python 2.4, pyGame 1.7.1 and pyOgre 1.0.6.0, I tried to run the code from the Wiki, and it presents me with a blank configuration dialog devoid of any options, so it can't select a renderer to use. Also, I tried using the code to bypass the configuration dialog (also from the Wiki), and it couldn't find any renderers to use.

I'm up to Beginner Tutorial 5, and everything else has run fine with working configuration dialogs. What am I doing wrong here?

dermont

10-11-2006 03:40:53

Check the Ogre.log file for errors. You'll probably find an error message indicating 'plugins.cfg' couldn't be found - ogre.getPluginPath() should point to that file, e.g:

print ogre.getPluginPath()
>> c:\Python24\lib\site-packages\pyogre\plugins.cfg

Therefore either a or b should be OK.

#self.root = ogre.Root("plugins.cfg")
a) self.root = ogre.Root(ogre.getPluginPath())
b) self.root = ogre.Root("c:/Python24/lib/site-packages/pyogre/plugins.cfg")

r0ot

24-11-2011 01:30:36

The code I'm using is kind of a calaboration from a view places on the internet, first from pygame.org, then from another forum, and finally to this.
The error I've landed on is:
Traceback (most recent call last):
File "_PyGame.py", line 75, in <module>
app = PyGameOGREApp()
File "_PyGame.py", line 11, in __init__
self._createWindow(width, height, fullscreen)
File "_PyGame.py", line 27, in _createWindow
renderParameters = ogre._StringStringMap()
AttributeError: 'module' object has no attribute '_StringStringMap'


I can't for the life of me understand what's going on, because here you all are using ogre._StringStringMap() fine without any problems.
Here is my code, hopefully someone can help me.

import pygame
from pygame.locals import *
import ogre.renderer.OGRE as ogre
from ctypes import *

class PyGameOGREApp:
"Provides a base for an application using PyGame and PyOgre"
def __init__(self, width=640, height=480, fullscreen=False):
self._initPyGame()
self._initPyOgre()
self._createWindow(width, height, fullscreen)
self._createViewport()
self._loadResources("resources.cfg")
self._createEntities()
def _initPyGame(self):
"Starts up PyGame"
pygame.init()
def _initPyOgre(self):
"Instantiates the PyOgre root and sceneManager objects and initialises"
self.root = ogre.Root("plugins.cfg")
self.root.showConfigDialog()
self.root.initialise(False)
self.sceneManager = self.root.createSceneManager(ogre.ST_GENERIC, "Default SceneManager")
def _createWindow(self, width, height, fullscreen):
"Creates a PyGame window and sets PyOgre to render onto it"
self.screen = pygame.display.set_mode((width,height))
renderParameters = ogre._StringStringMap()
renderParameters['externalWindowHandle'] = str(pygame.display.get_wm_info()['window'])
self.renderWindow = self.root.createRenderWindow('PyOgre through PyGame', width, height, \
fullscreen, renderParameters)
self.renderWindow.active = True
def _createViewport(self):
"Creates the user's viewport and camera"
self.camera = self.sceneManager.createCamera("camera")
self.camera.position = (0, 0, 500)
self.camera.lookAt((0, 0, -300))
self.camera.nearClipDistance = 5
self.viewport = self.renderWindow.addViewport(self.camera)
self.viewport.backgroundColour = (0, 0, 0)
def _loadResources(self, rcfile):
"Loads the resource paths from specified resource config file"
rc = ogre.ConfigFile()
rc.loadFromFile(rcfile)
for section, key, path in rc.values:
ogre.ResourceGroupManager.getSingleton().addResourceLocation(path, key, section)
ogre.ResourceGroupManager.getSingleton().initialiseAllResourceGroups()
def _createEntities(self):
"For simple demo purposes this can be used to create entities and attach them to sceneNode's"
self.entities = [self.sceneManager.createEntity("robot", "robot.mesh")]
self.sceneNodes = [self.sceneManager.rootSceneNode.createChildSceneNode("robotNode")]
for i in range(len(self.sceneNodes)):
self.sceneNodes.attachObject(self.entities)
def _createScene(self):
"Prepare the scene. All logic should go here"
pass
def _presentScene(self):
"Render the scene and anything else needed done at the time of presentation"
self.root.renderOneFrame()
def run(self):
"Brings the application to life"
while self._processEvents():
self._createScene()
self._presentScene()
pygame.quit()
def _processEvents(self):
"Process events and take appropriate action"
for event in pygame.event.get():
if event.type is QUIT:
return False
elif event.type is KEYDOWN and event.key is K_ESCAPE:
return False
return True

# Instantiate and run!
app = PyGameOGREApp()
app.run()

dermont

24-11-2011 07:54:03

The code I'm using is kind of a calaboration from a view places on the internet, first from pygame.org, then from another forum, and finally to this.
The error I've landed on is:
Traceback (most recent call last):
File "_PyGame.py", line 75, in <module>
app = PyGameOGREApp()
File "_PyGame.py", line 11, in __init__
self._createWindow(width, height, fullscreen)
File "_PyGame.py", line 27, in _createWindow
renderParameters = ogre._StringStringMap()
AttributeError: 'module' object has no attribute '_StringStringMap'


I can't for the life of me understand what's going on, because here you all are using ogre._StringStringMap() fine without any problems.
Here is my code, hopefully someone can help me.


For python-ogre use "ogre.NameValuePairList()".

There is an example on svn using pygame/python-ogre.
http://python-ogre.svn.sourceforge.net/ ... iew=markup

I don't know much about pygame though I'm not quite sure what the example here, and in the above link, show in terms of sharing a python-ogre/pygame render window/context. Maybe if you could elaborate on what you are trying to achieve someone can help.