How do you get the active animation controller?

After double clicking on an animation controller asset the animator window will open and allow you to inspect the controller. Is there a way to find out what the controller that is active in that window is?

I noticed you can get the state that is selected in that window through Selection.activeObject but I can’t find a path back to the controller from there.

Reflection is the answer to the universe.

// https://gist.github.com/forestrf/29c391c7cae0202ea18392b03bb70ac1
public AnimatorController GetCurrentActiveController()
{
    string typeName = "UnityEditor.Graphs.AnimatorControllerTool, UnityEditor.Graphs";
    Type t = Type.GetType(typeName);
    EditorWindow animator = GetWindow(t);
    BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.NonPublic;
    FieldInfo objectField = t.GetField("m_AnimatorController", bindingAttr);
    return (AnimatorController)objectField.GetValue(animator);
}

Necro, in case anyone else comes looking for an answer. Reflection isn’t needed. You can access the source-controller using the AssetDatabase, since States are part of the Controller asset.

AnimatorState state; // via Selection.activeObject, or MenuCommand.context
string controllerAssetPath = AssetDatabase.GetAssetPath (state);
AnimatorController animController = 
    AssetDatabase.LoadAssetAtPath<AnimatorController> (controllerAssetPath);