在Swift中的USDZ到FBX格式转换中遇到问题,RoomPlan API USDZ到FBX转换

ar7v8xwq  于 12个月前  发布在  Swift
关注(0)|答案(1)|浏览(208)

我已经从RoomPlan API生成了一个USDZ文件,现在这个USDZ文件在Unity中使用,Unity不支持USDZ文件,所以我必须将这个USDZ转换为FBX & USDZ到OBJ,OBJ转换工作,但当我尝试FBX转换不工作时,目标路径是空白请检查我的代码。

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

字符串

2j4z5cfb

2j4z5cfb1#

我相信你的转换不工作的原因是由于ModelIO.MDLAsset.export()方法不支持FBX文件格式。
我建议您检查您的源代码,如果它支持FBX转换使用以下方法从Apple Docs

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

字符串
正如文档中所说,MDLAsset.export只支持以下扩展:

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.

相关问题