Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Asset Importing & Exporting' started by Waz, May 23, 2020.

  1. Waz

    Waz

    Joined:
    May 1, 2010
    Posts:
    287
    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.  
     
    Melodicpinpon likes this.
  2. Melodicpinpon

    Melodicpinpon

    Joined:
    Feb 11, 2020
    Posts:
    5
    Thank you for sharing!

    I have two questions:
    -is there a way to import the objects with their hierarchy from Blender to Unity?
    -Is there a way to import curves from Blender to Unity?

    I would like to spare all the extra work to import this file into unity:
    https://www.z-anatomy.com/