ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Thanks to this forum, my first script !

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Avinash Ramanath
Thanks to this forum, my first script !
on Dec 18, 2018 at 3:06:25 am

Dear Friends,
After almost a year since I started learning JavaScripting for After Effects and with a lot of help from folks at this forum, I have put together my first script. Since so many ideas, tips and hints are from this forum's discussions I feel I should share the code here so that it may be of help to someone. Special thanks to:

Dan Ebberts
Nate Lovell - (youtube.com/user/peregrinecommando99)
http://www.motionscript.com
http://www.redefinery.com

The script does the following in the following order:

1. Centers the anchor point of the layer
2. Gets the layer's width and height via sourceRectAtTime() method
3. Pre-comps the layer and sets the Pre-comp's dimension got from step 2
4. Centers anchor point of the child layer and centers it to screen
5. Sets position keyframes to the child layer to animate it from out of the screen to the center of the screen
6. Duration is converted to frames (commented currently)

Please have a look and suggest changes and point out mistakes.

Thanks

{
app.beginUndoGroup("Crop and move");
var heroLayer = app.project.activeItem;
var myLayer = heroLayer.selectedLayers[0];
centerAnchorPoint(myLayer);

function centerAnchorPoint(layer) {
var comp = layer.containingComp;
var curTime = comp.time;
var layerAnchor = layer.anchorPoint.value;
/* find center by bounding box of the layer */
var x = layer.sourceRectAtTime(curTime, false).width / 2;
var y = layer.sourceRectAtTime(curTime, false).height / 2;
/* we need this for text layer */
x += layer.sourceRectAtTime(curTime, false).left;
y += layer.sourceRectAtTime(curTime, false).top;
var xAdd = (x - layerAnchor[0]) * (layer.scale.value[0] / 100);
var yAdd = (y - layerAnchor[1]) * (layer.scale.value[1] / 100);
/* set new anchor point*/
layer.anchorPoint.setValue([x, y]);
var layerPosition = layer.position.value;
/* fix position with adjustments */
layer.position.setValue([layerPosition[0] + xAdd, layerPosition[1] + yAdd, layerPosition[2]]);
};
//need to center the layer to the comp currently
// select the active item in the project window
// and make sure it's a comp
var myComp = app.project.activeItem;
if (myComp instanceof CompItem) {
// make sure one or more layers are selected
var myLayers = myComp.selectedLayers;
if (myLayers.length > 0) {
// set new comp's default in and out points to those of first layer
var newInPoint = myLayers[0].inPoint;
var newOutPoint = myLayers[0].outPoint;
// create new comp name of "comp " plus name of first layer
var newCompName = "Cropped ";
var layerName = myLayers[0].name;
if (layerName.length > 26) {
layerName = layerName.substring(0, 26);
}
newCompName += layerName;
//alert(newCompName);
// "precompose" expects an array of layer indices
var layerIndices = new Array();
for (var i = 0; i &lt; myLayers.length; i++) {
layerIndices[layerIndices.length] = myLayers[i].index;
var origPosition = myLayers[i].position.value;
//alert(origPosition);
var currLayer = myLayers[i].sourceRectAtTime(0, false);
var currLayerWidthFloat = currLayer.width;
var layerWidth = Math.round(currLayerWidthFloat);
// alert(layerWidth);
var currLayerHeightFloat = currLayer.height;
var layerHeight = Math.round(currLayerHeightFloat);

if (myLayers[i].inPoint &lt; newInPoint) newInPoint = myLayers[i].inPoint;
if (myLayers[i].outPoint > newOutPoint) newOutPoint = myLayers[i].outPoint;
}
// create the new comp
var newComp = myComp.layers.precompose(layerIndices, newCompName, true);
// set in and out points of new comp
var preCompLayer = myComp.selectedLayers[0];
preCompLayer.inPoint = newInPoint;
preCompLayer.outPoint = newOutPoint;
newComp.width = layerWidth;
newComp.height = layerHeight;
var myComp;
for (var i = 1; i &lt;= app.project.numItems; i++) {
if ((app.project.item(i) instanceof CompItem) && (app.project.item(i).name === newCompName)) {

var layerName = app.project.item(i).layer(1);

//start center anchor point
var mainComp = newCompName;
var mainLayer = layerName;
//start of Center Anchor point code
var myLayer = mainLayer;
centerAnchorPoint(myLayer);

function centerAnchorPoint(layer) {
var comp = layer.containingComp;
var curTime = comp.time;
var layerAnchor = layer.anchorPoint.value;
/* find center by bounding box of the layer */
var x = layer.sourceRectAtTime(curTime, false).width / 2;
var y = layer.sourceRectAtTime(curTime, false).height / 2;
/* we need this for text layer */
x += layer.sourceRectAtTime(curTime, false).left;
y += layer.sourceRectAtTime(curTime, false).top;
var xAdd = (x - layerAnchor[0]) * (layer.scale.value[0] / 100);
var yAdd = (y - layerAnchor[1]) * (layer.scale.value[1] / 100);
/* set new anchor point*/
layer.anchorPoint.setValue([x, y]);
var layerPosition = layer.position.value;
/* fix position with adjustments */
layer.position.setValue([layerPosition[0] + xAdd, layerPosition[1] + yAdd, layerPosition[2]]);
};
//end center anchor point
// start Centralise position of the layer
var currLayer = layerName.sourceRectAtTime(0, false);
var currLayerHeightFloat = currLayer.height;
var layerHeight = Math.round(currLayerHeightFloat);
var currLayerWidthFloat = currLayer.width;
var layerWidth = Math.round(currLayerWidthFloat);
var myPosition = layerName.property("position");
var positionProp = layerName.property("position");
//var positionValue = positionProp.valueAtTime(1, false);
// var posX = positionValue[0];
// var posY = positionValue[1];
// var keyframeInterval = 1;
// // alert(keyframeInterval);
// var duration = keyframeInterval;
// var durationInFrames = keyframeInterval * mainComp.frameDuration;
var down = 3;
var up = -1;
myPosition.setValueAtTime(0, [layerWidth / 2, layerHeight / 2 * up]);
myPosition.setValueAtTime(1, [layerWidth / 2, layerHeight / 2]);
var croppedCompPosition = layerName.containingComp.position;
if (myComp.layer(newCompName) == null) {
// stuff to do if layer doesn't exist
alert(" not found");
} else {
// stuff to do if layer does exist
var myNewPosition = myComp.layer(newCompName);
myNewPosition.position.setValue(origPosition);
}

// End Centralise position of the layer
}
}
} else {
alert("select at least one layer to precompose.");
}
} else {
alert("please select a composition.");
}
app.endUndoGroup();
}


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]