-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
collector.py
366 lines (280 loc) · 12.2 KB
/
collector.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# ----------------------------------------------------------------------------
# Copyright (c) 2019-2020, Diego Garcia Huerta.
#
# Your use of this software as distributed in this GitHub repository, is
# governed by the BSD 3-clause License.
#
# Your use of the Shotgun Pipeline Toolkit is governed by the applicable license
# agreement between you and Autodesk / Shotgun.
#
# The full license is in the file LICENSE, distributed with this software.
# ----------------------------------------------------------------------------
import os
from functools import partial
import sgtk
from krita import Krita
from sgtk import TankError
from sgtk.util.filesystem import create_valid_filename
__author__ = "Diego Garcia Huerta"
__contact__ = "https://www.linkedin.com/in/diegogh/"
HookBaseClass = sgtk.get_hook_baseclass()
class KritaSessionCollector(HookBaseClass):
"""
Collector that operates on the krita session. Should inherit from the basic
collector hook.
"""
@property
def settings(self):
"""
Dictionary defining the settings that this collector expects to receive
through the settings parameter in the process_current_session and
process_file methods.
A dictionary on the following form::
{
"Settings Name": {
"type": "settings_type",
"default": "default_value",
"description": "One line description of the setting"
}
The type string should be one of the data types that toolkit accepts as
part of its environment configuration.
"""
# grab any base class settings
collector_settings = super(KritaSessionCollector, self).settings or {}
# settings specific to this collector
krita_session_settings = {
"Work Template": {
"type": "template",
"default": None,
"description": "Template path for artist work files. Should "
"correspond to a template defined in "
"templates.yml. If configured, is made available"
"to publish plugins via the collected item's "
"properties. ",
},
"Publish Layers as Folder": {
"type": "bool",
"default": True,
"description": "Publish Image Layers as a single folder."
"If true (default) layers will be all exported"
" together as a folder publish."
"If false, each layer will be exported and"
" published as each own version stream.",
},
}
# update the base settings with these settings
collector_settings.update(krita_session_settings)
return collector_settings
def process_current_session(self, settings, parent_item):
"""
Analyzes the current session open in Krita and parents a subtree of
items under the parent_item passed in.
:param dict settings: Configured settings for this collector
:param parent_item: Root item instance
"""
items = []
# create an item representing the current krita session
session_item = self.collect_current_krita_session(settings, parent_item)
if session_item:
items.append(session_item)
# check if there are any layers to publish
publish_as_folder_setting = settings.get("Publish Layers as Folder")
if publish_as_folder_setting and publish_as_folder_setting.value:
layer_items = self.collect_krita_layers_as_folder(settings, session_item)
else:
layer_items = self.collect_krita_layers(settings, session_item)
items.append(layer_items)
return items
def collect_current_krita_session(self, settings, parent_item):
"""
Creates an item that represents the current krita session.
:param parent_item: Parent Item instance
:returns: Item of type krita.session
"""
publisher = self.parent
# get the path to the current file
path = _session_path()
if not path:
# no document is active, so nothing to see here!
return
# determine the display name for the item
if path:
file_info = publisher.util.get_file_path_components(path)
display_name = file_info["filename"]
else:
display_name = "Current Krita Session"
# create the session item for the publish hierarchy
session_item = parent_item.create_item(
"krita.session", "Krita Session", display_name
)
# get the icon path to display for this item
icon_path = os.path.join(self.disk_location, os.pardir, "icons", "krita.png")
session_item.set_icon_from_path(icon_path)
# if a work template is defined, add it to the item properties so
# that it can be used by attached publish plugins
work_template_setting = settings.get("Work Template")
if work_template_setting:
work_template = publisher.engine.get_template_by_name(
work_template_setting.value
)
# store the template on the item for use by publish plugins. we
# can't evaluate the fields here because there's no guarantee the
# current session path won't change once the item has been created.
# the attached publish plugins will need to resolve the fields at
# execution time.
session_item.properties["work_template"] = work_template
session_item.properties["publish_type"] = "Krita Document"
self.logger.debug("Work template defined for Krita collection.")
self.logger.info("Collected current Krita scene")
return session_item
def _recurse_layers(self, parentNode, fn=None, results=None):
if results is None:
results = []
# if no function is passed, let's assume the identity
if fn is None:
def fn(x):
return x
for node in parentNode.childNodes():
results.append(fn(node))
if node.childNodes():
self._recurse_layers(node, fn=fn, results=results)
return results
def create_node_layer_item(
self,
settings,
parent_item,
node,
display_name=None,
icon_name=None,
is_header=False,
):
publisher = self.parent
if display_name is None:
display_name = node.name()
# create the layers item for the publish hierarchy
layer_item = parent_item.create_item("krita.layer", "Krita Layer", display_name)
# get the icon path to display for this item
if icon_name is None:
icon_name = "krita_layer.png"
icon_path = os.path.join(self.disk_location, os.pardir, "icons", icon_name)
layer_item.set_icon_from_path(icon_path)
layer_item.properties["node"] = node
layer_item.properties["node_name"] = create_valid_filename(node.name())
layer_item.properties["publish_name"] = create_valid_filename(display_name)
layer_item.properties["publish_type"] = "Krita Layer"
layer_item.properties["is_header"] = is_header
if is_header:
layer_item.type_spec = "krita.layer.header"
# if a work template is defined, add it to the item properties so
# that it can be used by attached publish plugins
work_template = None
work_template_setting = settings.get("Work Template")
if work_template_setting:
work_template = publisher.engine.get_template_by_name(
work_template_setting.value
)
if not work_template:
raise TankError(
"Missing Work Template in templates.yml: %s "
% work_template_setting.value
)
layer_item.properties["work_template"] = work_template
return layer_item
def collect_krita_layers(self, settings, parent_item):
"""
Creates the items that represent the current document layers.
:param parent_item: Parent Item instance
:returns: Item of type krita.layer
"""
# get the path to the current file
path = _session_path()
if not path:
# no document is active, so nothing to see here!
return
layer_items = []
layer_names = []
krita_app = Krita.instance()
doc = krita_app.activeDocument()
if doc:
parent_node = doc.rootNode()
layer_names = self._recurse_layers(parent_node, fn=lambda x: x.name())
if len(layer_names) > 1:
self.logger.info("Found %s layers: %s" % (len(layer_names), layer_names))
top_layer_item = self.create_node_layer_item(
settings,
parent_item,
parent_node,
display_name="Document Layers",
icon_name="krita_layers.png",
is_header=True,
)
layer_item_fn = partial(
self.create_node_layer_item, settings, top_layer_item
)
layer_items = self._recurse_layers(parent_node, fn=layer_item_fn)
self.logger.info("Collected current document Layers")
return layer_items
def collect_krita_layers_as_folder(self, settings, parent_item):
"""
Creates an item that represents the current document krita layers
:param parent_item: Parent Item instance
:returns: Item of type krita.layers
"""
self.logger.debug("Collecting current document Layers as folder")
layer_item = None
publisher = self.parent
krita_app = Krita.instance()
doc = krita_app.activeDocument()
if doc:
parent_node = doc.rootNode()
layer_nodes = self._recurse_layers(parent_node, fn=lambda x: x)
if len(layer_nodes) > 1:
display_name = "Document Layers (Folder)"
# create the layers item for the publish hierarchy
layer_item = parent_item.create_item(
"krita.layers", "Krita Layers", display_name
)
# get the icon path to display for this item
icon_path = os.path.join(
self.disk_location, os.pardir, "icons", "krita_layers.png"
)
layer_item.properties["nodes"] = layer_nodes
layer_item.set_icon_from_path(icon_path)
layer_item.properties["publish_type"] = "Krita Layers"
# if a work template is defined, add it to the item properties so
# that it can be used by attached publish plugins
self.logger.debug("Checking Work template...")
work_template = None
work_template_setting = settings.get("Work Template")
if work_template_setting:
work_template = publisher.engine.get_template_by_name(
work_template_setting.value
)
if not work_template:
raise TankError(
"Missing Work Template in templates.yml: %s "
% work_template_setting.value
)
layer_item.properties["work_template"] = work_template
if work_template:
self.logger.debug(
"Work template defined for the layers as folder: %s "
% work_template
)
else:
self.logger.debug(
"No work template defined for the layers as folder."
)
self.logger.info("Collected current document Layers as folder")
return layer_item
def _session_path():
"""
Return the path to the current session
:return:
"""
path = None
krita_app = Krita.instance()
active_doc = krita_app.activeDocument()
if active_doc:
path = active_doc.fileName()
return path