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