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
| from Qt import QtWidgets,QtCore,QtGui import pymel.core as pm from functools import partial import Qt import logging from maya import OpenMayaUI as omui
logging.basicConfig() logger = logging.getLogger('LightingManager') logger.setLevel(logging.DEBUG)
if Qt.__binding__ == 'PySide': logger.debug('Using Pyside with shiboken') from shiboken import warpInstance from Qt.QtCore import Signal elif Qt.__binding__.startswith('PyQt'): logger.debug('Using PyQt with sip') from sip import warpInstance as warpInstance from Qt.QtCore import pyqtSignal as Signal else: logger.debug('Using PySide2 with shiboken') import shiboken2 from Qt.QtCore import Signal
def getMayaMainWindow(): win = omui.MQtUtil_mainWindow() ptr = shiboken2.wrapInstance(int(win),QtWidgets.QMainWindow) return ptr
def getDock(name = 'LightingManagerDock'): deleteDock(name) ctrl = pm.workspaceControl(name, dockToMainWindow = ('right',1), label = "LightingManager") qtCtrl = omui.MQtUtil_findControl(ctrl) ptr = shiboken2.wrapInstance(int(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 LightManagerUI exists") parent = QtWidgets.QDialog(parent = getMayaMainWindow()) parent.setObjectName('lightingManager') parent.setWindowTitle('Lighting Manager') layout = QtWidgets.QVBoxLayout(parent) super().__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) createBtn = QtWidgets.QPushButton('Create') createBtn.clicked.connect(self.createLight) layout.addWidget(createBtn, 0,1) 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,2) refreshBtn = QtWidgets.QPushButton('Refresh') refreshBtn.clicked.connect(self.populate) layout.addWidget(refreshBtn, 2,1) def createLight(self): lightType = self.lightTypeCB.currentText() func = self.lightTypes[lightType] light = func() self.addLight(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 = Signal(bool) def __init__(self, light): super().__init__() if isinstance(light, str): light = pm.PyNode(light) 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.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) def setButtonColor(self,color = None): if not color: color = self.light.color.get() assert len(color) == 3, "You must provide a list of 3 Color" r,g,b = [c*255 for c in color] self.colorBtn.setStyleSheet('background-color: rgba({},{},{},1.0)'.format(r,g,b)) def setColor(self): lightColor = self.light.color.get() color = pm.colorEditor(rgbValue = lightColor) r,g,b,a = [float(c) for c in color.split()] color = (r,g,b) self.light.color.set(color) self.setButtonColor(color) 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()) def showUI(): ui = LightManager() ui.show() return ui
|