将图层排列成网格的 Photoshop 脚本

问题描述 投票:0回答:1

我有一个简单的 PSD,其中有一个名为“faces”的图层组,其中有 24 个图层,每个图层为 175x175 像素。

我想自动将它们排列成 6x4 网格。

当我运行这个脚本时(在https://stackoverflow.com/a/12064474/470749的帮助下),我的每个图层似乎都被删除了。它们仍然存在,但内容现在显示为空。

我该如何解决这个问题?

// Check if we are running inside Adobe Photoshop
if (app.name !== "Adobe Photoshop") {
    alert("This script works only with Adobe Photoshop.");
} else {
    main();
}

function main() {
    var groupName = "faces";
    
    var itemWidth = 175;
    var itemHeight = 175;

    // Define the grid dimensions
    var columns = 6;
    var rows = 4;

    // Get the active document
    var doc = app.activeDocument;
    var layerSets = doc.layerSets;    
    var facesGroup = layerSets.getByName(groupName);

    // Check if the group exists
    if (facesGroup) {
        // Iterate over layers in the "faces" group
        for (var i = 0; i < facesGroup.artLayers.length; i++) {
            var currentLayer = facesGroup.artLayers[i];
            
            // Set the active layer
            doc.activeLayer = currentLayer;
            // Get a reference to the active layer
            var layer = doc.activeLayer;

              // Calculate column and row
              var column = i % columns;
              var row = Math.floor(i / columns);

              // Calculate x, y position
              var x = column * itemWidth;
              var y = row * itemHeight;
              
              // FIXNOW: Why does this not work?!
              
             MoveLayerTo(layer, x, y);
        }
        alert("Completed.");
    } else {
        alert("Group named 'faces' not found.");
    }
}

function MoveLayerTo(fLayer,fX,fY) {
  // https://stackoverflow.com/a/12064474/470749
  var Position = fLayer.bounds;
  Position[0] = fX - Position[0];
  Position[1] = fY - Position[1];

  fLayer.translate(-Position[0],-Position[1]);
}
photoshop photoshop-script
1个回答
0
投票

我认为答案是将我的标尺设置(在 Photoshop 中)从“%”更改为像素。

我从https://stackoverflow.com/a/12064474/470749的评论中得到它。

现在代码似乎可以工作了。

我做了一些小的改进,但上面的原始代码可能也可以工作(只要标尺设置为“px”)。


// Check if we are running inside Adobe Photoshop
if (app.name !== "Adobe Photoshop") {
    alert("This script works only with Adobe Photoshop.");
} else {
    main();
}

function main() {
    var groupName = "faces";
    
    var itemWidth = 175;
    var itemHeight = 175;

    // Define the grid dimensions
    var columns = 6;
    var rows = 4;

    // Get the active document
    var doc = app.activeDocument;
    var layerSets = doc.layerSets;    
    var facesGroup = layerSets.getByName(groupName);

    // Check if the group exists
    if (facesGroup) {
        // Iterate over layers in the "faces" group
        for (var i = 0; i < facesGroup.artLayers.length; i++) {
            var currentLayer = facesGroup.artLayers[i];
            
            // Set the active layer
            doc.activeLayer = currentLayer;
            // Get a reference to the active layer
            var layer = doc.activeLayer;

              // Calculate column and row
              var column = i % columns;
              var row = Math.floor(i / columns);

              // Calculate x, y position
              var x = column * itemWidth;
              var y = row * itemHeight;
              
              // FIXNOW: Why does this not work?!
              
             MoveLayerTo(layer, x, y);
        }
        alert("Completed.");
    } else {
        alert("Group named 'faces' not found.");
    }
}

function MoveLayerTo(fLayer, x, y) {
  // https://stackoverflow.com/a/12064474/470749
  var Position = fLayer.bounds;
  var originX = Position[0];
  var originY = Position[1];
  var offsetX = x - originX;
  var offsetY = y - originY;
  //var log = 'x,y = ' + x + ',' + y;
  //alert(log);
  
  //var log = 'offsetX,offsetY = ' + offsetX + ',' + offsetY; 
  //alert(log);

  fLayer.translate(-offsetX,-offsetY);
}
© www.soinside.com 2019 - 2024. All rights reserved.