Skip to content

Instantly share code, notes, and snippets.

@Linkupdated
Last active October 28, 2020 02:08
Show Gist options
  • Save Linkupdated/8827c467bef351d4f58cd3375e9ca72c to your computer and use it in GitHub Desktop.
Save Linkupdated/8827c467bef351d4f58cd3375e9ca72c to your computer and use it in GitHub Desktop.
Blend to FBX project converter updated to 2.8 -> https://github.com/iamsed/blendToFbxExporterForUnity
#
# blendToFbxExporter.py
#
# Created by Krzysztof Żarczyński (@iamsed) on 16.11.15.
# http://blog.kzarczynski.com/
# Copyright (c) 2015 Krzysztof Żarczyński. All rights reserved.
#
#
# The script exports multiple files from Blender into fbx models.
# It searches the path recursively and creates .fbx files next to the .blend files,
# then it renames corresponding Unity *.blend.meta files to *.fbx.meta to keep editor references,
# !!! then it deletes the .blend files !!!
# You need Blender to be installed on your machine.
# You need to specify the following command line arguments:
# Blender's directory, path to args-Unity-BlenderToFBX.py, input directory (with blend files)
#
import os
import os.path
import sys
import glob
import bpy
import fnmatch
import subprocess
if len(sys.argv) != 7:
print("\nusage: <path to Blender> --background --python <path to blendToFBXExporter.py> -- <path to args-Unity-BlenderToFBX.py> <path to the project (.blend files)>")
else:
pathToBlender = sys.argv[0]
pathToBlenderToFBX = sys.argv[5]
path = sys.argv[6]
alreadyHaveFBX = []
processedFiles = []
configfiles = [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(path)
for f in fnmatch.filter(files, '*.blend')]
for infile in configfiles:
outfilename = infile.replace('.blend', '.fbx')
if os.path.isfile(outfilename):
alreadyHaveFBX.append(infile)
else:
proc = ("{pathToBlender} --background --python {pathToBlenderToFBX} -- \"{infile}\" \"{outfile}\"").format(
pathToBlender=pathToBlender,
pathToBlenderToFBX=pathToBlenderToFBX,
infile=infile,
outfile=outfilename
)
status = subprocess.call(proc, shell=True)
bakfilename = infile.replace('.blend', '.blend_bak')
os.rename(infile, bakfilename)
processedFiles.append(infile)
configfiles = [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(path)
for f in fnmatch.filter(files, '*.blend.meta')]
for infile in configfiles:
outfilename = infile.replace('.blend', '.fbx')
if not os.path.isfile(outfilename):
os.rename(infile, outfilename)
print("Processed files:")
print(len(processedFiles))
print("Skipped files (.fbx already exists at the locaton):")
print(len(alreadyHaveFBX))
for infile in alreadyHaveFBX:
print(infile)
import bpy
import sys
blender249 = True
blender280 = (2,80,0) <= bpy.app.version
try:
import Blender
except:
blender249 = False
if not blender280:
if blender249:
try:
import export_fbx
except:
print('error: export_fbx not found.')
Blender.Quit()
else :
try:
import io_scene_fbx.export_fbx
except:
print('error: io_scene_fbx.export_fbx not found.')
# This might need to be bpy.Quit()
raise
# Accept command line arguments and load the contents of the .blend file
infile = sys.argv[5]
bpy.ops.wm.open_mainfile(filepath=infile)
outfile = sys.argv[6]
# Do the conversion
print("Starting blender to FBX conversion " + outfile)
if blender280:
import bpy.ops
bpy.ops.export_scene.fbx(filepath=outfile,
check_existing=False,
use_selection=False,
use_active_collection=False,
object_types= {'ARMATURE','CAMERA','LIGHT','MESH','OTHER','EMPTY'},
use_mesh_modifiers=True,
mesh_smooth_type='OFF',
use_custom_props=True,
bake_anim_use_nla_strips=False,
bake_anim_use_all_actions=False,
apply_scale_options='FBX_SCALE_ALL')
elif blender249:
mtx4_x90n = Blender.Mathutils.RotationMatrix(-90, 4, 'x')
export_fbx.write(outfile,
EXP_OBS_SELECTED=False,
EXP_MESH=True,
EXP_MESH_APPLY_MOD=True,
EXP_MESH_HQ_NORMALS=True,
EXP_ARMATURE=True,
EXP_LAMP=True,
EXP_CAMERA=True,
EXP_EMPTY=True,
EXP_IMAGE_COPY=False,
ANIM_ENABLE=True,
ANIM_OPTIMIZE=False,
ANIM_ACTION_ALL=True,
GLOBAL_MATRIX=mtx4_x90n)
else:
# blender 2.58 or newer
import math
from mathutils import Matrix
# -90 degrees
mtx4_x90n = Matrix.Rotation(-math.pi / 2.0, 4, 'X')
class FakeOp:
def report(self, tp, msg):
print("%s: %s" % (tp, msg))
exportObjects = ['ARMATURE', 'EMPTY', 'MESH']
minorVersion = bpy.app.version[1];
if minorVersion <= 58:
# 2.58
io_scene_fbx.export_fbx.save(FakeOp(), bpy.context, filepath=outfile,
global_matrix=mtx4_x90n,
use_selection=False,
object_types=exportObjects,
mesh_apply_modifiers=True,
ANIM_ENABLE=True,
ANIM_OPTIMIZE=False,
ANIM_OPTIMIZE_PRECISSION=6,
ANIM_ACTION_ALL=True,
batch_mode='OFF',
BATCH_OWN_DIR=False)
else:
# 2.59 and later
kwargs = io_scene_fbx.export_fbx.defaults_unity3d()
io_scene_fbx.export_fbx.save(FakeOp(), bpy.context, filepath=outfile, **kwargs)
# HQ normals are not supported in the current exporter
print("Finished blender to FBX conversion " + outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment