Files
integration/Plugins/BlueprintMCP/Source/BlueprintMCP/Handlers/StateMachine_RemoveState.h

82 lines
2.4 KiB
C
Raw Normal View History

2026-03-08 22:17:14 -04:00
#pragma once
#include "CoreMinimal.h"
#include "MCPHandler.h"
2026-03-10 07:17:42 -04:00
#include "MCPFetcher.h"
2026-03-08 22:17:14 -04:00
#include "MCPUtils.h"
#include "Kismet2/KismetEditorUtilities.h"
#include "Animation/AnimBlueprint.h"
#include "AnimStateNode.h"
#include "AnimStateTransitionNode.h"
#include "AnimationStateMachineGraph.h"
2026-03-12 00:44:17 -04:00
#include "StateMachine_RemoveState.generated.h"
2026-03-08 22:17:14 -04:00
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
2026-03-12 00:44:17 -04:00
UCLASS()
class UMCP_StateMachine_RemoveState : public UObject, public IMCPHandler
2026-03-08 22:17:14 -04:00
{
GENERATED_BODY()
public:
2026-03-10 07:17:42 -04:00
UPROPERTY(meta=(Description="Path to the state machine graph, e.g. /Game/MyAnimBP,graph:StateMachine"))
FString Path;
2026-03-08 22:17:14 -04:00
UPROPERTY(meta=(Description="Name of the state to remove"))
FString StateName;
virtual FString GetDescription() const override
{
return TEXT("Remove a state and its connected transitions from an animation state machine graph.");
}
2026-03-12 17:48:11 -04:00
virtual void Handle(FStringBuilderBase& Result) override
2026-03-08 22:17:14 -04:00
{
2026-03-10 07:17:42 -04:00
// Fetch the state machine graph via MCPFetcher
2026-03-13 13:46:12 -04:00
MCPFetcher F;
2026-03-10 07:17:42 -04:00
F.Walk(Path);
if (!F.Ok()) return;
2026-03-08 22:17:14 -04:00
2026-03-10 07:17:42 -04:00
UAnimationStateMachineGraph* SMGraph = F.Cast<UAnimationStateMachineGraph>();
if (!SMGraph) return;
2026-03-08 22:17:14 -04:00
2026-03-10 07:17:42 -04:00
// Find the owning AnimBlueprint for compile/save
UBlueprint* BP = Cast<UBlueprint>(SMGraph->GetOuter()->GetOuter());
if (!BP)
2026-03-08 22:17:14 -04:00
{
2026-03-10 07:17:42 -04:00
Result.Append(TEXT("ERROR: Could not find owning blueprint.\n"));
return;
2026-03-08 22:17:14 -04:00
}
2026-03-10 07:17:42 -04:00
// Find the state node
UAnimStateNode* StateNode = MCPUtils::FindStateByName(SMGraph, StateName, Result);
if (!StateNode) return;
// Collect and remove transitions connected to this state
int32 RemovedTransitions = 0;
for (UEdGraphNode* Node : TArray<UEdGraphNode*>(SMGraph->Nodes))
2026-03-08 22:17:14 -04:00
{
2026-03-10 07:17:42 -04:00
UAnimStateTransitionNode* TransNode = Cast<UAnimStateTransitionNode>(Node);
if (!TransNode) continue;
if (TransNode->GetPreviousState() != StateNode && TransNode->GetNextState() != StateNode) continue;
TransNode->BreakAllNodeLinks();
SMGraph->RemoveNode(TransNode);
RemovedTransitions++;
2026-03-08 22:17:14 -04:00
}
// Remove the state
StateNode->BreakAllNodeLinks();
SMGraph->RemoveNode(StateNode);
// Compile and save
2026-03-10 07:17:42 -04:00
FKismetEditorUtilities::CompileBlueprint(BP);
MCPUtils::SaveBlueprintPackage(BP);
2026-03-08 22:17:14 -04:00
2026-03-10 07:17:42 -04:00
Result.Appendf(TEXT("Removed state %s and %d transition(s).\n"),
*MCPUtils::FormatName(StateNode), RemovedTransitions);
2026-03-08 22:17:14 -04:00
}
};