Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.0 kB
1
Indexable
Never
// Get the selected layer
var selectedLayer = app.project.activeItem.selectedLayers[0];

// Duplicate the layer
var duplicatedLayer = selectedLayer.duplicate();

// Apply the "Find Edges" effect to the duplicated layer
var edgesEffect = duplicatedLayer.Effects.addProperty("Find Edges");

// Create a new solid layer to use as the mask layer
var maskLayer = app.project.items.addSolid([1, 1, 1], "Mask Layer", selectedLayer.width, selectedLayer.height, selectedLayer.pixelAspect);

// Apply the "Alpha Matte" effect to the duplicated layer with the mask layer as the matte layer
var alphaMatteEffect = selectedLayer.Effects.addProperty("Alpha Matte");
alphaMatteEffect.property("Matte Layer").setValue(maskLayer);

// Loop through each pixel in the duplicated layer to find the isolated areas
for (var y = 0; y < duplicatedLayer.height; y++) {
  for (var x = 0; x < duplicatedLayer.width; x++) {
    var pixel = duplicatedLayer.sampleImage([x, y], [1, 1], true);
    if (pixel[0] > 0) {
      // Create a new layer for each isolated area
      var isolatedLayer = selectedLayer.duplicate();
      isolatedLayer.name = "Isolated Layer " + x + "-" + y;

      // Apply a mask to the isolated layer using the mask layer
      var mask = isolatedLayer.Masks.addProperty("Mask");
      mask.property("Mask Path").setValue([[x, y], [x+1, y], [x+1, y+1], [x, y+1]]);
      mask.property("Mask Feather").setValue([10, 10]);
      mask.property("Mask Expansion").setValue(1);

      // Set the track matte of the isolated layer to "Alpha Matte"
      isolatedLayer.moveAfter(selectedLayer);
      var isolatedLayerAlphaMatteEffect = isolatedLayer.Effects.addProperty("Alpha Matte");
      isolatedLayerAlphaMatteEffect.property("Matte Layer").setValue(maskLayer);

      // Set the position of the isolated layer to the center of the isolated area
      var isolatedLayerPosition = isolatedLayer.property("Position");
      isolatedLayerPosition.setValue([x + 0.5, y + 0.5]);
    }
  }
}

// Remove the duplicated layer
duplicatedLayer.remove();