I have been trying to export a SNCScene to USDC or USDA by using Model I/O, since these function calls:
print("can export usdc: \(MDLAsset.canExportFileExtension("usdc"))")
print("can export usda: \(MDLAsset.canExportFileExtension("usda"))")
both print "True". Here are my steps:
let fileMan = FileManager.default
do {
let userDocsDir = try fileMan.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil, create: true)
let exportsDir = userDocsDir.appendingPathComponent("exports",
isDirectory: true)
try fileMan.createDirectory(at: exportsDir,
withIntermediateDirectories: true,
attributes: nil)
let uuid = UUID.init().uuidString
let exportName = exportsDir.appendingPathComponent(uuid,
isDirectory: false)
let exportFileUSDC = exportName.appendingPathExtension("usdc")
let exportFileUSDA = exportName.appendingPathExtension("usda")
let newScene = SCNScene()
let cubeGeom = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0)
cubeGeom.materials[0] = SCNMaterial()
cubeGeom.firstMaterial!.diffuse.contents = UIColor.red
cubeGeom.firstMaterial!.name = "red"
let box = SCNNode(geometry: cubeGeom)
newScene.rootNode.addChildNode(box)
let asset = MDLAsset(scnScene: newScene)
print(asset)
print(newScene)
try asset.export(to: exportFileUSDC)
try asset.export(to: exportFileUSDA)
}
catch {
log.error("\(error.localizedDescription)")
}
This code, although it compiles and runs without throwing exceptions, it produces buggy USDA/USDC files, in which the materials are absent and which, if opened by double-clicking on them, crash XCode with the following error:
"Assertion failed: (0 && “MTLPatchTable Creation Failed”), function Create, file /BuildRoot/Library/Caches/com.apple.xbs/Sources/SceneKit/SceneKit-470/lib/libOsd/sources/opensubdiv/osd/mtlPatchTable.mm, line 97."
My research shows that I shouldn't be doing anything wrong, but could someone take a look and help me out, e.g. tell me if I'm doing something wrong in these steps? You can try and run the above code for yourself.