0

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
Community
  • 1
  • 1

1 Answers1

0

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)
      }
 }
rickster
  • 124,678
  • 26
  • 272
  • 326
  • I see, thank you for the explanation, it has deepened my comprehension. I am no longer getting the error. For some reason though my asset isn't loading, all I see is my grey background. It's puzzling, I managed to get it to display when I had it operating as a scene. Thank you for your help so far though! Definitely fixed the problem I had at the time. – Jamie Draper Apr 05 '15 at 20:38
  • Just figured it out, my issue was the node name I was using to search with. I ended up getting the child node name using a println. – Jamie Draper Apr 05 '15 at 21:10