Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in '2019.2 Beta' started by Snorkel, Apr 25, 2019.

  1. Snorkel

    Snorkel

    Joined:
    Dec 3, 2012
    Posts:
    37
    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 :p )

    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

    Code (CSharp):
    1.  
    2. import bpy.ops
    3. import bpy
    4.  
    5. minor = bpy.app.version[1]
    6.  
    7. blender280 = minor >= 80
    8.  
    9. if not blender280:
    10.         try:
    11.             import io_scene_fbx.export_fbx
    12.         except:
    13.             print('error: io_scene_fbx.export_fbx not found.')
    14.             # This might need to be bpy.Quit()
    15.             raise
    16.  
    17. # Find the Blender output file
    18. import os
    19. outfile = os.getenv("UNITY_BLENDER_EXPORTER_OUTPUT_FILE")
    20.  
    21. # Do the conversion
    22. print("Starting blender to FBX conversion " + outfile)
    23.  
    24. if blender280:
    25.     bpy.ops.export_scene.fbx(filepath=outfile,
    26.         check_existing=False,
    27.         use_selection=False,
    28.         use_active_collection=False,
    29.         object_types= {'ARMATURE','CAMERA','LIGHT','MESH','EMPTY'},
    30.         use_mesh_modifiers=True,
    31.         mesh_smooth_type='OFF',
    32.         use_custom_props=True,
    33.         apply_scale_options='FBX_SCALE_ALL'
    34.     )
    35. else:
    36.     # blender 2.58 or newer
    37.     import math
    38.     from mathutils import Matrix
    39.     # -90 degrees
    40.     mtx4_x90n = Matrix.Rotation(-math.pi / 2.0, 4, 'X')
    41.  
    42.     print("moo")
    43.  
    44.     class FakeOp:
    45.         def report(self, tp, msg):
    46.             print("%s: %s" % (tp, msg))
    47.  
    48.     exportObjects = ['ARMATURE', 'EMPTY', 'MESH']
    49.  
    50.     minorVersion = bpy.app.version[1];
    51.     if minorVersion <= 58:
    52.         # 2.58
    53.         io_scene_fbx.export_fbx.save(FakeOp(), bpy.context, filepath=outfile,
    54.             global_matrix=mtx4_x90n,
    55.             use_selection=False,
    56.             object_types=exportObjects,
    57.             mesh_apply_modifiers=True,
    58.             ANIM_ENABLE=True,
    59.             ANIM_OPTIMIZE=False,
    60.             ANIM_OPTIMIZE_PRECISSION=6,
    61.             ANIM_ACTION_ALL=True,
    62.             batch_mode='OFF',
    63.             BATCH_OWN_DIR=False)
    64.     else:
    65.         # 2.59 and later
    66.         kwargs = io_scene_fbx.export_fbx.defaults_unity3d()
    67.         io_scene_fbx.export_fbx.save(FakeOp(), bpy.context, filepath=outfile, **kwargs)
    68.     # HQ normals are not supported in the current exporter
    69.  
    70. print("Finished blender to FBX conversion " + outfile)
    71.  
    72.  
     
    Voltonik_ and Lahcene like this.
  2. grobonom

    grobonom

    Joined:
    Jun 23, 2018
    Posts:
    335
    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:
    Code (CSharp):
    1.         # 2.59 and later
    2.  
    3.         # HACK SELECTIVE BLENDER IMPORT
    4.         for obj in bpy.data.objects:
    5.             obj.select = False if obj.name[0] in '_.' else True
    6.         #end HACK
    7.  
    8.  
    9.  
    10.         kwargs = io_scene_fbx.export_fbx.defaults_unity3d()
    11.  
    12.         # HACK SELECTIVE BLENDER IMPORT
    13.         kwargs["use_selection"] = True
    14.         #end HACK
    15.  
    16.         io_scene_fbx.export_fbx.save(FakeOp(), bpy.context, filepath=outfile, **kwargs)
    17.  
    18.     # HQ normals are not supported in the current exporter
    19.  
    20.  
    21.  
    22. 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 :/
    And i'm far from easy with python and moreover with how blender works for guessing what i should do :(

    do you think you could help me with this please ?

    thanks in advance :)

    Happy unitying !

    EDIT:
    i finally made it work...... it seems it was unity's code that didn't do proper things :/
    https://forum.unity.com/threads/selective-import-of-blender-models.572971/#post-4564066
     
    Last edited: May 21, 2019
    Lahcene and Snorkel like this.