Selective import of blender models

Hey guys :slight_smile:

I wonder wether there’s a way to import blender objects selectively.

I mean, in my Blender scene i got ‘The_Object’ i want to import and all other objects that i dont want to import in unity but that are usefull for… AO/lighting bake-in-blender…

I thought about blender layers ( simple and efficient way to show/hide things of a scene in blender ) but they are ALL imported in unity. ( would have been nice that unity exclude layer 19 from import. i guess it would just a conditionnal if(object_layer==19) discard_object_import(); )

anybody has a clue/trick/tutorial/import_script for this please ?

this would be… GREAAAAT !!!

:wink:

happy Unitying !

hmmm okay i guess without any answer that it’s not possible :confused:

I tried to make separate blend files and link them in blender but unity also imports linked objects :((

however, if someone have an idea or a technique for selective model import from blender files, i’d be fond of it :wink:

happy unitying !!!

Are you using the .blend file directly in Unity? It is generally better to export as fbx from Blender, there is an option when you export to only save selected objects that you can use.

1 Like

Use the FBX exporter.

Select the objects you want to export.

File → Export → FBX

Choose MAIN, tick “selected Objects”, change Apply Scale to “FBX all”

choose your location, and click “export FBX” in the upper right.

Seems lots of ppl don’t use blender <—> unity for saying export as this, never use that… XD lol

WORKFLOW is the master word when creating 3D. and the right way of doing things is to make then as smooth as possible. Imagine if every 10 meters, you should stop your car, get outta it, close the doors, wait 17 seconds, open the doors get into it, start engine, make 10 more meters and redo the whole shit ? :smile: lol

hopefully U3D team and Blender team did things in a much more intelligent way ( than the common one: NEVER USE BLENDS… lol )

There’s a lil script written in python bundled with unity. it is called
Unity-BlenderToFBX.py
and is located on a win machine
in programfiles/unity/hub/editor/theVersionYouUse/data/tools

you who ( with no doubt ) write some lil scripts in unity will be aware of what to do for editing this script and change it for your specific needs in importing your work into unity.

for example, i model with blender and create some light/shadow/mist/etc meshes that are used in texture bakes, but that i NEVER want in unity. I just don’t want to see them in unity as they are annoying and useless. but i need them in blender…
imagine you can put a thing in blender saying ‘i dont want this in unity’…
YES YOU CAN !!!

lets assume that you exclude objects in unity from a char in their name
in your blend you got 2 objects:
one named cube
and
one named _sphere

the ‘_’ will be the object excluder in unity3D import.

for this you just have to change the
Unity-BlenderToFBX.py
at the end and replace:

# 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)

with :

# 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)

and voila :slight_smile: in unity you’ll only retrieve the cube ! the sphere will be discarded !

happy blending and unitying !!!

2 Likes

It appears this don’t work properly anymore in U2019 :confused:

Anyone knowing python could help me please ?

finally i made it work with this:

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
        # deselect everything to close edit / pose mode etc.
        bpy.context.scene.objects.active = None

        # activate all layers
        for i in range(0, 20):
             bpy.data.scenes[0].layers[i] = True;
           
        # parse objects and disable those with underscore or dot
        for obj in bpy.data.objects:
            obj.select = False if obj.name[0] in '_.' else True
        kwargs = io_scene_fbx.export_fbx.defaults_unity3d()
        kwargs["use_selection"] = True
        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)

happy unitying :slight_smile:

1 Like

I found that making a collection called export and deactivating all the other collections gave me better control btw.

2 Likes

sorry i dunno what are collections :confused:
but am happy if this works for you :slight_smile:

happy unitying !

Hi !

Is there any way to get access to the generated FBX file ?

Just want to reemphasize what spiraloid said, this should be the go-to solution for anyone solving in 2021.

Just disable (uncheck) any objects you don’t want to take with you, save, and unity will magically update.
If you absolutely can’t do this, link the object you need into a new file.

Hi all :slight_smile:

Note that since blender 3.0.0+ this script won’t work.
Here’s the version with complete blender version testing…

import bpy.ops
import bpy
major = bpy.app.version[0]
minor = bpy.app.version[1]
underminor = bpy.app.version[2]
#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)
# Blender versions tests come here:
if (major == 2 and minor >= 80) or (major >= 3 and minor >= 0): # blender > 2.79 ( including 3.0+ )

    # ---> changes for B2.9 to U3D 18-10-2020
    # blender 2.8 or newer
 

    # 1st of all show/activate all collections
    for coll in bpy.data.collections:
        coll.hide_render = False
        coll.hide_select = False


    # deselect everything
    bpy.ops.object.select_all(action='DESELECT')
   
    # then parse all objects list.
    # activate the current one and only inactivate it if is has a '_' or '.' ath the start of its name.
    # also inactivate it if it's a linked object :-)
    for obj in bpy.data.objects:
        obj.select_set(True)
        if obj.name[0] in '_.':
            obj.select_set(False)
        the_obj = bpy.data.objects[obj.name]
        if the_obj.library != None:
            obj.select_set(False)

               
    bpy.ops.export_scene.fbx(filepath=outfile,
        check_existing=False,
        use_selection=True,  #use_selection=False,
        use_active_collection=False,
        object_types= {'ARMATURE','MESH'}, #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',
        use_space_transform=False, # modifs recentes pour reorientation dans unity ( need bake axis conversion at import )
        axis_forward='-Y',  # modifs recentes pour reorientation dans unity ( need bake axis conversion at import )
        axis_up='Z'  # modifs recentes pour reorientation dans unity ( need bake axis conversion at import )
    )
    # <--- changes for B2.9 tu U3D 18-10-2020
   
else:

    # blender 2.79 or older
    import math
    from mathutils import Matrix
    # -90 degrees
    mtx4_x90n = Matrix.Rotation(-math.pi / 2.0, 4, 'X')
    class FakeOp:
        def report(self, tp, msg):
            print("%s: %s" % (tp, msg))
    exportObjects = ['ARMATURE', 'EMPTY', 'MESH']
    if minor <= 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='OFzF',
            BATCH_OWN_DIR=False)
    else:
        # 2.59 and later
        # deselect everything to close edit / pose mode etc.
        bpy.context.scene.objects.active = None

        # activate all layers
        for i in range(0, 20):
             bpy.data.scenes[0].layers[i] = True
           
        # parse objects and disable those with underscore or dot and also those that are linked from othe files...

        for obj in bpy.data.objects:
            obj.select = True
            if obj.name[0] in '_.':
                obj.select = False
            the_obj = bpy.data.objects[obj.name]
            if the_obj.library != None:
                obj.select = False
        kwargs = io_scene_fbx.export_fbx.defaults_unity3d()
        kwargs["use_selection"] = True
        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)

Note that it is adapted to my needs ( with linked objects and objects starting with a _ or . discarded from import…

Happy unitying !

1 Like