Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Script only works properly if i re-run it each time – need help

  • Script only works properly if i re-run it each time – need help

  • Jay Sil

    January 17, 2021 at 7:31 pm

    Hi, i am trying to create a small script for my own workflow.

    The part of the script i need help with is where i have a button that creates a new layer above the layer i have selected.

    The problem is that the script only works properly once, after the first time it has been run either from extendscript or from the window tab within after effects – meaning, that the layer that was selected before running the script, gets the new layer created above it, which is right, but when i then select another layer and click the button, it still creates it above the original layer that was selected before the script was initiated. in order for it to work on another layer, i need to select the new layer and then re-run the script.

    TLDR – Is there something specific that needs to be done in order for the button to keep creating above the new layer that i select each time without having to re run the script?

    the code is as basic as it comes:

    var myLayers = app.project.activeItem.selectedLayers;
    var firstLayer = myLayers[0];
    var secondLayer = myLayers[1];
    
    moveButton.onClick = function() {
      secondLayer.moveBefore(firstLayer);
    }
    
    createButton.onClick = function() {
      var newSolid = myComp.layers.addSolid([0.8, 0.2, 0.5], "NEW LAYER", 1920, 1080, 1).moveBefore(firstLayer);
    }

  • Andrei Popa

    January 18, 2021 at 9:22 am

    You never assign a new value to your layer. Try this

    var myLayers = app.project.activeItem.selectedLayers;
    var firstLayer = myLayers[0];
    var secondLayer = myLayers[1];
    moveButton.onClick = function () {
    secondLayer.moveBefore(firstLayer);
    };
    createButton.onClick = function () {
    var newSolid = myComp.layers
    .addSolid([0.8, 0.2, 0.5], "NEW LAYER", 1920, 1080, 1)
    .moveBefore(app.project.activeItem.selectedLayers[0]);
    };
  • Filip Vandueren

    January 19, 2021 at 11:32 am

    I think the issue is that once you create a new Layer, the original layerCollection object becomes invalid, it doesn’t auto-update.

    so the myLayers = app.project.activeItem.selectedLayers; etc…
    needs to be re-evaluated every time the button is clicked, not just at the initialising of the window.

    Edit: Since you’re working from selectedLayers, (not app.project.activeItem.layers, which is what I was thinking of), this will be especially true. The selectedLayers isn’t up to date anymore

Viewing 1 - 3 of 3 posts

Log in to reply.

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