Mark
Mark

Reputation: 5102

How to send OSC bundle using python-osc?

On the repository page of python-osc I read:

Building bundles

from pythonosc import osc_bundle_builder
from pythonosc import osc_message_builder

bundle = osc_bundle_builder.OscBundleBuilder(
    osc_bundle_builder.IMMEDIATELY)
msg = osc_message_builder.OscMessageBuilder(address="/SYNC")
msg.add_arg(4.0)
# Add 4 messages in the bundle, each with more arguments.
bundle.add_content(msg.build())
msg.add_arg(2)
bundle.add_content(msg.build())
msg.add_arg("value")
bundle.add_content(msg.build())
msg.add_arg(b"\x01\x02\x03")
bundle.add_content(msg.build())

sub_bundle = bundle.build()
# Now add the same bundle inside itself.
bundle.add_content(sub_bundle)
# The bundle has 5 elements in total now.

bundle = bundle.build()
# You can now send it via a client as described in other examples.

But it's not clear to me how to "send it via a client as described in other examples". I tried:

client.send_message(bundle)

but it returns:

SimpleUDPClient.send_message() missing 1 required positional argument: 'value'

and

client.send_message("/test", bundle)

returns:

Infered arg_value type is not supported

What is the correct syntax to send OSC bundles?

Here how the client is constructed, from the example in the same page:

import argparse
import random
import time

from pythonosc import udp_client


if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  parser.add_argument("--ip", default="127.0.0.1",
      help="The ip of the OSC server")
  parser.add_argument("--port", type=int, default=5005,
      help="The port the OSC server is listening on")
  args = parser.parse_args()

  client = udp_client.SimpleUDPClient(args.ip, args.port)

  for x in range(10):
    client.send_message("/filter", random.random())
    time.sleep(1)

Upvotes: 1

Views: 148

Answers (1)

Mark
Mark

Reputation: 5102

Looking at the source code I found the solution:

client.send(bundle)

but this is not specified in the example. By the way also the bundle example is wrong because it adds multiple times the same content. The correct way to send a bundle is:

bundle = osc_bundle_builder.OscBundleBuilder(osc_bundle_builder.IMMEDIATELY)
msg = osc_message_builder.OscMessageBuilder(address="/foo/bar/1")
msg.add_arg(1)
msg.add_arg(2)
# add other arguments as needed

bundle.add_content(msg.build())
bundle = bundle.build()
client.send(bundle)

Upvotes: 0

Related Questions