Press "Enter" to skip to content

After Effects Expression: Snapping objects to whole pixels

Here at Primal Screen we’re doing a project that has to do with old-school 8-bit video games. We’re animating stuff that was originally 320x240px and blowing it up to 1080p. We figured out that the best way to do that is animate it at actual size, 320×240, and blow it up to 1080p from there. We couldn’t use AE’s native scale functionality, because it tries to interpolate the footage (i.e. soften the edges) and we want to keep that blocky retro look, so we used Magic Bullet InstantHD ReSizer, quality: best, smoothness: 0.5 (lowest possible).

Although the art was looking great, we quickly noticed a problem. When things were tweened, even in the tiny actual size comp, they often moved at sub-pixel speeds, i.e. an object might have an x coordinate of 32.5 for a frame, and when that happens, AE anti-aliases the whole thing, giving our cool block pixel art weird soft edges that don’t look right in that retro world.

The fix is an expression.

newX = Math.round(transform.position[0]);
newY = Math.round(transform.position[1]);
[newX, newY]

Line 1 + 2: Grab the current X and Y (aka position[0] and position[1]) of the object, round them to whole numbers, and store them as newX and newY.
Line 3: Set the position to [newX, newY]

And in case you’re an über-noob, here’s how to apply the expression.
Step 1: Select the object
Step 2: Press your P key, to pull down it’s position attributes.
Step 3: Animate the object however you want with the little stopwatch and keyframes
Step 4: Option-click* the stopwatch to bring down the Expression: Position field, and paste in the above code.

*I say option-click because that’s what it is on Mac OS X. I don’t know what modifier key they changed it to when they made AE for Windows.

And now the video demonstration! These two big red boxes have the exact same tweens (in fact, one is a dupe of the other), but only the bottom one has the expression seen above.

Snap to Pixels AE Expression from Christopher Silich on Vimeo.


  1. Russ Russ July 27, 2012

    alternatively you could have used mosaic on an adjustment layer, set to ‘sharp colours’

    Also render in draft mode. You’ll have to set draft mode in the render settings as well as the layer settings

    still a useful expression though, cheers

  2. Mathias Möhl Mathias Möhl October 26, 2012

    Alternatively, you could use the “Snap To Grid” iExpression.
    It offers even more flexibility, like keeping a certain percentage of the smoothness instead of eliminating it completely (say 80% of the time you stay exactly at one pixel, but instead of abruptly jumping to the next one, you travel to the next one smoothly for the next 20%) . This allows for some unique looks.

    More infos:

  3. […] So, how do you get that clean interpolation that we see in the second box?  Chris uses an After Effects expression called Math Round, that will force AE to interpolate at whole pixels only.  For the code for the Math Round expression see Chris’ post here. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: