Photoshop 动作使 1 个随机图层在每个组中可见

Photoshop action to make 1 random layer visible within each group

我正在尝试使用 Photoshop 动作生成由随机采样层组成的随机图像。我有 3 组图层,默认情况下它们都是不可见的。

  1. 在每个组中,我想让 1 个随机图层可见(总共会有 3 "on" 个图层)
  2. 将整个内容导出为 .png 文件。
  3. 重复n次

示例Groups/Layers:

[FRUITS]
* [Apples]
* [Oranges]
* [Pears]
* [Bananas]
* [Kiwis]

[VEGGIES]
* [Asparagus]
* [Cilantro]
* [Eggplant]

[MEATS]
* [Beef]
* [Pork]

默认隐藏所有层,但是当我播放一个动作时,我可能会得到以下结果(可见层):

Image1: [Apples] [Eggplant] [Pork]
Image2: [Pears] [Asparagus] [Pork]
Image3: [Kiwis] [Cilantro] [Beef]

这是我的脚本,但不要忘记在 运行 之前执行以下步骤:

  1. 隐藏除背景之外的所有图层和组。
  2. 保存您的 PSD。
  3. 关闭然后重新打开。

现在你可以开始摇滚了。

特征

  • 从你的组中制作你想要的无限图案。

  • 在名为 PNG.

    的单独文件夹中将所有图案保存为单独的 PNG 索引

观看 GIF(下方)以了解更多信息:

function Visible() {
  var Grps = app.activeDocument.layerSets; // loops through all groups
  for(var i = 0; i < Grps.length; i++){
    var tmp = app.activeDocument.layerSets[i].layers.length;
    app.activeDocument.layerSets[i].visible=true;
    var groupChildArr = app.activeDocument.layerSets[i].layers;
    var randLays = Math.floor(Math.random() * tmp);
    groupChildArr[randLays].visible = true;
    Save();
  }
  Revert();
}

function Save() {
  var outFolder = app.activeDocument; // psd name
  var outPath = outFolder.path;
  var fName = "PNG";   // define folder name
  var f = new Folder(outPath + "/" + fName);
  if ( ! f.exists ) {
    f.create()
  }
  var saveFile = new File(outPath + "/" + fName +"/" + "Pattern_" +  num + ".png");
  pngSaveOptions = new PNGSaveOptions();
  pngSaveOptions.interlaced = false;
  app.activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
}

// Original code - revert function does not work
// for some users
//function Revert(){
//  var idslct = charIDToTypeID( "slct" );
//  var desc300 = new ActionDescriptor();
//  var idnull = charIDToTypeID( "null" );
//  var ref163 = new ActionReference();
//  var idSnpS = charIDToTypeID( "SnpS" );
//  ref163.putName( idSnpS, "test.psd" );
//  desc300.putReference( idnull, ref163 );
//  executeAction( idslct, desc300, DialogModes.NO );
//}

function Revert(){
   var idRvrt = charIDToTypeID( "Rvrt" );
   executeAction( idRvrt, undefined, DialogModes.NO );
}

var count = prompt("How many patterns you want","");
for (var x=0 ; x<count;x++){
  var num = x+1;
  Visible();
}