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

Centering the anchor point

Explorer ,
Sep 12, 2017 Sep 12, 2017

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

TOPICS
Scripting
12.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

correct answers 1 Correct answer

Explorer , Sep 13, 2017 Sep 13, 2017

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

...
Translate
Enthusiast ,
Sep 12, 2017 Sep 12, 2017

    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] ]);

   

   

};

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
Explorer ,
Sep 12, 2017 Sep 12, 2017

Works like a charm! I will keep this for sure! You are awesome.

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 ,
Sep 12, 2017 Sep 12, 2017

In case you need to use it with box text, you will need to substitute boxTextSize for sourceRectAtTime

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
Explorer ,
Sep 12, 2017 Sep 12, 2017

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.

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 ,
Sep 12, 2017 Sep 12, 2017

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

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
Explorer ,
Sep 12, 2017 Sep 12, 2017

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)

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 ,
Sep 12, 2017 Sep 12, 2017

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.

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
Explorer ,
Sep 12, 2017 Sep 12, 2017

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!

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 ,
Sep 12, 2017 Sep 12, 2017

Looks good. I am partial to putting it in a switch or if ... else statement but above should work just as well

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
Explorer ,
Sep 13, 2017 Sep 13, 2017

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

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
Explorer ,
Sep 13, 2017 Sep 13, 2017

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] ]);

};

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 ,
Sep 13, 2017 Sep 13, 2017

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

}

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 ,
May 24, 2018 May 24, 2018

how do i add the script

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
Explorer ,
Dec 13, 2018 Dec 13, 2018

Thanks, very helpful

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 Expert ,
Dec 14, 2018 Dec 14, 2018
LATEST

Our new extension Pins & Boxes offers a very easy and convenient way to do that using expressions.

See this tutorial:

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
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