4
$\begingroup$

I need to create an addon that load and save pieces of armatures and I want to make the addon capable of loading these saved data in any file. Almost like a preset system but with entire objects.

something like:

def when_addon_loads():

    data = load_from_user_settings("c:/file_path.txt")
    snippets = parse_all(data.split("\n"))
    context.scene.armature_snippets = snippets
$\endgroup$

1 Answer 1

5
$\begingroup$

You can save individual data blocks to blend files using the bpy.data.libraries.write() function:

import bpy

filepath = "//new_library.blend"

# write selected objects and their data to a blend file
data_blocks = set(bpy.context.selected_objects)
bpy.data.libraries.write(filepath, data_blocks)


# write all meshes starting with a capital letter and
# set them with fake-user enabled so they aren't lost on re-saving
data_blocks = {mesh for mesh in bpy.data.meshes if mesh.name[:1].isupper()}
bpy.data.libraries.write(filepath, data_blocks, fake_user=True)


# write all materials, textures and node groups to a library
data_blocks = {*bpy.data.materials, *bpy.data.textures, *bpy.data.node_groups}
bpy.data.libraries.write(filepath, data_blocks)

See the API documentation for more info.

$\endgroup$
4
  • $\begingroup$ Is it still functional? In blender 2.93.1 it is not creating the objects .. only saves the materials. $\endgroup$
    – MRL
    Aug 4, 2021 at 17:26
  • $\begingroup$ The code is meant as an example, to be read, understood, and adjusted to your needs. If you copy-paste the code and execute it as-is, it will write the same blend file three times, each call to bpy.data.libraries.write() overwriting the blend file written by the previous call. $\endgroup$
    – dr. Sybren
    Aug 11, 2021 at 11:07
  • $\begingroup$ Thank you for the answer dr.Sybren. From the example, I didn't get that I must write more than the objects. If I need to save only objects, they must be written with the scene collection (I saved even a new scene for .write(). It wasn't so clear for me at the start of understanding the problem. $\endgroup$
    – MRL
    Aug 11, 2021 at 11:15
  • 1
    $\begingroup$ If you only save the objects, that means that the file won’t even have a scene. You’ll only find the objects in the Outliner, in that case. $\endgroup$
    – dr. Sybren
    Aug 11, 2021 at 15:05

You must log in to answer this question.

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