Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Scene is modified within a rendering callback of another scene." How to fix this error?

I am using ARKit image tracking configuration, once an image is detected, a 3D scene would pop up on the image.

But when I set two different images to trigger two different scene file, one image always has two different scene files pop up at the same image. I am pretty sure the images are different, the names are different, the scene file are different, the contents of scene are different as well.

Once the image is detected, below error pops up in the console as well:

[SceneKit] Error: Scene <SCNScene: 0x284ebcfa0> is modified within a rendering callback of another scene (<SCNScene: 0x28099c820>). This is not allowed and may lead to crash

Any reason and solution for this error?

like image 930
Tinloy Avatar asked Aug 01 '18 01:08

Tinloy


2 Answers

I had the same error with ARKit 2 in the image tracking. And after hours trying, I found the solution. Apparently you need to create your nodes in the background thread to be able to play with the scenes. This was my code:

DispatchQueue.main.async {
        if let imageAnchor = anchor as? ARImageAnchor {

            let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
            plane.firstMaterial?.diffuse.contents = UIColor(white: 1.0, alpha: 0.5)

            let planeNode = SCNNode(geometry: plane)
            planeNode.eulerAngles.x = -.pi
            node.addChildNode(planeNode)

            ...
        }
    }
like image 146
Diego Isco Avatar answered Sep 22 '22 21:09

Diego Isco


If you code like below, the error pops up.

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
    guard let imageAnchor = anchor as? ARImageAnchor else {
        return nil
    let buttonNode = SCNScene(named: "art.scnassets/social_buttons.scn")!.rootNode.childNode(withName: "card", recursively: false)
}

This is because you call new SCNScene (init new SCNScene) in renderer method.

Init SCNScene in viewDidLoad or other place. If so, the error might disappear.

like image 23
Kei Fujikawa Avatar answered Sep 21 '22 21:09

Kei Fujikawa