2
votes

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

2 Answers

1
votes

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() })
0
votes

You can use:

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

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