Graechan wrote:
the highlighted section is what I'm not sure of,(e.g. how to add new layers to an existing list)
To add a new element to a list of elements, you would use 'cons', which is short for "construct". The new element is placed at the
front of the list (to avoid having to walk along the length of the list to find its end).
The following code snippet uses
"named let" to loop through each of the layers. This is generally considered preferable to using "while" loops since there is no need to use 'set!' to change the state of a variable. Note that I use 'reverse' on the final list of visibles to maintain the top-to-bottom ordering of the layers (this is necessary because 'cons'ing builds the list in reverse order).
(let ((visibles (let loop ((layers (vector->list (cadr (gimp-image-get-layers image))))
(visibles '()) )
(if (null? layers)
(reverse visibles)
(loop (cdr layers)
(if (zero? (car (gimp-drawable-get-visible layer)))
visibles
(cons (car layers) visibles) ))))))
; To hide all of the originally visible layers
(map (lambda (x) (gimp-drawable-set-visible x FALSE)) visibles)
;
; DO STUFF HERE
;
; To restore all of the originally visible layers to being visible
(map (lambda (x) (gimp-drawable-set-visible x TRUE)) visibles)
)
Alternately, you could use 'for-each' instead of 'map'. 'for-each' is acceptable whenever you don't care about the result being returned which is the case here; however, I just prefer to use 'map' since it works either way.
Another approach to obtaining the visibles is to use folding. Script-fu's 'foldr' function is not exactly the same as the traditional 'fold-right', but it does serve the same purpose.
(let ((visibles (foldr (lambda (a x)
(if (zero? (car (gimp-drawable-get-visible x)))
a
(cons x a) ))
'()
(reverse (vector->list (cadr (gimp-image-get-layers image)))) )))
;
;
;