-
Notifications
You must be signed in to change notification settings - Fork 9
/
DRAG_AND_DROP_INSTALLER.py
134 lines (112 loc) · 5.17 KB
/
DRAG_AND_DROP_INSTALLER.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
"""
Drag and drop this file into your viewport to run the installer.
"""
import sys
import os
import glob
import shutil
import stat
import traceback
import time
import maya.cmds as cmds
def onMayaDroppedPythonFile(*args):
try:
package_name = "weights_editor_tool"
source_dir = os.path.dirname(__file__)
source_path = os.path.normpath(os.path.join(source_dir, "scripts", package_name))
# Make sure this installer is relative to the main tool.
relative_path = os.path.join(source_path, "weights_editor.py")
if not os.path.exists(relative_path):
raise RuntimeError("Unable to find 'scripts/weights_editor.py' relative to this installer file.")
# Suggest to install in user's script preferences.
prefs_dir = os.path.dirname(cmds.about(preferences=True))
scripts_dir = os.path.normpath(os.path.join(prefs_dir, "scripts"))
continue_option = "Continue"
manual_option = "No, let me choose"
cancel_option = "Cancel"
dialog = cmds.confirmDialog(
message=(
"The weights editor tool will be installed in a new folder here:<br>"
"<i>{}</i>".format(os.path.normpath(os.path.join(scripts_dir, package_name)))),
title="Installation path", icon="warning",
button=[continue_option, manual_option, cancel_option],
cancelButton=cancel_option, dismissString=cancel_option)
if dialog == continue_option:
install_path = scripts_dir
elif dialog == manual_option:
install_path = None
else:
return
# Open file picker to choose where to install to.
if install_path is None:
results = cmds.fileDialog2(
fileMode=3,
okCaption="Install here",
caption="Pick a folder to install to",
dir=scripts_dir)
# Exit if it was cancelled.
if not results:
return
install_path = os.path.normpath(results[0])
# Check if install path is in Python's path.
python_paths = [os.path.normpath(path) for path in sys.path]
if install_path not in python_paths:
cancel_option = "Cancel"
dialog = cmds.confirmDialog(
message=(
"Uh oh! Python can't see the path you picked:<br>"
"<i>{}</i><br><br>"
"This means the tool won't run unless you add this path to your environment variables using <b>Maya.env</b> or <b>userSetup.py</b>."
"<br><br>"
"Would you like to continue anyways?".format(install_path)),
title="Path not found in Python's paths!", icon="warning",
button=["Continue", cancel_option],
cancelButton=cancel_option, dismissString=cancel_option)
if dialog == cancel_option:
return
# If it already exists, asks if it's ok to overwrite.
tool_path = os.path.join(install_path, package_name)
if os.path.exists(tool_path):
dialog = cmds.confirmDialog(
message=(
"This folder already exists:<br>"
"<i>{}</i><br><br>"
"Continue to overwrite it?".format(tool_path)),
title="Warning!", icon="warning",
button=["OK", "Cancel"],
cancelButton="Cancel", dismissString="Cancel")
if dialog == "Cancel":
return
# May need to tweak permissions before deleting.
for root, dirs, files in os.walk(tool_path, topdown=False):
for name in files + dirs:
os.chmod(os.path.join(root, name), stat.S_IWUSR)
shutil.rmtree(tool_path)
# Windows may throw an 'access denied' exception doing a copytree right after a rmtree.
# Forcing it a slight delay seems to solve it.
time.sleep(1)
# Copy tool's directory over.
shutil.copytree(source_path, tool_path)
# Display success!
cmds.confirmDialog(
message=(
"The tool has been successfully installed!<br><br>"
"If you want to remove it then simply delete this folder:<br>"
"<i>{}</i><br><br>"
"Run the tool from the script editor by executing the following:<br>"
"<b>from weights_editor_tool import weights_editor<br>"
"weights_editor.run()</b>".format(tool_path)),
title="Install successful!",
button=["OK"])
except Exception as e:
# Display error message if an exception was raised.
print(traceback.format_exc())
cmds.confirmDialog(
message=(
"{}<br>"
"<br>"
"If you need help or have questions please send an e-mail with subject "
"<b>Weights editor installation</b> to <b>jasonlabbe@gmail.com</b>".format(e)),
title="Installation has failed!",
icon="critical",
button=["OK"])