Photoshop脚本中开启和关闭多个图层

3

我在Photoshop中有6个组,每个组内含有多个图层。我想打开/关闭每个组内的一个图层以创建图像的所有可能组合。

请问有人可以指点我吗?

我从未在Photoshop中编写过脚本,但正在尝试独立解决这个问题。


1
请看这个答案:https://dev59.com/9l7Va4cB1Zd3GeqPJWRd#8544923 - KatieK
1个回答

3

我对CS5脚本编写比较新,但我认为我可以解释它的工作方式。代码示例可能不是最有效的方法,但它能完成任务。

图层组和单个图层之间有很大的区别。所有图层和组都按DOM格式排序。要获取根节点,可以使用全局实例app来获取活动文档:app.activeDocument

混乱的部分在于单个图层和组有两个不同的数组。要获取单个图层的数组,请使用app.activeDocument.layers,对于组,请使用app.activeDocument.layerSets

要在层次结构中更深入,请使用layerSets数组向下迭代。

例如,假设以下层次结构:

-Border
+Icons
   +Left
       -Star
       -Home
   +Right
       -Add
       -Remove

这里的BorderStarHomeAddRemove都是单层,而IconsLeftRight则是组。

要启用组Left,我们需要迭代Icon组:

Icons = app.activeDocument.layerSets.getByName("Icons");
Left = Icons.layerSets.getByName("Left");
Left.visible = true;

如果你在CS5中通过鼠标单击显示一个图层/组,所有父级组都会自动显示。但是通过脚本操作时,情况并非如此,你还需要将所有父级组也启用。
Icons = app.activeDocument.layerSets.getByName("Icons");
Icons.visible = true;
Left = Icons.layerSets.getByName("Left");
Left.visible = true;

要显示边框层,您需要使用图层数组而不是图层。
app.activeDocument.layers.getByName("Border").visible = true;

如果您想显示“添加图层”,同样的事情也适用。
Icons = app.activeDocument.layerSets.getByName("Icons");
Icons.visible = true;
Right = Icons.layerSets.getByName("Right");
Right.visible = true;
AddLayer = Right.layers.getByName("Add");
AddLayer.visible = true;

如果您有许多组和图层,这可能会有些混乱。我创建了一个函数,根据提供的路径获取最终对象。它会自动确定是图层还是组。

//******************************************
// GET BY PATH
// Author: Max Kielland
//
// Gets the LayerSet or Layer at the path's end.
// Example path "Icons/left" will return the LayerSet object "Left"
// while "Icons/left/Star" will return the Layer object "Star"
// If fSetPath is true, all the parents will be visible as well.

function GetByPath(fPath,fSetPath) {

  var lGroup = null;
  var lPathArray = new Array();

  lPathArray = fPath.split('/');
  try {
    lGroup = app.activeDocument.layers.getByName(lPathArray[0]);
  } catch (err) {
    lGroup = app.activeDocument.layerSets.getByName(lPathArray[0]);
  }

  if (fSetPath)
    lGroup.visible = true;

  for (n=1; n<lPathArray.length; n++) {
    try {
      lGroup = lGroup.layerSets.getByName(lPathArray[n]);
    } catch(err) {
      lGroup = lGroup.layers.getByName(lPathArray[n]);
    }
    if (fSetPath == true)
      lGroup.visible = true;
  }

  return lGroup;
}

...并且需要一个函数,通过其路径简单地设置或清除一个组或层。

//******************************************
// SET STATUS
// Author: Max Kielland
//
// Sets the Group or Layer's visible property
// at the end of the path to fStatus.

function SetStatus(fPath, fStatus) {

  Obj = GetByPath(fPath,false);
  Obj.visible = fStatus;
}

最后,我编写了这个函数,可以从用户指定的根隐藏所有组和/或图层。

/******************************************
// CLEAR GROUP
// Author: Max Kielland
//
// Clears the visible property in a single
// group/layer with the option to clear all
// its children as well (fRecurs = true).
// fLayerSet can be a layerSet object or a
// String path.

function ClearGroup(fLayerSet,fRecurs) {

  var n;
  var TargetGroup;

  // Get LayerSet Object if reference is a string.
  if (typeof fLayerSet == "string")
    TargetGroup = GetByPath(fLayerSet);
  else
    TargetGroup = fLayerSet;

  // Iterate through all LayerSets
  for (n=0; n<TargetGroup.layerSets.length; n++) {
    if (fRecurs == true)
      ClearGroup(TargetGroup.layerSets[n],true);
    else
     TargetGroup.layerSets[n].visible = false;
  }

  // Iterate through all layers
  for (n=0; n<TargetGroup.layers.length; n++) {
    TargetGroup.layers[n].visible = false;
  }

  // Clear self
  TargetGroup.visible = false;
}

这是一个如何使用函数的示例。
// Hide group "Icon" and its children
ClearGroup("Icons",true);

//Show the layer "Home"
GetByPath("Icons/Left/Home",true);

// To just get the object "Right"
var MyGroup = GetByPath("Icons/Right");

// Save the current document as a PNG file
app.activeDocument.saveAs(File("Scripted Document.png"),PNGSaveOptions);

我希望这对不仅仅是我而言的某人有所帮助 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接