12

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.

iKlsR
  • 43,379
  • 12
  • 156
  • 189
user2683246
  • 337
  • 4
  • 11

2 Answers2

12

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.

CodeManX
  • 29,298
  • 3
  • 89
  • 128
  • +1. It seems that bpy.ops.wm.link() doesn't support wildcard (http://blender.stackexchange.com/questions/69985/link-all-objects-meshes-and-materials-from-another-file-with-wildcard)? 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? – Sibbs Gambling Dec 27 '16 at 23:35
12

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)

Gorgious
  • 30,723
  • 2
  • 44
  • 101
  • I often get EXCEPTION_ACCESS_VIOLATION on line: scn.objects.link(obj). It occurs randomly but almost on every other attempt. – Logic1 Jul 14 '18 at 04:43