简体   繁体   中英

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. 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:

layer.move(relativeObject, insertionLocation)

where insertionLocation can be:

ElementPlacement.INSIDE, ElementPlacement.PLACEATBEGINNING, ElementPlacement.PLACEATEND, ElementPlacement.PLACEBEFORE, or 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. I've also tried Action Manager code, but I'm finding it a bit difficult to understand.

Does anyone know of a dependable way to move layers in photoshop with python? Action Manager code preferred, but not necessary.

Hi you can use these methods instead of.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
  • Container = is the document where the layers are

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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