Photoshop脚本:在单独的文件中导出图层组合

时间:2016-03-03 16:06:39

标签: scripting flash-cs5 photoshop layer photoshop-script

在Photoshop CS5中,我有两组图层:" MODEL"和"材料。"

虽然第一个(" MODEL")将包含简单的图层("模型Tom","模型Jim","模型Harry" ; ...),另一个(" MATERIAL")将包含另一个组:" METAL"," WOOD"等等 当然,这些材料组本身也会有其他层(" METAL"将包含:" Iron"," steel"," Copper"等等," WOOD"将包含" Oak"," Cherry"," Maple"等等。)

我想要做的是导出由每个MODEL与所有MATERIALS的组合形成的jpg文件(质量72)。 两个图层合并在一个文件中。

文件名必须与要导出的最后一个子MATERIAL图层相同,目标文件夹(此时应该可能创建)应该被称为MODEL。

按照上面的示例,你会有类似的东西:

MODEL TOM
-Iron.jpg
-Steel.jpg
-Copper.jpg
-Oak.jpg
-Cherry.jpg
-Maple.jpg

MODEL JIM
-Iron.jpg
-Steel.jpg
-Copper.jpg
-Oak.jpg
-Cherry.jpg
-Maple.jpg

MODEL HARRY
-Iron.jpg
-Steel.jpg
-Copper.jpg
-Oak.jpg
-Cherry.jpg
-Maple.jpg

我知道一点php和javascript。 制作这样的东西是非常复杂的? 你有什么建议吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

欢迎使用Photoshop脚本。在层上循环很容易,但是组合变得复杂(并且可能有点令人头疼)并且没有简单的索引方法。

这个脚本是你想要的(基于为更复杂的设置做类似事情的脚本)它将遍历所有层并填充两个阵列,其中包含Avatar组中的所有内容,另一个包含背景组中的所有内容。

把Tom,Jim&哈利在一个名为" avatar"以及其他一切名为" backgrounds"。运行时,脚本会将文件保存到名为" 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();
}