forked from fredrikaverpil/pyvfx-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boilerplate.py
392 lines (312 loc) · 10.4 KB
/
boilerplate.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
# Author: fredrik.averpil@gmail.com, http://fredrikaverpil.tumblr.com
# Github: https://github.com/fredrikaverpil/pyVFX-boilerplate
''' Imports regardless of Qt type
--------------------------------------------------------------------------------
'''
import os, sys, json, platform
import xml.etree.ElementTree as xml
from cStringIO import StringIO
''' Configuration: Global settings
--------------------------------------------------------------------------------
QT_BINDINGS: Edit this to auto-detect or force Qt Bindnings: Auto|PySide|PyQt
EXTERNAL_SITE_PACKAGES_PATH_<OS>: Optional, location of site-packages location
(such as a virtualenv containing PySide and pysideuic and/or PyQt and SIP).
UI_FILE: Full path to .ui file to load, created by Qt Designer
WINDOW_TITLE: The visible title of the window
WINDOW_OBJECT: The name of the window object
sys.dont_write_bytecode: Skips .pyc file creation, if set to True '''
QT_BINDINGS = 'Auto'
EXTERNAL_SITE_PACKAGES_PATH_WINDOWS = None
EXTERNAL_SITE_PACKAGES_PATH_LINUX = None
EXTERNAL_SITE_PACKAGES_PATH_OSX = None
UI_FILE = os.path.join(os.path.dirname(__file__), 'main_window.ui')
WINDOW_TITLE = 'Hello World'
WINDOW_OBJECT = 'helloWorld'
sys.dont_write_bytecode = False
''' Configuration: Standalone mode
--------------------------------------------------------------------------------
MAYA_PALETTE: Uses approximately the same stylesheet as seen in Maya 2015 '''
MAYA_PALETTE = False
''' Configuration: Maya
--------------------------------------------------------------------------------
MAYA_LAUNCH_AS_DOCKED_WINDOW:
False = opens as free floating window
True = docks window to Maya UI '''
MAYA_LAUNCH_AS_DOCKED_WINDOW = False
''' Configuration: Nuke
--------------------------------------------------------------------------------
NUKE_LAUNCH_AS_PANEL: Opens a regular window or a panel
NUKE_PARENT_TO_NUKE_MAIN_WINDOW: If True, makes window stay on top of Nuke '''
NUKE_LAUNCH_AS_PANEL = False
NUKE_PARENT_TO_NUKE_MAIN_WINDOW = True
''' Run mode
--------------------------------------------------------------------------------
'''
RUN_MODE = 'standalone'
try:
import maya.cmds as cmds
import maya.OpenMayaUI as omui
import shiboken
RUN_MODE = 'maya'
except ImportError:
pass
try:
import nuke
import nukescripts
RUN_MODE = 'nuke'
except ImportError:
pass
''' Qt Bindnings
--------------------------------------------------------------------------------
'''
# Load external site-packages
def append_to_sys_path(path):
sys.path.append( path )
print 'Using external site-packages at', path
if ('windows' in platform.system().lower() and
type(EXTERNAL_SITE_PACKAGES_PATH_WINDOWS) != type(None) ):
append_to_sys_path(path=EXTERNAL_SITE_PACKAGES_PATH_WINDOWS)
elif ('linux' in platform.system().lower() and
type(EXTERNAL_SITE_PACKAGES_PATH_LINUX) != type(None) ):
append_to_sys_path(path=EXTERNAL_SITE_PACKAGES_PATH_LINUX)
elif ('darwin' in platform.system().lower() and
type(EXTERNAL_SITE_PACKAGES_PATH_OSX) != type(None) ):
append_to_sys_path(path=EXTERNAL_SITE_PACKAGES_PATH_OSX)
# Load Qt bindings
if QT_BINDINGS == 'Auto':
try:
from PySide import QtGui, QtGui, QtUiTools
import pysideuic
QT_BINDINGS = 'PySide'
except ImportError:
from PyQt4 import QtGui, QtCore, uic
import sip
QT_BINDINGS = 'PyQt'
elif QT_BINDINGS == 'PySide':
from PySide import QtGui, QtGui, QtUiTools
import pysideuic
elif QT_BINDINGS == 'PyQt':
from PyQt4 import QtGui, QtCore, uic
import sip
# Summary
print ('This app is now running in ' + RUN_MODE + ' mode,'
' using ' + QT_BINDINGS + ' Qt bindings')
''' Auto-setup classes and functions
--------------------------------------------------------------------------------
'''
class PyQtFixer(QtGui.QMainWindow):
def __init__(self, parent=None):
'''Super, loadUi, signal connections'''
super(PyQtFixer, self).__init__(parent)
print 'Making a detour (hack), necessary for when using PyQt'
def load_ui_type(UI_FILE):
'''
Pyside lacks the "load_ui_type" command, so we have to convert the ui file
to py code in-memory first and then execute it in a special frame to
retrieve the form_class.
'''
parsed = xml.parse(UI_FILE)
widget_class = parsed.find('widget').get('class')
form_class = parsed.find('class').text
with open(UI_FILE, 'r') as f:
o = StringIO()
frame = {}
if QT_BINDINGS == 'PySide':
pysideuic.compileUi(f, o, indent=0)
pyc = compile(o.getvalue(), '<string>', 'exec')
exec pyc in frame
# Fetch the base_class and form class based on their type in the xml
# from designer
form_class = frame['Ui_%s'%form_class]
base_class = eval('QtGui.%s'%widget_class)
elif QT_BINDINGS == 'PyQt':
form_class = PyQtFixer
base_class = QtGui.QMainWindow
return form_class, base_class
form, base = load_ui_type(UI_FILE)
def wrap_instance(ptr, base=None):
'''
Utility to convert a pointer to a Qt class instance (PySide/PyQt compatible)
:param ptr: Pointer to QObject in memory
:type ptr: long or Swig instance
:param base: (Optional) Base class to wrap with (Defaults to QObject,
which should handle anything)
:type base: QtGui.QWidget
:return: QWidget or subclass instance
:rtype: QtGui.QWidget
'''
if ptr is None:
return None
ptr = long(ptr) #Ensure type
if globals().has_key('shiboken'):
if base is None:
qObj = shiboken.wrapInstance(long(ptr), QtCore.QObject)
metaObj = qObj.metaObject()
cls = metaObj.className()
superCls = metaObj.superClass().className()
if hasattr(QtGui, cls):
base = getattr(QtGui, cls)
elif hasattr(QtGui, superCls):
base = getattr(QtGui, superCls)
else:
base = QtGui.QWidget
return shiboken.wrapInstance(long(ptr), base)
elif globals().has_key('sip'):
base = QtCore.QObject
return sip.wrapinstance(long(ptr), base)
else:
return None
def maya_main_window():
''' Returns the main Maya window. This works for both PySide and PyQt
since it uses the custom wrap_instance function. '''
main_window_ptr = omui.MQtUtil.mainWindow()
return wrap_instance( long( main_window_ptr ), QtGui.QWidget )
''' Main class
--------------------------------------------------------------------------------
'''
class HelloWorld(form, base):
def __init__(self, parent=None):
"""Super, loadUi, signal connections"""
super(HelloWorld, self).__init__(parent)
if QT_BINDINGS == 'PySide':
self.setupUi(self)
elif QT_BINDINGS == 'PyQt':
uic.loadUi(UI_FILE, self)
self.setObjectName(WINDOW_OBJECT)
self.setWindowTitle(WINDOW_TITLE)
# Access the UI, regardless of having used PySide or PyQt
# Example:
self.listWidget.addItem('Hello world')
def closeEvent(self, event):
''' Delete this object when closed.'''
self.deleteLater()
def set_maya_palette_with_tweaks(self):
palette_json_file = os.path.join(os.path.dirname(__file__),
'qpalette_maya2015.json')
groups = ['Disabled', 'Active', 'Inactive', 'Normal']
roles = [
'AlternateBase',
'Background',
'Base',
'Button',
'ButtonText',
'BrightText',
'Dark',
'Foreground',
'Highlight',
'HighlightedText',
'Light',
'Link',
'LinkVisited',
'Mid',
'Midlight',
'Shadow',
'ToolTipBase',
'ToolTipText',
'Text',
'Window',
'WindowText'
]
def set_palette_from_dict(dct):
palette = QtGui.QPalette()
for role in roles:
try:
for group in groups:
color = QtGui.QColor(dct['%s:%s' % (role, group)])
qGrp = getattr(QtGui.QPalette, group)
qRl = getattr(QtGui.QPalette, role)
palette.setColor(qGrp, qRl, color)
except:
print 'Could not use: ' + str(palette)
try:
QtGui.QApplication.setPalette(palette)
except:
print 'Could not set palette: ' + str(palette)
def set_style_plastique():
QtGui.QApplication.setStyle("plastique")
def set_maya_tweaks():
base_palette = QtGui.QApplication.palette()
# Set custom colors
LIGHT_COLOR = QtGui.QColor(100, 100, 100)
MID_COLOR = QtGui.QColor(68, 68, 68)
# Create a new palette
tab_palette = QtGui.QPalette(base_palette)
tab_palette.setBrush( QtGui.QPalette.Window,
QtGui.QBrush(LIGHT_COLOR))
tab_palette.setBrush( QtGui.QPalette.Button,
QtGui.QBrush(MID_COLOR))
# Define the widgets that needs tweaking
widget_palettes = {}
widget_palettes["QTabBar"] = tab_palette
widget_palettes["QTabWidget"] = tab_palette
# Set the new tweaked palette
for name, palette in widget_palettes.items():
QtGui.QApplication.setPalette(palette, name)
def read_json():
# read
with open(palette_json_file, 'rb') as data_file:
data = json.load(data_file)
return data
# Read the JSON theme data and set the palette
data = read_json()
set_palette_from_dict(data)
set_style_plastique()
set_maya_tweaks()
''' Run functions
--------------------------------------------------------------------------------
'''
def run_standalone():
app = QtGui.QApplication(sys.argv)
global gui
gui = HelloWorld()
if MAYA_PALETTE:
gui.set_maya_palette_with_tweaks()
gui.show()
sys.exit(app.exec_())
def run_maya():
if cmds.window(WINDOW_OBJECT, q=True, exists=True):
cmds.deleteUI(WINDOW_OBJECT)
if cmds.dockControl( 'MayaWindow|'+WINDOW_TITLE, q=True, ex=True):
cmds.deleteUI( 'MayaWindow|'+WINDOW_TITLE )
global gui
gui = HelloWorld( parent=maya_main_window() )
# Alternative way of setting parent window below:
#gui = HelloWorld( parent=QtGui.QApplication.activeWindow() )
if MAYA_LAUNCH_AS_DOCKED_WINDOW:
allowedAreas = ['right', 'left']
cmds.dockControl( WINDOW_TITLE, label=WINDOW_TITLE, area='left',
content=WINDOW_OBJECT, allowedArea=allowedAreas )
else:
#gui.setWindowModality(QtCore.Qt.WindowModal) # Set modality
gui.show()
def run_nuke():
module_name = __name__
if module_name == '__main__':
module_name = ''
else:
module_name = module_name + '.'
global gui
if NUKE_LAUNCH_AS_PANEL:
panel = nukescripts.panels.registerWidgetAsPanel(
widget = module_name + '.' + WINDOW_TITLE,
name = WINDOW_TITLE,
id='uk.co.thefoundry.' + WINDOW_TITLE,
create=True)
pane = nuke.getPaneFor('Properties.1')
panel.addToPane( pane )
gui = panel.customKnob.getObject().widget
else:
if NUKE_PARENT_TO_NUKE_MAIN_WINDOW:
gui = HelloWorld( parent=QtGui.QApplication.activeWindow() )
else:
gui = HelloWorld()
#gui.setWindowModality(QtCore.Qt.WindowModal) # Set modality
gui.show()
if RUN_MODE == 'standalone':
run_standalone()
'''
elif RUN_MODE == 'maya':
run_maya()
elif RUN_MODE == 'nuke':
run_nuke()
'''