2

I'm trying to load an .obj file to create an MDLAsset object in a macOS Swift app. Originally, I was creating the asset like so:

   let myAsset = URL(fileURLWithPath: "/Users/me/Development/MyProject/MyApp/Assets.xcassets/arrow.dataset/arrow.obj")
    arrowMdl = MDLAsset(url:arrow).object(at: 0)

Obviously that won't work when the app is in production - so, based off this SO answer, I tried adding the .obj file to a bundle, and then load it, like so:

    let path: String = Bundle.main.path(forResource: "Arrow", ofType: "bundle")!
    do {
        let arrowPath = try String(contentsOfFile: path)
    }
    catch let error as NSError {
        print(error.description)
    }

However, I keep getting the following error:

Error Domain=NSCocoaErrorDomain Code=257 "The file “Arrow.bundle” couldn’t be opened because you don’t have permission to view it." 

I made sure to set the permissions to read/write for everyone.

What am I doing wrong? Or, is there another way to load this asset? It looks as though MDLAsset requires a URL to for it to be initialized: https://developer.apple.com/documentation/modelio/mdlasset

narner
  • 2,908
  • 3
  • 26
  • 63
  • 1
    Try `let path: String = Bundle.main.path(forResource: "arrow", ofType: "obj")` Note the case sensitivity and type of the resource name. – Don Dec 04 '17 at 20:08
  • @Don In that case, should I just be copying my .obj file directly into the Xcode project structure? – narner Dec 04 '17 at 20:11
  • 1
    @namer You could do that. I didn't notice that you were using an Asset Catalog. The way to get datasets from an asset catalog is through the [NSDataAsset](https://developer.apple.com/documentation/uikit/nsdataasset) class, but you can't get a URL, only the data. You could write a small extension that takes that data, saves it in a user-accessible location, then provide the URL to the new file. However, now you have 2 copies of the file when you only need one, so I'd recommend taking the .obj out of the asset catalog, yes. – Don Dec 04 '17 at 20:38
  • @narner how did you solve this? – Jubei Jun 12 '19 at 23:05

0 Answers0