-
Notifications
You must be signed in to change notification settings - Fork 0
/
LightingManager.py
376 lines (287 loc) · 12.5 KB
/
LightingManager.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
import json
import os
import Qt
import time
from Qt import QtWidgets, QtCore, QtGui
import logging
"""
Lighting Manager By Chase Miller
INSTALLATION:
Place python files "Qt.py" and "LightingManager.py" in the MayaYEAR scripts folder.
To run script, type into Maya Python console the following:
import lightingManager
reload(lightingManager)
lightingManager.LightingManager(dock=False)
"""
logging.basicConfig()
logger = logging.getLogger('LightingManager')
# switch DEBUG to INFO when publishing.
logger.setLevel(logging.INFO)
# checks Qt binding
if Qt.__binding__.startswith('PyQt'):
# If using PyQt4 or PyQt5 we need to import sip
logger.debug('Using sip')
from sip import wrapinstance as wrapInstance
from Qt.QtCore import pyqtSignal as Signal
elif Qt.__binding__ == 'PySide':
# If using PySide (Maya 2016 and earlier), user needs shiboken instead
logger.debug('Using shiboken')
from shiboken import wrapInstance
from Qt.QtCore import Signal
else:
# PySide2(Maya 2017 and higher) uses shiboken2
logger.debug('Using shiboken2')
from shiboken2 import wrapInstance
from Qt.QtCore import Signal
from maya import OpenMayaUI as omui
import pymel.core as pm
# functional tools library, partial is for craeting temporary functions
from functools import partial
# Basic contoller for lights
class LightWidget(QtWidgets.QWidget):
# solo signal created for connecting to other Qt objects
onSolo = Signal(bool)
def __init__(self, light):
super(LightWidget, self).__init__()
# If light is a string, convert it to a PyMel object
if isinstance(light, basestring):
logger.debug('Converting node to a PyNode')
light = pm.PyNode(light)
# if transform make it a light shape,
if isinstance(light, pm.nodetypes.Transform):
light = light.getShape()
self.light = light
self.buildUI()
# THE UI
def buildUI(self):
layout = QtWidgets.QGridLayout(self)
self.name = name = QtWidgets.QCheckBox(str(self.light.getTransform()))
name.setChecked(self.light.visibility.get())
# lambdas are one time use functions.
name.toggled.connect(lambda val: self.light.visibility.set(val))
# (row 0, column 0)
layout.addWidget(name, 0, 0)
# button to solo the light
solo = QtWidgets.QPushButton('Solo')
solo.setCheckable(True)
solo.toggled.connect(lambda val: self.onSolo.emit(val))
layout.addWidget(solo, 0, 1)
# delete light button
delete = QtWidgets.QPushButton('X')
delete.clicked.connect(self.deleteLight)
delete.setMaximumWidth(20)
delete.setStyleSheet('background-color: rgba(255, 0, 0, 1.0);')
layout.addWidget(delete, 0, 2)
# slider for intensity of light
intensity = QtWidgets.QSlider(QtCore.Qt.Horizontal)
intensity.setMinimum(1)
intensity.setMaximum(1000)
# set its current value based on the intensity of the light itself
intensity.setValue(self.light.intensity.get())
intensity.valueChanged.connect(lambda val: self.light.intensity.set(val))
# row 1, column 2, take 1 row and 2 columns of space.
layout.addWidget(intensity, 1, 0, 1, 2)
# color light button
self.colorBtn = QtWidgets.QPushButton()
self.colorBtn.setMaximumWidth(20)
self.colorBtn.setMaximumHeight(20)
self.setButtonColor()
self.colorBtn.clicked.connect(self.setColor)
layout.addWidget(self.colorBtn, 1, 2)
# widget should never be larger than the maximum space it needs
self.setSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum)
def disableLight(self, val):
# takes a value, converts it to bool, then sets checkbox to value
self.name.setChecked(not bool(val))
def deleteLight(self):
# set parent to Nothing, will remove it from the manager UI and tells Qt to drop it
self.setParent(None)
# awkward period of time before Qt deletes it after clicking X..
# So mark its visibility to False
self.setVisible(False)
# delete it later just in case it hasn't gotten the hint yet
self.deleteLater()
pm.delete(self.light.getTransform())
def setColor(self):
lightColor = self.light.color.get()
color = pm.colorEditor(rgbValue=lightColor)
# it gives back a string instead of a list of numbers.
# split the string, then convert it to floats
r, g, b, a = [float(c) for c in color.split()]
color = (r, g, b)
self.light.color.set(color)
self.setButtonColor(color)
def setButtonColor(self, color=None):
# If no color, use color from the light
if not color:
color = self.light.color.get()
assert len(color) == 3, "You must provide a list of 3 colors"
# multiply members of color by 255 to get the correct number
r, g, b = [c * 255 for c in color]
self.colorBtn.setStyleSheet('background-color: rgba(%s, %s, %s, 1.0);' % (r, g, b))
# Main Lighting Manager
class LightingManager(QtWidgets.QWidget):
lightTypes = {
"Point Light": pm.pointLight,
"Spot Light": pm.spotLight,
"Area Light": partial(pm.shadingNode, 'areaLight', asLight=True),
"Directional Light": pm.directionalLight,
"Volume Light": partial(pm.shadingNode, 'volumeLight', asLight=True)
}
# Set Dock to True if you want it to dock
def __init__(self, dock=False):
if dock:
parent = getDock()
else:
deleteDock()
try:
pm.deleteUI('lightingManager')
except:
logger.debug('No previous UI exists')
parent = QtWidgets.QDialog(parent=getMayaMainWindow())
parent.setObjectName('lightingManager')
parent.setWindowTitle('Lighting Manager')
dlgLayout = QtWidgets.QVBoxLayout(parent)
super(LightingManager, self).__init__(parent=parent)
self.buildUI()
self.populate()
self.parent().layout().addWidget(self)
if not dock:
parent.show()
def buildUI(self):
layout = QtWidgets.QGridLayout(self)
# Comboboxes are essentially dropdown selectionwidgets
self.lightTypeCB = QtWidgets.QComboBox()
for lightType in sorted(self.lightTypes):
self.lightTypeCB.addItem(lightType)
# take 1 row, and two columns worth of space
layout.addWidget(self.lightTypeCB, 0, 0, 1, 2)
# create button
createBtn = QtWidgets.QPushButton('Create')
createBtn.setStyleSheet('background-color: rgba(53, 126, 189, 1.0);')
createBtn.clicked.connect(self.createLight)
layout.addWidget(createBtn, 0, 2)
# scroll container widget
scrollWidget = QtWidgets.QWidget()
scrollWidget.setSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum)
self.scrollLayout = QtWidgets.QVBoxLayout(scrollWidget)
scrollArea = QtWidgets.QScrollArea()
scrollArea.setWidgetResizable(True)
scrollArea.setWidget(scrollWidget)
layout.addWidget(scrollArea, 1, 0, 1, 3)
# Save button for lights setup
saveBtn = QtWidgets.QPushButton('Save')
saveBtn.clicked.connect(self.saveLights)
layout.addWidget(saveBtn, 2, 0)
# import button for lights
importBtn = QtWidgets.QPushButton('Import')
importBtn.clicked.connect(self.importLights)
layout.addWidget(importBtn, 2, 1)
# Refresh button
refreshBtn = QtWidgets.QPushButton('Refresh')
refreshBtn.clicked.connect(self.refresh)
layout.addWidget(refreshBtn, 2, 2)
def refresh(self):
# while scrollLayout.count() gives any Truth-y value, run logic
while self.scrollLayout.count():
widget = self.scrollLayout.takeAt(0).widget()
if widget:
# Set visibility to False because there is a period where it will still be alive
widget.setVisible(False)
# kill the widget when it can
widget.deleteLater()
self.populate()
def populate(self):
for light in pm.ls(type=["areaLight", "spotLight", "pointLight", "directionalLight", "volumeLight"]):
self.addLight(light)
# save lights to JSON file
def saveLights(self):
properties = {}
for lightWidget in self.findChildren(LightWidget):
light = lightWidget.light
transform = light.getTransform()
# add it to the dictionary.
properties[str(transform)] = {
'translate': list(transform.translate.get()),
'rotation': list(transform.rotate.get()),
'lightType': pm.objectType(light),
'intensity': light.intensity.get(),
'color': light.color.get()
}
# fetch the light manager directory to save in
directory = self.getDirectory()
# construct name of the lightFile to save
# %m%d%S = (month/day/secounds)
lightFile = os.path.join(directory, 'lightFile_%s.json' % time.strftime('%m%d%S'))
# open file to write
with open(lightFile, 'w') as f:
json.dump(properties, f, indent=4)
logger.info('Saving file to %s' % lightFile)
def getDirectory(self):
# gives us back the name of our library directory and create it if it doesn't exist
directory = os.path.join(pm.internalVar(userAppDir=True), 'lightManager')
if not os.path.exists(directory):
os.mkdir(directory)
return directory
def importLights(self):
directory = self.getDirectory()
fileName = QtWidgets.QFileDialog.getOpenFileName(self, "Light Browser", directory)
# open fileName in read mode
with open(fileName[0], 'r') as f:
properties = json.load(f)
for light, info in properties.items():
lightType = info.get('lightType')
for lt in self.lightTypes:
# the light type for Point Light is pointLight, convert Point Light to pointLight and then compare
if ('%sLight' % lt.split()[0].lower()) == lightType:
# If match found, break out
break
else:
logger.info('Cannot find a corresponding light type for %s (%s)' % (light, lightType))
continue
light = self.createLight(lightType=lt)
light.intensity.set(info.get('intensity'))
light.color.set(info.get('color'))
transform = light.getTransform()
transform.translate.set(info.get('translate'))
transform.rotate.set(info.get('rotation'))
self.populate()
def createLight(self, lightType=None, add=True):
# get text from the combobox if no light is given
if not lightType:
lightType = self.lightTypeCB.currentText()
# look up lightTypes dictionary to find function to call
func = self.lightTypes[lightType]
light = func()
# pass to addLight if the method has been told to add it
if add:
self.addLight(light)
return light
# create a LightWidget for light and add it to the UI
def addLight(self, light):
widget = LightWidget(light)
# connect the onSolo signal from the widget to isolate method
widget.onSolo.connect(self.isolate)
self.scrollLayout.addWidget(widget)
# function for isolateing a single light
def isolate(self, val):
lightWidgets = self.findChildren(LightWidget)
for widget in lightWidgets:
if widget != self.sender():
widget.disableLight(val)
def getMayaMainWindow():
win = omui.MQtUtil_mainWindow()
ptr = wrapInstance(long(win), QtWidgets.QMainWindow)
return ptr
def getDock(name='LightingManagerDock'):
# delete any conflicting docks
deleteDock(name)
ctrl = pm.workspaceControl(name, dockToMainWindow=('right', 1), label="Lighting Manager")
qtCtrl = omui.MQtUtil_findControl(ctrl)
# wrapInstance used to convert it to something Python can understand, in this case a QWidget
ptr = wrapInstance(long(qtCtrl), QtWidgets.QWidget)
return ptr
def deleteDock(name='LightingManagerDock'):
# workspaceControl used to see if dock exists
if pm.workspaceControl(name, query=True, exists=True):
pm.deleteUI(name)