forked from kamilburda/gimp-export-layers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_layers.py
206 lines (161 loc) · 7.23 KB
/
export_layers.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
#! /usr/bin/env python
#
#-------------------------------------------------------------------------------
#
# Export Layers - GIMP plug-in that exports layers as separate images
#
# Copyright (C) 2013, 2014 khalim19 <[email protected]>
#
# Export Layers 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.
#
# Export Layers 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 Export Layers. If not, see <http://www.gnu.org/licenses/>.
#
#-------------------------------------------------------------------------------
#===============================================================================
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
str = unicode
#===============================================================================
from export_layers import constants
if constants.DEBUG:
import sys
from export_layers.pylibgimpplugin import tee
tee.Tee(sys.stdout, open(constants.PLUGINS_LOG_STDOUT_PATH, 'a'),
log_header_title=constants.PLUGIN_TITLE, flush_file=True)
tee.Tee(sys.stderr, open(constants.PLUGINS_LOG_STDERR_PATH, 'a'),
log_header_title=constants.PLUGIN_TITLE, flush_file=True)
import gettext
gettext.install(constants.DOMAIN_NAME, constants.LOCALE_PATH, unicode=True)
import gimp
import gimpplugin
import gimpenums
from export_layers.pylibgimpplugin import settings
from export_layers.pylibgimpplugin import overwrite
from export_layers import settings_plugin
from export_layers import gui_plugin
from export_layers import exportlayers
#===============================================================================
class ExportLayersPlugin(gimpplugin.plugin):
def __init__(self):
self.special_settings = settings_plugin.SpecialSettings()
self.main_settings = settings_plugin.MainSettings()
self.gimpshelf_stream = settings.GimpShelfSettingStream(constants.SHELF_PREFIX)
self.config_file_stream = settings.JSONFileSettingStream(constants.CONFIG_FILE)
self.setting_persistor = settings.SettingPersistor([self.gimpshelf_stream], [self.gimpshelf_stream])
self.export_layers_settings = []
for setting in list(self.special_settings) + list(self.main_settings):
if setting.can_be_registered_to_pdb:
self.export_layers_settings.append(setting)
self.export_layers_to_settings = [
self.special_settings['run_mode'],
self.special_settings['image'],
]
self.export_layers_return_values = []
self.export_layers_to_return_values = []
def query(self):
gimp.domain_register(constants.DOMAIN_NAME, constants.LOCALE_PATH)
gimp.install_procedure(
"plug_in_export_layers",
_("Export layers as separate images in specified file format to specified "
"directory, using layer names as filenames"),
"",
"khalim19 <[email protected]>",
"khalim19",
"2013",
_("E_xport Layers..."),
"*",
gimpenums.PLUGIN,
self._create_plugin_params(self.export_layers_settings),
self._create_plugin_params(self.export_layers_return_values)
)
gimp.install_procedure(
"plug_in_export_layers_to",
_("Run \"{0}\" with the last values specified").format(constants.PLUGIN_TITLE),
_("If the plug-in is run for the first time (i.e. no last values exist), "
"default values will be used."),
"khalim19 <[email protected]>",
"khalim19",
"2013",
_("Export Layers _to"),
"*",
gimpenums.PLUGIN,
self._create_plugin_params(self.export_layers_to_settings),
self._create_plugin_params(self.export_layers_to_return_values)
)
gimp.menu_register("plug_in_export_layers", "<Image>/File/Export")
gimp.menu_register("plug_in_export_layers_to", "<Image>/File/Export")
def plug_in_export_layers(self, *args):
run_mode = args[0]
image = args[1]
self.special_settings['run_mode'].value = run_mode
self.special_settings['image'].value = image
if run_mode == gimpenums.RUN_INTERACTIVE:
self._run_export_layers_interactive(image)
elif run_mode == gimpenums.RUN_WITH_LAST_VALS:
self._run_with_last_vals(image)
else:
self._run_noninteractive(image, args)
def plug_in_export_layers_to(self, run_mode, image):
if run_mode == gimpenums.RUN_INTERACTIVE:
self.setting_persistor.load([self.special_settings['first_run']])
if self.special_settings['first_run'].value:
self._run_export_layers_interactive(image)
else:
self._run_export_layers_to_interactive(image)
else:
self._run_with_last_vals(image)
def _run_noninteractive(self, image, args):
# Start with the third parameter - run_mode and image are already set.
for setting, arg in zip(self.export_layers_settings[2:], args[2:]):
if isinstance(arg, bytes):
arg = arg.decode()
setting.value = arg
self._run_plugin_noninteractive(gimpenums.RUN_NONINTERACTIVE, image)
def _run_with_last_vals(self, image):
self.setting_persistor.read_setting_streams.append(self.config_file_stream)
status = self.setting_persistor.load(self.main_settings)
self.setting_persistor.read_setting_streams.pop()
if status == self.setting_persistor.READ_FAIL:
print(self.setting_persistor.status_message)
self._run_plugin_noninteractive(gimpenums.RUN_WITH_LAST_VALS, image)
def _run_export_layers_interactive(self, image):
gui_plugin.export_layers_gui(image, self.main_settings, self.special_settings,
self.gimpshelf_stream, self.config_file_stream)
def _run_export_layers_to_interactive(self, image):
gui_plugin.export_layers_to_gui(image, self.main_settings, self.setting_persistor)
def _run_plugin_noninteractive(self, run_mode, image):
self.main_settings.streamline(force=True)
layer_exporter = exportlayers.LayerExporter(
run_mode,
image,
self.main_settings,
overwrite_chooser=overwrite.NoninteractiveOverwriteChooser(self.main_settings['overwrite_mode'].value),
progress_updater=None
)
try:
layer_exporter.export_layers()
except exportlayers.ExportLayersCancelError as e:
print(e.message)
except exportlayers.ExportLayersError as e:
print(e.message)
raise
self.special_settings['first_run'].value = False
self.setting_persistor.save(self.main_settings, [self.special_settings['first_run']])
def _create_plugin_params(self, settings):
return [self._create_plugin_param(setting) for setting in settings]
def _create_plugin_param(self, setting):
return (setting.gimp_pdb_type, setting.name.encode(), setting.short_description.encode())
#===============================================================================
if __name__ == "__main__":
ExportLayersPlugin().start()