oscP5 in Python mode

edited April 2016 in Python Mode

Hello! I am using Processing in Python mode and I am trying to set up a function to send/receive OSC messages. Is there an example somewhere ? I am trying to convert the Java syntax into Python but I am wondering if the library works at all in Python mode. Please advise!

Answers

  • Sorry but I couldn't find any reference to oscP5 in your reply. am I missing something?

  • edited January 2016

    Maybe you've missed the fact that Python Mode is based on Jython?
    And thus it can use any Java library. And Processing itself is a Java library too!

  • I understand that, thank you. But about using oscP5: I am trying to refer to the examples but I am not sure what to change and how to make the java code work in python mode. For instance do I need the 'this' variable? In general is there some example about it? thank you for your help!

  • edited January 2016

    But about using oscP5: I am trying to refer to the examples...
    In general is there some example about it?

    Chances are very slim to find any specific Python Mode library examples. :(
    Python Mode got some when we hit CTRL+SHIFT+O @ "Contributed Libraries in Python" though.

    I am not sure what to change and how to make the Java code work in Python Mode.

    You should post your attempts. Trying to get help only by words hardly works!
    However, I barely know any Python. But who knows some1 else might show up? :-\"

    For instance do I need the this variable?

    In Java, this isn't a mere variable but a language keyword. Just like while, return, float, etc.
    Python doesn't have an "official" keyword for this.
    But in Python, by convention, the 1st parameter in all methods is named self. And it acts as 1.
    However, we can choose any other name if we'd like to piss pythonists off! >:)

    P.S.: Seems like Python Mode already declares a variable called this to represent the current sketch. ~O)

  • edited January 2016

    Ok, got it. Here is my attempt at covering one of the oscP5 examples (oscP5sendReceive.pde) into Python. Of course it doesn't work. The 'this' keyword is clearly wrong. 'self' does not work though. I would love to hear someone who has gone through this, even though it seems that processing in python doesn't have a lot of fans...

    add_library('oscP5')
    
    import oscP5
    import netP5
    
    oscP5 = OscP5(this, 12000)
    
    myRemoteLocation = NetAddress("127.0.0.1",12000)
    
    
    def setup():
    
      size(400,400)
    
      frameRate(25)
    
      oscP5 = OscP5(this,12000)
    
      myRemoteLocation = NetAddress("127.0.0.1",12000)
    
    
    def draw():
    
      background(0)  
    
    
    def mousePressed():
    
        myMessage = OscMessage("/test") 
    
        myMessage.add(123) 
    
        oscP5.send(myMessage, myRemoteLocation)
    
    
    def oscEvent(OscMessage, theOscMessage):
    
      print "### received an osc message."
    
      print " addrpattern:%s " % theOscMessage.addrPattern()
    
      print " typetag: %s" % theOscMessage.typetag()
    
  • Please re-edit your last post in order to properly format your code for the forum here:
    https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text

  • edited January 2016

    ... it seems that Processing in Python doesn't have a lot of fans...

    My guess is that most folks who use other Processing spin-offs are taught in school.
    And hardly come here for any help! 8-|

  • edited January 2016

    This is just my poor attempt to improve your attempt. I dunno Python after all: :(|)

    # OscP5 Python Conversion (v2.21)
    # forum.Processing.org/two/discussion/14685/oscp5-in-python-mode
    # 2016-Jan-30
    
    add_library('oscP5')
    
    def setup():
        size(600, 400)
        noLoop()
        frameRate(25)
    
        global osc, loc, msg
        osc = OscP5(this, 12000)
        loc = NetAddress('127.0.0.1', 12000)
        msg = OscMessage('/test')
    
    
    def draw():
        background(int(random(PImage.ALPHA_MASK)))
        frame.setTitle(`frameCount`)
    
    
    def mousePressed():
        if 'osc' not in globals(): return
    
        msg.clearArguments()
        msg.add(123)
        osc.send(msg, loc)
    
        print msg
        redraw()
    
    
    def oscEvent(osc, msg):
        print "### Received an Osc message."
        print "AddrPattern: %s" % msg.addrPattern()
        print "TypeTag: %s" % msg.typetag()
        redraw()
    
  • Thank you. Unfortunately it doesn't work. :-S I do think that the 'this' keyword is an issue.

  • edited January 2016 Answer ✓
    • I doubt that's the cause for the problem! OscP5 is a Java library for Processing.
    • And it demands the active PApplet to be passed as its constructor's 1st argument.
    • Like I've mentioned before, Python Mode already declares a variable called this.
    • And assigns to it the sketch's PApplet object reference.
    print this
    print isinstance(this, PApplet)
    exit()
    
  • my bad! it does work. I still need to figure out how and why but this is a good starting point. Thanks so much!

Sign In or Register to comment.