Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Scripting: shift markers from selected layers random within values

  • Scripting: shift markers from selected layers random within values

    Posted by Peter Zeet on April 10, 2019 at 12:13 am

    Hi there!
    Have been trying to shift markers from a bunch of selected layers, and all ok, but when I try to add some randomness to the shifting, the markers of all the layers move the same amount of frames. I’d need the markers to move random within a min max, but having each layer a different shifting, so if these are 2 layers with 2 markers
    *————*—
    *————*—

    after running the script, I’d love to get something like:
    —*———–*—
    —–*———–*—-

    and now I get:
    —*———*–
    —*———*–

    the script is this so far:

    //as seen on https://www.aenhancers.com/viewtopic.php?t=71 and tweaked a bit

    app.beginUndoGroup("shiftMarkers");

    var comp = app.project.activeItem;
    var myLayers = app.project.activeItem.selectedLayers;
    var newMarkers = new Array();
    var newTimes = new Array();
    var min=-3;
    var max=3;
    var ran=Math.floor(Math.random() * (max - min + 1)) + min;
    var myLayer;

    for (var j= 0; j< myLayers.length; j++){
    myLayer = myLayers[j];
    theMarker =myLayer.property("Marker")

    for(var i = theMarker.numKeys; i >= 1; i--){

    var markerVal = theMarker.keyValue(i);
    newMarkers[newMarkers.length] = markerVal;
    var markerKeyTime = theMarker.keyTime(i);
    var newMarkerTime =markerKeyTime+(ran*comp.frameDuration);
    newTimes[newTimes.length] = newMarkerTime;

    // remove original markers
    theMarker.removeKey(i);
    }

    // add new markers with new value.
    for(var k = 0; k < newMarkers.length; k++) {
    theMarker.setValueAtTime(newTimes[k], newMarkers[k]);

    }

    }

    app.endUndoGroup();

    Any ideas? Thanks!

    Peter Zeet replied 7 years, 3 months ago 2 Members · 3 Replies
  • 3 Replies
  • Dan Ebberts

    April 10, 2019 at 12:32 am

    I think you just need to move your ran calculation:

    var ran=Math.floor(Math.random() * (max – min + 1)) + min;

    inside your layer loop (j).

    Dan

  • Peter Zeet

    April 10, 2019 at 12:43 am

    oh! thanks!
    That works, but now I have a side effect… from here

    after running the script, now it seems it is not removing the old markers anymore

    mmm maybe I should add another removal?

  • Peter Zeet

    April 29, 2019 at 7:46 pm

    hi!
    after some attempts, I think I couldn’t figure it out properly… any ideas on this? I’m a bit lost
    super thanks!

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