-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtweener-install.py
More file actions
340 lines (272 loc) · 10.5 KB
/
tweener-install.py
File metadata and controls
340 lines (272 loc) · 10.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
import sys
import os
import shutil
import json
import uuid
import zipfile
import logging
import ssl
from functools import partial
import maya.cmds
# Python 3.0
if sys.version_info >= (3, 0):
from urllib.request import urlopen
else:
from urllib2 import urlopen
# Python 3.4
if sys.version_info >= (3, 4):
import importlib as imp
else:
import imp
import maya.cmds as cmds
import maya.mel as mel
import maya.utils as utils
# Maya version specific imports
maya_version = int(cmds.about(apiVersion=True))
if maya_version >= 20250000:
from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from shiboken6 import wrapInstance
else:
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from shiboken2 import wrapInstance
github_url = 'https://api.github.com/repos/monoteba/maya-tweener/releases/latest'
gMainProgressBar = mel.eval('$tmp = $gMainProgressBar')
plugin_name = 'tweener.py'
def onMayaDroppedPythonFile(*args):
QApplication.processEvents()
utils.executeDeferred(main)
def main():
result = cmds.confirmDialog(title='Tweener Installation',
message='Would you like to download and install Tweener?',
button=['Download', 'Offline Installation', 'Cancel'],
defaultButton='Download',
cancelButton='Cancel',
dismissString='Cancel')
if result == 'Cancel':
return
if result == 'Offline Installation':
show_offline_window()
return
sys.stdout.write('# Downloading Tweener...\n')
QApplication.processEvents()
zip_path = download()
if zip_path is None:
show_offline_window()
return
plugin_path = install(zip_path)
load(plugin_path)
def download():
try:
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS) if sys.version_info >= (3, 0) else ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
except Exception as e:
sys.stdout.write('Error: %s\n' % e)
sys.stdout.write('# Failed to set SSL Context.\n')
return None
try:
# get zip url from github
response = urlopen(github_url, timeout=10, context=ssl_context)
data = json.load(response)
except Exception as e:
sys.stdout.write('Error: %s\n' % e)
sys.stdout.write('# No internet connection: using offline installation.\n')
return None
try:
assets = data['assets']
except Exception as e:
logging.exception(e)
return None
if assets is None or len(assets) == 0:
sys.stderr.write('# Could not locate zip from url %s' % github_url)
cmds.progressBar(gMainProgressBar, edit=True, endProgress=True)
return None
asset = assets[0]
zip_url = asset['browser_download_url']
# download and temporarily save zip file
name = 'tweener-' + uuid.uuid1().hex + '.zip'
dir_path = os.path.dirname(__file__)
zip_path = dir_path + '/' + name
# url
try:
f = urlopen(zip_url, timeout=10, context=ssl_context)
except Exception as e:
logging.exception(e)
return None
# try to get file size
total_size = 1
try:
total_size = f.info().getheader('Content-Length').strip()
header = True
except AttributeError:
header = False # a response doesn't always include the "Content-Length" header
if header:
total_size = int(total_size)
cmds.progressBar(gMainProgressBar,
edit=True,
beginProgress=True,
isInterruptable=False,
status='"Downloading Tweener...',
maxValue=total_size)
try:
with open(zip_path, 'wb') as local_file:
while True:
chunk = f.read(4096)
if not chunk:
break
cmds.progressBar(gMainProgressBar, edit=True, step=len(chunk))
local_file.write(chunk)
QApplication.processEvents()
except Exception as e:
logging.exception(e)
cmds.progressBar(gMainProgressBar, edit=True, endProgress=True)
return None
finally:
sys.stdout.write('# Download successful, installing...\n')
cmds.progressBar(gMainProgressBar, edit=True, endProgress=True)
return zip_path
def install(zip_path, remove_zip=True):
# maya plug-ins dir
maya_app_dir = cmds.internalVar(userAppDir=True)
# create maya plug-ins dir (if it does not exist)
maya_plug_in_dir = maya_app_dir + 'plug-ins/'
if not os.path.exists(maya_plug_in_dir):
try:
sys.stdout.write('# plug-ins directory does not exists, creating it at %s\n' % maya_plug_in_dir)
os.makedirs(maya_plug_in_dir)
except Exception as e:
logging.exception(e)
cmds.progressBar(gMainProgressBar, edit=True, endProgress=True)
return None
# extract zip
extract_path = maya_plug_in_dir + 'tweener'
if os.path.exists(extract_path):
try:
shutil.rmtree(extract_path) # remove existing folder
except Exception as e:
logging.exception(e)
cmds.progressBar(gMainProgressBar, edit=True, endProgress=True)
return None
finally:
sys.stdout.write('# Removed old installation!\n')
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(extract_path)
# create maya modules dir (if it does not exist)
maya_modules_dir = maya_app_dir + 'modules/'
if not os.path.exists(maya_modules_dir):
try:
os.makedirs(maya_modules_dir)
except Exception as e:
logging.exception(e)
cmds.progressBar(gMainProgressBar, edit=True, endProgress=True)
return None
try:
with open(maya_modules_dir + 'tweener.mod', 'w') as f:
f.write('+ Tweener 0.0 %s\n' % extract_path)
f.write('MAYA_PLUG_IN_PATH +:= \n')
except Exception as e:
logging.exception(e)
cmds.progressBar(gMainProgressBar, edit=True, endProgress=True)
return None
finally:
sys.stdout.write('# Created module file at "%s"\n' % maya_modules_dir)
# clean-up
if remove_zip:
try:
sys.stdout.write('# Cleaning up...\n')
os.remove(zip_path)
except Exception as e:
logging.exception(e)
cmds.progressBar(gMainProgressBar, edit=True, endProgress=True)
finally:
sys.stdout.write('# Removed %s\n' % zip_path)
return extract_path
def load(plugin_path):
if os.name == 'nt':
env_path = ';%s' % plugin_path
else:
env_path = ':%s' % plugin_path
maya_plugin_path = mel.eval('getenv "MAYA_PLUG_IN_PATH"')
if plugin_path not in maya_plugin_path:
mel.eval('putenv "MAYA_PLUG_IN_PATH" "' + maya_plugin_path + env_path + '"')
if cmds.pluginInfo(plugin_name, q=True, r=True):
try:
cmds.unloadPlugin(plugin_name, force=True)
except Exception as e:
logging.exception(e)
sys.path.append(plugin_path)
try:
import tweener
if sys.version_info >= (3, 0):
import importlib
importlib.reload(tweener)
else:
imp.reload(tweener)
tweener.reload_mods()
except Exception as e:
logging.exception(e)
try:
cmds.loadPlugin(plugin_name)
if cmds.pluginInfo(plugin_name, q=True, r=True):
cmds.pluginInfo(plugin_name, e=True, autoload=True)
except Exception as e:
logging.exception(e)
try:
cmds.tweener()
except Exception as e:
cmds.warning('Could not execute tweener command: %s' % str(e))
answer = cmds.confirmDialog(title='Tweener Installed!',
message='Tweener was installed at:\n'
'%s\n\n'
'Would you like to add a shelf button to the current shelf?' % plugin_path,
button=['Yes', 'No'],
defaultButton='Yes',
cancelButton='No',
dismissString='No')
if answer == 'Yes':
try:
tweener.ui.add_shelf_button(path=plugin_path)
except Exception as e:
logging.exception(e)
sys.stdout.write('# Tweener install completed! See the Script Editor for more information.\n')
def show_offline_window():
window = cmds.window(title="Tweener Offline Install", resizeToFitChildren=True, sizeable=False)
form = cmds.formLayout(numberOfDivisions=100)
column = cmds.columnLayout(adjustableColumn=True)
cmds.text(label='Tweener was not downloadeded automatically.', align='left')
cmds.text(label=' ')
cmds.text(label='Please download the latest release from:', align='left')
cmds.text(
label='<a style="color:#ff8a00;" href="https://github.com/monoteba/maya-tweener/releases/latest">'
'https://github.com/monoteba/maya-tweener/releases/latest</a>',
align='left',
hyperlink=True,
highlightColor=(1.0, 1.0, 1.0))
cmds.text(label=' ')
cmds.text(label='The file is called \"tweener-1.0.0.zip\" or similar.', align='left')
cmds.text(label=' ')
cmds.text(label=' ')
cmds.setParent('..')
button = cmds.button(label='Install from .zip', command=partial(offline_install, window))
cmds.setParent('..')
cmds.formLayout(form, edit=True, attachForm=[(column, 'left', 10),
(column, 'top', 10),
(column, 'right', 10),
(button, 'left', 10),
(button, 'bottom', 10),
(button, 'right', 10)])
cmds.showWindow(window)
def offline_install(window, *args):
cmds.deleteUI(window)
zip_path = get_zip()
if not zip_path:
show_offline_window()
return
print(zip_path)
plugin_path = install(zip_path[0], remove_zip=False)
load(plugin_path)
def get_zip():
zipFilter = 'ZIP file (*.zip)'
return cmds.fileDialog2(fileFilter=zipFilter, dialogStyle=2, fileMode=1)