Creating new blueprint and components through code

Hi,

I have a button that creates a new blueprint with a class called FbxBuilder and I am trying to add components to that created blueprint but can’t seem to get it to work.

Here is my code:

(This runs when button is clicked):


        FString NameStr = "/Game/Blueprint";
	UPackage* myPackage = CreatePackage(NULL, *NameStr);
	UBlueprintFactory* BlueprintFactory = NewObject<UBlueprintFactory>();

	BlueprintFactory->ParentClass = AFbxBlueprints::StaticClass();

	FString CallingContext = "";

	UBlueprint* MyAsset = Cast<UBlueprint>(BlueprintFactory->FactoryCreateNew(UBlueprint::StaticClass(), myPackage, NAME_None, RF_Public | RF_Standalone, NULL, GWarn, FName(*CallingContext)));

	if (MyAsset)
	{
		// Notify the asset registry
		FAssetRegistryModule::AssetCreated(MyAsset);

		// Mark the package dirty...
		MyAsset->MarkPackageDirty();
	}
	AFbxBlueprints* myBP = (AFbxBlueprints*)MyAsset->GetBlueprintClass();
        myBp->FbxBpMaker();

That last line calls my FbxBlueprints class (the class of the blueprint) and here is the code for that:


AFbxBlueprints::AFbxBlueprints(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	
	BpRoot = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, TEXT("Root"));
	RootComponent = BpRoot;

	
}


void AFbxBlueprints::FbxBpMaker()
{

	FName MeshName("MYMESH");
	MyMesh = NewNamedObject<UStaticMeshComponent>(this, MeshName);
	MyMesh->RegisterComponent();
	MyMesh->AttachTo(RootComponent);

}

The reason I am doing this is that I am hoping to be able to do a ‘for’ loop and create as many StaticMeshComponents as I need.

The code compiles fine but when I do a Debug->Start New Instance from VS 2013 and click the button, I get a breakpoint from the RegisterComponent() at this location:


void UActorComponent::RegisterComponent()
{
	AActor* MyOwner = GetOwner();
	if (ensure(MyOwner && MyOwner->GetWorld()))
	{
		RegisterComponentWithWorld(MyOwner->GetWorld());
	}
}


I’ve tried a bunch of different things to get this to add a StaticMeshComponent, but nothing seems to work…any ideas?

Thanks in advance,

-Tim

PS - I am running this in ver. 4.8

Bump? Anyone? Really pulling my hair out on this one…

Ok, so I got the Static Mesh Component to show up in the blueprint by using this code in my blueprint class:

FbxBlueprints.cpp


void AFbxBlueprints::FbxBpMaker()
{

	FName MeshName("TestComponent");
	
	NextMesh = NewNamedObject<UStaticMeshComponent>(this, MeshName);
	NextMesh->AttachTo(RootComponent);
	NextMesh->RegisterComponentWithWorld(GetWorld());

}

But now I have another issue.

The component has no Details info even though I used this in the header:


UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "FbxBuilder")
		UStaticMeshComponent* NextMesh;

Also, when I click Compile in the blueprint the component disappears!

Anyone have any ideas about how to fix this?

Thanks in advance!

@grizly32 thanks for the info :)!
I have one question about your first post, w​​​​hat libraries did you include to create a UBlueprintFactory?

same question, since after adding include “Factories/BlueprintFactory.h” build failed