load a collada (dae) file into SCNNode (Swift - SceneKit)
Asked Answered
N

4

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)

Nit answered 19/8, 2014 at 15:41 Comment(3)
possible duplicate of How do you load a .dae file into an SCNNode in IOS SceneKit?Pickaninny
maybe. here it was answered correctly tho. (load an ENTIRE dae file, not just one ID from it)Nit
regarding this decade-old question, i fear the ONLY way to do this now, is as explained here: https://mcmap.net/q/245562/-how-do-you-load-a-dae-file-into-an-scnnode-in-ios-scenekit . Just glance in the file (with a text editor) and then it's trivial and smooth.Potman
A
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()
Arun answered 19/8, 2014 at 18:2 Comment(0)
I
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!)
   }
}
Inflectional answered 6/9, 2016 at 23:22 Comment(1)
The best answer!Strenuous
C
6

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")
Clouse answered 4/5, 2018 at 4:39 Comment(0)
S
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.

Spermaceti answered 19/8, 2014 at 15:43 Comment(1)
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.Pickaninny

© 2022 - 2024 — McMap. All rights reserved.