-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathquicklauncher.py
200 lines (158 loc) · 5.83 KB
/
quicklauncher.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
# The MIT License (MIT)
# Copyright (c) 2014 Cesar Saez
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import os
import sys
import inspect
from importlib import import_module
from maya import cmds, mel
# qt bindings
try:
from PySide6 import QtCore, QtGui, QtWidgets
except ImportError:
try:
from PySide2 import QtCore, QtGui, QtWidgets
except ImportError:
from PySide import QtCore, QtGui
QtWidgets = QtGui
__all__ = ['show', 'get_main_window', 'get_repo', 'set_repo', 'list_scripts',
'get_scripts', 'list_commands', 'get_commands', 'run_script',
'run_cmd']
def get_repo():
settings = QtCore.QSettings("csaez", "quicklauncher")
repo = settings.value("repo")
if not repo:
repo = os.path.join(os.path.expanduser("~"), "quicklauncher")
set_repo(repo)
return repo
def set_repo(repo_path):
if not os.path.exists(repo_path):
os.mkdir(repo_path)
settings = QtCore.QSettings("csaez", "quicklauncher")
settings.setValue("repo", repo_path)
def get_scripts(): # {name: path, ...}
items = [os.path.join(path, f)
for (path, _, files) in os.walk(get_repo())
for f in files if f.endswith(".py") or f.endswith(".mel")]
return {os.path.basename(x): x for x in items}
def list_scripts():
return list(get_scripts().keys())
def get_commands(): # {name: cmd, ...}
items = [(name.lower(), value)
for (name, value) in inspect.getmembers(cmds, callable)]
return dict(items)
def list_commands():
return list(get_commands().keys())
def run_script(script_name):
# validate
script_path = get_scripts().get(script_name)
if not script_path:
return False
# delegate to lang runner
if script_name.endswith(".py"):
return _run_python(script_path)
if script_name.endswith(".mel"):
return _run_mel(script_path)
return False
def _run_mel(script_path):
filepath = os.path.join(get_repo(), script_path)
if not os.path.exists(filepath):
return False
with open(filepath, "r") as fp:
mel.eval(fp.read())
return True
def _run_python(script_path):
# add to pythonpath and execute
sys.path.insert(0, os.path.dirname(script_path))
module_name = os.path.split(script_path)[-1].replace(".py", "")
import_module(module_name)
# cleanup
del sys.modules[module_name]
sys.path = sys.path[1:]
return True
def run_cmd(cmd_name):
cmd = get_commands().get(cmd_name.lower())
if cmd is not None:
cmd()
return True
return False
# GUI
class QuickLauncherMenu(QtWidgets.QMenu):
"""A menu to find and execute Maya commands and user scripts."""
def __init__(self, *args, **kwds):
super(QuickLauncherMenu, self).__init__(*args, **kwds)
self.init_gui()
def init_gui(self):
# set search field
self.lineEdit = QtWidgets.QLineEdit(self)
action = QtWidgets.QWidgetAction(self)
action.setDefaultWidget(self.lineEdit)
self.addAction(action)
self.lineEdit.setFocus()
# completion
items = list_scripts()
items.extend(list_commands())
completer = QtWidgets.QCompleter(items, self)
completer.setCompletionMode(QtWidgets.QCompleter.PopupCompletion)
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.lineEdit.setCompleter(completer)
# connect signals
self.lineEdit.returnPressed.connect(self.accept)
self.setMinimumWidth(300)
def accept(self):
text = self.lineEdit.text()
completion = self.lineEdit.completer().currentCompletion()
self.close()
return run_cmd(text) or \
run_cmd(completion) or \
run_script(text) or \
run_script(completion)
def select_repo():
repo = QtWidgets.QFileDialog.getExistingDirectory(
caption="Select repository directory",
parent=get_main_window(),
dir=get_repo())
if repo:
set_repo(repo)
def get_main_window(widget=None):
widget = widget or QtWidgets.QApplication.activeWindow()
if widget is None:
return
parent = widget.parent()
if parent is None:
return widget
return get_main_window(parent)
def show():
maya_window = get_main_window()
# look for existing instances of QuickLauncher
ql = get_instance(maya_window, QuickLauncherMenu)
if ql is None:
# create a new instance
ql = QuickLauncherMenu(maya_window)
# clear out, move and show
ql.lineEdit.setText("")
position_window(ql)
ql.exec_()
def position_window(window):
"""Position window to mouse cursor"""
pos = QtGui.QCursor.pos()
window.move(pos.x(), pos.y())
def get_instance(parent, gui_class):
for children in parent.children():
if isinstance(children, gui_class):
return children
return None