2
$\begingroup$

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?

$\endgroup$

2 Answers 2

2
$\begingroup$

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.

$\endgroup$
3
  • $\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, 2019 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, 2019 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, 2019 at 14:27
1
$\begingroup$

You can try: Create new scene named « Scene » (the original takes an new name: « Scene.001 », but you can rename it later)

Link your selected_object to « Scene » and then:

data_blocks=bpy.context.scene Etc…

Delete « Scene » Rename « Scene.001 » as « Scene »

$\endgroup$
1
  • $\begingroup$ thank you for the answer! unfortunately I do not get to code much for blender at the moment, so I can not really confirm anything. as far as I remember though, the way to go was to indeed make a new scene and then save the whole scene as a new file. I still think its a messy workaround, though, and would still try to avoid it. But thanks anyways for the effort! $\endgroup$
    – morph3us
    Sep 2, 2022 at 22:31

You must log in to answer this question.

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