在Photoshop脚本中打开和关闭多层 [英] Turning Multiple Layers On and Off in Photoshop Script

查看:133
本文介绍了在Photoshop脚本中打开和关闭多层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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

推荐答案

对于CS5脚本编写我还是很陌生,但我想我可以解释一下它是如何工作的.代码示例可能不是最有效的方法,但是可以解决问题.

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.

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

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.

麻烦的是,对于单个图层和组,有两个单独的数组. 要获取单层阵列,请对组使用app.activeDocument.layersapp.activeDocument.layerSets.

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.

要深入了解层次结构,请使用layerSets数组进行迭代.

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

BorderStarHomeAddRemove都是单层,而IconsLeftRight是组.

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

要打开组Left,我们需要迭代Icon组:

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;

如果通过用鼠标单击在CS5中显示一个图层/组,则所有父组也将自动显示.如果不是通过脚本编写脚本,则还必须启用所有父母.

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;

要显示边框"层,您需​​要改用"layers"数组.

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 :)

这篇关于在Photoshop脚本中打开和关闭多层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆