Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Getting a box text anchor point after resizing it

New Here ,
Mar 21, 2014 Mar 21, 2014

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?

TOPICS
Scripting
2.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 24, 2014 Mar 24, 2014

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 28, 2015 May 28, 2015

Hello. I can be with the same problem, you can write the complete expression for that I see, please?

Thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 06, 2016 Apr 06, 2016

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... ( ! )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 06, 2016 Apr 06, 2016

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:


ADBE Text Document boxTextSize attribute

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 06, 2016 Apr 06, 2016

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:

ae 0 text.png

Which shows the anchor in the center:

ae 0 anchor.png

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:

ae 1 text.png

ae 1 anchor.png

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)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 06, 2016 Apr 06, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 06, 2016 Apr 06, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 06, 2016 Apr 06, 2016

gotcha. good luck

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 14, 2017 Feb 14, 2017
LATEST

was wondering how to get the emoji thing going? could u provide some insight? ae scripting .....

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines