[用python win32com.client在Photoshop中移动图层

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

我一直在使用移动图层功能,用python编写photoshop脚本时遇到了麻烦。图层最终出现在其他图层集中,而不是在它们之后,等等。

在Photoshop的Javascript中执行此操作的本地命令是:

layer.move(relativeObject, insertionLocation)

其中insertLocation可以是:

ElementPlacement.INSIDE,ElementPlacement.PLACEATBEGINNING,ElementPlacement.PLACEATEND,ElementPlacement.PLACEBEFORE或ElementPlacement.PLACEAFTER

我当前的代码如下:

@staticmethod
def moveLayer(layer, relativeLayer = None, placement = 3):
    if placement == PHAPS.ElementPlacementPLACEATEND:
        layers = PHAPS.app().ActiveDocument.layers
        relativeLayer = layers[len(layers) - 1]
        placement = PHAPS.ElementPlacementPLACEAFTER
    elif placement == PHAPS.ElementPlacementPLACEATBEGINNING:
        relativeLayer = PHAPS.app().ActiveDocument.layers[0]
        placement = PHAPS.ElementPlacementPLACEBEFORE
    layer.Move(relativeLayer, placement)

这是我对常数值的猜测,这显然是错误的:

ElementPlacementPLACEATBEGINNING = 1
ElementPlacementINSIDE = 2
ElementPlacementPLACEBEFORE = 3
ElementPlacementPLACEAFTER = 4
ElementPlacementPLACEATEND = 5

我已经尝试对这些常量的本机值进行跟踪,但是Photoshop很好地隐藏了这些常量。我也尝试过Action Manager代码,但发现它有点难以理解。

有人知道用python在photoshop中移动图层的可靠方法吗?首选Action Manager代码,但不是必需的。

python python-3.x photoshop photoshop-script
1个回答
0
投票

嗨,您可以使用这些方法代替.Move()

def MoveAfter(self, RelativeObject=defaultNamedNotOptArg):
    'Move the PageItem in behind object'
    return self._oleobj_.InvokeTypes(1299596641, LCID, 1, (24, 0), ((9, 1),),RelativeObject
        )

def MoveBefore(self, RelativeObject=defaultNamedNotOptArg):
    'Move the PageItem in front of object'
    return self._oleobj_.InvokeTypes(1299596642, LCID, 1, (24, 0), ((9, 1),),RelativeObject
        )

def MoveToBeginning(self, Container=defaultNamedNotOptArg):
    'Move the PageItem to beginning of container'
    return self._oleobj_.InvokeTypes(1299596646, LCID, 1, (24, 0), ((9, 1),),Container
        )

def MoveToEnd(self, Container=defaultNamedNotOptArg):
    'Move the PageItem to end of container'
    return self._oleobj_.InvokeTypes(1299596645, LCID, 1, (24, 0), ((9, 1),),Container

其中:

  • RelativeObject =是相对图层参考
  • Container =是层所在的文档

示例

from win32com.client import Dispatch

app = Dispatch("Photoshop.Application")
doc = app.Open(r"hereTheProjectPath.psd")
layerRefA = doc.ArtLayers.Add()
layerRefA.MoveToEnd(doc)

layerRefB = doc.ArtLayers.Add()
layerRefB.MoveAfter(layerRefA)
© www.soinside.com 2019 - 2024. All rights reserved.