10

This works

let scene = SCNScene(named: "house.dae")

Is there an equivalent for a node?

let node = SCNNode(geometry: SCNGeometry( ..??.. "house.dae" ..??.. ))

I have searched high and low, finding nothing that will load an entire dae file into a SCNNode. (not just one ID from it)

quemeful
  • 9,542
  • 4
  • 60
  • 69
  • 1
    possible duplicate of [How do you load a .dae file into an SCNNode in IOS SceneKit?](http://stackoverflow.com/questions/25356301/how-do-you-load-a-dae-file-into-an-scnnode-in-ios-scenekit) – rickster Aug 20 '14 at 00:36
  • maybe. here it was answered correctly tho. (load an ENTIRE dae file, not just one ID from it) – quemeful Aug 20 '14 at 11:28
  • regarding this decade-old question, i fear the ONLY way to do this now, is as explained here: https://stackoverflow.com/a/75088130/294884 . Just glance in the file (with a text editor) and then it's trivial and smooth. – Fattie Jan 11 '23 at 19:47

4 Answers4

13
// add a SCNScene as childNode to another SCNScene (in this case to scene)
func addSceneToScene() {
    let geoScene = SCNScene(named: "art.scnassets/ball.dae")
    scene.rootNode.addChildNode(geoScene.rootNode.childNodeWithName("Ball", recursively: true))
}
addSceneToScene()
user3957490
  • 146
  • 2
11

iOS 9 introduced the SCNReferenceNode class. Now you can do:

if let filePath = Bundle.main.path(forResource: "Test", ofType: "dae", inDirectory: "GameScene.scnassets") {
   // ReferenceNode path -> ReferenceNode URL
   let referenceURL = URL(fileURLWithPath: filePath)

   if #available(iOS 9.0, *) {
      // Create reference node
      let referenceNode = SCNReferenceNode(URL: referenceURL)
      referenceNode?.load()
      scene.rootNode.addChildNode(referenceNode!)
   }
}
Ratnesh Jain
  • 671
  • 7
  • 14
Crashalot
  • 33,605
  • 61
  • 269
  • 439
5

That's what I use in real project:

extension SCNNode {

    convenience init(named name: String) {
        self.init()

        guard let scene = SCNScene(named: name) else {
            return
        }

        for childNode in scene.rootNode.childNodes {
            addChildNode(childNode)
        }
    }

}

After that you can call:

let node = SCNNode(named: "art.scnassets/house.dae")
Denis Bystruev
  • 326
  • 4
  • 11
1

The scene you get from SCNScene(named:) has a rootNode property whose children are the contents of the DAE file you loaded. You should be able to pull children off that node and add them to other nodes in an existing SCNScene.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
  • 2
    Note that you can't move the `rootNode` of one scene into a different scene. You'll need to pull out a child node instead. – rickster Aug 19 '14 at 18:05