StateMachine: bugreport

I had a problem when such changes as adding new state machines or states to Animator Controller via script weren’t saved.
For example, this thread: Modify AnimatorController through script and save it?

I fixed this problem by this way:

    [MenuItem("Tools/ModifyAnimator")]
    public static  void ModifyAnimator()
    {
        Object w = AssetDatabase.LoadAssetAtPath("Assets/Anim/TestAC_Generator.controller", typeof(AnimatorController));
        AnimatorController animatorController = w as AnimatorController;

        var sm = animatorController.layers[0].stateMachine;
        AnimatorStateMachine s = new AnimatorStateMachine();
        s.name = "2222";

        sm.AddStateMachine(s, Vector3.zero);

        foreach (var a in sm.stateMachines)
        {
            //if (AssetDatabase.GetAssetPath(a.stateMachine) != "") // doesn't work for some reasons
            if (AssetDatabase.GetAssetPath(a.stateMachine).Length == 0)
            {
                AssetDatabase.AddObjectToAsset(a.stateMachine, AssetDatabase.GetAssetPath(animatorController));
                a.stateMachine.hideFlags = HideFlags.HideInHierarchy;
            }
        }
    }

Reference below tell us, that if (AssetDatabase.GetAssetPath(this) != "") is used in AddStateMachine function and similar. I see it doesn’t work at all.

Maybe it worth replacing with the following(or check both conditions at the same time): if (AssetDatabase.GetAssetPath(this).Length == 0)

Reference:

Ok, so this isn’t working for me. If you don’t mine I could send you my code and give me a hand.