Search code examples
schemegimpscript-fu

Gimp Script-Fu: Delete a selection


I'm trying to write a script for Gimp that will crop an image, make a circular selection of what's left, invert the selection, and then delete the selection. I have the cropping, selection, and inversion part done, but the deleting is what's getting me.

(removed old code, see update)

That's the code I have. What's confusing me about the gimp-item-delete code is the item. I understand that I need to define my current selection as the item, but I'm not sure how to do this. If someone could explain how to do this, I would greatly appreciate it! Alternatively, if there's an easier way to do what I'm trying to do (but preferably still in a script), please let me know what you think. My knowledge of this coding is pretty limited, so simple explanations are appreciated.

UPDATE/EDIT: Here's the (full) updated code:

; XML2 Conversation Portrait Preview Crop
(define (script-fu-xml2-convo-preview image layer)
    (gimp-image-undo-group-start image)
    (gimp-selection-none image)
    (gimp-image-resize image 152 152 -280 -496)
    (gimp-layer-resize-to-image-size layer)
    (gimp-image-select-ellipse image 0 0 0 152 152)
    (gimp-selection-invert image)
    (gimp-displays-flush)
    (gimp-drawable-edit-clear layer)
    (gimp-selection-none image) 
    (gimp-image-undo-group-end image)
)
; populate script registration information
(script-fu-register 
    "script-fu-xml2-convo-preview"
    "XML2 Conversation Portrait Preview Crop"
    "Crops the preview window for XML2 conversation portraits."
    "BaconWizard17"
    "BaconWizard17"
    "September 2021"
    "*"
    SF-IMAGE        "Image"       0
    SF-DRAWABLE     "Layer"       0
)
; register the script within gimp menu
(script-fu-menu-register "script-fu-xml2-convo-preview" "<Image>/Marvel Mods/Skin Previews/XML2 PC")

So when I'm running this script, it appears to just crop it and doesn't delete the inverted circular selection. However, if I interact with the image in any way (click on it, duplicated it, copy/paste it, undo/redo), it will correct itself and show the properly cropped image with the deleted circular corners. When I first run the script, it also shows the incorrect image up at the top where you can pick which image you're working on, but after interacting with the image it will show the correct image. Here's an example:

The starting image that I create from the clipboard with Ctrl+Shift+V (after screenshotting it from the game using PrtScr):

How the image looks when I copy it in from my clipboard

Then, I run the script, which results in this:

How the image looks after I initially run the script. The background that I'm trying to delete is still visible

At the top of the screen, the image shows up like this (which is the top left 152x152 pixels of the starting image:

The image's thumbnail at the top of the screen, showing the wrong portion of the image

How the image looks once I interact with it in any way (clicking on it, copy/pasting it, duplicating it, undoing/redoing the operation):

The correctly cropped image after I interact with it

The thumbnail at the top of the screen after I interact with the image:

The thumbnail at the top of the screen showing the correct image

Maybe it's getting hung up on something during the operation? I'm not sure. I understand if this issue is unavoidable, but I would prefer to be able to just run the script and move on rather than having to interact with the image to get it to show up correctly. The image does have an alpha layer when it's pasted in.


Solution

  • gimp-item-delete is a memory/object management thing and is rarely used. What you are looking for is gimp-drawable-edit-clear.

    Note that it should be done (together with your final gimp-selection-none) before gimp-image-undo-group-end if you want your whole script to be undone by a single Ctrl-Z.

    Edit: It appears that your code works, it's just that the display isn't updated to show the result, which is exactly what gimp-display-flush is meant to do, so why is your code calling it before gimp-drawable-edit-clear? gimp-display-flush ought to be about the last thing done by your script.