Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssetPreprocessor to do jaw close fix in bullk #70

Open
wilg opened this issue Feb 8, 2023 · 0 comments
Open

AssetPreprocessor to do jaw close fix in bullk #70

wilg opened this issue Feb 8, 2023 · 0 comments

Comments

@wilg
Copy link

wilg commented Feb 8, 2023

I really didn't want to manually go through all my animations and process them one-by-one and then hook them up to new files. So I made an AssetPreprocessor that will do this on import.

The way it works is you add an Asset Label of CloseCCJaw to any models that you want to fix the jaw on. Then reimport them.

image

Here's the code if you would like to clean it up and include it in the package.

using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class JawCloserImporter : AssetPostprocessor {

    void OnPostprocessAnimation(GameObject root, AnimationClip clip) {
        if (AssetDatabase.GetLabels(assetImporter).Contains("CloseCCJaw")) {
            FixJaw(clip);
        }
    }

    public uint GetVersion() {
        return 2;
    }

    const string jawClose = "Jaw Close";

    static void FixJaw(AnimationClip inputClip) {

        var found = false;
        var curveBindings = AnimationUtility.GetCurveBindings(inputClip);
        var targetBinding = new EditorCurveBinding();
        var jawCurve = new AnimationCurve();
        Keyframe[] jawKeys;

        foreach (var binding in curveBindings) {
            if (binding.propertyName.Equals(jawClose)) {
                targetBinding = binding;
                found = true;
            }
        }

        if (found) {
            jawCurve = AnimationUtility.GetEditorCurve(inputClip, targetBinding);
        }
        else {
            targetBinding = new EditorCurveBinding() { propertyName = jawClose, type = typeof(Animator) };
            jawKeys = new Keyframe[] {
                    new Keyframe( 0f, 0f ),
                    new Keyframe( inputClip.length, 0f )
                };
            jawCurve.keys = jawKeys;
        }
        jawKeys = jawCurve.keys;
        for (int i = 0; i < jawKeys.Length; i++) {
            jawKeys[i].value = 1;
        }
        jawCurve.keys = jawKeys;

        AnimationUtility.SetEditorCurve(inputClip, targetBinding, jawCurve);

    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant