Activity › Forums › Adobe After Effects Expressions › Using a layer as a boundary for other layers
-
Using a layer as a boundary for other layers
Posted by Shane Shornock on October 25, 2023 at 1:32 pmHello all, I have a design that requires that I put photos of people in the state they are from. I have 800 photos in total, and I wonder if I could create a script that takes selected layers, places them on top of a layer of a state, and organizes them in a grid whose boundary is the state.
Shane Shornock replied 11 months, 2 weeks ago 2 Members · 3 Replies -
3 Replies
-
Tom Morton
October 25, 2023 at 3:06 pmwell anything is possible… but you haven’t given much information. Do you have a database of which photos are part of which state? Are the photos names randomly or can the names be used to define where the photo goes. If you have a database, what format is it in? If you don’t have a database, what could the script use to determine which photo / layer goes on which state?
Also, just to be sure… how is this video related, ie why do you need After Effects? If it’s a static image, I would lean towards, Indesign or Illustrator, but you haven’t described whether there’s some animation involved or anything? Any animation or movement would also affect how the script operates.
Let us know!
-
Shane Shornock
October 25, 2023 at 5:35 pm -
Shane Shornock
October 25, 2023 at 5:36 pmif (app.project.activeItem.selectedLayers.length < 2) {
alert(“Please select the boundary layer and at least one layer to adjust.”);
} else {
var comp = app.project.activeItem;// Assuming the first selected layer is the boundary layer
var boundaryLayer = comp.selectedLayers[0];
var boundaryRect = boundaryLayer.sourceRectAtTime(comp.time, false);// Calculate the number of rows and columns
var numLayers = comp.selectedLayers.length – 1; // subtract 1 for the boundary layer
var numRows = Math.ceil(Math.sqrt(numLayers));
var numCols = Math.ceil(numLayers / numRows);var xOffset = boundaryRect.width / (numCols + 1);
var yOffset = boundaryRect.height / (numRows + 1);var currentRow = 1;
var currentCol = 1;// Loop through the other selected layers and position them in a grid
for (var i = 1; i <= numLayers; i++) {
var layerToAdjust = comp.selectedLayers[i];
var layerRect = layerToAdjust.sourceRectAtTime(comp.time, false);// Calculate the scale factors for width and height
var scaleX = (xOffset / layerRect.width) * 100;
var scaleY = (yOffset / layerRect.height) * 100;// Use the smaller scale factor to ensure the layer fits inside its grid cell
var newScale = Math.min(scaleX, scaleY);
layerToAdjust.scale.setValue([newScale, newScale]);// Position layer in the grid
var newX = boundaryLayer.position.value[0] – (boundaryRect.width / 2) + (xOffset * currentCol);
var newY = boundaryLayer.position.value[1] – (boundaryRect.height / 2) + (yOffset * currentRow);
layerToAdjust.position.setValue([newX, newY]);// Move to next position in the grid
currentCol++;
if (currentCol > numCols) {
currentCol = 1;
currentRow++;
}
}alert(“Layers adjusted to grid successfully!”);
// Create an undo group for this action
app.beginUndoGroup(“Create Null and Parent Selected Layers”);// Get the active composition and its selected layers
var comp = app.project.activeItem;
var selectedLayers = comp.selectedLayers;// Create the null object
var nullLayer = comp.layers.addNull();
nullLayer.name = “Parent Null”;// Parent the selected layers to the null
for (var i = 1; i < selectedLayers.length; i++) {
selectedLayers[i].parent = nullLayer;
}// End the undo group
app.endUndoGroup();}
Reply to this Discussion! Login or Sign Up