I want to save selected objects into a separate .blend file in 2.8.

https://docs.blender.org/api/blender2.8/bpy.types.BlendDataLibraries.html suggests:

import bpy

filepath = "//new_library.blend"
data_blocks = set(bpy.context.selected_objects)
bpy.data.libraries.write(filepath, data_blocks)

I created a blank scene, added two spheres to it and saved everything. Afterwards I selected them both and let the script run through: A ".blend" file gets created, however it doesn't contain anything.

I re downloaded 2.79 and got the same result. So I am unsure where the problem lies and don't want to file a bug report immediately without having some outside input. Could somebody please clarify things for me, please?


Check the data

Chances are the objects are saved to the library blend, . but are not linked to any scene, or collection, hence the file appears empty when opened.

Check the library files bpy.data.objects collection to confirm.

Consider adding the scene datablock to the library.

  • $\begingroup$ I went ahead and foolishly did file a bug report and Brecht sorted it out in 5 minutes... developer.blender.org/T60792 Indeed, I needed to add the scene datablock AND the collections aswell. So in the end it seems I did not get the documentation right... I will update my initial answer once I got it to work! Thank you for responding! $\endgroup$
    – morph3us
    Jan 23 '19 at 12:09
  • $\begingroup$ When adding "bpy.context.scene" to the datablock, everything within the scene gets saved to the new file, not only the selection. (So it basically overrides "bpy.context.selected_objects") I tried to make some kind of context override with "ctx = bpy.context.scene.copy()", but this makes a new scene. I want the objects to stay on "bpy.data.scenes['Scene']", not move to "bpy.data.scenes['Scene.001']", however. I think I am missing something here... :( $\endgroup$
    – morph3us
    Jan 23 '19 at 13:36
  • $\begingroup$ Yeah, just like the meshes (or data part) come with the objects written to lib, so will objects linked to scene written to lib. To write to lib with scene and only selected objects could unlink unselected scene.objects, write scene, re-link. or .. run a bg script on file if no scene make one link objects to it, .. or .. hmmm Perhaps write up a new question explaining that you want to use libraries.write to save scene with only selected objects. $\endgroup$
    – batFINGER
    Jan 23 '19 at 14:27

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.