GIMP Script-fu

Check-in [9d264df06f]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fixed handling of layer groups.
Timelines: family | ancestors | sg-layer-bake
Files: files | file ages | folders
SHA1:9d264df06fb1e0184a0970375049f124b664600b
User & Date: saul 2015-05-22 22:58:59
Context
2015-05-22
22:58
Fixed handling of layer groups. Leaf check-in: 9d264df06f user: saul tags: sg-layer-bake
21:36
Script per liquidape (IRC request) check-in: 43ae3729f9 user: saul tags: sg-layer-bake
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to sg-layer-bake.scm.

5
6
7
8
9
10
11




























































12
13
14
15

16
17
18
19
20
21
22
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.

(define (script-fu-sg-layer-bake image)




























































  (gimp-image-undo-group-start image)
  (let ((composite-layer (car (gimp-layer-new-from-visible image image "composite")))
        (orig-sel (car (gimp-selection-save image))))
    (let loop ((layers (vector->list (cadr (gimp-image-get-layers image)))))

      (unless (null? layers)
        (unless (zero? (car (gimp-drawable-has-alpha (car layers))))
          (gimp-image-set-active-layer image (car layers))
          (let ((layer (car (gimp-layer-copy composite-layer TRUE)))
                (name (car (gimp-item-get-name (car layers)))))
            (gimp-image-insert-layer image layer 0 -1)
            (gimp-image-select-item image CHANNEL-OP-REPLACE (car layers))







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



<
>







5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

75
76
77
78
79
80
81
82
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.

(define (script-fu-sg-layer-bake image)
 
    ; get-layers takes an option 'mode' argument (that is not
    ; available with the 'gimp-image-get-layers' procedure).
    ; if 'mode' is either 0 or omitted altogether return just the 
    ;   top-level layers and fundaments
    ; If 'mode' is 1 then a nested list of all real layers (no
    ;   fundaments) is returned.
    ; If 'mode' is 2 then a nested list is returned, where the first
    ;   item in each sub-list is the fundament of the group.
    ;
    (define (get-layers image . mode)
      (let ((top-layers (vector->list (cadr (gimp-image-get-layers image)))))
        (if (or (null? mode) (zero? (car mode)))
          top-layers
          (let loop ((top-layers top-layers)
                     (layers '()))
            (if (null? top-layers)
              (reverse layers)
              (loop (cdr top-layers)
                    (cons (if (zero? (car (gimp-item-is-group (car top-layers))))
                            (car top-layers)
                            (if (= (car mode) 2)
                              (cons (car top-layers)
                                    (get-children (car top-layers) (car mode)))
                              (get-children (car top-layers) (car mode))))
                          layers)))))))
 
    ; get-children takes an option 'mode' argument (that is not
    ; available with the 'gimp-item-get-children' procedure).
    ; if 'mode' is either 0 or omitted altogether return just the 
    ;   top-level layers and fundaments
    ; If 'mode' is 1 then a nested list of all real layers (no
    ;   fundaments) is returned.
    ; If 'mode' is 2 then a nested list is returned, where the first
    ;   item in each sub-list is the fundament of the group.
    ;
    ; similar to 'get-layers' but for a group fundament
    ;
    (define (get-children fundament . mode)
      (let ((top-layers (vector->list (cadr (gimp-item-get-children fundament)))))
        (if (or (null? mode) (zero? (car mode)))
          top-layers
          (let loop ((top-layers top-layers)
                     (layers '()))
            (if (null? top-layers)
              (reverse layers)
              (loop (cdr top-layers)
                    (cons (if (zero? (car (gimp-item-is-group (car top-layers))))
                            (car top-layers)
                            (if (= (car mode) 2)
                              (cons (car top-layers)
                                    (get-children (car top-layers) (car mode)))
                              (get-children (car top-layers) (car mode))))
                          layers)))))))
    (define (flatten x)
        (cond ((null? x) '())
              ((not (pair? x)) (list x))
              (else (append (flatten (car x))
                            (flatten (cdr x))))))                          
  
  (gimp-image-undo-group-start image)
  (let ((composite-layer (car (gimp-layer-new-from-visible image image "composite")))
        (orig-sel (car (gimp-selection-save image))))

    (let loop ((layers (flatten (get-layers image 1))))
      (unless (null? layers)
        (unless (zero? (car (gimp-drawable-has-alpha (car layers))))
          (gimp-image-set-active-layer image (car layers))
          (let ((layer (car (gimp-layer-copy composite-layer TRUE)))
                (name (car (gimp-item-get-name (car layers)))))
            (gimp-image-insert-layer image layer 0 -1)
            (gimp-image-select-item image CHANNEL-OP-REPLACE (car layers))