4
$\begingroup$

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"}

@classmethod
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
$\endgroup$
5

1 Answer 1

3
$\begingroup$

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):
    os.makedirs(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?

$\endgroup$
2
  • $\begingroup$ I tried and apparently bpy.data.libraries.write() does creates a directory but not the file itself. $\endgroup$ Commented Sep 5, 2017 at 21:31
  • $\begingroup$ oops, now worked, was just the wrong path, sory. $\endgroup$ Commented Sep 5, 2017 at 22:51

You must log in to answer this question.

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