在JavaScript脚本中将所有Photoshop组导出为.png时,如何解决图层偏移?

时间:2019-05-26 00:39:38

标签: photoshop photoshop-script

当我在photoshop中播放动画时,它看起来不错。每层位置正确。当我运行将每个组保存为png的脚本时,结果会有一些图层偏移。

我相信图层在动画中的位置存在怪异的问题。图层中的图像在一帧中可能具有不同的位置,而另一帧中可能没有。我相信我必须获得脚本才能在动画中步进帧才能获得正确的位置,但是我不确定如何做到这一点。

var set;
var layer;
var count = 0;
for(var i = 0; i < app.activeDocument.layerSets.length; i ++)
{
    set = app.activeDocument.layerSets[i];
    set.visible = false;
    count ++;
}
for(var i = 0; i < app.activeDocument.layerSets.length; i ++)
{
    set = app.activeDocument.layerSets[i];
    set.visible = true;

    // save
    var fileName = set.name.replace(/[\\\*\/\?:"\|<> ]/g,'');
    if(fileName.length ==0) fileName = "autoname";
    var handle = File(app.activeDocument.path + "/" + fileName + ".png");
    pngSaveOptions = new PNGSaveOptions();
    activeDocument.saveAs(handle, pngSaveOptions, true, Extension.LOWERCASE);

    set.visible = false;
}

alert("Saved " + count + " images.");

正确的动画在photoshop中播放。生成的.png不正确。 https://imgur.com/a/y9PLPUX

2 个答案:

答案 0 :(得分:1)

如果您的动画已经在Photoshop时间轴中,也许将动画渲染为帧会更容易吗?转到File > Export > Render Video...,选择Photoshop Image Sequence作为渲染器,选择PNG作为格式,在“渲染选项”中不要忘记选择Straight Alpha Channel。这样会将所有帧呈现为透明的PNG。

答案 1 :(得分:0)

弄清楚了。该脚本具有可以转到动画中另一帧的功能。我用它来将动画的所有子组渲染为各自独立的.pngs

var set;
var layer;

for(var docI = 0; docI < app.documents.length; docI ++) {
    var doc = app.documents[docI];
    for (var frameIndex = 1; frameIndex <= 8; frameIndex++) {
        goToFrame(frameIndex);
        for (var i = 0; i < doc.layerSets.length; i++) {
            set = doc.layerSets[i];
            if (set.visible) {
                for (var setI = 0; setI < set.layerSets.length; setI++) {
                    var innerSet = set.layerSets[setI];
                    innerSet.visible = false;
                }
                for (var setI = 0; setI < set.layerSets.length; setI++) {
                    var innerSet = set.layerSets[setI];
                    innerSet.visible = true;
                    var fileName = innerSet.name.replace(/[\\\*\/\?:"\|<> ]/g, '');
                    if (fileName.length == 0) fileName = "autoname";
                    var handle = File(doc.path + "/output/" + doc.name.replace("player", fileName).replace(".psd", '') + (doc.layerSets.length - i) + ".png");
                    pngSaveOptions = new PNGSaveOptions();
                    activeDocument.saveAs(handle, pngSaveOptions, true, Extension.LOWERCASE);
                    innerSet.visible = false;
                }

            }
        }
    }
}

alert("Saved images.");

function goToFrame(index) {
    var jumpToFrameNumber = index; // counts from 1

    var desc = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putIndex( stringIDToTypeID( "animationFrameClass" ), jumpToFrameNumber );
    desc.putReference( charIDToTypeID( "null" ), ref1 );
    executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
}