Category Archives: AfterEffects

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.