#pragma once #include "CoreMinimal.h" #include "MCPHandler.h" #include "MCPFetcher.h" #include "MCPUtils.h" #include "Kismet2/KismetEditorUtilities.h" #include "Animation/AnimBlueprint.h" #include "AnimStateNode.h" #include "AnimStateTransitionNode.h" #include "AnimationStateMachineGraph.h" #include "StateMachine_RemoveState.generated.h" // --------------------------------------------------------------------------- // --------------------------------------------------------------------------- // --------------------------------------------------------------------------- UCLASS() class UMCP_StateMachine_RemoveState : public UObject, public IMCPHandler { GENERATED_BODY() public: UPROPERTY(meta=(Description="Path to the state machine graph, e.g. /Game/MyAnimBP,graph:StateMachine")) FString Path; 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."); } virtual void Handle(FStringBuilderBase& Result) override { // Fetch the state machine graph via MCPFetcher MCPFetcher F; F.Walk(Path); if (!F.Ok()) return; UAnimationStateMachineGraph* SMGraph = F.Cast(); if (!SMGraph) return; // Find the owning AnimBlueprint for compile/save UBlueprint* BP = Cast(SMGraph->GetOuter()->GetOuter()); if (!BP) { Result.Append(TEXT("ERROR: Could not find owning blueprint.\n")); return; } // 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(SMGraph->Nodes)) { UAnimStateTransitionNode* TransNode = Cast(Node); if (!TransNode) continue; if (TransNode->GetPreviousState() != StateNode && TransNode->GetNextState() != StateNode) continue; TransNode->BreakAllNodeLinks(); SMGraph->RemoveNode(TransNode); RemovedTransitions++; } // Remove the state StateNode->BreakAllNodeLinks(); SMGraph->RemoveNode(StateNode); // Compile and save FKismetEditorUtilities::CompileBlueprint(BP); MCPUtils::SaveBlueprintPackage(BP); Result.Appendf(TEXT("Removed state %s and %d transition(s).\n"), *MCPUtils::FormatName(StateNode), RemovedTransitions); } };