-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
collector.py
283 lines (239 loc) · 10.6 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
# Copyright (c) 2017 Shotgun Software Inc.
#
# CONFIDENTIAL AND PROPRIETARY
#
# This work is provided "AS IS" and subject to the Shotgun Pipeline Toolkit
# Source Code License included in this distribution package. See LICENSE.
# By accessing, using, copying or modifying this work you indicate your
# agreement to the Shotgun Pipeline Toolkit Source Code License. All rights
# not expressly granted therein are reserved by Shotgun Software Inc.
import os
from functools import partial
import sgtk
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()
import sd
class SubstanceDesignerSessionCollector(HookBaseClass):
"""
Collector that operates on the substancedesigner 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(SubstanceDesignerSessionCollector, self).settings or {}
)
# settings specific to this collector
substancedesigner_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. ",
}
}
# update the base settings with these settings
collector_settings.update(substancedesigner_session_settings)
return collector_settings
def process_current_session(self, settings, parent_item):
"""
Analyzes the current session open in SubstanceDesigner 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 substancedesigner session
session_item = self.collect_current_substancedesigner_session(
settings, parent_item
)
if session_item:
items.append(session_item)
return items
def collect_current_substancedesigner_session(self, settings, parent_item):
"""
Creates an item that represents the current substancedesigner session.
:param parent_item: Parent Item instance
:returns: Item of type substancedesigner.session
"""
publisher = self.parent
# 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
)
# we will 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.
items = []
# graph textures
ctx = sd.getContext()
app = ctx.getSDApplication()
uiMgr = app.getQtForPythonUIMgr()
pck_man = app.getPackageMgr()
user_pcks = pck_man.getUserPackages()
for pck in user_pcks:
pck_filepath = pck.getFilePath()
pck_file_info = publisher.util.get_file_path_components(pck_filepath)
pck_id = pck_file_info["filename"]
pck_item = parent_item.create_item(
"substancedesigner.package", "Substance Designer Package", pck_id
)
icon_path = os.path.join(
self.disk_location, os.pardir, "icons", "substancedesigner.png"
)
pck_item.set_icon_from_path(icon_path)
pck_item.properties["package"] = pck
pck_item.properties["work_template"] = work_template
pck_item.properties["publish_type"] = "Substance Designer File"
items.append(pck_item)
has_mdl_graphs = False
has_comp_graphs = False
for resource in pck.getChildrenResources(True):
self.logger.info("Resource <%s> %s" % (type(resource), resource))
if isinstance(resource, sd.api.sbs.sdsbscompgraph.SDSBSCompGraph):
graph_id = resource.getIdentifier()
graph_item = pck_item.create_item(
"substancedesigner.graph.textures",
"Textures",
graph_id + " Textures",
)
icon_path = os.path.join(
self.disk_location, os.pardir, "icons", "texture.png"
)
graph_item.set_icon_from_path(icon_path)
graph_item.properties["package"] = pck
graph_item.properties["resource"] = resource
graph_item.properties["work_template"] = work_template
graph_item.properties["publish_type"] = "Texture Folder"
graph_item.properties["extra_fields"] = {
"substancedesigner.graph.name": graph_id
}
items.append(graph_item)
has_comp_graphs = True
if isinstance(resource, sd.api.mdl.sdmdlgraph.SDMDLGraph):
has_mdl_graphs = True
graph_id = resource.getIdentifier()
graph_item = pck_item.create_item(
"substancedesigner.graph.mdle",
"Graph as MDLE",
graph_id + " MDLE",
)
icon_path = os.path.join(
self.disk_location,
os.pardir,
"icons",
"substancedesigner_graph_mdle.png",
)
graph_item.set_icon_from_path(icon_path)
graph_item.properties["package"] = pck
graph_item.properties["resource"] = resource
graph_item.properties["work_template"] = work_template
graph_item.properties["publish_type"] = "MDLE File"
graph_item.properties["extra_fields"] = {
"substancedesigner.graph.name": graph_id
}
items.append(graph_item)
has_mdl_graphs = True
graph_id = resource.getIdentifier()
graph_item = pck_item.create_item(
"substancedesigner.graph.preset",
"Graph as preset",
graph_id + " Preset(mdl)",
)
icon_path = os.path.join(
self.disk_location,
os.pardir,
"icons",
"substancedesigner_graph_preset.png",
)
graph_item.set_icon_from_path(icon_path)
graph_item.properties["package"] = pck
graph_item.properties["resource"] = resource
graph_item.properties["work_template"] = work_template
graph_item.properties["publish_type"] = "MDL File"
graph_item.properties["extra_fields"] = {
"substancedesigner.graph.name": graph_id
}
items.append(graph_item)
if has_mdl_graphs:
pck_mdl_item = pck_item.create_item(
"substancedesigner.package.mdl",
"Package as MDL Module",
pck_id + " MDL Module",
)
icon_path = os.path.join(
self.disk_location,
os.pardir,
"icons",
"substancedesigner_graph_mdl.png",
)
pck_mdl_item.set_icon_from_path(icon_path)
pck_mdl_item.properties["package"] = pck
pck_mdl_item.properties["work_template"] = work_template
pck_mdl_item.properties["publish_type"] = "MDL File"
items.append(pck_mdl_item)
if has_comp_graphs:
pck_archive_item = pck_item.create_item(
"substancedesigner.package.archive",
"Substance Designer Package Archive",
pck_id + " Archive",
)
icon_path = os.path.join(
self.disk_location,
os.pardir,
"icons",
"substancedesignerarchive.png",
)
pck_archive_item.set_icon_from_path(icon_path)
pck_archive_item.properties["package"] = pck
pck_archive_item.properties["work_template"] = work_template
pck_archive_item.properties[
"publish_type"
] = "Substance Designer Archive"
items.append(pck_archive_item)
self.logger.info("Collected %s items from the session" % len(items))
return items
def _session_path():
"""
Return the path to the current session
:return:
"""
ctx = sd.getContext()
app = ctx.getSDApplication()
pm = app.getPackageMgr()
uiMgr = app.getQtForPythonUIMgr()
path = None
current_package_filename = ""
current_graph = uiMgr.getCurrentGraph()
if current_graph:
pck = current_graph.getPackage()
current_package_filename = pck.getFilePath()
path = current_package_filename
return path