### Simple OSC Server in Python Source: https://github.com/nvidia-omniverse/python-osc/blob/master/README.rst This example demonstrates a simple OSC server that listens for OSC messages on specified addresses and executes corresponding handlers. It uses the pythonosc library to create a dispatcher, map addresses to handlers, and start a server. ```python """Small example OSC server This program listens to several addresses, and prints some information about received packets. """ import argparse import math from pythonosc.dispatcher import Dispatcher from pythonosc import osc_server def print_volume_handler(unused_addr, args, volume): print("[{0}] ~ {1}".format(args[0], volume)) def print_compute_handler(unused_addr, args, volume): try: print("[{0}] ~ {1}".format(args[0], args[1](volume))) except ValueError: pass if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--ip", default="127.0.0.1", help="The ip to listen on") parser.add_argument("--port", type=int, default=5005, help="The port to listen on") args = parser.parse_args() dispatcher = Dispatcher() dispatcher.map("/filter", print) dispatcher.map("/volume", print_volume_handler, "Volume") dispatcher.map("/logvolume", print_compute_handler, "Log volume", math.log) server = osc_server.ThreadingOSCUDPServer( (args.ip, args.port), dispatcher) print("Serving on {}".format(server.server_address)) server.serve_forever() ``` -------------------------------- ### Installing python-osc from source Source: https://github.com/nvidia-omniverse/python-osc/blob/master/README.rst These commands install the python-osc library from source. The first command runs the unit tests, and the second command installs the library. ```bash $ python setup.py test $ python setup.py install ``` -------------------------------- ### Installing python-osc with pip Source: https://github.com/nvidia-omniverse/python-osc/blob/master/README.rst This command installs the python-osc library using pip, the Python package installer. It downloads and installs the latest version of the library and its dependencies from the Python Package Index (PyPI). ```bash $ pip install python-osc ``` -------------------------------- ### Building OSC Bundles in Python Source: https://github.com/nvidia-omniverse/python-osc/blob/master/README.rst This example demonstrates how to build OSC bundles using the pythonosc library. It creates a bundle, adds messages with arguments, and nests bundles within each other. ```python 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. ``` -------------------------------- ### Mapping Address to Function Source: https://github.com/nvidia-omniverse/python-osc/blob/master/docs/dispatcher.rst Maps an OSC address to a Python function using the Dispatcher. This example demonstrates how to associate a specific address pattern with a handler function. ```python from pythonosc.dispatcher import Dispatcher disp = Dispatcher() disp.map("/some/address*", some_printing_func) ``` -------------------------------- ### Simple OSC Client in Python Source: https://github.com/nvidia-omniverse/python-osc/blob/master/README.rst This example demonstrates a simple OSC client that sends random values to a specified address. It uses the pythonosc library to create a UDP client, send messages, and includes command-line arguments for specifying the IP address and port. ```python """Small example OSC client This program sends 10 random values between 0.0 and 1.0 to the /filter address, waiting for 1 seconds between each value. """ 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) ``` -------------------------------- ### Sending OSC messages with SimpleUDPClient in Python Source: https://github.com/nvidia-omniverse/python-osc/blob/master/docs/client.rst This example demonstrates how to create a SimpleUDPClient and send OSC messages with different data types (int, float, string) to a specified address. ```python from pythonosc.udp_client import SimpleUDPClient ip = "127.0.0.1" port = 1337 client = SimpleUDPClient(ip, port) # Create client client.send_message("/some/address", 123) # Send float message client.send_message("/some/address", [1, 2., "hello"]) # Send message with int, float and string ``` -------------------------------- ### Async Concurrent OSC Server Example Source: https://github.com/nvidia-omniverse/python-osc/blob/master/docs/server.rst This code demonstrates how to create an asynchronous OSC server that runs concurrently with a main program loop using asyncio. The server listens for OSC messages and invokes the appropriate callback functions using a dispatcher. The main loop runs for 10 iterations, sleeping for a second on each iteration, allowing the server to handle incoming messages. ```python from pythonosc.osc_server import AsyncIOOSCUDPServer from pythonosc.dispatcher import Dispatcher import asyncio def filter_handler(address, *args): print(f"{address}: {args}") dispatcher = Dispatcher() dispatcher.map("/filter", filter_handler) ip = "127.0.0.1" port = 1337 async def loop(): """Example main loop that only runs for 10 iterations before finishing""" for i in range(10): print(f"Loop {i}") await asyncio.sleep(1) async def init_main(): server = AsyncIOOSCUDPServer((ip, port), dispatcher, asyncio.get_event_loop()) transport, protocol = await server.create_serve_endpoint() # Create datagram endpoint and start serving await loop() # Enter main loop of program transport.close() # Clean up serve endpoint asyncio.run(init_main()) ``` -------------------------------- ### Async Exclusive OSC Server Example Source: https://github.com/nvidia-omniverse/python-osc/blob/master/docs/server.rst This code demonstrates how to create an asynchronous OSC server that runs exclusively in the asyncio event loop. The server listens for OSC messages and invokes the appropriate callback functions using a dispatcher. This mode does not include a main loop, and the server runs as the only task in the event loop. ```python from pythonosc.osc_server import AsyncIOOSCUDPServer from pythonosc.dispatcher import Dispatcher import asyncio def filter_handler(address, *args): print(f"{address}: {args}") dispatcher = Dispatcher() dispatcher.map("/filter", filter_handler) ip = "127.0.0.1" port = 1337 server = AsyncIOOSCUDPServer((ip, port), dispatcher, asyncio.get_event_loop()) server.serve() ``` -------------------------------- ### Blocking OSC Server Example Source: https://github.com/nvidia-omniverse/python-osc/blob/master/docs/server.rst This code demonstrates how to create a blocking OSC server that listens for OSC messages and invokes the appropriate callback functions using a dispatcher. The server blocks program execution and remains idle between handling requests. It defines handlers for specific addresses and a default handler for unmapped addresses. ```python from pythonosc.dispatcher import Dispatcher from pythonosc.osc_server import BlockingOSCUDPServer def print_handler(address, *args): print(f"{address}: {args}") def default_handler(address, *args): print(f"DEFAULT {address}: {args}") dispatcher = Dispatcher() dispatcher.map("/something/*", print_handler) dispatcher.set_default_handler(default_handler) ip = "127.0.0.1" port = 1337 server = BlockingOSCUDPServer((ip, port), dispatcher) server.serve_forever() # Blocks forever ``` -------------------------------- ### Initializing Dispatcher Source: https://github.com/nvidia-omniverse/python-osc/blob/master/docs/dispatcher.rst Initializes a Dispatcher object from the pythonosc library. The Dispatcher is used to map OSC addresses to Python functions. ```python from pythonosc.dispatcher import Dispatcher from typing import List, Any dispatcher = Dispatcher() ``` -------------------------------- ### Mapping Wildcard Address to Function Source: https://github.com/nvidia-omniverse/python-osc/blob/master/docs/dispatcher.rst Maps a wildcard OSC address to a Python function. The function processes OSC messages with two float arguments, checking the address and printing filter values. ```python def set_filter(address: str, *args: List[Any]) -> None: # We expect two float arguments if not len(args) == 2 or type(args[0]) is not float or type(args[1]) is not float: return # Check that address starts with filter if not address[:-1] == "/filter": # Cut off the last character return value1 = args[0] value2 = args[1] filterno = address[-1] print(f"Setting filter {filterno} values: {value1}, {value2}") dispatcher.map("/filter*", set_filter) # Map wildcard address to set_filter function # Set up server and client for testing from pythonosc.osc_server import BlockingOSCUDPServer from pythonosc.udp_client import SimpleUDPClient server = BlockingOSCUDPServer(("127.0.0.1", 1337), dispatcher) client = SimpleUDPClient("127.0.0.1", 1337) # Send message and receive exactly one message (blocking) client.send_message("/filter1", [1., 2.]) server.handle_request() client.send_message("/filter8", [6., -2.]) server.handle_request() ``` -------------------------------- ### Mapping Address with Fixed Arguments Source: https://github.com/nvidia-omniverse/python-osc/blob/master/docs/dispatcher.rst Maps an OSC address to a function, including fixed arguments that are passed to the function before the OSC message arguments. The handler callback signature must account for these fixed arguments. ```python handler = disp.map("/some/other/address", some_printing_func, "This is a fixed arg", "and this is another fixed arg") ``` -------------------------------- ### Setting Default Handler Source: https://github.com/nvidia-omniverse/python-osc/blob/master/docs/dispatcher.rst Sets a default handler function that is called for every unmatched OSC address. This is useful for debugging or learning about OSC messages. ```python disp.set_default_handler(some_handler_function) ``` -------------------------------- ### Unmapping Address with Function and Arguments Source: https://github.com/nvidia-omniverse/python-osc/blob/master/docs/dispatcher.rst Removes a mapping between an OSC address and a handler function from the Dispatcher by specifying the address, the handler function, and the fixed arguments. ```python disp.unmap("some/other/address", some_printing_func, *some_fixed_args) ``` -------------------------------- ### Unmapping Address Source: https://github.com/nvidia-omniverse/python-osc/blob/master/docs/dispatcher.rst Removes a mapping between an OSC address and a handler function from the Dispatcher. This can be done using the address and the Handler object, or by providing the function and fixed arguments used during the mapping. ```python disp.unmap("some/other/address", handler) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.