modular-avatar/Packages/nadena.dev.modular-avatar/Editor/PluginDefinition/PluginDefinition.cs

211 lines
7.7 KiB
C#
Raw Normal View History

2023-09-17 17:26:06 +08:00
using nadena.dev.ndmf;
2023-08-05 14:47:03 +08:00
using nadena.dev.ndmf.animation;
2023-09-10 16:14:19 +08:00
using nadena.dev.ndmf.fluent;
2023-08-05 14:47:03 +08:00
using UnityEngine;
[assembly: ExportsPlugin(
typeof(nadena.dev.modular_avatar.core.editor.plugin.PluginDefinition)
)]
namespace nadena.dev.modular_avatar.core.editor.plugin
{
2023-09-10 16:14:19 +08:00
class PluginDefinition : Plugin<PluginDefinition>
2023-08-05 14:47:03 +08:00
{
public override string QualifiedName => "nadena.dev.modular-avatar";
2023-09-10 16:14:19 +08:00
public override string DisplayName => "Modular Avatar";
2023-09-10 16:14:19 +08:00
protected override void Configure()
{
Sequence seq = InPhase(BuildPhase.Resolving);
seq.Run(ResolveObjectReferences.Instance);
// Protect against accidental destructive edits by cloning the input controllers ASAP
seq.Run("Clone animators", AnimationUtil.CloneAllControllers);
2023-08-05 14:47:03 +08:00
seq = InPhase(BuildPhase.Transforming);
2023-09-10 16:14:19 +08:00
seq.WithRequiredExtension(typeof(ModularAvatarContext), _s1 =>
{
seq.Run(ClearEditorOnlyTags.Instance);
seq.Run(MeshSettingsPluginPass.Instance);
seq.Run(RenameParametersPluginPass.Instance);
seq.Run(MergeAnimatorPluginPass.Instance);
seq.Run(MenuInstallPluginPass.Instance);
seq.WithRequiredExtension(typeof(TrackObjectRenamesContext), _s2 =>
{
seq.Run(MergeArmaturePluginPass.Instance);
seq.Run(BoneProxyPluginPass.Instance);
seq.Run(VisibleHeadAccessoryPluginPass.Instance);
seq.Run(ReplaceObjectPluginPass.Instance);
});
seq.Run(BlendshapeSyncAnimationPluginPass.Instance);
seq.Run(PhysbonesBlockerPluginPass.Instance);
seq.Run("Fixup Expressions Menu", ctx =>
{
var maContext = ctx.Extension<ModularAvatarContext>().BuildContext;
FixupExpressionsMenuPass.FixupExpressionsMenu(maContext);
});
seq.Run("Rebind humanoid avatar", ctx =>
{
// workaround problem with avatar matching
// https://github.com/bdunderscore/modular-avatar/issues/430
var animator = ctx.AvatarRootObject.GetComponent<Animator>();
if (animator)
{
var avatar = animator.avatar;
animator.avatar = null;
// ReSharper disable once Unity.InefficientPropertyAccess
animator.avatar = avatar;
}
});
2023-09-10 16:14:19 +08:00
});
2023-09-10 16:14:19 +08:00
InPhase(BuildPhase.Optimizing)
.WithRequiredExtension(typeof(ModularAvatarContext),
s => s.Run(GCGameObjectsPluginPass.Instance));
}
2023-08-05 14:47:03 +08:00
}
/// <summary>
/// This plugin runs very early in order to resolve all AvatarObjectReferences to their
/// referent before any other plugins perform heirarchy manipulations.
/// </summary>
2023-09-10 16:14:19 +08:00
internal class ResolveObjectReferences : Pass<ResolveObjectReferences>
2023-08-05 14:47:03 +08:00
{
2023-09-10 16:14:19 +08:00
protected override void Execute(ndmf.BuildContext context)
2023-08-05 14:47:03 +08:00
{
foreach (var obj in context.AvatarRootObject.GetComponentsInChildren<AvatarTagComponent>())
{
obj.ResolveReferences();
}
}
}
2023-09-10 16:14:19 +08:00
abstract class MAPass<T> : Pass<T> where T : Pass<T>, new()
2023-08-05 14:47:03 +08:00
{
protected BuildContext MAContext(ndmf.BuildContext context)
{
return context.Extension<ModularAvatarContext>().BuildContext;
}
}
2023-09-10 16:14:19 +08:00
class ClearEditorOnlyTags : MAPass<ClearEditorOnlyTags>
2023-08-05 14:47:03 +08:00
{
2023-09-10 16:14:19 +08:00
protected override void Execute(ndmf.BuildContext context)
2023-08-05 14:47:03 +08:00
{
Traverse(context.AvatarRootTransform);
}
void Traverse(Transform obj)
{
// EditorOnly objects can be used for multiple purposes - users might want a camera rig to be available in
// play mode, for example. For now, we'll prune MA components from EditorOnly objects, but otherwise leave
// them in place when in play mode.
if (obj.CompareTag("EditorOnly"))
{
foreach (var component in obj.GetComponentsInChildren<AvatarTagComponent>(true))
{
UnityEngine.Object.DestroyImmediate(component);
}
}
else
{
foreach (Transform transform in obj)
{
Traverse(transform);
}
}
}
}
2023-09-10 16:14:19 +08:00
class MeshSettingsPluginPass : MAPass<MeshSettingsPluginPass>
2023-08-05 14:47:03 +08:00
{
2023-09-10 16:14:19 +08:00
protected override void Execute(ndmf.BuildContext context)
2023-08-05 14:47:03 +08:00
{
new MeshSettingsPass(MAContext(context)).OnPreprocessAvatar();
}
}
2023-09-10 16:14:19 +08:00
class RenameParametersPluginPass : MAPass<RenameParametersPluginPass>
2023-08-05 14:47:03 +08:00
{
2023-09-10 16:14:19 +08:00
protected override void Execute(ndmf.BuildContext context)
2023-08-05 14:47:03 +08:00
{
new RenameParametersHook().OnPreprocessAvatar(context.AvatarRootObject, MAContext(context));
}
}
2023-09-10 16:14:19 +08:00
class MergeAnimatorPluginPass : MAPass<MergeAnimatorPluginPass>
2023-08-05 14:47:03 +08:00
{
2023-09-10 16:14:19 +08:00
protected override void Execute(ndmf.BuildContext context)
2023-08-05 14:47:03 +08:00
{
new MergeAnimatorProcessor().OnPreprocessAvatar(context.AvatarRootObject, MAContext(context));
}
}
2023-09-10 16:14:19 +08:00
class MenuInstallPluginPass : MAPass<MenuInstallPluginPass>
2023-08-05 14:47:03 +08:00
{
2023-09-10 16:14:19 +08:00
protected override void Execute(ndmf.BuildContext context)
2023-08-05 14:47:03 +08:00
{
new MenuInstallHook().OnPreprocessAvatar(context.AvatarRootObject, MAContext(context));
}
}
2023-09-10 16:14:19 +08:00
class MergeArmaturePluginPass : MAPass<MergeArmaturePluginPass>
2023-08-05 14:47:03 +08:00
{
2023-09-10 16:14:19 +08:00
protected override void Execute(ndmf.BuildContext context)
2023-08-05 14:47:03 +08:00
{
// The animation database is currently only used by the merge armature hook; it should probably become
// an extension context instead.
MAContext(context).AnimationDatabase.Bootstrap(context.AvatarDescriptor);
new MergeArmatureHook().OnPreprocessAvatar(context, context.AvatarRootObject);
MAContext(context).AnimationDatabase.Commit();
}
}
2023-09-10 16:14:19 +08:00
class BoneProxyPluginPass : MAPass<BoneProxyPluginPass>
2023-08-05 14:47:03 +08:00
{
2023-09-10 16:14:19 +08:00
protected override void Execute(ndmf.BuildContext context)
2023-08-05 14:47:03 +08:00
{
new BoneProxyProcessor().OnPreprocessAvatar(context.AvatarRootObject);
}
}
2023-09-10 16:14:19 +08:00
class VisibleHeadAccessoryPluginPass : MAPass<VisibleHeadAccessoryPluginPass>
2023-08-05 14:47:03 +08:00
{
2023-09-10 16:14:19 +08:00
protected override void Execute(ndmf.BuildContext context)
2023-08-05 14:47:03 +08:00
{
new VisibleHeadAccessoryProcessor(context.AvatarDescriptor).Process(MAContext(context));
}
}
2023-09-10 16:14:19 +08:00
class ReplaceObjectPluginPass : MAPass<ReplaceObjectPluginPass>
2023-08-05 14:47:03 +08:00
{
2023-09-10 16:14:19 +08:00
protected override void Execute(ndmf.BuildContext context)
2023-08-05 14:47:03 +08:00
{
new ReplaceObjectPass(context).Process();
}
}
2023-09-10 16:14:19 +08:00
class BlendshapeSyncAnimationPluginPass : MAPass<BlendshapeSyncAnimationPluginPass>
2023-08-05 14:47:03 +08:00
{
2023-09-10 16:14:19 +08:00
protected override void Execute(ndmf.BuildContext context)
2023-08-05 14:47:03 +08:00
{
new BlendshapeSyncAnimationProcessor().OnPreprocessAvatar(context.AvatarRootObject, MAContext(context));
}
}
2023-09-10 16:14:19 +08:00
class PhysbonesBlockerPluginPass : MAPass<PhysbonesBlockerPluginPass>
2023-08-05 14:47:03 +08:00
{
2023-09-10 16:14:19 +08:00
protected override void Execute(ndmf.BuildContext context)
2023-08-05 14:47:03 +08:00
{
PhysboneBlockerPass.Process(context.AvatarRootObject);
}
}
2023-09-10 16:14:19 +08:00
class GCGameObjectsPluginPass : MAPass<GCGameObjectsPluginPass>
2023-08-05 14:47:03 +08:00
{
2023-09-10 16:14:19 +08:00
protected override void Execute(ndmf.BuildContext context)
2023-08-05 14:47:03 +08:00
{
new GCGameObjectsPass(MAContext(context), context.AvatarRootObject).OnPreprocessAvatar();
}
}
}