Search Unity

Question Postprocessing an FBX Animation clip

Discussion in 'Asset Importing & Exporting' started by RealPlanet, Jan 4, 2023.

  1. RealPlanet

    RealPlanet

    Joined:
    Mar 22, 2021
    Posts:
    3
    Hello,
    I've wrote a small postprocessor which reads a specific file which is imported along with the FBX model file.
    A bunch of animation events are then generated for all the animation clips contained inside the FBX file.
    At the end of this operation a warning is generated:

    Importer(FBXImporter) generated inconsistent result for asset(guid:f5306b774d35da644b532d4ffe0037f2)


    I'm guessing this happens because the model itself DOES NOT display the animation events in the inspector
    upload_2023-1-4_10-46-19.png

    But inspecting the animation clip itself shows that the process completed successfully:
    upload_2023-1-4_10-46-48.png

    Is it possible to fix this?

    Code:
    Code (CSharp):
    1.     private void OnPostprocessAnimation(GameObject root, AnimationClip clip)
    2.     {
    3.         ParseFileForClip(clip);
    4.     }
    5.  
    6.     private void ParseFileForClip(AnimationClip clip)
    7.     {
    8.         string markerPath = Path.ChangeExtension(assetPath, ".xml");
    9.         if (!File.Exists(markerPath))
    10.             return;
    11.  
    12.         using (FileStream fileReader = File.OpenRead(markerPath))
    13.         {
    14.             XmlSerializer ser = new(typeof(Blender.Scene));
    15.             Blender.Scene scene = (Blender.Scene)ser.Deserialize(fileReader);
    16.             fileReader.Close();
    17.             if (scene == null)
    18.                 return;
    19.  
    20.             List<string> names = new();
    21.  
    22.             IEnumerable<Blender.Scene.Marker> markers = scene.Actions.SelectMany(a => a.Markers);
    23.             List<AnimationEvent> processedEvents = new();
    24.  
    25.             foreach (Blender.Scene.Marker marker in markers)
    26.             {
    27.              
    28.  
    29.                 string[] tokens = marker.Name.Split("@");
    30.                 if(tokens.Length != 2)
    31.                 {
    32.                     Debug.LogWarning($"Unsupported notetrack format! | {marker.Name}");
    33.                     return;
    34.                 }
    35.  
    36.                 string ntType = tokens[0];
    37.                 string ntValue = tokens[1];
    38.  
    39.                 switch (ntType)
    40.                 {
    41.                     case "evt":
    42.                         Debug.Log("Adding GENERIC event: " + ntValue);
    43.                         processedEvents.Add(ToAnimationEvent("OnNotetrack", ntValue, marker.Frame / scene.FPS));
    44.                         break;
    45.                     case "sndnt":
    46.                         Debug.Log("Adding SOUND event: " + ntValue);
    47.                         processedEvents.Add(ToAnimationEvent("OnSoundNotetrack", ntValue, marker.Frame / scene.FPS));
    48.                         break;
    49.                     default:
    50.                         Debug.Log("Unknown notetrack: " + marker.Name);
    51.                         break;
    52.                 }
    53.             }
    54.  
    55.             AnimationUtility.SetAnimationEvents(clip, processedEvents.ToArray());
    56.             Debug.Log($"Added animation events to clip '{assetPath}'");
    57.         }
    58.     }
    The Blender.Scene type is only used for deserializing the data, it can be ignored for this question!
    Thank you all!
     
  2. RealPlanet

    RealPlanet

    Joined:
    Mar 22, 2021
    Posts:
    3
    Update:
    The warning disappeared once i deleted the fbx and rexported it from Blender, the events between inspector/animation clip is still incorrect but at least no more warnings. Moving this code to onModelPostProcess did somewhat fix the issue but happened to make a new one. Event times are correctly calculated and displayed for the clip but will generate an import warning (out of range timings) in the fbx inspector.
     
  3. bastien_humeau

    bastien_humeau

    Unity Technologies

    Joined:
    Jun 14, 2017
    Posts:
    191
    Hello!

    The `generated inconsistent result` warning usually lets you know that for the same tracked dependencies of an asset, a different result was created during the import.

    Given that you are using an extra file to modify the result of the animation clips, you should declare a dependency on that file so that the AssetDatabase knows that if this file changes your fbx need to be re-imported.

    Your assetpostprocessor should call context.DependsOnSourceAsset("path/to/your/event/data/file"); to make sure the dependency is correctly declared.
    You should probably also implements the GetVersion method to change its version value whenever you're changing the AssetPostprocessor code.
    This will ensure the warning won't happen again, and that the AssetDatabase caching is reliable for your assets.

    Regarding the events themselves, they don't show up in the importer events because we don't read them from the clips themselves but from the ModelImporter.clipAnimations data which contains an events array.
    You could change your code to modify this information directly from OnPreprocessModel instead of modifying the clips in OnPostprocessAnimation, but I think our internal mechanism to do that is not that great and you're probably better off modifying the clips directly if you don't mind not seeing the data on the importer.
     
    RealPlanet likes this.
  4. RealPlanet

    RealPlanet

    Joined:
    Mar 22, 2021
    Posts:
    3
    Thank you, I appreciate the help!
    I did try to use OnPostprocessModel to modified the fbx clips directly but i was getting some weird results.
    The events itself did show up correctly inspecting the clip, but the model inspector would report out of range events and the events would be shown on the wrong frames. As you mentioned I don't mind the events not showing up in the inspector so I will keep the original code and add the dependency to avoid the warning! :)
     
    bastien_humeau likes this.