Generating blueprint programmatically

Hello.

I am looking for a way / example on how to generate Blueprint programmatically. Basically I would like to create a Blueprint object and attach some components to it (instanced static mesh components and static mesh components) and save the generated Blueprint / have it available in the asset browser.

Thanks for any tips / advices on this.

After a bit of digging through source it seems I would need to leverage FKismetEditorUtilities::CreateBlueprint.

Hey ttvd, did you try this? the FKismetEditorUtilities::CreateBlueprint method I mean?

I have the exact same question as you do. Would love to know if you have any advancing in your search.

Hello. Yes! Create Package; Create Actor and attach all your components to it. Then use FKismetEditorUtilities::CreateBlueprintFromActor on your created actor. Then delete actor. We did it this way. Let me know if you find a better one. Hope this helps!!

1 Like

ttvd, nice! this is a bit dirty due to actor create/delete, but it is much simpler than working with FKismetEditorUtilities::CreateBlueprint. Thanks a lot, you’ve been a great help.

ttvd, nice! this is a bit dirty due to actor create/delete, but it is much simpler than working with FKismetEditorUtilities::CreateBlueprint. Thanks a lot, you’ve been a great help.

Is this question outdated? If you solved it, post anwser yourself and mark it :slight_smile:

Hey ttvd, I am trying to use this method but I keep getting a “Cast of Package /Engine/Transient to Level failed” error.

Would you mind taking a look?

FString NameStr = "/Game/Blueprint";
UPackage* myPackage = CreatePackage(NULL, *NameStr);
AMyClass* testActor = ConstructObject<AMyClass>(AMyClass::StaticClass());
UBlueprint* NewBlueprint = FKismetEditorUtilities::CreateBlueprintFromActor(FName("MyBP"), myPackage, testActor, false);
FAssetRegistryModule::AssetCreated(NewBlueprint);
NewBlueprint->MarkPackageDirty();
myPackage->SetDirtyFlag(true);
testActor->Destroy();

Thanks in advance!

Hey; please have a look at BakeBlueprint here:

and CloneComponentsAndCreateActor here:

Hope this helps.

Basically you are creating your actor within a transient package (2nd null parameter to ConstructObject); you need to create it within the current level for this to work, I think. See 2nd link above.

Hey! Thanks so much for the links! I am still having problems figuring out how to assign the actor to the correct ‘Outer’ though :frowning:

Do you know how to get the UWorld and ULevel I would need to assign the Actor to in order to not get that “Transient to Level” error?

Can’t seem to figure this out…very frustrating…

Thanks!

Hey. I would imagine it’s something like GWorld->GetCurrentLevel(); or ()->GetCurrentLevel() etc.

GWorld! That was it! Thank you, thank you!

I was using UWorld->GetCurrentLevel() and kept getting errors, didn’t even know there was a GWorld!

Thank you very much again for your help!! :slight_smile: