How do you convert Wavefront OBJ file to an SCNNode with Model I/O

Jason Leach picture Jason Leach · Jan 8, 2016 · Viewed 7.5k times · Source

I've imported a Wavefront OBJ file from a URL and now I'd like to insert it into my scene (SceneKit) on my iOS 9 app (in Swift). What I've done so far is:

let asset = MDLAsset(URL: localFileUrl)
print("count = \(asset.count)")  // 1

Any help converting this to a SCNNode would be appreciated. According to to Apple's docs:

Model I/O can share data buffers with the MetalKit, GLKit, and SceneKit frameworks to help you load, process, and render 3D assets efficiently.

But I'm not sure how to get buffer from an MDLAsset into a SCNNode.

Answer

Jason Leach picture Jason Leach · Jan 11, 2016

Turns out this quite easy as many of the ModelIO classes already bridge. I was doing import ModelIO which gave me access to all the ModelIO classes and likewise import SceneKit which gave me the SceneKit classes, but, I was missing import SceneKit.ModelIO to bring in the SceneKit support for ModelIO.

let url = NSURL(string: "url-to-your-obj-here")
let asset = MDLAsset(URL: url!)
let object = asset.objectAtIndex(0)
let node = SCNNode(MDLObject: object)

Easy as that...