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

105 lines
3.2 KiB
C++

#pragma once
#include "CoreMinimal.h"
#include "MCPServer.h"
#include "MCPHandler.h"
#include "MCPAssets.h"
#include "MCPUtils.h"
#include "MCPPackageMaker.h"
#include "Engine/Blueprint.h"
#include "Kismet2/KismetEditorUtilities.h"
#include "Blueprint_Create.generated.h"
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
UCLASS()
class UMCP_Blueprint_Create : public UObject, public IMCPHandler
{
GENERATED_BODY()
public:
UPROPERTY(meta=(Description="Full asset path for the new Blueprint (e.g. '/Game/Blueprints/BP_MyActor')"))
FString AssetPath;
UPROPERTY(meta=(Description="Parent class name (C++ class name or Blueprint name)"))
FString ParentClass;
UPROPERTY(meta=(Optional, Description="Blueprint type: Normal, Interface, FunctionLibrary, or MacroLibrary (default: Normal)"))
FString BlueprintType;
virtual FString GetDescription() const override
{
return TEXT("Create a new Blueprint asset with a specified parent class and type.");
}
virtual void Handle() override
{
MCPPackageMaker Maker(AssetPath);
if (!Maker.Ok()) return;
// Resolve parent class — try C++ class first, then Blueprint asset
UClass* ParentClassObj = MCPUtils::FindClassByName(ParentClass);
if (!ParentClassObj)
{
MCPAssets<UBlueprint> ParentAssets;
if (!ParentAssets.Exact(ParentClass).AllContent().ETwo().Load()) return;
if (!ParentAssets.Objects().IsEmpty() && ParentAssets.Object()->GeneratedClass)
ParentClassObj = ParentAssets.Object()->GeneratedClass;
}
if (!ParentClassObj)
{
UMCPServer::Printf(TEXT("ERROR: Could not find parent class '%s'. Provide a C++ class name (e.g. 'Actor', 'Pawn') or Blueprint name.\n"),
*ParentClass);
return;
}
// Map blueprintType string to EBlueprintType
EBlueprintType BlueprintTypeEnum = BPTYPE_Normal;
if (!BlueprintType.IsEmpty())
{
if (!MCPUtils::StringToEnum(BlueprintType, BlueprintTypeEnum, TEXT("BPTYPE_"))) return;
}
// For Interface type, parent must be UInterface
if ((BlueprintTypeEnum == BPTYPE_Interface) && !ParentClassObj->IsChildOf(UInterface::StaticClass()))
{
ParentClassObj = UInterface::StaticClass();
}
// Create the package and Blueprint
if (!Maker.Make()) return;
UBlueprint* NewBP = FKismetEditorUtilities::CreateBlueprint(
ParentClassObj,
Maker.Package(),
FName(*Maker.Name()),
BlueprintTypeEnum,
UBlueprint::StaticClass(),
UBlueprintGeneratedClass::StaticClass()
);
if (!NewBP)
{
UMCPServer::Print(TEXT("ERROR: FKismetEditorUtilities::CreateBlueprint returned null\n"));
return;
}
// Compile and save
FKismetEditorUtilities::CompileBlueprint(NewBP);
bool bSaved = MCPUtils::SaveBlueprintPackage(NewBP);
// Report result
UMCPServer::Printf(TEXT("Created: %s\n"), *MCPUtils::FormatName(NewBP));
UMCPServer::Printf(TEXT("Parent: %s\n"), *MCPUtils::FormatName(ParentClassObj));
if (!bSaved)
UMCPServer::Print(TEXT("Warning: save failed\n"));
for (UEdGraph* Graph : MCPUtils::AllGraphs(NewBP))
UMCPServer::Printf(TEXT("Graph: %s\n"), *MCPUtils::FormatName(Graph));
}
};