Last active
June 19, 2020 15:37
-
-
Save wojtossfm/cd0808a35753341036fef8efec7704ce to your computer and use it in GitHub Desktop.
The synty FBX files appear jumbled when imported into blender/godot and need to be fixed before they can be used. Tested with blender 2.80 and python3.7 (but 3.X should work) on windows but should work on other OSes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import argparse | |
import pathlib | |
import subprocess | |
try: | |
import bpy | |
except ImportError: | |
bpy = None | |
def main_blender(raw_args): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('fbx') | |
args = parser.parse_args(raw_args) | |
# In the future I want this to support processing a directory (all files from it) | |
# but for now they end up with altered names so that is why this does them only one per invocation | |
target = pathlib.Path(args.fbx) | |
if target.is_dir(): | |
files = [path for path in target.iterdir() if path.suffix == '.fbx' and not path.stem.endswith('_fixed')] | |
else: | |
files = [target] | |
for filepath in files: | |
inpath = str(filepath) | |
outpath = inpath.replace('.fbx', '_fixed.fbx') | |
bpy.ops.wm.read_factory_settings(use_empty=True) | |
bpy.ops.import_scene.fbx(filepath=inpath, use_anim=False, ignore_leaf_bones=True, force_connect_children=True, automatic_bone_orientation=True) | |
bpy.ops.object.posemode_toggle() | |
bpy.ops.pose.transforms_clear() | |
bpy.ops.object.posemode_toggle() | |
bpy.ops.export_scene.fbx(filepath=outpath, add_leaf_bones=False) | |
def main_nonblender(raw_args): | |
parser = argparse.ArgumentParser( | |
usage="python.exe adjust_fbx_for_godot.py <path-to-blender> <path-to-fbx>" | |
) | |
parser.add_argument('blender', help='path to your blender executable') | |
parser.add_argument('file_or_directory') | |
args = parser.parse_args(raw_args) | |
blender = pathlib.Path(args.blender).absolute() | |
target = pathlib.Path(args.file_or_directory).absolute() | |
script = pathlib.Path(__file__).absolute() | |
subprocess.check_call([ | |
str(blender), "--background", '--python', str(script), '--', str(target) | |
]) | |
def main(): | |
if bpy is None: | |
main_nonblender(sys.argv[1:]) | |
else: | |
try: | |
raw_args = sys.argv[sys.argv.index('--') + 1:] | |
except ValueError: | |
raw_args = [] | |
main_blender(raw_args) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment