Skip to main content
Inspiring
January 13, 2022
Answered

Rounded Rectangle Mask via Scripting

  • January 13, 2022
  • 2 replies
  • 2753 views

Hey Guys,

 

I am creating a script that applies a round rectangle mask to any layer that is selected. I have written the following code.

 

My question is, how do I make the vertices scale correctly to the size of any given comp? For example the code below has the vertices hard coded for a compostion of 1920x1080. How can I make this dynamic so it would work the same on an 8k comp for example.

 

Thanks,

Liam

 

btnMaskRoudRect.onClick = function() {
    if(app.project.activeItem == undefined || app.project.activeItem == null) {
        alert("Please Select a Composition.");
        return false;
     }
     if(app.project.activeItem.selectedLayers.length < 1) {
         alert("Please Select a Layer.");
     }
 else {
    maskRoudRect(app.project.activeItem.selectedLayers);
     }
     function maskRoudRect() {
        for(var i = 0; i < app.project.activeItem.selectedLayers.length; i++) {
        app.beginUndoGroup("Undo Rounded Rectangle Mask");
        myLayer = app.project.activeItem.selectedLayers[i];
        comp = app.project.activeItem;
        newMask = myLayer.Masks.addProperty("ADBE Mask Atom");
        newMask.maskMode = MaskMode.ADD;
        myProperty = newMask.property("ADBE Mask Shape");
        myShape = myProperty.value;
        myShape.vertices = [[1900, 0],[20, 0],[0, 20],[0, 1060],[20, 1080],[1900, 1080],[1920, 1060],[1920, 20]];
        myShape.inTangents = [[11.045654296875,0],[0,0],[0,-11.457153320312],[0,0],[-11.457153320312,0],[0,0],[0,11.045654296875],[0,0]];
        myShape.outTangents = [[0,0],[-11.457153320312,0],[0,0],[0,11.045654296875],[0,0],[11.045654296875,0],[0,0],[0,-11.457153320312]];
        myShape.closed = true;
        myProperty.setValue(myShape);
        app.endUndoGroup();
    } 
}

 

This topic has been closed for replies.
Correct answer Mathias Moehl

say a vertex is at [x,y] in a comp with 1920x1080 and you want to scale everything to fit your comp.

Then you can do

var scaleX = comp.width/1920;
var scaleY = comp.height/1080;

scaledVertex = [x*scaleX, y*scaleY]

2 replies

Mylenium
Legend
January 13, 2022

You can simply proportionally scale based on existing info as Mathias suggested, you can implement the actual Bèzier formula or given the cubic nature of the curves just assume that around 33.33 percent tangent length makes for a perfect circle and thus multiply your values with 0.3333 or something. Whatever works best for you.

 

Mylenium

Mylenium
Legend
January 13, 2022

Obviously you'd have to retrieve the comp size and insert the values as variables, none of which exists in your current code.

 

Mylenium

Inspiring
January 13, 2022

Thanks for replying Mylenium.

 

Correct, they don't. I can retrieve those values via comp.width and comp.height.

 

My issue is, once I have these, how do I use this to calculate where the vertices should be in any given comp?

 

Thanks,

Liam

Mathias Moehl
Community Expert
Mathias MoehlCommunity ExpertCorrect answer
Community Expert
January 13, 2022

say a vertex is at [x,y] in a comp with 1920x1080 and you want to scale everything to fit your comp.

Then you can do

var scaleX = comp.width/1920;
var scaleY = comp.height/1080;

scaledVertex = [x*scaleX, y*scaleY]
Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects