Trouble USDZ to fbx Formate conversion in swift, RoomPlan API USDZ to FBX Conversion

249 Views Asked by At

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)")
    }
1

There are 1 best solutions below

0
Sean Hong On

I believe the reason for your conversion not working is due to ModelIO.MDLAsset.export() method not supporting FBX file formats.

I suggest you check in your source code if it supports FBX conversion by using the following method from Apple Docs

if MDLAsset.canImportFileExtension("fbx") {
  try MDLAsset.export(urlExportPath)
}

As it says on the documentation, MDLAsset.export only supports the following extensions:

The set of supported extensions and formats includes:
.abc
Alembic
.usd, .usda, .usdc
Universal Scene Description
.usdz
Universal Scene Description (Mobile)
.ply
Polygon
.obj
Wavefront Object
.stl
Standard Tessellation Language
Additional formats may be supported as well.