Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions The function stops working as soon as I put it in the UI button

  • The function stops working as soon as I put it in the UI button

    Posted by Smet Kira on August 5, 2023 at 9:43 am

    Hi all. Tell me what I’m doing wrong. I want my smet() function to work via a button click. A window appears with the input of the number of masks, after pressing this number is inserted into the code and cuts the layer by this number of masks. By itself, the smet() function works correctly, but as soon as I paste it into a button, After Effects freezes.

    var nW = new Window(“palette”,”Разрезы”,undefined);

    var nSlice = nW.add("edittext",undefined,"");

    var ok = nW.add("button",undefined, "Ok");

    nW.size = [200,100];

    nSlice.size = [50,20];

    nW.show();

    nW.center();

    ok.onClick = function() {

    var numSl = parseInt(nSlice.text)

    smet();

    nW.hide();

    }

    function smet(){

    var comp = app.project.activeItem;

    var selectedLayers = comp.selectedLayers[0];

    var ind = selectedLayers.index;

    var nm = selectedLayers.name;

    var textSourceRect = selectedLayers.sourceRectAtTime(comp.time, false);

    app.beginUndoGroup("123")

    var preWidth = parseInt(textSourceRect.width);

    var preHeight = parseInt(textSourceRect.height);

    var selLPos = selectedLayers.property("Position").setValue([preWidth/2,preHeight/2]);

    var precomp = comp.layers.precompose([ind], nm, true);

    precomp.width = preWidth;

    precomp.height = preHeight;

    precomp.name = nm

    for (var i=0; i<numSl-1;i++) {

    var copy = app.executeCommand(app.findMenuCommandId("Duplicate"))

    var down = app.executeCommand(app.findMenuCommandId("Send Layer Backward"))

    }

    var comp = app.project.activeItem

    for (var i = 0; i<=numSl;i++){

    var selM = comp.layer(i+1);

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

    newMask.maskMode = MaskMode.ADD;

    var myProp = newMask.property("ADBE Mask Shape");

    myShape = myProp.value

    var hPos = preHeight/numSl

    myShape.vertices = [[0,i*hPos],[preWidth,i*hPos],[preWidth,i*hPos+hPos],[0,i*hPos+hPos]]

    myShape.closed = true;

    myProp.setValue(myShape);

    }

    app.endUndoGroup();

    }


    Andrei Popa replied 2 years, 9 months ago 2 Members · 1 Reply
  • 1 Reply
  • Andrei Popa

    August 7, 2023 at 12:30 pm

    I suggest you pass a parameter to your smet function, and don’t use global variables. This changes seem to make your code work:

    ....

    ok.onClick = function () {

    var numSl = parseInt(nSlice.text)

    smet(numSl);

    nW.hide();

    }

    function smet(numSl) {

    ...

    The rest of the code remains the same

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