-
Notifications
You must be signed in to change notification settings - Fork 0
/
materialSwitch.py
285 lines (237 loc) · 11.4 KB
/
materialSwitch.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
# Colorize Tool by Timothy Halim
# 15 June 2021
#
# This tool will duplicate the material, remove the textures and replace it with constant,
# and store the original connection data in a json file that can be used to restore it later
#
# Install:
# - Copy and paste content to maya script editor, and run it from there
# - Or use type "execfile('[path to this file]')" in script editor and run it
#
# Usage:
# - Get Selected Objects : This button will search all of the material used for selected objects
# and store the connections in a json file,
# if nothing is selected then it will search all mesh in scene
# - Check Descendants : Enabling this, will search all of material used for all of selected object descendants
# - Colorize : Will change object material to flat material from the data stored in json file
# - Restore : Will restore object material to original material from the data stored in json file
# - Delete Flat Shader : Will restore object material and delete stored flat shaders
import os
import json
import traceback
from PySide2.QtWidgets import QApplication, QDialog, QVBoxLayout, QPushButton, QCheckBox, QListWidget
import pymel.core as pm
def undoOn(function):
def funcCall(*args,**kwargs):
result = None
try:
name = function.__name__
pm.undoInfo(openChunk=True, chunkName=name)
result = function( *args,**kwargs )
except Exception as e:
print(traceback.format_exc())
pm.displayError( "Error : %s" %e )
finally:
pm.undoInfo(closeChunk=True)
return result
return funcCall
class ColorizeTool(QDialog):
def __init__(self):
super(ColorizeTool, self).__init__(self.getAppWindow())
self.closeExistingDialog()
self.setWindowTitle("Colorize Tool")
self.setObjectName(self.__class__.__name__)
self.buildUI()
self.connectSignal()
self.initUI()
def getAppWindow(self):
for w in QApplication.topLevelWidgets():
if w.objectName() == 'MayaWindow':
return w
def closeExistingDialog(self):
childWidgets = self.parent().findChildren(QDialog)
for w in childWidgets:
try:
if w.objectName() == self.__class__.__name__:
if w.isVisible():
w.close()
except:
pass
def buildUI(self):
self.masterLayout = QVBoxLayout(self)
self.getObjectButton = QPushButton("Get Selected Objects")
self.checkHierarchy = QCheckBox("Check Descendants")
self.objectListWidget = QListWidget()
self.colorizeButton = QPushButton("Colorize")
self.restoreButton = QPushButton("Restore")
self.deleteButton = QPushButton("Delete Json")
for w in (self.getObjectButton, self.checkHierarchy, self.objectListWidget,
self.colorizeButton, self.restoreButton, self.deleteButton):
self.masterLayout.addWidget(w)
def connectSignal(self):
self.getObjectButton.clicked.connect(self.getSelectedMesh)
self.colorizeButton.clicked.connect(self.colorize)
self.restoreButton.clicked.connect(self.restore)
self.deleteButton.clicked.connect(self.delete)
def initUI(self):
self.checkHierarchy.setChecked(True)
self.checkJson()
if os.path.isfile(self.getJsonFile()):
with open(self.getJsonFile(), "r+") as f:
self.shaderData = json.load(f)
self.meshes = []
for k, v in self.shaderData.items():
self.meshes += pm.ls(v['objects'])
self.meshes = sorted(set(self.meshes))
self.objectListWidget.clear()
self.objectListWidget.addItems([m.name() for m in self.meshes])
def getJsonFile(self):
currentFile = pm.system.sceneName()
currentDir = os.path.dirname(currentFile)
outputName = '.'.join(os.path.basename(currentFile).split('.')[:-1]) + ".json"
outputFile = os.path.join(currentDir,outputName).replace("\\", "/")
return outputFile
def checkJson(self):
jsonExist = os.path.isfile(self.getJsonFile())
self.colorizeButton.setEnabled(jsonExist)
self.restoreButton.setEnabled(jsonExist)
self.deleteButton.setEnabled(jsonExist)
def getSelectedMesh(self):
self.objectListWidget.clear()
selection = pm.ls(sl=True)
if selection:
self.meshes = []
for o in selection:
shape = o.getShape() if hasattr(o, 'getShape') else None
if shape and shape.nodeType() == 'mesh':
self.meshes.append(o)
if self.checkHierarchy.isChecked():
childs = pm.listRelatives(o, ad=True)
for child in childs:
shape = child.getShape() if hasattr(child, 'getShape') else child
if shape and shape.nodeType() == 'mesh':
self.meshes.append(child.getTransform())
else:
self.meshes = [shape.getTransform() for shape in pm.ls(type='mesh')]
self.meshes = sorted(set(self.meshes), key = lambda k : k.name())
self.objectListWidget.addItems([m.name() for m in self.meshes])
self.processData()
@undoOn
def processData(self):
self.shaderData = {}
shadingGroup = []
for obj in self.meshes:
shadingEngines = pm.listConnections(obj.getShape(), type='shadingEngine')
if not shadingEngines:
continue
objShadingGroup = shadingEngines[0]
if objShadingGroup not in shadingGroup:
shadingGroup.append(objShadingGroup)
currentData = self.shaderData.get(objShadingGroup.name(), {'objects':[], 'flatShader':None})
currentData['objects'] += [obj.name()]
self.shaderData[objShadingGroup.name()] = currentData
for sg in shadingGroup:
currentData = self.shaderData[sg.name()]
if currentData['flatShader'] is None:
shaderExist = pm.ls(sg.name()+"_flat")
if shaderExist:
pm.delete(pm.listHistory(shaderExist[0]))
currentData['flatShader'] = pm.duplicate(sg, upstreamNodes=True, name=sg.name()+"_flat", rr=True)[0].name()
sgConnections = pm.listHistory(currentData['flatShader'])
sgTextures = pm.ls(sgConnections, textures=True)
sgShaders = pm.ls(sgConnections, materials=True)
for texture in sgTextures:
connections = pm.listConnections(texture, connections=True, destination=True, plugs=True)
for connection in connections:
source = connection[0]
destination = connection[1]
if destination.node() in sgShaders:
rgba = [0,0,0,0]
maxSample = 10
for u in range(maxSample):
for v in range(maxSample):
x = float(u)/maxSample
y = float(v)/maxSample
total = maxSample*maxSample
r,g,b,a = pm.colorAtPoint(source, o='RGBA', u=x, v=y)
rgba[0] += r/total
rgba[1] += g/total
rgba[2] += b/total
rgba[3] += a/total
# pump up saturation
saturation = .8
iMax = rgba.index(max(rgba[0:3]))
iMin = rgba.index(min(rgba[0:3]))
if iMax != iMin:
iMid = next((i for i in range(3) if i != iMax and i != iMin), 0)
ratio = (rgba[iMax] - rgba[iMin])/rgba[iMax]
rgba[iMax] = rgba[iMax]
rgba[iMin] = rgba[iMin] * (1-saturation)
newRatio = (rgba[iMax] - rgba[iMin])/rgba[iMax]
rgba[iMid] = rgba[iMid] * ((1-saturation)-(ratio-newRatio))
pm.disconnectAttr(source, destination=destination)
pm.setAttr(destination, rgba[0], rgba[1], rgba[2], type='double3')
pm.delete(pm.listHistory(source.node()))
jsonOutput = self.getJsonFile()
with open(jsonOutput, "w+") as f:
f.write(json.dumps(self.shaderData, indent=4))
self.checkJson()
@undoOn
def colorize(self):
jsonOutput = self.getJsonFile()
if os.path.isfile(jsonOutput):
with open(jsonOutput, "r+") as f:
self.shaderData = json.load(f)
for k, v in self.shaderData.items():
originalShader = pm.ls(k)
if originalShader:
originalShader = originalShader[0]
flatShader = pm.ls(v['flatShader'])
if flatShader:
flatShader = flatShader[0]
if flatShader and originalShader:
objects = pm.ls(v['objects'])
pm.sets(originalShader, e=True, remove=objects)
pm.sets(flatShader, e=True, forceElement=objects)
else:
raise Exception(jsonOutput +" is missing")
@undoOn
def restore(self):
jsonOutput = self.getJsonFile()
if os.path.isfile(jsonOutput):
with open(jsonOutput, "r+") as f:
self.shaderData = json.load(f)
for k, v in self.shaderData.items():
originalShader = pm.ls(k)
if originalShader:
originalShader = originalShader[0]
flatShader = pm.ls(v['flatShader'])
if flatShader:
flatShader = flatShader[0]
if flatShader and originalShader:
objects = pm.ls(v['objects'])
pm.sets(flatShader, e=True, remove=objects)
pm.sets(originalShader, e=True, forceElement=objects)
else:
raise Exception(jsonOutput +" is missing")
@undoOn
def delete(self):
try:
self.restore()
except:
pass
jsonOutput = self.getJsonFile()
if os.path.isfile(jsonOutput):
with open(jsonOutput, "r+") as f:
self.shaderData = json.load(f)
for k, v in self.shaderData.items():
flatShader = pm.ls(v['flatShader'])
if flatShader:
flatShader = flatShader[0]
pm.delete(pm.listHistory(flatShader))
os.remove(jsonOutput)
self.checkJson()
else:
raise Exception(jsonOutput +" is missing")
w = ColorizeTool()
w.show()