0
votes

I'm currently trying to import a dae file into a SCNNode that will then be added to a scene. I found some great stuff on here but I've hit a wall.

The answer I've been trying to implement was found here - Stackoverflow: load a collada (dae) file into SCNNode (Swift - SceneKit)

I've tried to implement the top solution but I get an error which says:

"Value of optional type 'SCNNode?' not unwrapped; did you mean to use '!' or '?'

It may be that I've overlooked something very fundamental, I am very much a novice.

I'll include my ViewController viewDidload code below, if anyone could shed some light on this I would be infinitely grateful!

let scnView = self.view as SCNView
let scene = MasterScene()
scnView.scene = scene
scnView.backgroundColor = UIColor.grayColor()

// enable default lighting
scnView.autoenablesDefaultLighting = true
// enable default camera
scnView.allowsCameraControl = true

var node = SCNNode()
let assetScene = SCNScene(named: "s185.dae")
scene.rootNode.addChildNode(assetScene?.rootNode.childNodeWithName("s185", recursively: true))
// Last line produces error
1
Which line produces the error? - nhgrif
Apologies, the last line of code, scene.rootNode...etc. - Jamie Draper

1 Answers

0
votes

Your assetScene is an optional. You seem to have got that part, since you're using optional chaining to access its rootNode.

However, any result produced by an expression that uses optional chaining is itself an optional — you don't know that assetScene exists, so you don't know if anything you tried to get out of it exists, either.

But addChildNode takes a non-optional SCNNode — you can't pass an optional to it. You need to unwrap that optional first. Even if you unwrap assetScene with an if let, you'll still get an optional, because you don't know if childNodeWithName found a node.

Try something like this:

 if let assetScene = SCNScene(named: "s185.dae") {
      if let node = assetScene.rootNode.childNodeWithName("s185", recursively: true) {
           scene.rootNode.addChildNode(node)
      }
 }