-
Notifications
You must be signed in to change notification settings - Fork 1
/
synfigexport.py
executable file
·330 lines (303 loc) · 9.92 KB
/
synfigexport.py
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/python
# <Image>/File/Export/Synfig
# exports gimp document to synfig's canvas and png images
# if output is omited then script saves image to same dir
# with source image
##
# Author: IL'dar AKHmetgaleev aka AkhIL
# e-mail: akhilman at gmail dot com
# web: http://akhilman.blogspot.com
#
# This code is licensed under
# Creative Commons Attribution 3.0 Unported License
# http://creativecommons.org/licenses/by/3.0/
##
#
# History:
# 2008-01-31 first public release
# 2008-04-26 gimp-2.2 compatibility fix by dooglus
# 2008-08-18 now works without alpha channel
#
from gimpfu import *
import os
import gzip
documentbegin = """\
<canvas version="0.3" width="%(width)i" height="%(height)i" xres="%(xres)f" yres="%(yres)f" view-box="-%(x)f %(y)f %(x)f -%(y)f" >
<name>%(name)s</name>
"""
documentend = """\
</canvas>
"""
inlinebegin = """\
<layer type="PasteCanvas" active="%(active)s" version="0.1" desc="%(name)s">
<param name="z_depth">
<real value="0.0000000000"/>
</param>
<param name="amount">
<real value="%(amount)f"/>
</param>
<param name="blend_method">
<integer value="%(blend_method)i"/>
</param>
<param name="origin">
<vector>
<x>%(x)f</x>
<y>%(y)f</y>
</vector>
</param>
<param name="canvas">
<canvas xres="10.000000" yres="10.000000">
"""
inlineend = """\
</canvas>
</param>
</layer>
"""
imagelayer = """\
<layer type="import" active="true" version="0.1" desc="%(name)s">
<param name="z_depth">
<real value="0.0000000000"/>
</param>
<param name="amount">
<real value="%(amount)f"/>
</param>
<param name="blend_method">
<integer value="%(blend_method)i"/>
</param>
<param name="tl">
<vector>
<x>-%(x)f</x>
<y>%(y)f</y>
</vector>
</param>
<param name="br">
<vector>
<x>%(x)f</x>
<y>-%(y)f</y>
</vector>
</param>
<param name="filename">
<string>%(file)s</string>
</param>
</layer>
"""
zoomlayer = """
<layer type="zoom" active="true" version="0.1" desc="%(name)s">
<param name="amount">
<real value="0.0000000000"/>
</param>
<param name="center">
<vector>
<x>0.0000000000</x>
<y>0.0000000000</y>
</vector>
</param>
</layer>
"""
rotatelayer = """
<layer type="rotate" active="true" version="0.1" desc="%(name)s">
<param name="origin">
<vector>
<x>0.0000000000</x>
<y>0.0000000000</y>
</vector>
</param>
<param name="amount">
<angle value="0.000000"/>
</param>
</layer>
"""
translatelayer = """
<layer type="translate" active="true" version="0.1" desc="%(name)s">
<param name="origin">
<vector>
<x>0.0000000000</x>
<y>0.0000000000</y>
</vector>
</param>
</layer>
"""
# from synfig-core/src/synfig/color.h
BLEND_COMPOSITE=0
BLEND_STRAIGHT=1
BLEND_ONTO=13
BLEND_STRAIGHT_ONTO=21
BLEND_BEHIND=12
BLEND_SCREEN=16
BLEND_OVERLAY=20
BLEND_HARD_LIGHT=17
BLEND_MULTIPLY=6
BLEND_DIVIDE=7
BLEND_ADD=4
BLEND_SUBTRACT=5
BLEND_DIFFERENCE=18
BLEND_BRIGHTEN=2
BLEND_DARKEN=3
BLEND_COLOR=8
BLEND_HUE=9
BLEND_SATURATION=10
BLEND_LUMINANCE=11
BLEND_ALPHA_BRIGHTEN=14
BLEND_ALPHA_DARKEN=15
BLEND_ALPHA_OVER=19
def gimp2synfig_mode_converter(mode):
"""
converts gimp's layer compositning mode to synfig's blend method
"""
modes = {
NORMAL_MODE : BLEND_COMPOSITE,
DISSOLVE_MODE : BLEND_COMPOSITE,
BEHIND_MODE : BLEND_BEHIND,
MULTIPLY_MODE : BLEND_MULTIPLY,
SCREEN_MODE : BLEND_SCREEN,
OVERLAY_MODE : BLEND_OVERLAY,
DIFFERENCE_MODE : BLEND_DIFFERENCE,
ADDITION_MODE : BLEND_ADD,
SUBTRACT_MODE : BLEND_SUBTRACT,
DARKEN_ONLY_MODE : BLEND_DARKEN,
LIGHTEN_ONLY_MODE : BLEND_BRIGHTEN,
HUE_MODE : BLEND_HUE,
SATURATION_MODE : BLEND_SATURATION,
COLOR_MODE : BLEND_COLOR,
VALUE_MODE : BLEND_LUMINANCE,
DIVIDE_MODE : BLEND_DIVIDE,
DODGE_MODE : BLEND_BRIGHTEN,
BURN_MODE : BLEND_MULTIPLY,
HARDLIGHT_MODE : BLEND_HARD_LIGHT,
SOFTLIGHT_MODE : BLEND_COMPOSITE,
GRAIN_EXTRACT_MODE: BLEND_COMPOSITE,
GRAIN_MERGE_MODE : BLEND_COMPOSITE,
COLOR_ERASE_MODE : BLEND_COMPOSITE,
}
return modes[mode]
def python_fu_exportsynfig(img,layer, output, span, doinvisible, applymask, dozoom, dorot, dotrans):
if output:
prefix = os.path.splitext(output)[0]
suffix = os.path.splitext(output)[1]
suffix = "sifz"
else:
prefix = img.filename.split('.xcf')[0]
suffix = "sifz"
name = os.path.basename(prefix)
prefix = os.path.dirname(prefix)
layersprefix = os.path.join(prefix,"%s_layers"%name)
if not os.path.isdir(layersprefix):
os.makedirs(layersprefix) # make path for layers
siffile = gzip.open("%s.%s"%(os.path.join(prefix,name), suffix),"w")
pixelsize = 1/(img.width**2 + img.height**2)**0.5*span
siffile.write(documentbegin %{ \
"width":img.width, \
"height":img.height, \
"xres":pdb.gimp_image_get_resolution(img)[0]*39.37007904, \
"yres":pdb.gimp_image_get_resolution(img)[1]*39.37007904, \
"x":img.width*pixelsize/2, \
"y":img.height*pixelsize/2, \
"name":img.name \
})
# aplying layer masks
if applymask:
img = img.duplicate()
for l in img.layers:
if not l.visible and not doinvisible:
continue # don't process invisible
if pdb.gimp_layer_get_mask(l):
pdb.gimp_layer_remove_mask(l, 0)
# exporting layers
totallayers = len(img.layers)
donelayers = 0
for l in reversed(img.layers):
if not l.visible:
if not doinvisible:
continue # don't process invisible
else:
active = 'false'
else:
active = 'true'
siffile.write(inlinebegin % {\
"name":l.name, \
"amount":l.opacity*0.01, \
"active":active, \
"blend_method":gimp2synfig_mode_converter(l.mode), \
"x":(l.width/2.0+pdb.gimp_drawable_offsets(l)[0]-img.width/2.0)*pixelsize, \
"y":(l.height/2.0+pdb.gimp_drawable_offsets(l)[1]-img.height/2.0)*pixelsize*-1 \
})
# maknig file names
filename = "%s.png" % ( \
l.name.replace(" ","_").replace('#',"_",) \
)
maskname = "%s_mask.png" % ( \
l.name.replace(" ","_").replace("#","_") \
)
# exporting layer
newimg = gimp.Image(l.width,l.height,img.base_type)
pdb.gimp_edit_copy(l)
newlayer = gimp.Layer(newimg, l.name, l.width, l.height, l.type, 1, NORMAL_MODE)
pdb.gimp_drawable_fill(newlayer, TRANSPARENT_FILL)
newimg.add_layer(newlayer)
pdb.gimp_floating_sel_anchor(pdb.gimp_edit_paste(newlayer, True))
pdb.gimp_file_save(newimg, newlayer, os.path.join(layersprefix,filename), filename)
pdb.gimp_image_delete(newimg)
siffile.write(imagelayer % {\
"name":filename, \
"file":os.path.join("%s_layers"%name,filename), \
"blend_method":BLEND_COMPOSITE, \
"amount":1.0, \
"x":l.width/2*pixelsize, \
"y":l.height/2*pixelsize \
})
# exporting layer mask
if pdb.gimp_layer_get_mask(l):
newimg = gimp.Image(l.width,l.height,GRAY)
pdb.gimp_edit_copy(pdb.gimp_layer_get_mask(l))
newlayer = gimp.Layer(newimg, l.name, l.width, l.height, GRAYA_IMAGE)
pdb.gimp_drawable_fill(newlayer, TRANSPARENT_FILL)
newimg.add_layer(newlayer)
pdb.gimp_floating_sel_anchor(pdb.gimp_edit_paste(newlayer, True))
pdb.gimp_invert(newlayer)
mask = pdb.gimp_layer_create_mask(newlayer,ADD_COPY_MASK)
pdb.gimp_image_add_layer_mask(newimg,newlayer,mask)
pdb.gimp_layer_remove_mask(newlayer, 0)
pdb.gimp_file_save(newimg, newlayer, os.path.join(layersprefix,maskname), filename)
pdb.gimp_image_delete(newimg)
siffile.write(imagelayer % {\
"name":maskname, \
"file":os.path.join("%s_layers"%name,maskname), \
"blend_method":BLEND_ALPHA_OVER, \
"amount":1.0, \
"x":l.width/2*pixelsize, \
"y":l.height/2*pixelsize \
})
# adding transform layers
if dozoom:
siffile.write(zoomlayer%{"name":"%s_scale"%l.name})
if dorot:
siffile.write(rotatelayer%{"name":"%s_rot"%l.name})
if dotrans:
siffile.write(translatelayer%{"name":"%s_loc"%l.name})
siffile.write(inlineend)
donelayers += 1
if applymask:
pdb.gimp_image_delete(img)
siffile.write(documentend)
siffile.close()
register(
"python_fu_exportsynfig",
"Export document to synfig's format",
"Export document to synfig's format\nBy default saves to same dir as source image",
"AkhIL",
"AkhIL",
"2008-08-18",
"<Image>/File/E_xport/_Synfig",
"RGB*, GRAY*",
[
(PF_STRING, "output", "output path (optional)", ""),
(PF_FLOAT, "span", "Image Span",9.1788),
(PF_BOOL, "doinvisible", "Export invisible layers",True),
(PF_BOOL, "applymask", "Apply layer masks",False),
(PF_BOOL, "dozoom", "Add zoom layers",False),
(PF_BOOL, "dorot", "Add rotate layers",False),
(PF_BOOL, "dotrans", "Add translate layers",False)
],
[],
python_fu_exportsynfig)
main()