I'm trying to create a script to pack and save all images when saving the blend file. This is what I have right now:

def save_data(dummy):
    #add fake user to images
    for img in bpy.data.images:
         img.use_fake_user = True

    #pack and save the images

def register():
    if not save_data in bpy.app.handlers.save_pre:

def unregister():
    if save_data in bpy.app.handlers.save_pre:

The problem with this script is that the images must be saved from memory first for these commands to work (bpy.ops.file.pack_all() and bpy.ops.image.save_dirty()). There is bpy.ops.image.pack(as_png=True) command that will pack the images directly from memory but it runs on individual images and it doesn't seem to take effect unless the cursor is inside UV/Image Editor when running it.

Is there a good way to do what I need (pack and save the images directly from memory)? Should I have the script save the files to a temporary location and then pack them?

  • $\begingroup$ how about you get current bpy.context.area.ui_type then set it to "bpy.context.area.ui_type = 'VIEW'" then run script then restore bpy.context.area.ui_type from before? $\endgroup$ – Mobin Nov 5 '19 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.