12
$\begingroup$

Is it possible to import an object from a library file through Python without using bpy.ops.wm.link_append? I want an object to appear in bpy.data.objects without updating current scene.

$\endgroup$

2 Answers 2

12
$\begingroup$

Since Blender 2.72, there are two separate operators:

  • bpy.ops.wm.link()
  • bpy.ops.wm.append()

Apart from operators, there's also a lower-level API to import datablocks (selectively) from .blends: bpy.data.libraries.load()

The docs include a few examples how you can use this function.

$\endgroup$
1
  • $\begingroup$ +1. It seems that bpy.ops.wm.link() doesn't support wildcard (blender.stackexchange.com/questions/69985/…)? As a workaround, I used the code by @der_die_das_jojo, but that actually appends everything over, although the name is scn.objects.link()... Any better ideas? $\endgroup$ Commented Dec 27, 2016 at 23:35
12
$\begingroup$

Here is an example how to use bpy.data.libraries.load() and link it to current scene

import bpy
scn = bpy.context.scene
filepath = "D:\\File.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.collection.objects.link(obj)
$\endgroup$
1
  • $\begingroup$ I often get EXCEPTION_ACCESS_VIOLATION on line: scn.objects.link(obj). It occurs randomly but almost on every other attempt. $\endgroup$
    – Logic1
    Commented Jul 14, 2018 at 4:43

You must log in to answer this question.

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