Search code examples
photoshopphotoshop-script

Photoshop action to make 1 random layer visible within each group


I'm trying to use Photoshop actions to generate randomized images that are composed of a random sampling of layers. I have 3 groups of layers which are ALL not visible by default.

  1. Within each group I'd like to make 1 random layer visible (in total there will be 3 "on" layers)
  2. Export the whole thing as a .png file.
  3. Repeat n times

Example Groups/Layers:

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

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

[MEATS]
* [Beef]
* [Pork]

All layers are hidden by default, but when I play an action, I might get the following result (visible layers):

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

Solution

  • Here Is my script, but don't forget to do the following steps before running it:

    1. Hide All Layers and Groups Except Background.
    2. Save Your PSD.
    3. Close and then reopen.

    Now you are ready to rock.

    Features

    • Make Unlimited Patterns you want from your groups.

    • Saved All Patterns As Indexed separate PNG in Separate Folder named PNG.

    Watch GIF (below) to understand more:

    enter image description here

    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();
    }