Photoshop JS脚本创建并应用图层蒙版

5
我用JS编写了一个Photoshop脚本,可以将每个图层文件夹按照精灵表格的方式分开。我已经解决了这部分问题,但我正在尝试在运行脚本时消除任何形式的人为错误。目前,您需要准备好每个图层文件夹的正确命名,并且还必须对选择应用图层蒙版。
我想消除用户应用图层蒙版的需求。我可以选择图层,然后选择要蒙版的部分,但我不知道如何应用或创建蒙版。
我希望它被应用的位置:
function maskIt(){
    if(currentFrameCount < (frameNumber-1)){
        currentFrameCount = currentFrameCount+1;
        currentFrame = ("frame"+currentFrameCount);
        activeDocument.layers[currentFrame].visable;
        activeDocument.selection.selectAll();
        //createMask();
        maskComplete = false;
    } else  if (currentFrameCount == (frameNumber-1)){
        currentFrameCount = currentFrameCount+1;
        currentFrame = ("frame"+currentFrameCount);
        activeDocument.layers[currentFrame].visable;
        activeDocument.selection.selectAll();
        //createMask();
        currentFrameCount = 0;
        maskComplete = true;
    }
}
3个回答

3

以下是一个更简洁的版本,添加了try来检查是否存在蒙版。如果你想应用任何蒙版,只需将deleteLayerMask(true);这行代码移动到catch (e) {}下面即可。

addMasks();

function addMasks(){

    try{
        loadLayerSelection();
        addLayerMask();
        deleteLayerMask(true);
        } catch (e) {}
};

// =======================================================
function loadLayerSelection() {
    var c2t = function (s) {
        return app.charIDToTypeID(s);
    };

    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };

    var descriptor = new ActionDescriptor();
    var reference = new ActionReference();
    var reference2 = new ActionReference();

    reference.putProperty( s2t( "channel" ), s2t( "selection" ));
    descriptor.putReference( c2t( "null" ), reference );
    reference2.putEnumerated( s2t( "channel" ), s2t( "channel" ), s2t( "transparencyEnum" ));
    descriptor.putReference( s2t( "to" ), reference2 );
    executeAction( s2t( "set" ), descriptor, DialogModes.NO );
}

// =======================================================
function addLayerMask() {
    var c2t = function (s) {
        return app.charIDToTypeID(s);
    };

    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };

    var descriptor = new ActionDescriptor();
    var reference = new ActionReference();

    descriptor.putClass( s2t( "new" ), s2t( "channel" ));
    reference.putEnumerated( s2t( "channel" ), s2t( "channel" ), s2t( "mask" ));
    descriptor.putReference( s2t( "at" ), reference );
    descriptor.putEnumerated( s2t( "using" ), c2t( "UsrM" ), s2t( "revealSelection" ));
    executeAction( s2t( "make" ), descriptor, DialogModes.NO );
}

// =======================================================
function deleteLayerMask(apply) {
    var c2t = function (s) {
        return app.charIDToTypeID(s);
    };

    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };

    var descriptor = new ActionDescriptor();
    var reference = new ActionReference();

    reference.putEnumerated( s2t( "channel" ), s2t( "channel" ), s2t( "mask" ));
    descriptor.putReference( c2t( "null" ), reference );
    descriptor.putBoolean( s2t( "apply" ), apply );
    executeAction( s2t( "delete" ), descriptor, DialogModes.NO );
}

1

这两个从脚本监听器整理出来的函数应该会对你有所帮助:

// FUNCTION MAKE MASK ()
function makeMask()
{
  // =======================================================
  var id4556 = charIDToTypeID( "setd" );
  var desc983 = new ActionDescriptor();
  var id4557 = charIDToTypeID( "null" );
  var ref657 = new ActionReference();
  var id4558 = charIDToTypeID( "Chnl" );
  var id4559 = charIDToTypeID( "fsel" );
  ref657.putProperty( id4558, id4559 );
  desc983.putReference( id4557, ref657 );
  var id4560 = charIDToTypeID( "T   " );
  var ref658 = new ActionReference();
  var id4561 = charIDToTypeID( "Chnl" );
  var id4562 = charIDToTypeID( "Chnl" );
  var id4563 = charIDToTypeID( "Trsp" );
  ref658.putEnumerated( id4561, id4562, id4563 );
  desc983.putReference( id4560, ref658 );
  executeAction( id4556, desc983, DialogModes.NO );

  // =======================================================
  var id4564 = charIDToTypeID( "Mk  " );
  var desc984 = new ActionDescriptor();
  var id4565 = charIDToTypeID( "Nw  " );
  var id4566 = charIDToTypeID( "Chnl" );
  desc984.putClass( id4565, id4566 );
  var id4567 = charIDToTypeID( "At  " );
  var ref659 = new ActionReference();
  var id4568 = charIDToTypeID( "Chnl" );
  var id4569 = charIDToTypeID( "Chnl" );
  var id4570 = charIDToTypeID( "Msk " );
  ref659.putEnumerated( id4568, id4569, id4570 );
  desc984.putReference( id4567, ref659 );
  var id4571 = charIDToTypeID( "Usng" );
  var id4572 = charIDToTypeID( "UsrM" );
  var id4573 = charIDToTypeID( "RvlS" );
  desc984.putEnumerated( id4571, id4572, id4573 );
  executeAction( id4564, desc984, DialogModes.NO );
}


// FUNCTION APPLY LAYER MASK()
function applyLayerMask()
{
  // =======================================================
  var id1949 = charIDToTypeID( "Dlt " );
  var desc398 = new ActionDescriptor();
  var id1950 = charIDToTypeID( "null" );
  var ref291 = new ActionReference();
  var id1951 = charIDToTypeID( "Chnl" );
  var id1952 = charIDToTypeID( "Chnl" );
  var id1953 = charIDToTypeID( "Msk " );
  ref291.putEnumerated( id1951, id1952, id1953 );
  desc398.putReference( id1950, ref291 );
  var id1954 = charIDToTypeID( "Aply" );
  desc398.putBoolean( id1954, true );
  executeAction( id1949, desc398, DialogModes.NO );
}

如果您试图排除用户错误,您可能需要第三个功能,它将检测图层是否有蒙版(选择该图层,尝试复制和粘贴图层蒙版;如果无法粘贴,则没有图层蒙版)


-1
一种更加简洁的方式,可以将遮罩(剪切和/或图层)应用于当前选定的对象:
// Clipping Mask
const applyClippingMask = function () {
    // Grab the needed IDs
    const groupEventID = stringIDToTypeID('groupEvent')
    const nullID = stringIDToTypeID('null')
    const layerID = stringIDToTypeID('layer')
    const ordinalID = stringIDToTypeID('ordinal')
    const targetEnumID = stringIDToTypeID('targetEnum')

    // Prep the action
    const actionDesc = new ActionDescriptor()
    const actionRef = new ActionReference()

    // Prep the mask
    actionRef.putEnumerated(layerID, ordinalID, targetEnumID)
    actionDesc.putReference(nullID, actionRef)
    
    // Apply the mask
    executeAction(groupEventID, actionDesc, DialogModes.NO)
}

// Layer mask from selection
const applyLayerMask = function () {
    // Required internal IDs
    const makeID = stringIDToTypeID('make')
    const newID = stringIDToTypeID('new')
    const channelID = stringIDToTypeID('channel')
    const atID = stringIDToTypeID('at')
    const usingID = stringIDToTypeID('using')
    const userMaskEnabledID = stringIDToTypeID('userMaskEnabled')
    const revealSelectionID = stringIDToTypeID('revealSelection')

    // Prep the action
    const actionDesc = new ActionDescriptor()
    const actionRef = new ActionReference()

    // Setup the mask
    actionDesc.putClass(newID, channelID)
    actionRef.putEnumerated(channelID, channelID, maskID)
    actionDesc.putReference(atID, actionRef)
    actionDesc.putEnumerated(usingID, userMaskEnabledID, revealSelectionID)

    // Apply the mask
    executeAction(makeID, actionDesc, DialogModes.NO)
}

根据Sergey的评论,编辑以包括剪辑和图层蒙版功能

在Photoshop CC 2021中有效。


1
问题是关于应用图层蒙版的,你的代码创建了剪贴蒙版。 - Sergey Kritskiy
误读需求 - 已更新回复以包括一种清晰应用图层蒙版的方法 :) - darryn.ten

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