DEV Community

Shoichi Okaniwa
Shoichi Okaniwa

Posted on • Originally published at qiita.com

Outputting FBX in ASCII Format Using FBX SDK Python

Exporting FBX in ASCII Format Instead of Binary

When exporting FBX files using the Python version of the FBX SDK, the default output is in binary format. I wanted to output in ASCII format, so I researched how to do this.

I have placed the sample project here:
https://github.com/segurvita/fbx_sdk_python_sample

Referenced the Official C++ Sample Code

I found a sample code on the following page of the official C++ documentation for saving scenes in ASCII format:
Autodesk FBX 2017 HELP / C++ API Reference / Examples / Common/Common.cxx

Here is an excerpt from the relevant C++ code:

int lFormatIndex, lFormatCount = pManager->GetIOPluginRegistry()->GetWriterFormatCount();
for (lFormatIndex=0; lFormatIndex<lFormatCount; lFormatIndex++)
{
    if (pManager->GetIOPluginRegistry()->WriterIsFBX(lFormatIndex))
    {
        FbxString lDesc = pManager->GetIOPluginRegistry()->GetWriterFormatDescription(lFormatIndex);
        const char *lASCII = "ascii";
        if (lDesc.Find(lASCII) >= 0)
        {
            pFileFormat = lFormatIndex;
            break;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Reading this code suggests that you should specify a format ID representing ASCII when outputting the file. However, it seems this format ID is not defined as a constant and must be searched dynamically. (If anyone knows a more efficient method, please let me know.)

Searching for the Format ID

Using the official C++ code as a reference, here is the corresponding Python code.

The process of searching for the ASCII format ID is encapsulated in the following function:

def get_ascii_format_id(manager):
    # Loop through the number of writable formats
    for formatId in range(manager.GetIOPluginRegistry().GetWriterFormatCount()):
        # Check if it's an FBX format
        if manager.GetIOPluginRegistry().WriterIsFBX(formatId):
            # Check if it's in ASCII format
            if "ascii" in manager.GetIOPluginRegistry().GetWriterFormatDescription(formatId):
                # Return the format ID
                return formatId

    # Return auto if the ASCII format is not found
    return -1    
Enter fullscreen mode Exit fullscreen mode

You should input an instance created with FbxManager.Create() as the manager argument. The return value is the format ID for the ASCII format.

File Exporting Process

Specify the function from earlier as the second argument in exporter.Initialize() as shown below:

exporter = FbxExporter.Create(manager, "")
exporter.Initialize("./test.fbx", get_ascii_format_id(manager))
exporter.Export(scene)
Enter fullscreen mode Exit fullscreen mode

With this, I was able to successfully output the FBX in ASCII format.

Conclusion

The syntax differs significantly between C++ and Python, such as with loop statements, making the writing process interesting.

The sample project is placed here:
https://github.com/segurvita/fbx_sdk_python_sample

Additionally, I referred to the following sites while creating this article. Thank you.

Top comments (0)