-
Notifications
You must be signed in to change notification settings - Fork 2
/
mk_usdcat_all.pyw
403 lines (323 loc) · 13.1 KB
/
mk_usdcat_all.pyw
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
import sys
import re
from pathlib import Path
from functools import partial
import subprocess
import fileinput
import time
import platform
import logging as logger
from PySide2 import QtWidgets
from PySide2 import QtCore
from ui_main_qt5 import Ui_MainWindow
logger.basicConfig(
level=logger.DEBUG,
filename='mk_usdcat_all.log',
format='%(asctime)s:%(levelname)s:%(message)s'
)
_CUR_OS = platform.system() # "Windows", "Darwin", "Linux"
_HYTHON_BINARY = "hython.exe" if _CUR_OS == "Windows" else "hython"
_REQUIRED_BINARIES = (_HYTHON_BINARY, 'usdcat')
_ALL_USD_EXTS = ('.usd', '.usdc', '.usda')
_USD_DEFAULT_EXT = '.usd'
_USD_ASCII_FORMATS = {False: 'usdc', True: 'usda'}
class USDCatAll(QtWidgets.QWidget):
def __init__(self):
super(USDCatAll, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def log_exec_time(func):
'''
Simple utility function to log execution time
'''
def wrapper(*args, **kwargs):
start_time = time.time()
ret = func(*args, **kwargs)
exec_time = time.time() - start_time
logger.debug(
'****Execution time: {:.3f} second (--in "{}").'.format(exec_time, func.__name__)
)
return ret
return wrapper
def ui_launch():
app = QtWidgets.QApplication(sys.argv)
win = USDCatAll()
ui_init(win.ui)
create_connections(win.ui)
win.show()
sys.exit(app.exec_())
def ui_init(ui):
if _CUR_OS == "Windows":
guess_install_path = "C:/Program Files/Side Effects Software"
elif _CUR_OS == "Darwin":
guess_install_path = "/Applications/Houdini/Current/Frameworks/Houdini.framework/Versions/Current/Resources"
houdini_latest_install_bin_folder = find_houdini_latest_install_bin_folder(guess_install_path)
if houdini_latest_install_bin_folder:
ui.preq_houdini_bin_folder_lineEdit.setText(houdini_latest_install_bin_folder.as_posix())
# First time run validation
visualize_folder_input_validation(
line_edit=ui.preq_houdini_bin_folder_lineEdit,
validate_label=ui.preq_houdini_bin_folder_validate_status_label,
required_files=_REQUIRED_BINARIES
)
enable_del_orig_option(
ui.usdcat_change_extension_checkBox.isChecked(),
ui.usdcat_delete_originals_checkBox
)
ui.usdcat_folder_to_batch_lineEdit.setFocus()
def create_connections(ui):
ui.preq_houdini_bin_folder_lineEdit.editingFinished.connect(partial(
visualize_folder_input_validation,
line_edit=ui.preq_houdini_bin_folder_lineEdit,
validate_label=ui.preq_houdini_bin_folder_validate_status_label,
required_files=_REQUIRED_BINARIES
))
ui.preq_houdini_bin_folder_browse_btn.clicked.connect(partial(
browse_houdini_bin_folder,
line_edit=ui.preq_houdini_bin_folder_lineEdit,
validate_label=ui.preq_houdini_bin_folder_validate_status_label
))
ui.preq_houdini_bin_folder_open_pushButton.clicked.connect(partial(
open_explorer,
get_folder_method=ui.preq_houdini_bin_folder_lineEdit.text
))
ui.usdcat_change_extension_checkBox.toggled.connect(partial(
enable_del_orig_option,
del_orig_option_widget=ui.usdcat_delete_originals_checkBox
))
ui.usdcat_folder_to_batch_lineEdit.returnPressed.connect(partial(
batch_conversion,
get_bin_folder_method=ui.preq_houdini_bin_folder_lineEdit.text,
get_folder_to_usdcat_method=ui.usdcat_folder_to_batch_lineEdit.text,
get_ascii_output_mode_method=ui.usdcat_output_ascii_radioButton.isChecked,
get_change_extension_mode_method=ui.usdcat_change_extension_checkBox.isChecked,
get_remove_orig_mode_method=ui.usdcat_delete_originals_checkBox.isChecked,
progress_widget=ui.usdcat_batch_progressBar
))
ui.usdview_file_to_run_lineEdit.returnPressed.connect(partial(
launch_usdview,
get_bin_folder_method=ui.preq_houdini_bin_folder_lineEdit.text,
get_file_to_usdview_method=ui.usdview_file_to_run_lineEdit.text
))
def browse_houdini_bin_folder(line_edit, validate_label):
houdini_bin_folder = QtWidgets.QFileDialog.getExistingDirectory()
if houdini_bin_folder:
line_edit.setText(houdini_bin_folder)
visualize_folder_input_validation(
line_edit=line_edit,
validate_label=validate_label,
required_files=_REQUIRED_BINARIES
)
def find_houdini_latest_install_bin_folder(houdini_install_path):
houdini_install_path = Path(houdini_install_path)
# In MacOS we're using "Current" symlink instead of looking for all versions installed
if _CUR_OS == "Windows":
# e.g. 'C:/Program Files/Side Effects Software/Houdini {XX.X.XXX}'
houdini_install_path = list(houdini_install_path.glob('Houdini*'))
houdini_install_path = sorted(houdini_install_path)[-1] \
if houdini_install_path else Path()
if houdini_install_path != Path():
houdini_install_path = houdini_install_path / "bin"
return houdini_install_path \
if houdini_install_path.exists() and houdini_install_path != Path() else Path()
def validate_folder_path(folder_path, required_files=()):
'''
Check if folder exists, and whether there are specified files within that folder
'''
folder_path = Path(folder_path)
if not folder_path.exists():
logger.warning('Folder does not exist: {}'.format(folder_path))
return False
else:
# Validate all the specified files inside
for required_file in required_files:
if not (folder_path / required_file).is_file():
logger.warning('File does not exist: {}'.format(required_file))
return False
return True
def update_validate_label(validate_label, valid):
'''
'''
if valid:
validate_label.setText('Valid')
validate_label.setStyleSheet('color:green')
else:
validate_label.setText('Invalid')
validate_label.setStyleSheet('color:red')
def visualize_folder_input_validation(line_edit, validate_label, required_files=()):
'''
Update QLabel to show whether the selected bin folder has required files in order to operate
'''
validate_result = validate_folder_path(
line_edit.text(),
required_files=required_files
)
update_validate_label(
validate_label,
validate_result
)
def enable_del_orig_option(checked, del_orig_option_widget):
del_orig_option_widget.setEnabled(checked)
def get_usd_input_output_extensions(ascii_output_mode):
'''
:param int ascii_output_mode:
'''
infile_ext = '.' + _USD_ASCII_FORMATS[not ascii_output_mode]
outfile_ext = '.' + _USD_ASCII_FORMATS[ascii_output_mode]
return infile_ext, outfile_ext
@log_exec_time
def batch_usdcat(hython_path, usdcat_module_path, folder_to_usdcat, ascii_output_mode, change_extension, remove_orig, progress_widget):
'''
:param Path folder_to_usdcat:
'''
progress_widget.setValue(0)
# Assuming default behavior is not changing the extension
infile_ext = _USD_DEFAULT_EXT
if change_extension:
infile_ext, outfile_ext = get_usd_input_output_extensions(ascii_output_mode=ascii_output_mode)
infiles = set()
for usd_ext in _ALL_USD_EXTS:
infiles.update(tuple(folder_to_usdcat.rglob('*' + usd_ext))) # taking even .usdc or .usda
num_jobs = len(infiles)
logger.debug('PERFORMING {} "USDCAT CONVERSION" jobs...'.format(num_jobs))
for i, infile in enumerate(infiles):
logger.debug('--Converting asset: {}'.format(infile.name))
if not change_extension:
cmd = [
hython_path,
usdcat_module_path,
infile.as_posix(),
'--out',
infile.as_posix(), # retaining the .usd extension and overwriting files
'--usdFormat',
_USD_ASCII_FORMATS[ascii_output_mode]
]
subprocess_call(cmd)
else:
cmd = [
hython_path,
usdcat_module_path,
infile.as_posix(),
'--out',
infile.with_suffix(outfile_ext).as_posix()
]
subprocess_call(cmd)
if remove_orig:
infile.unlink()
progress_widget.setValue(round(i / num_jobs * 100))
QtCore.QCoreApplication.processEvents() # update the progress bar visually
@log_exec_time
def subprocess_call(cmd):
# logger.debug(str(cmd))
try:
subprocess.call(cmd)
except Exception as e:
logger.warning('--Failed: {}'.format(e))
def batch_conversion(
get_bin_folder_method,
get_folder_to_usdcat_method,
get_ascii_output_mode_method,
get_change_extension_mode_method,
get_remove_orig_mode_method,
progress_widget
):
# Sanity check
bin_path = get_bin_folder_method()
folder_to_usdcat = get_folder_to_usdcat_method()
bin_path_validated = validate_folder_path(bin_path, _REQUIRED_BINARIES)
folder_to_usdcat_validated = validate_folder_path(folder_to_usdcat)
ascii_output_mode = get_ascii_output_mode_method()
change_extension = get_change_extension_mode_method()
if bin_path_validated and folder_to_usdcat_validated:
bin_path = Path(bin_path)
hython_path = bin_path / _HYTHON_BINARY
usdcat_module_path = bin_path / 'usdcat'
folder_to_usdcat = Path(folder_to_usdcat)
do_batch_usdcat = partial(
batch_usdcat,
hython_path=hython_path.as_posix(),
usdcat_module_path=usdcat_module_path.as_posix(),
folder_to_usdcat=folder_to_usdcat,
ascii_output_mode=ascii_output_mode,
change_extension=change_extension,
remove_orig=get_remove_orig_mode_method(),
progress_widget=progress_widget
)
do_edit_usd_asset_path = partial(
edit_usd_asset_path,
folder_to_usdcat=folder_to_usdcat,
ascii_output_mode=ascii_output_mode
)
if not change_extension:
do_batch_usdcat() # No need for editing file content
else:
if ascii_output_mode:
# Convert USD format with usdcat before editing file content
do_batch_usdcat()
do_edit_usd_asset_path()
else:
# Edit file content before converting USD format with usdcat
do_edit_usd_asset_path()
do_batch_usdcat()
progress_widget.setValue(100)
else:
logger.error('Invalid input. Aborted')
def count_jobs(generator_obj):
return len(tuple(generator_obj))
@log_exec_time
def edit_usd_asset_path(folder_to_usdcat, ascii_output_mode):
'''
:param Path folder_to_usdcat:
'''
infile_ext, outfile_ext = get_usd_input_output_extensions(ascii_output_mode=ascii_output_mode)
files_to_edit = set()
# First look for .usda files
files_to_edit.update(tuple(folder_to_usdcat.rglob('*' + _USD_ASCII_FORMATS[True])))
if not files_to_edit:
# Look for .usd files
# TODO: see if found .usd files are binary or ascii
files_to_edit.update(tuple(folder_to_usdcat.rglob('*' + _USD_DEFAULT_EXT)))
logger.debug('PERFORMING {} "USD ASSET PATH EDIT" jobs...'.format(len(files_to_edit)))
if files_to_edit:
files_to_edit = [infile.as_posix() for infile in files_to_edit]
# First edit if found '.usdc' or '.usda' in lines
with fileinput.input(files=files_to_edit, inplace=True) as usdfile:
try:
for line in usdfile:
print(line.replace(infile_ext + '@', outfile_ext + '@'), end='')
except Exception as e:
logger.exception(e)
# Second edit if found '.usd' in lines
with fileinput.input(files=files_to_edit, inplace=True) as usdfile:
try:
for line in usdfile:
print(line.replace(_USD_DEFAULT_EXT + '@', outfile_ext + '@'), end='')
except Exception as e:
logger.exception(e)
def open_explorer(get_folder_method):
folder_path = Path(get_folder_method())
if folder_path.exists() and folder_path.is_dir():
if _CUR_OS == "Windows":
from pathlib import PureWindowsPath
cmd = ['explorer', str(PureWindowsPath(folder_path))]
elif _CUR_OS == "Darwin":
cmd = ['open', '-R', folder_path.as_posix()]
subprocess.Popen(cmd)
def launch_usdview(get_bin_folder_method, get_file_to_usdview_method):
bin_path = get_bin_folder_method()
file_to_usdview = Path(get_file_to_usdview_method())
bin_path_validated = validate_folder_path(bin_path, _REQUIRED_BINARIES)
# file_to_usdview_validated = file_to_usdview.exists() and file_to_usdview.suffix in _USD_ASCII_FORMATS
if bin_path_validated:
bin_path = Path(bin_path)
hython_path = bin_path / _HYTHON_BINARY
usdview_module_path = bin_path / 'usdview'
cmd = [
hython_path,
usdview_module_path,
file_to_usdview.as_posix()
]
subprocess_call(cmd)
if __name__ == '__main__':
ui_launch()
pass