-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightingManager.py
262 lines (202 loc) · 8.48 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
import Qt
import os
import json
import time
from PySide2 import QtWidgets, QtGui, QtCore
import pymel.core as pm
from functools import partial
from past.types import basestring
from shiboken2 import wrapInstance
import logging
from maya import OpenMayaUI as omui
logging.basicConfig()
logger = logging.getLogger('LightingManager')
logger.setLevel(logging.DEBUG)
logger.debug('Using PySide2 with shiboken2')
def getMayaMainWindow():
win = omui.MQtUtil_mainWindow()
ptr = wrapInstance(Qt.long(win), QtWidgets.QMainWindow) #Convert the memory address into integer
return ptr
def getDock(name='LightingManagerDock'):
deleteDock(name)
ctrl = pm.workspaceControl(name, dockToMainWindow=('right', 1), label="Lighting Manager")
qtCtrl = omui.MQtUtil_findControl(ctrl)
ptr = wrapInstance(Qt.long(qtCtrl), QtWidgets.QWidget)
return ptr
def deleteDock(name='LightingManagerDock'):
if pm.workspaceControl(name, query=True, exists=True):
pm.deleteUI(name)
class LightManager(QtWidgets.QWidget):
lightTypes = {
"Point Light": pm.pointLight,
"Spot Light": pm.spotLight,
"Directional Light": pm.directionalLight,
"Area Light": partial(pm.shadingNode, 'areaLight', asLight=True),
"Volume Light": partial(pm.shadingNode, 'volumeLight', asLight=True)
}
def __init__(self, dock=True):
if dock:
parent = getDock()
else:
deleteDock()
try:
pm.deleteUI('lightingManager')
except:
logger.debug("No previous UI exists")
parent = QtWidgets.QDialog(parent=getMayaMainWindow())
parent.setObjectName('lightManager')
parent.setWindowTitle('Lighting Manager')
layout = QtWidgets.QVBoxLayout(parent)
super(LightManager, self).__init__(parent=parent)
self.buildUI()
self.populate()
self.parent().layout().addWidget(self)
if not dock:
parent.show()
def populate(self):
while self.scrollLayout.count():
widget = self.scrollLayout.takeAt(0).widget()
if widget:
widget.setVisible(False)
widget.deleteLater()
for light in pm.ls(type=["areaLight", "spotLight", "pointLight", "directionalLight", "volumeLight"]):
self.addLight(light)
def buildUI(self):
layout = QtWidgets.QGridLayout(self)
self.lightTypeCB = QtWidgets.QComboBox()
for lightType in sorted(self.lightTypes):
self.lightTypeCB.addItem(lightType)
layout.addWidget(self.lightTypeCB, 0, 0, 1, 2)
createBtn = QtWidgets.QPushButton('Create')
createBtn.clicked.connect(self.createLight)
layout.addWidget(createBtn, 0, 2)
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)
refreshBtn = QtWidgets.QPushButton('Refresh')
refreshBtn.clicked.connect(self.populate())
layout.addWidget(refreshBtn, 2, 1)
saveBtn = QtWidgets.QPushButton('Save')
saveBtn.clicked.connect(self.saveLights)
layout.addWidget(saveBtn, 2, 0)
importBtn = QtWidgets.QPushButton('Import')
importBtn.clicked.connect(self.importLights)
layout.addWidget(importBtn, 2, 2)
def saveLights(self):
properties = { }
for lightWidget in self.findChildren(LightWidget):
light = lightWidget.light
transform = light.getTransform()
properties[str(transform)] = {
'translate': list(transform.translate.get()),
'rotation': list(transform.rotate.get()),
'lightType':pm.objectType(light),
'intensity': light.intensity.get(),
'colour': light.color.get()
}
directory = self.getDirectory()
lightFile = os.path.join(directory, 'lightFile_%s.json' % time.strftime('%m%d'))
with open(lightFile, 'w') as f:
json.dump(properties, f, indent=4)
logger.info('Saving file to %s' % lightFile)
def getDirectory(self):
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)
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:
if '%sLight' % lt.split()[0].lower() == lightType:
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('colour'))
transform = light.getTransform()
transform.translate.set(info.get('translate'))
transform.rotate.set(info.get('rotation'))
def createLight(self, lightType=None, add=True):
if not lightType:
lightType = self.lightTypeCB.currentText()
func = self.lightTypes[lightType]
light = func()
if add:
self.addLight(light)
return light
def addLight(self, light):
widget = LightWidget(light)
self.scrollLayout.addWidget(widget)
widget.onSolo.connect(self.onSolo)
def onSolo(self, value):
lightWidgets = self.findChildren(LightWidget)
for widget in lightWidgets:
if widget != self.sender():
widget.disableLight(value)
class LightWidget(QtWidgets.QWidget):
onSolo = QtCore.Signal(bool)
def __init__(self, light):
super(LightWidget, self).__init__()
if isinstance(light, basestring):
light = pm.PyNode(light)
if isinstance(light, pm.nodetypes.Transform):
light = light.getShape()
self.light = light
self.buildUI()
def buildUI(self):
layout = QtWidgets.QGridLayout(self)
self.name = QtWidgets.QCheckBox(str(self.light.getTransform()))
self.name.setChecked(self.light.visibility.get())
self.name.toggled.connect(lambda val: self.light.getTransform().visibility.set(val))
layout.addWidget(self.name, 0, 0)
soloBtn = QtWidgets.QPushButton('Solo')
soloBtn.setCheckable(True)
soloBtn.toggled.connect(lambda val: self.onSolo.emit(val))
layout.addWidget(soloBtn, 0, 1)
deleteBtn = QtWidgets.QPushButton('X')
deleteBtn.clicked.connect(self.deleteLight)
deleteBtn.setMaximumWidth(10)
layout.addWidget(deleteBtn, 0, 2)
intensity = QtWidgets.QSlider(QtCore.Qt.Horizontal)
intensity.setMinimum(1)
intensity.setMaximum(1000)
intensity.setValue(self.light.intensity.get())
intensity.valueChanged.connect(lambda val: self.light.intensity.set(val))
layout.addWidget(intensity, 1, 0, 1, 2)
self.colourBtn = QtWidgets.QPushButton()
self.colourBtn.setMaximumWidth(20)
self.colourBtn.setMaximumHeight(20)
self.setButtonColour()
self.colourBtn.clicked.connect(self.setColour)
layout.addWidget(self.colourBtn, 1, 2)
def setButtonColour(self, colour=None):
if not colour:
colour = self.light.color.get()
assert len(colour) == 3, "You must provide a list of 3 colours"
r,g,b = [c*255 for c in colour]
self.colourBtn.setStyleSheet('background-color: rgba(%s, %s, %s, 1.0)' % (r,g,b))
def setColour(self):
lightColour = self.light.color.get()
colour = pm.colorEditor(rgbValue=lightColour)
r,g,b,a = [float(c) for c in colour.split()]
colour = (r,g,b)
self.light.color.set(colour)
self.setButtonColour(colour)
def disableLight(self, value):
self.name.setChecked(not value)
def deleteLight(self):
self.setParent(None)
self.setVisible(False)
self.deleteLater()
pm.delete(self.light.getTransform())