Copy link to clipboard
Copied
Maybe this has been addressed but I can't find info about it. Here's the description of my problem:
1.- I create a box text and I can calculate the text's anchor point because is right in the box center.
2.- I change the box width and/or height. The anchor point remains in the original place.
3.- There's no way I can get the box anchor point coordinates, neither absolute nor relatives to the text box.
If After Effects can remember where the original anchor point was (because it doesn't change after resizing the text box), this information must be stored somewhere. But where?
Copy link to clipboard
Copied
I found the answer by myself, hehehe:
var srcRect = layer.sourceRectAtTime(0, false); <--- this gives you the layer's source rectangle at time 0. After that, you'll have in srcRect.left and srcRect.top the offset from the top-left corner to the layer's anchor point.
However, this is not exactly what I needed... yet. There's still a small difference with point texts: point texts have the anchor point UNDER the text line, and the position I got for box texts was right above the first line.
So the "y" position for my text would be srcRect.top - layer.sourceText.value.fontSize
Copy link to clipboard
Copied
Hello. I can be with the same problem, you can write the complete expression for that I see, please?
Thanks.
Copy link to clipboard
Copied
The only way I've found to try to sort of figure out this "magic" anchor offset so far is to create a new boxed text layer, copy all the settings of the sourceText.value + layer transformation values, and then calculate the difference between top/left of the two layers' .sourceRectAtTime() values... ( ! )
Copy link to clipboard
Copied
I wasn't aware you could use sourceRectAtTime for boxText, but only for point text. To get a box text's center, you could use the boxTextSize value (an array), so the horizontal center would be boxTextSize[0]/2 and the vertical center would be boxTextSize[1]/2.
from page 184 of the AE CS6 Scripting Guide:
TextDocument boxText attribute
textDocument.boxText Description
True if a text layer is a layer of paragraph (bounded) text; otherwise false.
Type
Boolean; read-only.
TextDocument boxTextSize attribute
textDocument.boxTextSize Description
The size of a paragraph (box) text layer as a [width, height] array of pixel dimensions.
Type
Array of two integers (minimum value of 1); read/write.
Also, c/o David Torno, UgQ, AgileUI, et al:
Copy link to clipboard
Copied
The problem with that is that if you resize the boxed text, the position and anchor stays the same, but are now "out of sync" with what you would expect them to be, i.e. the anchor is no longer at the center of the box, but at the OLD center of the box:
E.g. observe this:
Which shows the anchor in the center:
However, if you extend the text box both vertically and horizontally, you'll see that the anchor point and position stays the same, but the anchor is now not at the center any more:
So.. this is where the "magic" offset comes into place...
Again: the anchor and position is STILL the same, AND the .sourceRectAtTime() function returns THE SAME VALUES.
So, I had to do something fiddly like this to get the "hidden" offset:
function measurePhantomBoxTextOffset(sourceLayer)
{
function trim(s)
{
return (s || "").toString().replace(/^(?:\s*)(.*?)(?:\s*)$/, "$1");
}
// is it non-null, a text layer, and a boxed text?
if (sourceLayer != null && sourceLayer instanceof TextLayer && sourceLayer.sourceText.value.boxText)
{
var text = sourceLayer.sourceText.value;
// if the text is empty, we must first set something otherwise we're getting 0,0 for top,left
var wasEmpty = (trim(text.text).length == 0);
if (wasEmpty)
{
sourceLayer.sourceText.setValue("abcABC123");
text = sourceLayer.sourceText.value;
}
// Create a copy of it (where the anchor will be in the known centre position)
var layer = comp.layers.addBoxText(text.boxTextSize, text);
try
{
//AELayerUtils.copyTime(sourceLayer, layer); // apparently not required for measurement
//AELayerUtils.copyTransformation(sourceLayer, layer); // apparently not required for measurement
// This is not enough! (if some other font is selected, it will get the wrong value)
// layer.sourceText.setValue(sourceLayer.sourceText.value);
AELayerUtils.copyCharStyle(sourceLayer, layer);
var a = sourceLayer.sourceRectAtTime(sourceLayer.startTime, false);
var b = layer.sourceRectAtTime(sourceLayer.startTime, false)
return [ (a.left - b.left), (a.top - b.top), 0 ];
}
catch (err)
{
}
finally
{
layer.remove();
if (wasEmpty) sourceLayer.sourceText.setValue("");
}
}
// fallback for everything else
return [0, 0, 0];
}
(In this case AELayerUtils is just some helper methods)
Copy link to clipboard
Copied
Right. I was suggesting that you recalculate half the boxTextSize after resizing.
I am surprised that sourceRectAtTime is accurate for your boxed text, because it never has for me. If it's working, though, great.
Copy link to clipboard
Copied
Hm, I've not had a problem with it so far, but I only got into AE scripting in CC 2014..
PS: the half boxTextSize wasn't really any issue for me (even though the original post was about it): I am doing some custom text layout to get emoji support hacked in there somehow (in lack of multi-formatting in TextDocument) and the position was being thrown off with boxed texts that had been resized
Now with this I'm on track again ..momentarily; some new issue popped up now: Another day of chaos an AE scripting hehe
Copy link to clipboard
Copied
gotcha. good luck
Copy link to clipboard
Copied
was wondering how to get the emoji thing going? could u provide some insight? ae scripting .....
Find more inspiration, events, and resources on the new Adobe Community
Explore Now