Reputation: 35
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.
Upvotes: 2
Views: 1586
Reputation: 842
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
Upvotes: 0
Reputation: 57
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:
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)
Upvotes: 0