Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Feedback Blender import process - using Collections to import a subset of models

Discussion in 'External Tools' started by Waz, May 23, 2020.

  1. Waz

    Waz

    Joined:
    May 1, 2010
    Posts:
    257
    There are two ways to import .blend models into Unity:
    1. export the specific models you need as FBX from Blender, import the FBX
    2. import the whole .blend file, ignore the parts you don't need
    Both of these have significant workflow hurdles. The first is not automatic, so can be a source of errors in addition to taking extra effort, but it has a huge advantage over the second method: you only need to import the game-ready assets, not high-poly models that are not to be used from Unity and which slow the import significantly.

    In the past, we would kludge the Unity-BlenderToFBX.py to skip certain assets, based on tags in the name or by using the hide_render Blender object property. Both of these are ugly kludges, and I don't think anyone ever suggested Unity should do it that way.

    With the Collections feature of Blender 2.8 however, there's an opportunity to do a much better job.

    Currently, I'm using a modified Unity-BlenderToFBX.py that excludes all assets in a collection named "Aux", and it works extremely well. Ideally, the Blender ModelImporter would show a list of Collections and allow them to be toggled on and off.

    For those interested, this is the relevant code snippet:

    Code (Python):
    1. if blender280:
    2.     import bpy.ops
    3.     render = []
    4.     override = bpy.context.copy()
    5.     exclude = set()
    6.     for c in bpy.data.collections:
    7.         if c.name == "Aux":
    8.             for obj in c.all_objects:
    9.                 exclude.add(obj)
    10.     if len(exclude) == 0:
    11.         # Better way: import anything not in the Aux collection:
    12.         for obj in bpy.context.view_layer.objects:
    13.             if not obj.hide_render:
    14.                 render.append(obj)
    15.     else:
    16.         # Kludge way: skip objects which don't have the Render flag set:
    17.         for obj in bpy.context.view_layer.objects:
    18.             if obj not in exclude:
    19.                 render.append(obj)
    20.  
    21.     override['selected_objects'] = render # select objects to be exported
    22.     bpy.ops.export_scene.fbx(
    23.         override,
    24.         filepath=outfile,
    25.         check_existing=False,
    26.         use_selection=True, # USe the selection set above
    27.         use_active_collection=False,
    28.  
     
unityunity