Gimp-Forum.net

Full Version: Undo Operations in a Script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is it possible to have a script that can undo its own actions in the middle of operating? For example:
- An image with layers A, B, and C
- Hide layer C
- Flatten image
- Export image
- Undo the flattening and hiding to get the image back to its original state
- Hide layer B
- Flatten image
- Export image

I know that undo groups can be used to manually undo several steps of a script simultaneously after the script runs, but I wasn't sure if the script could undo its own actions automatically (mainly irreversible ones, like flattening the image or indexing colors).
Not as far as I know... But in general , scripts know what they are doing (and would have to keep an accurate track if how many undo-worthy steps have been performed, even across loops).

In your case(*), you can do a gimp_layer_new_from_visible() and pass the resulting layer to the export API. It seems that you don't even need to add the layer to the image:

Code:
layer=pdb.gimp_layer_new_from_visible(image,image,'##IMGUR##')
pdb.file_png_save2(image,layer,file,file,False,3,True,True,False,False,False,False,True)
gimp.delete(layer)

Remembering which layers have been made visible/invisible and restoring their state is trivial.

(*) In more complicated cases, you just duplicate the image, do your thing, and throw away the copy... This may be faster (and unexpectedly take less RAM) because you can totally disable the undo mechanism.

PS: This looks a lot like what my ofn-export-layer-combinations does... feel free to steal code.
(03-09-2023, 04:32 PM)Ofnuts Wrote: [ -> ]PS: This looks a lot like what my ofn-export-layer-combinations does... feel free to steal code.

Does it use the gimp_layer_new_from_visible method or the duplicating method?
(03-17-2023, 10:18 PM)BaconWizard17 Wrote: [ -> ]
(03-09-2023, 04:32 PM)Ofnuts Wrote: [ -> ]PS: This looks a lot like what my ofn-export-layer-combinations does... feel free to steal code.

Does it use the gimp_layer_new_from_visible method or the duplicating method?

That one does a gimp_layer_new_from_visible .

If you want to be fancy, you can use the save dialogs with RUN_INTERACTIVE and RUN_WITH_LAST_VALS, so you get an option dialog when saving the first image, and the rest reuses the same options. But when you use RUN_WITH_LAST_VALS Gimp saves the whole image and not just the layer, so you have to duplicate the "visible" layer into a new image just to save it.
(03-18-2023, 10:58 AM)Ofnuts Wrote: [ -> ]
(03-17-2023, 10:18 PM)BaconWizard17 Wrote: [ -> ]
(03-09-2023, 04:32 PM)Ofnuts Wrote: [ -> ]PS: This looks a lot like what my ofn-export-layer-combinations does... feel free to steal code.

Does it use the gimp_layer_new_from_visible method or the duplicating method?

That one does a gimp_layer_new_from_visible .

If you want to be fancy, you can use the save dialogs with RUN_INTERACTIVE and RUN_WITH_LAST_VALS, so you get an option dialog when saving the first image, and the rest reuses the same options. But when you use RUN_WITH_LAST_VALS Gimp saves the whole image and not just the layer, so you have to duplicate the "visible" layer into a new image just to save it.

I appreciate the pointers! The example that I gave is pretty simple, but I think the duplicate and discard option might work well for me. If I run into any issues, I'll definitely come back and ask for more help.