PyObjC: MetalPerformanceShaders Framework

12.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates how to import the `MetalPerformanceShaders` framework through PyObjC and access a basic class like `MPSKernel`. Real-world usage of this framework is highly dependent on Apple's Metal API, requiring a Metal device, command queue, and texture management for actual GPU-accelerated operations.

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}")

view raw JSON →