Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects [script] creates nulls as parents at the top, bottom, left, and right edges

  • [script] creates nulls as parents at the top, bottom, left, and right edges

    Posted by Shusaku Takagi on August 5, 2024 at 7:32 pm

    The following script creates nulls as parents at the top, bottom, left, and right edges of the selected multiple layers' rectangles.

    This script is effective for shape layers with anchor points at [0,0], but it does not work for layers with anchor points not at zero or for regular layers.

    Could someone resolve this issue? Thank you.




    var proj = app.project; // Ensure a composition is open var comp = proj.activeItem; // Ensure layers are selected var selectedLayers = comp.selectedLayers; // Prompt the user to select the position of the null object var positionOptions = ["center", "top", "bottom", "left", "right"]; var position = prompt("select position (center, top, bottom, left, right):", "right"); // Start undo group app.beginUndoGroup("Create Null at Selected Position"); // Calculate the bounding box of the selected layers var minX = Infinity, maxX = -Infinity; var minY = Infinity, maxY = -Infinity; for (var i = 0; i < selectedLayers.length; i++) { var layer = selectedLayers[i]; var layerBounds = layer.sourceRectAtTime(comp.time, false); var layerMinX = layer.position.value[0] + layerBounds.left; var layerMaxX = layerMinX + layerBounds.width; var layerMinY = layer.position.value[1] + layerBounds.top; var layerMaxY = layerMinY + layerBounds.height; minX = Math.min(minX, layerMinX); maxX = Math.max(maxX, layerMaxX); minY = Math.min(minY, layerMinY); maxY = Math.max(maxY, layerMaxY); } var centerX = (minX + maxX) / 2; var centerY = (minY + maxY) / 2; var centerZ = 0; var nullPosition = [centerX, centerY, centerZ]; switch (position) { case "top": nullPosition = [centerX, minY, centerZ]; break; case "bottom": nullPosition = [centerX, maxY, centerZ]; break; case "left": nullPosition = [minX, centerY, centerZ]; break; case "right": nullPosition = [maxX, centerY, centerZ]; break; } // Create a null object at the calculated position var nullLayer = comp.layers.addNull(); nullLayer.name = "Positioned Null (" + position + ")"; nullLayer.position.setValue(nullPosition); // Parent the selected layers to the null object for (var i = 0; i < selectedLayers.length; i++) { var layer = selectedLayers[i]; layer.parent = nullLayer; } // End undo group app.endUndoGroup();
    Shusaku Takagi replied 2 years ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy