Skip to content

Commit f3e1d60

Browse files
committed
Added a proper OSCmooth animation and file cleanup routine and removal button. Fixed accidental enumeration on list modification. Dynamic Playable Layer detection (now non-Humanoid rigs are recognized).
1 parent eaa61ad commit f3e1d60

File tree

5 files changed

+227
-62
lines changed

5 files changed

+227
-62
lines changed

.gitignore

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,30 @@ Library*
369369
ProjectSettings*
370370
Temp*
371371
VRC.SDKBase*
372-
VRC.Enums*
372+
VRC.Enums*
373+
/UnityEngine.UI.csproj
374+
/UnityEngine.TestRunner.csproj
375+
/UnityEditor.UI.csproj
376+
/UnityEditor.TestRunner.csproj
377+
/Unity.VSCode.Editor.csproj
378+
/Unity.VisualStudio.Editor.csproj
379+
/Unity.Timeline.Editor.csproj
380+
/Unity.Timeline.csproj
381+
/Unity.TextMeshPro.Editor.csproj
382+
/Unity.TextMeshPro.csproj
383+
/Unity.Rider.Editor.csproj
384+
/Unity.PlasticSCM.Editor.csproj
385+
/Unity.Mathematics.Editor.csproj
386+
/Unity.Mathematics.csproj
387+
/Unity.CollabProxy.Editor.csproj
388+
/Unity.Burst.Editor.csproj
389+
/Unity.Burst.csproj
390+
/OSCmooth.sln
391+
/Assets/_Sacred Bunny/Controllers/FX_1.3.controller
392+
/Assets/_Sacred Bunny/Controllers
393+
/Assets/Scenes/SampleScene.unity
394+
/Assets/Scenes
395+
/Assets/OSCmooth/Generated/Anims/Animator_354a5319244b7d64ca734626cd690e47
396+
/Assets
397+
/Assembly-CSharp.csproj
398+
/Assembly-CSharp-Editor.csproj

Assets/OSCmooth/Editor/OSCmoothAnimationHandler.cs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,40 @@
55
using UnityEngine.Jobs;
66
using OSCTools.OSCmooth.Util;
77
using OSCTools.OSCmooth.Types;
8+
using System.Linq;
89

910
namespace OSCTools.OSCmooth.Animation
1011
{
11-
public class OSCmoothAnimationHandler
12+
public static class OSCmoothAnimationHandler
1213
{
13-
public AnimatorController _animatorController;
14-
public List<OSCmoothParameter> _parameters;
15-
public bool _writeDefaults;
16-
public string _animExportDirectory;
17-
18-
public OSCmoothAnimationHandler() { }
19-
public OSCmoothAnimationHandler(List<OSCmoothParameter> smoothLayer, AnimatorController animatorController, bool writeDefaults = false)
20-
{
21-
_parameters = smoothLayer;
22-
_animatorController = animatorController;
23-
_writeDefaults = writeDefaults;
14+
public static AnimatorController _animatorController;
15+
public static List<OSCmoothParameter> _parameters;
16+
public static bool _writeDefaults;
17+
public static string _animExportDirectory;
18+
19+
public static void RemoveAllOSCmoothFromController()
20+
{
21+
AnimUtil.RevertStateMachineParameters(_animatorController);
22+
AnimUtil.RemoveExtendedParametersInController("OSCm", _animatorController);
23+
AnimUtil.RemoveContainingLayersInController("OSCm", _animatorController);
2424
}
25-
public void CreateSmoothAnimationLayer()
25+
public static void CreateSmoothAnimationLayer()
2626
{
27+
// Cleanup Animator before applying OSCmooth:
28+
OSCmoothAnimationHandler.RemoveAllOSCmoothFromController();
29+
30+
// Creating new OSCmooth setup.
2731
AnimatorControllerLayer animLayer;
2832

29-
// Looking for existing animation layer, and will delete it to replace with a new one. Will look into
30-
// creating a more thorough solution to much more effectively overwrite the existing layer for a future update.
31-
if(_writeDefaults)
32-
{
33-
AnimUtil.RemoveAnimLayerInController("_OSCmooth_Smoothing_Gen", _animatorController);
33+
if (_writeDefaults)
3434
animLayer = AnimUtil.CreateAnimLayerInController("_OSCmooth_Smoothing_WD_Gen", _animatorController);
35-
}
36-
else
37-
{
38-
AnimUtil.RemoveAnimLayerInController("_OSCmooth_Smoothing_WD_Gen", _animatorController);
39-
animLayer = AnimUtil.CreateAnimLayerInController("_OSCmooth_Smoothing_Gen", _animatorController);
40-
}
35+
else animLayer = AnimUtil.CreateAnimLayerInController("_OSCmooth_Smoothing_Gen", _animatorController);
4136

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

46-
if(_writeDefaults)
41+
if (_writeDefaults)
4742
{
4843
state[0] = animLayer.stateMachine.AddState("OSCmooth_Local_WD", new Vector3(30, 170, 0));
4944
state[1] = animLayer.stateMachine.AddState("OSCmooth_Net_WD", new Vector3(30, 170 + 60, 0));
@@ -53,7 +48,7 @@ public void CreateSmoothAnimationLayer()
5348
state[0] = animLayer.stateMachine.AddState("OSCmooth_Local", new Vector3(30, 170, 0));
5449
state[1] = animLayer.stateMachine.AddState("OSCmooth_Net", new Vector3(30, 170 + 60, 0));
5550
}
56-
51+
5752

5853
state[0].writeDefaultValues = _writeDefaults;
5954
state[1].writeDefaultValues = _writeDefaults;
@@ -76,7 +71,7 @@ public void CreateSmoothAnimationLayer()
7671
if (_writeDefaults)
7772
{
7873
nameLocalWD = "OSCm_Local_WD";
79-
nameRemoteWD = "OSCm_Remote_WD";
74+
nameRemoteWD = "OSCm_Remote_WD";
8075
}
8176

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

107102
// Creating a '1Set' parameter that holds a value of one at all times for the Direct BlendTree
108-
103+
109104
if (_writeDefaults)
110105
{
111106
ParameterUtil.CheckAndCreateParameter("OSCm/BlendSet", _animatorController, AnimatorControllerParameterType.Float, 1f);
@@ -133,9 +128,9 @@ public void CreateSmoothAnimationLayer()
133128
{
134129
motionLocal = AnimUtil.CreateSmoothingBlendTree(_animatorController, animLayer.stateMachine, p.localSmoothness, p.paramName, p.flipInputOutput, 1f, _animExportDirectory, "OSCm/Local/", "SmootherWD", "OSCm/Proxy/", "Proxy");
135130
motionRemote = AnimUtil.CreateSmoothingBlendTree(_animatorController, animLayer.stateMachine, p.remoteSmoothness, p.paramName, p.flipInputOutput, 1f, _animExportDirectory, "OSCm/Remote/", "SmootherRemoteWD", "OSCm/Proxy/", "Proxy");
136-
}
131+
}
137132

138-
localChildMotion.Add(new ChildMotion
133+
localChildMotion.Add(new ChildMotion
139134
{
140135
directBlendParameter = "OSCm/BlendSet",
141136
motion = motionLocal,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace OSCTools.OSCmooth
8+
{
9+
public static class OSCmoothFilters
10+
{
11+
public static readonly string[] BlackList =
12+
{
13+
"OSCm/", "IsLocal", "Smooth", "Proxy", "Proxy/", "_Float", "_Normalizer", "_FTI", "OSCm_BlendSet", "BlendSet", "Blend"
14+
};
15+
public static readonly string[] AllLayerNames =
16+
{
17+
"_OSCmooth_Smoothing_WD_Gen", "_OSCmooth_Smoothing_Gen"
18+
};
19+
public static readonly string[] ParameterExtensions =
20+
{
21+
"OSCm/Proxy/", "OSCm_Proxy"
22+
};
23+
}
24+
}

Assets/OSCmooth/Editor/OSCmoothUtil.cs

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using System.IO;
1+
using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
2+
using OSCTools.OSCmooth.Types;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
26
using UnityEditor;
37
using UnityEditor.Animations;
48
using UnityEngine;
@@ -62,16 +66,90 @@ public static void RenameAllStateMachineInstancesOfBlendParameter(AnimatorContro
6266
}
6367
}
6468

65-
public static void RemoveAnimLayerInController(string layerName, AnimatorController animatorController)
69+
public static List<string> GetAllStateMachineParameters(AnimatorController animatorController)
6670
{
67-
for (int i = 0; i < animatorController.layers.Length; i++)
71+
List<string> stateParams = new List<string>();
72+
73+
Object[] animatorAssets = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(animatorController));
74+
75+
foreach (Object asset in animatorAssets)
6876
{
69-
if (animatorController.layers[i].name == layerName)
77+
if (asset?.GetType() == typeof(BlendTree))
7078
{
71-
animatorController.RemoveLayer(i);
79+
if (!stateParams.Contains(((BlendTree)asset).blendParameter))
80+
stateParams.Add(((BlendTree)asset).blendParameter);
81+
82+
if (!stateParams.Contains(((BlendTree)asset).blendParameterY))
83+
stateParams.Add(((BlendTree)asset).blendParameterY);
84+
85+
for (int i = 0; i < ((BlendTree)asset).children.Length; i++)
86+
{
87+
if (!stateParams.Contains(((BlendTree)asset).children[i].directBlendParameter))
88+
stateParams.Add(((BlendTree)asset).children[i].directBlendParameter);
89+
}
90+
91+
continue;
92+
}
93+
94+
if (asset?.GetType() == typeof(AnimatorState))
95+
{
96+
if (!stateParams.Contains(((AnimatorState)asset).timeParameter))
97+
stateParams.Add(((AnimatorState)asset).timeParameter);
98+
99+
if (!stateParams.Contains(((AnimatorState)asset).speedParameter))
100+
stateParams.Add(((AnimatorState)asset).speedParameter);
101+
102+
if (!stateParams.Contains(((AnimatorState)asset).cycleOffsetParameter))
103+
stateParams.Add(((AnimatorState)asset).cycleOffsetParameter);
104+
105+
if (!stateParams.Contains(((AnimatorState)asset).mirrorParameter))
106+
stateParams.Add(((AnimatorState)asset).mirrorParameter);
72107
}
73108
}
74109

110+
return stateParams;
111+
}
112+
113+
public static void RevertStateMachineParameters(AnimatorController animatorController)
114+
{
115+
string[] stateParams = GetAllStateMachineParameters(animatorController).ToArray();
116+
117+
foreach (var oscmParam in OSCmoothFilters.ParameterExtensions)
118+
{
119+
foreach (var stateParam in stateParams)
120+
{
121+
if (stateParam.Contains(oscmParam))
122+
{
123+
RenameAllStateMachineInstancesOfBlendParameter(animatorController, stateParam, stateParam.Replace(oscmParam, ""));
124+
}
125+
}
126+
}
127+
}
128+
129+
public static void RemoveExtendedParametersInController(string name, AnimatorController animatorController)
130+
{
131+
for (int i = 0; i < animatorController.parameters.Length;)
132+
{
133+
if (animatorController.parameters[i].name.Contains(name))
134+
{
135+
animatorController.RemoveParameter(i);
136+
continue;
137+
}
138+
i++;
139+
}
140+
}
141+
142+
public static void RemoveContainingLayersInController(string name, AnimatorController animatorController)
143+
{
144+
for (int i = 0; i < animatorController.layers.Length;)
145+
{
146+
if (animatorController.layers[i].name.Contains(name))
147+
{
148+
animatorController.RemoveLayer(i);
149+
continue;
150+
}
151+
i++;
152+
}
75153
}
76154

77155
public static AnimatorControllerLayer CreateAnimLayerInController(string layerName, AnimatorController animatorController)
@@ -108,7 +186,15 @@ public static AnimatorControllerLayer CreateAnimLayerInController(string layerNa
108186
return layer;
109187
}
110188

111-
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)
189+
public static void RemoveAssociatedAnimatorFolder(AnimatorController animatorController)
190+
{
191+
string animatorGUID;
192+
long id;
193+
194+
AssetDatabase.TryGetGUIDAndLocalFileIdentifier(animatorController, out animatorGUID, out id);
195+
}
196+
197+
public static AnimationClip[] CreateFloatSmootherAnimation(AnimatorController animatorController, string paramName, string smoothSuffix, string proxyPrefix, string directory, float initThreshold = -1, float finalThreshold = 1, bool driveBase = false)
112198
{
113199
string animatorGUID;
114200
long id;
@@ -207,7 +293,7 @@ public static BlendTree CreateSmoothingBlendTree(AnimatorController animatorCont
207293
};
208294

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

212298
rootTree.AddChild(falseTree, driveBase ? 1f : 0f);
213299
rootTree.AddChild(trueTree, driveBase ? 0f : 1f);

0 commit comments

Comments
 (0)