-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmhl-export-layers.scm
More file actions
191 lines (159 loc) · 6.32 KB
/
Copy pathmhl-export-layers.scm
File metadata and controls
191 lines (159 loc) · 6.32 KB
1
2
3
4
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
;mhl-export-layers.scm
;==============================================================================
;MHL-Export layers
;
;GIMP Script-Fu that exports layers to separate files.
;
;Copyright (C) 2023-2026 Melon (https://github.com/Mhlov)
;
; LICENSE
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; 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.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>.
;
;==============================================================================
;Tested on GIMP 3.2.4
(define (mhl-el-get-layers list-of-layers
visible-only)
(define layers '())
(for-each
(lambda (layer)
(if
(or
(and (= TRUE visible-only)
(= TRUE (car (gimp-item-get-visible layer))))
(= FALSE visible-only))
(set! layers (append layers
(list layer)))))
; list of layers
(vector->list (car list-of-layers)))
layers)
; Make a string for filename from the layer list index
(define (mhl-el-index-to-name index
max-n)
(define name (number->string (+ index 1)))
; adding leading zeros
(let ((i 0))
(while (< i (- (string-length (number->string max-n))
(string-length name)))
(set! name (string-append "0" name))))
name)
(define (mhl-el-compile-name filename-as
layer-name
layer-index
n-of-layers)
(define name "")
; filename-as: 0-name, 1-number, 2-number-name
(cond ( ;if filename-as layer name
(= filename-as 0)
(set! name layer-name))
( ;if filename-as layer number
(= filename-as 1)
(set! name (mhl-el-index-to-name layer-index
n-of-layers)))
( ;if filename-as layer number + layer name
(= filename-as 2)
(set! name (string-append (mhl-el-index-to-name layer-index
n-of-layers)
"-"
layer-name)))
( else (throw "Something went horribly wrong -_-'")))
name)
; Hide all layers from the list
; and return a list with the initial visibility values
(define (mhl-el-hide-all-layers layers)
(define initial-values '())
(for-each
(lambda (layer)
(set! initial-values
(append initial-values (gimp-item-get-visible layer)))
(gimp-item-set-visible layer FALSE))
layers)
initial-values)
; Restore the initial visibility values in the layers from the list
(define (mhl-el-restore-visibility-of-all-layers layers
values)
(let ((i 0))
(while (< i (length layers))
(let*
(
(layer (car (list-tail layers i)))
(value (car (list-tail values i))))
(gimp-item-set-visible layer value))
(set! i (+ i 1)))))
; main
(define (mhl-export-layers image
folder
filename-as
filename-prefix
filetype
visible-only
selected-only)
; Start of the undo group
(gimp-image-undo-group-start image)
(define all-layers (vector->list (car (gimp-image-get-layers image))))
(define layers (mhl-el-get-layers (if (= TRUE selected-only)
;then
(gimp-image-get-selected-layers image)
;else
(gimp-image-get-layers image))
visible-only))
(define n-of-layers (length layers))
; Hide all layers and show only the desired one.
(define initial-visibility-values (mhl-el-hide-all-layers all-layers))
(let ((i 0))
(while (< i n-of-layers)
(let*
(
(layer (car (list-tail layers i)))
(file-name (string-append folder
"/"
filename-prefix
(mhl-el-compile-name
filename-as
(car (gimp-item-get-name layer))
i
n-of-layers)
"."
filetype)))
(gimp-item-set-visible layer TRUE)
;(gimp-message file-name)
(gimp-file-save (if (= i 0) 0 2)
image
file-name)
(gimp-item-set-visible layer FALSE)
)
(set! i (+ i 1))))
(mhl-el-restore-visibility-of-all-layers all-layers
initial-visibility-values)
; End of the undo group
(gimp-image-undo-group-end image)
; Flush all internal changes to the user interface
(gimp-displays-flush))
(script-fu-register "mhl-export-layers"
"Export Layers"
"Export layers in separate files."
"MHL <mhl@localhost>"
"MHL"
"2023"
"*"
SF-IMAGE "Image" 0
SF-DIRNAME "Folder name" "./"
SF-OPTION "Filename from layer" '("name" "number" "number-name")
SF-STRING "Filename prefix" ""
SF-STRING "File type extension" "png"
SF-TOGGLE "Visible layers only" FALSE
SF-TOGGLE "Selected layers only" FALSE
)
(script-fu-menu-register "mhl-export-layers"
"<Image>/Filters/MHL")