I have generated a USDZ file from RoomPlan API, now this USDZ file is used in Unity, and Unity do not support USDZ files, So I have to Convert this USDZ to FBX & USDZ to OBJ, OBJ conversion is working but when I try FBX conversion is not working, destination path is Blank please check my code.
let inputFileName = "Rom2.usdz"//"plantpot.usdz"
let outputFileName = "Rom3.obj"//"Rom3.fbx"
override func viewDidLoad() {
super.viewDidLoad()
let fm = FileManager.default
let docsurl = try! fm.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
debugPrint(docsurl)
guard let usdzURL = Bundle.main.url(forResource: "plantpot", withExtension: "usdz") else {
fatalError("Failed to find USDZ file in bundle")
}
// Load the USDZ file using SceneKit
guard let scene = try? SCNScene(url: usdzURL, options: [.checkConsistency: true]) else {
print("Failed to load scene")
return
}
// Create a Model I/O asset from the SceneKit scene
let asset = MDLAsset(scnScene: scene)
debugPrint("Count",asset.count)
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fbxURL = documentsDirectory.appendingPathComponent(outputFileName)
if FileManager.default.fileExists(atPath: fbxURL.path) {
print("Output file already exists")
return
}
if !FileManager.default.isWritableFile(atPath: fbxURL.path) {
print("The file at \(fbxURL.path) exists but is not writable.")
} else {
print("The file at \(fbxURL.path) exists and is writable.")
}
do {
try asset.export(to: fbxURL)
print("Conversion complete. Output file: \(fbxURL.path)")
} catch let error {
print("Error exporting to FBX: \(error.localizedDescription)")
}
print("Conversion complete. Output file: \(fbxURL.path)")
}