Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import 3d model in SceneKit on iOS

I have a problem when importing a .obj file from a URL and convert it to a SCNNode

here's the code (swift3):

    let url = URL.init(string: "https://cloud.box.com/shared/static/ock9d81kakj91dz1x4ea.obj")
    let asset = MDLAsset(url: url! as URL)
    let object = asset.object(at: 0)
    let node = SCNNode(mdlObject: object)

but when i run the project, the console shows that:

Could not open OBJ file

how to deal with this?

like image 502
ZHAO Yida Avatar asked Oct 14 '16 08:10

ZHAO Yida


1 Answers

I think you are exceeding some internal iOS limit with your OBJ file. Please file a report at https://bugreport.apple.com.

This slightly modified version of your code works perfectly in a macOS playground (Xcode 8.0). But in an iOS playground, I see the same "Could not open OBJ file" in the console.

import SceneKit
import ModelIO
import SceneKit.ModelIO

if let url = URL.init(string: "https://cloud.box.com/shared/static/ock9d81kakj91dz1x4ea.obj") {
    let asset = MDLAsset(url: url)
    print(asset)
    let object = asset.object(at: 0)
    print(object)
    let node = SCNNode.init(mdlObject: object)
    print(node)
}

I was able to download and open the OBJ file with Xcode. Then within the scene editor, I converted it to SCN format. That gave me a .SCN file that could be embedded in the iOS project and opened with SCNScene (like the famous spinning spaceship). So if you can live with embedding a static file in your iOS app, that's a way to get your model in. But if you need dynamically loaded models, it won't work.

like image 112
Hal Mueller Avatar answered Oct 05 '22 17:10

Hal Mueller