我在Photoshop中有6個組,每個組中包含多個圖層。我正在打開/關閉每個組中的圖層,以創建圖像的每種可能的組合。在Photoshop腳本中打開和關閉多個圖層
有人能指出我正確的方向嗎?
我從來沒有在Photoshop中腳本,但試圖找出我自己的。
我在Photoshop中有6個組,每個組中包含多個圖層。我正在打開/關閉每個組中的圖層,以創建圖像的每種可能的組合。在Photoshop腳本中打開和關閉多個圖層
有人能指出我正確的方向嗎?
我從來沒有在Photoshop中腳本,但試圖找出我自己的。
我對CS5腳本編程很陌生,但我想我可以解釋它是如何工作的。代碼示例可能不是最有效的方法,但它的確有用。
一組圖層或單個圖層本身之間存在很大差異。 所有圖層和組按DOM格式排序。要獲取根目錄,可以使用全局實例app
獲取活動文檔:app.activeDocument
。
凌亂的部分是,有兩個單獨的數組單層和組。 要獲得單層陣列,請使用組app.activeDocument.layers
和app.activeDocument.layerSets
。
要更深層次地使用層次結構,請使用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;
}
...以及通過其路徑簡單地設置或清除組或層的功能之一。
//******************************************
// 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);
一個例子,我希望這是對別人不只是我:)
看一看這個答案更加有用:HTTP:/ /stackoverflow.com/a/8544923/327466 – KatieK 2012-02-23 22:52:49