TokyoToo
TokyoToo

Reputation: 926

SCNScene - fatal error calling SCNScene(named:): Thread 1: EXC_BAD_INSTRUCTION

I'm using a shared project and have spent a lot of time updated it to Swift 2 so I'd like to get this working but unfortunately I have a runtime error to contend with. The storyboard has nothing on it so everything is done programatically. Seeing that it's done programatically I'm not sure why I'm getting this runtime error.

Please tell me what "glasses1.dae" and "glasses2" are in this code. What identifiers are these?

function signature specialization of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

Thread 1: EXC_BAD_INSTRUCTION ...

class ViewController: UIViewController {
private let notificationCenter : NSNotificationCenter = NSNotificationCenter.defaultCenter()

private let screenWidth : CGFloat = 320
private let scaleX : CGFloat = (320 / 750)
private let scaleY : CGFloat = (568 / 1334)

private let eyeRectL : UILabel = UILabel()
private let eyeRectR : UILabel = UILabel()

private var scnView : SCNView!
private var glasses : SCNNode!

override func viewDidLoad() {
    super.viewDidLoad()

    // Scene
    let scene = SCNScene(named: "glasses1.dae")! // fatal error: unexpectedly found nil while unwrapping an Optional value 

    // Camera
    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 0)
    scene.rootNode.addChildNode(cameraNode)

    // Light
    let lightNode = SCNNode()
    lightNode.light = SCNLight()
    lightNode.light!.type = SCNLightTypeOmni
    lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
    scene.rootNode.addChildNode(lightNode)

    // Ambient Light
    let ambientLightNode = SCNNode()
    ambientLightNode.light = SCNLight()
    ambientLightNode.light!.type = SCNLightTypeAmbient
    ambientLightNode.light!.color = UIColor.darkGrayColor()
    scene.rootNode.addChildNode(ambientLightNode)

    glasses = scene.rootNode.childNodeWithName("glasses2", recursively: true)! // what is "glasses2"?

    scnView =  SCNView()
    scnView.scene = scene
    scnView.backgroundColor = UIColor.clearColor()
    scnView.frame = self.view.bounds

    let moohaha = setupMoohaha()
    let cameraView = visage.moohahaCameraView

    self.view.addSubview(cameraView)
    self.view.addSubview(scnView)

    self.view.addSubview(eyeRectL)
    self.view.addSubview(eyeRectR)

    moohaha.beginFaceDetection()
}

Upvotes: 1

Views: 1069

Answers (1)

Charles A.
Charles A.

Reputation: 11123

A .dae file contains scene information and is used to load a scene created externally (not in code). That "glasses1.dae" string is the name of a file that should be part of your project. Presumably the childWithNodeName(_:recursively:) method call is used to find a child node with the name "glasses2" that is created as part of the scene defined in the .dea file used to create the scene.

The ! operator at the end of the two lines you are indicating is used to force unwrap an optional value, and it will crash the program if the value contains nil. As such, I assume that your scene is not being created because the "glasses.dea" file is not actually part of your project and SceneKit is failing to load it and returning a nil scene.

Upvotes: 3

Related Questions