2d mode through Editor Script

I am trying to set The Scene view to be in 2d mode with an editor script.

i can read the state on the sceneview with:

SceneView.lastActiveSceneView.in2DMode;

not problem, no errors! But if i try to Set it i get the following error!
Any clues?

NullReferenceException: Object reference not set to an instance of an object
UnityEditor.Tools.get_viewToolActive ()
UnityEditor.Tools.get_viewTool ()
UnityEditor.SceneView.set_in2DMode (Boolean value)

Thanks in advance!

Just came across this solution:


    SceneView sv = SceneView.sceneViews[0] as SceneView;
    sv.in2DMode = true;

Obviously it’s possible to have multiple scene views, so you might want to put this in a loop.