syphon

Syphon for Python

Documentation Build PyPI

Python wrapper for the Syphon GPU texture sharing framework. This library was created to support both the Metal backend and the deprecated OpenGL backend. It requires macOS 12 or above and supports Python 3.10–3.14, including the free-threaded 3.13t and 3.14t builds.

The implementation is based on PyObjC to wrap the Syphon framework directly from Python. This approach eliminates the native wrapper layer and allows Python developers to extend the library as needed.

State of Development

  • Syphon Server Discovery
  • Metal Server
  • Metal Client
  • OpenGL Server
  • OpenGL Client
  • Syphon Client On Frame Callback
  • Async Frame Waiting
  • Context Managers
  • Private Servers and Server Renaming
  • Direct OpenGL Rendering

Usage

To install syphon-python it is recommended to use a prebuilt binary from PyPI:

pip install syphon-python

Since version 0.2.0, PyOpenGL is optional. To use the OpenGL backend, install it with:

pip install 'syphon-python[opengl]'

To work with NumPy images, as shown in the following example, also install the NumPy dependency:

pip install 'syphon-python[numpy]'

The following code snippet is a basic example showing how to share numpy images as MTLTexture with a SyphonMetalServer. There are more examples in examples.

import time

import numpy as np

import syphon
from syphon.utils.numpy import copy_image_to_mtl_texture
from syphon.utils.raw import create_mtl_texture

# create the server and stop it automatically when leaving the block
with syphon.SyphonMetalServer("Demo") as server, syphon.SyphonServerDirectory() as directory:
    directory.run_loop_interval = 0.001

    # create a texture on the server's Metal device
    texture = create_mtl_texture(server.device, 512, 512)

    # create an opaque red RGBA image
    texture_data = np.zeros((512, 512, 4), dtype=np.uint8)
    texture_data[:, :, 0] = 255  # fill red
    texture_data[:, :, 3] = 255  # fill alpha

    try:
        while True:
            # copy the image onto the texture and publish it
            copy_image_to_mtl_texture(texture_data, texture)
            server.publish_frame_texture(texture)

            # process discovery requests without opening a window
            directory.update_run_loop()
            time.sleep(1 / 60)
    except KeyboardInterrupt:
        pass

For applications without a UI, directory.update_run_loop() processes Cocoa events on the main thread so that other applications can discover the server. Calling it regularly keeps the server discoverable.

Development

To develop or manually build the library, use uv and Python 3.14 to set up the local repository. Building the Syphon framework requires full Xcode with its Metal toolchain; Command Line Tools alone are not sufficient. Installing a prebuilt wheel does not require Xcode.

Installation

# clone the repository and its submodules
git clone --recurse-submodules https://github.com/cansik/syphon-python.git
cd syphon-python

# install the library and development dependencies
uv sync --locked

This builds the framework and installs the library in editable mode, so Python changes are immediately available. For an existing checkout, run git submodule update --init --recursive first.

To install the additional dependencies used by the examples:

uv sync --locked --group examples

# run a Metal example
uv run --group examples python -m examples.MetalServerExampleMini

# run an OpenGL example with the optional dependency
uv run --group examples --extra opengl python -m examples.OpenGLServerExample

Build

Create a source archive and a wheel package in dist/:

uv build

The wheel includes the Syphon framework for both Apple Silicon and Intel. The source archive includes the framework sources, so it can also be built without cloning the repository:

python -m pip install ./dist/syphon_python-0.2.0.tar.gz

If you change the native framework sources during development, rebuild the editable installation with:

uv sync --locked --reinstall-package syphon-python

The build uses the selected Xcode installation or finds it at /Applications/Xcode.app automatically. If the Metal toolchain is missing, install it in Xcode > Settings > Components. For Xcode installed in a custom location, specify its path when building:

DEVELOPER_DIR=/path/to/Xcode.app/Contents/Developer uv build

Tests

Run the tests locally with pytest:

uv run pytest

# also test the optional NumPy and OpenGL support
uv run --extra numpy --extra opengl pytest

After running uv build, use uv run pytest --dist-dir dist to check the generated packages as well. Rendering tests require a Metal device or an OpenGL context; use -m 'not metal and not opengl' to skip them.

To test with a different Python version, for example a free-threaded build:

uv run --python 3.14t --extra numpy --extra opengl pytest

Formatting

The project uses Ruff to check and format Python code:

# check the code and formatting
uv run ruff check .
uv run ruff format --check .

# apply formatting
uv run ruff format .

Generate Documentation

# generate documentation into docs/
uv run --group docs --extra numpy python scripts/generate_doc.py

# launch the local documentation server
uv run --group docs --extra numpy python scripts/generate_doc.py --serve

For release and CI instructions, see the maintainer guide.

Citation

If you use syphon-python in your research, please cite it using CITATION.cff.

About

MIT License - Copyright (c) 2026 Florian Bruggisser

Documentation

The syphon-python library is a wrapper for the Syphon framework for the Python language. It exposes the Objective-C API to the Python world and adds helper methods to easily interoperate with Syphon. Syphon is an open source Mac OS X technology that allows applications to share video and still images with each other in real time.

Why a new library?

There are already wrappers like syphonpy that do the same thing as syphon-python. There are two main reasons why syphon-python was implemented:

Modern Graphics Pipeline

Most existing Python wrappers for Syphon only support the OpenGL framework. Even though OpenGL is still available in modern MacOS versions, as of MacOS Mojave 10.14, the framework is marked as deprecated and should be avoided. And while many applications are switching to the Metal graphics backend, Syphon needs to do the same.

The Syphon framework already supports the Metal graphics backend, but the wrappers usually do not. Syphon-python adds support for Metal and retains OpenGL.

Objective-C to Python

To add support for the new Metal graphics backend, the existing wrappers could be extended. However, many of them use an intermediate wrapper in C to expose the Objective-C API to Python. For Python developers it can be difficult to extend existing C code, so syphon-python uses PyObjC, a Python to Objective-C bridge.

Syphon Package

The main package of syphon-python is called syphon and contains all the necessary objects and classes of the library. To use syphon-python in a project, start with the following import statement.

import syphon

For each Metal class there is also an OpenGL counterpart. It is worth noting that Syphon supports interopability between Metal and OpenGL. This means that it is possible to run a Metal-based Syphon server and receive it in an OpenGL client and vice versa.

Syphon Server

To share graphic textures with other applications, a syphon server (sender) has to be created. All server implementations are based on the syphon.server.BaseSyphonServer and share the same interface, except the constructor. The following code example creates either a Metal oder OpenGL based server, using the app name Demo and using the default device or context.

# create a Metal based server
server = syphon.SyphonMetalServer("Demo")

# create an OpenGL based server
server = syphon.SyphonOpenGLServer("Demo")

To publish a texture, the method syphon.server.BaseSyphonServer.publish_frame_texture() can be used. We assume that the corresponding texture has already been created and is available in the texture variable. To see how to create and fill a MTLTexture or glTexture, please have a look at the examples.

texture = ...  # MTLTexture or glTexture
server.publish_frame_texture(texture)

The syphon.server.BaseSyphonServer.publish_frame_texture() contains a flag called flip to flip the texture horizontally. This can be set to True if the image is upside down on the receiver side.

It is also possible to check if a server has connected clients with the has_clients property.

To clean up and release allocated resources, a server should be stopped with the syphon.server.BaseSyphonServer.stop() method.

if not server.has_clients:
    server.stop()

Metal Server

On initialisation, the syphon.server.SyphonMetalServer creates a new system default Metal device as well as a new command queue. It is possible to override which MTLDevice the Syphon server is running on or which type of command queue is used. This can be done by using the additional parameters of the syphon.server.SyphonMetalServer.

import Metal

# overwrite the mtl_device
mtl_device = Metal.MTLCreateSystemDefaultDevice()
server = syphon.SyphonMetalServer("Demo", device=mtl_device)

# or also create custom command queue
mtl_command_queue = mtl_device.newCommandQueue()
server = syphon.SyphonMetalServer("Demo", device=mtl_device, command_queue=mtl_command_queue)

OpenGL Server

Install syphon-python[opengl] to enable OpenGL support. Metal usage does not require this extra.

On initialisation, the syphon.server.SyphonOpenGLServer tries to find the current cglContextObj using the current NSOpenGLContext. It is possible to override the automatic lookup by passing a valid cglContextObj as a parameter to the syphon.server.SyphonOpenGLServer.

import AppKit

ns_ctx = AppKit.NSOpenGLContext.currentContext()
cgl_context = ns_ctx.CGLContextObj()

server = syphon.SyphonOpenGLServer("Demo", cgl_context_obj=cgl_context)

For example, if glfw is used to create an OpenGL window, it is enough to set the current context through glfw and the syphon.server.SyphonOpenGLServer will be able to find this context on its own.

glfw.make_context_current(window)
server = syphon.SyphonOpenGLServer("Demo")

Shared Directory

To get a list of active Syphon servers on the system, the syphon.server_directory.SyphonServerDirectory can be used. The resulting list of objects is of type syphon.server_directory.SyphonServerDescription.

directory = syphon.SyphonServerDirectory()
servers = directory.servers

for server in servers:
    print(f"{server.app_name} ({server.uuid})")

It is also possible to listen for events when a server changes its status. However, it is important to update the NSRunLoop to receive messages. This can be done by repeatedly calling directory.update_run_loop().

def handler(event):
    print("A new server has been announced.")


directory.add_observer(syphon.SyphonServerNotification.Announce, handler)

while True:
    directory.update_run_loop()
    time.sleep(1.0)

Syphon Client

To receive graphic textures from other applications, a syphon client (receiver) must be created. All client implementations are based on syphon.client.BaseSyphonClient and share the same interface except for the constructor. The following code example creates either a Metal or OpenGL based client, using the first found server description and the default device or context.

# receive the first server description
directory = syphon.SyphonServerDirectory()
server_info = directory.servers[0]

# create a Metal client
client = syphon.SyphonMetalClient(server_info)

# create an OpenGL client
client = syphon.SyphonOpenGLClient(server_info)

To get textures, it is possible to first check if the server has provided a new texture using the has_new_frame property, and then read the new frame image using the new_frame_image property.

if client.has_new_frame:
    texture = client.new_frame_image  # either MTLTexture or glTexture

To stop the client and disconnect from the server, the syphon.client.BaseSyphonClient.stop() method can be used.

client.stop()

Metal Client

As with the metal server, it is possible to overwrite the device which the metal client is running on. This can be done by using the additional parameters of the syphon.client.SyphonMetalClient.

import Metal

# overwrite the mtl_device
mtl_device = Metal.MTLCreateSystemDefaultDevice()
client = syphon.SyphonMetalClient(server_info, device=mtl_device)

OpenGL Client

Install syphon-python[opengl] to enable OpenGL support.

As with the opengl server, it is possible to override the automatic lookup by passing a valid cglContextObj as a parameter to the syphon.client.SyphonOpenGLClient.

import AppKit

ns_ctx = AppKit.NSOpenGLContext.currentContext()
cgl_context = ns_ctx.CGLContextObj()

client = syphon.SyphonOpenGLClient(server_info, cgl_context_obj=cgl_context)

Utilities

To make sharing graphic textures as easy as possible, the library provides some utility methods to manipulate texture data.

Raw

The syphon.utils.raw module contains methods to create and manipulate textures with a raw bytes array.

Create MTLTexture

To create an MTLTexture the method syphon.utils.raw.create_mtl_texture can be used. It is possible to create your own default device or use a server's syphon.server.SyphonMetalServer.device property to get the current device.

import Metal
from syphon.utils.raw import create_mtl_texture

mtl_device = Metal.MTLCreateSystemDefaultDevice()
texture = create_mtl_texture(mtl_device, 512, 512)

Manipulate MTLTexture

To write bytes to an MTLTexture the method syphon.utils.raw.copy_bytes_to_mtl_texture() can be used.

from syphon.utils.raw import copy_bytes_to_mtl_texture

data = ...  # bytes() based buffer
texture = ...  # MLTTexture object

copy_bytes_to_mtl_texture(data, texture)

To read bytes from an MTLTexture the method syphon.utils.raw.copy_mtl_texture_to_bytes() can be used.

from syphon.utils.raw import copy_mtl_texture_to_bytes

texture = ...  # MLTTexture object

data = copy_mtl_texture_to_bytes(texture)  # returns bytes

Numpy

If you are working with Numpy arrays, the syphon.utils.numpy package contains helper methods for reading and writing numpy images to and from MTLTexture.

It is important to note that the numpy package is not installed by default, it must be installed using pip install 'syphon-python[numpy]'.

To write a numpy image to a MTLTexture, the syphon.utils.numpy.copy_image_to_mtl_texture() method can be used.

import numpy as np
from syphon.utils.numpy import copy_image_to_mtl_texture

texture = ...  # MLTTexture object

# create RGBA image
texture_data = np.zeros((512, 512, 4), dtype=np.uint8)

# copy image to texture
copy_image_to_mtl_texture(texture_data, texture)

To read a numpy image from a MTLTexture, the syphon.utils.numpy.copy_mtl_texture_to_image() method can be used.

import numpy as np
from syphon.utils.numpy import copy_mtl_texture_to_image

texture = ...  # MLTTexture object

texture_data = copy_mtl_texture_to_image(texture)  # returns numpy array

Python Binding

As described in the Objective-C to Python chapter, the syphon-python library is based on the PyObjC Python to Objective-C bridge. This means that there is no intermediate wrapper between Python and Objective-C, and it is possible to access and call Objective-C objects directly from Python. This can be useful if a method of the original Syphon framework has not yet been exposed by the wrapper.

Access Objective-C Objects

To access the raw Objective-C object of a syphon.server.SyphonMetalServer, it is possible to access the syphon.server.SyphonMetalServer.context variable. To get a list of methods that can be called, the dir() method can be used.

server = syphon.SyphonMetalServer("Demo")
objc_syphon_metal_server = server.context

print(dir(objc_syphon_metal_server))

Raw Pointers to Python Objective-C Objects

The framework expects PyObjC pointers to be passed to the methods. Sometimes only raw ctype pointers are available. This example shows how to cast a nanogui MTLTexture pointer to a PyObjC object.

import ctypes
from typing import Any

import objc
from nanogui import Screen


def get_mtl_texture(texture: Any) -> Any:
    ctypes.pythonapi.PyCapsule_GetName.restype = ctypes.c_char_p
    ctypes.pythonapi.PyCapsule_GetName.argtypes = [ctypes.py_object]
    capsule_name = ctypes.pythonapi.PyCapsule_GetName(texture)

    ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p
    ctypes.pythonapi.PyCapsule_GetPointer.argtypes = [ctypes.py_object, ctypes.c_char_p]
    result = ctypes.pythonapi.PyCapsule_GetPointer(texture, capsule_name)

    mtl_texture = objc.objc_object(c_void_p=result)
    return mtl_texture


class SimpleServerScreen(Screen):
    ...

    def send(self):
        texture = self.metal_texture()  # of type PyCapsule
        texture_pointer = get_mtl_texture(texture)
        self.syphon_server.publish_frame_texture(texture_pointer, is_flipped=True)

Frame Notifications

When receiving frames, an application can check has_new_frame in its rendering loop. If the application does not already have such a loop, a callback can be used to notify it when a frame arrives. Both Metal and OpenGL clients accept a new_frame_handler which receives the client as its argument.

The following example uses a Python event to signal the application that a new frame is available.

import threading

available = threading.Event()

with syphon.SyphonMetalClient(description, new_frame_handler=lambda client: available.set()) as client:
    # wait for a notification, then retrieve the latest texture
    if available.wait(timeout=5):
        available.clear()
        texture = client.new_frame_image

Syphon calls the handler on a background thread. Keep the handler short and perform UI or OpenGL work on the application thread. A client must also be stopped outside this callback to avoid blocking Syphon's own notification thread.

For applications using asyncio, callback_loop=asyncio.get_running_loop() can be passed to deliver the handler on that event loop instead. The handler is still a regular Python function; it can use asyncio.create_task() to start asynchronous work.

Waiting with Asyncio

If an application already uses asyncio, wait_for_frame() allows it to wait for an image while other tasks continue running. The method waits for availability; the image is then retrieved separately through new_frame_image.

async def receive_frame(description):
    with syphon.SyphonMetalClient(description) as client:
        # wait up to five seconds for a frame
        await client.wait_for_frame(timeout=5)
        texture = client.new_frame_image
        return texture

A timeout raises asyncio.TimeoutError, and a disconnected server raises ConnectionError. Syphon provides the latest frame, so a slow receiver may skip intermediate frames. Waiting does not create a queue of images.

For complete examples, see MetalCallbackExample and MetalAsyncExample. The asyncio example also shows how to process Cocoa events for server discovery without a UI.

Private Servers

By default, Syphon servers appear in the shared directory so that other applications can find them. If an output is only intended for a specific client, is_private=True can be used to hide it from discovery. The client then connects using the server's description directly.

with syphon.SyphonMetalServer("Internal", is_private=True) as server:
    # connect directly without searching the shared directory
    with syphon.SyphonMetalClient(server.server_description, server.device) as client:
        ...

This works for both Metal and OpenGL servers. A private server is hidden, but is not protected by authentication. When passing its description to another process, preserve the complete server_description.raw dictionary. It can be converted back using SyphonServerDescription.from_native(raw).

Changing the Server Name

An application may want to rename an output when its content changes, for example when switching from a camera feed to a preview. Assigning server.name updates the running server and its name in discovery.

server.name = "Camera Preview"

Accessing the Server Output

To preview a server's output in the same application, server.new_frame_image can be used without creating an additional client. It returns a Metal texture or an OpenGL image, depending on the server.

# retrieve the current output after publishing a frame
image = server.new_frame_image
if image is not None:
    ...  # display or inspect the image

The result may be None before the first frame is published. Keep the image alive while using it and release Python references when finished; PyObjC handles native memory management. The image represents the current output rather than a saved copy of an earlier frame.

See PrivateMetalExample for a complete example of private connections, renaming, and output access.

Direct OpenGL Rendering

If an application already renders with OpenGL, it can draw directly into Syphon's framebuffer instead of creating a separate texture to publish. bind_to_draw_frame() selects that framebuffer, and unbind_and_publish() publishes the result after drawing.

The following example assumes an OpenGL context is already current.

with syphon.SyphonOpenGLServer("Direct output", depth_buffer_resolution=24) as server:
    if server.bind_to_draw_frame((640, 480)):
        try:
            ...  # draw the scene using OpenGL
        finally:
            # publish the frame and restore the framebuffer binding
            server.unbind_and_publish()

For scenes that need smoother edges or depth and stencil buffers, the server accepts antialias_sample_count, depth_buffer_resolution, and stencil_buffer_resolution. These options are disabled by default; the driver chooses a supported configuration. Supported parameter values are listed in syphon.server.SyphonOpenGLServer.

Only unbind after a successful bind, and do both on the same thread. The OpenGL context must remain alive and available exclusively to that thread during drawing. Unbind before stopping the server.

See OpenGLDirectRenderingExample for a complete example which creates its own context.

Resource Cleanup

Clients and servers hold native resources while they are running. A with statement is a convenient way to release them when leaving a block, including when an exception occurs.

with syphon.SyphonMetalServer("Demo") as server:
    ...  # create and publish textures

# the server has now been stopped

The same pattern works for clients. Calling stop() explicitly is also supported, and calling it more than once is harmless.

Removing Directory Observers

An application may only need discovery notifications while a server-selection view is open. add_observer() returns a token which can be used to remove that subscription when it is no longer needed.

with syphon.SyphonServerDirectory() as directory:
    # listen for newly available servers
    token = directory.add_observer(syphon.SyphonServerNotification.Announce, print)
    directory.update_run_loop()

    # stop receiving this notification
    directory.remove_observer(token)

Leaving the with block removes any remaining observers registered through that directory wrapper. Alternatively, call directory.close() explicitly. This does not stop Syphon's shared directory or remove another wrapper's observers.

 1"""
 2.. include:: ../README.md
 3.. include:: ../DOCUMENTATION.md
 4"""
 5
 6from pathlib import Path
 7
 8import objc
 9
10_SYPHON_LIBS_PATH = Path(__file__).parent.joinpath("libs")
11
12
13def _load_lib_bundle(bundle_name: str, scan_classes: bool = False):
14    """
15    Load a dynamic library bundle using Objective-C.
16
17    Parameters:
18    - bundle_name (str): The name of the bundle to load.
19    - scan_classes (bool, optional): If True, scan classes in the bundle. Defaults to False.
20    """
21    framework_path = _SYPHON_LIBS_PATH.joinpath(f"{bundle_name}.framework")
22    objc.loadBundle(f"{bundle_name}", globals(), bundle_path=str(framework_path), scan_classes=scan_classes)
23
24
25# initialize syphon bundle
26_load_lib_bundle("Syphon")
27
28from syphon.client import (
29    BaseSyphonClient as BaseSyphonClient,
30    SyphonMetalClient as SyphonMetalClient,
31    SyphonOpenGLClient as SyphonOpenGLClient,
32)
33from syphon.server import (
34    BaseSyphonServer as BaseSyphonServer,
35    SyphonMetalServer as SyphonMetalServer,
36    SyphonOpenGLServer as SyphonOpenGLServer,
37)
38from syphon.server_directory import (
39    SyphonServerDescription as SyphonServerDescription,
40    SyphonServerDirectory as SyphonServerDirectory,
41    SyphonServerNotification as SyphonServerNotification,
42)