Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Control position and scale from another comp

  • Control position and scale from another comp

    Posted by Steve Sierra on March 11, 2018 at 1:30 pm

    Hello everyone,

    In a script, I am trying to have multiple layers (let’s call them “MaskedLayers”)in a comp (“PreComp”) masked by a layer(“MasterLayer”) in PreComp’s parent comp(“MasterComp”).

    There will be multiple preComps in MasterComp.
    PreComps won’t be the same size.
    PreComp and MasterComp won’t be the same size.
    PreComps have to be able to move, rotate and scale like any normal layer, without altering the masking continuity…

    I copied an instance of MasterLayer above each maskedLayer in PreComp, linked them to a null (“PreCompNull”)set Alpha matte.
    Now, I am trying to drive PreCompNull with MasterLayer, regardless of PreComp’s original pos, rot and scale as well as their changing over time.

    I’ve managed to have the position part solved, regardless of PreComps original and time varying positions, but as soon as PreComp’s scale or rotation are altered, the masking continuity breaks…
    I tried different things in scale, but it goes pretty crazy ???? Haven’t tried anything on rotation yet.

    Here’s the logic of the expression I put in PreCompNull’s position :

    var MasterLayer = comp(“MasterComp”).layer(“MasterLayer”);
    var PreCompLayer = comp(“MasterComp”).layer(“PreComp”);
    var MaskedLayer = thisComp.layer(“MaskedLayer”);
    a = MasterLayer.transform.position;
    b = PreCompLayer .transform.position;
    c = b – a;
    d = MaskedLayer.transform.position;
    d – c;

    Thanks in advance for any help !
    Cheers !

    Steve Sierra replied 8 years, 4 months ago 1 Member · 3 Replies
  • 3 Replies
  • Steve Sierra

    March 13, 2018 at 8:35 am

    Hello again,

    I made a few changes: instead of the “MaskedLayer” position, I used the center of “PreComp”. (I’m thinking I should maybe use “PreCompLayer”s anchorPoint ?)
    I managed to get both position and rotation to work, but I still can’t figure out how to incorporate “preCompLayer” scale changes.
    I think I have to use preCompLayer’s scale and maybe the size ratio between MasterComp andPreComp in my expression… but I don’t know where and how ?

    Any help would be greatly appreciated !
    Here’s my new expression for PreCompNull’s position:

    preCompLayer=comp(“MasterComp”).layer(“PreCompLayer”);
    a = preCompLayer.transform.position;
    s = preCompLayer.transform.scale;
    b = comp(“MasterComp”).layer(“MasterLayer”).transform.position;
    d =[comp(“PreComp”).width/2comp(“PreComp”).height/2];
    c = fromWorld(a)-fromWorld(b);
    lightPos=d-c;
    preRot= -preCompLayer.transform.rotation;
    eRot=degreesToRadians(preRot);
    eCos=Math.cos(eRot);
    eSin=Math.sin(eRot);
    u=lightPos-d;
    x=eCos*u[0]-eSin*u[1];
    y=eSin*u[0]+eCos*u[1];
    [d[0]+x, d[1]+y];

  • Steve Sierra

    March 13, 2018 at 8:40 am

    Oops, forgot a comma :

    preCompLayer=comp(“MasterComp”).layer(“PreCompLayer”);
    a = preCompLayer.transform.position;
    s = preCompLayer.transform.scale;
    b = comp(“MasterComp”).layer(“MasterLayer”).transform.position;
    d =[comp(“PreComp”).width/2, comp(“PreComp”).height/2];
    c = fromWorld(a)-fromWorld(b);
    lightPos=d-c;
    preRot= -preCompLayer.transform.rotation;
    eRot=degreesToRadians(preRot);
    eCos=Math.cos(eRot);
    eSin=Math.sin(eRot);
    u=lightPos-d;
    x=eCos*u[0]-eSin*u[1];
    y=eSin*u[0]+eCos*u[1];
    [d[0]+x, d[1]+y];

  • Steve Sierra

    March 13, 2018 at 1:48 pm

    Hi again,

    Ok, I think I got it…
    Here’s a recap :
    Let’s say you have layer(“Alayer”) in “Acomp”.
    in “Bcomp”, you have layer(“Acomp”) and layer(“Blayer”).
    “ALayer’s” AnchorPoint and Position have to be set at the center of the comp.
    No parenting anywhere.

    If you want to drive layer(“Alayer”) with layer(“Blayer”),
    regardless of :
    -“Acomp” and “Bcomp” size difference
    -layer(“Acomp”)’s pos, scale, rot and anchor point’s location

    put this expression in the Anchor Point property of “Alayer” :

    //Raw Variables
    AcompLayer=comp(“Bcomp”).layer(“Acomp”);
    d = AcompLayer.transform.anchorPoint;
    s = AcompLayer.transform.scale;
    h = AcompLayer.transform.position;
    g = comp(“Bcomp”).layer(“Blayer”).transform.position;
    //
    //Counter Scale
    function CounterScale(Pos, s){
    return [Pos[0]*100/s[0], Pos[1]*100/s[1]];
    };
    a = CounterScale(h, s);
    b = CounterScale(g, s);
    c = fromWorld(a) – fromWorld(b);
    scaledPos = d + c;
    //
    //Counter Rotation
    preRot = -AcompLayer.transform.rotation;
    eRot = degreesToRadians(preRot);
    eCos = Math.cos(eRot);
    eSin = Math.sin(eRot);
    u = scaledPos – d;
    x = eCos*u[0] – eSin*u[1];
    y = eSin*u[0] + eCos*u[1];
    [(d[0] + x), (d[1] + y)];

    Put this in “Alayer’s” Scale property :

    //Counter Scale
    a = comp(“Bcomp”).layer(“Blayer”).transform.scale;
    b = comp(“Bcomp”).layer(“Acomp”).transform.scale;
    c = [a[0] * 100 / b[0], a[1] * 100 / b[1]];
    //
    //Add Controller Scale
    d = comp(“Bcomp”).layer(“Blayer”).transform.scale;
    [c[0] * d[0] / 100, c[1] * d[1] / 100];

    And this in “Alayer’s” Rotation property :

    a = comp(“Bcomp”).layer(“Blayer”).transform.rotation;
    b = comp(“Bcomp”).layer(“Acomp”).transform.rotation;
    a – b;

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy