-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryptoToLayered.jsx
100 lines (72 loc) · 2.28 KB
/
CryptoToLayered.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
<javascriptresource>
<name>Cryptomatte to Layered PSD</name>
<about>
This will convert an exr cyrptomatte file to a layered PSD.
This assumes that the EXR was unpacked by EXR-IO
https://www.exr-io.com/
Evan Viera
</about>
<enableinfo>true</enableinfo>
<menu>filter</menu>
<category>Viera</category>
<type>automate</type>
</javascriptresource>
#include "vieraLibrary.jsx"
var doc = activeDocument;
var layerIndex = 0;
var rgbLayer;
// Collect all Cryptomatte Layers
var cryptoLayers = getAllCrypoLayers( );
// Find "RenderLayer.Combined.RGBA" and store as variable rgbLayer
// Disable the visibility for all layers. Important for the RGB to A function.
var RgbCombined = new RegExp( /Combined.RGBA/gim );
for ( var i = 0; i < doc.layers.length; i++ )
{
if ( doc.layers[ i ].name.match( RgbCombined ) ) rgbLayer = doc.layers[ i ];
doc.layers[ i ].visible = false;
}
// Loop Through Crypto and convert RGB to A
for ( var i = 0; i < cryptoLayers.length; i++ )
{
var currentLayer = cryptoLayers[ i ];
currentLayer.visible = true;
var convertedLayer = applyCryptoToRgbLayer( currentLayer );
convertedLayer.visible = false;
}
// Make all newly created layers visible.
for ( var i = 0; i < cryptoLayers.length; i++ ) cryptoLayers[ i ].visible = true;
/* ------------------------------------------------
Main function.
This will call the convert
function that transfors RGB into the layer's
matte. Then will duplicate the RGB layer and
flatten it.
------------------------------------------------
*/
function applyCryptoToRgbLayer( __cryptoLayer )
{
doc.activeLayer = __cryptoLayer;
convertRGBToMask();
var rgbDuplicate = rgbLayer.duplicate( __cryptoLayer, ElementPlacement.PLACEBEFORE );
rgbDuplicate.grouped = true;
return rgbDuplicate.merge( );
}
/* ------------------------------------------------
Loops through and collects all crypto layers that
it finds via the regex expression.
------------------------------------------------
*/
function getAllCrypoLayers( )
{
var collectedLayers = [];
var regEx = new RegExp( /Crypto/gim );
for ( var i = 0; i < doc.layers.length; i++ )
{
var currentLayer = doc.layers[ i ];
if ( currentLayer.name.match( regEx ) )
{
collectedLayers.push( currentLayer );
}
}
return collectedLayers;
}