public void WriteToFile(string Filename) { int Major, Minor, Revision; bool Status = true; int FileFormat = -1; bool bEmbedMedia = false; // Create an exporter. FbxExporter Exporter = FbxExporter.Create(SdkManager, ""); // set file format // Write in fall back format if pEmbedMedia is true FileFormat = SdkManager.GetIOPluginRegistry().GetNativeWriterFormat(); FbxIOSettings IOS_REF = SdkManager.GetIOSettings(); // Set the export states. By default, the export states are always set to // true except for the option eEXPORT_TEXTURE_AS_EMBEDDED. The code below // shows how to change these states. IOS_REF.SetBoolProp(fbx_wrapper.EXP_FBX_MATERIAL, true); IOS_REF.SetBoolProp(fbx_wrapper.EXP_FBX_TEXTURE, true); IOS_REF.SetBoolProp(fbx_wrapper.EXP_FBX_EMBEDDED, bEmbedMedia); IOS_REF.SetBoolProp(fbx_wrapper.EXP_FBX_SHAPE, true); IOS_REF.SetBoolProp(fbx_wrapper.EXP_FBX_GOBO, true); IOS_REF.SetBoolProp(fbx_wrapper.EXP_FBX_ANIMATION, true); IOS_REF.SetBoolProp(fbx_wrapper.EXP_FBX_GLOBAL_SETTINGS, true); string CompatibilitySetting = fbx_wrapper.FBX_2013_00_COMPATIBLE; if (!Exporter.SetFileExportVersion(new FbxString(CompatibilitySetting), FbxSceneRenamer.ERenamingMode.eNone)) { Debug.LogWarning("Call to KFbxExporter::SetFileExportVersion(FBX_2013_00_COMPATIBLE) to export 2013 fbx file format failed.\n"); } //// Initialize the exporter by providing a filename. if (!Exporter.Initialize(Filename, FileFormat, SdkManager.GetIOSettings())) { Debug.LogWarning("Call to KFbxExporter::Initialize() failed.\n"); string errorString = Exporter.GetStatus().GetErrorString(); Debug.LogWarning("Error returned:" + errorString); return; } //FbxManager.GetFileFormatVersion(Major, Minor, Revision); // Export the scene. Status = Exporter.Export(Scene); if (!Status) { string errorString = Exporter.GetStatus().GetErrorString(); Debug.LogWarning("Error returned:" + errorString); } Clear(); return; }
public static void ExportMesh(Mesh mesh, string directory, string fileName) { var filePath = Path.Combine(directory, fileName); // Make a temporary copy of the mesh to modify it Mesh tempMesh = Object.Instantiate(mesh); tempMesh.name = mesh.name; // If meters, divide by 100 since default is cm. Assume centered at origin. if (fbxUnit == FbxSystemUnit.m) { Vector3[] vertices = tempMesh.vertices; for (int i = 0; i < vertices.Length; ++i) { vertices[i] /= 100.0f; } tempMesh.vertices = vertices; } // You could handle other SystemUnits here // FBX Manager FbxManager manager = FbxManager.Create(); manager.SetIOSettings(FbxIOSettings.Create(manager, Globals.IOSROOT)); // FBX Exporter FbxExporter fbxExporter = FbxExporter.Create(manager, "Exporter"); // Binary int fileFormat = -1; // Ascii if (saveFbxAsAscii) { fileFormat = manager.GetIOPluginRegistry().FindWriterIDByDescription("FBX ascii (*.fbx)"); } fbxExporter.Initialize(filePath, fileFormat, manager.GetIOSettings()); fbxExporter.SetFileExportVersion("FBX201400"); // FBX Scene FbxScene fbxScene = FbxScene.Create(manager, "Scene"); FbxDocumentInfo sceneInfo = FbxDocumentInfo.Create(manager, "SceneInfo"); // Set up scene info sceneInfo.mTitle = fbxFileTitle; sceneInfo.mSubject = fbxFileSubject; sceneInfo.mComment = fbxFileComment; sceneInfo.mAuthor = fbxFileAuthor; sceneInfo.mRevision = fbxFileRevision; sceneInfo.mKeywords = fbxFileKeywords; sceneInfo.Original_ApplicationName.Set(fbxFileApplication); sceneInfo.LastSaved_ApplicationName.Set(fbxFileApplication); fbxScene.SetSceneInfo(sceneInfo); // Set up Global settings FbxGlobalSettings globalSettings = fbxScene.GetGlobalSettings(); globalSettings.SetSystemUnit(fbxUnit); globalSettings.SetAxisSystem(fbxAxisSystem); FbxNode modelNode = FbxNode.Create(fbxScene, tempMesh.name); // Add mesh to a node in the scene // TODO Wat??? // using (ModelExporter modelExporter = new ModelExporter()) // { // if (!modelExporter.ExportMesh(tempMesh, modelNode)) // Debug.LogError("Problem Exporting Mesh"); // } // add the model to the scene fbxScene.GetRootNode().AddChild(modelNode); // Finally actually save the scene bool sceneSuccess = fbxExporter.Export(fbxScene); AssetDatabase.Refresh(); // clean up temporary model if (Application.isPlaying) { Object.Destroy(tempMesh); } else { Object.DestroyImmediate(tempMesh); } }