Files
integration/Plugins/UEWingman/Deprecated/StateMachine_SetBlendSpace.h

218 lines
6.3 KiB
C
Raw Normal View History

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 "EdGraph/EdGraphPin.h"
#include "Kismet2/KismetEditorUtilities.h"
#include "Animation/AnimBlueprint.h"
#include "Animation/BlendSpace.h"
#include "AnimGraphNode_BlendSpacePlayer.h"
2026-03-09 09:20:46 -04:00
#include "EdGraphSchema_K2.h"
2026-03-08 22:17:14 -04:00
#include "AnimStateNode.h"
#include "AnimationStateMachineGraph.h"
#include "K2Node_VariableGet.h"
2026-03-12 00:44:17 -04:00
#include "StateMachine_SetBlendSpace.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_SetBlendSpace : 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="Blend Space asset package path"))
2026-03-08 22:17:14 -04:00
FString BlendSpace;
UPROPERTY(meta=(Optional, Description="Blueprint variable name to wire to the X axis input"))
FString XVariable;
UPROPERTY(meta=(Optional, Description="Blueprint variable name to wire to the Y axis input"))
FString YVariable;
virtual FString GetDescription() const override
{
return TEXT("Place a BlendSpacePlayer in a state's inner graph, connect it to the output pose, "
"and optionally wire blueprint variables to the X and Y axis inputs.");
}
virtual void Handle() override
2026-03-08 22:17:14 -04:00
{
2026-03-10 07:17:42 -04:00
// Load the anim blueprint
2026-03-18 10:17:58 -04:00
WingFetcher F;
2026-03-14 00:02:00 -04:00
UAnimBlueprint* AnimBP = F.Asset(Blueprint).Cast<UAnimBlueprint>();
if (!AnimBP) return;
2026-03-08 22:17:14 -04:00
2026-03-10 07:17:42 -04:00
// Find the state machine graph and state
2026-03-18 10:17:58 -04:00
UAnimationStateMachineGraph* SMGraph = WingUtils::FindStateMachineGraph(AnimBP, Graph);
if (!SMGraph) { UWingServer::Printf(TEXT("ERROR: State machine graph '%s' not found\n"), *Graph); return; }
2026-03-10 07:17:42 -04:00
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();
2026-03-18 10:17:58 -04:00
if (!InnerGraph) { UWingServer::Printf(TEXT("ERROR: State '%s' has no bound graph\n"), *StateName); return; }
2026-03-08 22:17:14 -04:00
2026-03-10 07:17:42 -04:00
// Load the blend space asset
2026-03-18 10:17:58 -04:00
WingFetcher F2;
2026-03-14 00:02:00 -04:00
UBlendSpace* BlendSpaceAsset = F2.Asset(BlendSpace).Cast<UBlendSpace>();
if (!BlendSpaceAsset) return;
2026-03-08 22:17:14 -04:00
// Find existing BlendSpacePlayer or create one
UAnimGraphNode_BlendSpacePlayer* BSNode = nullptr;
for (UEdGraphNode* Node : InnerGraph->Nodes)
{
BSNode = Cast<UAnimGraphNode_BlendSpacePlayer>(Node);
if (BSNode) break;
}
if (!BSNode)
{
BSNode = NewObject<UAnimGraphNode_BlendSpacePlayer>(InnerGraph);
BSNode->CreateNewGuid();
BSNode->PostPlacedNewNode();
BSNode->AllocateDefaultPins();
BSNode->NodePosX = 0;
BSNode->NodePosY = 0;
InnerGraph->AddNode(BSNode, false, false);
}
BSNode->SetAnimationAsset(BlendSpaceAsset);
// Connect BlendSpacePlayer output to the Output Animation Pose node
2026-03-10 07:17:42 -04:00
ConnectToOutputPose(BSNode, InnerGraph);
// Wire X and Y variables if provided
WireVariable(AnimBP, InnerGraph, BSNode, XVariable, TEXT("X"));
WireVariable(AnimBP, InnerGraph, BSNode, YVariable, TEXT("Y"));
2026-03-10 07:17:42 -04:00
// Compile
2026-03-10 07:17:42 -04:00
FKismetEditorUtilities::CompileBlueprint(AnimBP);
2026-03-18 10:17:58 -04:00
UWingServer::Printf(TEXT("BlendSpacePlayer %s placed in state %s\n"),
*WingUtils::FormatName(BSNode), *StateName);
2026-03-10 07:17:42 -04:00
}
private:
void ConnectToOutputPose(UAnimGraphNode_BlendSpacePlayer* BSNode, UEdGraph* InnerGraph)
{
// Find the result node (AnimGraphNode_Root or AnimGraphNode_StateResult)
UEdGraphNode* ResultNode = nullptr;
for (UEdGraphNode* Node : InnerGraph->Nodes)
2026-03-08 22:17:14 -04:00
{
2026-03-10 07:17:42 -04:00
if (Node->GetClass()->GetName().Contains(TEXT("AnimGraphNode_Root")) ||
Node->GetClass()->GetName().Contains(TEXT("AnimGraphNode_StateResult")))
2026-03-08 22:17:14 -04:00
{
2026-03-10 07:17:42 -04:00
ResultNode = Node;
break;
2026-03-08 22:17:14 -04:00
}
2026-03-10 07:17:42 -04:00
}
if (!ResultNode) return;
2026-03-08 22:17:14 -04:00
2026-03-10 07:17:42 -04:00
// Find the pose output pin on BlendSpacePlayer and input pin on result node
UEdGraphPin* BSOutputPin = nullptr;
for (UEdGraphPin* Pin : BSNode->Pins)
{
if (Pin && Pin->Direction == EGPD_Output && Pin->PinType.PinCategory == UEdGraphSchema_K2::PC_Struct)
2026-03-08 22:17:14 -04:00
{
2026-03-10 07:17:42 -04:00
BSOutputPin = Pin;
break;
2026-03-08 22:17:14 -04:00
}
}
2026-03-10 07:17:42 -04:00
UEdGraphPin* ResultInputPin = nullptr;
for (UEdGraphPin* Pin : ResultNode->Pins)
2026-03-08 22:17:14 -04:00
{
2026-03-10 07:17:42 -04:00
if (Pin && Pin->Direction == EGPD_Input && Pin->PinType.PinCategory == UEdGraphSchema_K2::PC_Struct)
2026-03-08 22:17:14 -04:00
{
2026-03-10 07:17:42 -04:00
ResultInputPin = Pin;
break;
2026-03-08 22:17:14 -04:00
}
2026-03-10 07:17:42 -04:00
}
if (!BSOutputPin || !ResultInputPin) return;
ResultInputPin->BreakAllPinLinks();
const UEdGraphSchema* Schema = InnerGraph->GetSchema();
if (Schema)
Schema->TryCreateConnection(BSOutputPin, ResultInputPin);
}
void WireVariable(UAnimBlueprint* AnimBP, UEdGraph* InnerGraph,
UAnimGraphNode_BlendSpacePlayer* BSNode, const FString& VarName,
const TCHAR* PinName)
2026-03-10 07:17:42 -04:00
{
if (VarName.IsEmpty()) return;
// Verify the variable exists in the blueprint
FName VarFName(*VarName);
bool bVarFound = false;
for (FBPVariableDescription& Var : AnimBP->NewVariables)
{
if (Var.VarName == VarFName)
2026-03-08 22:17:14 -04:00
{
2026-03-10 07:17:42 -04:00
bVarFound = true;
break;
2026-03-08 22:17:14 -04:00
}
2026-03-10 07:17:42 -04:00
}
if (!bVarFound)
{
if (UClass* GenClass = AnimBP->SkeletonGeneratedClass)
2026-03-08 22:17:14 -04:00
{
2026-03-10 07:17:42 -04:00
if (GenClass->FindPropertyByName(VarFName))
bVarFound = true;
2026-03-08 22:17:14 -04:00
}
2026-03-10 07:17:42 -04:00
}
if (!bVarFound)
{
2026-03-18 10:17:58 -04:00
UWingServer::Printf(TEXT("WARNING: Variable '%s' not found, skipping %s wire\n"), *VarName, PinName);
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
// Create a VariableGet node
UK2Node_VariableGet* GetNode = NewObject<UK2Node_VariableGet>(InnerGraph);
GetNode->VariableReference.SetSelfMember(VarFName);
GetNode->NodePosX = BSNode->NodePosX - 250;
GetNode->NodePosY = BSNode->NodePosY;
InnerGraph->AddNode(GetNode, false, false);
GetNode->AllocateDefaultPins();
// Find the variable output pin
UEdGraphPin* VarOutPin = nullptr;
for (UEdGraphPin* Pin : GetNode->Pins)
{
if (Pin && Pin->Direction == EGPD_Output && Pin->PinName == VarFName)
2026-03-08 22:17:14 -04:00
{
2026-03-10 07:17:42 -04:00
VarOutPin = Pin;
break;
2026-03-08 22:17:14 -04:00
}
2026-03-10 07:17:42 -04:00
}
2026-03-08 22:17:14 -04:00
2026-03-10 07:17:42 -04:00
UEdGraphPin* TargetPin = BSNode->FindPin(FName(PinName));
2026-03-08 22:17:14 -04:00
2026-03-10 07:17:42 -04:00
if (VarOutPin && TargetPin)
{
const UEdGraphSchema* Schema = InnerGraph->GetSchema();
if (Schema)
Schema->TryCreateConnection(VarOutPin, TargetPin);
}
2026-03-08 22:17:14 -04:00
}
};