Search code examples
gimpscript-fu

Invalid ID for argument 'layer' in GIMP script-fu


I'm using the following script to batch-process some image files with GIMP script-fu:

       (let* ((filename (car filelist))
              (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
              (imagelayer (car (gimp-image-get-layers image)))
              (bglayer (car (gimp-layer-new image 8400 5939 1 ""bg"" 100 LAYER-MODE-NORMAL))))
         (gimp-image-add-layer image bglayer 1)
         (gimp-layer-set-offsets (car (gimp-image-get-layers image)) 0 870)

Line 2 loads an image, line 3 gets the single layer of the image, line 4 creates a new background layer, line 5 adds the new layer to the image, and line 6 sets the offsets of the image layer.

However line 6 throws the following error:

GIMP-Error: Calling error for procedure 'gimp-layer-set-offsets':
Procedure 'gimp-layer-set-offsets' has been called with an invalid ID for 
argument 'layer'. Most likely a plug-in is trying to work on a layer that 
doesn't exist any longer.

I tried to change line 6 to the following, but I'm getting the same error:

         (gimp-layer-set-offsets imagelayer 0 870)

Strange thing is, the error does not appear always, sometimes the routine runs through without an error.

Is this a GIMP bug or an error in my script?


Solution

  • gimp-image-get-layers returns a list of 2 values, the number of layers and a list of the layer ids.

    By using car, you have selected to use the number of layers value as the layer id.

    Try using gimp-image-get-active-layer instead.