Skip to content

Instantly share code, notes, and snippets.

@AlexAegis
Created April 17, 2019 13:50
Show Gist options
  • Save AlexAegis/0353ad3c70a16134aad130fe8ff89e58 to your computer and use it in GitHub Desktop.
Save AlexAegis/0353ad3c70a16134aad130fe8ff89e58 to your computer and use it in GitHub Desktop.
Photoshop layer permutator
/**
* Photoshop only accepts ExtendScript which is compliant to ECMA-262 (version 3)
*
* This script, when executed in Photoshop exports all the possible combinations of layers in the first group of the active activeDocument
* For example:
* If the top group has 2 subgroups, each having 2 layers
* this script will export 4 pictures with the following layers being visible:
* [0, 0]
* [0, 1]
* [1, 0]
* [1, 1]
*
* If we look at this as a matrix then
* each row represents a picture,
* each column represents a subgroup
* each number represents which layer is the only one visible in that particular subgroup for that picture.
*
* Make sure that the folder you want to save in exists!
* It does not touch anything besides the first groups visibility settings, you can have your background below that
* If you want something to always be on top then just make a subgroup for that and have only one layer in it
*
* (Protip: You can quickly get out of hands with the amount of produced pictures.
* Just multiply together how many layers there are in each subgroup to know how many picture will be rendered)
*/
function cartesian(array) {
var results = [[]];
for (var i = 0; i < array.length; i++) {
var currentSubArray = array[i];
var temp = [];
for (var j = 0; j < results.length; j++) {
for (var k = 0; k < currentSubArray.length; k++) {
temp.push(results[j].concat(currentSubArray[k]));
}
}
results = temp;
}
return results;
}
function save(doc, saveFile) {
var saveOptions = new PNGSaveOptions();
saveOptions.compression = 2; // 0..9 (0 - without compression)
saveOptions.interlaced = false;
doc.saveAs(saveFile, saveOptions, true);
}
var stemsSet = app.activeDocument.layerSets[0];
var groupCount = stemsSet.layerSets.length;
var matrix = [];
for (var i = 0; i < groupCount; i++) {
var layerSet = stemsSet.layerSets[i];
var line = [];
for (var j = 0; j < layerSet.layers.length; j++) {
line.push(j);
}
matrix.push(line);
}
function hideAll() {
for (var i = 0; i < groupCount; i++) {
var layerSet = stemsSet.layerSets[i];
for (var j = 0; j < layerSet.layers.length; j++) {
var layer = layerSet.layers[j];
layer.visible = false;
}
}
}
var cp = cartesian(matrix);
for (var index = 0; index < cp.length; index++) {
var perm = cp[index];
hideAll();
var fPermName = '';
for (var permi = 0; permi < perm.length; permi++) {
fPermName = fPermName + perm[permi];
stemsSet.layerSets[permi].layers[perm[permi]].visible = true;
}
save(
app.activeDocument,
new File('~/Desktop/perm/perm_' + fPermName + '.png')
);
}
@AlexAegis
Copy link
Author

It's certainly not pretty, but does the job. This is all I could do practically blindly, with my hands tied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment