Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions How to get the same result as timeToFrames with scripting?

  • How to get the same result as timeToFrames with scripting?

    Posted by Simon Bjork on December 10, 2020 at 9:41 pm

    I’ve been banging my head trying to get the same result as the expression timeToFrames but via scripting (as it’s missing and can’t use timeToCurrentFormat).

    Every time it seems to work, it breaks in more extreme situations (like changing the frame-rate to very high/low).

    I’m trying to do some simple calculation like get the first and last frame via scripting.

    Also, I’m struggeling to to do the framesToTime calculation as well, it always seems like there are some rounding error that breaks the calculation.

    Any suggestions?

    * Sorry if the post is unclear, I’ve been looking at this for too long I think.

    Simon Bjork replied 5 years, 8 months ago 2 Members · 4 Replies
  • 4 Replies
  • Dan Ebberts

    December 10, 2020 at 10:23 pm

    So the straightforward approach like this doesn’t work for you?

    function timeToFrames(theTime,theComp){

    return Math.round(theTime/theComp.frameDuration);

    }

    function framesToTime(theFrames,theComp){

    return theFrames*theComp.frameDuration;

    }

  • Simon Bjork

    December 11, 2020 at 9:45 am

    Thanks for the reply and example code Dan!

    I started over from scratch with your code and it actually seems to be that simple.

    The things that always seemd to mess up my calculations were custom start frames on the composition and how the frame was setup in the TimeDisplayStyle was set (to 0 or 1).

    Here’s some example code that in my first tests seem to work. Does it look correct to you?

    function timeToFrames(theTime,theComp){
    return Math.round(theTime/theComp.frameDuration);
    }

    function framesToTime(theFrames,theComp){
    return theFrames*theComp.frameDuration;
    }

    function getFrameRange(como){
    var first = (app.project.displayStartFrame) + (comp.displayStartTime/comp.frameDuration)
    var last = first + timeToFrames(comp.duration, comp) -1;
    return [first, last];
    }
    function print(x){
    $.writeln(x);
    }
    var comp = app.project.activeItem;

    // Print the frame range to the console.
    print(getFrameRange());

    // Now try to set the time indicator at a specific frame.
    // 1. Create a comp that has a start frame on 1031
    // 2. In TimeDisplayStyle set the frame count to start at 1.
    var frame = 1035

    var startFrameTime = framesToTime(app.project.displayStartFrame, comp);
    var time = (framesToTime(frame, comp) - framesToTime(comp.displayStartTime/comp.frameDuration, comp));

    // Need to add some small number for this to work?
    var timeFix = time - startFrameTime + 0.000000001;

    // Move the time indicator to a given frame.
    comp.time = timeFix;

  • Simon Bjork

    December 11, 2020 at 9:11 pm

    It still seems like it’s doing some rounding errors.

    In the above code it seems to work if the getFrameRange function adds a Math.floor to each value before it returns them.

    I also struggled with setting the startFrame of a comp. From what I understand you can use comp.displayStartFrame directly now, but as far as I can tell that won’t work in older AE versions.

    This seems does seem to work, but I’m not sure for older ones as well if there a different rounding error.

    function setStartFrame(frame, comp){
    if(app.project.displayStartFrame == 1){
    var frame = frame-1
    }
    var startFrameSec = framesToTime(frame, comp) + 0.00001;
    comp.displayStartTime = startFrameSec;

    } // function setStartFrame(comp, frame){

    var frame = 231
    // Tested with different frame rates, 8, 24, 300.
    var comp = app.project.activeItem;
    setStartFrame(frame, comp);
  • Simon Bjork

    December 12, 2020 at 1:08 pm

    It almost feels like the whole thing is random? Things that worked stopped working the next day etc?

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