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

  • 3
    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? – p2or Jan 15 '16 at 10:49
  • I really think this is not implemented. I doubt a bounty will help there any further. – Monster Jan 15 '16 at 19:53

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.

  • 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. – Monster Jul 6 '16 at 6:52
  • You don't need to close the library, because when you go out of the with statement you have already closed it – user36531 Jul 6 '16 at 6:58
  • 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. – Monster Jul 6 '16 at 7:10

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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