Skip to main content
Inspiring
September 12, 2017
Resuelto

Centering the anchor point

  • September 12, 2017
  • 4 respuestas
  • 12375 visualizaciones

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

Este tema ha sido cerrado para respuestas.
Mejor respuesta de jonathanp53717501

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


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

};

4 respuestas

Mathias Moehl
Community Expert
Community Expert
December 14, 2018

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
avinashr60929084
Inspiring
December 14, 2018

Thanks, very helpful

TryxDesigns
Participant
May 24, 2018

how do i add the script

Alex White
Legend
September 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] ]);

   

   

};

Inspiring
September 12, 2017

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

tardigrade01
Inspiring
September 12, 2017

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