16
$\begingroup$

Load and add linked object

I'm working on an add-on. The purpose is to link and un-link groups from another blend file. Thanks to stack exchange and the Blender documentation this works pretty well.

This means I can load a library and import a group from it.

with bpy.data.libraries.load(path, link=True) as (sourceData, targetData):
    targetData.groups = sourceData.groups

Then I search the library and extract the group to create an instance of the group:

instance = bpy.data.objects.new(name, None)
instance.dupli_type = 'GROUP'
instance.dupli_group = group
context.scene.objects.link(instance)

The result is a dupli-group object that links in the other blend file. (the link is persistent and will survive a reload .. that is what I want).

enter image description here

Unlink/Remove/Delete

My idea is to do the opposite. If the user does not want this link anymore, I would like to get rid of

  • the instance
  • the group
  • the linked library

This works pretty nicely too for this instance:

context.scene.objects.unlink(instance)
bpy.data.objects.remove(instance)

and the group:

group.user_clear()
bpy.data.groups.remove(group)

I saw (in the outliner/Blendfile)there is still a user to the library. I guess it is the group object. So I removed that too:

for object in  library.users_id:
    for object in  library.users_id:
        object.user_clear()
        bpy.data.objects.remove(object)

Now the outline shows me the library without any user ... but

enter image description here

How can I unload/remove/delete the library without reloading the blend file?

It seems there is no API call to do that. bpy.data.libraries.remove() does not exist. There is a load but no unload.

Saving/Loading the file removes the library. But this is far away from any comfort.

Reloading the blend file destroys the current context. E.g. undo would not be possible anymore. It also involves unnecessary file operations, that leads to another cycle in the backup files. Beside of that it simply feels incorrect to use such a workaround (Imagine you need to restart your PC each time you delete a file)

I found this one: Q: Proper way to remove unused linked Group data-blocks but this does not answer my question as I want to get rid of the library not just the group.

Do I miss something?

Thank you Monster

$\endgroup$
2
  • 3
    $\begingroup$ Great question! Would be awesome to have this as add-on. Major issue is removing the group IMO, unfortunately I also could not find a proper way of doing this. Why not starting a bounty to get more attention? $\endgroup$
    – p2or
    Commented Jan 15, 2016 at 10:49
  • $\begingroup$ I really think this is not implemented. I doubt a bounty will help there any further. $\endgroup$
    – Monster
    Commented Jan 15, 2016 at 19:53

3 Answers 3

6
$\begingroup$

Quite an old thread, but it seems there is finally a solution available. Newer versions of Blender provide the batch_remove method in the bpy.types.BlendData type. So you can call:

bpy.data.batch_remove(ids=(my_library,))

my_library is a bpy.types.Library object in this case and no string/name.

Keep in mind that to this date, the documentation states, that this method is considered experimental, but as long as you don't deal with blend file critical data blocks like scenes, it seems you should be fine.

$\endgroup$
0
$\begingroup$

Straight forward way of removing all the libraries

enter image description here

import bpy

for lib in bpy.data.libraries:
    bpy.data.batch_remove(ids=(lib,))
$\endgroup$
-1
$\begingroup$

Using a python "with" statement you don't need to unload/remove/delete the library, because when you leave the block of with, the resources are cleanly released regardless of the outcome of the code in the block. I suggest this post to better understand the use of with.

$\endgroup$
3
  • $\begingroup$ Unfortunately I do not see how this helps. The "with" clause is used along with load() - which is a (timely) separate step. I do not know what exactly the context managers are doing beside closing the file? My experiments didn't show any success in getting rid of the library reference. $\endgroup$
    – Monster
    Commented Jul 6, 2016 at 6:52
  • $\begingroup$ You don't need to close the library, because when you go out of the with statement you have already closed it $\endgroup$
    – user36531
    Commented Jul 6, 2016 at 6:58
  • $\begingroup$ My aim is less to close it. It automatically does this as you correctly state in your answer. I want to remove the reference to the library after the last user gets removed. $\endgroup$
    – Monster
    Commented Jul 6, 2016 at 7:10

You must log in to answer this question.

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