Moderator: Paul Tuersley

Topic Author
Posts: 7
Joined: Mon Mar 17, 2008 3:25 pm

Fixed animation at random time

Sun Apr 14, 2013 12:48 pm


Anyone have an idea for how to make script that at a random time interval will do fixed animation to selected layer(s)?

Something like this:
random_min_time: 5 sec
random_max_time: 10 sec
// there will be between 5 to 10 sec. between the fixed animations first key frames.

fixed animation:

Script generated key frames will be added to/mixed with existing key frames on the timeline if any are present.

Any help appreciated
Posts: 3
Joined: Thu Apr 25, 2013 10:51 am

Re: Fixed animation at random time

Thu Aug 01, 2013 12:52 pm

This is sort of an old post by now, but I went ahead and wrote a script that does what you're looking for. If you've already solved this, then at the very least I was able to get some good practice out of it. :-)

    Performs a pre-determined opacity animation on selected layers in the comp at random intervals between 5 and 10 seconds apart.
    Per request from AEnhancers user kennethbonde:
    Script written by Robert Headrick (, August 1, 2013.
app.beginUndoGroup("Opacity Animator");

//initialize variables
var selectedLayers = app.project.activeItem.selectedLayers;
var fps = app.project.activeItem.frameDuration;
var minTime = 5 //measured in seconds
var maxTime = 10 //measured in seconds

//create functions
function placeAnimation (layerIndex,keyTime) {
    var layerOpacity = selectedLayers[layerIndex].property("Opacity");
    layerOpacity.setValueAtTime(keyTime + (fps*2),100);
    layerOpacity.setValueAtTime(keyTime + (fps*5),100);
    layerOpacity.setValueAtTime(keyTime + (fps*20),0);

function randomFromInterval(from,to) {
    return Math.floor(Math.random()*(to-from+1)+from);

function incrementByRandom(incrementer) {
    var incrementValue = randomFromInterval(minTime/fps,maxTime/fps); //dividing by fps returns value in frames
    var returnValue = incrementer + (incrementValue*fps); //multiplying by fps returns value in time units
    return returnValue;

//perform actions for each selected layer
if (selectedLayers.length > 0) {
    for (var i = 0; i < selectedLayers.length; i++) {
        var layerInPoint = selectedLayers[i].inPoint;
        var layerOutPoint = selectedLayers[i].outPoint;
        var currentLayerIndex = i;
        //create keyframes at intervals until end of layer is reached
        for (var j = incrementByRandom(layerInPoint); j <= layerOutPoint; j = incrementByRandom(j)) {
   } else {
       alert("You have no layers selected!");


Who is online

Users browsing this forum: No registered users and 5 guests