You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用ExtendScript在Adobe Photoshop中填充路径?

解决ExtendScript路径填充失效的问题

问题根源分析

你的代码中导致fillPath失效的核心原因有三个:

  • 形状运算模式错误:drawLines里设置了subPath.operation = ShapeOperation.SHAPEXOR,该模式用于多路径间的异或运算,单个闭合路径使用此模式会抵消填充区域,导致无法正常填充。
  • 路径未处于选中状态:drawLines最后执行了item.deselect(),而fillPath需要目标路径处于选中状态才能生效。
  • 不必要的透明度保留设置:preserveTransparency设为true时,仅会填充图层已有像素的区域,若目标图层为空白透明图层,填充操作会无效果。

修改后的代码

1. 修正drawLines函数

export const getSolidColor = (hex) => {
    const color = new SolidColor()
    color.rgb.hexValue = hex
    return color
}

export const drawLines = (doc, coordinates, title) => {
    const lines = []
    for (let i = 0; i < coordinates.length; i++) { // 用let声明变量i,避免全局污染
        const { anchor, kind, ldr, rdr } = coordinates[i]
        const point = [anchor.x, anchor.y]
        const line = new PathPointInfo()
        line.kind = kind
        line.anchor = point
        line.leftDirection = ldr ? [ldr.x, ldr.y] : point
        line.rightDirection = rdr ? [rdr.x, rdr.y] : point
        lines.push(line)
    }

    const subPath = new SubPathInfo()
    subPath.closed = true
    subPath.entireSubPath = lines
    subPath.operation = ShapeOperation.SHAPENONE // 改为无形状运算模式

    const item = doc.pathItems.add(title, [subPath])
    // 移除item.deselect(),让路径保持选中状态
    return item
}

2. 修正填充逻辑代码

export const convertPoints = (points) => {
    const newPoints = []
    for (let i = 0; i < points.length; i++) { // 用let声明变量i
        newPoints.push({
            kind: PointKind.CORNERPOINT,
            anchor: {
                x: points[i][0],
                y: points[i][1]
            },
            ldr: null,
            rdr: null
        })
    }
    return newPoints
}

const newPoints = convertPoints([
    [width * 0.33, height * 0],
    [width * 0.33, height * 1],
    [width * 0.5, height * 1],
    [width * 0.5, height * 0]
])
const fillColor = getSolidColor("0080ff")
const opacity = 90
const fillMode = ColorBlendMode.NORMAL
const preserveTransparency = false // 关闭透明度保留,允许填充整个路径区域
const feather = 0
const wholePath = true
const antiAlias = true

// 执行填充
const pathItem = drawLines(newDoc, newPoints, "Line 2")
pathItem.fillPath(
    fillColor,
    fillMode,
    opacity,
    preserveTransparency,
    feather,
    wholePath,
    antiAlias
)

额外注意事项

  • 若需要填充到指定图层,需先将目标图层设为活动图层:doc.activeLayer = targetLayer,再执行fillPath。
  • 确保循环变量i使用let声明,避免全局变量冲突导致循环逻辑异常。

内容的提问来源于stack exchange,提问作者Lance

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 00:07:46