Skip to main content
Hey Evgenii
Inspiring
January 22, 2022
Question

Why do I get non-integer numbers when using script?

  • January 22, 2022
  • 3 replies
  • 501 views

Hi guys, I am trying to build a simple script that sets keyframes automatically every 'i' seconds and changes  position by fixed amount of pixels ( in my case -640) on every keyframe. 

I wrote a simple lines of code to do it: 

var curX = 320.0;
    for (i = 10; i <= 240; i += 5){

    selection[0].setValueAtTime(i, [curX,540])
    curX += -640.0;
};

 

 

 

 

But all values of every keyframe are non-integer numbers, although in the code above there are only integers. Here is the some values I get when running the code:

10 sec position x:320 y: 540;

15 sec position x:-321,9194 y: 540;

19,29 sec position x:-958,29 y: 540;

24.29 sec position x:-1598,9316 y: 540;

......

....

0;02;59;27  position x: -21433,1681 y: 540;

selection in this code is app.project.activeItem.selectedProperties, so selection[0] is position

Any suggestion why is this happening and how to fix?

Thank you in advance 🙂

This topic has been closed for replies.

3 replies

Mathias Moehl
Community Expert
Community Expert
January 23, 2022

How do you check the value of the keyframe? Do you read the value shown in the time-line? Do you see the same value in the dialog that pops up when you double-click on a keyframe?

If you see clean int numbers in the dialog, but point numbers in the timeline, then most likely your keyframes are not exactly at the start of a frame, but in-between frames.

If your comp has a frame rate of 23.976 fps, for example, and you insert a keyframe at 1s, then this keyrame sits a tiny bit before the (beginning of the) 25th frame, (since after 1s only 23.976 and not full 24frames have been played, yet).

 

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Mylenium
Legend
January 23, 2022

Well, then straighten out your math further by using timeToFrames() and such or additional rounding.

 

Mylenium

Mylenium
Legend
January 22, 2022

Integers can be perfectly used to represent fractional numbers. That's a general misunderstanding many people have. If you want whole numbers, simply add a Math.round().

 

Mylenium

Hey Evgenii
Inspiring
January 23, 2022
I did
var
curX = Math.round(320.0);
for (i = 10; i <= 240; i += 5){
selection[0].setValueAtTime(i, [Math.round(curX),540])
curX += Math.round(-640.0);
};
but it gives the same result