Get all clipped layers from the base layer?

Is there an elegant way to get all clipped layers from the selected layer?
Look at the following image:

image

If I select the base layer, in this case, “Layer 1”, how can I get all the layers that are clipped to it? I tried to use the batchPlay to make a group but then I saw that it requires a layerSectionStart and end.
In the end, I would like to perform some operations on the base layer which can involve losing all clipped layers, and then I would like to clip them all back to the selected layer.

It would be great if the DOM supported this, so we could write something like layer.clippedLayers to get the array of clipped layers. (similar to layer.linkedLayers). In the ExtendScript I got those by performing several operations:

  1. Make a group
  2. Loop the group and get all layers that are clipped from the group (store them into an array)
  3. Ungroup

and then after the operations are complete clip them back to the layer:

  1. select a layer by id from the array
  2. clip the layer down

Never mind I found a solution. This one returns the array of clipped layers from the selected layer:

import { app } from "photoshop";
import { Layer } from "photoshop/dom/Layer";

export const getAllClippedLayers = (selectedLayer: Layer) => {
  // Layer parent:
  // 1. group (if the layer is located inside the group)
  // 2. document
  const layerParent = selectedLayer.parent
    ? selectedLayer.parent
    : app.activeDocument;
  return layerParent.layers.filter((layer) => layer.isClippingMask);
};

At the very end of the operation if there are for example two selected layers located inside the same group, and they both have some clipped layers to them, then that array would need to be filtered again to remove the duplicates and leave only the unique values with only the layers that need to be clipped back. Something like this:

const layersToClipBack = allClippedLayers.filter(
    (layer, index, self) =>
      self.findIndex((item) => item.id === layer.id) === index && !layer.isClippingMask
  );
2 Likes

This is one of the things that I would really like to see in DOM. Since clipped layers are very common but getting its array is unnecessarily complicated.

This is the solution I came up with using what’s available right now, but yes something like layer.clippedLayers would be the best. Agree.