loonghao / photoshop-python-api

Python API for Photoshop.
https://loonghao.github.io/photoshop-python-api/
MIT License
604 stars 68 forks source link

Cannot move a layer image object with actions scripting #256

Open KennethSidibe opened 1 year ago

KennethSidibe commented 1 year ago

Hello,

I am currently working on a project where I need to manipulate Photoshop files using Python. Specifically, I'm trying to move a layer image object in a Photoshop document. However, I am running into some trouble with this task and could use some guidance.

Here is a snippet of the code I'm currently using:

idTrnf = ps.app.charIDToTypeID("Trnf")
            mainEventsDetails = ps.ActionDescriptor()

            idFTcs = ps.app.charIDToTypeID("FTcs")
            idQCSt = ps.app.charIDToTypeID("QCSt")
            idQcsa = ps.app.charIDToTypeID("Qcsa")

            mainEventsDetails.putEnumerated(idFTcs, idQCSt, idQcsa)

            offsetDetails = ps.ActionDescriptor()
            idHrzn = ps.app.charIDToTypeID("Hrzn")
            idPxl = ps.app.charIDToTypeID("#Pxl")
            offsetDetails.putUnitDouble(idHrzn, idPxl, 1909.000000)

            idVrtc = ps.app.charIDToTypeID("Vrtc")
            idPxl = ps.app.charIDToTypeID("#Pxl")
            offsetDetails.putUnitDouble(idVrtc, idPxl, 30.000000)

            idOfst = ps.app.charIDToTypeID("Ofst")
            idOfst2 = ps.app.charIDToTypeID("Ofst")

            mainEventsDetails.putObject(idOfst, offsetDetails)

            ps.app.executeAction(idTrnf, mainEventsDetails, 1)

When the putObject method is invoked, I'm encountering a COMException error:

line 745, in _invoke self.__com_Invoke(memid, riid_null, lcid, invkind, _ctypes.COMError: (-2147220261, None, (None, None, None, 0, None))

(-2147212704, None, (None, None, None, 0, None)).

I believe the issue lies in the putObject method, where I'm attempting to adjust the offsets of the image object. However, I'm not certain about the cause of this issue or how to resolve it.

I'd greatly appreciate any insight or advice on how to properly move a layer image object in Photoshop using actions scripting in Python.

Thank you!

Investigamer commented 1 year ago

The descriptor has no target, so it doesn't know what layer to perform the transform on. Here's the updated code, targeting the active layer:

idTrnf = ps.app.charIDToTypeID("Trnf")
mainEventsDetails = ps.ActionDescriptor()

# Add a target reference
ref = ps.ActionReference()
ref.putEnumerated(sID("layer"), sID("ordinal"), sID("targetEnum"))
mainEventsDetails.putReference(sID("target"),  ref)

idFTcs = ps.app.charIDToTypeID("FTcs")
idQCSt = ps.app.charIDToTypeID("QCSt")
idQcsa = ps.app.charIDToTypeID("Qcsa")
mainEventsDetails.putEnumerated(idFTcs, idQCSt, idQcsa)

offsetDetails = ps.ActionDescriptor()
idHrzn = ps.app.charIDToTypeID("Hrzn")
idPxl = ps.app.charIDToTypeID("#Pxl")
offsetDetails.putUnitDouble(idHrzn, idPxl, 1909.000000)

idVrtc = ps.app.charIDToTypeID("Vrtc")
idPxl = ps.app.charIDToTypeID("#Pxl")
offsetDetails.putUnitDouble(idVrtc, idPxl, 30.000000)

idOfst = ps.app.charIDToTypeID("Ofst")
idOfst2 = ps.app.charIDToTypeID("Ofst")
mainEventsDetails.putObject(idOfst, offsetDetails)

ps.app.executeAction(idTrnf, mainEventsDetails, 1)

There is also a simpler way this action could be performed, just get the layer you need to move and translate it with the API.

layer = app.activeDocument.artLayers.getByName("Layer Name")
layer.translate(1909, 30)