0

I'm working on a simple program where Max-msp send some integers via OSC to python. I'm using the pyOSC library for python. Now i can easily print the numbers but i can't decode it and use it as an integer inside python (i'm noob).

ideas?

import OSC
import time, threading

receive_address = '127.0.0.1', 9000

def printing_handler(addr, tags, stuff, source):
    print "---"
    print "received new osc msg from %s" % OSC.getUrlStr(source)
    print "with addr : %s" % addr
    print "typetags %s" % tags
    print "data %s" % stuff
    print "---"

s = OSC.OSCServer(receive_address) 
s.addMsgHandler("/numero", printing_handler) 

st = threading.Thread( target = s.serve_forever )
st.start()

thank you very much !

okgo
  • 9
  • 3

2 Answers2

0

It's not clear what all the types / length of stuff is, but, for example if type(stuff[0]) is already <type 'int'>, then you can simply use that value as-is.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

stuff is the OSC message's arguments. You should be able to retrieve them directly. Let's say you're sending 3 ints from Max, you should be able to do something like this:

def printing_handler(addr, tags, stuff, source):
    print "---"
    arg1 = stuff[0]
    arg2 = stuff[1]
    arg3 = stuff[2]
    print "received",arg1,arg2,arg3
    print "---"

If it helps, you can try playing with the pyOSC example code:

George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • thank you, the problem is that if i try to return arg1 for example i get the error: Message-callback did not return OSCMessage or None: – okgo Sep 25 '17 at 14:56
  • strange, I've just tested with the snippet above with 3 numbers sent from max and it worked. is there anything else in your script ? – George Profenza Sep 25 '17 at 15:30
  • yes the code above works really well, the problem is that i would like to export arg1 for example and use it outside the script – okgo Sep 25 '17 at 15:46
  • Sorry, but it's unclear how you are planning to export arg1 and use it outside the script. Could you please clarify ? If you want to use arg1 outside the callback function, create arg1 outside the scope function, but remember to use the `global arg1` inside the callback if you are not using a class (e.g. `self`) – George Profenza Sep 25 '17 at 15:50