2026-03-08 22:17:14 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "CoreMinimal.h"
|
2026-03-18 10:17:58 -04:00
|
|
|
#include "WingServer.h"
|
|
|
|
|
#include "WingHandler.h"
|
|
|
|
|
#include "WingFetcher.h"
|
|
|
|
|
#include "WingUtils.h"
|
2026-03-08 22:17:14 -04:00
|
|
|
#include "EdGraph/EdGraph.h"
|
|
|
|
|
#include "EdGraph/EdGraphNode.h"
|
|
|
|
|
#include "Kismet2/KismetEditorUtilities.h"
|
|
|
|
|
#include "Animation/AnimBlueprint.h"
|
|
|
|
|
#include "Animation/AnimSequence.h"
|
|
|
|
|
#include "AnimGraphNode_SequencePlayer.h"
|
|
|
|
|
#include "AnimStateNode.h"
|
|
|
|
|
#include "AnimationStateMachineGraph.h"
|
2026-03-12 00:44:17 -04:00
|
|
|
#include "StateMachine_SetAnimation.generated.h"
|
2026-03-08 22:17:14 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
2026-03-15 19:32:09 -04:00
|
|
|
UCLASS()
|
2026-03-18 10:17:58 -04:00
|
|
|
class UWing_StateMachine_SetAnimation : public UObject, public IWingHandler
|
2026-03-08 22:17:14 -04:00
|
|
|
{
|
|
|
|
|
GENERATED_BODY()
|
|
|
|
|
|
|
|
|
|
public:
|
2026-03-14 00:02:00 -04:00
|
|
|
UPROPERTY(meta=(Description="Animation Blueprint package path"))
|
2026-03-08 22:17:14 -04:00
|
|
|
FString Blueprint;
|
|
|
|
|
|
|
|
|
|
UPROPERTY(meta=(Description="State machine graph name"))
|
|
|
|
|
FString Graph;
|
|
|
|
|
|
|
|
|
|
UPROPERTY(meta=(Description="Name of the state to modify"))
|
|
|
|
|
FString StateName;
|
|
|
|
|
|
2026-03-14 00:02:00 -04:00
|
|
|
UPROPERTY(meta=(Description="Animation asset package path to assign"))
|
2026-03-08 22:17:14 -04:00
|
|
|
FString AnimationAsset;
|
|
|
|
|
|
|
|
|
|
virtual FString GetDescription() const override
|
|
|
|
|
{
|
|
|
|
|
return TEXT("Set or replace the animation sequence played by a state in an animation state machine.");
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-13 23:41:59 -04:00
|
|
|
virtual void Handle() override
|
2026-03-08 22:17:14 -04:00
|
|
|
{
|
2026-03-10 07:17:42 -04:00
|
|
|
// Resolve the anim blueprint
|
2026-03-18 10:17:58 -04:00
|
|
|
WingFetcher F;
|
2026-03-10 07:17:42 -04:00
|
|
|
UAnimBlueprint* AnimBP = F.Walk(Blueprint).Cast<UAnimBlueprint>();
|
|
|
|
|
if (!AnimBP) return;
|
|
|
|
|
|
|
|
|
|
// Find the state machine graph
|
2026-03-18 10:17:58 -04:00
|
|
|
UAnimationStateMachineGraph* SMGraph = WingUtils::FindStateMachineGraph(AnimBP, Graph);
|
2026-03-10 07:17:42 -04:00
|
|
|
if (!SMGraph)
|
|
|
|
|
{
|
2026-03-18 10:17:58 -04:00
|
|
|
UWingServer::Printf(TEXT("ERROR: State machine graph '%s' not found in %s\n"), *Graph, *WingUtils::FormatName(AnimBP));
|
2026-03-10 07:17:42 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2026-03-08 22:17:14 -04:00
|
|
|
|
2026-03-10 07:17:42 -04:00
|
|
|
// Find the target state
|
2026-03-18 10:17:58 -04:00
|
|
|
UAnimStateNode* StateNode = WingUtils::FindStateByName(SMGraph, StateName);
|
2026-03-08 22:17:14 -04:00
|
|
|
if (!StateNode) return;
|
|
|
|
|
|
|
|
|
|
UEdGraph* InnerGraph = StateNode->GetBoundGraph();
|
|
|
|
|
if (!InnerGraph)
|
|
|
|
|
{
|
2026-03-18 10:17:58 -04:00
|
|
|
UWingServer::Printf(TEXT("ERROR: State '%s' has no bound graph\n"), *StateName);
|
2026-03-10 07:17:42 -04:00
|
|
|
return;
|
2026-03-08 22:17:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the animation asset
|
2026-03-18 10:17:58 -04:00
|
|
|
WingFetcher F2;
|
2026-03-14 00:02:00 -04:00
|
|
|
UAnimSequence* AnimSeq = F2.Asset(AnimationAsset).Cast<UAnimSequence>();
|
|
|
|
|
if (!AnimSeq) return;
|
2026-03-08 22:17:14 -04:00
|
|
|
|
|
|
|
|
// Find existing SequencePlayer or create one
|
|
|
|
|
UAnimGraphNode_SequencePlayer* SeqNode = nullptr;
|
|
|
|
|
for (UEdGraphNode* Node : InnerGraph->Nodes)
|
|
|
|
|
{
|
|
|
|
|
SeqNode = Cast<UAnimGraphNode_SequencePlayer>(Node);
|
|
|
|
|
if (SeqNode) break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool bCreatedNew = false;
|
|
|
|
|
if (!SeqNode)
|
|
|
|
|
{
|
|
|
|
|
SeqNode = NewObject<UAnimGraphNode_SequencePlayer>(InnerGraph);
|
|
|
|
|
SeqNode->CreateNewGuid();
|
|
|
|
|
SeqNode->PostPlacedNewNode();
|
|
|
|
|
SeqNode->AllocateDefaultPins();
|
|
|
|
|
SeqNode->NodePosX = 0;
|
|
|
|
|
SeqNode->NodePosY = 0;
|
|
|
|
|
InnerGraph->AddNode(SeqNode, false, false);
|
|
|
|
|
bCreatedNew = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SeqNode->SetAnimationAsset(AnimSeq);
|
|
|
|
|
|
2026-03-19 10:56:20 -04:00
|
|
|
// Compile
|
2026-03-08 22:17:14 -04:00
|
|
|
FKismetEditorUtilities::CompileBlueprint(AnimBP);
|
|
|
|
|
|
2026-03-10 07:17:42 -04:00
|
|
|
if (bCreatedNew)
|
2026-03-18 10:17:58 -04:00
|
|
|
UWingServer::Printf(TEXT("Created sequence player in state '%s', assigned %s\n"), *StateName, *WingUtils::FormatName(AnimSeq));
|
2026-03-10 07:17:42 -04:00
|
|
|
else
|
2026-03-18 10:17:58 -04:00
|
|
|
UWingServer::Printf(TEXT("Updated sequence player in state '%s' to %s\n"), *StateName, *WingUtils::FormatName(AnimSeq));
|
2026-03-08 22:17:14 -04:00
|
|
|
}
|
|
|
|
|
};
|