Python-Ogre Minimal Application

inneractive

11-06-2007 06:51:12

I'm continuing my Python-Ogre education by building a minimal application that is not dependent on the SampleFramework. I'm using the "MinimalApplication" Ogre Wiki entry and sf_OIS.py as inspiration. Right now I have code to get an Ogre render window up with a frame listener. Any comments, corrections, or improvement suggestions would be welcome. When it is finished I can add it to the Python-Ogre wiki.

Right now I guess I need to figure out clean up. Will python garbage collection not be enough? Should I manually release resources? Also, I noticed that the Ogre Config Dialog automatically loads up all the plugins and resources in my .cfg files. So I guess I need to create my own config dialog if I want to create an app that does not rely on those .cfg files.

Here is the code so far:
#!/usr/bin/env python
# This code is Public Domain and was written for Python-Ogre 1.0.
"""minimal_application.py sets up a basic framework for interacting with Ogre."""

import ogre.renderer.OGRE as ogre
import ogre.io.OIS as OIS


class Application(object):
"""Application class is the base for an Ogre application."""

def __init__(self):
self.root = None
self.renderWindow = None
self.sceneManager = None
self.camera = None
self.viewport = None
self.frameListener = None

def start(self):
"""Starts the rendering loop."""
if not self.__setup():
return False
# Render until a frame listner returns false.
self.root.startRendering()

def __setup(self):
"""Sets up the ogre application."""
self.root = ogre.Root()
self.root.setFrameSmoothingPeriod (5.0)
if not self.__configure():
return False
self.__chooseSceneManager()
self.__createCamera()
self.__createViewport()
self.__createFrameListener()
return True

def __configure(self):
"""This shows the config dialog and creates the Ogre Render Window."""
if not self.root.showConfigDialog():
return False
self.renderWindow = self.root.initialise(True, "OGRE Render Window")
return True

def __chooseSceneManager(self):
"""Chooses a default SceneManager."""
self.sceneManager = self.root.createSceneManager(ogre.ST_GENERIC,"GenericSM")

def __createCamera(self):
"""Creates the camera."""
self.camera = self.sceneManager.createCamera('UserCam')

def __createViewport(self):
"""Creates the Viewport."""
self.viewport = self.renderWindow.addViewport(self.camera)

def __createFrameListener(self):
"""Creates the FrameListener."""
self.frameListener = FrameListener(self.renderWindow, self.camera)
self.root.addFrameListener(self.frameListener)


class FrameListener(ogre.FrameListener, ogre.WindowEventListener):
"""FrameListener class, which takes care of basic mouse and keyboard input."""

def __init__(self, renderWindow, camera):
ogre.FrameListener.__init__(self)
ogre.WindowEventListener.__init__(self)
self.camera = camera
self.renderWindow = renderWindow
self.inputManager = None
self.keyboard = None
self.mouse = None
self.__setupInput()

def frameStarted(self, frameEvent):
# If the render window is closed, quit the application.
if(self.renderWindow.isClosed()):
return False
# If the escape key is pressed quit the application.
self.keyboard.capture()
return not self.keyboard.isKeyDown(OIS.KC_ESCAPE)

def __setupInput(self):
"""Sets up the OIS input manager and user input devices."""
# Setup OIS with the Ogre Render Window.
window = self.renderWindow.getCustomAttributeInt("WINDOW")
self.inputManager = OIS.createPythonInputSystem([("WINDOW",str(window))])
# Setup the user input devices.
self.keyboard = self.inputManager.createInputObjectKeyboard(OIS.OISKeyboard, False)
# Add the render window as an event listener.
ogre.WindowEventUtilities.addWindowEventListener(self.renderWindow, self);


if __name__ == '__main__':
# Import Psyco if it is available.
try:
import psyco
psyco.full()
except ImportError:
pass
# Start the application.
try:
app = Application()
app.start()
except ogre.OgreException, e:
print e

andy

11-06-2007 07:44:50

You could also look at Demo_blank and Demo_Spinner as neither use the framework

Cheers

Andy

inneractive

11-06-2007 11:03:06

Thanks for the info, I'll check them out.

deficite

11-06-2007 17:23:36

I'm always glad to see people do stuff like this. Although it's not useful for me anymore, I do remember when I first started using OGRE and how I almost gave up until I found Tutorial 0 on the wiki. I don't like using example frameworks for anything. I want to learn the real way of doing things. Nothing against the example framework, it's just not for me. I ended up making my own minimalist framework and an old version of it is on the wiki as "Using PyOgre with Pygame". I'm not really interested in updating it, as PyOgre has been all but deprecated, and with the coming of OIS, I don't really need Pygame's input anymore (which was the main reason I wanted to use Pygame with OGRE)

inneractive

11-06-2007 21:19:18

Yeah I like to see how everything is working, then tinker around with stuff to get a better understanding of what can be done. Thanks for pointing out your tutorial as well. I'm looking forward to checking it out.