Append python script for Blender 2.8 [duplicate]

I'm trying to write a script to append an Object or a Collection from a blend file to a new file. Can someone please point what I'm doing wrong?

import bpy

filepath  = "/home/lucas/Desktop/Auto_Eye/02_versions/auto_eye_v03.blend"
directory = "\\Object\\"
filename  = "auto_eye_v03"

bpy.ops.wm.append(filepath=filepath, directory=directory, filename=filename)


This code gives me this error:

Error: '\Object\auto_eye_v03': not a library
Traceback (most recent call last):
File "/Text", line 7, in <module>
File "/home/lucas/Programs/blender-2.80/2.80/scripts/modules/bpy/ops.py", line 201, in __call__
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Error: '\Object\auto_eye_v03': not a library

Error: Python script failed, check the message in the system console


And the other method that I've found here on Blender Stack Exchange.

import bpy
scn = bpy.context.scene
filepath = "/home/lucas/Desktop/Auto_Eye/02_versions/auto_eye_v03.blend"

#append object from .blend file
data_to.objects = data_from.objects

for obj in data_to.objects:
if obj is not None:


And this code is given me this error.

Traceback (most recent call last):
File "/Text.001", line 6, in <module>
OSError: load: /home/lucas/Desktop/Auto_Eye/02_versions/auto_eye_v03.blend failed to open blend file

Error: Python script failed, check the message in the system console


I run this two commands on the Blender Console.

>>> bpy.data.libraries
<bpy_collection[0], BlendDataLibraries>

>>> bpy.data.objects
<bpy_collection[5], BlendDataObjects>


So I didn't have anything on the libraries of the file, but I can't find a way do add then to the libraries.

In the "bpy.data.objects" it lists the objects that I have on the scene, so I need to learn how to append the objects from there.

• I have a few suspicions. It's saying that "auto_eye_v03" isnt a library, so either you need to define filename as "auto_eye_v03.blend" or you need to tell it which object(s) within that file to append using filename – VirtualTurtle Jul 12 '19 at 18:16

With some help on twitter, this code is working now.

import bpy

bpy.ops.wm.append(
filepath="cube.blend",
directory="/home/lucas/Desktop/cube.blend\\Object\\",
filename="Cube")


Basically,

filepath= .blend file name

directory= directory of your file and the last part is the folder inside the blend file.

filename= Is the object, material, collection or whatever you want to append.

It's recommended to use BlendDataLibraries. The solution you've posted in your question works perfectly. It's just the path to blend file that is wrong, see the error message:

"... failed to open blend file"

Following demo appends all objects from a given blend file based on their name (starting with "Cube"). Code based on: How to Link/Append a data-block using the Python API?

import bpy

# path to the blend
filepath = "/path/to/file.blend"        # OSX, LINUX
filepath = "C:\\path\\to\\file.blend"   # WINDOWS

# name of object(s) to append or link
obj_name = "Cube"

# append, set to true to keep the link to the original file

# link all objects starting with 'Cube'
data_to.objects = [name for name in data_from.objects if name.startswith(obj_name)]


For dealing with paths in general, you can use pythons pathlib module. I recommend use blenders python console to test whether a path exists or not:
>>> from pathlib import Path