Files
UnrealEngineUWP/Engine/Source/Editor/AnimGraph/Private/AnimGraphNode_BlendSpacePlayer.cpp
Thomas Sarkanen 16eee0289d 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

322 lines
11 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "AnimGraphNode_BlendSpacePlayer.h"
#include "UObject/UObjectHash.h"
#include "UObject/UObjectIterator.h"
#include "ToolMenus.h"
#include "GraphEditorActions.h"
#include "Kismet2/CompilerResultsLog.h"
#include "BlueprintNodeSpawner.h"
#include "BlueprintActionDatabaseRegistrar.h"
#include "Animation/AimOffsetBlendSpace.h"
#include "Animation/AimOffsetBlendSpace1D.h"
#include "AnimGraphCommands.h"
#include "IAnimBlueprintCopyTermDefaultsContext.h"
#include "IAnimBlueprintNodeOverrideAssetsContext.h"
/////////////////////////////////////////////////////
// UAnimGraphNode_BlendSpacePlayer
#define LOCTEXT_NAMESPACE "A3Nodes"
UAnimGraphNode_BlendSpacePlayer::UAnimGraphNode_BlendSpacePlayer(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}
FText UAnimGraphNode_BlendSpacePlayer::GetTooltipText() const
{
// FText::Format() is slow, so we utilize the cached list title
return GetNodeTitle(ENodeTitleType::ListView);
}
FText UAnimGraphNode_BlendSpacePlayer::GetNodeTitleForBlendSpace(ENodeTitleType::Type TitleType, UBlendSpace* InBlendSpace) const
{
const FText BlendSpaceName = FText::FromString(InBlendSpace->GetName());
if (TitleType == ENodeTitleType::ListView || TitleType == ENodeTitleType::MenuTitle)
{
FFormatNamedArguments Args;
Args.Add(TEXT("BlendSpaceName"), BlendSpaceName);
// FText::Format() is slow, so we cache this to save on performance
CachedNodeTitles.SetCachedTitle(TitleType, FText::Format(LOCTEXT("BlendspacePlayer", "Blendspace Player '{BlendSpaceName}'"), Args), this);
}
else
{
FFormatNamedArguments TitleArgs;
TitleArgs.Add(TEXT("BlendSpaceName"), BlendSpaceName);
FText Title = FText::Format(LOCTEXT("BlendSpacePlayerFullTitle", "{BlendSpaceName}\nBlendspace Player"), TitleArgs);
if (TitleType == ENodeTitleType::FullTitle)
{
FFormatNamedArguments Args;
Args.Add(TEXT("Title"), Title);
if(Node.GetGroupMethod() == EAnimSyncMethod::SyncGroup)
{
Args.Add(TEXT("SyncGroupName"), FText::FromName(Node.GetGroupName()));
Title = FText::Format(LOCTEXT("BlendSpaceNodeGroupSubtitle", "{Title}\nSync group {SyncGroupName}"), Args);
}
else if(Node.GetGroupMethod() == EAnimSyncMethod::Graph)
{
Title = FText::Format(LOCTEXT("BlendSpaceNodeGroupSubtitle", "{Title}\nGraph sync group"), Args);
UObject* ObjectBeingDebugged = GetAnimBlueprint()->GetObjectBeingDebugged();
UAnimBlueprintGeneratedClass* GeneratedClass = GetAnimBlueprint()->GetAnimBlueprintGeneratedClass();
if (ObjectBeingDebugged && GeneratedClass)
{
int32 NodeIndex = GeneratedClass->GetNodeIndexFromGuid(NodeGuid);
if(NodeIndex != INDEX_NONE)
{
if(const FName* SyncGroupNamePtr = GeneratedClass->GetAnimBlueprintDebugData().NodeSyncsThisFrame.Find(NodeIndex))
{
Args.Add(TEXT("SyncGroupName"), FText::FromName(*SyncGroupNamePtr));
Title = FText::Format(LOCTEXT("BlendSpaceNodeGraphGroupSubtitle", "{Title}\nGraph sync group {SyncGroupName}"), Args);
}
}
}
}
}
// FText::Format() is slow, so we cache this to save on performance
CachedNodeTitles.SetCachedTitle(TitleType, Title, this);
}
return CachedNodeTitles[TitleType];
}
FText UAnimGraphNode_BlendSpacePlayer::GetNodeTitle(ENodeTitleType::Type TitleType) const
{
if (Node.GetBlendSpace() == nullptr)
{
// we may have a valid variable connected or default pin value
UEdGraphPin* BlendSpacePin = FindPin(GET_MEMBER_NAME_STRING_CHECKED(FAnimNode_BlendSpacePlayer, BlendSpace));
if (BlendSpacePin && BlendSpacePin->LinkedTo.Num() > 0)
{
return LOCTEXT("BlendspacePlayer_Variable_Title", "Blendspace Player");
}
else if (BlendSpacePin && BlendSpacePin->DefaultObject != nullptr)
{
return GetNodeTitleForBlendSpace(TitleType, CastChecked<UBlendSpace>(BlendSpacePin->DefaultObject));
}
else
{
if (TitleType == ENodeTitleType::ListView || TitleType == ENodeTitleType::MenuTitle)
{
return LOCTEXT("BlendspacePlayer_NONE_ListTitle", "Blendspace Player '(None)'");
}
else
{
return LOCTEXT("BlendspacePlayer_NONE_Title", "(None)\nBlendspace Player");
}
}
}
// @TODO: the bone can be altered in the property editor, so we have to
// choose to mark this dirty when that happens for this to properly work
else //if (!CachedNodeTitles.IsTitleCached(TitleType, this))
{
return GetNodeTitleForBlendSpace(TitleType, Node.GetBlendSpace());
}
}
void UAnimGraphNode_BlendSpacePlayer::ValidateAnimNodeDuringCompilation(class USkeleton* ForSkeleton, class FCompilerResultsLog& MessageLog)
{
Super::ValidateAnimNodeDuringCompilation(ForSkeleton, MessageLog);
UBlendSpace* BlendSpaceToCheck = Node.GetBlendSpace();
UEdGraphPin* BlendSpacePin = FindPin(GET_MEMBER_NAME_STRING_CHECKED(FAnimNode_BlendSpacePlayer, BlendSpace));
if (BlendSpacePin != nullptr && BlendSpaceToCheck == nullptr)
{
BlendSpaceToCheck = Cast<UBlendSpace>(BlendSpacePin->DefaultObject);
}
if (BlendSpaceToCheck == nullptr)
{
// Check for bindings
bool bHasBinding = false;
if(BlendSpacePin != nullptr)
{
if (FAnimGraphNodePropertyBinding* BindingPtr = PropertyBindings.Find(BlendSpacePin->GetFName()))
{
bHasBinding = true;
}
}
// we may have a connected node or binding
if (BlendSpacePin == nullptr || (BlendSpacePin->LinkedTo.Num() == 0 && !bHasBinding))
{
MessageLog.Error(TEXT("@@ references an unknown blend space"), this);
}
}
else
{
USkeleton* BlendSpaceSkeleton = BlendSpaceToCheck->GetSkeleton();
if (BlendSpaceSkeleton && // if blend space doesn't have skeleton, it might be due to blend space not loaded yet, @todo: wait with anim blueprint compilation until all assets are loaded?
!ForSkeleton->IsCompatible(BlendSpaceSkeleton))
{
MessageLog.Error(TEXT("@@ references blendspace that uses an incompatible skeleton @@"), this, BlendSpaceSkeleton);
}
}
}
void UAnimGraphNode_BlendSpacePlayer::BakeDataDuringCompilation(class FCompilerResultsLog& MessageLog)
{
UAnimBlueprint* AnimBlueprint = GetAnimBlueprint();
AnimBlueprint->FindOrAddGroup(Node.GetGroupName());
}
void UAnimGraphNode_BlendSpacePlayer::GetNodeContextMenuActions(UToolMenu* Menu, UGraphNodeContextMenuContext* Context) const
{
if (!Context->bIsDebugging)
{
// add an option to convert to single frame
{
FToolMenuSection& Section = Menu->AddSection("AnimGraphNodeBlendSpaceEvaluator", NSLOCTEXT("A3Nodes", "BlendSpaceHeading", "Blend Space"));
Section.AddMenuEntry(FAnimGraphCommands::Get().OpenRelatedAsset);
Section.AddMenuEntry(FAnimGraphCommands::Get().ConvertToBSEvaluator);
Section.AddMenuEntry(FAnimGraphCommands::Get().ConvertToBSGraph);
}
}
}
void UAnimGraphNode_BlendSpacePlayer::GetMenuActions(FBlueprintActionDatabaseRegistrar& ActionRegistrar) const
{
struct GetMenuActions_Utils
{
static void SetNodeBlendSpace(UEdGraphNode* NewNode, bool /*bIsTemplateNode*/, TWeakObjectPtr<UBlendSpace> BlendSpace)
{
UAnimGraphNode_BlendSpacePlayer* BlendSpaceNode = CastChecked<UAnimGraphNode_BlendSpacePlayer>(NewNode);
BlendSpaceNode->Node.SetBlendSpace(BlendSpace.Get());
}
static UBlueprintNodeSpawner* MakeBlendSpaceAction(TSubclassOf<UEdGraphNode> const NodeClass, const UBlendSpace* BlendSpace)
{
UBlueprintNodeSpawner* NodeSpawner = nullptr;
bool const bIsAimOffset = BlendSpace->IsA(UAimOffsetBlendSpace::StaticClass()) ||
BlendSpace->IsA(UAimOffsetBlendSpace1D::StaticClass());
if (!bIsAimOffset)
{
NodeSpawner = UBlueprintNodeSpawner::Create(NodeClass);
check(NodeSpawner != nullptr);
TWeakObjectPtr<UBlendSpace> BlendSpacePtr = MakeWeakObjectPtr(const_cast<UBlendSpace*>(BlendSpace));
NodeSpawner->CustomizeNodeDelegate = UBlueprintNodeSpawner::FCustomizeNodeDelegate::CreateStatic(GetMenuActions_Utils::SetNodeBlendSpace, BlendSpacePtr);
}
return NodeSpawner;
}
};
if (const UObject* RegistrarTarget = ActionRegistrar.GetActionKeyFilter())
{
if (const UBlendSpace* TargetBlendSpace = Cast<UBlendSpace>(RegistrarTarget))
{
if(TargetBlendSpace->IsAsset())
{
if (UBlueprintNodeSpawner* NodeSpawner = GetMenuActions_Utils::MakeBlendSpaceAction(GetClass(), TargetBlendSpace))
{
ActionRegistrar.AddBlueprintAction(TargetBlendSpace, NodeSpawner);
}
}
}
// else, the Blueprint database is specifically looking for actions pertaining to something different (not a BlendSpace asset)
}
else
{
UClass* NodeClass = GetClass();
for (TObjectIterator<UBlendSpace> BlendSpaceIt; BlendSpaceIt; ++BlendSpaceIt)
{
UBlendSpace* BlendSpace = *BlendSpaceIt;
if(BlendSpace->IsAsset())
{
if (UBlueprintNodeSpawner* NodeSpawner = GetMenuActions_Utils::MakeBlendSpaceAction(NodeClass, BlendSpace))
{
ActionRegistrar.AddBlueprintAction(BlendSpace, NodeSpawner);
}
}
}
}
}
FBlueprintNodeSignature UAnimGraphNode_BlendSpacePlayer::GetSignature() const
{
FBlueprintNodeSignature NodeSignature = Super::GetSignature();
NodeSignature.AddSubObject(Node.GetBlendSpace());
return NodeSignature;
}
void UAnimGraphNode_BlendSpacePlayer::SetAnimationAsset(UAnimationAsset* Asset)
{
if (UBlendSpace* BlendSpace = Cast<UBlendSpace>(Asset))
{
Node.SetBlendSpace(BlendSpace);
}
}
void UAnimGraphNode_BlendSpacePlayer::OnOverrideAssets(IAnimBlueprintNodeOverrideAssetsContext& InContext) const
{
if(InContext.GetAssets().Num() > 0)
{
if (UBlendSpace* BlendSpace = Cast<UBlendSpace>(InContext.GetAssets()[0]))
{
FAnimNode_BlendSpacePlayer& AnimNode = InContext.GetAnimNode<FAnimNode_BlendSpacePlayer>();
AnimNode.SetBlendSpace(BlendSpace);
}
}
}
void UAnimGraphNode_BlendSpacePlayer::GetAllAnimationSequencesReferred(TArray<UAnimationAsset*>& AnimationAssets) const
{
if(Node.GetBlendSpace())
{
HandleAnimReferenceCollection(Node.BlendSpace, AnimationAssets);
}
}
void UAnimGraphNode_BlendSpacePlayer::ReplaceReferredAnimations(const TMap<UAnimationAsset*, UAnimationAsset*>& AnimAssetReplacementMap)
{
HandleAnimReferenceReplacement(Node.BlendSpace, AnimAssetReplacementMap);
}
bool UAnimGraphNode_BlendSpacePlayer::DoesSupportTimeForTransitionGetter() const
{
return true;
}
UAnimationAsset* UAnimGraphNode_BlendSpacePlayer::GetAnimationAsset() const
{
UBlendSpace* BlendSpace = Node.GetBlendSpace();
UEdGraphPin* BlendSpacePin = FindPin(GET_MEMBER_NAME_STRING_CHECKED(FAnimNode_BlendSpacePlayer, BlendSpace));
if (BlendSpacePin != nullptr && BlendSpace == nullptr)
{
BlendSpace = Cast<UBlendSpace>(BlendSpacePin->DefaultObject);
}
return BlendSpace;
}
const TCHAR* UAnimGraphNode_BlendSpacePlayer::GetTimePropertyName() const
{
return TEXT("InternalTimeAccumulator");
}
UScriptStruct* UAnimGraphNode_BlendSpacePlayer::GetTimePropertyStruct() const
{
return FAnimNode_BlendSpacePlayer::StaticStruct();
}
EAnimAssetHandlerType UAnimGraphNode_BlendSpacePlayer::SupportsAssetClass(const UClass* AssetClass) const
{
if (AssetClass->IsChildOf(UBlendSpace::StaticClass()) && !IsAimOffsetBlendSpace(AssetClass))
{
return EAnimAssetHandlerType::PrimaryHandler;
}
else
{
return EAnimAssetHandlerType::NotSupported;
}
}
#undef LOCTEXT_NAMESPACE