在 Photoshop 中循环遍历路径点

Loop over path points in Photoshop

我正在尝试在 Photoshop 中迭代创建路径,找出锚点位置等

var srcDoc = app.activeDocument;

// create the array of PathPointInfo objects
var lineArray = new Array();
lineArray.push(new PathPointInfo());
lineArray[0].kind = PointKind.CORNERPOINT;
lineArray[0].anchor = new Array(20, 160);
lineArray[0].leftDirection =  [35, 200];
lineArray[0].rightDirection = lineArray[0].anchor;

lineArray.push(new PathPointInfo());
lineArray[1].kind = PointKind.CORNERPOINT;
lineArray[1].anchor = new Array(20, 40);
lineArray[1].leftDirection = lineArray[1].anchor;
lineArray[1].rightDirection =  [220, 260];

// create a SubPathInfo object, which holds the line array in its entireSubPath property.
var lineSubPathArray = new Array();
lineSubPathArray.push(new SubPathInfo());
lineSubPathArray[0].operation = ShapeOperation.SHAPEXOR;
lineSubPathArray[0].closed = false;
lineSubPathArray[0].entireSubPath = lineArray;

//create the path item, passing subpath to add method
var myPathItem = srcDoc.pathItems.add("A Line", lineSubPathArray);


for (var i = 0; i < lineSubPathArray[0].entireSubPath.length; i++)
 {
   var b = lineSubPathArray[0].entireSubPath[i].anchor;
   alert(b);
}

这很好用,但我不想创建路径并找出它的信息,而是想遍历每条路径并获得相同的信息。这应该与上面的循环相同,只是没有显式调用 lineSubPathArray 及其部分。

for (var i = 0; i < srcDoc.pathItems[0].subPathItems.pathPoints.length; i++) // wrong I think
{
  var b = srcDoc.pathItems[0].entireSubPath[i].anchor; // wrong
  alert(b);
}

几乎:您需要遍历 subPathItems,其中包含 pathPoints:

  var srcDoc = activeDocument;
  var workPath = srcDoc.pathItems[0];
  var i, k, b;
  for (i = 0; i < workPath.subPathItems.length; i++) {
      for (k = 0; k < workPath.subPathItems[i].pathPoints.length; k++) {
          b = workPath.subPathItems[i].pathPoints[k].anchor;
          alert(b);
      }
  }