Can't select clip or track using C# Selection.activeObject

I tried using the code posted by @seant_unity here but I guess some things have changed in the last 2.3 years :slight_smile:

The object is selected and there is something there - debug.log prints out “Hello there” but does not continue through the next if condition.

I’m really after the track object - I’d like to add an animation/custom clip to the selected track. I’m hoping it will be a relatively easy step from selecting the clip to selecting the track.

Please let me know if I can provide any additional info.

var obj = UnityEditor.Selection.activeObject;
if (obj != null)
{
    Debug.Log("Hello there"); // this works

    var fi = obj.GetType().GetField("m_Item", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
    if (fi != null)
    {
        Debug.Log("You are a bold one");

        var clip = fi.GetValue(obj) as TimelineClip;
        if (clip != null)
            Debug.Log(clip.displayName);

        //var track = fi.GetValue(obj) as AnimationTrack;
        //if (track != null)
        //    Debug.Log(track.name);

    }
}

“m_Item” has been changed to “m_Clip”.

In 2019.2 we’ve added TimelineEditor.selectedClip/selectedClips to make it easier to access.

Thanks very much @seant_unity . Works perfectly. I can select a clip in a track and then click a button in the editor to add an curve editable clip. Two click bliss! :smile:

For the two click animators of the future (before 2019.2):

if (GUILayout.Button("Add Clip to Selected Track"))
{
    AnimationTrack theAnimationTrack;
    TimelineClip theAnimationClip;
    AnimationCurve curve;

    var obj = UnityEditor.Selection.activeObject;
    if (obj != null)
    {

        var fi = obj.GetType().GetField("m_Clip", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
        if (fi != null)
        {

            var clip = fi.GetValue(obj) as TimelineClip;
            if (clip != null)
                Debug.Log(clip.displayName);
                Debug.Log(clip.parentTrack.name);

                theAnimationTrack = clip.parentTrack as AnimationTrack;

                var d = theAnimationTrack.duration;
                theAnimationClip = theAnimationTrack.CreateRecordableClip("ComingForwardClip");
                theAnimationClip.start = d;

                theAnimationClip.duration = 20;
                AnimationPlayableAsset animationPlayableAsset = theAnimationClip.asset as AnimationPlayableAsset;
                Keyframe[] keys;
                keys = new Keyframe[3];
                keys[0] = new Keyframe(0.0f, 2.0f);
                keys[1] = new Keyframe(1.0f, 0.0f);
                keys[2] = new Keyframe(2.0f, -1.0f);
                curve = new AnimationCurve(keys);

                typeof(TimelineClip).GetMethod("AllocateAnimatedParameterCurves", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(theAnimationClip, new object[] { });
                animationPlayableAsset.clip.SetCurve("", typeof(Transform), "localPosition.z", curve);

                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
        }
    }
}
2 Likes