2
$\begingroup$

I'm trying to write a script that when I save the file (Ctrl + S) save the images on file too.

    bl_info = {
    "name": "Auto-save images",
    "author": "mcunha98",
    "version": (1, 0),
    "blender": (2, 78, 0),
    "location": "",
    "description": "Auto save images datablocks after save .blend",
    "warning": "Works only with external images",
    "wiki_url": "",
    "category": "Paint"}


import bpy

def save_external_images(dummy):
    for img in bpy.data.images:
        img.save()

def register():
    bpy.app.handlers.save_pre.append(save_external_images)


def unregister():
    bpy.app.handlers.save_pre.remove(save_external_images)


if __name__ == "__main__":
    register()

I saw [this thread][1] as reference , but in fact the image not save when I save the file, someone can help me to understand why ?

I don't have any kind of warning or output on console window warning about erros, simply the image file not was save

$\endgroup$
  • $\begingroup$ Can you fix the URL of the thread you're referring to? $\endgroup$ – dr. Sybren Sep 13 '17 at 0:01
  • $\begingroup$ is the source of the code above, consider this $\endgroup$ – MCunha98 Sep 14 '17 at 16:54
1
$\begingroup$

The problem is that the handler isn't persistent, so it will be registered when you first register the add-on, but as soon as you open a file it'll be gone again. Annotate it like this to make it work:

@bpy.app.handlers.persistent
def save_external_images(dummy):
    for img in bpy.data.images:
        print('Saving', img.name)
        img.save()

For more info, see the Blender API documentation.

$\endgroup$
  • $\begingroup$ And not is possible define the add-on as a 'startup' or something like that to avoid to call and call again the script to do the auto save ? $\endgroup$ – MCunha98 Sep 15 '17 at 23:41
  • $\begingroup$ If you want code to be executed on every save, it has to run on every save. I'm not sure what you mean. $\endgroup$ – dr. Sybren Sep 16 '17 at 10:11
  • $\begingroup$ My goal is when I save the file (Ctrl+S) don't need to go to the image editor and press save (Alt+S) to save the image $\endgroup$ – MCunha98 Sep 18 '17 at 1:41
  • $\begingroup$ Yes, you can create an addon out of your code. As a matter of fact, it already is. Just save it as an external file on your disk, and install it. $\endgroup$ – dr. Sybren Sep 18 '17 at 7:46

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.