Autodesk fbx sdk Program (2)

Source: Internet
Author: User

This is the second learning Note of the Autodesk fbx sdk. The following Chinese characters are translated from the Autodesk fbx sdk Program.

In the previous article, we talked about some basic operations of the fbx sdk, creating FbxManager, and writing our first fbx sdk example.

Today is the second part of the fbx sdk guide. How can I create an FBX file converter?

Convert FBX to the DAE and Obj formats! Convert FBX binary to text format!


Knowledge points of this article:

1. FbxManager creation and destruction

2. Create and use FbxImporter

3. Use of FbxExporter

4. file formats supported by Fbx SDK


Autodesk fbx sdk document address:

http://docs.autodesk.com/FBX/2014/ENU/FBX-SDK-Documentation/index.html

Official example running effect:


I don't know whether it is MFC or. Net. I don't know either of them, so I still write a console ......



# Include <fbxsdk. h> # pragma comment (lib, "libfbxsdk. lib ") FbxManager * g_pFbxManager = NULL;/*** initialize FbxSDK ***/bool InitializeFbxSDK () {bool bRet = false; do {// create FbxManagerg_pFbxManager = FbxManager :: create (); // Create authorization * pFbxIOSettings = FbxIOSettings: Create (g_pFbxManager, IOSROOT); // bind the relationship g_pFbxManager-> SetIOSettings (pFbxIOSettings); bRet = true ;} while (0); return bRet;}/*** get the format that Fbx SDK supports reading ***/v Oid GetFileCanImport () {int count = g_pFbxManager-> GetIOPluginRegistry ()-> GetReaderFormatCount (); printf ("the following % d file formats can be imported: \ n", count ); fbxString s; int I = 0; for (int I = 0; I <count; I ++) {s + = g_pFbxManager-> GetIOPluginRegistry ()-> GetReaderFormatDescription (I ); // obtain the description // s + = g_pFbxManager-> GetIOPluginRegistry ()-> GetReaderFormatExtension (I); // obtain the file suffix s = "% d: "+ s +" \ n "; printf (s. buffer (), I); s. clear () ;}/ *** get Fbx SDK export format: ***/void GetFileCanExport () {int count = g_pFbxManager-> GetIOPluginRegistry ()-> GetWriterFormatCount (); printf ("supports exporting the following % d file formats: \ n", count); FbxString s; int I = 0; for (int I = 0; I <count; I ++) {s + = g_pFbxManager-> GetIOPluginRegistry ()-> GetWriterFormatDescription (I); // get the description // s + = g_pFbxManager-> GetIOPluginRegistry () -> GetWriterFormatExtension (I); // obtain the file suffix s = "% d:" + s + "\ n"; printf (s. buffer (), I); s. cle Ar () ;}}/*** read an Fbx file to FbxScene ***/bool ImportFbxModel (FbxScene * scene, const char * importfilename) {int fileVerMajorNum, fileVerMinorNum, fileVerRevision; // file version number and minor version number, corrected version number: int sdkVerMajorNum, sdkVerMinorNum, sdkVerRevision; // fbx sdk version number: int animStackCount; // Number of animations: bool bRet = false; char password [1024]; // password. The file may be encrypted. enter a password // obtain the fbx sdk version number FbxManager: GetFileFormatVersion (sdkVerMajorNum, sdkVerMinorNum, sdkVerR Evision); // Create a condition * pFbxImporter = FbxImporter: Create (g_pFbxManager, importfilename); // Initialize required bool importret = pFbxImporter-> Initialize (importfilename,-1, g_pFbxManager-> GetIOSettings (); // obtain the Fbx file version pFbxImporter-> GetFileVersion (fileVerMajorNum, fileVerMinorNum, fileVerRevision); if (! Importret) // import error {FbxString errorStr = pFbxImporter-> GetStatus (). getErrorString (); printf ("\ nFbxImporter initialization failed, error cause: % s \ n", errorStr. buffer (); if (pFbxImporter-> GetStatus (). getCode () = FbxStatus: eInvalidFileVersion) // if the file version is incorrect {printf ("\ n fbx sdk version: % d. % d. % d \ n ", sdkVerMajorNum, sdkVerMinorNum, sdkVerRevision); printf (" \ nFBX file version: % d. % d. % d \ n ", fileVerMajorNum, fileVerMinorNum, fileVerRevision);} return false;} p Rintf ("\ n ****** imported file ******* \ n"); printf ("\ n fbx sdk version: % d. % d. % d \ n ", sdkVerMajorNum, sdkVerMinorNum, sdkVerRevision); if (pFbxImporter-> IsFBX ()) // If the imported file is in FBX format {printf ("\ n FBX file version: % d. % d. % d \ n ", fileVerMajorNum, fileVerMinorNum, fileVerRevision); // In the FBX file, one or more" animation stacks "in a Scene may exist ", an "animation stack" stores an animation data. If you want to obtain "Animation stack" information, you do not need to load all Sceneprintf ("\ n animation stack information: \ n "); animStackC Ount = pFbxImporter-> GetAnimStackCount (); printf ("Quantity: % d \ n", animStackCount); printf ("Name: % s \ n ", pFbxImporter-> GetActiveAnimStackName (); for (int I = 0; I <animStackCount; I ++) {FbxTakeInfo * pFbxTakeInfo = pFbxImporter-> GetTakeInfo (I ); printf ("Animation Stack % d \ n", I); printf ("Name: % s \ n", pFbxTakeInfo-> mName. buffer (); printf ("Description: % s \ n", pFbxTakeInfo-> mDescription. buffer (); printf ("Import Name: % s \ n", PFbxTakeInfo-> mImportName. Buffer (); // The imported name printf ("Import State: % s \ n", pFbxTakeInfo-> mSelect? "True": "false");} // sets the imported content. By default, all content is imported into g_pFbxManager-> GetIOSettings ()-> SetBoolProp (IMP_FBX_MATERIAL, true ); g_pFbxManager-> GetIOSettings ()-> SetBoolProp (enabled, true); g_pFbxManager-> GetIOSettings ()-> SetBoolProp (IMP_FBX_LINK, true); g_pFbxManager-> GetIOSettings () -> SetBoolProp (IMP_FBX_SHAPE, true); g_pFbxManager-> GetIOSettings ()-> SetBoolProp (IMP_FBX_GOBO, true); g_pFbxManager-> GetIOSettings ()-> SetBoolProp (IMP_FBX_ANIMATION, true); g_pFbxManager-> GetIOSettings ()-> SetBoolProp (IMP_FBX_GLOBAL_SETTINGS, true);} // If the imported file is not in FBX format, then there is no above logic // Import Fbx to the scenario bRet = pFbxImporter-> Import (scene); // If the File Import fails, the returned error Code is a Password error if (bRet = false & pFbxImporter-> GetStatus (). getCode () = FbxStatus: ePasswordError) {printf ("Enter password: \ n"); password [0] = '\ 0 '; FBXSDK_CRT_SECURE_NO_WARNING_BEGIN // This macro is used to disable the 4996 warning. Similar to scanf strcpy strcat, scanf ("% s", password), passwdStr (password), g_pFbxManager-> GetIOSettings ()-> SetStringProp (IMP_FBX_PASSWORD, passwdStr); // set StringProp for FbxIOSetting, that is, string attribute. Prop indicates propertyg_pFbxManager-> GetIOSettings ()-> SetBoolProp (IMP_FBX_PASSWORD_ENABLE, true); // set the Bool attribute, use password bRet = pFbxImporter-> Import (scene); // enter the password and re-Importif (bRet = false & pFbxIm Porter-> GetStatus (). GetCode () = FbxStatus: ePasswordError) {printf ("File Import error, incorrect password! ") ;}} PFbxImporter-> Destroy (); return bRet;}/*** export FbxScene to model file ***/bool ExportFbxSceneToModel (FbxScene * scene, const char * exportfilename, int exportformat, bool pexportmedia) {bool bRet = false; // Create FbxExporterFbxExporter * pFbxExport = FbxExporter: Create (g_pFbxManager ,""); if (exportformat <0 | exportformat> = g_pFbxManager-> GetIOPluginRegistry ()-> GetWriterFormatCount ()) {// if you select the exported file format, printf is not supported ("this format cannot be exported !! \ N "); return false; exportformat = g_pFbxManager-> GetIOPluginRegistry ()-> GetNativeWriterFormat (); printf (" try to export the default format (FBX): % d ", exportformat); if (! Pexportmedia) // if you do not export multimedia {int formatcount = g_pFbxManager-> GetIOPluginRegistry ()-> GetWriterFormatCount (); // try to export the FBX ascii file, you can see the available for (int I = 0; I <formatcount; I ++) {if (g_pFbxManager-> GetIOPluginRegistry ()-> WriterIsFBX (I )) {FbxString desStr = g_pFbxManager-> GetIOPluginRegistry ()-> GetWriterFormatDescription (I); if (desStr. find ("ascii") >=0) {exportformat = I; break ;}}}// if the export format is correct, the preceding logic if (! PFbxExport-> Initialize (exportfilename,-1, g_pFbxManager-> GetIOSettings () {printf ("FbxExport-> Initialize Faild \ n"); printf ("FbxExport initialization failure cause: % s ", pFbxExport-> GetStatus (). getErrorString (); return false;} if (g_pFbxManager-> GetIOPluginRegistry ()-> WriterIsFBX (exportformat) {g_pFbxManager-> GetIOSettings ()-> SetBoolProp (accept, true ); g_pFbxManager-> GetIOSettings ()-> SetBoolProp (EXP_FBX_TEXTURE, true); g_pFbxManager-> GetIOSettings ()-> SetBoolProp (batch, pexportmedia); g_pFbxManager-> GetIOSettings () -> SetBoolProp (EXP_FBX_SHAPE, true); g_pFbxManager-> GetIOSettings ()-> SetBoolProp (partial, true); g_pFbxManager-> GetIOSettings ()-> SetBoolProp (partial, true ); g_pFbxManager-> GetIOSettings ()-> SetBoolProp (EXP_FBX_GLOBAL_SETTINGS, true);} bRet = pFbxExport-> Export (scene); pFbxExport-> Destroy (); return bRet ;} /*** convert a model file ***/void ConvertModelFile (const char * importfilename, const char * exportfilename, int writefileformat) {printf ("Import file path: % s \ n export file path: % s \ n Export File Format: % d \ n ", importfilename, exportfilename, writefileformat); // create FbxScene, the name is BaoxiangFbxScene * pFbxScene = FbxScene: Create (g_pFbxManager, "Baoxiang "); printf ("\ n ****** conversion start ******* \ n"); bool B = ImportFbxModel (pFbxScene, importfilename); if (B) {printf ("\ n ** model file loaded successfully ***** \ n ");} else {printf ("\ n ** model file loading failed ***** \ n"); pFbxScene-> Destroy (); return ;} printf ("\ n ** start to export *** \ n"); B = ExportFbxSceneToModel (pFbxScene, exportfilename, writefileformat, false); if (B) {printf ("\ n ** model file exported successfully ***** \ n ");} else {printf ("\ n ** failed to export model file ***** \ n"); pFbxScene-> Destroy (); return ;}} int main (int argc, char ** argv) {InitializeFbxSDK (); GetFileCanImport (); GetFileCanExport (); char importfilename [1024]; int exportformat = 0; char exportfilename [1024]; printf ("\ n enter the import file path:"); scanf ("% s", importfilename); printf ("\ n enter the export format :"); scanf ("% d", & exportformat); printf ("\ n enter the export file path:"); scanf ("% s", exportfilename); ConvertModelFile (importfilename, exportfilename, exportformat); system ("pause"); return 0 ;}


Package and download the project:

http://code.taobao.org/svn/xgameengine/trunk/OtherProject/FbxDemo


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.