-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdev_controller.py
443 lines (340 loc) · 12.3 KB
/
dev_controller.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
import os
import sys
import json
import subprocess
import traceback
# from Qt import QtWidgets
# from Qt import QtCore
# from Qt import QtGui
# from Qt import QtCompat
try:
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2.QtUiTools import *
from PySide2 import __version__
import shiboken2 as shiboken
except ImportError:
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtUiTools import *
from PySide import __version__
import shiboken
import syntax
reload(syntax)
import maya.cmds as cmds
import maya.OpenMayaUI as apiUI
modulePath = os.path.dirname(__file__)
__windowTitle__ = 'Dev hub tool'
__UIName__ = 'devHubToolWindow'
config_filePath = modulePath + '/configure.json'
def setup_ui(uifile, base_instance=None):
"""Load a Qt Designer .ui file and returns an instance of the user interface
Args:
uifile (str): Absolute path to .ui file
base_instance (QWidget): The widget into which UI widgets are loaded
Returns:
QWidget: the base instance
"""
ui = QtCompat.loadUi(uifile) # Qt.py mapped function
if not base_instance:
return ui
else:
for member in dir(ui):
if not member.startswith('__') and \
member is not 'staticMetaObject':
setattr(base_instance, member, getattr(ui, member))
return ui
class RaiseError(QDialog):
def __init__(self, parent = None, title = '', message = '', state = 'error'):
super(RaiseError, self).__init__(parent)
self.setWindowTitle(title)
mainLayout = QVBoxLayout()
headerLayout = QHBoxLayout()
icon = QLabel()
icon.setPixmap(QPixmap(modulePath + '/icon/error.png').scaled(32,32))
label = QLabel()
label.setText(title)
label.setStyleSheet("font-size: 23px;")
headerLayout.addWidget(icon)
headerLayout.addWidget(label)
headerLayout.setStretch(1, 1)
textEdit = QTextEdit()
textEdit.setText(message)
textEdit.setReadOnly(True)
textEdit.setWordWrapMode(QTextOption.NoWrap)
mainLayout.addLayout(headerLayout)
mainLayout.addWidget(textEdit)
self.setLayout(mainLayout)
class tool_widget(QWidget):
tool_command = str()
location = str()
saveSetting = Signal(str)
def __init__(self, parent = None, parentwidget = None, modulename = '', command = '', location = ''):
super(tool_widget, self).__init__(parent)
# QWidget.__init__(parent)
self._parentwidget = parentwidget
self.tool_command = command
self.modulename = modulename
self.setupUi()
self.initConnection()
def setupUi(self):
# ===================== MAIN WIDGET =====================
self.horizontalLayout = QHBoxLayout( self )
self.groupbox = QGroupBox(self)
self.layout = QVBoxLayout(self.groupbox)
self.H_layout = QHBoxLayout( self)
self.label = QLabel(self)
self.openLocation = QPushButton(self.groupbox)
self.openLocation.setText("Open folder")
self.groupbox.setLayout(self.layout)
self.luanch_button = QPushButton(self)
self.luanch_button.setText("Luanch")
self.setting_button= QPushButton(self)
self.setting_button.setIcon(QIcon(modulePath + '/icon/gear.png'))
self.H_layout.addWidget(self.label)
self.H_layout.addWidget(self.openLocation)
self.H_layout.addWidget(self.setting_button)
self.layout.addLayout(self.H_layout)
self.layout.addWidget(self.luanch_button)
self.horizontalLayout.addWidget(self.groupbox)
self.H_layout.setStretchFactor(self.label, 1)
# Setup stylesheet
luanch_button_styleSheet = \
'''
QPushButton:disabled {background-color:#3f3f3f;
color: #757575;}
QPushButton{color : black ;background-color: #b8ff2b;
font-style : bold;border: none;
}
'''
self.luanch_button.setStyleSheet(luanch_button_styleSheet)
self.luanch_button.setMinimumHeight(30)
self.groupbox.setStyleSheet('''QGroupBox {font: bold;}''')
# ===================== SETTING WIDGET =====================
self.setting_widget = QWidget()
self.setting_widget.resize(450, 400)
setting_V_layout = QVBoxLayout( self.setting_widget )
setting_locationLabel = QLabel(self.setting_widget)
setting_locationLabel.setText("Location :")
self.setting_location = QLineEdit(self.setting_widget)
setting_label = QLabel(self.setting_widget)
setting_label.setText("command :")
self.setting_commandEditor = QTextEdit(self.setting_widget)
self.setting_commandEditor.setWordWrapMode(QTextOption.NoWrap)
self.setting_commandEditor.setPlainText(self.tool_command)
self.setting_saveSetting = QPushButton(self.setting_widget)
self.setting_saveSetting.setText("save setting")
setting_V_layout.addWidget(setting_locationLabel)
setting_V_layout.addWidget(self.setting_location)
setting_V_layout.addWidget(setting_label)
setting_V_layout.addWidget(self.setting_commandEditor)
setting_V_layout.addWidget(self.setting_saveSetting)
highlight = syntax.PythonHighlighter(self.setting_commandEditor.document())
if self.tool_command == '':
self.luanch_button.setEnabled(False)
def initConnection(self):
''' Initial connection in Widget '''
# Main widget
self.luanch_button.clicked.connect(self.__runCommand )
self.openLocation.clicked.connect( self.__openExplorer)
self.setting_button.clicked.connect(self.call_settingWindow)
# setting widget
self.setting_saveSetting.clicked.connect(self.__updateCommand)
def __updateCommand(self):
''' update command to '''
raw_data = self._parentwidget.tool_data
command = self.setting_commandEditor.toPlainText()
location = self.setting_location.text()
# Update
raw_data['tools'][self.modulename]['runcmd'] = command
raw_data['tools'][self.modulename]['location'] = location
save_config(config_filePath, raw_data)
def call_settingWindow(self):
''' open setting window '''
self.setting_widget.show()
def setTitle(self, title):
self.groupbox.setTitle(title)
self.modulename = title
def setLinkStatus(self, is_link = False):
if is_link and self.tool_command != '':
status = "Status : Linked"
pixmap = QPixmap( modulePath + '/icon/linked.png')
self.label.setPixmap(pixmap)
else :
status = "Status : Not linked"
pixmap = QPixmap( modulePath + '/icon/unlinked.png')
self.label.setPixmap(pixmap)
# self.label.setText(status)
def setCommand(self, command):
self.tool_command = command
def setLocation(self,location):
self.location = location
self.setting_location.setText(location)
def __openExplorer(self):
"""Open File explorer after finish."""
pathTofolder = self.location.replace('/', '\\')
subprocess.Popen('explorer \/select,\"%s\"' % pathTofolder)
def __runCommand(self):
''' Run given command '''
if self.tool_command == '':
print ("Please set up command")
return
try:
print(self.tool_command)
exec(self.tool_command)
except Exception as e:
print('\n============ ERROR ============\n')
try:
_exc = traceback.format_exc(e)
_m = _exc + "\n\nPlease check your tool's setting."
_d = RaiseError(parent=self, title='Excecute Error', message=_m, state='error')
_d.exec_()
except Exception as e :
traceback.print_exc(e)
# _dialog = QMessageBox.critical(self, "Execute error", _m)
# _dialog.exec_()
class dev_tools_controller(QMainWindow):
def __init__(self, parent = None):
super(dev_tools_controller, self).__init__(parent)
# QMainWindow.__init__(parent)
UI_path = modulePath + '/ui/devTools_manager.ui'
# # ---- LoadUI -----
# self.base_instance = setup_ui(UI_path, self)
# # -----------------
# ---- LoadUI -----
loader = QUiLoader()
currentDir = os.path.dirname(__file__)
file = QFile( UI_path )
file.open(QFile.ReadOnly)
self.ui = loader.load(file, parentWidget = self)
file.close()
# -----------------
# print self.base_instance
self._windowName_ = __windowTitle__
self._UIname_ = __UIName__
# Load data from dataset
self.tool_data = load_config(config_filePath)
self.__setupPythonPath()
self.__setupUI()
self.__initConnect()
self.ui.show()
def __setupPythonPath(self):
''' Append container's location path to PythonPath '''
if not self.tool_data['setting']['container_path'] in sys.path:
sys.path.append(self.tool_data['setting']['container_path'])
def __refresh(self):
''' Refresh data '''
self.tool_data = load_config(config_filePath)
self.__setupPythonPath()
# Clear tools list widget
self.ui.tools_listWidget.clear()
# Re-setui UI
self.__setupUI()
def __setupUI(self):
''' setup UI '''
self.ui.pushButton_refresh.setIcon(QIcon(modulePath + "/icon/refresh.png"))
# ===== set main tool page =====
# Create button
for module in sorted(self.tool_data["tools"].keys()) :
print (module)
runcmd = self.tool_data["tools"][module]['runcmd']
location = self.tool_data["tools"][module]['location']
# tools_listWidget
item = QListWidgetItem(self.ui.tools_listWidget)
item_widget = tool_widget(parent = self.ui, parentwidget = self, modulename = module, command = runcmd)
item_widget.setTitle(module)
item_widget.setLinkStatus(self.checkLinkStatus(modulename = module, modulePath = location))
item_widget.setLocation(location)
item.setSizeHint( item_widget.sizeHint() )
self.ui.tools_listWidget.addItem( item )
self.ui.tools_listWidget.setItemWidget( item, item_widget)
# ===== Setup Setting page =====
self.ui.lineEdit_location.setText(self.tool_data['setting']['container_path'])
def __initConnect(self):
''' Initial connection '''
self.ui.pushButton_saveSetting.clicked.connect(self.pushButton_saveSetting_onClicked)
self.ui.pushButton_refresh.clicked.connect(self.__refresh)
self.ui.pushButton_location_openExplorer.clicked.connect(self.pushButton_location_openExplorer_onClicked)
def checkLinkStatus(self,modulename, modulePath):
return True
def pushButton_saveSetting_onClicked(self):
# Get corrent data
# Get location
location = self.ui.lineEdit_location.text()
# Update data
self.tool_data['setting']['container_path'] = location
save_config(config_filePath, self.tool_data)
def pushButton_location_openExplorer_onClicked(self):
dialog = QFileDialog(self.ui)
dialog.setFileMode(QFileDialog.Directory)
dialog.setViewMode(QFileDialog.Detail)
if dialog.exec_():
fileNames = dialog.selectedFiles()
print self.ui.lineEdit_location.setText(fileNames[0])
# #####################################################################
def load_config(config_filePath):
# If not config
if not os.path.exists(config_filePath) :
sturcture = {'setting':{'container_path':''},'tools':{}}
save_config(config_filePath, sturcture)
# Load data
f = open(config_filePath, 'r')
data = json.loads( f.read() )
f.close()
try:
containerPath = data['setting']['container_path']
except KeyError :
if not data.has_key('setting') :
data['setting'] = {'container_path':''}
containerPath = data['setting']['container_path']
if not os.path.exists(containerPath):
containerPath = os.path.dirname(__file__)
# Check recent added module [When have new module]
allModules = set([i for i in os.listdir(containerPath) \
if os.path.isdir (containerPath + '/' + i) \
and os.path.exists(containerPath + '/' + i + '/__init__.py')])
diff_add = list(allModules.difference(set(data["tools"].keys()))) # find new module
diff_rem = list(set(data["tools"].keys()).difference(allModules)) # fine not exists module
if diff_add or diff_rem :
if diff_add :
# Add structure and place holder
for module in diff_add :
location = containerPath + '/' + module
data["tools"][module] = {"runcmd":"","location":location}
if diff_rem :
# Remove not exists item from data set
for module in diff_rem :
data['tools'].pop(module)
save_config(config_filePath,data)
print(data)
return data
def save_config(config_filePath,data):
''' write data to configure.json '''
f = open(config_filePath, 'w')
f.write( json.dumps(data,indent = 2) )
f.close()
print("Update data:\n" + json.dumps(data,indent = 2))
def getMayaWindow():
"""
Get the main Maya window as a QMainWindow instance
@return: QMainWindow instance of the top level Maya windows
"""
ptr = apiUI.MQtUtil.mainWindow()
if ptr is not None:
return shiboken.wrapInstance(long(ptr), QMainWindow)
def clearUI():
if cmds.window(__UIName__, exists=True):
cmds.deleteUI(__UIName__)
clearUI()
def run():
clearUI()
app = dev_tools_controller( getMayaWindow() )
# app.run()
if __name__ == '__main__':
print("run")
# run()
app = QApplication(sys.argv)
form = dev_tools_controller(getMayaWindow())
form.show()
app.exec_()