3
$\begingroup$

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
with bpy.data.libraries.load(filepath) as (data_from, data_to):
    data_to.objects = data_from.objects

#link object to current scene
for obj in data_to.objects:
    if obj is not None:
        scn.objects.link(obj)

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.

$\endgroup$
1
  • 1
    $\begingroup$ 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 $\endgroup$ Commented Jul 12, 2019 at 18:16

2 Answers 2

7
$\begingroup$

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 = False

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

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

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
>>> p = Path('/home/<user>/Desktop/file.blend')
>>> p.exists()
>>> True
$\endgroup$
3
  • $\begingroup$ This is great. I'm trying to replace my use of bpy.ops.wm.append. This API doesn't have any way to auto-select all imported objects though; after importing a Collection, how would you get all the objects from it to reparent them? $\endgroup$
    – GaryO
    Commented Jul 10, 2020 at 22:30
  • $\begingroup$ Added a collection example to blender.stackexchange.com/a/34543/31447 already. Does that help? @GaryO $\endgroup$
    – brockmann
    Commented Jul 10, 2020 at 22:44
  • $\begingroup$ Thanks - I'll try that. (I'm surprised the context-manager variables are still live after the context manager exits. Interesting.) $\endgroup$
    – GaryO
    Commented Jul 10, 2020 at 22:56
5
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ So I'm trying this code out for an addon, and I can't figure out how to get the script to reference the packed blend file in the addon .zip file. $\endgroup$ Commented Jul 2, 2022 at 18:53
  • $\begingroup$ I have exactly what you need, if possible please send and email to [email protected] or I think I can post it replying to this thread. $\endgroup$ Commented Jul 3, 2022 at 22:31
  • $\begingroup$ A reply here or an answer at my current question Would be greatly appreciated. $\endgroup$ Commented Jul 4, 2022 at 2:14

Not the answer you're looking for? Browse other questions tagged .