8
$\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$
8
$\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$
  • $\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$ – Sibbs Gambling Dec 27 '16 at 23:35
7
$\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.objects.link(obj)
$\endgroup$
  • $\begingroup$ I often get EXCEPTION_ACCESS_VIOLATION on line: scn.objects.link(obj). It occurs randomly but almost on every other attempt. $\endgroup$ – Logic1 Jul 14 '18 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.