Hello! I've been trying to create a custom USDZ viewer using the Vision Pro. Basically, I want to be able to load in a file and have a custom control system I can use to transform, playback animations, etc.
I'm getting stuck right at the starting line however. As far as I can tell, the only way to access the file system through SwiftUI is to use the DocumentGroup struct to bring up the view. This requires implementing a file type through the FileDocument protocol.
All of the resources I'm finding use text files as their example, so I'm unsure of how to implement USDZ files. Here is the FileDocument I've built so far:
import SwiftUI
import UniformTypeIdentifiers
import RealityKit
struct CoreUsdzFile: FileDocument {
// we only support .usdz files
static var readableContentTypes = [UTType.usdz]
// make empty by default
var content: ModelEntity = .init()
// initializer to create new, empty usdz files
init(initialContent: ModelEntity = .init()){
content = initialContent
}
// import or read file
init(configuration: ReadConfiguration) throws {
if let data = configuration.file.regularFileContents {
// convert file content to ModelEntity?
content = ModelEntity.init(mesh: data)
} else {
throw CocoaError(.fileReadCorruptFile)
}
}
// save file wrapper
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
let data = Data(content)
return FileWrapper(regularFileWithContents: data)
}
}
My errors are on conversion of the file data into a ModelEntity and the reverse. I'm not sure if ModelEntity is the correct typing here, but as far as I can tell .usdz files are imported as ModelEntities.
Any help is much appreciated!
Dylan
Load usdz files with Entity.load(contentsOf: URL) Reference