Change default clip in animation window for animator

Short and simple, every time I click on a gameobject with an animator component, the animation window switches to show a default clip, as far as I can tell it always defaults to the first clip added to the animator. If I select any other game object and return, it switches back to that one clip, even if I was just editing a different one. I can’t for the life of me figure out how to make it remember what the last selected clip was. I’m almost at the point where I’m considering building some ungodly editor utility that monitors the animation window and switches it back for you, but before I do that, is there something I’m missing on how to have it remember what clip I was viewing?

Well, I just went ahead and made an editor utility that does it for me. In my case I already have an editor that I use to initially open the clip (would be a bit different otherwise), I added this code when a clip is initially opened:

            var animationWindow = EditorWindow.GetWindow<AnimationWindow>();
          
            targetObject = animator.gameObject;
            targetClip = clip;
          
            Selection.activeGameObject = targetObject;
            animationWindow.animationClip = clip;

            if(!hasCreatedOnChangeEvent)
                Selection.selectionChanged += OnSelectionChange;
            hasCreatedOnChangeEvent = true;

Then, the OnSelectionChange callback looks like this:

        private void OnSelectionChange()
        {
            if (targetClip == null || !EditorWindow.HasOpenInstances<AnimationWindow>())
                return;

            if (Selection.activeGameObject == targetObject)
            {
                var animationWindow = EditorWindow.GetWindow<AnimationWindow>();
                if(animationWindow.animationClip != targetClip)
                 animationWindow.animationClip = targetClip;
            }
        }

When it’s all in place, changing your selected game object and returning back to the initial object will swap back to the clip you previously opened it on. My use case is a bit unique since I only care about one object and one clip on that object which I select elsewhere, but you could probably expand it to keep track of the AnimationWindow clip and selected game object and make a cache to always remember what clip you had selected for each object. It will also forget whenever you recompile or reload the editor, something you could probably solve by throwing the data on a serialized object. A task for another day though.