# Help with add-on to auto save image when I save .blend

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

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

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()


• 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 ? – MCunha98 Sep 15 '17 at 23:41
• If you want code to be executed on every save, it has to run on every save. I'm not sure what you mean. – dr. Sybren Sep 16 '17 at 10:11
• 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 – MCunha98 Sep 18 '17 at 1:41
• 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. – dr. Sybren Sep 18 '17 at 7:46