Fix for Blender 2.79 files in 2019.2.0a13 (Unity-BlenderToFBX.py)

I just upgraded from a9 to a13 and importing of .blend files from blender 2.79b was broken.

It was detecting 2.79 as 2.49 or older …

I edited the script, replacing the way version detection is done and deleted pre 2.5 code (almost 10 years ago :stuck_out_tongue: )

For people that have same problem can find that file at:
C:\Program Files\Unity\Hub\Editor\2019.2.0a13\Editor\Data\Tools\Unity-BlenderToFBX.py

*Didn’t test this with 2.8 yet

import bpy.ops
import bpy

minor = bpy.app.version[1]

blender280 = minor >= 80

if not blender280:
        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 blender280:
    bpy.ops.export_scene.fbx(filepath=outfile,
        check_existing=False,
        use_selection=False,
        use_active_collection=False,
        object_types= {'ARMATURE','CAMERA','LIGHT','MESH','EMPTY'},
        use_mesh_modifiers=True,
        mesh_smooth_type='OFF',
        use_custom_props=True,
        apply_scale_options='FBX_SCALE_ALL'
    )
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']

    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)
2 Likes

Hi @Snorkel

I made a small hack in this script ( that i found on the web ) for allowing me
to define wether an obj will be imported in unity or not, according to the 1st char of its name.

here is the lil change i made:

        # 2.59 and later

        # HACK SELECTIVE BLENDER IMPORT
        for obj in bpy.data.objects:
            obj.select = False if obj.name[0] in '_.' else True
        #end HACK

 

        kwargs = io_scene_fbx.export_fbx.defaults_unity3d()

        # HACK SELECTIVE BLENDER IMPORT
        kwargs["use_selection"] = True
        #end HACK

        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)

it replaces the end of your script.

For some reason i don’t get this script don’t work anymore with unity 2019 :confused:
And i’m far from easy with python and moreover with how blender works for guessing what i should do :frowning:

do you think you could help me with this please ?

thanks in advance :slight_smile:

Happy unitying !

EDIT:
i finally made it work… it seems it was unity’s code that didn’t do proper things :confused:

2 Likes