Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Random composition from project

  • Random composition from project

    Posted by Dany Sterkhov on May 22, 2016 at 11:48 pm

    Good evening!

    I’m trying to make generator, which takes random composition from folders, mapped to buttons. For example, when I push “Square” button, it places composition from “Square” folder to my current comp.

    I wrote code for placing selected comps into current comp (below, don’t mind russian text, I put it for myself to remind what this stuff do), but just wanted to ask, is there a way to “seed” my selection, make it random?

    app.activeViewer.setActive(); // Выбирает активное окно
    var scriptName = "Paste Comp Into Comp";
    var myComp = app.project.activeItem;

    app.beginUndoGroup(scriptName); //Начало отмены

    for(var i = 1; i <= app.project.numItems; i++){ //Берёт информацию из окна Project
    if ((app.project.item(i) instanceof CompItem) && app.project.item(i).selected){ // Проверяет, выбранна ли композиция и композиция ли это
    myComp.layers.add(app.project.item(i)); //Добавляет выбранные элементы на таймлайн
    }
    }
    app.endUndoGroup();

    Miguel De mendoza replied 10 years, 3 months ago 2 Members · 1 Reply
  • 1 Reply
  • Miguel De mendoza

    May 23, 2016 at 9:14 pm

    You can use Math.random() to get random float between 0 and 1. So, to make a random selection inside a folder you can write this function:

    function getRandom(min, max) {
    return Math.random() * (max - min) + min;
    }

    and then feed it with 1 and folder items lenght-1 and then round it with Math.round().

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