Loading Libraries

I’ve been trying to edit a Python script I wrote for 2.49b to 2.6x. I have a function that loads data from a library, but I don’t know how to replace it with what is available from the API in 2.62.


def load_objects(filename,obj_list):
 bpy.types.Library.Open(filename)
 cfile_objs = Library.Datablocks("Object")
 for c in obj_list:
  if member(c,cfile_objs):
   obW = Library.Load(c,"Object",0)
 bpy.types.Library.Update()
 bpy.types.Library.Close()

Blender says that bpy.types.Library.Open was not found. I couldn’t find anything from http://www.blender.org/documentation/blender_python_api_2_62_1/bpy.types.Library.html?highlight=library#bpy.types.Library that opens a file. In general, I am having a lot of trouble understanding the Python API documentation for 2.6x, even though I had no trouble understand it for 2.49b.

Try this:


with bpy.data.libraries.load(filepath) as (src, _):
    try:
        objlist = [{'name':obj} for obj in src.objects]
    except UnicodeDecodeError as detail:
        print(detail)
bpy.ops.wm.link_append(directory=filepath + '/Object/', link=False, autoselect=True, files=objlist)

Thanks for the help, but that code doesn’t quite do what I want. It loads all objects from the source file, and I only want to load the objects in obj_list (obj_list is a list of strings). I tried modifying the code that you gave:


def load_objects(filename,obj_list):
 objlist = []
 with bpy.data.libraries.load(filename) as (src, _):
  try:
   for obj in src.objects:
    if obj in obj_list:
     objlist.append({'name':obj})
  except UnicodeDecodeError as detail:
   print(detail)
 bpy.ops.wm.link_append(directory=filename + '/Object/', link=False, autoselect=True, files=objlist)

but when I did this I got an error on line 180 of ops.py saying “Nothing indicated”. I made another attempt


def load_objects(filename,obj_list):
    with bpy.data.libraries.load(filename) as (src, _):
        try:
            objlist1 = [{'name':obj} for obj in src.objects]
            for i in objlist1:
                if not(i in obj_list):
                    objlist1.remove(i)
        except UnicodeDecodeError as detail:
            print(detail)
    bpy.ops.wm.link_append(directory=filename + '/Object/', link=False, autoselect=True, files=objlist1)

but this also loaded all objects instead of just the ones in obj_list.

The objlist is a list of dictionaries, so your list of strings will never be the same.
But comparing only the value of this dict should help:


def load_objects(filename,obj_list):
    objlist = []
    with bpy.data.libraries.load(filename) as (src, _):
       try:
           for obj in src.objects:
               if obj.get('name') in obj_list:    # only compare the value of the dict
                   objlist.append(obj)
       except UnicodeDecodeError as detail:
           print(detail)
    bpy.ops.wm.link_append(directory=filename + '/Object/', link=False, autoselect=True, files=objlist)

I tried that code and on line “if obj.get(‘name’) in obj_list:” I got an error stating “AttributeError: ‘str’ object has no attribute ‘get’”.

Sry 4096 my fault (Shame on me). But this should work:


def load_objects(filename,obj_list):
    objlist = []
    with bpy.data.libraries.load(filename) as (src, _):
       try:
           for obj in src.objects:
               temp_dic = [{'name':obj} for obj in src.objects]
       except UnicodeDecodeError as detail:
           print(detail)
       for obj in temp_dic:
           if obj.get('name') in obj_list:
               objlist.append(obj)
    bpy.ops.wm.link_append(directory=filename + '/Object/', link=False, autoselect=True, files=objlist)

1 Like

That seems to have worked. Thanks for the help.