Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Randomize Layer Order

  • Peter Stone

    July 28, 2014 at 6:10 am

    I am not sure how to actually implement this. Here is what I am looking for: I have an shape layer from a “create shapes from vector layer,” This leaves me with 1 shape layer with a few hundred groups in contents each with a path and a fill. I want each fill/path to appear in a random order- Either a by a trim paths animator set to individual, or better yet by with an opacity slider where each fill/path group fades in in sequence. I think I am stuck with a trim paths animator as long as all the paths stay in a single shape layer.

    My first work around was to use the explode shape layer script here- https://aescripts.com/explode-shape-layer/, then precomp all the resultant shape layers, and use your code (below) to randomize layer order in the precomp, and then randomize the layer InPoints (I hadnt worked that one out yet). However, the randomize layer order runs out of memory on my computer for very large numbers of shape layers, and its a messy method because I have deal with anchor point transformations from the explode-shape-layer script.

    I think it would be much cleaner if I can randomize the order of groups within the shape layer without exploding it, then use trim paths. A bonus would be to instead put a 0 to 100 opacity keyframe on each group at random time within my comp length- obviously the 0 opacity keyframe would have to always come first, and I could adjust the total completion time by adjusting the length of the comp.

    Much appreciation for any help

    {
    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();
    }
    }

  • Andre Gomes

    February 18, 2016 at 11:06 pm

    I’m not sure if this has been discussed yet. But I was wondering where this script is inputted. or is it entered as a txt file and executed from a run script function inside of ae?

    {
    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();
    }
    }

  • Dan Ebberts

    February 18, 2016 at 11:15 pm

    That’s a script that you would save as a .jsx file and run from AE’s file menu:

    File > Scripts > Run Script File…

    or, you could launch the ExtendScript editor (File > Scripts > Open Script Editor), paste the code in, and run it from there.

    Dan

  • Anna Fromm

    March 29, 2022 at 9:13 pm

    Thank you, Dan! Your script helps me to save a lot of time!

  • Nate Johnson

    November 29, 2023 at 12:47 am

    Can someone please explain how and where to use this script? How do I take the text from above and get it into AE? Then, with the comp I want to use it on open, go File, Script, Run… ? Do I PreComp everything and put it as an expression? If so, with what parameter? I’m sure the answers are basic, but it’s completely illuding me… Thank you kindly.

     

    Nate

  • Dan Ebberts

    November 29, 2023 at 12:57 am

    You’d copy/paste it into a plain text editor and save it as a .jsx file. Then do as you describe (File, Script, Run). This is a cleaned up version because it looks like it must have gotten munched in the last site conversion here:

    {
    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();
    }
    }
Page 4 of 4

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