/
lcmtv311_stable.py
337 lines (275 loc) · 12.2 KB
/
lcmtv311_stable.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
import maya.cmds as cmds
import re
import maya.mel as mel
import os
class LCMT:
"""Light Contribution Management Tool
Version: 3.1.1
Author: Nestor Prado
more@nestorprado.com
SCAD Spring 2012"""
lightDB = ''
path = ''
fullPath = ''
saveImages = False
def __init__(self):
#Create new file for database if there isn't one
self.path = cmds.workspace(q=True, rd=True)+'scripts/'
self.fullPath = self.path + 'LCMT_lightTypesDB.txt'
self.saveImages = False
#if file doesn't exist
if not os.path.exists(self.fullPath):
#check to see if there is a scripts directory in the project space
if not os.path.exists(self.path):
os.makedirs(self.path)
f = open(self.fullPath, 'w')
f.write('key|bounce|rim|background|wall|kick')
f.close()
self.lightDB = 'key|bounce|rim|background|wall|kick'
else:
#if the file exists initialize write the lightDB parameter
f = open(self.fullPath, 'r')
content = f.read()
self.lightDB = content
f.close()
def addLightTypesToFile(self, newContents=''):
if os.path.exists(self.fullPath):
f = open(self.fullPath, 'a')
f.write('|'+newContents)
f.close()
f = open(self.fullPath, 'r')
self.lightDB = f.read()
f.close()
else:
print 'ERROR Writing file: ', self.fullPath, 'it doesn\'t EXIST!!'
return -1
def extractLightName(self, name):
lightTypes = self.lightDB
to_find = re.compile(lightTypes,re.IGNORECASE)
to_search = name
match = re.search(to_find, to_search)
if match!=None:
return match.group().lower()
else: return name
def groupLightsByName(self, lights):
lightGroups = dict()
for light in lights:
lightNameType = self.extractLightName(light)
if lightNameType not in lightGroups.keys():
lightGroups[lightNameType] = [light]
else:
lightGroups[lightNameType].append(light)
return lightGroups
def createLayersFromLights(self, selectedGeometry=[], lightsSelected=[]):
#select all the lights in the scene includin IBL nodes
lights = cmds.ls(type=['light','mentalrayIblShape'])
if selectedGeometry !=None and selectedGeometry !=[]:
geometry = selectedGeometry
else:
#UPDATE:
geometry = cmds.ls(dag=True,geometry=True, selection=True)
if geometry == []:
#select all the geometry in the scene
geometry = cmds.ls(geometry=True)
#take out the ibl shapes from the geo selection
geometry = list(set(geometry) - set(lights))
if lightsSelected !=None and lightsSelected !=[]:
selectedLights = lightsSelected
else:
#see if there is any number of lights the the artist has selected
selectedLights = cmds.ls(dag=True, selection=True, type=['light','mentalrayIblShape'])
#if there isn't any lights selected just create one layer for each light
if selectedLights == []:
lightGroups = self.groupLightsByName(lights)
for group in lightGroups:
test = lightGroups[group]
lightTrans = cmds.listRelatives(test, p=1)
layerElements = geometry + lightGroups[group]
layerName = self.extractLightName(lightGroups[group][-1])
cmds.createRenderLayer(layerElements, name=layerName)
else:
#if we have a certain number of lights selected create a layer with all of those lights attached
lightTrans = cmds.listRelatives(selectedLights, p=1)
layerElements = geometry + lightTrans
layerName = self.extractLightName(lightTrans[-1])
cmds.createRenderLayer(layerElements, name=layerName)
def sortLightsByType(self, lights):
lightTypes = []
type=1
for light in lights:
type=1
if cmds.objectType( light, isType='mentalrayIblShape' ) == True:
type = 2
lightTypes.append((light,type))
lightTypes = sorted(lightTypes, key=lambda light: light[-1])
lights = []
for light in lightTypes:
lights.append(light[0])
return lights
def saveCurrentImageInRenderView(self, filename):
path = cmds.file(query=True,sceneName=True)
sceneName = os.path.split(path)[1].rsplit('.')[0]
projectSpace = cmds.workspace(q=True, rd=True)
imagesFolder = projectSpace + 'images/tmp/'
editor = 'renderView'
print 'Saving file', imagesFolder+sceneName+'_'+filename
cmds.renderWindowEditor(editor, e=True, writeImage=imagesFolder+sceneName+'_'+filename)
def isLightHidden(self, light):
lightTrans = cmds.listRelatives(light, p=1)[0]
return cmds.getAttr('%s.visibility' % lightTrans) == False or cmds.getAttr('%s.visibility' % light) == False
def renderOnlyThisLight(self, lights):
if type(lights)!=list:
lights = [lights]
lightNames =''
for light in lights:
lightNames += '_'+cmds.listRelatives(light, p=1)[0]
wasHidden = False
if self.isLightHidden(light) :
cmds.showHidden(light)
wasHidden = True
#-Revise!--if it is an ibl light then turn off emit Final Gather
if cmds.objectType( light, isType='mentalrayIblShape' ) == True:
if cmds.getAttr('%s.visibleInFinalGather' % light) == False:
cmds.setAttr('%s.visibleInFinalGather' % light, 1)
wasHidden = True
mel.eval("renderIntoNewWindow render")
rv = cmds.getPanel(scriptType='renderWindowPanel')
caption = cmds.renderWindowEditor(rv, query=True, pca=True)
newCaption = caption+' contriburion of '+lightNames.replace('_',' ')
cmds.renderWindowEditor(rv, edit=True, pca= newCaption)
# save the frame in mel
mel.eval("renderWindowMenuCommand keepImageInRenderView renderView;")
if self.saveImages:
self.saveCurrentImageInRenderView('contributionOf'+lightNames)
for light in lights:
if wasHidden:
cmds.hide(light)
#-Revise!-- if it was the ibl node disable final gather again
if cmds.objectType( light, isType='mentalrayIblShape' ) == True:
cmds.setAttr('%s.visibleInFinalGather' % light, 0)
def renderAllLights(self, renderLights=[],useGroups=False):
lights = cmds.ls(dag=True,visible=True,lights=True, type='mentalrayIblShape')
#Check if there is any lights selected to only do those
if renderLights == [] or renderLights == None:
renderLights = cmds.ls( dag=True, sl=True , lights=True, type='mentalrayIblShape')
#if there isn't any light selected just get all the lights
if renderLights == []:
renderLights = lights
lightNames = ""
for light in renderLights:
lightNames = lightNames + light + '\n'
windowName = 'ProgressWindow'
if cmds.window(windowName, exists=True):
cmds.deleteUI(windowName)
window = cmds.window(windowName,t="Progress Report")
cmds.columnLayout()
cmds.iconTextStaticLabel( st='textOnly', l='Rendering Lights:' )
cmds.iconTextStaticLabel( st='textOnly', l=lightNames )
cmds.iconTextStaticLabel( st='textOnly', l='Process Bar' )
progressControl = cmds.progressBar(maxValue=len(renderLights), width=300)
cmds.showWindow( window )
lights = self.sortLightsByType(lights)
#-Revised--hide ibl node that is at the end of lights list (sorted previously)
if cmds.objectType( lights[-1], isType='mentalrayIblShape' ) == True:
cmds.setAttr('%s.visibleInFinalGather' % lights[-1], 0)
cmds.setAttr('%s.visibleInEnvironment' % lights[-1], 0)
cmds.hide(lights)
lightCount = 0
if useGroups==True:
renderLightsGroups = self.groupLightsByName(renderLights)
cmds.progressBar(progressControl,edit=True, maxValue=len(renderLightsGroups.keys()), width=300)
for group in renderLightsGroups:
self.renderOnlyThisLight(renderLightsGroups[group])
progressInc = cmds.progressBar(progressControl, edit=True, pr=lightCount+1)
lightCount+=1
else:
print renderLights
for light in renderLights:
self.renderOnlyThisLight(light)
progressInc = cmds.progressBar(progressControl, edit=True, pr=lightCount+1)
lightCount+=1
cmds.showHidden(lights)
#-Revised--since we sorted the lights by type we know that the lastone will be the IBL
if cmds.objectType( lights[-1], isType='mentalrayIblShape' ) == True:
cmds.setAttr('%s.visibleInFinalGather' % lights[-1], 1)
cmds.setAttr('%s.visibleInEnvironment' % lights[-1], 1)
def updateScollList(self, mode, listName):
lights = cmds.ls(dag =True,visible=True,lights=True, type='mentalrayIblShape')
lightGroups = self.groupLightsByName(lights)
if mode:
cmds.iconTextScrollList(listName,edit=True, ra=True)
cmds.iconTextScrollList(listName,edit=True, allowMultiSelection=True, append=lightGroups.keys())
else:
cmds.iconTextScrollList(listName,edit=True, ra=True)
cmds.iconTextScrollList(listName,edit=True, allowMultiSelection=True, append=lights)
def getElementsFromLightScrollList(self, listName, useGroups):
lights = cmds.ls(dag =True,visible=True,lights=True, type='mentalrayIblShape')
lightGroups = self.groupLightsByName(lights)
selectedGroups = cmds.iconTextScrollList(listName, query=True, si=True )
if selectedGroups == None:
return None
if cmds.checkBox(useGroups, query=True, value=True):
returnLights = []
for group in selectedGroups:
returnLights.extend(lightGroups[group])
return returnLights
else:
return selectedGroups
def addLightTypes(self):
result = cmds.promptDialog(
title='New Light Types',
message='Add New Light Types (Separated by comas):',
button=['OK', 'Cancel'],
defaultButton='OK',
cancelButton='Cancel',
dismissString='Cancel')
if result == 'OK':
text = cmds.promptDialog(query=True, text=True)
text = text.replace(',','|')
self.addLightTypesToFile(text)
def displayLightTypes(self):
oldLightTypes = self.lightDB.replace('|',',')
cmds.confirmDialog(message='The Light Types in the DB are:\n'+oldLightTypes)
def resetLightTypesToDefault(self):
result = cmds.confirmDialog( title='Confirm', message='Are you sure?', button=['Yes','No'], defaultButton='Yes', cancelButton='No', dismissString='No' )
if result == 'Yes':
f = open(self.fullPath, 'w')
f.write('key|bounce|rim|background|wall|kick')
f.close()
self.lightDB='key|bounce|rim|background|wall|kick'
def toggleSaveImages(self):
self.saveImages = not(self.saveImages)
def displayUI(self):
windowName = 'LCMTUIWindow'
if cmds.window(windowName, exists=True):
cmds.deleteUI(windowName)
window = cmds.window(windowName, menuBar = True,t="LCMT v3.1.1")
fileMenu = cmds.menu( label='Manage Light Types')
cmds.menuItem( label='Add More Light Types',command=lambda *args:self.addLightTypes())
cmds.menuItem( label='See Current Light Types', command=lambda *args:self.displayLightTypes())
cmds.menuItem( label='Reset Light Types to Default Values', command=lambda *args:self.resetLightTypesToDefault())
cmds.paneLayout( configuration='vertical2' )
lightStageColumn = cmds.columnLayout(adjustableColumn=True)
cmds.text('Lights in the SCENE')
lights = cmds.ls(dag =True,visible=True,lights=True, type='mentalrayIblShape')
lightList = cmds.iconTextScrollList(allowMultiSelection=True, append=lights)
cmds.rowLayout(numberOfColumns = 2)
useGroupLights = cmds.checkBox( label='Group Lights', onCommand = lambda *args: self.updateScollList(True, lightList), offCommand = lambda *args: self.updateScollList(False, lightList))
cmds.checkBox( label='Save Images?', cc = lambda *args: self.toggleSaveImages())
cmds.setParent('..')
cmds.iconTextScrollList(lightList,edit=True,selectCommand=lambda *args: cmds.select(self.getElementsFromLightScrollList(lightList,useGroupLights),vis=True))
cmds.button(label='Render Lights!', command = lambda *args: self.renderAllLights(self.getElementsFromLightScrollList(lightList,useGroupLights),cmds.checkBox(useGroupLights, query=True, value=True)))
cmds.setParent('..')
renderLayersColumn = cmds.columnLayout(adjustableColumn=True)
#new column
cmds.text('Geometry in the SCENE')
geometry = cmds.ls(geometry=True)
#take out the ibl shapes from the geo selection
geometry = list(set(geometry) - set(lights))
geoList = cmds.iconTextScrollList(allowMultiSelection=True, append=geometry,selectCommand=lambda *args: cmds.select(cmds.iconTextScrollList(geoList, query=True, si=True )) )
cmds.text('Create Render Layers from selected geometry and lights')
cmds.button(label='Create Render Layers!', command = lambda *args: self.createLayersFromLights(cmds.iconTextScrollList(geoList, query=True, si=True ),self.getElementsFromLightScrollList(lightList,useGroupLights)))
cmds.setParent('..')
cmds.showWindow()
LCMT = LCMT()
LCMT.displayUI()