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

发布于 2025-01-08 07:01:31 字数 136 浏览 0 评论 0原文

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

有人能指出我正确的方向吗?

我从来没有在 Photoshop 中编写过脚本,而是尝试自己解决这个问题。

I have 6 groups in Photoshop that contain a number of layers within each group. I'm looking to turn on/off a layer within each group to create every possible combination of the image.

Can someone point me in the right direction?

I've never scripted in Photoshop but trying to figure this out on my own.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2025-01-15 07:01:31

我自己对 CS5 脚本编写很陌生,但我想我可以解释它是如何工作的。代码示例可能不是最有效的方法,但它确实有效。

一组层或单个层本身之间存在很大差异。
所有层和组均以 DOM 格式排序。要获取 root 权限,您可以使用全局实例 app 来获取活动文档:app.activeDocument

混乱的部分是单层和组有两个单独的数组。
要获取单层数组,请使用 app.activeDocument.layersapp.activeDocument.layerSets 作为组。

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

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

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

这里 BorderStarHomeAdd删除都是单层,而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);

,我希望这对我以外的其他人有用:)

I'm quite new to CS5 scripting myself, but I think I can explain how it works. The code examples may not be the most effective way to do it, but it does the trick.

There is a big difference between a Group of layers or the individual layer itself.
All layers and groups are ordered in the DOM format. To get your root you can use the global instance app to get the active document: app.activeDocument.

The messy part is that there are two separate arrays for single layers and groups.
To get the array of single layers use app.activeDocument.layers and app.activeDocument.layerSets for the groups.

To go deeper down in the hieralcy, use the layerSets array to iterate down.

For example, let us assume the following hieralcy:

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

Here Border, Star, Home, Add and Remove are all single layers while Icons, Left and Right are Groups.

To turn on group Left we need to iterate down the Icon group:

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

If you show a layer/group in CS5 by clicking with your mouse, all parent groups will automatically be shown too. By scripting this isn't the case, you have to enable all parents as well.

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

To show the Border layer you need to use the layers array instead.

app.activeDocument.layers.getByName("Border").visible = true;

Same things apply if you want to show the Add layer.

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;

This can be a bit messy if you have a lot of groups and layers. I created a function that follows a supplied path to get the end object. It will determine by itself if it is a layer or a group.

//******************************************
// 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;
}

...and one function to simply set or clear a group or layer by its path.

//******************************************
// 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;
}

..and at last I wrote this function to hide all groups and/or layers from a user specified root.

/******************************************
// 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;
}

Here is an example of how to use the functions

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

I hope this is useful to someone more than just me :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文