-
Notifications
You must be signed in to change notification settings - Fork 0
/
BakeSelectedLayer.jsx
151 lines (109 loc) · 3.28 KB
/
BakeSelectedLayer.jsx
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
<javascriptresource>
<name>Bake Selected Layer</name>
<about>
This will bake the selected layer onto all the layers below.
This is best used for adjustment layers, but it can be a
layer of any type.
Evan Viera
</about>
<menu>filter</menu>
<category>Viera</category>
<type>automate</type>
<enableinfo>true</enableinfo>
</javascriptresource>
/*
-------------------------------------------------------------
REVISIONS
When an adjustment layer is baked that is also
a clipping layer, it will break the clipping
heierachy.
Solution: Iterate through all layers, collect
their relevant information and set it back
once done.
-------------------------------------------------------------
*/
#include "vieraLibrary.jsx"
var doc = activeDocument;
var selectedLayer = doc.activeLayer;
var layerParent = selectedLayer.parent;
var collectedLayers = [ ];
var layerIndex = getIndex( selectedLayer );
if ( selectedLayer.grouped == true ) collectClippingLayers( );
else collectLayersBelow( layerIndex, layerParent );
proofCopy( );
copyAndMergeSelectedLayer( );
selectedLayer.remove( );
doc.activeLayer = doc.layers[ 0 ];
proofPaste( );
/*
-------------------------------------------------------------
Collects all layers below into array
-------------------------------------------------------------
*/
function collectLayersBelow( __index, __parent )
{
for ( var i = __index; i < __parent.layers.length; i++ ) {
var currentLayer = __parent.layers[ i ];
var isvalid = validateLayer( currentLayer );
if ( currentLayer.typename == "LayerSet" ) {
collectLayersBelow( 0, currentLayer );
} else if ( isvalid == true ) {
collectedLayers.push( currentLayer );
currentLayer.allLocked = false;
}
}
}
/*
-------------------------------------------------------------
Collects all clipping layers of a group
-------------------------------------------------------------
*/
function collectClippingLayers( )
{
var i = layerIndex;
var currentLayer = layerParent.layers[ i ];
while ( currentLayer.grouped == true )
{
currentLayer = layerParent.layers[ i ];
var isvalid = validateLayer( currentLayer );
if ( currentLayer.typename == "LayerSet" ) {
collectLayersBelow( 0, currentLayer );
} else if ( isvalid == true ) {
collectedLayers.push( currentLayer );
}
i++;
}
}
/*
-------------------------------------------------------------
Executes actions for all layers in array
-------------------------------------------------------------
*/
function copyAndMergeSelectedLayer( )
{
for ( var i = 0; i < collectedLayers.length; i++ )
{
var duplicatedLayer = selectedLayer.duplicate( collectedLayers[i],
ElementPlacement.PLACEBEFORE );
if ( duplicatedLayer.grouped == false ) duplicatedLayer.grouped = true;
duplicatedLayer.merge( );
}
}
/*
-------------------------------------------------------------
Layer is ArtLayer, Visible, and Pixel (Normal)
-------------------------------------------------------------
*/
function validateLayer( __layer )
{
if ( __layer.kind == LayerKind.NORMAL ) {
if ( __layer.typename == "ArtLayer" ) {
if ( __layer.visible == true) {
// if ( __layer.blendMode == BlendMode.NORMAL ) {
return true;
// }
}
}
}
return false;
}