-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderhive_installer.py
More file actions
577 lines (479 loc) · 13.5 KB
/
Copy pathrenderhive_installer.py
File metadata and controls
577 lines (479 loc) · 13.5 KB
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
from __future__ import print_function
import importlib
import datetime
import json
import os
import shutil
import sys
import tempfile
import maya.cmds as cmds
import maya.mel as mel
from api.version import PLUGIN_VERSION
SHELF_NAME = "RenderHive"
BUTTON_ANNOTATION = "Open RenderHive Maya Submitter"
MAIN_MENU_NAME = "RenderHiveMainMenu"
MAIN_MENU_LABEL = "RenderHive"
STARTUP_BLOCK_BEGIN = "# >>> RenderHive Maya startup >>>"
STARTUP_BLOCK_END = "# <<< RenderHive Maya startup <<<"
def get_installed_package_dir():
return os.path.join(
cmds.internalVar(
userScriptDir=True
),
"RenderHive"
)
def _ignore_runtime_content(
directory,
names,
):
ignored = []
ignored_names = {
"__pycache__",
".git",
".idea",
".vscode",
".venv",
"venv",
"backup",
"backups",
"logs",
"reports",
"tests",
"tools",
"contracts",
}
for name in names:
lowered = name.lower()
if (
name in ignored_names
or lowered.startswith("backup_")
or lowered.endswith(".zip")
or lowered.endswith(".pyc")
or lowered.endswith(".md")
or lowered.endswith(".yaml")
or lowered.endswith(".yml")
):
ignored.append(
name
)
return ignored
def _validate_staged_package(path):
required = (
"renderhive_maya_submitter.py",
os.path.join("api", "version.py"),
os.path.join("ui", "qt_submitter_window.py"),
os.path.join("validation", "validator.py"),
)
missing = [item for item in required if not os.path.isfile(os.path.join(path, item))]
if missing:
raise RuntimeError("Installer package is incomplete: {}".format(", ".join(missing)))
def copy_package_to_maya_scripts(source_dir):
install_dir = get_installed_package_dir()
parent = os.path.dirname(install_dir)
if not os.path.isdir(parent):
os.makedirs(parent)
stage_dir = tempfile.mkdtemp(prefix="RenderHive_stage_", dir=parent)
backup_dir = ""
try:
shutil.rmtree(stage_dir)
shutil.copytree(source_dir, stage_dir, ignore=_ignore_runtime_content)
_validate_staged_package(stage_dir)
if os.path.isdir(install_dir):
stamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
backup_dir = "{}_backup_{}".format(install_dir, stamp)
os.replace(install_dir, backup_dir)
os.replace(stage_dir, install_dir)
_validate_staged_package(install_dir)
return install_dir
except Exception:
if os.path.isdir(install_dir):
shutil.rmtree(install_dir, ignore_errors=True)
if backup_dir and os.path.isdir(backup_dir):
os.replace(backup_dir, install_dir)
raise
finally:
if os.path.isdir(stage_dir):
shutil.rmtree(stage_dir, ignore_errors=True)
def get_shelf_top_level():
return mel.eval(
"$tmp = $gShelfTopLevel"
)
def _python_open_command(install_dir):
return """
import importlib
import os
import sys
renderhive_path = r"{install_dir}"
if renderhive_path in sys.path:
sys.path.remove(renderhive_path)
sys.path.insert(0, renderhive_path)
import renderhive_maya_submitter
renderhive_maya_submitter.show_submitter()
""".format(
install_dir=str(install_dir).replace("\\", "\\\\")
)
def _python_validate_command(install_dir):
return """
import importlib
import os
import sys
renderhive_path = r"{install_dir}"
if renderhive_path in sys.path:
sys.path.remove(renderhive_path)
sys.path.insert(0, renderhive_path)
import renderhive_maya_submitter
renderhive_maya_submitter.show_submitter()
renderhive_maya_submitter.validate_scene_from_ui()
""".format(
install_dir=str(install_dir).replace("\\", "\\\\")
)
def remove_main_menu():
try:
if cmds.menu(MAIN_MENU_NAME, exists=True):
cmds.deleteUI(MAIN_MENU_NAME, menu=True)
except Exception:
pass
def ensure_main_menu(install_dir=None):
install_dir = os.path.abspath(
install_dir or get_installed_package_dir()
)
remove_main_menu()
try:
main_window = mel.eval("$tmp = $gMainWindow")
menu = cmds.menu(
MAIN_MENU_NAME,
label=MAIN_MENU_LABEL,
parent=main_window,
tearOff=False,
)
cmds.menuItem(
parent=menu,
label="Open RenderHive",
annotation="Open the RenderHive Maya Submitter",
sourceType="python",
command=_python_open_command(install_dir),
)
cmds.menuItem(
parent=menu,
label="Validate Current Scene",
annotation="Open RenderHive and validate the current Maya scene",
sourceType="python",
command=_python_validate_command(install_dir),
)
cmds.menuItem(parent=menu, divider=True)
cmds.menuItem(
parent=menu,
label="Uninstall RenderHive",
annotation="Remove RenderHive from this Maya installation",
sourceType="python",
command=(
"import renderhive_maya_submitter; "
"renderhive_maya_submitter.uninstall_renderhive_from_maya()"
),
)
return menu
except Exception:
return None
def _startup_block(install_dir):
safe_path = repr(os.path.abspath(install_dir))
return """{begin}
try:
import maya.utils as _renderhive_maya_utils
def _renderhive_install_menu():
import importlib
import sys
_renderhive_path = {path}
if _renderhive_path in sys.path:
sys.path.remove(_renderhive_path)
sys.path.insert(0, _renderhive_path)
import renderhive_installer
renderhive_installer.ensure_main_menu(_renderhive_path)
_renderhive_maya_utils.executeDeferred(_renderhive_install_menu)
except Exception:
pass
{end}
""".format(
begin=STARTUP_BLOCK_BEGIN,
end=STARTUP_BLOCK_END,
path=safe_path,
)
def _remove_startup_block(content):
start = content.find(STARTUP_BLOCK_BEGIN)
if start < 0:
return content
end = content.find(STARTUP_BLOCK_END, start)
if end < 0:
return content[:start].rstrip() + "\n"
end += len(STARTUP_BLOCK_END)
return (content[:start] + content[end:]).strip() + "\n"
def get_user_setup_path():
return os.path.join(
cmds.internalVar(userScriptDir=True),
"userSetup.py",
)
def install_startup_hook(install_dir):
path = get_user_setup_path()
folder = os.path.dirname(path)
if not os.path.isdir(folder):
os.makedirs(folder)
content = ""
if os.path.isfile(path):
try:
with open(path, "r", encoding="utf-8") as handle:
content = handle.read()
except Exception:
content = ""
content = _remove_startup_block(content).rstrip()
if content:
content += "\n\n"
content += _startup_block(install_dir)
with open(path, "w", encoding="utf-8") as handle:
handle.write(content)
return path
def remove_startup_hook():
path = get_user_setup_path()
if not os.path.isfile(path):
return False
try:
with open(path, "r", encoding="utf-8") as handle:
content = handle.read()
updated = _remove_startup_block(content)
with open(path, "w", encoding="utf-8") as handle:
handle.write(updated)
return True
except Exception:
return False
def ensure_shelf():
shelf_top = get_shelf_top_level()
shelves = cmds.tabLayout(
shelf_top,
query=True,
childArray=True
) or []
if SHELF_NAME not in shelves:
cmds.shelfLayout(
SHELF_NAME,
parent=shelf_top
)
cmds.tabLayout(
shelf_top,
edit=True,
selectTab=SHELF_NAME
)
return SHELF_NAME
def _is_renderhive_button(
button
):
values = []
for flag in (
"label",
"annotation",
"command",
):
try:
values.append(
cmds.shelfButton(
button,
query=True,
**{flag: True}
) or ""
)
except Exception:
pass
return "renderhive" in " ".join(
str(value)
for value in values
).lower()
def remove_renderhive_shelf_buttons():
try:
shelf_top = get_shelf_top_level()
shelves = cmds.tabLayout(
shelf_top,
query=True,
childArray=True
) or []
for shelf_name in shelves:
children = cmds.shelfLayout(
shelf_name,
query=True,
childArray=True
) or []
for child in children:
if _is_renderhive_button(
child
):
cmds.deleteUI(
child
)
try:
mel.eval(
"saveAllShelves $gShelfTopLevel;"
)
except Exception:
pass
except Exception:
pass
def create_shelf_button(
install_dir
):
shelf_name = ensure_shelf()
remove_renderhive_shelf_buttons()
icon_path = os.path.join(
install_dir,
"icons",
"renderhive_shelf_icon.png"
).replace(
"\\",
"/"
)
cmds.shelfButton(
parent=shelf_name,
label="",
annotation=BUTTON_ANNOTATION,
image=icon_path,
image1=icon_path,
imageOverlayLabel="",
style="iconOnly",
sourceType="python",
command=_python_open_command(install_dir),
)
try:
mel.eval(
"saveAllShelves $gShelfTopLevel;"
)
except Exception:
pass
def write_install_info(
install_dir,
source_dir,
):
info_path = os.path.join(
install_dir,
"renderhive_install_info.json"
)
with open(
info_path,
"w",
encoding="utf-8"
) as handle:
json.dump(
{
"source_dir": os.path.abspath(
source_dir
),
"install_dir": os.path.abspath(
install_dir
),
"plugin_version": PLUGIN_VERSION,
},
handle,
indent=4,
)
return info_path
def install_from_drag_drop(
source_dir
):
install_dir = copy_package_to_maya_scripts(
source_dir
)
write_install_info(
install_dir,
source_dir
)
create_shelf_button(
install_dir
)
install_startup_hook(install_dir)
ensure_main_menu(install_dir)
cmds.confirmDialog(
title="RenderHive Installed",
message=(
"RenderHive v{} was installed successfully.\n\n"
"Installed to:\n{}\n\n"
"A RenderHive shelf button and main-menu entry were created."
).format(
PLUGIN_VERSION,
install_dir
),
button=["OK"],
icon="information",
)
def close_renderhive_windows():
try:
from ui.qt_compat import QtWidgets
app = QtWidgets.QApplication.instance()
if app is not None:
for widget in app.topLevelWidgets():
if widget.objectName() in (
"RenderHiveWindow",
"RenderHiveQtSubmitter",
):
widget.close()
widget.deleteLater()
except Exception:
pass
try:
if cmds.window(
"renderHiveMayaSubmitter",
exists=True
):
cmds.deleteUI(
"renderHiveMayaSubmitter"
)
except Exception:
pass
def uninstall_renderhive(
confirm=True
):
install_dir = get_installed_package_dir()
if confirm:
result = cmds.confirmDialog(
title="Uninstall RenderHive",
message=(
"Remove the RenderHive shelf button and installed Maya copy?\n\n"
"{}\n\n"
"The original source package will not be deleted."
).format(
install_dir
),
button=["Uninstall", "Cancel"],
defaultButton="Cancel",
cancelButton="Cancel",
dismissString="Cancel",
icon="warning",
)
if result != "Uninstall":
return False
close_renderhive_windows()
remove_renderhive_shelf_buttons()
remove_main_menu()
remove_startup_hook()
if os.path.isdir(
install_dir
):
shutil.rmtree(
install_dir
)
# Clear the plugin from Maya's python memory cache
import sys
modules_to_remove = []
for mod_name, mod in sys.modules.items():
if getattr(mod, "__file__", None) and install_dir in getattr(mod, "__file__", ""):
modules_to_remove.append(mod_name)
elif mod_name.startswith("renderhive_"):
modules_to_remove.append(mod_name)
for mod_name in modules_to_remove:
try:
del sys.modules[mod_name]
except KeyError:
pass
cmds.confirmDialog(
title="RenderHive Uninstalled",
message=(
"RenderHive was removed from Maya.\n\n"
"Restart Maya if the shelf still appears."
),
button=["OK"],
icon="information",
)
return True