How to check whether object is linked

Hi all :slight_smile:

How to check in a blend file whether an object is linked through the file menu->link ?

bpy.data.objects… seems to be the way but i find no flag to check an object link status ?

Thanks in advance :slight_smile:

happy blending !

Just check the library attribute :wink:

cube = bpy.data.objects["Cube"]
# cube.library is None if not linked
# cube.library is an instance of <class 'bpy_types.Library'> if linked
print(cube.library.name)
# will print file_name.blend if linked

See you :slight_smile: ++
Tricotou

great !!! i got the name of all objects :smiley:

This should work… i give it a try right away.

Thanks a lot @tricotou !

EDIT: WONDERFULL !!! this works like a charm !

i use this for unity3D import from blender.
Here’s the whole script:

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 and also those that are linked from othe files...
        for obj in bpy.data.objects:
            obj.select = False if obj.name[0] in '_.' else True
            the_obj = bpy.data.objects[obj.name]
            obj.select = False if the_obj.library != None 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)

My blends have several linked objects ( for AO calculations ) and i didn’t want those linked obj to be imported in unity.

This is for this project:

Thanks again @tricotou ! this will damn simplify the objects appearance and the whole workflow !

Happy blending !

1 Like

i made a little change at the end of the script as my conditions were exclusive one_another:

        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 
 

This also allows to use linked mesh in a scene, but unlink the object for limiting it’s max draw type while not importing it in unity by adding a _ or a . at the beginning of its name
:wink:

Hi all :slight_smile:

Back there with some new things on this small python script used for blender —> unity3D import.

I switched to blender 2.9 and had to modify the 2.9 part of this script.
sot here it is :slight_smile:

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:

    # ---> changes for B2.9 tu U3D 18-10-2020

    # 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=True, #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'
    )
    # <--- changes for B2.9 tu U3D 18-10-2020
    
else:

    # blender 2.58 or newer
    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']
 
    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 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)

Happy blending !!!