Copy link to clipboard
Copied
Is there an easy way to set the anchor point of a layer right in its center using script?
Currently, my script adds text layers to the active composition and styles the text. The things is I need the anchor point to be straight in the middle of the layer to make some position calculations.
By default, it seems to be set to the center/bottom of the text. (X/Y)
Thanks in advance
For some reason, the script I pasted earlier is missing the first line and I can't edit it, so there it is again:
...function centerAnchorPoint( layer ){
var comp = layer.containingComp;
var curTime = comp.time;
var layerAnchor = layer.anchorPoint.value;
var x;
var y;
try{
x = layer.sourceText.value.boxTextSize[0]/2;
y = layer.sourceText.value.boxTextSize[1]/2;
x += layer.sourceText.value.boxTextPos[0];
y += layer.sourceText.value.boxTextPos[1];
}catc
Copy link to clipboard
Copied
var myComp = app.project.activeItem;
var myLayer = myComp.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] ]);
};
Copy link to clipboard
Copied
Works like a charm! I will keep this for sure! You are awesome.
Copy link to clipboard
Copied
In case you need to use it with box text, you will need to substitute boxTextSize for sourceRectAtTime
Copy link to clipboard
Copied
I did try it with a text layer and it seemed to work fine. I did see a little offset of a couple pixels to the top, that might fix it, I'll try it and we'll see if that changes something.
Edit: After looking at the code again I think everything is just fine. it's already using sourceRectAtTime.
Copy link to clipboard
Copied
Right, I was just pointing out that point text (if you click the text icon and start typing) uses sourceRectAtTime, while boxed text (if you click the text icon and then draw a text box before typing) uses boxTestSize
Copy link to clipboard
Copied
Oh, thanks for the information, I'll see if I can tweak the function to choose the right one all by itself. (if so I'll repost it here)
Copy link to clipboard
Copied
I just brought it up because this same issue vexed me for months when I was new to ExtendScript. Drilling down to the boxTextSize property can be a headache, but this thread proved invaluable for me as a reference when setting up my function.
Copy link to clipboard
Copied
I just fixed it up and tested it with these layer types:
Null object, Adjustement layer, solid, text box, unboxed text
There's the final code:
var comp = layer.containingComp;
var curTime = comp.time;
var layerAnchor = layer.anchorPoint.value;
var x;
var y;
try{
x = layer.sourceText.value.boxTextSize[0]/2;
y = layer.sourceText.value.boxTextSize[1]/2;
x += layer.sourceText.value.boxTextPos[0];
y += layer.sourceText.value.boxTextPos[1];
}catch(e){
x = layer.sourceRectAtTime(curTime, false).width/2;
y = layer.sourceRectAtTime(curTime, false).height/2;
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);
layer.anchorPoint.setValue([ x, y ]);
var layerPosition = layer.position.value ;
layer.position.setValue([ layerPosition[0] + xAdd, layerPosition[1] + yAdd, layerPosition[2] ]);
};
Thanks for your quick answers! You guys rock!
Copy link to clipboard
Copied
Looks good. I am partial to putting it in a switch or if ... else statement but above should work just as well
Copy link to clipboard
Copied
I tried to do it this way but was unable to obtain an undefined value. Instead AE always crashed and gave me an error.
text box -> can use both boxtextSize and sourceRectAtTime. (sourceRectAtTime -> wrong value)
unboxed text -> can only use sourceRectAtTime, otherwise will crash AE and not return undefined.
That's why I used a try catch.
If you know way that works to check if the text object has the boxTextSize property without crashing Ae and without using a try/catch, I'll sure use it
Copy link to clipboard
Copied
For some reason, the script I pasted earlier is missing the first line and I can't edit it, so there it is again:
function centerAnchorPoint( layer ){
var comp = layer.containingComp;
var curTime = comp.time;
var layerAnchor = layer.anchorPoint.value;
var x;
var y;
try{
x = layer.sourceText.value.boxTextSize[0]/2;
y = layer.sourceText.value.boxTextSize[1]/2;
x += layer.sourceText.value.boxTextPos[0];
y += layer.sourceText.value.boxTextPos[1];
}catch(e){
x = layer.sourceRectAtTime(curTime, false).width/2;
y = layer.sourceRectAtTime(curTime, false).height/2;
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);
layer.anchorPoint.setValue([ x, y ]);
var layerPosition = layer.position.value ;
layer.position.setValue([ layerPosition[0] + xAdd, layerPosition[1] + yAdd, layerPosition[2] ]);
};
Copy link to clipboard
Copied
In my function, here was how I set it up where you assume theLayer is an instanceof TextLayer:
if(theLayer.property("Source Text").value.pointText){
//do calculations using sourceRectAtTime
} else {
//do calculations using boxTextSize
}
Copy link to clipboard
Copied
how do i add the script
Copy link to clipboard
Copied
Thanks, very helpful
Copy link to clipboard
Copied
Our new extension Pins & Boxes offers a very easy and convenient way to do that using expressions.
See this tutorial:
Find more inspiration, events, and resources on the new Adobe Community
Explore Now