Skip to content

Commit

Permalink
Added a proper OSCmooth animation and file cleanup routine and remova…
Browse files Browse the repository at this point in the history
…l button. Fixed accidental enumeration on list modification. Dynamic Playable Layer detection (now non-Humanoid rigs are recognized).
  • Loading branch information
regzo2 committed Jan 18, 2023
1 parent eaa61ad commit f3e1d60
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 62 deletions.
28 changes: 27 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,30 @@ Library*
ProjectSettings*
Temp*
VRC.SDKBase*
VRC.Enums*
VRC.Enums*
/UnityEngine.UI.csproj
/UnityEngine.TestRunner.csproj
/UnityEditor.UI.csproj
/UnityEditor.TestRunner.csproj
/Unity.VSCode.Editor.csproj
/Unity.VisualStudio.Editor.csproj
/Unity.Timeline.Editor.csproj
/Unity.Timeline.csproj
/Unity.TextMeshPro.Editor.csproj
/Unity.TextMeshPro.csproj
/Unity.Rider.Editor.csproj
/Unity.PlasticSCM.Editor.csproj
/Unity.Mathematics.Editor.csproj
/Unity.Mathematics.csproj
/Unity.CollabProxy.Editor.csproj
/Unity.Burst.Editor.csproj
/Unity.Burst.csproj
/OSCmooth.sln
/Assets/_Sacred Bunny/Controllers/FX_1.3.controller
/Assets/_Sacred Bunny/Controllers
/Assets/Scenes/SampleScene.unity
/Assets/Scenes
/Assets/OSCmooth/Generated/Anims/Animator_354a5319244b7d64ca734626cd690e47
/Assets
/Assembly-CSharp.csproj
/Assembly-CSharp-Editor.csproj
55 changes: 25 additions & 30 deletions Assets/OSCmooth/Editor/OSCmoothAnimationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,40 @@
using UnityEngine.Jobs;
using OSCTools.OSCmooth.Util;
using OSCTools.OSCmooth.Types;
using System.Linq;

namespace OSCTools.OSCmooth.Animation
{
public class OSCmoothAnimationHandler
public static class OSCmoothAnimationHandler
{
public AnimatorController _animatorController;
public List<OSCmoothParameter> _parameters;
public bool _writeDefaults;
public string _animExportDirectory;

public OSCmoothAnimationHandler() { }
public OSCmoothAnimationHandler(List<OSCmoothParameter> smoothLayer, AnimatorController animatorController, bool writeDefaults = false)
{
_parameters = smoothLayer;
_animatorController = animatorController;
_writeDefaults = writeDefaults;
public static AnimatorController _animatorController;
public static List<OSCmoothParameter> _parameters;
public static bool _writeDefaults;
public static string _animExportDirectory;

public static void RemoveAllOSCmoothFromController()
{
AnimUtil.RevertStateMachineParameters(_animatorController);
AnimUtil.RemoveExtendedParametersInController("OSCm", _animatorController);
AnimUtil.RemoveContainingLayersInController("OSCm", _animatorController);
}
public void CreateSmoothAnimationLayer()
public static void CreateSmoothAnimationLayer()
{
// Cleanup Animator before applying OSCmooth:
OSCmoothAnimationHandler.RemoveAllOSCmoothFromController();

// Creating new OSCmooth setup.
AnimatorControllerLayer animLayer;

// Looking for existing animation layer, and will delete it to replace with a new one. Will look into
// creating a more thorough solution to much more effectively overwrite the existing layer for a future update.
if(_writeDefaults)
{
AnimUtil.RemoveAnimLayerInController("_OSCmooth_Smoothing_Gen", _animatorController);
if (_writeDefaults)
animLayer = AnimUtil.CreateAnimLayerInController("_OSCmooth_Smoothing_WD_Gen", _animatorController);
}
else
{
AnimUtil.RemoveAnimLayerInController("_OSCmooth_Smoothing_WD_Gen", _animatorController);
animLayer = AnimUtil.CreateAnimLayerInController("_OSCmooth_Smoothing_Gen", _animatorController);
}
else animLayer = AnimUtil.CreateAnimLayerInController("_OSCmooth_Smoothing_Gen", _animatorController);

// Creating a Direct BlendTree that will hold all of the smooth driver animations. This is to effectively create a 'sublayer'
// system within the Direct BlendTree to tidy up the animator base layers from bloating up visually.
AnimatorState[] state = new AnimatorState[2];

if(_writeDefaults)
if (_writeDefaults)
{
state[0] = animLayer.stateMachine.AddState("OSCmooth_Local_WD", new Vector3(30, 170, 0));
state[1] = animLayer.stateMachine.AddState("OSCmooth_Net_WD", new Vector3(30, 170 + 60, 0));
Expand All @@ -53,7 +48,7 @@ public void CreateSmoothAnimationLayer()
state[0] = animLayer.stateMachine.AddState("OSCmooth_Local", new Vector3(30, 170, 0));
state[1] = animLayer.stateMachine.AddState("OSCmooth_Net", new Vector3(30, 170 + 60, 0));
}


state[0].writeDefaultValues = _writeDefaults;
state[1].writeDefaultValues = _writeDefaults;
Expand All @@ -76,7 +71,7 @@ public void CreateSmoothAnimationLayer()
if (_writeDefaults)
{
nameLocalWD = "OSCm_Local_WD";
nameRemoteWD = "OSCm_Remote_WD";
nameRemoteWD = "OSCm_Remote_WD";
}

var basisLocalBlendTree = new BlendTree()
Expand Down Expand Up @@ -105,7 +100,7 @@ public void CreateSmoothAnimationLayer()
AssetDatabase.AddObjectToAsset(basisRemoteBlendTree, AssetDatabase.GetAssetPath(animLayer.stateMachine));

// Creating a '1Set' parameter that holds a value of one at all times for the Direct BlendTree

if (_writeDefaults)
{
ParameterUtil.CheckAndCreateParameter("OSCm/BlendSet", _animatorController, AnimatorControllerParameterType.Float, 1f);
Expand Down Expand Up @@ -133,9 +128,9 @@ public void CreateSmoothAnimationLayer()
{
motionLocal = AnimUtil.CreateSmoothingBlendTree(_animatorController, animLayer.stateMachine, p.localSmoothness, p.paramName, p.flipInputOutput, 1f, _animExportDirectory, "OSCm/Local/", "SmootherWD", "OSCm/Proxy/", "Proxy");
motionRemote = AnimUtil.CreateSmoothingBlendTree(_animatorController, animLayer.stateMachine, p.remoteSmoothness, p.paramName, p.flipInputOutput, 1f, _animExportDirectory, "OSCm/Remote/", "SmootherRemoteWD", "OSCm/Proxy/", "Proxy");
}
}

localChildMotion.Add(new ChildMotion
localChildMotion.Add(new ChildMotion
{
directBlendParameter = "OSCm/BlendSet",
motion = motionLocal,
Expand Down
24 changes: 24 additions & 0 deletions Assets/OSCmooth/Editor/OSCmoothFilters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OSCTools.OSCmooth
{
public static class OSCmoothFilters
{
public static readonly string[] BlackList =
{
"OSCm/", "IsLocal", "Smooth", "Proxy", "Proxy/", "_Float", "_Normalizer", "_FTI", "OSCm_BlendSet", "BlendSet", "Blend"
};
public static readonly string[] AllLayerNames =
{
"_OSCmooth_Smoothing_WD_Gen", "_OSCmooth_Smoothing_Gen"
};
public static readonly string[] ParameterExtensions =
{
"OSCm/Proxy/", "OSCm_Proxy"
};
}
}
100 changes: 93 additions & 7 deletions Assets/OSCmooth/Editor/OSCmoothUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.IO;
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
using OSCTools.OSCmooth.Types;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
Expand Down Expand Up @@ -62,16 +66,90 @@ public static void RenameAllStateMachineInstancesOfBlendParameter(AnimatorContro
}
}

public static void RemoveAnimLayerInController(string layerName, AnimatorController animatorController)
public static List<string> GetAllStateMachineParameters(AnimatorController animatorController)
{
for (int i = 0; i < animatorController.layers.Length; i++)
List<string> stateParams = new List<string>();

Object[] animatorAssets = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(animatorController));

foreach (Object asset in animatorAssets)
{
if (animatorController.layers[i].name == layerName)
if (asset?.GetType() == typeof(BlendTree))
{
animatorController.RemoveLayer(i);
if (!stateParams.Contains(((BlendTree)asset).blendParameter))
stateParams.Add(((BlendTree)asset).blendParameter);

if (!stateParams.Contains(((BlendTree)asset).blendParameterY))
stateParams.Add(((BlendTree)asset).blendParameterY);

for (int i = 0; i < ((BlendTree)asset).children.Length; i++)
{
if (!stateParams.Contains(((BlendTree)asset).children[i].directBlendParameter))
stateParams.Add(((BlendTree)asset).children[i].directBlendParameter);
}

continue;
}

if (asset?.GetType() == typeof(AnimatorState))
{
if (!stateParams.Contains(((AnimatorState)asset).timeParameter))
stateParams.Add(((AnimatorState)asset).timeParameter);

if (!stateParams.Contains(((AnimatorState)asset).speedParameter))
stateParams.Add(((AnimatorState)asset).speedParameter);

if (!stateParams.Contains(((AnimatorState)asset).cycleOffsetParameter))
stateParams.Add(((AnimatorState)asset).cycleOffsetParameter);

if (!stateParams.Contains(((AnimatorState)asset).mirrorParameter))
stateParams.Add(((AnimatorState)asset).mirrorParameter);
}
}

return stateParams;
}

public static void RevertStateMachineParameters(AnimatorController animatorController)
{
string[] stateParams = GetAllStateMachineParameters(animatorController).ToArray();

foreach (var oscmParam in OSCmoothFilters.ParameterExtensions)
{
foreach (var stateParam in stateParams)
{
if (stateParam.Contains(oscmParam))
{
RenameAllStateMachineInstancesOfBlendParameter(animatorController, stateParam, stateParam.Replace(oscmParam, ""));
}
}
}
}

public static void RemoveExtendedParametersInController(string name, AnimatorController animatorController)
{
for (int i = 0; i < animatorController.parameters.Length;)
{
if (animatorController.parameters[i].name.Contains(name))
{
animatorController.RemoveParameter(i);
continue;
}
i++;
}
}

public static void RemoveContainingLayersInController(string name, AnimatorController animatorController)
{
for (int i = 0; i < animatorController.layers.Length;)
{
if (animatorController.layers[i].name.Contains(name))
{
animatorController.RemoveLayer(i);
continue;
}
i++;
}
}

public static AnimatorControllerLayer CreateAnimLayerInController(string layerName, AnimatorController animatorController)
Expand Down Expand Up @@ -108,7 +186,15 @@ public static AnimatorControllerLayer CreateAnimLayerInController(string layerNa
return layer;
}

public static AnimationClip[] CreateFloatSmootherAnimation(AnimatorController animatorController, string paramName, string smoothSuffix, string proxyPrefix, string proxySuffix, string directory, float initThreshold = -1, float finalThreshold = 1, bool driveBase = false)
public static void RemoveAssociatedAnimatorFolder(AnimatorController animatorController)
{
string animatorGUID;
long id;

AssetDatabase.TryGetGUIDAndLocalFileIdentifier(animatorController, out animatorGUID, out id);
}

public static AnimationClip[] CreateFloatSmootherAnimation(AnimatorController animatorController, string paramName, string smoothSuffix, string proxyPrefix, string directory, float initThreshold = -1, float finalThreshold = 1, bool driveBase = false)
{
string animatorGUID;
long id;
Expand Down Expand Up @@ -207,7 +293,7 @@ public static BlendTree CreateSmoothingBlendTree(AnimatorController animatorCont
};

// Create smoothing anims
AnimationClip[] driverAnims = AnimUtil.CreateFloatSmootherAnimation(animatorController, paramName, smoothnessSuffix, proxyPrefix, proxySuffix, directory, -range, range, driveBase);
AnimationClip[] driverAnims = AnimUtil.CreateFloatSmootherAnimation(animatorController, paramName, smoothnessSuffix, proxyPrefix, directory, -range, range, driveBase);

rootTree.AddChild(falseTree, driveBase ? 1f : 0f);
rootTree.AddChild(trueTree, driveBase ? 0f : 1f);
Expand Down
Loading

0 comments on commit f3e1d60

Please sign in to comment.