-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulti_layer_patch_copy.py
308 lines (232 loc) · 7.85 KB
/
multi_layer_patch_copy.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
# ------------------------------------------------------------------------------
# Multi Layer-Patch Copy (WIP)
# ------------------------------------------------------------------------------
# Define Udim mapping
# Specify the source patch from which paint data is copied from - to the target
# patch to wich data is copied.
#
# eg: source patch : target patch / source patch : target patch
#
# eg: 1:2 / 3 : 4 (read - Udim 1001 is copied to Udim 1002 and
# Udim 1003 is copied to udim 1004)
#
# eg: 1:4-8 / 10:15,21 (read - Udim 1001 copied to udim 1004,1005...1008 and
# Udim 1010 is copied to Udim 1015 and 1021)
#
# The script works recursively and takes care of...
# all the selected paintable layers.
# all the layers inside the selected groups.
# all adjustment and other type of layers in the adjustment stacks.
# all the masks and mask staks on the all types of layers.
#
# The udim mapping entered will be saved to a txt file in the log folder.
# You can recover it using 'recover Udim Mapping' button.
#
# copy the script to the same location as your log folder in
# windows: C:\Users\[user_name]\Documents\Mari\Scripts
# linux: /home/[user_name]/Mari/Scripts
# Mac: /home/[Username]/Mari/Scripts
#
# Creates a menu item in Patches > Multi Layer-Patch Copy
#
# @uthor sreenivas alapati (cg-cnu)
# ------------------------------------------------------------------------------
## fix recover..
import mari
import os
import PythonQt
GUI = PythonQt.QtGui
def getPath():
''' get the respective path '''
if mari.app.version().isWindows():
#user = os.popen('whoami').read().split('\\')[-1].rstrip("\n")
#path = "C:/User/" + user + "/Documents/Mari/Logs/UDIMmappings.txt"
pluginPath = str (mari.resources.path("MARI_USER_PATH")).replace("\\", "/")
path = pluginPath + "/Logs/UDIMmappings.txt"
else:
user = os.popen('whoami').read().split()[0]
path = str('/home/' + user + '/Mari/Logs/UDIMmappings.txt')
return path
def getUdimMap(data):
''' Updates the global variables sourcePatches and targetPatches
from the udim mapping data user provides '''
global sourcePatches, targetPatches
sourcePatches = []
targetPatches = []
data = data.replace("\n", "")
data = data.replace(' ', '')
data2 = data.split('/')
for i in data2:
data3 = i.split(':')
sourcePatches.append(int (data3[0]) - 1)
tmp_values = []
data4 = data3[1].split(",")
for j in data4:
if "-" not in j:
tmp_values.append(int(j) - 1)
else:
k = j.split('-')
l = range(int (k[0]), int(k[1]) + 1)
for m in l:
tmp_values.append(int(m) - 1)
targetPatches.append(tmp_values)
return
def getGroupLayers(group):
''' Returns the list of layers for the given layer group '''
groupStack = group.layerStack()
layerList = groupStack.layerList()
return layerList
def copyPatches(imgSet):
''' For the given imageset will copy the data
from source patch to target patches '''
for patch in sourcePatches:
sourceImg = imgSet.image(patch, -1)
index = sourcePatches.index(patch)
target_patches = targetPatches[index]
for each in target_patches:
targetImg = imgSet.image(each, -1)
targetImg.copyFrom(sourceImg)
return
def getAllData():
''' get all the necessary layer and patch data '''
global curGeo, curChan, allLayers
global layers, grpLayers, selGroups, selLayers
global patches, selPatches
curGeo = mari.geo.current()
curChan = curGeo.currentChannel()
patches = list (curGeo.patchList() )
selPatches = [patch for patch in patches if patch.isSelected() ]
allLayers = list (curChan.layerList())
grpLayers = [layer for layer in allLayers if layer.isGroupLayer()]
if len(grpLayers) != 0:
for group in grpLayers:
layers_in_grp = list (getGroupLayers(group))
for each in layers_in_grp:
allLayers.append(each)
grpLayers += [ layer for layer in layers_in_grp if layer.isGroupLayer() ]
layers = [layer for layer in allLayers if not layer.isGroupLayer() ]
selLayers = [layer for layer in allLayers if layer.isSelected() ]
selGroups = [layer for layer in allLayers if layer.isGroupLayer() and layer.isSelected() ]
for group in selGroups:
layers_in_grp = list (getGroupLayers(group))
for each in layers_in_grp:
if each.isGroupLayer():
selGroups.append(each)
else:
selLayers.append(each)
return
def copyStacks(layers):
''' copies the image sets'''
for layer in layers:
if layer.isPaintableLayer():
imgSet = layer.imageSet()
copyPatches(imgSet)
if layer.hasMask() and not layer.hasMaskStack():
imgSet = layer.maskImageSet()
copyPatches(imgSet)
try:
if layer.hasMaskStack():
mask_stack = layer.maskStack()
mask_stack_elements = list (mask_stack.layerList())
for layer in mask_stack_elements:
layers.append(layer)
except AttributeError:
pass
try:
if layer.hasAdjustmentStack():
adjust_stack = layer.adjustmentStack()
adjust_stack_elements = adjust_stack.layerList()
for layer in mask_stack_elements:
layers.append(layer)
except AttributeError:
pass
return
def updateUdimMap(data):
''' update the udim mapping to the file in logs '''
path = getPath()
objectName = str(curGeo.name())
try:
f = open(path, 'r')
oldMappings = f.readline()
f.close()
index = None
for line in oldMappings:
if line.startswith (objectName):
index = oldMappings.index(line)
if index != None:
UdimMapFile = open(path, 'w')
oldMappings[index + 1] = str (data) + '\n'
for i in oldMappings:
udimMapFile.write(str(i))
else:
udimMapFile = open(path, 'a')
udimMapFile.write('\n' + objectName + '\n' + data)
except IOError:
udimMapFile = open(path, 'w')
udimMapFile.write('\n' + objectName + '\n' + data)
return
def recoverUdimMap():
''' recovers the udim mapping if any '''
path = getPath()
curGeo = mari.geo.current()
objectName = str(curGeo.name())
index = None
try:
with open(path, 'r') as f:
oldMappings = f.readline()
for line in oldMappings:
if line.startswith(objectName):
index = oldMappings.index(line)
oldUDIMmap = oldMappings[index+1]
field.setPlainText(oldUDIMmap)
if index == None:
mari.utils.message('no previous mappings')
except IOError:
mari.utils.message('no previous mappings')
return
def multiLayerPatchCopy():
''' copies the imgsets for the patches in the udim mapping '''
data = field.toPlainText()
if data == "":
mari.utils.message('Please enter atleast one mapping in format source: targe')
return
getUdimMap(data)
getAllData()
updateUdimMap(data)
mari.history.startMacro("Multi Layer-Patch Copy")
if channelCheck.isChecked():
copyStacks(allLayers)
else:
copyStacks(selLayers)
mari.history.stopMacro()
layerPatchDialog.close()
return
#--------------------------- ui ----------------------------
layerPatchDialog = GUI.QDialog()
layerPatchDialog.setWindowTitle("Multi Layer-Patch Copy")
vLayout = GUI.QVBoxLayout()
layerPatchDialog.setLayout(vLayout)
layerPatchDialog.setGeometry(800,200,400,280)
recoverUdimMapButton = GUI.QPushButton ('Recover UDIM Mapping')
recoverUdimMapButton.connect ('clicked()', lambda: recoverUdimMap())
recoverUdimMapButton.setToolTip('Recover the previous udim mapping')
vLayout.addWidget(recoverUdimMapButton)
vLayout.addWidget(GUI.QLabel('UDIM Mapping'))
field = GUI.QTextEdit()
vLayout.addWidget(field)
channelCheck = GUI.QCheckBox('all Layers')
vLayout.addWidget(channelCheck)
channelCheck.setToolTip('copy for all the layers in the current channel')
copyButton = GUI.QPushButton('Copy')
copyButton.connect('clicked()', lambda: multiLayerPatchCopy())
copyButton.setToolTip('copy the patches in the udim map')
vLayout.addWidget(copyButton)
### show the ui
def showLayerPatchUi():
'''display the ui'''
if mari.projects.current() is None:
mari.utils.message('no project currently open')
return
layerPatchDialog.show()
return
mari.menus.addAction(mari.actions.create('Multi Layer-Patch copy', 'showLayerPatchUi()'), 'MainWindow/Patches')