Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Resize all layers in script-fu
#1
Hi all! I've got a bit of a question about scripting. I'm trying to get a script to resize two both layers of an image instead of just the active one, but I'm not entirely sure how to go about doing that. First, I set up the image with this script:
Code:
(define (script-fu-mua-xml2-single-preview)
    (let*
        (
            (theImageWidth  543)
            (theImageHeight 1080)
            (theImage
                (car
                    (gimp-image-new
                       theImageWidth
                       theImageHeight
                       RGB
                    )
                )
            )
            (backgroundLayer
                (car
                    (gimp-layer-new
                        theImage
                        theImageWidth
                       theImageHeight
                       RGBA-IMAGE
                       "Background"
                       100
                       LAYER-MODE-NORMAL
                    )
                )
            )
            (portraitLayer
                (car
                    (gimp-layer-new
                        theImage
                       theImageWidth
                       theImageHeight
                       RGBA-IMAGE
                       "Portraits"
                       100
                       LAYER-MODE-NORMAL
                    )
                )
            )
        )
        (gimp-image-add-layer theImage backgroundLayer 0)
        (gimp-image-add-layer theImage portraitLayer 0)
        (gimp-image-set-active-layer theImage backgroundLayer)
        (gimp-display-new theImage)
    )
)

I then do some work with that image and export it. After that, I want to double the width of the image by resizing the canvas. In doing so, I want all the layers to resize as well. Here's what I have so far:
Code:
(define (script-fu-mua-xml2-double-preview image layer)
    (gimp-image-undo-group-start image)
    (gimp-selection-none image)
    (gimp-image-resize image 1086 1080 0 0)
    (gimp-layer-resize-to-image-size layer)
    (gimp-displays-flush)
    (gimp-image-undo-group-end image)
)

I cut out some extra stuff from both scripts like some codes to add vertical/horizontal guides in various places which all works the way I want it to. Generally, I have the top layer selected when I execute the second script, so the top layer is resized, but the bottom one isn't. I want it to be able to resize both layers to the image size, though, regardless of which one I have actively selected (if it's possible to do so). How do I go about doing that?
Modder/Skinner at MarvelMods.com using GIMP to create, edit, and export textures and previews more efficiently.

My GIMP scripts hosted on GitHub
Reply
#2
(10-23-2022, 04:08 PM)BaconWizard17 Wrote: Hi all! I've got a bit of a question about scripting. I'm trying to get a script to resize two both layers of an image instead of just the active one, but I'm not entirely sure how to go about doing that. First, I set up the image with this script:
...

If you want to resize all of the layers use gimp-image-get-layers to get the number of layers and an array of layers. See Filters/Python-Fu/Console/Browse and type layers into the search box. Simply call gimp-layer-resize-to-image-size for each of the layers.
Reply
#3
(10-23-2022, 06:42 PM)programmer_ceds Wrote: See Filters/Python-Fu/Console/Browse and type layers into the search box.

I think you meant Filters ➤ Script-fu ➤ Console ➤ Browse (even if I'm pretty sure the OP would be more comfortable doing the whole thing in Python).
Reply
#4
(10-23-2022, 07:17 PM)Ofnuts Wrote: I think you meant Filters ➤ Script-fu ➤ Console ➤ Browse (even if I'm pretty sure the OP would be more comfortable doing the whole thing in Python).

It doesn't actually matter how you get at the PDB it still shows the functions in the Script-Fu form with hyphens rather than underscores.
Reply
#5
I am more familiar with Python coding than I am with Scheme, but I've gotten this far with Scheme so I may as well finish the job. I'm assuming that using gimp-image-get-layers would just be: 
Code:
(let*
    (
        (num-layers (car (gimp-image-get-layers image)))
       (layer-ids (cdr (gimp-image-get-layers image)))
    )
        
)

(I haven't done anything with multiple return values before, but from some quick googling this seems to be the right way to do that)

I haven't been able to find terrific documentation on looping in Scheme. Based on what I've seen, I was able to put this together, but it doesn't resize any layers
Code:
(define layer-resize
    (lambda (a-layer layer-ids)
        (gimp-layer-resize-to-image-size a-layer)
    )
)

Any insight?
Modder/Skinner at MarvelMods.com using GIMP to create, edit, and export textures and previews more efficiently.

My GIMP scripts hosted on GitHub
Reply
#6
(10-24-2022, 09:59 PM)BaconWizard17 Wrote: I am more familiar with Python coding than I am with Scheme, but I've gotten this far with Scheme so I may as well finish the job. I'm assuming that using gimp-image-get-layers would just be: 
Code:
(let*
   (
       (num-layers (car (gimp-image-get-layers image)))
       (layer-ids (cdr (gimp-image-get-layers image)))
   )
        
)

(I haven't done anything with multiple return values before, but from some quick googling this seems to be the right way to do that)

I haven't been able to find terrific documentation on looping in Scheme. Based on what I've seen, I was able to put this together, but it doesn't resize any layers
Code:
(define layer-resize
   (lambda (a-layer layer-ids)
       (gimp-layer-resize-to-image-size a-layer)
   )
)

Any insight?
You could try something like:

Code:
(set! x 0)
  (while (< x num_layers)
    (gimp-layer-resize-to-image-size (aref layer_ids x))
    (set! x (+ x 1))
  )    ; end - while
Reply
#7
(10-25-2022, 07:19 AM)programmer_ceds Wrote:
(10-24-2022, 09:59 PM)BaconWizard17 Wrote: I am more familiar with Python coding than I am with Scheme, but I've gotten this far with Scheme so I may as well finish the job. I'm assuming that using gimp-image-get-layers would just be: 
Code:
(let*
   (
       (num-layers (car (gimp-image-get-layers image)))
       (layer-ids (cdr (gimp-image-get-layers image)))
   )
        
)

(I haven't done anything with multiple return values before, but from some quick googling this seems to be the right way to do that)

I haven't been able to find terrific documentation on looping in Scheme. Based on what I've seen, I was able to put this together, but it doesn't resize any layers
Code:
(define layer-resize
   (lambda (a-layer layer-ids)
       (gimp-layer-resize-to-image-size a-layer)
   )
)

Any insight?
You could try something like:

Code:
 (set! x 0)
 (while (< x num_layers)
   (gimp-layer-resize-to-image-size (aref layer_ids x))
   (set! x (+ x 1))
 )    ; end - while

This got me on the right path (though I had to use "define" instead of "set!" in the first line), but it seems that it doesn't like how I'm assigning the "num_layers" and "layer_ids" variables. That makes me assume my syntax is incorrect there. I haven't been able to find anything that uses gimp-image-get-layers, so I can't find how I'm supposed to actually get the values.
Modder/Skinner at MarvelMods.com using GIMP to create, edit, and export textures and previews more efficiently.

My GIMP scripts hosted on GitHub
Reply
#8
(10-26-2022, 09:11 PM)BaconWizard17 Wrote: it seems that it doesn't like how I'm assigning the "num_layers" and "layer_ids" variables. That makes me assume my syntax is incorrect there. I haven't been able to find anything that uses gimp-image-get-layers, so I can't find how I'm supposed to actually get the values.

You are using cdr to get the layer ids, use cadr. Here's my go:

Code:
 ; Testing resize layers to image size
  (let ((num-layers (car (gimp-image-get-layers image)))
       (layer-ids (cadr (gimp-image-get-layers image)))
       (i 0))
    (while (< i num-layers)
      (gimp-layer-resize-to-image-size (vector-ref layer-ids i))
      (set! i (+ i 1))))
Reply
#9
(10-26-2022, 11:00 PM)teapot Wrote:
(10-26-2022, 09:11 PM)BaconWizard17 Wrote: it seems that it doesn't like how I'm assigning the "num_layers" and "layer_ids" variables. That makes me assume my syntax is incorrect there. I haven't been able to find anything that uses gimp-image-get-layers, so I can't find how I'm supposed to actually get the values.

You are using cdr to get the layer ids, use cadr. Here's my go:

Code:
 ; Testing resize layers to image size
  (let ((num-layers (car (gimp-image-get-layers image)))
       (layer-ids (cadr (gimp-image-get-layers image)))
       (i 0))
    (while (< i num-layers)
      (gimp-layer-resize-to-image-size (vector-ref layer-ids i))
      (set! i (+ i 1))))

That worked perfectly! Thank you for the help! 

I noticed you used "let" instead of "let*". Both variations work for me, but is there a difference between the two?
Modder/Skinner at MarvelMods.com using GIMP to create, edit, and export textures and previews more efficiently.

My GIMP scripts hosted on GitHub
Reply
#10
(10-26-2022, 11:23 PM)BaconWizard17 Wrote:
(10-26-2022, 11:00 PM)teapot Wrote:
(10-26-2022, 09:11 PM)BaconWizard17 Wrote: it seems that it doesn't like how I'm assigning the "num_layers" and "layer_ids" variables. That makes me assume my syntax is incorrect there. I haven't been able to find anything that uses gimp-image-get-layers, so I can't find how I'm supposed to actually get the values.

You are using cdr to get the layer ids, use cadr. Here's my go:

Code:
 ; Testing resize layers to image size
  (let ((num-layers (car (gimp-image-get-layers image)))
       (layer-ids (cadr (gimp-image-get-layers image)))
       (i 0))
    (while (< i num-layers)
      (gimp-layer-resize-to-image-size (vector-ref layer-ids i))
      (set! i (+ i 1))))

That worked perfectly! Thank you for the help! 

I noticed you used "let" instead of "let*". Both variations work for me, but is there a difference between the two?

Your'e welcome..

let* when the order of evaluation matters e.g. when one variable uses another one.
let when it doesn't matter.

http://computer-programming-forum.com/40...afb466.htm
Reply


Forum Jump: