PyObjC: MetalPerformanceShaders Framework
PyObjC is a bridge between Python and Objective-C that allows Python applications to use Objective-C frameworks and objects. `pyobjc-framework-metalperformanceshaders` provides Python bindings for Apple's MetalPerformanceShaders framework on macOS, offering highly optimized GPU-accelerated functions for tasks like image processing, machine learning, and physics simulations. The library is actively maintained, with version 12.1 being the latest as of November 2025, and receives frequent updates tied to new macOS SDK releases.
Warnings
- breaking `pyobjc-framework-metalperformanceshaders` version 12.0 dropped support for Python 3.9. Version 11.0 dropped support for Python 3.8. Ensure your Python version meets the `pyobjc` requirements (currently Python >=3.10).
- breaking PyObjC 10.3 introduced changes to how `__init__` and `__new__` methods are handled when subclassing Objective-C classes in Python. While a partial revert in 10.3.1 allowed `__init__` when a user-defined `__new__` exists, it can still break code relying on the old behavior where `__init__` was called regardless. The recommended pattern is to use `SomeClass.alloc().init()` or the more Pythonic `SomeClass(...)` (introduced in PyObjC 10.3) for object initialization.
- gotcha PyObjC framework bindings, including `pyobjc-framework-metalperformanceshaders`, do not include their own documentation. Users must refer to Apple's official Objective-C and Swift documentation for the MetalPerformanceShaders framework to understand the available APIs and their usage.
- gotcha Installing PyObjC from source (e.g., using `pip install --no-binary :all: pyobjc`) requires a full Xcode installation or the Xcode Command Line Tools to be present, along with a sufficiently new macOS SDK. Building with an older SDK can lead to compilation errors due to missing protocol declarations or other API changes.
- deprecated The `isAlloc` attribute of `objc.selector` is deprecated and will be removed in PyObjC 12. While this change is primarily internal to the bridge, users with highly specialized code that directly inspects `objc.selector` attributes should be aware.
Install
-
pip install pyobjc-framework-metalperformanceshaders
Imports
- MetalPerformanceShaders
import MetalPerformanceShaders
Quickstart
import objc
from Foundation import NSObject # Often implicitly imported or used by other frameworks
import MetalPerformanceShaders
# Accessing a basic class from the framework
# MPSKernel is a base class for many Metal Performance Shaders operations.
try:
# Simply accessing the class confirms the import and binding are working.
_ = MetalPerformanceShaders.MPSKernel
print("Successfully imported MetalPerformanceShaders and accessed MPSKernel.")
# Note: Full usage of MetalPerformanceShaders (e.g., creating a blur filter,
# running a neural network, or performing ray tracing) is complex.
# It requires setting up a Metal device, command queue, and textures.
# For example:
# from Metal import MTLCreateSystemDefaultDevice
# device = MTLCreateSystemDefaultDevice()
# commandQueue = device.newCommandQueue()
# # ... then create MPSImageGaussianBlur, encode to command buffer, etc.
print("Note: Actual usage of MetalPerformanceShaders requires a Metal context (device, command queue, textures). Refer to Apple's Metal Performance Shaders documentation for details.")
except objc.nosuchmethod_error as e:
print(f"Error accessing MetalPerformanceShaders.MPSKernel: {e}")
print("This might indicate an issue with the framework bindings or an incompatible macOS version.")
except ImportError as e:
print(f"Error importing MetalPerformanceShaders: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")