Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Shuffle layers order while keeping track mattes link??

  • Shuffle layers order while keeping track mattes link??

    Posted by Antoine Dupont on December 27, 2020 at 8:41 pm
    app.beginUndoGroup("Randomize");
    var myComp = app.project.activeItem;
    var n = myComp.numLayers;
    var myLayers = [];
    var myIdx = [];
    for (var i = 1; i<= n; i++){
    myIdx[i-1] = i;
    myLayers[i-1] = myComp.layer(i);
    }
    var idx;
    var temp;
    for (var i = 0; i < myIdx.length; i++){
    idx = i + Math.floor(Math.random()*(myIdx.length - i));
    temp = myIdx[i];
    myIdx[i] = myIdx[idx];
    myIdx[idx] = temp;
    }
    for (var i = 0; i < myIdx.length; i++){
    myLayers[myIdx[i]-1].moveToBeginning();
    }
    app.endUndoGroup();

    This code shuffle the order of the layers.

    How can I shuffle the order of track mattes without breaking their link?

    Thank you!

    Filip Vandueren replied 5 years, 8 months ago 2 Members · 1 Reply
  • 1 Reply
  • Filip Vandueren

    December 28, 2020 at 9:19 pm

    Like this:

    app.beginUndoGroup("Randomize - keep Mattes");
    var myComp = app.project.activeItem;
    var n = myComp.numLayers;
    var myLayers = [];
    var myIdx = [];
    for (var i = 1; i<= n; i++){
    if (myComp.layer(i).isTrackMatte) {
    continue
    }
    myLayers.push(myComp.layer(i));
    }
    while(myLayers.length) {
    randomIndex = Math.floor(Math.random()*myLayers.length)
    l=myLayers.splice(randomIndex,1)[0];
    if (l.hasTrackMatte) {
    myIndex = l.index;
    l.moveToBeginning();
    // matte now has the same index as it's target had beofre it was moved up
    myComp.layer(myIndex).moveToBeginning();
    } else {
    l.moveToBeginning();
    }
    }
    app.endUndoGroup();

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