2

Attempting to create a cocoapod that utilizes an .scnassets folder. The folder is imported into Resources without any problems with the children objects, but I can't find a way to load the .dae files into an SCNScene. From my understanding there may be a problem with how XCode converts .dae into .scn assets.

.podspec includes:

  s.resource_bundles = {
      'ARUtilsScenes' => ['ARUtils/Assets/ARScenes.scnassets']
   }

which properly loads the ARScenes.scnassets into Resources folder. I'm attempting to load a scene inside of my Pod (inside the pod and not a project that uses the pod, and have tried a variety of methods):

let arrowScene = SCNScene(named: "arrow.dae")

let arrowScene = SCNScene(named: "Resources/ARScenes.scnassets/arrow.dae")

let arrowScene = SCNScene(named: "arrow.dae", inDirectory: "Resources/ARScenes.scnassets", options: nil)

I've tried a variety of file/folder names, and am able to load images but I'm not able to get a .dae loaded as an SCNScene. Do I need to add .xib files to my resource_bundles, or is there some other way to ensure .scnassets folder properly compiles these .dae to .scn as well as make them available for loading into an SCNScene?

Z. Bagley
  • 8,942
  • 1
  • 40
  • 52
  • Is it possible to share your Pod somewhere? It's easier for others to try out in the same scenario. – Chinh Nguyen Jan 22 '19 at 06:58
  • I'll see what I can do (may not get to it until the weekend), but the general idea is just using `SCNScene` inside a CocoaPod from a `resource_bundle` item. Similar to how `UIImage` is imported by targeting the bundle: stackoverflow.com/a/35903720/856336, but with `SCNScene` (which doesn't have a bundle attribute, only a directory attribute) – Z. Bagley Jan 22 '19 at 14:44

3 Answers3

4

Suppose ZARKit is your framework distributed in CocoaPods.

.podspec includes:

s.resource_bundles = {
      'ARUtilsScenes' => ['ARUtils/Assets/ARScenes.scnassets']
   }

then you should do this

let bundle = Bundle(for: #Your Class#.self).
let resourcePath = bundle.path(forResource: <#T##String?#>, ofType: <#T##String?#>)
let resourceBundle = Bundle(path: resourcePath)

let url = resourceBundle.url(forResource: "ARScenes.scnassets/<#YourResourceName-arrow#>", withExtension: "<#scn#>")
let arrowScene = SCNScene(url: url)

edit: by Z. Bagley

Accepted answer, but this was the actual code I used:

        // find bundle based on existing class in pod
        let frameworkBundle = Bundle(for: ARUtilsClass.self) //ARUtilsClass is an empty class, Pod is only extensions for existing classes
        // append the scenes bundle, scnassets, and scn/dae
        let arrowURL = frameworkBundle.resourceURL?.appendingPathComponent("ARUtilsScenes.bundle/ARScenes.scnassets/newArrow.scn")
        // create a node in parent
        var arrow = SCNNode()
        do {
            // get arrow scene from bundle
            let bundleArrowScene = try SCNScene(url: (arrowURL)!, options: nil)
            // add arrow node from scene
            arrow = bundleArrowScene.rootNode.childNode(withName: "arrow", recursively: true)!
        } catch {
            print("failed to find arrow resource")
        }
Z. Bagley
  • 8,942
  • 1
  • 40
  • 52
Frank
  • 320
  • 1
  • 8
0

When accessing the .dae asset from the .scnassets folder, there should be no need to include the "Resources/" in the path

For example: let arrowScene = SCNScene(named: "ARScenes.scnassets/arrow.dae")

Assuming that your .scnassets folder is called that.

Codermonk
  • 883
  • 16
  • 32
  • That's true for a normal scenekit inclusion, but this is for a Cocoa Pod, which bundles these items prior to XCode's build time. I've tried every varient of paths, and this works fine in my main project, but I need to include this asset in the Cocoa Pod. – Z. Bagley Jan 21 '19 at 21:26
  • Oh, Of course, I misread - this Answer https://stackoverflow.com/a/35903720/856336 should be what you are looking for. Basically find the bundle of any of your classes in your Pod - then create the Bundle URL for the resource (which you defined in the podspec) - and use that Bundle when requesting the resource, using the format I mentioned above. – Codermonk Jan 22 '19 at 00:27
  • I wish that worked, but it only works for images (mentioned in the question there that I got this working for images but not .dae or .scn). Thanks for looking into that though. Has something to do with how bundles SceneKit scenes I'm guessing, since you can't directly target a bundle with `SCNScene` (similar to how you can with `UIImage` in that question) – Z. Bagley Jan 22 '19 at 14:35
0

There is a known bug introduced with updated build system by Apple which causes asynchronous compilation of asset catalogs making them unaccessible in the final build. For reference see this thread on Cocoapods GitHub. There are several workarounds for this listed, but none of them guarantee the results, so for now we seem to have to live with it

Alexander Telegin
  • 665
  • 1
  • 7
  • 22
  • 1
    Thanks for the info, I hadn't seen this one before. The issue is in reference to `resource(s)` as opposed to `resouce_bundle` (which was implemented to add in a way to precompile bundles and target them, if I've read the docs correct). I'll definitely look into the legacy mode mentioned, and update you on how that goes. – Z. Bagley Jan 25 '19 at 17:33