3

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.

Duarte Farrajota Ramos
  • 59,425
  • 39
  • 130
  • 187
Lucas Falcão
  • 301
  • 1
  • 3
  • 8
  • 1
    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

2 Answers2

7

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
brockmann
  • 12,613
  • 4
  • 50
  • 93
  • 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? – GaryO Jul 10 '20 at 22:30
  • Added a collection example to https://blender.stackexchange.com/a/34543/31447 already. Does that help? @GaryO – brockmann Jul 10 '20 at 22:44
  • Thanks - I'll try that. (I'm surprised the context-manager variables are still live after the context manager exits. Interesting.) – GaryO Jul 10 '20 at 22:56
5

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.

Lucas Falcão
  • 301
  • 1
  • 3
  • 8