-
Notifications
You must be signed in to change notification settings - Fork 1
/
marinamer.py
322 lines (220 loc) · 8 KB
/
marinamer.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
# ------------------------------------------------------------------------------
# marinamer
# ------------------------------------------------------------------------------
# rename multiple layers and assign colors.
#
# select a bunch of layers and assign
# prefix - sepeprator - layer name - seperator - suffix - num/abc - color
#
# > The existing layer name will be used if 'layer name' is empty
# > You can rename even the layers/adjustment layers in
# the groups / adjustment stacks / mask stacks.
# > Autofill terms can be edited at /logs/texTerms.txt
#
# 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 Tools > marinamer
#
# @uthor sreenivas alapati (cg-cnu)
# ------------------------------------------------------------------------------
import mari
import PySide
import string
GUI = PySide.QtGui
class Dialog(GUI.QDialog):
def __init__(self):
super(Dialog, self).__init__()
self.alphabets = list( string.ascii_lowercase )
self.seperators = ["N", " ","_", "-", "/", "+", ".", ","]
self.sequenceList = ["NAN", "001", "abc"]
self.colors = ["color", "black", "cyan", "dark blue",
"light blue", "light green", "red",
"white", "yellow"]
self.setWindowTitle("marinamer")
self.setGeometry(680, 400, 800, 60)
mainLayout = GUI.QVBoxLayout()
self.setLayout(mainLayout)
renameLayout = GUI.QHBoxLayout()
mainLayout.addLayout(renameLayout)
self.prefix = GUI.QLineEdit()
self.prefix.setPlaceholderText("prefix")
renameLayout.addWidget(self.prefix)
self.prefixSeperator = GUI.QComboBox()
renameLayout.addWidget(self.prefixSeperator)
self.prefixSeperator.addItems(self.seperators)
self.layerName = GUI.QLineEdit()
self.layerName.setPlaceholderText("layer name")
renameLayout.addWidget(self.layerName)
self.suffixSeperator = GUI.QComboBox()
renameLayout.addWidget(self.suffixSeperator)
self.suffixSeperator.addItems(self.seperators)
self.suffix = GUI.QLineEdit()
self.suffix.setPlaceholderText("suffix")
renameLayout.addWidget(self.suffix)
self.numberSeperator = GUI.QComboBox()
renameLayout.addWidget(self.numberSeperator)
self.numberSeperator.addItems(self.seperators)
self.layerNumber = GUI.QComboBox()
renameLayout.addWidget(self.layerNumber)
self.layerNumber.addItems(self.sequenceList)
self.layerColor = GUI.QComboBox()
renameLayout.addWidget(self.layerColor)
self.layerColor.addItems(self.colors)
optionsLayout = GUI.QHBoxLayout()
mainLayout.addLayout(optionsLayout)
self.renameBtn = GUI.QPushButton("rename")
self.renameBtn.clicked.connect(lambda: self.rename())
optionsLayout.addWidget(self.renameBtn)
self.renameBtn = GUI.QPushButton("reset")
self.renameBtn.clicked.connect(lambda: self.resetUi())
optionsLayout.addWidget(self.renameBtn)
self.closeBtn = GUI.QPushButton("close")
self.closeBtn.clicked.connect(lambda: self.cancel())
optionsLayout.addWidget(self.closeBtn)
self.texTerms = []
self.loadtexTerms()
self.setTexTermQCompleter()
def resetUi(self):
''' reset the ui for the data '''
self.prefix.clear()
self.layerName.clear()
self.suffix.clear()
self.prefixSeperator.setCurrentIndex(0)
self.suffixSeperator.setCurrentIndex(0)
self.numberSeperator.setCurrentIndex(0)
self.layerNumber.setCurrentIndex(0)
self.layerColor.setCurrentIndex(0)
def getLayersInGroup(self, group):
''' given a group returns all the layers in the group '''
tmpLayersInGroup = list( group.groupStack().layerList() )
for layer in tmpLayersInGroup:
self.layerList.append(layer)
if layer.isGroupLayer():
self.getLayersInGroup(layer)
def getLayersInAdjStack(self, layer):
''' given a layer returns all the alyers inside the adjustment stack '''
layersInAdjStack = []
try:
if layer.hasAdjustmentStack():
layersInAdjStack = list ( layer.adjustmentStack().layerList() )
for layer in layersInAdjStack:
self.layerList.append(layer)
if layer.isGroupLayer():
self.getLayersInGroup(layer)
except:
pass
def getLayersInMaskStack(self, layer):
''' given a layer returns maskStack '''
layersInMaskStack = []
try:
if layer.hasMaskStack():
layersInMaskStack = list ( layer.maskStack().layerList() )
for layer in layersInMaskStack:
self.layerList.append(layer)
if layer.isGroupLayer():
self.getLayersInGroup(layer)
except:
pass
def getSelLayers(self):
''' given a layer returns all the layers including the masks and mask stacks '''
curGeo = mari.geo.current()
curChan = curGeo.currentChannel()
self.layerList = list (curChan.layerList())
groups = [ layer for layer in self.layerList if layer.isGroupLayer() ]
for group in groups:
self.getLayersInGroup(group)
self.getLayersInAdjStack(group)
self.getLayersInMaskStack(group)
for layer in self.layerList:
self.getLayersInAdjStack(layer)
self.getLayersInMaskStack(layer)
self.selLayers = [layer for layer in self.layerList if layer.isSelected() ]
def appendTexTerms(self, term):
''' update the texTerms list '''
if term not in self.texTerms:
self.texTerms.append(term)
def setTexTermQCompleter(self):
''' set the completer to the ui '''
self.texCompleter = GUI.QCompleter(self.texTerms, self)
self.texCompleter.setCompletionMode(GUI.QCompleter.PopupCompletion)
self.prefix.setCompleter(self.texCompleter)
self.suffix.setCompleter(self.texCompleter)
self.layerName.setCompleter(self.texCompleter)
def getSeperators(self, index):
''' get the seperators '''
if index == 0:
seperatorText = ""
else:
seperatorText = self.seperators[ index ]
return seperatorText
def getNewLayerName(self, index, layer):
''' get the final name to be renamed'''
prefixText = self.prefix.text()
self.appendTexTerms(prefixText)
prefixSeperatorText = self.getSeperators( self.prefixSeperator.currentIndex() )
layerNameText = self.layerName.text()
self.appendTexTerms(layerNameText)
if layerNameText == "":
layerNameText = layer.name()
suffixSeperatorText = self.getSeperators( self.suffixSeperator.currentIndex() )
suffixText = self.suffix.text()
self.appendTexTerms(suffixText)
numberSeperatorText = self.getSeperators( self.numberSeperator.currentIndex() )
if self.layerNumber.currentIndex() == 1:
sequence = str(index + 1 )
elif self.layerNumber.currentIndex() == 2:
sequence = self.alphabets[index]
else:
sequence = ""
newName = prefixText + prefixSeperatorText + layerNameText + suffixSeperatorText
newName = newName + suffixText + numberSeperatorText + sequence
self.setTexTermQCompleter()
return newName
def getColor(self):
''' get the color from the ui '''
colorIndex = self.layerColor.currentIndex()
if colorIndex == 0:
color = "none"
else:
color = self.colors[colorIndex]
return color
def rename(self):
''' main func to rename '''
self.getSelLayers()
selectedLayers = self.selLayers
mari.history.startMacro("marinamer")
for index, layer in enumerate(selectedLayers):
newName = self.getNewLayerName(index, layer)
layer.setName(newName)
color = self.getColor()
layer.setColorTag(color)
mari.history.stopMacro()
self.updateTexTerms()
def loadtexTerms(self):
''' load the list from the temp file '''
try:
file = open('texTerms.txt', 'r')
self.texTermsList = file.readlines()
self.texTerms = [ term.rstrip("\n") for term in self.texTermsList ]
except IOError:
# text terms file dosen't exist-ignore
pass
def updateTexTerms(self):
''' update the list in a temp file '''
with open('texTerms.txt', 'w') as myFile:
for term in self.texTerms:
myFile.write(term + "\n")
def cancel(self):
''' shutdown the ui '''
self.close()
def showUi(self):
''' display ui '''
if not mari.projects.current():
mari.utils.message("No project currently open")
return
self.show()
MAIN = Dialog()
mari.menus.addAction(mari.actions.create('marinamer', 'MAIN.showUi()'), "MainWindow/Tools")