modular-avatar/Editor/PluginDefinition/PluginDefinition.cs

271 lines
9.7 KiB
C#
Raw Normal View History

#region
using System;
using nadena.dev.modular_avatar.animation;
using nadena.dev.modular_avatar.core.editor.plugin;
using nadena.dev.modular_avatar.editor.ErrorReporting;
using nadena.dev.ndmf;
2023-09-10 16:14:19 +08:00
using nadena.dev.ndmf.fluent;
2023-08-05 14:47:03 +08:00
using UnityEngine;
using Object = UnityEngine.Object;
#endregion
2023-08-05 14:47:03 +08:00
[assembly: ExportsPlugin(
typeof(PluginDefinition)
2023-08-05 14:47:03 +08:00
)]
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";
public override Texture2D LogoTexture => LogoDisplay.LOGO_ASSET;
// 00a0e9
public override Color? ThemeColor => new Color(0x00 / 255f, 0xa0 / 255f, 0xe9 / 255f, 1);
protected override void OnUnhandledException(Exception e)
{
BuildReport.LogException(e);
}
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);
seq.Run("Validate configuration",
context => ComponentValidation.ValidateAll(context.AvatarRootObject));
2023-09-10 16:14:19 +08:00
seq.WithRequiredExtension(typeof(ModularAvatarContext), _s1 =>
{
seq.Run(ClearEditorOnlyTags.Instance);
seq.Run(MeshSettingsPluginPass.Instance);
seq.Run(ScaleAdjusterPass.Instance).PreviewingWith(new ScaleAdjusterPreview());
#if MA_VRCSDK3_AVATARS
seq.Run(ReactiveObjectPrepass.Instance);
2023-09-10 16:14:19 +08:00
seq.Run(RenameParametersPluginPass.Instance);
seq.Run(ParameterAssignerPass.Instance);
seq.Run(MergeBlendTreePass.Instance);
2023-09-10 16:14:19 +08:00
seq.Run(MergeAnimatorPluginPass.Instance);
seq.Run(ApplyAnimatorDefaultValuesPass.Instance);
#endif
seq.WithRequiredExtension(typeof(AnimationServicesContext), _s2 =>
2023-09-10 16:14:19 +08:00
{
#if MA_VRCSDK3_AVATARS
seq.Run("Shape Changer", ctx => new ReactiveObjectPass(ctx).Execute())
.PreviewingWith(new ShapeChangerPreview(), new ObjectSwitcherPreview(), new MaterialSetterPreview());
// TODO: We currently run this above MergeArmaturePlugin, because Merge Armature might destroy
// game objects which contain Menu Installers. It'd probably be better however to teach Merge Armature
// to retain those objects? maybe?
seq.Run(MenuInstallPluginPass.Instance);
#endif
2023-09-10 16:14:19 +08:00
seq.Run(MergeArmaturePluginPass.Instance);
seq.Run(BoneProxyPluginPass.Instance);
#if MA_VRCSDK3_AVATARS
2023-09-10 16:14:19 +08:00
seq.Run(VisibleHeadAccessoryPluginPass.Instance);
#endif
2023-09-24 15:59:02 +08:00
seq.Run("World Fixed Object",
ctx => new WorldFixedObjectProcessor().Process(ctx)
2023-09-24 15:59:02 +08:00
);
2023-09-10 16:14:19 +08:00
seq.Run(ReplaceObjectPluginPass.Instance);
#if MA_VRCSDK3_AVATARS
seq.Run(BlendshapeSyncAnimationPluginPass.Instance);
seq.Run(GameObjectDelayDisablePass.Instance);
#endif
2024-08-17 09:52:04 +08:00
seq.Run(ConstraintConverterPass.Instance);
2023-09-10 16:14:19 +08:00
});
#if MA_VRCSDK3_AVATARS
seq.Run(PhysbonesBlockerPluginPass.Instance);
seq.Run("Fixup Expressions Menu", ctx =>
{
var maContext = ctx.Extension<ModularAvatarContext>().BuildContext;
FixupExpressionsMenuPass.FixupExpressionsMenu(maContext);
});
2024-12-02 04:30:49 +08:00
seq.Run(SyncParameterSequencePass.Instance);
#endif
2024-12-02 04:30:49 +08:00
seq.Run(RemoveVertexColorPass.Instance).PreviewingWith(new RemoveVertexColorPreview());
seq.Run(RebindHumanoidAvatarPass.Instance);
seq.Run("Purge ModularAvatar components", ctx =>
{
foreach (var component in ctx.AvatarRootTransform.GetComponentsInChildren<AvatarTagComponent>(true))
{
Object.DestroyImmediate(component);
2024-03-09 16:57:15 +08:00
}
foreach (var component in ctx.AvatarRootTransform.GetComponentsInChildren<MAMoveIndependently>(true))
2024-03-09 16:57:15 +08:00
{
Object.DestroyImmediate(component);
}
});
#if MA_VRCSDK3_AVATARS
seq.Run(PruneParametersPass.Instance);
#endif
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))
{
Object.DestroyImmediate(component);
2023-08-05 14:47:03 +08:00
}
}
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();
}
}
#if MA_VRCSDK3_AVATARS
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));
}
}
#endif
2023-08-05 14:47:03 +08:00
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
{
new MergeArmatureHook().OnPreprocessAvatar(context, context.AvatarRootObject);
}
}
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);
}
}
#if MA_VRCSDK3_AVATARS
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(MAContext(context)).Process();
2023-08-05 14:47:03 +08:00
}
}
#endif
2023-08-05 14:47:03 +08:00
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();
}
}
#if MA_VRCSDK3_AVATARS
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(MAContext(context));
2023-08-05 14:47:03 +08:00
}
}
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);
}
}
#endif
2023-08-05 14:47:03 +08:00
class RebindHumanoidAvatarPass : MAPass<RebindHumanoidAvatarPass>
{
protected override void Execute(ndmf.BuildContext context)
{
new RebindHumanoidAvatar(context).Process();
}
}
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();
}
}
}