The Blender Python API provides the ability to append or link objects from another *.blend file to the current scene using the “append” command.
To append an object (for example – a mesh) from another .blend file, we need to do the following:
- Specify the full path to the blend-file from which to get an object.
1 |
file_path = 'D:/11.blend' |
- Specify the relative path to the required object in the internal hierarchy of the blend file.
1 |
inner_path = 'Object' |
- Specify the name of the required object.
1 |
object_name = 'Suzanne' |
- And execute the “append” operator with three required parameters:
1 2 3 4 5 6 7 8 |
import bpy import os bpy.ops.wm.append( filepath=os.path.join(file_path, inner_path, object_name), directory=os.path.join(file_path, inner_path), filename=object_name ) |
After executing this code, by clicking the “Run Script” button, we will append the “Suzanne” mesh from the specified 11.blend file into the current scene.
Full code:
1 2 3 4 5 6 7 8 9 10 11 12 |
import bpy import os file_path = 'D:/11.blend' inner_path = 'Object' object_name = 'Suzanne' bpy.ops.wm.append( filepath=os.path.join(file_path, inner_path, object_name), directory=os.path.join(file_path, inner_path), filename=object_name ) |
this is legacy code, using operators is best avoided since it doesn’t run correctly sometimes.
the new way is using BlendDataLibraries https://docs.blender.org/api/blender_python_api_2_77_1/bpy.types.BlendDataLibraries.html
example:
Thank you for the information, I will study it and write the updated tutorial!
I think your example code was perfect because I only wanted to import a single material from a .blend that contains many. All of the other examples I’ve seen pull in all materials from the source blend….not what I wanted. BTW I’m using blender 4.0 and it works perfectly
While this code is nice and neat, I can’t get it to work. What am I doing wrong?
Maybe the file_path or inner_path you set are not exists?
How to do if we don’t know object name ?
This way you can use bpy.data.libraries.load to append all objects from the source file.
The filepath argument is not necessary.