unity – How do I record scripted rotation changes in animation file?


I can create an animation file by clicking “Animation”, then selecting a gameobject, then press record and then change bone rotations in the Inspector.
The animation window will be notified about the bone rotation changes and add this to the animation file like this:

enter image description here

This works fine as long as the Inspector is used to change the bone rotations.

Bone rotations done by a script are not recorded.

Is there a trick to record these bone rotations via script anyways?
For example using something like (pseudo-code) this? ->

rShoulder.rotation.dirty = true;

For your interest, I have attached the script that I use to rotate the bones and which are not recorded. This script works perfectly fine, but it doesn’t trigger animation changes:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyPlayer : MonoBehaviour
{
    public Transform Target;

    private void OnGUI()
    {
        if (GUI.Button(new Rect(0, 70, 50, 30), "Transfer rotations to other model with same rig"))
        {
            List<Transform> nTheseTransforms = new List<Transform>();
            Helpers.GetAllChildren(this.transform, ref nTheseTransforms);

            List<Transform> nTargetTransforms = new List<Transform>();
            Helpers.GetAllChildren(Target, ref nTargetTransforms);

            foreach (Transform nThisTransform in nTheseTransforms)
            {
                foreach (Transform nTargetTransform in nTargetTransforms)
                {
                    if (nThisTransform.name == nTargetTransform.name)
                    {
                        nTargetTransform.localRotation = nThisTransform.localRotation;
                        break;
                    }
                }
            }
        }
    }
    public static void GetAllChildren(Transform parent, ref List<Transform> transforms)
    {
        foreach (Transform t in parent)
        {
            transforms.Add(t);
            GetAllChildren(t, ref transforms);
        }
    }

}