Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
simple python osc test server that will print pupil/norm_pos mssages
installing pyOSC:
git clone git:// pyosc
cd pyosc
python install (may need sudo)
from OSC import OSCServer
import sys
from time import sleep
server = OSCServer( ("localhost", 7110) )
server.timeout = 0
run = True
# this method of reporting timeouts only works by convention
# that before calling handle_request() field .timed_out is
# set to False
def handle_timeout(self):
self.timed_out = True
# funny python's way to add a method to an instance of a class
import types
server.handle_timeout = types.MethodType(handle_timeout, server)
def callback(path, tags, args, source):
print path,args
server.addMsgHandler( "/pupil/norm_pos", callback )
while run:
# do the game stuff:
# call user script
# clear timed_out flag
server.timed_out = False
# handle all pending requests then return
while not server.timed_out:
a script that will replay pupil server messages to a osc server.
as implemented here only the pupil_norm_pos is relayed.
implementeing other messages to be send as well is a matter of renaming the vaiables.
installing pyOSC:
git clone git:// pyosc
cd pyosc
python install (may need sudo)
#zmq setup
import zmq
context = zmq.Context()
socket = context.socket(zmq.SUB)
#filter by messages by stating string 'STRING'. '' receives all messages
socket.setsockopt(zmq.SUBSCRIBE, '')
#osc setup
from OSC import OSCClient, OSCMessage
client = OSCClient()
client.connect( ("localhost", 7110) )
while True:
msg = socket.recv()
print "raw msg:\n", msg
items = msg.split("\n")
msg_type = items.pop(0)
items = dict([i.split(':') for i in items[:-1] ])
if msg_type == 'Pupil':
if items.get('norm_pupil', 'None') != "None":
pupil_x,pupil_y = map(float,items['norm_pupil'][1:-1].split(','))
client.send( OSCMessage("/pupil/norm_pos", (pupil_y,pupil_y)) )
# process event msgs from plugins here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment