I do not have much experience with UXP and English is not my first language, so please forgive me if I am writing in a text that is difficult to understand…
I am trying to create a plugin in Photoshop that will duplicate multiple layers individually in the same selection.
I have a selection on each active layer, so I was wondering if there is a way to duplicate the selection repeatedly on each active layer.
However, when I try to duplicate a layer based on a selection, the pixels are expaned to that area.
Maybe just for clarification, are you trying to 1) take the pixels from multiple layers and put them on new layers or 2) copy the pixels from one layer onto multiple different layers to replace what is on that layer, or 3) something else?
and 2) could be accomplished using the batchPlay code from Alchemist, I think, as these two options sound like a copy-and-paste type maneuver.
Self resolved.
After duplicating the layer, I resized the selected area to the correct size by using the layer’s scale function to resize it to the correct size.
After that, I moved the newly created layer to the correct position and got the same behavior as when copying and pasting the area.
You certainly can do this with the Imaging API. I would be remiss if I didn’t mention that just the steps above could also be done by:
Select the target layer.
Set the selection.
Copy
Paste
→ Repeat
Also, you can use app.activeDocument.selection.bounds instead of imaging.getSelection.
Lastly, the stretching you are seeing is due to using the targetSize option for getPixels. Without that option, you won’t see the scaling. You will have to account for the trimmed buffer as it will only contain the smallest rectangle that encompasses the non-empty pixels.
I will be updating the documentation to clarify that targetSize is for scaling.