SceneKit load 3d model to subclass of NSView

Simple example showing how to load 3d object into SceneKit framework.
I have tested format: usdz, scn (Apple’s) and dea.
The result is different from whose format.

You could use your mouse/trackpad to manipulate the view.

If you look for models, get Blender (its free) or get the default model from Xcode.

Preview could view 3d model, but the advances is you could manipulate the Scene from AppleScript.

use framework "Foundation"
use framework "AppKit"
use framework "SceneKit"
use scripting additions

property arguments : missing value

on run
		if my NSThread's isMainThread() as boolean then
			my performDialog:arguments
			my performSelectorOnMainThread:"performDialog:" withObject:"arguments" waitUntilDone:true
		end if
	on error the errorMessage number the errorNumber
		set the ErrorText to "Error: " & the errorNumber & ". " & the errorMessage
		return the ErrorText
	end try
end run

on performDialog:arguments
	set format to {"usdz", "scn", "dae"}
	set thePath to POSIX path of (path to downloads folder) & "ship" & "." & item 1 of format
	set theScene to its sceneWithURL(thePath)
	set sceneRect to current application's NSMakeRect(0, 0, 1024, 720)
	set sceneView to current application's SCNView's alloc()'s initWithFrame:sceneRect options:(missing value)
	sceneView's setBackgroundColor:(current application's NSColor's darkGrayColor())
	sceneView's setScene:theScene
	* [A Boolean value that determines whether SceneKit automatically adds lights to a scene.]
	* 	Set this to false and you could get a nice mask layer for 3d object.
	sceneView's setAutoenablesDefaultLighting:true
	* [A Boolean value that determines whether the user can manipulate the current point of view that is used to render the scene.]
	*	So we could manipulate the view with a mouse/trackpad.
	sceneView's setAllowsCameraControl:true
	set sceneView to createView(0, 0, 1024, 720, {sceneView})
	set subviewItems to {sceneView}
	set theWindow to createWindowWithRectAndSubview(subviewItems, 0, 0, 1024, 720)
	theWindow's |center|()
	theWindow's makeKeyAndOrderFront:me
end performDialog:

on createView(x, y, width, height, subviews)
	set viewRectSize to current application's NSMakeRect(x, y, width, height)
	set theView to current application's NSView's alloc()'s initWithFrame:viewRectSize
	theView's setSubviews:subviews -- list
	return theView
end createView

on createWindowWithRectAndSubview(subviewItems, x, y, width, height)
	set windowSize to current application's NSMakeRect(x, y, width, height)
	set winStyle to (current application's NSWindowStyleMaskTitled as integer) + (current application's NSWindowStyleMaskClosable as integer) + (current application's NSWindowStyleMaskMiniaturizable as integer) + (current application's NSWindowStyleMaskResizable as integer)
	set theWindow to current application's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:true
	repeat with anSubview in subviewItems
		(theWindow's contentView()'s addSubview:anSubview)
	end repeat
	return theWindow
end createWindowWithRectAndSubview

on sceneWithURL(thePath)
	set theURL to current application's |NSURL|'s fileURLWithPath:thePath
	set {theScene, theError} to current application's SCNScene's sceneWithURL:theURL options:(missing value) |error|:(reference)
	if theScene is missing value then error (theError's localizedDescription() as string)
	return theScene
end sceneWithURL