For my addon, I want to pick the selected armature and save in an external file, so I wrote the following operator for doing it:

class SaveArmatures(bpy.types.Operator):
bl_idname = "armature_snippets.save_armatures"
bl_label = "Save Armatures"
bl_description = ""
bl_options = {"REGISTER", "UNDO"}

def poll(cls, context):
    if context.active_object:
        return context.active_object.type == "ARMATURE"

def execute(self, context):
    ob = {context.active_object}

    # Here the split method is just to get the first term of the string 
    #because it is a multifile addon
    path = context.user_preferences.addons[__name__.split(".")[0]].preferences.save_path

    bpy.data.libraries.write(os.path.join(path, "Snippets.blend"), ob, fake_user = True)
    return {"FINISHED"}

But when I run it raises this error. Obviously I need to create the file first but how to do it?

Traceback (most recent call last):
  File "C:\Users\Jea\AppData\Roaming\Blender Foundation\Blender\2.78\scripts\addons\Rigging_armature_snippets\loadsave.py", line 33, in execute
    bpy.data.libraries.write(os.path.join(path, "Snippets.blend"), ob, fake_user = True)
OSError: Error: Cannot open file C:\Users\Jea\Desktop\snippets\armature_snippets\Snippets.blend@ for writing: No such file or directory

location: <unknown location>:-1
  • $\begingroup$ related:blender.stackexchange.com/questions/89496/… $\endgroup$ – Jeacom Sep 5 '17 at 14:26
  • $\begingroup$ yeah, and need to creafe the file from python. Thats the problem. $\endgroup$ – Jeacom Sep 5 '17 at 14:43
  • $\begingroup$ Take a look at stackoverflow.com/q/23326310/6590534 $\endgroup$ – Dimali Sep 5 '17 at 14:52
  • 1
    $\begingroup$ Doh, should have tested this first, rather than assume you had an existing path. Can confirm that library.write(..) creates a new file, as long as you have a valid path to the folder, as outlined by @aliasguru $\endgroup$ – batFINGER Sep 5 '17 at 18:36
  • $\begingroup$ Wieeird, why not create the file in one go since it creates the directory. $\endgroup$ – Jeacom Sep 5 '17 at 22:54

Writing data into .blend files which are not existing yet works just fine. What you need to pay attention to is to make sure that the directory where the file is supposed to be stored in exists.

So in your given example, check if the folder C:\Users\Jea\Desktop\snippets\armature_snippets\ exists using the os.path module, and if not, create it:

if not os.path.exists(directory):

I'm on a Linux machine here, so I cannot check if the Python implementation of these methods on Windows is case sensitive or not. On Linux it is, so '/somePath' is considered something else than '/somepath'. Maybe you came across that?

|improve this answer|||||
  • $\begingroup$ I tried and apparently bpy.data.libraries.write() does creates a directory but not the file itself. $\endgroup$ – Jeacom Sep 5 '17 at 21:31
  • $\begingroup$ oops, now worked, was just the wrong path, sory. $\endgroup$ – Jeacom Sep 5 '17 at 22:51

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.