Home > Blockchain >  Efficient Node Enumeration, SceneKit SWIFT
Efficient Node Enumeration, SceneKit SWIFT

Time:02-14

I have a very large tree structure of nodes for some of my animation models. As such, the below code can sometimes filter through 1000 nodes. If I'm doing that quickly, it can overwhelm my app. Is there a way to confine the rootNode search to only the top level or two and not the entire tree structure?

self.sceneView.scene.rootNode.enumerateChildNodes { (node, stop) in
            if node.name == "myRootNode" {
         //This is the node to manipulate
      }

}

CodePudding user response:

If you want to search just the top level use func childNode(withName name: String, recursively: Bool). If you want to search two levels:

if let node = self.sceneView.scene.rootNode.childNode(withName: "myRootNode", recursively: false) {
  // Do whatever you need to do
} else {
  for node in self.sceneView.scene.rootNode.children {
     if let node = node.childNode(withName: "myRootNode", recursively: false) {
         // Do whatever you need to do
         break
     }
  }
}

CodePudding user response:

This here is an example of how to filter the rootNode (the node containing all the nodes from your scene setup). It will filter the rootNode only.

You can run the same filter method on any childNode (just as you need)

sceneView.scene?.rootNode.childNodes.filter({ $0.name == "yourNodeName" }).forEach({                
    // $0 is your filtered node
    print($0.name)                 
})
  • Related