I’m working on connecting some things into the game engine over OSC, and I’m running into some frustrating performance issues. Essentially, if I’m streaming more than one or two values, I get this terrible lag (several seconds), and it seems to get worse the longer the game is running.
My main interest is in working with the Wiimote, which does send a lot of data constantly.
First, I followed this great tutorial http://www.local-guru.net/blog/2009/03/08/using-the-blendergameengine-as-osc-client.
that worked fine, I can send a single value from Pd and get realtime response in Blender. I extended that code to take data sent from Marije Baalman’s WiiOSC http://www.nescivi.nl/?p=73
It works, but the lag is awful, particularly noticeable for the buttons. Similar python code works great standalone, so it’s not an issue with slowness in python, pybluez, or the python osc library; it seems to be something unique to Blender. Any suggestion on what might be going on?
I’m testing this on two platforms - Kubuntu 8.04, with Blender 2.45, and OSX 10.4, with Blender 2.48. The behavior is the same on both platforms.
Here’s the code I’m using Blender:
import GameLogic
import Blender
import Rasterizer
from Blender import Material
import socket
import osc
Rasterizer.showMouse(1)
cont = GameLogic.getCurrentController()
own = cont.getOwner()
if not own.connected:
print "Connecting.."
own.connected = 1
mesh = own.getMesh()
for i in range(mesh.getVertexArrayLength(0)):
v = mesh.getVertex(0,i)
v.color = [0.0, 1.0, 0.0, 1.0]
GameLogic.socket = socket.socket( socket.AF_INET, socket. SOCK_DGRAM )
GameLogic.socket.bind(('localhost', 9001))
GameLogic.socket.setblocking(0)
GameLogic.socket.settimeout(0.1)
else:
try:
data = GameLogic.socket.recv(1024)
msglist = osc.decodeOSC(data)
if type(msglist[0]) == list:
for msg in msglist:
address=msg[0]
if address=="/wii/acc/x":
x=msg[3] * 5
own.setPosition([x,0,0])
elif address=="/wii/keys/a":
value=msg[3]
if value==1:
own.scaling=[2,2,2]
else:
own.scaling=[1,1,1]
except socket.error:
pass
At first I thought this might be a python issue, so I put together a standalone test listener using the same python osc library (I’m using simpleOSC.py from ixi: http://www.ixi-audio.net/content/body_backyard_python.html)
My standalone app works great, no lag at all:
import osc
import time
import sys
import socket
keys={}
accels=[0,0,0]
wiimoteX = 0
wiimoteY = 0
def handleMessage(msg):
global keys
global accels
s=msg[0]
v=msg[3]
msgpath = s.split('/')
if msgpath[1]=='wii':
if msgpath[2]=='keys':
k=msgpath[3]
keys[k]=v
elif msgpath[2]=='acc':
axis=msgpath[3]
if axis=='x':
accels[0]=v
elif axis=='y':
accels[1]=v
elif axis=='z':
accels[2]=v
def getOSCData():
global oscListener
data=None
try:
data = oscListener.recv(1024)
except:
pass
if data != None:
message=osc.decodeOSC(data)
#print message
if type(message[0])==str:
handleMessage(message)
elif type(message[0])==list:
for msg in message:
handleMessage(msg)
oscListener = socket.socket (socket.AF_INET, socket.SOCK_DGRAM)
oscListener.bind(('',9001))
oscListener.setblocking(0)
print oscListener
print 'ready to receive messages on port 9001'
while 1:
getOSCData()
print "Accels: %.3f %.3f %.3f" % (accels[0], accels[1], accels[2]),
print keys
thanks