0

I'm new to Script-Fu and am trying to write a script which takes in a layer name and color and recolors all black pixels in that layer the specified color. My code is below. I register it using script-fu-register and calling it from within the graphic interface. When I try to run it, I get the illegal function error. It seems to be something related to gimp-image-get-layer-by-name (when I comment out that part, the function that is supposed to save the file as a png runs fine). Would be very grateful for any suggestions!

(define (script-fu-recolor-layer image color layername imgoutname)
(gimp-image-undo-group-start image)
(gimp-selection-none image)
(gimp-context-set-foreground color)

(gimp-message (number->string (car (gimp-image-get-layer-by-name image layername))))

(let*
    (
        (activelayer (car (gimp-image-get-layer-by-name image layername)))

        )

    (

        (gimp-image-select-color image 0 activelayer '(0 0 0))

        (gimp-edit-bucket-fill activelayer FG-BUCKET-FILL NORMAL-MODE 100 0 0 0 0)

        (gimp-item-set-visible activelayer 1)

        ) 
)

; source: https://stackoverflow.com/questions/49922377/how-to-export-flattened-image-with-gimp-script-fu
(let* (
    (duplicateImg (car (gimp-image-duplicate image) ) )
    )
    (let* (
        (flatLayer (car (gimp-image-flatten duplicateImg) ) )
        )

(
    (file-png-save 1 duplicateImg flatLayer imgoutname imgoutname 1 0 0 0 0 0 0)
)
)
)

(gimp-image-undo-group-end image)
(gimp-displays-flush)
)
listopad
  • 1
  • 1
  • How is the script registered and how do you call it? Also, if the layer is the 2nd argument of the plugin after `image`, it is automatically set to the active layer when the script is called, no need to set a specific name. This is the canonical way to work on a layer. – xenoid Aug 23 '22 at 17:40
  • @xenoid I'm registering it using script-fu-register and calling it from within the graphic interface. Thanks for the tip re: the layer fed in as the 2nd argument automatically becoming active -- but I still had to feed layerid into the functions 'gimp-image-select-color', 'gimp-edit-bucket-fill' and 'gimp-item-set-visible', so I need to retrieve the ID – listopad Aug 24 '22 at 15:05
  • The "layer argument" is actually whatever you need to pass to functions that take a layer or drawable). See for instance the `tdrawable` arg in `clothify.scm` (part of the scripts installed with Gimp). – xenoid Aug 24 '22 at 15:31

1 Answers1

0

So for whatever reason the code above throws an error but if I declare the active layer variable using "define" it runs fine:

(define activelayer (car (gimp-image-get-layer-by-name image layername)))

Instead of assigning it within the let* clause. So changing this worked for me -- but I still don't understand why it didn't work with let*.

listopad
  • 1
  • 1