3

I use following code, it will check the touch point and it will add the object if the point is empty or delete the object.

@objc func didTap(withGestureRecognizer recognizer: UIGestureRecognizer) {

        let tapLocation = recognizer.location(in: sceneView)
        let hitTestResults = sceneView.hitTest(tapLocation)

        guard let node = hitTestResults.first?.node else {

            let hitTestResultsWithFeaturePoints = sceneView.hitTest(tapLocation, types: .featurePoint)

            if let hitTestResultWithFeaturePoints = hitTestResultsWithFeaturePoints.first {

                let translation = hitTestResultWithFeaturePoints.worldTransform.translation

                guard let carScene = SCNScene(named: "car.dae") else { return }
                let carNode = SCNNode()
                let carSceneChildNodes = carScene.rootNode.childNodes
                for childNode in carSceneChildNodes {
                    carNode.addChildNode(childNode)
                }
                carNode.position = SCNVector3(translation.x, translation.y, translation.z)
                carNode.scale = SCNVector3(0.5, 0.5, 0.5)
                sceneView.scene.rootNode.addChildNode(carNode)
            }
            return

        }
    node.removeFromParentNode()
}

But my object is create by DAE file, it include lot of childNodes. if i use node.removeFromParentNode() it will only remove one node

if i use following code it will remove all of object on the screen.

sceneView.scene.rootNode.enumerateChildNodes { (existingNode, _) in
        existingNode.removeFromParentNode()
    }

How can I remove specific nodes from a scenekit scene?

2 Answers 2

3

You should name your nodes then you can use the name to filter them out.

sceneView.scene.rootNode.childNodes.filter({ $0.name == "yourName" }).forEach({ $0.removeFromParentNode() })
7
  • thanks for response, but it doesn't work and show "Extra argument 'withName' in call" error message.
    – Peggy
    Jan 3, 2019 at 1:45
  • Right sorry i confused SpriteKit with SceneKit. Check edited answer.
    – Arie Pinto
    Jan 3, 2019 at 1:50
  • i try to use sceneView.scene.rootNode.childNodes.filter({ $0.name == "car.dae" }).forEach({ $0.removeFromParentNode() }) , but it doesn't work. should i change didTap func?
    – Peggy
    Jan 3, 2019 at 2:06
  • Well that wouldn't work because car.dae is an SCNScene not a node, so try declaring carScene outside the didTap func so you can filter it's childNodes.
    – Arie Pinto
    Jan 3, 2019 at 9:06
  • i sorry i don't have any idea about declaring carScene outside the didTap func.how should i do?
    – Peggy
    Jan 4, 2019 at 6:15
2

You can use:

func childNode(withName name: String, 
   recursively: Bool) -> SCNNode?

Head over to docs, https://developer.apple.com/documentation/scenekit/scnnode/1407951-childnode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.