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

如何使用Python实现Photoshop智能对象Relink to File重链操作

Python操作Photoshop智能对象重链/内容编辑实现

功能说明

需要实现两个核心操作:

  • 智能对象Relink to File(重链到新文件),对应按钮位置:
    Relink to File按钮
  • 智能对象Edit Contents(编辑内容),对应按钮位置:
    Edit Contents按钮

当前已完成顶层智能对象图层遍历逻辑,可识别类型值为17的智能对象图层,基础代码如下:

import win32com.client

psApp = win32com.client.Dispatch('Photoshop.Application')
psDoc = psApp.Application.ActiveDocument

for layer in psDoc.layers:
    if layer.kind == 17:  # layer kind 17 is Smart Object
        print(layer.name)
        # 待补充:重链/编辑内容逻辑

实现代码

Photoshop的COM接口未直接暴露这两个功能的顶层方法,需要通过ActionDescriptor调用PS内置动作实现,两个功能的可运行代码如下:

1. 智能对象重链到指定文件

将函数整合到你的代码中,传入智能对象图层对象、新文件的绝对路径即可完成重链:

import os
import win32com.client

def relink_smart_object(target_layer, new_file_abs_path):
    # 校验路径合法性
    new_file_abs_path = os.path.abspath(new_file_abs_path)
    if not os.path.isfile(new_file_abs_path):
        raise FileNotFoundError(f"指定的重链文件不存在: {new_file_abs_path}")
    
    ps_app = win32com.client.Dispatch("Photoshop.Application")
    # 构造动作参数
    id_placed = ps_app.CharIDToTypeID("Plc ")
    id_null = ps_app.CharIDToTypeID("null")
    relink_ref = win32com.client.Dispatch("Photoshop.ActionReference")
    relink_ref.PutIdentifier(id_placed, target_layer.ID)

    relink_desc = win32com.client.Dispatch("Photoshop.ActionDescriptor")
    relink_desc.PutReference(id_null, relink_ref)

    id_target = ps_app.CharIDToTypeID("T   ")
    id_file = ps_app.CharIDToTypeID("File")
    file_desc = win32com.client.Dispatch("Photoshop.ActionDescriptor")
    file_desc.PutPath(id_file, new_file_abs_path)
    relink_desc.PutObject(id_target, id_placed, file_desc)

    # 执行重链动作
    relink_event_id = ps_app.StringIDToTypeID("placedLayerRelinkToFile")
    ps_app.ExecuteAction(relink_event_id, relink_desc, 3)

# 调用示例
if __name__ == "__main__":
    ps_app = win32com.client.Dispatch('Photoshop.Application')
    active_doc = ps_app.ActiveDocument
    # 替换为你要重链到的新文件绝对路径
    new_file_path = r"C:\your_work_dir\new_asset.png"

    for layer in active_doc.layers:
        if layer.kind == 17:
            print(f"重链智能对象: {layer.name}")
            relink_smart_object(layer, new_file_path)

提示:当前遍历逻辑仅能识别文档顶层的智能对象,如果智能对象存放在图层组内,需要额外编写递归遍历图层组的逻辑。

2. 打开智能对象进入内容编辑模式

调用以下函数即可实现和点击Edit Contents按钮完全一致的效果,会直接打开智能对象的源文档窗口:

import win32com.client

def open_smart_object_for_edit(target_layer):
    ps_app = win32com.client.Dispatch("Photoshop.Application")
    # 构造动作参数
    id_placed = ps_app.CharIDToTypeID("Plc ")
    id_null = ps_app.CharIDToTypeID("null")
    edit_ref = win32com.client.Dispatch("Photoshop.ActionReference")
    edit_ref.PutIdentifier(id_placed, target_layer.ID)

    edit_desc = win32com.client.Dispatch("Photoshop.ActionDescriptor")
    edit_desc.PutReference(id_null, edit_ref)

    # 执行编辑内容动作
    edit_event_id = ps_app.StringIDToTypeID("placedLayerEditContents")
    ps_app.ExecuteAction(edit_event_id, edit_desc, 3)

# 调用示例
if __name__ == "__main__":
    ps_app = win32com.client.Dispatch('Photoshop.Application')
    active_doc = ps_app.ActiveDocument

    for layer in active_doc.layers:
        if layer.kind == 17:
            print(f"打开智能对象编辑: {layer.name}")
            open_smart_object_for_edit(layer)
            # 执行后可直接通过ps_app.ActiveDocument获取打开的智能对象源文档,进行后续编辑操作

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 10:15:31