In any directory, All fbx files ASCII format or BINARY format There was a time when I wanted to convert all at once, so I tried it with python fbx sdk.
FBX Python Bindings
Refer to the official documentation and put the FBX Python Bindings into your environment. Installing Python FBX (2020.0.1)
By the way, if you want to see the latest version at that time, It seems that you can get it by going as follows. https://www.autodesk.com/products/fbx/overview -> GET FBX SDK -> FBX Python Bindings
FBX file format conversion It seems that you can do it like this.
I was allowed to refer to this
Convert any fbx file format
from fbx import FbxManager
from fbx import FbxScene
from fbx import FbxImporter
from fbx import FbxExporter
FBX_MANAGER = FbxManager.Create()
def _get_file_fomrat(format_name):
io_plugin_registry = FBX_MANAGER.GetIOPluginRegistry()
for format_id in range(io_plugin_registry.GetWriterFormatCount()):
if io_plugin_registry.WriterIsFBX(format_id):
desc = io_plugin_registry.GetWriterFormatDescription(format_id)
if format_name in desc:
return format_id
# Default format is auto
return -1
def _convert(path, file_format_id):
scene = FbxScene.Create(FBX_MANAGER, "")
importer = FbxImporter.Create(FBX_MANAGER, "")
importer.Initialize(path, -1)
importer.Import(scene)
importer.Destroy()
exporter = FbxExporter.Create(FBX_MANAGER, "")
exporter.Initialize(path, file_format_id)
exporter.Export(scene)
exporter.Destroy()
scene.Destroy()
#Convert the target fbx file to binary
_convert('target_fbx_file_path', _get_file_fomrat('binary'))
#Convert the target fbx file to ASCII
_convert('target_fbx_file_path', _get_file_fomrat('ascii'))
It's useless to convert to the same format, so Try to exclude conversions to the same format as you want.
Here seems to be unofficial but in binary format Since the header information is listed, I will refer to it.
Check if the optional fbx file is in binary format
FBX_BINARY_SIGNATURE = b"Kaydara FBX Binary \x00\x1A\x00"
FBX_BINARY_SIGNATURE_LENGTH = len(FBX_BINARY_SIGNATURE)
def _is_binary_fbx(path):
with open(path, 'rb') as file:
return file.read(FBX_BINARY_SIGNATURE_LENGTH) == FBX_BINARY_SIGNATURE
return False
Since the first 23 bytes are fixed binary data, The files are read as much as necessary and compared.
If you put this judgment at the beginning of the conversion method, Only files that are different from the desired format will be converted.
def _convert(path, file_format_id, required_binary):
if _is_binary_fbx(path) != required_binary:
scene = FbxScene.Create(FBX_MANAGER, "")
importer = FbxImporter.Create(FBX_MANAGER, "")
importer.Initialize(path, -1)
importer.Import(scene)
importer.Destroy()
exporter = FbxExporter.Create(FBX_MANAGER, "")
exporter.Initialize(path, file_format_id)
exporter.Export(scene)
exporter.Destroy()
scene.Destroy()
Converting 1fbx as 1 job Parallelize conversions on a file-by-file basis I will try to speed it up.
Use concurrent.futures.ProcessPoolExecutor. python 3.x series is required.
Parallelize and speed up
import concurrent.futures
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.submit(_convert, 'target_fbx_file_path', _get_file_fomrat('ascii'), false)
I want to target everything in any directory, so os.walk.
import os
import sys
with concurrent.futures.ProcessPoolExecutor() as executor:
file_format_id = _get_file_fomrat('binary')
for root, _dirs, files in os.walk('target_directory_path'):
for filename in files:
if filename.endswith('.fbx'):
path = os.path.join(root, filename)
executor.submit(_convert, path, file_format_id, true)
I think this made batch conversion possible a little faster.
The converted file depends on the version of FBX SDK you are using, It can be specified as follows.
from fbx import FbxSceneRenamer
# FBXVersion:Become 7500
exporter.SetFileExportVersion('FBX201600', FbxSceneRenamer.eNone)
# FBXVersion:Become 7200
exporter.SetFileExportVersion('FBX201200', FbxSceneRenamer.eNone)
The version that can be specified is Defined in fbxio.h FBX_xxxx_xx_COMPATIBLE type character string.
In summary, it looks like the one uploaded below. https://github.com/yassy0413/fbx-file-utility-python/blob/master/fbx_file_format_utility.py
Recommended Posts