0
$\begingroup$

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:

@persistent
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
    bpy.ops.file.pack_all()
    bpy.ops.image.save_dirty()

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

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

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?

$\endgroup$
  • $\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.