I need to import 3D model from server URL but it's not working properly. Here is my code:
guard let path = modelPath, !path.isEmpty else {
fatalError("Failed to find model file path.")
}
guard let modelURL = URL(string: path) else {
fatalError("Failed to find model URL.")
}
let asset = MDLAsset(url:modelURL)
guard let object = asset.object(at: 0) as? MDLMesh else {
fatalError("Failed to get mesh from asset.")
}
...crash here at object.
MDLAsset(url:)does not handle downloading models from a server, it's only forURLs that point to local storage.You will have to download it yourself (using
URLSessionor a framework like Alamofire).Example using
URLSession:Download task will return temporary location for the file that will be deleted after the callback closure return so if you need to reuse the file you will have to re-save it somewhere.
The
tempLocationfile will have an extension of.tmp, whichMDLAssetwill not be able to process. Even if you don't need to persist the file, I didn't come up with a better way than to re-save it with the needed extension (.objthat is).