Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Confusion around right approach to override Timeline animated properties by script

Discussion in 'Timeline' started by fherbst, May 4, 2018.

  1. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    802
    I'm trying to wrap my head around something that I think can't be that difficult, but I haven't found an approach yet that works.

    Here's what I'm trying to do: I have an AnimationTrack that contains 15 different clips that all animate the same object. The clips have some overlap so that they blend into each other.
    Now I want to be able to, per each of these "sections" (the clip relate to environmental effects for specific sections of an experience), to override what they do. E.g. one of the sections has "animate the skycolor to red", but I want the user to be able, at runtime, to override that with green, while still having the proper fades to the previous and next clips.

    First thing I tried was to use a custom script to figure out the right weights each clip has and to apply the right override color based on these weights. That does not work for blending between clips, as the "original" color will start to fade in and I have no way to mix "previous clip color" with "override color".

    Second thing I tried was to create replacement Animation Clips with custom keyframes and put them into the AnimationClipPlayables at runtime when needed. This does not work at runtime since there is currently no API for creating AnimationClips that are compatible to timeline that works outside of the Editor.

    Third thing I tried was having a custom Mixer/Track (since, after all, I want to mix the clips in a custom way), but that failed since even with decompiling the Timeline dll most of the interesting stuff ("OnCreatePlayableGraph") is internal and cannot be overriden, so I can't replace the AnimationMixer with my custom one. The only thing I can override is "CreateTrackMixer", and according to decompilation, that's not even used by the AnimationTrack class! It's hardcoded to use AnimationMixerPlayable! ... that would've been the right spot if not for that internal oversight.

    Fourth thing I tried was doing it the "timeline way" and override the actual graph that is generated.
    Currently it looks like this:
    upload_2018-5-4_11-15-20.png

    With the override (modifying the graph in PlayableDirector.played), it looks like this:
    upload_2018-5-4_12-43-11.png

    However, the issue with this approach is that the Timeline doesn't set the weights on these three AnimationMixers, it still changes the weights of the clips. So, no luck again.

    Fifth Thing was looking into the experimental AnimationStream. From what I found, it seems that these can be used in AnimationStreamPlayables that can modify the data running through them. This means I'm running into the same issue as with approach #4, of the Timeline not being able to set weights on something else than the actual AnimationClip unless I hack that in as well.
    upload_2018-5-4_13-1-43.png

    Any advice on what the right plan of action would be here? I guess the base question boils down to:

    "How do I modify the output of an AnimationClip before it goes into an AnimationMixer, while still using the Timeline?"
     
    Last edited: May 4, 2018
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    I don't think what you are trying to do is possible using timeline and built-in animation tracks. Based on the description of the problem, it seems like you would be better off writing a custom track and mixer to do this, at least for the dynamic elements that you are trying to animate.
     
  3. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    802
    These dynamic elements have animationclips with dozens of artist-edited parameters and nicely animated motion curves. The idea was just to allow the user to modify some of these, and only when he wants to. Well, a temporary "override" in exactly that sense.

    Is there at least some documentation around or some example how to write a custom track and mixer that can also take AnimationClips and blend them? Like I said, I thought the "best" way to do this would be to just subclass the AnimationTrack and the AnimationMixer and change what's needed, but that doesn't seem possible as of now.
     
  4. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    There may be more thorough examples I am not aware of, but here's a basic animation track and clip you can start from.

    Track:
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.Playables;
    4. using UnityEngine.Timeline;
    5. using UnityEngine.Animations;
    6.  
    7. namespace Custom
    8. {
    9.     [System.Serializable]
    10.     [TrackClipType(typeof(CustomAnimationAsset))]
    11.     [TrackBindingType(typeof(Animator))]
    12.     public class CustomAnimationTrack : TrackAsset
    13.     {
    14.         public override IEnumerable<PlayableBinding> outputs
    15.         {
    16.             get { yield return new PlayableBinding { sourceObject = this, streamName = name, streamType = DataStreamType.Animation, sourceBindingType = typeof(Animator) }; }
    17.         }
    18.  
    19.         public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
    20.         {
    21.             return AnimationLayerMixerPlayable.Create(graph, inputCount);
    22.         }
    23.     }
    24. }
    25.  
    and the clip:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.Animations;
    4. using UnityEngine.Playables;
    5. using UnityEngine.Timeline;
    6.  
    7. namespace Custom
    8. {
    9.     [Serializable]
    10.     public class CustomAnimationAsset : PlayableAsset, IPropertyPreview
    11.     {
    12.         [SerializeField] AnimationClip m_Clip;
    13.  
    14.         public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
    15.         {
    16.             // could create a subgraph of script playables, mixers, etc...as well. Use playable.SetPropogateSetTime()
    17.             // on any non-leafs nodes to have time values set by timeline propagate down the graph automatically
    18.            
    19.             return AnimationClipPlayable.Create(graph, m_Clip);
    20.         }
    21.  
    22.         // Adds the animated values to 'preview' mode
    23.         public void GatherProperties(PlayableDirector director, IPropertyCollector driver)
    24.         {
    25.             if (m_Clip != null)
    26.             {
    27.                 driver.AddFromClip(m_Clip);
    28.             }
    29.         }
    30.  
    31.     }
    32. }
    33.  
    I hope that helps you solve the problem!
     
  5. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    802
    Hey there, I'm stuck again! I tried today getting a custom graph to work using your example code. After fixing some stuff that is actually already obsolete in 2018.2, I ended up with this:

    Code (CSharp):
    1. [System.Serializable]
    2.     public class OverrideBehaviour : PlayableBehaviour
    3.     {
    4.         public OverrideContent content;
    5.         public AnimationMixerPlayable mixer;
    6.  
    7.         [Header("Output")]
    8.         public float mixerWeight;
    9.  
    10.         public override void PrepareFrame(Playable playable, FrameData info)
    11.         {
    12.             Debug.Log("[Prepare Frame] called every frame, weights: " + info.weight + " - " + info.effectiveWeight);
    13.             base.PrepareFrame(playable, info);
    14.         }
    15.  
    16.         public override void OnBehaviourPlay(Playable playable, FrameData info)
    17.         {
    18.             Debug.Log("[OnBehaviourPlay] starts playing with weight : " + info.weight + " - " + info.effectiveWeight);
    19.             base.OnBehaviourPlay(playable, info);
    20.         }
    21.  
    22.         public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    23.         {
    24.             Debug.Log("[ProcessFrame] this is never called");
    25.             base.ProcessFrame(playable, info, playerData);
    26.         }
    27.     }
    Code (CSharp):
    1. [Serializable]
    2.     public class OverrideAnimationAsset : PlayableAsset, IPropertyPreview
    3.     {
    4.         [SerializeField] AnimationClip m_Clip;
    5.         [SerializeField] OverrideContent overrideContent;
    6.  
    7.         public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
    8.         {
    9.             // could create a subgraph of script playables, mixers, etc...as well. Use playable.SetPropogateSetTime()
    10.             // on any non-leafs nodes to have time values set by timeline propagate down the graph automatically
    11.            
    12.             var p = AnimationClipPlayable.Create(graph, m_Clip);
    13.             var newPlayable = AnimationMixerPlayable.Create(graph, 0, false);
    14.  
    15.             var overridePlayable = ScriptPlayable<OverrideBehaviour>.Create(graph);
    16.            
    17.             var overrideBehaviour = overridePlayable.GetBehaviour();
    18.             overrideBehaviour.content = overrideContent;
    19.             overrideBehaviour.mixer = newPlayable;
    20.  
    21.             // return overridePlayable;
    22.  
    23.             newPlayable.AddInput(p, 0, 1f);
    24.             newPlayable.AddInput(overridePlayable, 0, 1f);
    25.             newPlayable.SetPropagateSetTime(true);
    26.            
    27.             return newPlayable;
    28.         }
    29.  
    30.         // Adds the animated values to 'preview' mode
    31.         public void GatherProperties(PlayableDirector director, IPropertyCollector driver)
    32.         {
    33.             if (m_Clip != null)
    34.             {
    35.                 driver.AddFromClip(m_Clip);
    36.             }
    37.         }
    38.     }
    with OverrideContent being a ScriptableObject that's supposed to hold my content.

    This, together with your TrackAsset code, works for having the clips in the timeline, being able to set a ScriptableObject on it and seeing that the graph is right in the PlayableGraphVisualizer.

    However, to my despair, OnProcessFrame is never called! Both in Play and Edit mode, OnPrepareFrame is happily called, telling me that the playable has a weight of 1, but OnProcessFrame - never ever. Am I doing anything wrong here? I have used custom Playables before, and there OnProcessFrame "just worked".

    I tried multiple things, including playing with the weights and the weight normalization, only having this custom playable in the mixer, ... but it just won't process the frame...
     
  6. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    ProcessFrame is never called when the output is an animation playable output (i.e the stream type is animation). Most custom tracks are 'script playables' which uses an output type that (only) calls ProcessFrame, but animation doesn't call process frame (which is the stage used to evaluate the animation).

    What are you trying to using ProcessFrame for?
     
  7. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    802
    Same as in the whole thread – I'm trying to override the output of several animation clips on a Timeline from code.
    So what I did there is what you suggested, creating a custom track with clips that build a little Playable graph by themselves – basically a Mixer with two Inputs, the AnimationClipPlayable and my custom ScriptPlayable<OverrideBehaviour>, both with weight 1 (weight normalization off). That looks fine in the GraphVisualizer.
    My idea was that basically once the AnimationClipPlayable has processed the frame, the OverrideBehaviour can do custom stuff on top. If ProcessFrame isn't the right place to do it, what's the right method to override for this?

    Adding to the options above, I tried AnimationJobs, but they aren't documented at all and I couldn't get them to work. I found one snippet in a Unity blogpost, but that is outdated and contains code that is already obsolete again / didn't even make it into a public build.
     
  8. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Ah, ok.. I understand now. My confusion was that I assumed you were trying to blend in different clips that targeted different properties, instead of dynamically trying to change the target property.

    There are several solutions that are possible here. The first is editor side, generate all possible clips you might need. That maybe a bit crazy, but overriding/changing the bindings on an animation clip is not possible in the player. You could take an animation clip that animates color.r, and generate a new clip that animates color.g for example. Then have a script playable that just sets the weights to the appropriate clips. Obviously this could generate a lot of clips, depending on how many combinations you need.

    The second is have a playable that extracts the AnimationCurves from an AnimationClip, then in PrepareFrame (or ProcessFrame) update the property on the binding directly. You could change your binding to a script playable output instead, but it would effectively be a manual animation system. You would likely need to use reflection to get the properties to animate.

    The third, if you are willing to jump into the 2018.2 beta, is C# animation jobs. (You mentioned you tried animation jobs, maybe this will help). It looks like it might be what you are looking for. It's still experimental, but it seems like you could put a C# job playable between the mixer and the clip playable.

    Hopefully one of those suggestions works for you. The most flexible solution would be being able to retarget or create a new animation clip at runtime but that isn't possible because the animation clips are optimized during the player build process, and become baked.
     
  9. Dr-Game

    Dr-Game

    Joined:
    Mar 12, 2015
    Posts:
    161
    HIHI~How can I set the Animation Clip (Override) at RunTime??
    Is it only possible in custom track?
    I want use TimeLine as a template.Want to feed character ,animationclip,specific position at running time ~~
     
  10. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    802
    I would recommend making a new thread for this different issue.
     
  11. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    413
    @fherbst Did you ever find a solution that worked for you?
     
  12. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    802