Can you paint the engine bay with the engine still installed?

I had to pull my motor/tran together when I painted mine. Problem is
how do you sand everything down with the motor still in there - especially
if your changing colors. Someone before me had painted my engine
compartmant flat black with spray can paint on top of the yellow - I
used paint thinner and that spray can paint came right off. I pulled
everything, used a 3" twisted wire cup brush on a angle grinder to remove
to take it down to metal, then, primared and painted it original yellow,
followed by clear.