Skip to content

Commit

Permalink
Fix crash with missing scripts on the avatar.
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rkc0d3r committed Aug 2, 2024
1 parent decba43 commit 4ee2036
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## v3.7.4
### Bug Fixes
* Fix viseme blendshapes and eyelid blendshapes sometimes not getting reassigned correctly if they aren't on a mesh named `Body`.
* Fix crash with missing scripts on the avatar.

## v3.7.3
### Bug Fixes
Expand Down
26 changes: 25 additions & 1 deletion Editor/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,35 @@ public static IEnumerable<Transform> GetAllDescendants(this Transform transform)
}
}
}

public static Component[] GetNonNullComponents(this Transform transform)
{
var components = transform.GetComponents<Component>();
if (components.All(c => c != null))
{
return components;
}
var path = new List<string>();
var current = transform;
while (current != null)
{
path.Add(current.name);
current = current.parent;
}
string pathString = string.Join("/", path.Reverse<string>());
var nonNullComponents = components.Where(c => c != null).ToArray();
Debug.LogWarning($"Found {components.Length - nonNullComponents.Length} null components on {pathString}. You might be missing some scripts.");
return nonNullComponents;
}

public static Component[] GetNonNullComponents(this GameObject gameObject)
{
return gameObject.transform.GetNonNullComponents();
}
}

public static class AnimatorControllerExtensions
{

public static IEnumerable<AnimatorState> EnumerateAllStates(this AnimatorController controller)
{
var queue = new Queue<AnimatorStateMachine>();
Expand Down
14 changes: 7 additions & 7 deletions Editor/d4rkAvatarOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ private void DeleteAllUnusedSkinnedMeshRenderers()
{
var obj = skinnedMeshRenderer.gameObject;
DestroyImmediate(skinnedMeshRenderer);
if (!keepTransforms.Contains(obj.transform) && (obj.transform.childCount == 0 && obj.GetComponents<Component>().Length == 1))
if (!keepTransforms.Contains(obj.transform) && (obj.transform.childCount == 0 && obj.GetNonNullComponents().Length == 1))
DestroyImmediate(obj);
}
}
Expand Down Expand Up @@ -1287,7 +1287,7 @@ public bool IsAnimatableBinding(EditorCurveBinding binding) {
}
}
animatableBindings.Add(("ComponentExists", typeof(GameObject)));
foreach (var component in targetObject.GetComponents<Component>()) {
foreach (var component in targetObject.GetNonNullComponents()) {
animatableBindings.Add(("ComponentExists", component.GetType()));
}
if (targetObject.TryGetComponent(out VRCStation station)) {
Expand Down Expand Up @@ -2003,7 +2003,7 @@ bool IsPossibleBinding(EditorCurveBinding binding)
{
// AnimationUtility.GetAnimatableBindings(transform.gameObject, gameObject)
// is too slow, so we just check if the components mentioned in the bindings exist at that path
possibleTypeNames.UnionWith(transform.GetComponents<Component>().Select(c => c.GetType().FullName));
possibleTypeNames.UnionWith(transform.GetNonNullComponents().Select(c => c.GetType().FullName));
possibleTypeNames.Add(typeof(GameObject).FullName);
}
possibleBindingTypes[binding.path] = possibleTypeNames;
Expand Down Expand Up @@ -2224,7 +2224,7 @@ void AddDependency(Transform t, Object obj)
{
result[physBone].UnionWith(dependencies);
}
result[physBone].UnionWith(current.GetComponents<Component>().Where(c => c != physBone && !(c is Transform)));
result[physBone].UnionWith(current.GetNonNullComponents().Where(c => c != physBone && !(c is Transform)));
}
}

Expand Down Expand Up @@ -2774,7 +2774,7 @@ public HashSet<Component> FindAllUnusedComponents()
.Where(r => !behaviourToggles.Contains(GetPathToRoot(r))));

alwaysDisabledBehaviours.UnionWith(FindAllAlwaysDisabledGameObjects()
.SelectMany(t => t.GetComponents<Component>().Where(c => c != null && !(c is Transform))));
.SelectMany(t => t.GetNonNullComponents().Where(c => !(c is Transform))));

var exclusions = GetAllExcludedTransforms();

Expand Down Expand Up @@ -4643,7 +4643,7 @@ string CalculateNewBlendShapeName(string blendShapeName) {
{
var obj = combinableSkinnedMeshes[meshID].gameObject;
DestroyImmediate(combinableSkinnedMeshes[meshID]);
if (!keepTransforms.Contains(obj.transform) && (obj.transform.childCount == 0 && obj.GetComponents<Component>().Length == 1))
if (!keepTransforms.Contains(obj.transform) && (obj.transform.childCount == 0 && obj.GetNonNullComponents().Length == 1))
DestroyImmediate(obj);
}

Expand Down Expand Up @@ -4834,7 +4834,7 @@ private void DestroyUnusedGameObjects()

foreach (var obj in transform.GetAllDescendants())
{
if (obj.GetComponents<Component>().Length > 1)
if (obj.GetNonNullComponents().Length > 1)
{
used.Add(obj);
}
Expand Down

0 comments on commit 4ee2036

Please sign in to comment.