timeToCurrentFormat() replacement
  • Hey. Recently I had to access the current time of the composition for the script I was creating in one of the two ways: frames or timecode (smtp). There's a timeToCurrentFormat() global function, however the result it returns depends on the current settings in the Timeline window - which sucks.

    First I tried to develop a custom function that would calculate the correct values, however it was always going of track in long compositions, or with long Start Timecode settings - especially when dealing with floating-point framerates like 29,97.

    Luckily I came up with a very simple method based entirely on the build in AE math, so you can be sure that the reslut is always the same as it is in the Timeline panel. Here's the code:

            function malty_formatTime(t, fps, smtp)
            {
    // t - time in seconds (like app.project.activeItem.time)
    // fps - framerate (not frame duration) - app.project.activeItem.frameRate
    // smtp - boolean (optional. if true the result is presented in timecode, if false the result is in frames)

    var currentSetting = app.project.timeDisplayType;
                 app.project.timeDisplayType = (smtp==true) ? TimeDisplayType.TIMECODE : TimeDisplayType.FRAMES;
                 var result = timeToCurrentFormat(t, fps);
                 app.project.timeDisplayType = currentSetting;
                 return result;
            }

    Hope it will prove useful.
    Enjoy.