Multiple extensions capability

In the ARKit Placing Objects Demo, in the VirtualObject class, there is VirtualObjectDefinition function that looks for the models in the Models.scnassets folder and it also looks them up by their .scn extension. I'd like to know how to modify this call so it can look for more than just the .scn extension, but for .dae as well for example.


Here's the function:

init(definition: VirtualObjectDefinition) {

self.definition = definition

if let url = Bundle.main.url(forResource: "Models.scnassets/\(definition.modelName)/\(definition.modelName)", withExtension: "scn") {

super.init(url: url)!

} else {

fatalError("can't find expected virtual object bundle resources")

}

}


I'm guessing what needs to be modified is the withExtension component, but I'm not sure of the right format for adding multiple extensions.

Accepted Reply

IMO, the best you write two initial functions, one for loading scn and other for dae, thus you can load right model in purpose:


init(scnDefinition: VirtualObjectDefinition) { ... }

init(daeDefinition: VirtualObjectDefinition) { ... }


Other solution is trial-err for both model's format - you need only one init function with very little modifying:


init(definition: VirtualObjectDefinition) {

self.definition = definition

if let url = Bundle.main.url(forResource: "Models.scnassets/\(definition.modelName)/\(definition.modelName)", withExtension: "scn") ??

Bundle.main.url(forResource: "Models.scnassets/\(definition.modelName)/\(definition.modelName)", withExtension: "dae")

{

super.init(url: url)!

} else {

fatalError("can't find expected virtual object bundle resources")

}

}

Replies

IMO, the best you write two initial functions, one for loading scn and other for dae, thus you can load right model in purpose:


init(scnDefinition: VirtualObjectDefinition) { ... }

init(daeDefinition: VirtualObjectDefinition) { ... }


Other solution is trial-err for both model's format - you need only one init function with very little modifying:


init(definition: VirtualObjectDefinition) {

self.definition = definition

if let url = Bundle.main.url(forResource: "Models.scnassets/\(definition.modelName)/\(definition.modelName)", withExtension: "scn") ??

Bundle.main.url(forResource: "Models.scnassets/\(definition.modelName)/\(definition.modelName)", withExtension: "dae")

{

super.init(url: url)!

} else {

fatalError("can't find expected virtual object bundle resources")

}

}

I just needed to a else if statement to handle the dae