{"library":"pyobjc-framework-metalkit","title":"PyObjC: MetalKit Framework","description":"PyObjC provides Pythonic bindings to macOS Cocoa frameworks, enabling Python applications to interact with native macOS APIs. `pyobjc-framework-metalkit` specifically offers wrappers for the MetalKit framework, simplifying the use of Apple's low-overhead 3D graphics API (Metal) from Python. The current version is 12.1, and PyObjC maintains a regular release cadence, often aligning with macOS SDK updates and Python version support.","language":"python","status":"active","last_verified":"Sat May 16","install":{"commands":["pip install pyobjc-framework-metalkit"],"cli":null},"imports":["from MetalKit import MTKView","from Metal import MTLCreateSystemDefaultDevice","from PyObjCTools import AppHelper"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import objc\nfrom AppKit import NSApplication, NSWindow, NSView, NSApp, NSMakeRect, NSBackingStoreBuffered, NSWindowStyleMaskTitled, NSWindowStyleMaskClosable\nfrom MetalKit import MTKView\nfrom Metal import MTLCreateSystemDefaultDevice, MTLPixelFormatBGRA8Unorm\nfrom PyObjCTools import AppHelper\n\n# Define an MTKViewDelegate for basic rendering\nclass PyMetalViewDelegate(objc.NSObject):\n    def drawInMTKView_(self, view):\n        # In a real app, you'd perform complex Metal rendering here.\n        # This example simply clears the view to a solid color.\n        commandQueue = view.device().newCommandQueue()\n        commandBuffer = commandQueue.commandBuffer()\n        renderPassDescriptor = view.currentRenderPassDescriptor()\n        \n        if renderPassDescriptor:\n            # Set clear color and load action\n            renderPassDescriptor.colorAttachments().objectAtIndex_(0).setClearColor_(\n                objc.runtime.struct(red=0.1, green=0.3, blue=0.5, alpha=1.0, _pyobjc_structs_type_name=\"MTLClearColor\")\n            )\n            renderPassDescriptor.colorAttachments().objectAtIndex_(0).setLoadAction_(0) # MTLLoadActionClear\n            \n            renderEncoder = commandBuffer.renderCommandEncoderWithDescriptor_(renderPassDescriptor)\n            renderEncoder.endEncoding()\n            commandBuffer.presentDrawable_(view.currentDrawable())\n            commandBuffer.commit()\n\n    def mtkView_drawableSizeWillChange_(self, view, size):\n        # Respond to view size changes if needed for Metal resources\n        pass\n\nclass PyMetalAppDelegate(objc.NSObject):\n    def applicationDidFinishLaunching_(self, notification):\n        device = MTLCreateSystemDefaultDevice()\n        if device is None:\n            print(\"Metal is not supported on this device.\")\n            NSApp().terminate_(self)\n            return\n\n        window_rect = NSMakeRect(0, 0, 640, 480)\n        window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(\n            window_rect,\n            NSWindowStyleMaskTitled | NSWindowStyleMaskClosable,\n            NSBackingStoreBuffered,\n            False\n        )\n        window.setTitle_(\"PyObjC MetalKit Quickstart\")\n        window.makeKeyAndOrderFront_(None)\n\n        mtk_view = MTKView.alloc().initWithFrame_device_(window_rect, device)\n        mtk_view.setColorPixelFormat_(MTLPixelFormatBGRA8Unorm) # Using enum value\n        mtk_view.setClearColor_(objc.runtime.struct(red=0.2, green=0.4, blue=0.6, alpha=1.0, _pyobjc_structs_type_name=\"MTLClearColor\"))\n        mtk_view.setPaused_(False) # Ensure view is not paused\n        mtk_view.setEnableSetNeedsDisplay_(False) # Allow continuous drawing\n\n        # Set the delegate for rendering\n        self.metal_delegate = PyMetalViewDelegate.alloc().init()\n        mtk_view.setDelegate_(self.metal_delegate)\n\n        window.contentView().addSubview_(mtk_view)\n        self.window = window\n        self.mtk_view = mtk_view\n        print(f\"MTKView created with device: {device.name()}\")\n        print(\"Close the window to terminate the application.\")\n\nif __name__ == '__main__':\n    # AppHelper.runEventLoop simplifies running a Cocoa application by managing NSApplication and its lifecycle.\n    AppHelper.runEventLoop(mainLoop=PyMetalAppDelegate.alloc().init())\n","lang":"python","description":"This quickstart demonstrates how to initialize a Metal device, create an `MTKView`, and set up a basic rendering delegate using PyObjC. It showcases the integration of MetalKit with a minimal Cocoa application structure. Run this on macOS to see a window displaying a continuously cleared MetalKIt view. Remember to close the window to terminate the Python script.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-16","installed_version":null,"pypi_latest":"12.1","is_stale":null,"summary":{"python_range":"3.10–3.9","success_rate":0,"avg_install_s":null,"avg_import_s":null,"wheel_type":null},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"pyobjc-framework-metalkit","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"pyobjc-framework-metalkit","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":2.9,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"pyobjc-framework-metalkit","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"pyobjc-framework-metalkit","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":3,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"pyobjc-framework-metalkit","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"pyobjc-framework-metalkit","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":2.9,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"pyobjc-framework-metalkit","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"pyobjc-framework-metalkit","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":2.5,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"pyobjc-framework-metalkit","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":null},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"pyobjc-framework-metalkit","exit_code":1,"wheel_type":null,"failure_reason":"build_error","import_side_effects":null,"install_time_s":3.4,"import_time_s":null,"mem_mb":null,"disk_size":null}]}}