Photoshopスクリプトで複数のレイヤーをオンとオフにします
-
29-10-2019 - |
質問
Photoshopには、各グループ内に多数のレイヤーを含む6つのグループがあります。各グループ内のレイヤーをオン/オフにして、画像のあらゆる組み合わせを作成したいと考えています。
誰かが私を正しい方向に向けることができますか?
Photoshopでスクリプトしたことはありませんが、自分でこれを理解しようとしています。
解決
私はCS5スクリプトを自分自身にまったく初めて使用していますが、それがどのように機能するかを説明できると思います。コードの例はそれを行う最も効果的な方法ではないかもしれませんが、それはトリックを行います。
層のグループまたは個々の層自体には大きな違いがあります。すべてのレイヤーとグループは、DOM形式で注文されます。ルートを取得するには、グローバルインスタンスを使用できます app
アクティブなドキュメントを取得するには: app.activeDocument
.
乱雑な部分は、単一層とグループ用の2つの別々の配列があることです。単一層の配列を使用するには app.activeDocument.layers
と app.activeDocument.layerSets
グループのために。
Hieralcyを深く下るには、レイヤーセット配列を使用して繰り返します。
たとえば、次の階層を想定しましょう。
-Border
+Icons
+Left
-Star
-Home
+Right
-Add
-Remove
ここ Border
, Star
, Home
, Add
と Remove
すべての単一層です Icons
, Left
と Right
グループです。
グループをオンにする 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;
}
...そして、そのパスでグループまたはレイヤーを単純に設定またはクリアする1つの関数。
//******************************************
// 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);
これが私よりも誰かにとって役立つことを願っています:)