Files
UnrealEngineUWP/Engine/Source/Editor/AnimGraph/Private/AnimBlueprintExtension.cpp

291 lines
9.7 KiB
C++
Raw Normal View History

Anim node data/compiler refactor Per-node constant data is now held on a generated struct as part of sparse class data. Per-node mutable data (i.e. pin links/property access mappings) is now held on a generated 'mutable data' struct that is compiled as part of the generated class. The anim BP compiler is now extended more conventionally using UAnimBlueprintExtension, derived from UBlueprintExtension. This directly replaces the older 'compiler handler' pattern that was added in an emergency fashion for 4.26. Anim graph nodes now request their required extensions and these are held on the UAnimBlueprint in the UBlueprint::Extensions array. The Extensions array is potentially refreshed with any node addition or removal. The Extensions array is force-refreshed each time an anim BP is compiled for the first time to deal with newly added or removed requirements. Const-corrected a bunch of UAnimInstance/FAnimInstanceProxy APIs that rely on (now truly) const data. Added a split state/constant version of FInputScaleBiasClamp to allow some of its data to be split into constants. Tweaked alignment/ordering of FPoseLinkBase to save a few bytes per pose link. Deprecated FAnimNode_Base::OverrideAsset in favor of a more UAnimGraphNode_Base-based approach. Individual nodes can still have runtime overrides via specific accessors. The new approach will also give us the oppurtunity to override multiple assets per node if required in the future. Moved property access into Engine module & removed event support from it - this was never used. Reworked property access compilation API a little - construction/lifetime was a bit confusing previously. Optimized path used to create UK2Node_StructMemberSet nodes in per-node custom events. When using mutable data, the structure used is large and very sparsely connected (i.e. only a few properties are written) so we only create pins that are actually going to be used, rather than creating all of them and conly connecting a few. Patched the following nodes to use the new data approach: - Asset players (sequences, blendspaces, aim offsets) - Blend lists - Ref poses - Roots #rb Jurre.deBaare, Martin.Wilson, Keith.Yerex [CL 16090510 by Thomas Sarkanen in ue5-main branch]
2021-04-22 04:57:09 -04:00
// Copyright Epic Games, Inc. All Rights Reserved.
#include "AnimBlueprintExtension.h"
#include "Animation/AnimSubsystem.h"
#include "Animation/AnimSubsystemInstance.h"
#include "IAnimBlueprintCopyTermDefaultsContext.h"
#include "Animation/AnimBlueprint.h"
#include "Templates/SubclassOf.h"
#include "AnimBlueprintExtension_Base.h"
#include "AnimBlueprintExtension_Attributes.h"
#include "AnimBlueprintExtension_PropertyAccess.h"
#include "Kismet2/BlueprintEditorUtils.h"
Anim blueprint encapsulation improvements Adds 'template' anim BP concept. These anim BPs have no TargetSkeleton and as such cannot have direct references to animations placed inside of their anim graphs Adds function call support from anim nodes. All anim graph nodes can now call functions when initialized, updated, evaluated or when they first become relevant. Relevancy is established using a new FAnimSubsystemInstance_NodeRelevancy which tracks nodes in a local map, per UAnimInstance, if nodes require it. Functions are displayed on the node if bound, and in the details panel. Added a new override point to FAnimSubsystemInstance to allow for WT init. Moved FMemberReference customization into a public header so it can be used on anim node functions. Wrapped functions up into FAnimNodeFunctionRef structure so they can be re-used more effectively. Converted CallFunction node to use them. Added a couple of simple BP function libraries to demonstrate the use of the new FAnimNodeContext (this is intended to be a generic way of exposing FAnimNode_Base types to script without the node types themselves having to be known to script directly). Added the ability to set exposed properties as 'always dynamic' so they appear in mutable data rather than being merged into constants. This allows for the anim node data API to be changed to be less strict (and more script friendly). Now values can be set in mutable data (via GET_MUTABLE_ANIM_NODE_DATA_PTR) and attempted sets to constant data can be ignored and reported to client code. A few minor crash fixes with edge cases (inc when trying to open an asset editor fails because of a missing skeleton/cancellation). Also fixes an issue where literal linked anim graph/control rig/custom poroperty node inputs didnt get copied #rb Jurre.deBaare,Aaron.Cox [CL 16703644 by Thomas Sarkanen in ue5-main branch]
2021-06-17 08:58:34 -04:00
#include "AnimBlueprintExtension_NodeRelevancy.h"
#include "AnimBlueprintExtension_Tag.h"
Anim node data/compiler refactor Per-node constant data is now held on a generated struct as part of sparse class data. Per-node mutable data (i.e. pin links/property access mappings) is now held on a generated 'mutable data' struct that is compiled as part of the generated class. The anim BP compiler is now extended more conventionally using UAnimBlueprintExtension, derived from UBlueprintExtension. This directly replaces the older 'compiler handler' pattern that was added in an emergency fashion for 4.26. Anim graph nodes now request their required extensions and these are held on the UAnimBlueprint in the UBlueprint::Extensions array. The Extensions array is potentially refreshed with any node addition or removal. The Extensions array is force-refreshed each time an anim BP is compiled for the first time to deal with newly added or removed requirements. Const-corrected a bunch of UAnimInstance/FAnimInstanceProxy APIs that rely on (now truly) const data. Added a split state/constant version of FInputScaleBiasClamp to allow some of its data to be split into constants. Tweaked alignment/ordering of FPoseLinkBase to save a few bytes per pose link. Deprecated FAnimNode_Base::OverrideAsset in favor of a more UAnimGraphNode_Base-based approach. Individual nodes can still have runtime overrides via specific accessors. The new approach will also give us the oppurtunity to override multiple assets per node if required in the future. Moved property access into Engine module & removed event support from it - this was never used. Reworked property access compilation API a little - construction/lifetime was a bit confusing previously. Optimized path used to create UK2Node_StructMemberSet nodes in per-node custom events. When using mutable data, the structure used is large and very sparsely connected (i.e. only a few properties are written) so we only create pins that are actually going to be used, rather than creating all of them and conly connecting a few. Patched the following nodes to use the new data approach: - Asset players (sequences, blendspaces, aim offsets) - Blend lists - Ref poses - Roots #rb Jurre.deBaare, Martin.Wilson, Keith.Yerex [CL 16090510 by Thomas Sarkanen in ue5-main branch]
2021-04-22 04:57:09 -04:00
// Set used to refresh extensions. Checks that an extension has a reference from an anim node for each refresh.
static TSet<TSubclassOf<UAnimBlueprintExtension>> RefreshSet;
static bool GIsRefreshingExtensions = false;
UAnimBlueprintExtension* UAnimBlueprintExtension::RequestExtension(UAnimBlueprint* InAnimBlueprint, TSubclassOf<UAnimBlueprintExtension> InExtensionType)
{
// Do not use RequestExtension when a blueprint is being compiled. Extensions should be consistent throughout all compilation stages.
check(!InAnimBlueprint->bBeingCompiled);
if(GIsRefreshingExtensions)
{
RefreshSet.Add(InExtensionType);
}
// Look for an existing extension
if(UAnimBlueprintExtension* ExistingExtension = GetExtension(InAnimBlueprint, InExtensionType))
{
return ExistingExtension;
}
// Not found, create one
UAnimBlueprintExtension* NewExtension = NewObject<UAnimBlueprintExtension>(InAnimBlueprint, InExtensionType.Get());
InAnimBlueprint->Extensions.Add(NewExtension);
return NewExtension;
}
UAnimBlueprintExtension* UAnimBlueprintExtension::GetExtension(UAnimBlueprint* InAnimBlueprint, TSubclassOf<UAnimBlueprintExtension> InExtensionType)
{
// Look for an existing extension
for(UBlueprintExtension* Extension : InAnimBlueprint->Extensions)
{
if(Extension && Extension->GetClass() == InExtensionType)
{
return CastChecked<UAnimBlueprintExtension>(Extension);
}
}
return nullptr;
}
TArray<UAnimBlueprintExtension*> UAnimBlueprintExtension::GetExtensions(UAnimBlueprint* InAnimBlueprint)
{
TArray<UAnimBlueprintExtension*> Extensions;
for(UBlueprintExtension* Extension : InAnimBlueprint->Extensions)
{
if(Extension && Extension->GetClass()->IsChildOf(UAnimBlueprintExtension::StaticClass()))
{
Extensions.Add(CastChecked<UAnimBlueprintExtension>(Extension));
}
}
return Extensions;
}
void UAnimBlueprintExtension::RequestExtensionsForNode(UAnimGraphNode_Base* InAnimGraphNode)
{
if(UAnimBlueprint* AnimBlueprint = InAnimGraphNode->GetAnimBlueprint())
{
TArray<TSubclassOf<UAnimBlueprintExtension>> ExtensionClasses =
{
UAnimBlueprintExtension_Base::StaticClass(),
UAnimBlueprintExtension_Attributes::StaticClass(),
UAnimBlueprintExtension_PropertyAccess::StaticClass()
};
Anim blueprint encapsulation improvements Adds 'template' anim BP concept. These anim BPs have no TargetSkeleton and as such cannot have direct references to animations placed inside of their anim graphs Adds function call support from anim nodes. All anim graph nodes can now call functions when initialized, updated, evaluated or when they first become relevant. Relevancy is established using a new FAnimSubsystemInstance_NodeRelevancy which tracks nodes in a local map, per UAnimInstance, if nodes require it. Functions are displayed on the node if bound, and in the details panel. Added a new override point to FAnimSubsystemInstance to allow for WT init. Moved FMemberReference customization into a public header so it can be used on anim node functions. Wrapped functions up into FAnimNodeFunctionRef structure so they can be re-used more effectively. Converted CallFunction node to use them. Added a couple of simple BP function libraries to demonstrate the use of the new FAnimNodeContext (this is intended to be a generic way of exposing FAnimNode_Base types to script without the node types themselves having to be known to script directly). Added the ability to set exposed properties as 'always dynamic' so they appear in mutable data rather than being merged into constants. This allows for the anim node data API to be changed to be less strict (and more script friendly). Now values can be set in mutable data (via GET_MUTABLE_ANIM_NODE_DATA_PTR) and attempted sets to constant data can be ignored and reported to client code. A few minor crash fixes with edge cases (inc when trying to open an asset editor fails because of a missing skeleton/cancellation). Also fixes an issue where literal linked anim graph/control rig/custom poroperty node inputs didnt get copied #rb Jurre.deBaare,Aaron.Cox [CL 16703644 by Thomas Sarkanen in ue5-main branch]
2021-06-17 08:58:34 -04:00
if(InAnimGraphNode->InitialUpdateFunction.ResolveMember<UFunction>(InAnimGraphNode->GetBlueprintClassFromNode()) != nullptr ||
InAnimGraphNode->BecomeRelevantFunction.ResolveMember<UFunction>(InAnimGraphNode->GetBlueprintClassFromNode()) != nullptr)
Anim blueprint encapsulation improvements Adds 'template' anim BP concept. These anim BPs have no TargetSkeleton and as such cannot have direct references to animations placed inside of their anim graphs Adds function call support from anim nodes. All anim graph nodes can now call functions when initialized, updated, evaluated or when they first become relevant. Relevancy is established using a new FAnimSubsystemInstance_NodeRelevancy which tracks nodes in a local map, per UAnimInstance, if nodes require it. Functions are displayed on the node if bound, and in the details panel. Added a new override point to FAnimSubsystemInstance to allow for WT init. Moved FMemberReference customization into a public header so it can be used on anim node functions. Wrapped functions up into FAnimNodeFunctionRef structure so they can be re-used more effectively. Converted CallFunction node to use them. Added a couple of simple BP function libraries to demonstrate the use of the new FAnimNodeContext (this is intended to be a generic way of exposing FAnimNode_Base types to script without the node types themselves having to be known to script directly). Added the ability to set exposed properties as 'always dynamic' so they appear in mutable data rather than being merged into constants. This allows for the anim node data API to be changed to be less strict (and more script friendly). Now values can be set in mutable data (via GET_MUTABLE_ANIM_NODE_DATA_PTR) and attempted sets to constant data can be ignored and reported to client code. A few minor crash fixes with edge cases (inc when trying to open an asset editor fails because of a missing skeleton/cancellation). Also fixes an issue where literal linked anim graph/control rig/custom poroperty node inputs didnt get copied #rb Jurre.deBaare,Aaron.Cox [CL 16703644 by Thomas Sarkanen in ue5-main branch]
2021-06-17 08:58:34 -04:00
{
ExtensionClasses.Add(UAnimBlueprintExtension_NodeRelevancy::StaticClass());
}
if(InAnimGraphNode->Tag != NAME_None)
{
ExtensionClasses.Add(UAnimBlueprintExtension_Tag::StaticClass());
}
Anim node data/compiler refactor Per-node constant data is now held on a generated struct as part of sparse class data. Per-node mutable data (i.e. pin links/property access mappings) is now held on a generated 'mutable data' struct that is compiled as part of the generated class. The anim BP compiler is now extended more conventionally using UAnimBlueprintExtension, derived from UBlueprintExtension. This directly replaces the older 'compiler handler' pattern that was added in an emergency fashion for 4.26. Anim graph nodes now request their required extensions and these are held on the UAnimBlueprint in the UBlueprint::Extensions array. The Extensions array is potentially refreshed with any node addition or removal. The Extensions array is force-refreshed each time an anim BP is compiled for the first time to deal with newly added or removed requirements. Const-corrected a bunch of UAnimInstance/FAnimInstanceProxy APIs that rely on (now truly) const data. Added a split state/constant version of FInputScaleBiasClamp to allow some of its data to be split into constants. Tweaked alignment/ordering of FPoseLinkBase to save a few bytes per pose link. Deprecated FAnimNode_Base::OverrideAsset in favor of a more UAnimGraphNode_Base-based approach. Individual nodes can still have runtime overrides via specific accessors. The new approach will also give us the oppurtunity to override multiple assets per node if required in the future. Moved property access into Engine module & removed event support from it - this was never used. Reworked property access compilation API a little - construction/lifetime was a bit confusing previously. Optimized path used to create UK2Node_StructMemberSet nodes in per-node custom events. When using mutable data, the structure used is large and very sparsely connected (i.e. only a few properties are written) so we only create pins that are actually going to be used, rather than creating all of them and conly connecting a few. Patched the following nodes to use the new data approach: - Asset players (sequences, blendspaces, aim offsets) - Blend lists - Ref poses - Roots #rb Jurre.deBaare, Martin.Wilson, Keith.Yerex [CL 16090510 by Thomas Sarkanen in ue5-main branch]
2021-04-22 04:57:09 -04:00
InAnimGraphNode->GetRequiredExtensions(ExtensionClasses);
for(const TSubclassOf<UAnimBlueprintExtension>& ExtensionClass : ExtensionClasses)
{
// Request any subsystem that we need to compile
RequestExtension(AnimBlueprint, ExtensionClass);
}
}
}
void UAnimBlueprintExtension::RefreshExtensions(UAnimBlueprint* InAnimBlueprint)
{
GIsRefreshingExtensions = true;
RefreshSet.Empty();
TArray<UAnimGraphNode_Base*> AllNodes;
FBlueprintEditorUtils::GetAllNodesOfClass<UAnimGraphNode_Base>(InAnimBlueprint, AllNodes);
for(UAnimGraphNode_Base* Node : AllNodes)
{
RequestExtensionsForNode(Node);
}
// Remove all extensions that are no longer needed
InAnimBlueprint->Extensions.RemoveAll([](UBlueprintExtension* InExtension)
{
if(UAnimBlueprintExtension* AnimBlueprintExtension = Cast<UAnimBlueprintExtension>(InExtension))
{
return !RefreshSet.Contains(AnimBlueprintExtension->GetClass());
}
return false;
});
RefreshSet.Empty();
GIsRefreshingExtensions = false;
}
void UAnimBlueprintExtension::ForEachExtension(UAnimBlueprint* InAnimBlueprint, TFunctionRef<void(UAnimBlueprintExtension*)> InFunction)
{
for (UBlueprintExtension* BlueprintExtension : InAnimBlueprint->Extensions)
{
if(UAnimBlueprintExtension* AnimBlueprintExtension = Cast<UAnimBlueprintExtension>(BlueprintExtension))
{
InFunction(AnimBlueprintExtension);
}
}
}
const UScriptStruct* UAnimBlueprintExtension::GetInstanceDataType() const
{
UScriptStruct* FoundStruct = FAnimSubsystemInstance::StaticStruct();
for (TFieldIterator<FProperty> PropIt(GetClass(), EFieldIteratorFlags::IncludeSuper); PropIt; ++PropIt)
{
if (FStructProperty* StructProp = CastField<FStructProperty>(*PropIt))
{
if (StructProp->Struct->IsChildOf(FAnimSubsystemInstance::StaticStruct()))
{
FoundStruct = StructProp->Struct;
break;
}
}
}
return FoundStruct;
}
const UScriptStruct* UAnimBlueprintExtension::GetClassDataType() const
{
UScriptStruct* FoundStruct = FAnimSubsystem::StaticStruct();
for (TFieldIterator<FProperty> PropIt(GetClass(), EFieldIteratorFlags::IncludeSuper); PropIt; ++PropIt)
{
if (FStructProperty* StructProp = CastField<FStructProperty>(*PropIt))
{
if (StructProp->Struct->IsChildOf(FAnimSubsystem::StaticStruct()))
{
FoundStruct = StructProp->Struct;
break;
}
}
}
return FoundStruct;
}
const FStructProperty* UAnimBlueprintExtension::GetInstanceDataProperty() const
{
for (TFieldIterator<FProperty> PropIt(GetClass(), EFieldIteratorFlags::IncludeSuper); PropIt; ++PropIt)
{
if (FStructProperty* StructProp = CastField<FStructProperty>(*PropIt))
{
if (StructProp->Struct->IsChildOf(FAnimSubsystemInstance::StaticStruct()))
{
return StructProp;
}
}
}
return nullptr;
}
const FStructProperty* UAnimBlueprintExtension::GetClassDataProperty() const
{
for (TFieldIterator<FProperty> PropIt(GetClass(), EFieldIteratorFlags::IncludeSuper); PropIt; ++PropIt)
{
if (FStructProperty* StructProp = CastField<FStructProperty>(*PropIt))
{
if (StructProp->Struct->IsChildOf(FAnimSubsystem::StaticStruct()))
{
return StructProp;
}
}
}
return nullptr;
}
UAnimBlueprint* UAnimBlueprintExtension::GetAnimBlueprint() const
{
return CastChecked<UAnimBlueprint>(GetOuter());
}
void* UAnimBlueprintExtension::GetClassDataInternal()
{
if(const FStructProperty* Property = GetClassDataProperty())
{
return Property->ContainerPtrToValuePtr<void>(this);
}
static FAnimSubsystem Default;
return &Default;
}
void* UAnimBlueprintExtension::GetInstanceDataInternal()
{
if(const FStructProperty* Property = GetInstanceDataProperty())
{
return Property->ContainerPtrToValuePtr<void>(this);
}
static FAnimSubsystemInstance Default;
return &Default;
}
void UAnimBlueprintExtension::BeginCompilation(IAnimBlueprintCompilerCreationContext& InCreationContext)
{
HandleBeginCompilation(InCreationContext);
}
void UAnimBlueprintExtension::StartCompilingClass(const UClass* InClass, IAnimBlueprintCompilationBracketContext& InCompilationContext, IAnimBlueprintGeneratedClassCompiledData& OutCompiledData)
{
HandleStartCompilingClass(InClass, InCompilationContext, OutCompiledData);
}
void UAnimBlueprintExtension::PreProcessAnimationNodes(TArrayView<UAnimGraphNode_Base*> InAnimNodes, IAnimBlueprintCompilationContext& InCompilationContext, IAnimBlueprintGeneratedClassCompiledData& OutCompiledData)
{
HandlePreProcessAnimationNodes(InAnimNodes, InCompilationContext, OutCompiledData);
}
void UAnimBlueprintExtension::PostProcessAnimationNodes(TArrayView<UAnimGraphNode_Base*> InAnimNodes, IAnimBlueprintCompilationContext& InCompilationContext, IAnimBlueprintGeneratedClassCompiledData& OutCompiledData)
{
HandlePostProcessAnimationNodes(InAnimNodes, InCompilationContext, OutCompiledData);
}
void UAnimBlueprintExtension::FinishCompilingClass(const UClass* InClass, IAnimBlueprintCompilationBracketContext& InCompilationContext, IAnimBlueprintGeneratedClassCompiledData& OutCompiledData)
{
HandleFinishCompilingClass(InClass, InCompilationContext, OutCompiledData);
}
void UAnimBlueprintExtension::PostExpansionStep(const UEdGraph* InGraph, IAnimBlueprintPostExpansionStepContext& InCompilationContext, IAnimBlueprintGeneratedClassCompiledData& OutCompiledData)
{
HandlePostExpansionStep(InGraph, InCompilationContext, OutCompiledData);
}
void UAnimBlueprintExtension::CopyTermDefaultsToDefaultObject(UObject* InDefaultObject, IAnimBlueprintCopyTermDefaultsContext& InCompilationContext, IAnimBlueprintExtensionCopyTermDefaultsContext& InPerExtensionContext)
{
if(InPerExtensionContext.GetTargetProperty() && InPerExtensionContext.GetDestinationPtr() && InPerExtensionContext.GetSourcePtr())
{
InPerExtensionContext.GetTargetProperty()->CopyCompleteValue(InPerExtensionContext.GetDestinationPtr(), InPerExtensionContext.GetSourcePtr());
}
HandleCopyTermDefaultsToDefaultObject(InDefaultObject, InCompilationContext, InPerExtensionContext);
}
void UAnimBlueprintExtension::CopyTermDefaultsToSparseClassData(IAnimBlueprintCopyTermDefaultsContext& InCompilationContext, IAnimBlueprintExtensionCopyTermDefaultsContext& InPerExtensionContext)
{
if(InPerExtensionContext.GetTargetProperty() && InPerExtensionContext.GetDestinationPtr() && InPerExtensionContext.GetSourcePtr())
{
InPerExtensionContext.GetTargetProperty()->CopyCompleteValue(InPerExtensionContext.GetDestinationPtr(), InPerExtensionContext.GetSourcePtr());
}
HandleCopyTermDefaultsToSparseClassData(InCompilationContext, InPerExtensionContext);
}
void UAnimBlueprintExtension::EndCompilation()
{
HandleEndCompilation();
}