PyObjC SceneKit Framework

12.1 · active · verified Tue Apr 14

PyObjC is the bridge between Python and the Objective-C programming language, used to develop applications for macOS. This specific package, `pyobjc-framework-scenekit` (currently at version 12.1), provides Python wrappers for Apple's SceneKit framework, enabling 3D graphics and game development. PyObjC generally follows the macOS SDK release cycle for updates.

Warnings

Install

Imports

Quickstart

This quickstart code initializes a basic macOS application window and embeds an SCNView. It then sets up a SceneKit scene with a camera, lights, and a simple rotating cube, allowing user camera control. The `AppHelper.runEventLoop()` ensures the Cocoa application event loop is started and managed.

import SceneKit
from Cocoa import NSApplication, NSWindow, NSView, NSRect, NSMakeRect
from PyObjCTools import AppHelper
import objc

class AppDelegate(objc.NSObject):
    def applicationDidFinishLaunching_(self, aNotification):
        print("SceneKit app launched!")

        # Create a window
        rect = NSMakeRect(100, 100, 640, 480)
        self.window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
            rect, 
            (1 << 0) | (1 << 1) | (1 << 2), # NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable
            2, # NSBackingStoreBuffered
            False
        )
        self.window.setTitle_("PyObjC SceneKit Cube")
        
        # Create an SCNView (SceneKit view)
        self.scnView = SceneKit.SCNView.alloc().initWithFrame_(rect)
        self.window.setContentView_(self.scnView)

        # Create a scene
        scene = SceneKit.SCNScene.scene()
        self.scnView.setScene_(scene)

        # Create a camera and add it to a node
        camera = SceneKit.SCNCamera.camera()
        cameraNode = SceneKit.SCNNode.node()
        cameraNode.setCamera_(camera)
        cameraNode.setPosition_(SceneKit.SCNVector3(0, 0, 15))
        scene.rootNode().addChildNode_(cameraNode)
        self.scnView.setPointOfView_(cameraNode)

        # Create a light and add it to a node
        light = SceneKit.SCNLight.light()
        light.setType_(SceneKit.SCNLightTypeOmni)
        lightNode = SceneKit.SCNNode.node()
        lightNode.setLight_(light)
        lightNode.setPosition_(SceneKit.SCNVector3(0, 10, 10))
        scene.rootNode().addChildNode_(lightNode)

        # Create an ambient light
        ambientLight = SceneKit.SCNLight.light()
        ambientLight.setType_(SceneKit.SCNLightTypeAmbient)
        ambientLight.setColor_(SceneKit.SCNColor.colorWithRed_green_blue_alpha_(0.1, 0.1, 0.1, 1.0))
        ambientLightNode = SceneKit.SCNNode.node()
        ambientLightNode.setLight_(ambientLight)
        scene.rootNode().addChildNode_(ambientLightNode)

        # Create a cube geometry
        box = SceneKit.SCNBox.boxWithWidth_height_length_chamferRadius_(5.0, 5.0, 5.0, 0.5)
        boxNode = SceneKit.SCNNode.nodeWithGeometry_(box)
        scene.rootNode().addChildNode_(boxNode)
        
        # Allow the user to manipulate the camera
        self.scnView.setAllowsCameraControl_(True)

        self.window.makeKeyAndOrderFront_(None)

def main():
    app = NSApplication.sharedApplication()
    delegate = AppDelegate.alloc().init()
    app.setDelegate_(delegate)
    AppHelper.runEventLoop()

if __name__ == '__main__':
    main()

view raw JSON →