Python-osc, add_arg in a for loop

Question:

I want to add_arg to my OSC message without knowning the exact length of a list.
The thing is, I want to avoid typing my osc message like this:

probabilityMSG.add_arg(classProbability[0], arg_type='f')
probabilityMSG.add_arg(classProbability[1], arg_type='f')
probabilityMSG.add_arg(classProbability[2], arg_type='f')
probabilityMSG.add_arg(classProbability[3], arg_type='f')

So I try to put that add_arg in a for loop.
Below is my function:

def send_OSC(overallClassProbability, predictedClass, predictedParams):
    ipAddress = "127.0.0.1"
    port = 5005

    client = udp_client.SimpleUDPClient(ipAddress, port)

    classProbability = overallClassProbability.flatten()

    probabilityMSG = osc_message_builder.OscMessageBuilder(address = '/percentage')
    classMSG = osc_message_builder.OscMessageBuilder(address= '/class')
    paramsMSG = osc_message_builder.OscMessageBuilder(address= '/params')

    # -------
    for i in range(len(classProbability)):
        probabilityMSG.add_arg(classProbability[i], arg_type='f')

    # -------
    for i in range(len(predictedParams)):
        paramsMSG.add_arg(predictedParams[i], arg_type='f')

    # -------
    classMSG.add_arg(predictedClass, arg_type='i')

    # -------
    paramsMSG = paramsMSG.build()
    classMSG = classMSG.build()
    probabilityMSG = probabilityMSG.build()
    client.send(paramsMSG)
    client.send(classMSG)
    client.send(probabilityMSG)

But I have nothing coming out with this technic…
It seems like python-osc cannot handle this.

Do you have any idea?

EDIT: Should I write a self modifying Python code?
I was thinking about making a script that would modify the OSC message sending script.

I am referring to this post for the self modifying Python script.

Asked By: saltyrainbow

||

Answers:

I have no idea why it didn’t work last time. But, yeah, seems to work fine with add_arg within a for loop.

Example:

for i in range(len(classProbability)):
    probabilityMSG.add_arg(classProbability[i], arg_type='f')

Nevertheless, I did not find any mention of this in the documentation of python-osc. Maybe it seems obvious for advanced users.

Answered By: saltyrainbow
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.