To append all objects from an external blend file to the scene, we can use the BlendDataLibraries mechanism.
Each Blend file along with its main function of working with scenes can also perform the functions of a library – a container filled with meshes, materials, brushes, and other objects that can be loaded into our current working scene.
To access the contents of the blend file and import the necessary objects from it, we can use the “bpy.data.libraries.load” method, specifying the full path to the file containing the required data in its parameters.
1 2 3 4 5 |
import bpy src_path='_PATH_TO_FILR_.blend' with bpy.data.libraries.load(src_path) as (data_from, data_to): |
When called in this way, the method will return two objects of the “bpy_lib” type. We can use them as lists of “bpy.data” attributes with which we can interact.
For example, the “data_from” attribute names list:
1 2 3 4 5 6 7 |
print(dir(data_from)) # ['actions', 'armatures', 'brushes', 'cache_files', 'cameras', 'collections', 'curves', 'fonts', 'grease_pencils', 'hairs', 'images', 'lattices', 'lightprobes', 'lights', 'linestyles', 'masks', 'materials', 'meshes', 'metaballs', 'movieclips', 'node_groups', 'objects', 'paint_curves', 'palettes', 'particles', 'pointclouds', 'scenes', 'screens', 'simulations', 'sounds', 'speakers', 'texts', 'textures', 'volumes', 'workspaces', 'worlds'] |
To append all objects from the source file to our scene, we need the “objects” attribute.
We can get the required data by simply assigning data_to = data_from:
1 2 |
with bpy.data.libraries.load(src_path) as (data_from, data_to): data_to.objects = data_from.objects |
After that, cycling through all the objects from “data_to”, we can add them to the scene:
1 2 |
for obj in data_to.objects: bpy.context.scene.collection.objects.link(obj) |
Full code for adding all objects from an external file to the scene:
1 2 3 4 5 6 7 8 9 |
import bpy src_path='_PATH_TO_FILR_.blend' with bpy.data.libraries.load(src_path) as (data_from, data_to): data_to.objects = data_from.objects for obj in data_to.objects: bpy.context.scene.collection.objects.link(obj) |