-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoggle_layer_visibility_lock.py
317 lines (222 loc) · 10.8 KB
/
toggle_layer_visibility_lock.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
# ------------------------------------------------------------------------------
# Toggle layer visibility and lock
# ------------------------------------------------------------------------------
# ctrl + shift + V - Toggles the visibility of selected layers
# alt + shift + V - Toggles the visibility of Unselected layers
# ctrl + shift + L - Toggles the lock status of selected layers
# alt + shift + L - Toggles the lock status of unselected layers
#
# 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 menue items in Layers
# The default shortcut can be changed from Edit > shortcuts
#
# @uthor sreenivas alapati (cg-cnu)
# ------------------------------------------------------------------------------
import mari
# ------------------------------------------------------------------------------
def findParentLayerStack(layer,layer_stack):
"Returns the direct parent layer stack of the layer starting the search from the layer_stack"
for search_layer in layer_stack.layerList():
if search_layer==layer:
return layer_stack
if search_layer.isGroupLayer():
result = findParentLayerStack(layer,search_layer.groupStack())
if result is not None:
return result
elif search_layer.hasMaskStack():
result = findParentLayerStack(layer,search_layer.maskStack())
if result is not None:
return result
return None
# ------------------------------------------------------------------------------
def returnTrue(layer):
"""Returns True for any object passed to it."""
return True
# ------------------------------------------------------------------------------
def getLayerList(layer_list, criterionFn):
"""Returns a list of all of the layers in the stack that match the given criterion function, including substacks."""
matching_layer = []
for layer in layer_list:
if criterionFn(layer):
matching_layer.append(layer)
if hasattr(layer, 'layerStack'):
matching_layer.extend(getLayerList(layer.layerStack().layerList(), criterionFn))
if layer.hasMaskStack():
matching_layer.extend(getLayerList(layer.maskStack().layerList(), criterionFn))
if hasattr(layer, 'hasAdjustmentStack') and layer.hasAdjustmentStack():
matching_layer.extend(getLayerList(layer.adjustmentStack().layerList(), criterionFn))
return matching_layer
# ------------------------------------------------------------------------------
def findLayerSelection():
"""Searches for the current selection if mari.current.layer is not the same as layer.isSelected and returns a full list of layers of parent stacks"""
curGeo = mari.geo.current()
curChannel = curGeo.currentChannel()
channels = curGeo.channelList()
curLayer = mari.current.layer()
layers = ()
layerList = ()
selectionList = []
layerSelect = False
parentLayers = []
# If the current layer is selected I am assuming that the user has the channel selected as well
if curLayer.isSelected():
layerList = curChannel.layerList()
layers = getLayerList(layerList,returnTrue)
# scan layers in channels for selection and append to selectionList
for layer in layers:
if layer.isSelected():
selectionList.append(layer)
layerSelect = True
# If the current layer is not selected go hunting for the selection in all channels
else:
for channel in channels:
layerList = channel.layerList()
layers = getLayerList(layerList,returnTrue)
# scan layers in channels for selection and append to selectionList
for layer in layers:
if layer.isSelected():
selectionList.append(layer)
curChannel = channel
layerSelect = True
# if nothing found:
if not layerSelect:
mari.utils.message('No Layer Selection found. \n \n Please select at least one Layer.')
return
# For each selected layer find the parent layerStack. For example for layers in groups this means the group
for layer in selectionList:
parents = findParentLayerStack(layer,curChannel)
# for each parent LayerStack generate a full list of its layers and append one by one to parentLayers variable
layer_list = parents.layerList()
for layer in layer_list:
parentLayers.append(layer)
return parentLayers
# ------------------------------------------------------------------------------
def getLayersInGroup(group):
''' given a group will return all the layers in the group '''
groupStack = group.layerStack()
layerList = groupStack.layerList()
return layerList
def layerData():
''' Updates the global Variables of all the layers, selected Layers,
unselected layers and groups, selected groups and unselected groups '''
global layers, selLayers, unSelLayers
global groups, selGroups, unSelGroups
if not mari.projects.current():
mari.utils.message('No project currently open')
return -1
layerList = findLayerSelection()
layers = [ layer for layer in layerList if not layer.isGroupLayer() ]
selLayers = [ layer for layer in layers if layer.isSelected() ]
unSelLayers = [ layer for layer in layers if not layer.isSelected() ]
groups = [ layer for layer in layerList if layer.isGroupLayer() ]
selGroups = [ group for group in groups if group.isSelected() ]
unSelGroups = [ group for group in groups if not group.isSelected() ]
return
def toggleSelVisibility():
''' Toggles the visibility of the selected layers '''
if layerData() != -1:
mari.history.startMacro('Toggle Selected Layer Visibility')
mari.app.setWaitCursor()
# Turning off viewport for better perfromance
deactivateViewportToggle = mari.actions.find('/Mari/Canvas/Toggle Shader Compiling')
deactivateViewportToggle.trigger()
for layer in selLayers:
layer.setVisibility(not layer.isVisible())
for group in selGroups:
group.setVisibility(not group.isVisible())
mari.app.restoreCursor()
mari.history.stopMacro()
deactivateViewportToggle.trigger()
return
def toggleUnselVisibility():
''' Toggles the visibility of the un selected layers '''
if layerData() != -1:
mari.history.startMacro('Toggle Unselected Layer Visibility')
mari.app.setWaitCursor()
# Turning off viewport for better perfromance
deactivateViewportToggle = mari.actions.find('/Mari/Canvas/Toggle Shader Compiling')
deactivateViewportToggle.trigger()
for layer in unSelLayers:
layer.setVisibility(not layer.isVisible())
for group in unSelGroups:
group.setVisibility(not group.isVisible())
mari.app.restoreCursor()
mari.history.stopMacro()
deactivateViewportToggle.trigger()
return
def toggleSelLock():
''' Toggles the lock status of the selected layers '''
if layerData() != -1:
mari.history.startMacro('Toggle Selected Layer Lock')
mari.app.setWaitCursor()
# Turning off viewport for better perfromance
deactivateViewportToggle = mari.actions.find('/Mari/Canvas/Toggle Shader Compiling')
deactivateViewportToggle.trigger()
for layer in selLayers:
layer.setLocked(not layer.isLocked())
for group in selGroups:
group.setLocked(not group.isLocked())
mari.app.restoreCursor()
mari.history.stopMacro()
deactivateViewportToggle.trigger()
return
def toggleUnselLock():
''' Toggles the lock status of the Unselected layers '''
if layerData() != -1:
mari.history.startMacro('Toggle Unselected Layer Lock')
mari.app.setWaitCursor()
# Turning off viewport for better perfromance
deactivateViewportToggle = mari.actions.find('/Mari/Canvas/Toggle Shader Compiling')
deactivateViewportToggle.trigger()
for layer in unSelLayers:
layer.setLocked(not layer.isLocked())
for group in unSelGroups:
group.setLocked(not group.isLocked())
mari.app.restoreCursor()
mari.history.stopMacro()
deactivateViewportToggle.trigger()
return
# ----------------------UI INTEGRATION-------------------------------#
### Toggle Layer Visbility ###
UI_path = 'MainWindow/&Layers/' + u'Visibility + Lock'
script_menu_path = 'MainWindow/Scripts/Layers/' + u'Visibility + Lock'
toggleSelVisibilityItem = mari.actions.create('Toggle Selected Visibility', 'toggleSelVisibility()')
mari.menus.addAction(toggleSelVisibilityItem, UI_path, 'Remove Layers')
mari.menus.addAction(toggleSelVisibilityItem, script_menu_path)
icon_filename = 'ToggleVisibility.png'
icon_path = mari.resources.path(mari.resources.ICONS) + '/' + icon_filename
toggleSelVisibilityItem.setIconPath(icon_path)
toggleSelVisibilityItem.setShortcut('Ctrl+Shift+V')
toggleUnselVisibilityItem = mari.actions.create('Toggle Unselected Visibility', 'toggleUnselVisibility()')
mari.menus.addAction(toggleUnselVisibilityItem, UI_path)
mari.menus.addAction(toggleUnselVisibilityItem, script_menu_path)
icon_filename = 'ToggleVisibility.png'
icon_path = mari.resources.path(mari.resources.ICONS) + '/' + icon_filename
toggleUnselVisibilityItem.setIconPath(icon_path)
toggleUnselVisibilityItem.setShortcut('Alt+Shift+V')
# --------------------------------------------------------------------
### Toggle Layer Lock ###
UI_path = 'MainWindow/&Layers/' + u'Visibility + Lock'
script_menu_path = 'MainWindow/Scripts/Layers/Visibility + Lock'
toggleSelLockItem = mari.actions.create('Toggle Selected Lock', 'toggleSelLock()')
mari.menus.addAction(toggleSelLockItem, UI_path)
mari.menus.addAction(toggleSelLockItem, script_menu_path)
icon_filename = 'Lock.png'
icon_path = mari.resources.path(mari.resources.ICONS) + '/' + icon_filename
toggleSelLockItem.setIconPath(icon_path)
toggleSelLockItem.setShortcut('Ctrl+Shift+L')
toggleUnselLockItem = mari.actions.create('Toggle Unselected Lock', 'toggleUnselLock()')
mari.menus.addAction(toggleUnselLockItem, UI_path)
mari.menus.addAction(toggleUnselLockItem, script_menu_path)
icon_filename = 'Lock.png'
icon_path = mari.resources.path(mari.resources.ICONS) + '/' + icon_filename
toggleUnselLockItem.setIconPath(icon_path)
toggleUnselLockItem.setShortcut('Alt+Shift+L')
# --------------------------------------------------------------------
### Lock/Visibility Separator Main Interface ###
mari.menus.addSeparator(UI_path,'Toggle Selected Lock')
mari.menus.addSeparator('MainWindow/&Layers/','Remove Layers')