I now mostly post over here: blog.greer-inc.com

I'm a Digital Designer, Art Director and Developer. I'm into photography, interactive design, the web, advertising and art.

Communicating between Blender game engine and Processing

Posted: January 7th, 2010 | Author: | Filed under: Blender, processing | Tags: , , , , | Comments Off

The Blender game engine gives you a pretty sophisticated world to play with.  Physics calculations like collisions, gravity, rebounds, etc are taken care of for you in the system.  Communicating between the game engine and other systems allows you to work with fewer limitations, or to work in a more familiar system where necessary.

In this example, Processing is used to access the macbook’s sudden motion (tilt) sensor, and to send the data into the game engine using OSC.

Some of the python scripts used here are pretty old, and various versions exist online.  Along the way, the authors seem to have been lost.  There have been varying amendments to most to produce this example.

With both Processing and Blender installed, download the source files here: motion2Blender

Connecting Blender to OSC.
To try out this example, you’ll need Blender 2.49 or later, Processing, the processing libraries oscP5 and sms, and a macbook post 2005 (with a sudden motion sensor in it).

1.  From the blender folder, open motionSensing.blend.
- you may need to open blender from the Terminal in order to see python errors or print commands properly (see footnote).

2.  There are two important python scripts: “connServer.py” and “oscLoc.py”.  The first will attempt to connect to the OSC server when the game is first run, the second listens out for osc messages once the connection is made.

3.  Hit ‘P’ whilst hovering over the 3D viewport to start the game in Blender.

4.  Ensure that you have the libraries oscP5 and sms in your Processing sketchbook libraries folder (see footnote).

5. To get data sending across OSC, open up motion.pde (in processing/motion/).  Hit the run button, or cmd+R.

6.  Tilt your mac book about, and the ball in the game engine should roll around in concert.

More on Blender:
The connServer.py and oscLoc.py scripts are linked into logic bricks located on the ball in the scene.      - connServer.py is triggered when the game first starts.
- oscLoc.py is connected to an “Always” sensor, and so is running at all times (but will only get any data after the connServer.py has been triggered).

You may need to specify the port number for OSC to listen on (line 35 of connServer.py) – this should correspond to the port that Processing is broadcasting to ( myRemoteLocation = new NetAddress(“″,12000); in the setup function).

You’ll need to adjust oscLoc.py to correlate to the messages you’re sending in on OSC.  This example is set up with processing, but it could receive OSC from anything.

Whenever an OSC message is received by Blender, the oscLoc script sends any data it receives to the OSC callback manager: “manage.handle(data)”.  In turn, this handler examines the first element in the array, and sends the message to the appropriate function.  In this example, all data sent will be “/move”, which will call a function called ‘move’:

Here’s the simple function which is being called:

Force is being applied to the object based on the movements of the laptop.

Launching Blender from the Terminal:
If working on a mac, you may need to launch blender from the terminal in order to be able to see the python outputs for prints and the like.  You should be able to see these in the Console, but it often refuses to update properly.

To launch from the terminal, navigate to your blender folder in Terminal, (type “cd /Applications/blender/” and hit enter), then type “blender.app/Contents/MacOS/blender &” to launch.  Python output and debugging will be printed to the terminal.  Alternatively, right click on the blender application, and select “show package contents’.  From the window which opens, navigate to Contents/MacOS, and drag the ‘blender’ file into the terminal window, hit enter to run.

Installing Processing Libraries
You can now add a library to Processing by adding its folder to the Libraries folder in your Sketches directory.  If Processing is running, you will need to restart it before it will recognise a new library.

To set your sketchbook location, open Processing, and from the top bar select Processing > Preferences.  Each library in the Libraries folder should be in a folder containing a subfolder called ‘library’, within which there should be a .jar file, eg:

Comments are closed.