I'd paint last. Car's usually get covered in crap: little scratches, oil, dents etc when pulling engines out or putting them back in. It's easy enough to get the engine in or out without making a big mess, but just to be safe I'd save the painting till last.