Undo Operations in a Script - Printable Version +- Gimp-Forum.net (https://www.gimp-forum.net) +-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP) +--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP) +---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions) +---- Thread: Undo Operations in a Script (/Thread-Undo-Operations-in-a-Script) |
Undo Operations in a Script - BaconWizard17 - 03-08-2023 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). RE: Undo Operations in a Script - Ofnuts - 03-09-2023 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##') 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. RE: Undo Operations in a Script - BaconWizard17 - 03-17-2023 (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? RE: Undo Operations in a Script - Ofnuts - 03-18-2023 (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. 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. RE: Undo Operations in a Script - BaconWizard17 - 04-16-2023 (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. 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. |