Photoshop Script help

Community Beginner ,
Mar 24, 2021 Mar 24, 2021

Copy link to clipboard

Copied

I'm using Photoshop 2021 (22.3.0 release).

I have a script (shown below) that allows me to create random images from layer groups and outputs them as PNG files. This works great. But what I need the script to do is output forever-looping animated GIFs instead of PNGs.

 

My scenario is that I have a common background that is animated using frame-based animation on the timeline. Each of the aforementioned layer groups contain static images. So I'd like the script to spit out, say, 100, GIFs with each GIF having the common – animated – background, with static layer on top.

Is this possible? Thanks in advance!

Here's the script:

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

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();
}
TOPICS
Actions and scripting, How to, Mac, Problem or error

Views

235

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Mar 25, 2021 Mar 25, 2021
If the gif meet your needs there is no need to switch in other code.  I has missed that you were talking about frame animation. The easier approach might be to use timeline animation and make the lower, animated part an animated Smart Object.  »But...it animates everything, whereas I need only the background to be animated with a static random selection of traits sitting on top.«I think you may have misplaced the Save-part, it might belong in the for (var x=0 ; x<count;x++){clause itself.

Likes

translate

Translate

Translate
Adobe Community Professional ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

Saving a gif is possible if that is what you are asking; AM code might be faster, though. 

#target photoshop
if (app.documents.length > 0) {
////////////////////////////////////
// get document-path and -name; thanks to xbytor;
	var myDocument = app.activeDocument;
	var myDocName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
	var myPath = myDocument.path;
// save for web options;
	var webOptions = new ExportOptionsSaveForWeb();
	webOptions.format = SaveDocumentType.COMPUSERVEGIF;
	webOptions.optimized = true;
	webOptions.colorReduction = ColorReductionType.ADAPTIVE; 
	webOptions.dither = Dither.NONE; 
	webOptions.quality = 100; 
	webOptions.includeProfile = true; 
	webOptions.matteColor = new RGBColor(); 
	webOptions.matteColor.red = 255; 
	webOptions.matteColor.green = 255; 
	webOptions.matteColor.blue = 255; 
// export;
	myDocument.exportDocument(new File(myPath+"/"+myDocName+".gif"), ExportType.SAVEFORWEB, webOptions);
	};

 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

Hi @c.pfaffenbichler I appreciate you getting back to me on this thank you.

 

Since posting originally, I now have this script code:

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 = "GIF";   // define folder name
  var f = new Folder(outPath + "/" + fName);
  if ( ! f.exists ) {
    f.create()
  }
  var saveFile = new File(outPath + "/" + fName +"/" + "Pattern_" +  num + ".gif");
  
  SaveForWeb(saveFile);
}

function SaveForWeb(saveFile) {

  var sfwOptions = new ExportOptionsSaveForWeb();

     sfwOptions.format = SaveDocumentType.COMPUSERVEGIF;

     sfwOptions.includeProfile = false;
     sfwOptions.interlaced = true;
     sfwOptions.optimized = false;
     sfwOptions.transparency = 0;
     sfwOptions.ColorReductionType = ColorReductionType.SELECTIVE;
     sfwOptions.dither = Dither.NONE;
     sfwOptions.webSnap = 0;
     sfwOptions.colors = 256;
     sfwOptions.quality = 0;
    activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);

}

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

 

Would the code you posted work better than what I have? I've attached a sample PSD file similar to how I need it set up; simple animation (open Timeline) in a folder and 3 groups of 'traits'. 

 

Running the above script does almost what I need it to do, it outputs the correct number of GIF files. But...it animates everything, whereas I need only the background to be animated with a static random selection of traits sitting on top.

 

Is this even possible?

 

Thanks again!

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 25, 2021 Mar 25, 2021

Copy link to clipboard

Copied

If the gif meet your needs there is no need to switch in other code. 
 

I has missed that you were talking about frame animation. 
The easier approach might be to use timeline animation and make the lower, animated part an animated Smart Object. 
 

»But...it animates everything, whereas I need only the background to be animated with a static random selection of traits sitting on top.«
I think you may have misplaced the Save-part, it might belong in the 
for (var x=0 ; x<count;x++){
clause itself.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 26, 2021 Mar 26, 2021

Copy link to clipboard

Copied

Making the animation a smart object helped thanks!

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines