# Creating new blueprint and components through code

**URL:** <https://forums.unrealengine.com/t/creating-new-blueprint-and-components-through-code/32743>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [June 16, 2015, 2:57pm UTC](https://forums.unrealengine.com/t/creating-new-blueprint-and-components-through-code/32743 "2015-06-16T14:57:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grizly32](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@grizly32](https://forums.unrealengine.com/u/grizly32)\
**Post date:** [June 16, 2015, 2:57pm UTC](https://forums.unrealengine.com/t/creating-new-blueprint-and-components-through-code/32743/1 "2015-06-16T14:57:17Z")

</div>

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):

```auto

        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:

```auto

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:

```auto

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

---

<div class="post-metadata">

**Author:** ![grizly32](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@grizly32](https://forums.unrealengine.com/u/grizly32)\
**Post date:** [June 16, 2015, 5:27pm UTC](https://forums.unrealengine.com/t/creating-new-blueprint-and-components-through-code/32743/2 "2015-06-16T17:27:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![grizly32](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@grizly32](https://forums.unrealengine.com/u/grizly32)\
**Post date:** [June 16, 2015, 10:39pm UTC](https://forums.unrealengine.com/t/creating-new-blueprint-and-components-through-code/32743/3 "2015-06-16T22:39:47Z")

</div>

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

FbxBlueprints.cpp

```auto

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:

```auto

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!

---

<div class="post-metadata">

**Author:** ![iGala](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@iGala](https://forums.unrealengine.com/u/iGala)\
**Post date:** [March 20, 2018, 1:30pm UTC](https://forums.unrealengine.com/t/creating-new-blueprint-and-components-through-code/32743/4 "2018-03-20T13:30:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mike\_M](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mike\_M](https://forums.unrealengine.com/u/Mike_M)\
**Post date:** [March 11, 2021, 8:14pm UTC](https://forums.unrealengine.com/t/creating-new-blueprint-and-components-through-code/32743/5 "2021-03-11T20:14:53Z")

</div>

> [@iGala](#):
>
> @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
