欢迎的Photoshop脚本。循环遍历图层很容易,但对于组来说变得复杂(并且可能会让人头疼),并且没有简单的索引事物的方法。
这个脚本会做你想要的(基于一个脚本来做一个更复杂的设置类似的事情)它会遍历所有的图层,并填充两个数组之一,包括阿凡达组中的所有内容,其他与背景组中的所有内容。
认沽汤姆,吉姆&哈利层被称为“化身”和所谓的“背景”组中一切小组。运行时,脚本会将文件保存到名为“output”的文件夹中。
//pref pixels
app.preferences.rulerUnits = Units.PIXELS;
// call the source document
var srcDoc = app.activeDocument;
// vegetables
var numOfLayers = srcDoc.layers.length;
var layers = srcDoc.layers;
// group layer vegetables
var charArr = new Array(); // Tom, Jim & Harry avatars
var bkgArr = new Array(); // background images
var allLayers = new Array();
var theLayers = collectAllLayers(app.activeDocument, 0);
// go over the character image layers
getLayerNamesInGroupsFromArray(charArr);
// go over the background image layers
getLayerNamesInGroupsFromArray(bkgArr);
// Characters on backgrounds
loopOverGroup("backgrounds", bkgArr, "avatars", charArr);
function loopOverGroup(groupname1, arr1, groupname2, arr2)
{
for (var i = 0; i < arr1.length; i++)
{
var subLayer1 = arr1[i];
app.activeDocument.activeLayer = app.activeDocument.layerSets.getByName(groupname1).artLayers.getByName(subLayer1);
app.activeDocument.activeLayer.visible = true;
for (var j = 0; j < arr2.length; j++)
{
var subLayer2 = arr2[j];
// alert(subLayer2);
app.activeDocument.activeLayer = app.activeDocument.layerSets.getByName(groupname2).artLayers.getByName(subLayer2);
app.activeDocument.activeLayer.visible = true;
// Name the file
var tempName = subLayer1 + "_" + subLayer2;
duplicateIt(tempName);
activeDocument.flatten();
//create folder for it
makeDirectory("output", srcDoc.path);
// Set filePath and fileName to source path
var filePath = srcDoc.path + "/output/" + tempName + ".jpg";
myJpgQuality = 12;
// jpg file options
var jpgFile = new File(filePath);
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.formatOptions = FormatOptions.OPTIMIZEDBASELINE;
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = myJpgQuality;
activeDocument.saveAs(jpgFile, jpgSaveOptions, true, Extension.LOWERCASE);
// close it
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
app.activeDocument = srcDoc;
app.activeDocument.activeLayer.visible = false;
} // end loop j
}// end loop i
}
// function collect all layers
function collectAllLayers (theParent, level)
{
for (var m = theParent.layers.length - 1; m >= 0; m--)
{
var theLayer = theParent.layers[m];
var parentName = "Background";
if (theParent.typename == "LayerSet")
{
if (level == 1) parentName = theParent.name;
}
// apply the function to layersets;
if (theLayer.typename == "ArtLayer")
{
// switch the background background layer on
var layerName = theLayer.name;
if (parentName != "Background")
{
theLayer.visible = true;
}
// looking for backgrounds
if (parentName == "backgrounds")
{
bkgArr.push (parentName + ":" + theLayer.name);
theLayer.visible = false;
}
// looking for avatars
if (parentName == "avatars")
{
charArr.push (parentName + ":" + theLayer.name);
theLayer.visible = false;
}
}
else
{
allLayers.push(level + theLayer.name);
collectAllLayers(theLayer, level + 1)
}
}
}
function duplicateIt(str)
{
// duplicate image into new document
if (arguments.length == 0) str = "temp";
var id428 = charIDToTypeID("Dplc");
var desc92 = new ActionDescriptor();
var id429 = charIDToTypeID("null");
var ref27 = new ActionReference();
var id430 = charIDToTypeID("Dcmn");
var id431 = charIDToTypeID("Ordn");
var id432 = charIDToTypeID("Frst");
ref27.putEnumerated(id430, id431, id432);
desc92.putReference(id429, ref27);
var id433 = charIDToTypeID("Nm ");
desc92.putString(id433, str); // name
executeAction(id428, desc92, DialogModes.NO);
}
function getLayerNamesInGroupsFromArray(arr)
{
// go over the background image layers
for (var i = 0; i < arr.length; i++)
{
var temp = arr[i].toString();
var groupName = temp.substring(0, temp.indexOf(":"));
var layerName = temp.substring(temp.indexOf(":")+1, temp.length);
arr[i] = layerName;
}
return arr;
}
function makeDirectory(foldername, apath)
{
// create directory
var myDirectoryName = apath + "/" + foldername;
var myDirectory = new Folder(myDirectoryName);
if(!myDirectory.exists) myDirectory.create();
}