Search Unity

  1. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  2. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  3. Unity 2017.1 is now released.
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  6. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

Modify graph by code

Discussion in 'Timeline' started by paintbox1, Aug 11, 2017 at 10:27 AM.

  1. paintbox1


    Jan 20, 2014
    Hi, I'm trying to create a voice over clip, which in my imagination consists of
    one audio clip + multiple viseme clips(changes of the mouth blendshape values according to the heard phonemes at the corresponding timestamps)

    The voice over clip gets all the information from an ExposedReference to a custom voice over Asset.

    1. Can I embed audio and viseme clips in my voice over clip?
    2. Can I automatically create these audio and viseme "subclips" when the ExposedReference to the asset in the voice over clip gets assigned? Or maybe by a button near the ExposedReference field?

    I'd be grateful for any suggestions.

  2. seant_unity


    Unity Technologies

    Aug 25, 2015
    That sounds super interesting!

    So here are a few suggestions that might help.
    - If your custom voice over is an Asset (i.e. on disk, and not in scene), you don't need an exposed reference. Just a regular C# reference will work.
    - For the audio, your best option is to create and reference a separate audio track. The timeline API lets you create/move/modify/delete clips and tracks.
    - You can create a custom editor for your voice over clip playable asset to add buttons. Or, probably better, is create a custom editor for your custom track that does the audio track management. The tracks give you much more access to timeline data than the clip playable assets do.

    Hope that helps!!
    paintbox1 likes this.
  3. paintbox1


    Jan 20, 2014
    Thanks, that really helped me! (I've sent you a PM)

    But now after I created the audio and viseme tracks and clips the timeline editor doesnt refresh automatically. Can I somehow force this when I'm done?
  4. seant_unity


    Unity Technologies

    Aug 25, 2015
    Ah right -- we've nothing exposed in the public API to do that. I will definitely put that on our backlog as something that is necessary to add.

    I did a bit of hunting for a workaround until we can add that, and here's what I came up with. Here's a bit of background in case someone comes up with something better

    The timeline window has a few different refresh modes
    - basic refresh just redraws and evaluates the Timeline playable graph. Most common. (fast)
    - RebuildGraph will rebuild the timeline graph, but keeps the same view. Used when data is modified. (slower)
    - RebuildView will rebuild the timeline view. Used when clips and tracks are added or removed. (also rebuilds the graph). (slowest)

    In your case you need to do it all. The workaround is using the Undo system. This code tricks the TimelineWindow into doing rebuilding everything. It's not pretty but it works.

    Alternately you can try changing the selection over multiple frames.

    In the code the 'RebuildTimeline' object could be your track.

    Code (CSharp):
    1.     public override void OnInspectorGUI()
    2.     {
    3.         if (GUILayout.Button("Rebuild"))
    4.         {
    5.             var dummyObject = target as RebuildTimeline;
    6.             var timeline = TimelineEditor.timelineAsset;
    7.             if (timeline != null)
    8.             {
    9.                 timeline.CreateTrack<AnimationTrack>(null, "Blah");
    11.                 // fake an undo with Timeline in the name to cause a rebuild
    12.                 Undo.IncrementCurrentGroup();
    13.                 Undo.RecordObject(dummyObject, "Timeline Dummy Undo");
    14.                 dummyObject.dummy = dummyObject.dummy + 1;
    15.                 Undo.PerformUndo();
    16.                 var window = EditorWindow.GetWindow<EditorWindow>(false, "Timeline", false);
    17.                 if (window)
    18.                     window.Repaint();
    19.                 EditorGUIUtility.ExitGUI();
    20.             }
    21.         }
    renaudbedard_SEM likes this.
  5. paintbox1


    Jan 20, 2014
    Wow, didn't expect support on the weekend.

    Seems you're working with a newer API version. There's nothing called TimelineEditor in my version of unity.
    But I can just access the timeline through my track.

    Code (csharp):
    2.     private VoiceOverTrack track
    3.     {
    4.         get
    5.         {
    6.             return as VoiceOverTrack;
    7.         }
    8.     }
    10.     private void UpdateTimelineEditorView()
    11.     {
    12.         var timeline = this.track.timelineAsset;
    13.         if (timeline != null)
    14.         {
    15.             // fake an undo with Timeline in the name to cause a rebuild
    16.             Undo.IncrementCurrentGroup();
    17.             Undo.RecordObject(this.track, "Timeline Dummy Undo");
    18.             this.track.muted = true;
    19.             Undo.PerformUndo();
    20.             var window = EditorWindow.GetWindow<EditorWindow>(false, "Timeline", false);
    21.             if (window)
    22.                 window.Repaint();
    23.             EditorGUIUtility.ExitGUI();
    24.         }
    25.     }
    Calling this method after the modifications instantly displays the changes.
    Thanks again!