简体   繁体   English

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

[英]move layer in Photoshop with python win32com.client

I've been having a hell of a time with the move layer function, programming a photoshop script in python.我一直在使用移动图层功能,在 python 中编写 photoshop 脚本。 Layers are ending up inside of other layer sets rather than after them, etc.层最终在其他层集内部而不是在它们之后,等等。

The native command to do this in Photoshop's Javascript is:在 Photoshop 的 Javascript 中执行此操作的本机命令是:

layer.move(relativeObject, insertionLocation)

where insertionLocation can be:其中 insertionLocation 可以是:

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

My current code looks like this:我当前的代码如下所示:

@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)

And here were my guesses at the values of the constants, which are apparently wrong:这是我对常量值的猜测,这显然是错误的:

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

I've tried doing a trace on the native values for those constants, but Photoshop does a good job of hiding those.我已经尝试对这些常量的原始值进行跟踪,但 Photoshop 可以很好地隐藏它们。 I've also tried Action Manager code, but I'm finding it a bit difficult to understand.我也尝试过 Action Manager 代码,但我发现它有点难以理解。

Does anyone know of a dependable way to move layers in photoshop with python?有谁知道使用 python 在 photoshop 中移动图层的可靠方法? Action Manager code preferred, but not necessary.首选 Action Manager 代码,但不是必需的。

Hi you can use these methods instead of.Move()您好,您可以使用这些方法代替 .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

where:在哪里:

  • RelativeObject = is the relative Layer reference RelativeObject = 是相对图层引用
  • Container = is the document where the layers are Container = 是图层所在的文档

example例子

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)

You can find all those constant values you're looking for here https://github.com/lohriialo/photoshop-scripting-python/blob/master/api_reference/photoshop_2020.py#L243您可以在这里找到所有这些常量值https://github.com/lohriialo/photoshop-scripting-python/blob/master/api_reference/photoshop_2020.py#L243

psPlaceInside                 =0          # from enum PsElementPlacement
psPlaceAtBeginning            =1          # from enum PsElementPlacement
psPlaceAtEnd                  =2          # from enum PsElementPlacement
psPlaceBefore                 =3          # from enum PsElementPlacement
psPlaceAfter                  =4          # from enum PsElementPlacement

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM