Search Unity

Question Animated Custom Properties with Timeline tracks

Discussion in 'Animation' started by theazz, Nov 1, 2022.

  1. theazz

    theazz

    Joined:
    Oct 20, 2013
    Posts:
    60
    Hey folks

    is there a way to get access to animated custom properties on animation clips played through timeline? Another thread mentions using.

    Code (CSharp):
    1. animator.BindStreamProperty(m_animator.transform, typeof(Animator), property);
    but I'm not much of a coder and I dunno how what to do with that.

    I wanna get the floats i've exported through from maya and use them driving some postprocessing stuff, ive got all that working, and it'd work fine through a state machine, but we need to do this through timeline (and have it evaluate when scrubbing / playing in edit mode).

    cheers
     
  2. julienb

    julienb

    Unity Technologies

    Joined:
    Sep 9, 2016
    Posts:
    177
    BindStreamProperty will not work with Timeline (the data is written in AnimationControllerPlayable and Timeline doesn't use this playable).

    To evaluate custom animated properties in Timeline, you will have to re-bind the properties to fields of a component. You can do this when the animation clip is imported.

    1- Create an asset post-processor and define a OnPostprocessGameObjectWithAnimatedUserProperties method. This will be called when an animation clip that has custom properties is imported.
    2- Rebind the custom properties to fields of a component. The example in the documentation shows how to rebind properties to a particle system, but you can use any component.
    3- In Timeline, create an Animation track and bind it to the GameObject that contains the Animator. Add the AnimationClip (the one that contains custom properties) and add it to the track. The custom properties should then be written to the component.
     
  3. theazz

    theazz

    Joined:
    Oct 20, 2013
    Posts:
    60
    Thanks very much @julienb - I've almost got it! I'm trying to take the example and make a more generic system where all animated custom properties are exposed to access via other things. In my code though, for some reason the property is getting bound to the new name (it goes dark blue in the inspector) but the value isn't changing when i scrub the timeline. I have a very simple class to hold the data.

    Possibly unrelated, but i also get "Importer(FBXImporter) generated inconsistent result" when i reimport it.

    Code (CSharp):
    1. public class AnimCustomData : MonoBehaviour
    2. {
    3.     [Serializable]
    4.     public class AnimCustomProperty
    5.     {
    6.         public string m_name;
    7.         public float m_value;
    8.     }
    9.  
    10.     [SerializeField]
    11.     public List<AnimCustomProperty> m_animCustomProperties = new List<AnimCustomProperty>();
    12. }
    13.  
    14. class AnimBinder : AssetPostprocessor
    15. {
    16.     void OnPostprocessGameObjectWithAnimatedUserProperties(GameObject go, EditorCurveBinding[] bindings)
    17.     {
    18.         if (bindings.Length != 0)
    19.         {
    20.             AnimCustomData animData = go.AddComponent<AnimCustomData>();
    21.  
    22.             for (int i = 0; i < bindings.Length; i++)
    23.             {
    24.                 animData.m_animCustomProperties.Add(new AnimCustomData.AnimCustomProperty());
    25.                 animData.m_animCustomProperties[i].m_name = bindings[i].propertyName;
    26.  
    27.                 bindings[i].propertyName = "m_animCustomProperties.Array.data[" + i + "].m_value";
    28.                 bindings[i].path = AnimationUtility.CalculateTransformPath(go.transform, go.transform.root);
    29.                 bindings[i].type = typeof(AnimCustomData);
    30.             }
    31.         }
    32.     }
    33. }
     
    Last edited: Nov 3, 2022
  4. julienb

    julienb

    Unity Technologies

    Joined:
    Sep 9, 2016
    Posts:
    177
  5. theazz

    theazz

    Joined:
    Oct 20, 2013
    Posts:
    60
    oh but
    m_value
    in my code is a float? or I literally can't address that property like this? So impossible to make a generic component that just has all the floats of all custom curves on a clip? Can i do this with a dictionary where the value type is float? I tried but got stuck finding the property name of a dict value.
     
  6. julienb

    julienb

    Unity Technologies

    Joined:
    Sep 9, 2016
    Posts:
    177
    Even though the animated value is indeed a float, the property path points to a list (m_animCustomProperties) of elements of a custom class type (AnimCustomProperty). That is not supported by the animation system.

    A dictionary cannot be animated. You'll have to add specific animatable values in AnimCustomData for each custom property in your clip.
     
  7. theazz

    theazz

    Joined:
    Oct 20, 2013
    Posts:
    60
    Aight, I will do just that then, thanks for your help!