How do you load a .dae file into an SCNNode in iOS SceneKit?

M-V picture M-V · Aug 18, 2014 · Viewed 24.7k times · Source

I am having trouble understanding how geometry from .dae files should be loaded into an iOS SceneKit scene graph.

I know that you can load .dae files directly into an SCNScene:

// create a new scene
let scene = SCNScene("test.scnassets/test.dae")

I also know that you can create an empty SCNScene and add built-in 3D objects into it as child nodes:

let scene = SCNScene()
var sphereNode = SCNNode()
sphereNode.geometry = SCNSphere(radius: 1.0)
scene.rootNode.addChildNode(sphereNode)

In the second case, can you load a .dae file into SCNNode instead of using built in objects? Or is it expected that all .dae files need to be loaded into the SCNScene first?

If the latter is true, how then do you place the SCNNode objects into the right place in the scene graph hierarchy under SCNScene?

EDIT #1:

This is working for me now, which is modified version of what @FlippinFun suggested.

let url = NSBundle.mainBundle().URLForResource("test.scnassets/test", withExtension: "dae")
let source = SCNSceneSource(URL: url, options: nil)
let block = source.entryWithIdentifier("ID10", withClass: SCNGeometry.self) as SCNGeometry
scene.rootNode.addChildNode(SCNNode(geometry: block))

But perhaps there's a better way? The above involves manually loading each identifier from the .dae tree. In my file, there's a node "SketchUp" with child identifiers "ID2" and "ID10". This method will not let you load the root "SketchUp". (I get a runtime error.)

Answer

Toyos picture Toyos · Aug 18, 2014

The usual pattern is to load your scene and retrieve the node you are interested in with its name using

[scene.rootNode childNodeWithName:aName recursively:YES];

Then you can reparent this node to your main scene.