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.

3 thoughts on “After Effects Expression: Snapping objects to whole pixels”

  1. 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. 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:
    http://www.mamoworld.com/index.php?option=com_content&view=article&id=228%3Asnap-to-grid&catid=48%3Amovement-modifier&Itemid=93

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>