← Blender.Today
  • 2
  • Published 7 Apr 2020

How to Appending custom object from external blender file using relative path

blender file path and contained

How to Appending custom object from external blender file using relative path so i can pack the file with the addon i can append file easily with absolute path like this

but I could fined how to do it with relative path

# external blender file path ---> D:\blend_folder
# blender file name ---> external_blender_file.blend
# object name ---> object_2


#~~~~~~~~~~~~~~~~~~[ this with absolute path ]~~~~~~~~~~~~~~~~~~~~~
# import bpy
# bpy.ops.wm.append(filename="OBJECT_NAME", directory="/PATH/TO/BLENDFILE/NAME.blend\\Object\\")


import bpy
bpy.ops.wm.append(filename="object_2", directory="D:/blend_folder/external_blender_file.blend\\Object\\")


#~~~~~~~~~~~~~~~[ How to make it with relative path ......???? ]~~~~~~~~~~~~~~~~~

husham79
7 Apr 2020

1 Comment

husham79 Author

There is a pretty nice example using BlendDataLibraries and a relative path in the API: docs.blender.org/api/current/bpy.types.BlendDataLibraries.html

Example script to append all objects named Cube... from a blend file called link_library.blend in the same folder as the script like the add-on directory (tested):

import bpy import os

# "//" prefix is a Blender specific identifier for the current blend file
#filepath = "//link_library.blend"
#abs_filepath = bpy.path.abspath(filepath) # returns the absolute path

# os.path.abspath(__file__) returns path to the script
filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "link_library.blend")

# link all objects starting with 'Cube'
with bpy.data.libraries.load(filepath, link=False) as (data_from, data_to):
    data_to.objects = [name for name in data_from.objects if name.startswith("Cube")]

# link object to scene collection
for obj in data_to.objects:
    if obj is not None:
       bpy.context.collection.objects.link(obj)

Further reading:

blender.stackexchange.com/questions/173524/how-can-i-append-an-object-from-an-external-blender-file-using-a-relative-path-w/173628