In order to append the necessary meshes into the scene by name from an external blend file, we can use the wm.append operator or use the BlendDataLibraries mechanism.
You can get the contents of an external blend file by calling the bpy.data.libraries.load method:
1 |
with bpy.data.libraries.load(path_to_lib) as (data_from, data_to): |
specifying the path to the external blend file as an input parameter.
The method returns two pointers to libraries of objects: data_from – a library of objects obtained from an external blend file and data_to – an empty library into which pointers to objects loaded into the scene should be copied.
Let’s define the path to the external blend file:
1 |
path_to_lib = '//lib.blend' |
and the list of object names that we want to get from it:
1 |
names_to_append = ['Suzanne', 'Torus'] |
Now we can read this external blend file and copy the objects from the “objects” section of the data_from library into the same “objects” section of the data_to library:
1 2 |
with bpy.data.libraries.load(path_to_lib) as (data_from, data_to): data_to.objects = [mesh for mesh in data_from.objects if mesh in names_to_append] |
When the names of the required meshes are appended to the data_to library, these objects will be copied to the current scene.
Now we just need to link them to the active scene collection:
1 2 |
for obj in data_to.objects: bpy.context.collection.objects.link(obj) |