3dscenekitscnnode

Converted OBJ to SCN and now the object won't load


let pathToBalloon = Bundle.main.path(forResource: "./Balloon2", ofType: "scn")!
    let balloonAsset = MDLAsset(url: URL(fileURLWithPath: pathToBalloon))
    let balloonObject: MDLObject? = balloonAsset.object(at: 0)
    let balloon = SCNNode(mdlObject: balloonObject!) // Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Using an .obj file with a balloon works great and displays in SCNView just fine. To use extra features Xcode asked me to convert to a .scn format. The model loads just fine in the Xcode editor, however balloonAsset.object(at: 0) now returns nil.

I really wanted to smooth the geometry and other stuff so continuing to use .obj files is meh.

What could the issue be?


Solution

  • Model I/O does not support opening SceneKit files (see file formats supported by the Model I/O).

    Instead you should use SceneKit's +[SCNScene sceneNamed:] or +sceneWithURL:options:error:.