-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClothingLayersToPng.jsx
116 lines (96 loc) · 4.1 KB
/
ClothingLayersToPng.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
//http://absorbthenet.com/photoshopscripts/saveeachlayeraspng.jsx
//https://forums.adobe.com/thread/931920
//https://www.adobe.com/content/dam/acom/en/devnet/photoshop/scripting/Photoshop-CS6-JavaScript-Ref.pdf
#target photoshop
var mainFolderPath = '';
var docRef = app.activeDocument
var allLayerSets = docRef.layerSets
main();
function main() {
var wasCreated = promptAndCreateNewDestinationFolder();
if(!wasCreated) {
alert('The new folder at "' + mainFolderPath + '" was not created for some reason. Exiting the script.')
return;
}
//this will go recursively through all folders and turn visibility to false for every layer
processFoldersVisibility(docRef, false);
//this is the main loop going through all sets of fabrics, cuts and trim colors
loopThroughAllOptions(allLayerSets);
}
function loopThroughAllOptions(allLayerSets) {
var trimColors = allLayerSets.getByName('trim colors').artLayers;
var trimMasks = allLayerSets.getByName('trim masks').artLayers;
var clothingOptions = allLayerSets.getByName('clothing options').artLayers;
var fabricSwatches = allLayerSets.getByName('fabric swatches').artLayers;
for(var i = 0; i < clothingOptions.length; i++) {
var clothingOption = clothingOptions[i];
clothingOption.visible = true;
var matchingTrim = getMatchingTrim(trimMasks, clothingOption);
for(var k = 0; k < fabricSwatches.length; k++) {
var fabric = fabricSwatches[k];
fabric.visible = true;
if(matchingTrim) {
matchingTrim.visible = true;
for(var z = 0; z < trimColors.length; z++) {
var trimColor = trimColors[z];
//in order for this color mask grouping to work correctly, we need to put the color layer
//before the trim layer we want it applied to, then set grouped=true
var dupedColor = trimColor.duplicate(matchingTrim, ElementPlacement.PLACEBEFORE );
dupedColor.grouped = true;
dupedColor.visible = true;
saveCurrentStatus(mainFolderPath + '/' +
fabric.name + '-' + trimColor.name + '-trim' +
'.png');
dupedColor.grouped = false;
dupedColor.remove();
}
matchingTrim.visible = false;
} else {
saveCurrentStatus(mainFolderPath + '/' +
fabric.name +
'.png');
}
fabric.visible = false;
}
clothingOption.visible = false;
}
}
function getMatchingTrim(trimMasks, clothingOption) {
try {
return trimMasks.getByName(clothingOption.name + ' trim');
} catch(e) {
return null;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function promptAndCreateNewDestinationFolder() {
var folderName = prompt('What is the name of the new folder you want the pngs to be put inside of?', 'defaultName');
//this is setting a global variable
mainFolderPath = app.activeDocument.path + '/' + folderName;
var folderObject = new Folder(mainFolderPath);
return folderObject.create();
}
function saveCurrentStatus(newFilePathname) {
var myPSPNGSaveOptions = new PNGSaveOptions();
myPSPNGSaveOptions.compression = 0;
docRef.saveAs(File(newFilePathname), myPSPNGSaveOptions, true, Extension.LOWERCASE);
}
// ===============================================================================================
// SETS EVERY FOLDER AND LAYERS VISIBILITY TO OFF TO START.
function processFoldersVisibility ( activeFolder, visibility ){
if ( activeFolder.layerSets.length > 0 ) {
for (var sf = 0; sf < activeFolder.layerSets.length; sf++){
processFoldersVisibility ( activeFolder.layerSets[sf], visibility );
}
}
processFilesVisibility( activeFolder, visibility );
}
function processFilesVisibility ( activeFolder, visibility ){
// Process Each artLayer
for (var a = 0; a < activeFolder.artLayers.length; a++){
//alert(activeFolder.artLayers[a])
activeFolder.artLayers[a].visible = visibility;
}
}
// ENDS THE HANDLING OF THE FOLDERS AND LAYERS VISIBILITY
// =============================================================================