Create blueprint asset file with C++

It’s perhaps not too difficult to achieve, but right now I don’t find the right information concerning this.

I want to create a new blueprint in C++ as part of an importer module within our project, which I’m writing myself. With that I mean creating an actual full blueprint asset file (not just a node or a function etc.), that goes into the editor’s asset browser for content creators to use. I want to be able to define the name, superclass and file path of the blueprint in my function.

What I found so far was this:

    FString PackageName = TEXT("/Game/SomePackageLocation/" + name);
    UPackage *Package = CreatePackage(NULL, *PackageName);

    UMyAsset *NewAsset = new (Package, FName("MyAsset"), Flags | RF_Public) 
                       UMyAsset(FPostConstructInitializeProperties());

    if (NewAsset != NULL)
    {
        // Fill in the assets data here
    }

    FAssetRegistryModule::AssetCreated(NewAsset);
    NewAsset->MarkPackageDirty();

I’m don’t know how to use this code snipped or what exactly it does (I can only assume it creates an asset) and I also think it’s not applicable to what I want to achieve, since I want a blueprint based on a given superclass.

How can I do that?

unfortunately u cannot create a uasset file from any external sources. you need to use the editor for this.

That doesn’t seem right. I mean other import plugins achieve the same, that’s also what the AssetRegistryModule is for, right?

hmm maybe im just not understanding it right but if you want to create a Custom Editor Asset use this: https://orfeasel.com/creating-custom-editor-assets/

That already goes in the right direction. The difference is, that my goal doesn’t involve using the editor at the point of blueprint creation. It’s all about automation as part of a character import tool chain. The import (JSON data) is invoked in the editor, that’s true. After that, data is read, converted and should then automatically create blueprints for each imported character, so each blueprint should simply be based on our character class.

From your link, I’m fixating on this line:

//Create the editor asset 
UOrfeasCustomAsset* OrfeasEditorAsset = NewObject<UOrfeasCustomAsset>(InParent, InClass, InName, Flags);

That looks like the function I would need. Does this already create a new asset and then I’d only need to call FAssetRegistryModule::AssetCreated(NewAsset);
to have it registered? Unfortunately I can’t try right now, so I ask just to be sure up front.

I was able to find a function that may help after digging around in the FKismetEditorUtilities:

   FAssetToolsModule& AssetToolsModule = FModuleManager::GetModuleChecked<FAssetToolsModule>("AssetTools");
	
	FString name = "testBlueprint";
	name = UPackageTools::SanitizePackageName(name);
	name = ObjectTools::SanitizeObjectName(name);
	FName fname = *name;

	FString packagePath = "/Game/Test/" + name;
	UPackage* OuterForAsset = CreatePackage(nullptr, *packagePath);

	UClass* BlueprintClass = nullptr;
	UClass* BlueprintGeneratedClass = nullptr;

	IKismetCompilerInterface& KismetCompilerModule = FModuleManager::LoadModuleChecked<IKismetCompilerInterface>("KismetCompiler");
	KismetCompilerModule.GetBlueprintTypesForClass(YourClassType::StaticClass(), BlueprintClass, BlueprintGeneratedClass);
	UBlueprint* newBlueprint = FKismetEditorUtilities::CreateBlueprint(YourClassType::StaticClass(), OuterForAsset, fname, BPTYPE_Normal, BlueprintClass, BlueprintGeneratedClass, FName("GeneratingBlueprintTest"));
    FAssetRegistryModule::AssetCreated(newBlueprint);
	FAssetEditorManager::Get().OpenEditorForAsset(newBlueprint);

    OuterForAsset->SetDirtyFlag(true);
    TArray<UPackage*> packagesToSave;
    packagesToSave.Add(OuterForAsset);
    FEditorFileUtils::PromptForCheckoutAndSave(packagesToSave, false, false);

For my case this works, it creates the asset, saves it, and opens up the blueprint window in editor upon creation, which is all I need. However, as a warning, I ran in to trouble when I tried to save the asset before opening up the blueprint window in editor.

2 Likes

Awesome solution. You’re a saviour for me and my poor project.