-
Notifications
You must be signed in to change notification settings - Fork 0
/
importer.py
539 lines (414 loc) · 18.6 KB
/
importer.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# python built-in imports
import os
import re
import shutil
import getpass
# PySide specific imports
from PySide2 import QtWidgets, QtCore, QtGui
# Software specific import
import nuke
def get_project():
"""get project path for each group
:return: project path
:rtype: string
"""
user = getpass.getuser()
project_path = "C:/Users/{}/Documents/maya/2018/prefs/mega_tuyau_settings/project_path.mega".format(user)
with open(project_path) as data:
path = os.path.normpath(data.read())
return path
class Importer(QtWidgets.QDialog):
"""Create UI and populate it"""
def __init__(self):
super(Importer, self).__init__()
self.num_count = 1
# setting the path directory
path = nuke.root().name()
path = path.split('/')
path_directory = os.path.join(main_path,
'render',
'shots',
path[-4],
path[-3])
self.HD_path = os.path.join(path_directory, 'HD')
self.LD_path = os.path.join(path_directory, 'LD')
self.FML_path = os.path.join(path_directory, 'FML')
self.setWindowTitle('Import File')
self.build_ui()
self.start_quality()
self.pass_populate()
def build_ui(self):
"""UI Creation"""
layout = QtWidgets.QHBoxLayout(self)
layout.setContentsMargins(3, 3, 3, 3)
data_widget = QtWidgets.QWidget()
data_widget.setFixedWidth(270)
data_layout = QtWidgets.QVBoxLayout(data_widget)
data_layout.setContentsMargins(3, 3, 3, 3)
layout.addWidget(data_widget)
version_widget = QtWidgets.QWidget()
version_layout = QtWidgets.QHBoxLayout(version_widget)
version_layout.setContentsMargins(0, 0, 0, 0)
version_layout.setAlignment(QtCore.Qt.AlignCenter)
data_layout.addWidget(version_widget)
self.version_drop_down = QtWidgets.QComboBox()
self.version_drop_down.setMinimumWidth(100)
version_layout.addWidget(self.version_drop_down)
self.ld_label = QtWidgets.QLabel('Show LD')
self.ld_checkbox = QtWidgets.QCheckBox()
self.ld_checkbox.setChecked(0)
self.fml_label = QtWidgets.QLabel('Show FML')
self.fml_checkbox = QtWidgets.QCheckBox()
self.fml_checkbox.setChecked(0)
version_layout.addWidget(self.ld_checkbox)
version_layout.addWidget(self.ld_label)
version_layout.addWidget(self.fml_checkbox)
version_layout.addWidget(self.fml_label)
path_label = QtWidgets.QLabel('Avaible Pass')
path_label.setStyleSheet(
"background-color: #5d5d5d;border: 3px solid #5d5d5d;border-radius: 3px;boldLabelFont;")
path_label.setFont(QtGui.QFont('boldLabelFont', 8, QtGui.QFont.Bold))
data_layout.addWidget(path_label)
self.path_tree = QtWidgets.QTreeWidget()
self.path_tree.setHeaderHidden(True)
self.path_tree.setIconSize(QtCore.QSize(100, 100))
self.path_tree.setStyleSheet("QTreeWidget::item{padding: 5px 0;}")
self.path_tree.setFont(QtGui.QFont('Liberation Mono'))
self.path_tree.setSelectionMode(
QtWidgets.QAbstractItemView.MultiSelection)
data_layout.addWidget(self.path_tree)
nbr_widget = QtWidgets.QWidget()
nbr_layout = QtWidgets.QHBoxLayout(nbr_widget)
nbr_layout.setAlignment(QtCore.Qt.AlignLeft)
nbr_layout.setContentsMargins(3, 3, 3, 3)
nbr_layout.setAlignment(QtCore.Qt.AlignLeft)
data_layout.addWidget(nbr_widget)
self.ref_label = QtWidgets.QLabel('Work on local')
self.pass_drop_down_label = QtWidgets.QLabel('All pass from ')
self.checkbox = QtWidgets.QCheckBox()
self.pass_drop_down = QtWidgets.QComboBox()
self.checkbox.setChecked(1)
nbr_layout.addWidget(self.checkbox)
nbr_layout.addWidget(self.ref_label)
nbr_layout.setAlignment(QtCore.Qt.AlignLeft)
passes_widget = QtWidgets.QWidget()
passes_layout = QtWidgets.QHBoxLayout(passes_widget)
passes_layout.setAlignment(QtCore.Qt.AlignLeft)
passes_layout.setContentsMargins(3, 3, 3, 3)
data_layout.addWidget(passes_widget)
self.pass_drop_down_label = QtWidgets.QLabel('Import all passes from')
self.pass_drop_down_label2 = QtWidgets.QLabel('All pass from ')
self.checkbox_passes = QtWidgets.QCheckBox()
self.pass_drop_down = QtWidgets.QComboBox()
passes_layout.addWidget(self.checkbox_passes)
passes_layout.addWidget(self.pass_drop_down_label)
passes_layout.addWidget(self.pass_drop_down)
self.import_button = QtWidgets.QPushButton('Import')
data_layout.addWidget(self.import_button)
self.fml_checkbox.clicked.connect(self.fml)
self.ld_checkbox.clicked.connect(self.ld)
self.import_button.clicked.connect(self.import_fct)
self.ld_checkbox.stateChanged.connect(self.get_version)
self.ld_checkbox.stateChanged.connect(self.pass_populate)
self.version_drop_down.activated.connect(self.pass_populate)
self.get_version()
self.fml_checkbox.stateChanged.connect(self.get_version)
self.fml_checkbox.stateChanged.connect(self.pass_populate)
def fml(self):
"""uncheck ld button if fml is check"""
self.ld_checkbox.setChecked(0)
def ld(self):
"""uncheck fml button if ld is check"""
self.fml_checkbox.setChecked(0)
# quality init
def start_quality(self):
"""chose first quality to be shown
If there is no HD render it shows the LD if no LD it shows
FML else return no render found
"""
if not os.path.exists(self.HD_path):
if os.path.exists(self.LD_path):
self.ld_checkbox.setChecked(1)
if os.path.exists(self.FML_path):
self.fml_checkbox.setChecked(1)
else:
print("no render found")
def last_version(self, path):
"""get and set last version
:param: path(str): path depending on quality
"""
version_list = sorted(os.listdir(path), reverse=True)
self.version_drop_down.clear()
for version in version_list:
self.version_drop_down.addItem(version)
def get_version(self):
"""get render last version by quality"""
print(self.LD_path)
if os.path.exists(self.HD_path):
self.last_version(self.HD_path)
if self.ld_checkbox.isChecked() and os.path.exists(self.LD_path):
self.last_version(self.LD_path)
if self.fml_checkbox.isChecked() and os.path.exists(self.FML_path):
self.last_version(self.FML_path)
def pass_populate(self):
"""show all the pass in the UI"""
self.parentitem = {}
self.pass_drop_down.clear()
self.pass_drop_down.addItem('All layers')
self.path_tree.clear()
# choose which quality to display
path_directory = self.HD_path
if not os.path.exists(self.HD_path):
path_directory = self.LD_path
if not os.path.exists(path_directory):
path_directory = self.FML_path
else:
print('no render for this shot')
if self.ld_checkbox.isChecked():
path_directory = self.LD_path
if self.fml_checkbox.isChecked():
path_directory = self.FML_path
version = self.version_drop_down.currentText()
path_version = os.path.join(path_directory,
version)
path_list = os.listdir(path_version)
# get all pass and add them
for category in path_list:
path_name_list = []
item_type = QtWidgets.QTreeWidgetItem(category)
item_type.setText(0, category)
item_type.setFlags(QtCore.Qt.ItemIsEnabled)
self.parentitem[category] = item_type
all_path_directory = os.path.join(path_version,
category)
if os.listdir(all_path_directory):
self.pass_drop_down.addItem(category)
paths = os.listdir(all_path_directory)
for path in paths:
if 'ID' in path.upper():
path_name = path.split('_')
path_name = path_name[-3] + '_' + path_name[-2]
else:
path_name = path.split('_')
path_name = path_name[-2]
if path_name not in path_name_list:
path_name_list.append(path_name)
for item in path_name_list:
path_item = QtWidgets.QTreeWidgetItem(item)
item_type.addChild(path_item)
path_item.setText(0, item)
self.path_tree.addTopLevelItem(item_type)
def count_files(self, path):
"""Count the number of file to copy on local
:param: path(str): dir in which count the file
:return: number of file
:rtype: (int)
"""
files = []
if os.path.isdir(path):
for path, dirs, filename in os.walk(path):
files.extend(filename)
return len(files)
def makedirs(self, dest):
"""Create the directory that will contain the files
:param: dest(str): path where to create the directory
"""
if not os.path.exists(dest):
os.makedirs(dest)
else:
pass
def copyfile(self, src, dest):
"""copy the files
:param: src(str): source path
:param: dest(str): destination path
"""
if not os.path.isfile(dest):
shutil.copy(src, dest)
else:
os.remove(dest)
shutil.copy(src, dest)
def copy_files_with_progress(self, src, dest, name):
"""update the loading bar for each file copied
:param: src(str): source path
:param: dest(str): destination path
:param: name(str): str that the file must contain to be copied
"""
num_files = self.count_files(src)
if num_files == 0:
return
self.makedirs(dest)
num_copied = 0
for path, dirs, filenames in os.walk(src):
for directory in dirs:
dest_dir = path.replace(src, dest)
self.makedirs(os.path.join(dest_dir, directory))
sfiles = []
for sfile in filenames:
QtWidgets.QApplication.processEvents()
if name in sfile:
src_file = os.path.join(path, sfile)
dest_file = os.path.join(path.replace(src, dest), sfile)
sfiles.append((src_file, dest_file))
else:
pass
self.num_count = len(sfiles)
for src, dst in sfiles:
shutil.copy(src, dst)
num_copied += 1
progress = int(num_copied / float(self.num_count) * 100)
QtWidgets.QApplication.processEvents()
self.progress.setValue(progress)
def import_fct(self):
"""main fonction
it'll copy, import, rename and set correctly all the file in nuke
"""
self.progressWindow = QtWidgets.QDialog()
self.progress = QtWidgets.QProgressBar(self)
layout = QtWidgets.QHBoxLayout(self.progressWindow)
layout.addWidget(self.progress)
self.progressWindow.show()
# define the proxy folder
proxy_path = 'D:/TEMP_NUKE'
dic_frame = {}
path = nuke.root().name()
path = path.split('/')
path_directory = self.HD_path
version = self.version_drop_down.currentText()
selected = self.path_tree.selectedItems()
# create proxy folder
proxy_path = os.path.join(proxy_path,
path[-4],
path[-3])
# if the box is check it'will import all pass of a layer or all layer
if self.checkbox_passes.isChecked():
child_list = []
# get layers
layer = self.pass_drop_down.currentText()
if layer in self.parentitem:
layer_sel = self.parentitem[layer]
layer_sel.setSelected(True)
count = layer_sel.childCount()
for i in range(count):
child = layer_sel.child(i)
child_list.append(child)
selected = child_list
if layer == 'All layers':
for key, value in self.parentitem.iteritems():
value.setSelected(True)
count = value.childCount()
for i in range(count):
child = value.child(i)
child_list.append(child)
selected = child_list
# import by selected pass
for i in selected:
selected = i.text(0)
qual = 'HD'
if self.ld_checkbox.isChecked():
path_directory = self.LD_path
qual = 'LD'
elif self.fml_checkbox.isChecked():
path_directory = self.FML_path
qual = 'FML'
if QtWidgets.QTreeWidgetItem.parent(i):
pass_type = QtWidgets.QTreeWidgetItem.parent(i)
pass_type = pass_type.text(0)
path_path = os.path.abspath(os.path.join(path_directory,
version,
pass_type))
all_frame_list = os.listdir(path_path)
selectedlist = []
for frame in all_frame_list:
name_frame = frame[:-9] + '_'
if '_' + selected in name_frame:
frame_nbr = frame[-8:]
frame_nbr = frame_nbr[:-4]
selectedlist.append(int(frame_nbr))
selectedlist = sorted(selectedlist)
end = selectedlist[-1]
first = selectedlist[0]
start_end = [first, end]
dic_frame = {name_frame: start_end}
# create the path
for key, value in dic_frame.items():
self.progress.setValue(0)
new_path = (os.path.abspath("{}/{}####.exr {}-{}".format(
path_path, key, value[0], value[1]))).replace("\\", "/")
main_path = (os.path.abspath("{}/{}####.exr".format(path_path, key))).replace("\\", "/")
proxy = os.path.join(proxy_path,
qual)
proxycoucou = proxy
new_proxy_path = "{}/{}####.exr {}-{}".format(
proxy, key, value[0], value[1])
name = pass_type + '_' + selected
# if the node already exist just update the path
if nuke.exists(name):
node = nuke.toNode(name)
node.knob('file').fromUserText(os.path.abspath(new_path))
if self.checkbox.isChecked():
self.progressWindow.setWindowTitle(selected)
self.copy_files_with_progress(
path_path, proxycoucou, key)
node.knob('proxy').fromUserText(
os.path.normpath(new_proxy_path))
# else create the node
else:
print(new_path)
readNode = nuke.nodes.Read(file=main_path, first=value[0], last=value[1], name=name)
node = nuke.toNode(name)
if self.checkbox.isChecked():
self.progressWindow.setWindowTitle(selected)
self.copy_files_with_progress(
path_path, proxycoucou, key)
# self.progressWindow.close()
node.knob('proxy').fromUserText(
os.path.normpath(new_proxy_path))
self.progressWindow.close()
# auto create Deep EXRID network
if nuke.exists('ALL_ALL_ExrID'):
value = nuke.toNode('ALL_ALL_ExrID').knob('file').getValue()
value = value.replace('%04d', '####')
first = nuke.toNode('ALL_ALL_ExrID').knob('first').getValue()
last = nuke.toNode('ALL_ALL_ExrID').knob('last').getValue()
value = value + ' {}-{}'.format(int(first), int(last))
infopath = nuke.root().name()
infopath = infopath.split('/')
nodename = infopath[-4] + '_' + infopath[-3] + '_DeepRead'
if nuke.exists(nodename):
print('exist')
my_node = nuke.toNode(nodename)
else:
my_node = nuke.createNode('DeepRead')
my_node.setName(nodename)
my_node.knob('on_error').setValue(3)
my_node.knob('file').fromUserText(value)
open_exrid = nodename.replace('DeepRead', 'OpenEXRId')
if nuke.exists(open_exrid):
print('exist')
key = nuke.toNode(open_exrid)
else:
key = nuke.createNode('DeepOpenEXRId')
key.setName(open_exrid)
key.setInput(0, my_node)
deeptoimage = nodename.replace('DeepRead', 'DeepToImage')
if nuke.exists(deeptoimage):
print('exist')
value = nuke.toNode(deeptoimage)
else:
value = nuke.createNode('DeepToImage')
value.setName(deeptoimage)
value.setInput(0, key)
exr_pass = nuke.toNode('ALL_ALL_ExrID')
nuke.delete(exr_pass)
def show_ui():
"""Init UI"""
ui = Importer()
ui.show()
return ui
p = re.compile("[0-9]{4}_[0-9]{4}_Comp.nk$")
if not p.match(nuke.root().name().split('/')[-1]):
nuke.message('open a valid comp file please')
else:
main_path = get_project()
ui = Importer()
ui.show()