Last active
May 19, 2017 14:23
-
-
Save Tenebrous/e8e36b0c791476af4eff6ef87a541fdc to your computer and use it in GitHub Desktop.
Update Unity-BlenderToFBX.py to support lights & cameras
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
blender249 = True | |
try: import Blender | |
except: | |
blender249 = False | |
import bpy | |
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 | |
# Find the Blender output file | |
import os | |
outfile = os.getenv("UNITY_BLENDER_EXPORTER_OUTPUT_FILE") | |
# Do the conversion | |
print("Starting blender to FBX conversion " + outfile) | |
if 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') | |
print("moo") | |
class FakeOp: | |
def report(self, tp, msg): | |
print("%s: %s" % (tp, msg)) | |
exportObjects = ['ARMATURE', 'EMPTY', 'MESH', 'CAMERA', 'LAMP'] | |
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() | |
kwargs['object_types'] = exportObjects | |
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