Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Scripting – Centre Mask on Shape Layer

  • Scripting – Centre Mask on Shape Layer

    Posted by Liam Carlin on March 7, 2022 at 10:17 am

    Hey Guys,

    I have created this snippet of code that creates a rectangle mask on any layer given it’s dimension. It works fine on all layers except Shape Layers and Text Layers. I can’t quite figure where I’m going wrong. It doesn’t centre when I thought it would. How do I center the mask?

    The image show’s how it’s not cantered.

    myLayer = app.project.activeItem.selectedLayers[i];

    newMask = myLayer.Masks.addProperty(“ADBE Mask Atom”);

    myProperty = newMask.property(“ADBE Mask Shape”);

    newMask.name = “Rectangle Mask”;

    newMask.maskMode = MaskMode.ADD;

    myShape = myProperty.value;

    myShape.vertices = [[myLayer.width,0],[0,0],[0,myLayer.height],[myLayer.width,myLayer.height]];

    myShape.inTangents = [[0,0],[0,0],[0,0],[0,0]];

    myShape.outTangents = [[0,0],[0,0],[0,0],[0,0]];

    myShape.closed = true;

    myProperty.setValue(myShape);

    Thanks,
    Liam

    Liam Carlin replied 4 years, 2 months ago 2 Members · 4 Replies
  • 4 Replies
  • Filip Vandueren

    March 7, 2022 at 12:10 pm

    Hi Liam, shape layers and textlayers – because they are rasterized continuosly- behave as if they always are a composition-sized layer.

    Using an exprerssio with sourceRectAtTime(), you can get the correct dimensions, although for some shapes it may or may not include the stroke-width.

    For example, see this question from yesterday:

    https://creativecow.net/forums/thread/mask-path-shape-to-always-match-text-total-scale-and-width/

    Mask Path Shape to always match text total scale and width

  • Liam Carlin

    March 8, 2022 at 4:20 am

    Thanks Filip,

    That makes sense. I have previously used sourceRectAtTime(); on text layers before.

    However I don’t actually know how to implement this. Are you able to see where I am going wrong.

    Thanks,
    Liam

    app.beginUndoGroup("Undo Rectangle Mask");

    myLayer = app.project.activeItem.selectedLayers[i];

    newMask = myLayer.Masks.addProperty("ADBE Mask Atom");

    myProperty = newMask.property("ADBE Mask Shape");

    newMask.name = "Rectangle Mask";

    newMask.maskMode = MaskMode.ADD;

    myShape = myProperty.value;

    sr= myLayer.sourceRectAtTime();

    pts= [

    [sr.left, sr.top],

    [sr.left+sr.width, sr.top],

    [sr.left+sr.width, sr.top+sr.height],

    [sr.left, sr.top+sr.height],

    ];

    createPath(pts,[],[],true);

    myLayer.sr;

    myShape.vertices = [[myLayer.width,0],[0,0],[0,myLayer.height],[myLayer.width,myLayer.height]];

    myShape.inTangents = [[0,0],[0,0],[0,0],[0,0]];

    myShape.outTangents = [[0,0],[0,0],[0,0],[0,0]];

    myShape.closed = true;

    myProperty.setValue(myShape);

    app.endUndoGroup();

  • Filip Vandueren

    March 8, 2022 at 7:19 am

    Hi Liam, the example I posted was an expression, the syntax and methods are a bit different.

    In a script it would be like this:

    app.beginUndoGroup("Rectangle Mask");

    myLayer = app.project.activeItem.selectedLayers[0];

    newMask = myLayer.Masks.addProperty("ADBE Mask Atom");

    myProperty = newMask.property("ADBE Mask Shape");

    newMask.name = "Rectangle Mask";

    newMask.maskMode = MaskMode.ADD;

    myShape = myProperty.value;

    sr= myLayer.sourceRectAtTime(myLayer.time,true);

    myShape.vertices = [

    [sr.left, sr.top],

    [sr.left+sr.width, sr.top],

    [sr.left+sr.width, sr.top+sr.height],

    [sr.left, sr.top+sr.height],

    ];

    myShape.closed = true;

    myProperty.setValue(myShape);

    app.endUndoGroup();

  • Liam Carlin

    March 8, 2022 at 11:22 am

    Thanks incredible, thank you Filip! It makes a lot of sense. Thanks for your time 👍

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