Proximity based control?

Hey Everyone,

I’ve just learned how to animate an objects layer property, is it possible to automate this with python, based on the proximity of another object?

I.e. If I have a cube and sphere in a scene, is there a way in python to make it so that when the sphere moves outside a certain distance from the cube, the cubes layer property gets changed from one to two?

Thanks heaps,
Simon O