Static mesh built in cpp called in editor is not visible

I try to build a static mesh in a function library called from an editor tool blueprint.
But I can’t get my static mesh to be visible. Do I miss something ?

	auto actor = world->SpawnActor<AActor>();
	actor->SetActorLabel(TEXT("Terrain"));

	UStaticMeshComponent * mesh_component = NewObject<UStaticMeshComponent>(actor, FName(TEXT("Mesh")));

	actor->SetRootComponent(mesh_component);

	FMeshDescription mesh_desc;

	FStaticMeshAttributes attributes(mesh_desc);
	attributes.Register();

	TVertexAttributesRef<FVector3f> positions = mesh_desc.GetVertexPositions();

	mesh_desc.ReserveNewVertices(4);
	FVertexID v0 = mesh_desc.CreateVertex();
	FVertexID v1 = mesh_desc.CreateVertex();
	FVertexID v2 = mesh_desc.CreateVertex();
	FVertexID v3 = mesh_desc.CreateVertex();
	
	mesh_desc.ReserveNewVertexInstances(4);
	FVertexInstanceID vi0 = mesh_desc.CreateVertexInstance(v0);
	FVertexInstanceID vi1 = mesh_desc.CreateVertexInstance(v1);
	FVertexInstanceID vi2 = mesh_desc.CreateVertexInstance(v2);
	FVertexInstanceID vi3 = mesh_desc.CreateVertexInstance(v3);

	mesh_desc.ReserveNewUVs(4);
	FUVID uv0 = mesh_desc.CreateUV();
	FUVID uv1 = mesh_desc.CreateUV();
	FUVID uv2 = mesh_desc.CreateUV();
	FUVID uv3 = mesh_desc.CreateUV();

	FPolygonGroupID polygon_group = mesh_desc.CreatePolygonGroup();

	mesh_desc.ReserveNewPolygons(1);
	mesh_desc.CreatePolygon(polygon_group, {vi0, vi1, vi2, vi3});

	positions = attributes.GetVertexPositions();

	positions[0] = FVector3f(-100, -100, 0);
	positions[1] = FVector3f(100, -100, 0);
	positions[2] = FVector3f(100, 100, 0);
	positions[3] = FVector3f(-100, 100, 0);

	TVertexInstanceAttributesRef<FVector3f> normals = attributes.GetVertexInstanceNormals();

	normals[0] = FVector3f(0, 0, 1);
	normals[1] = FVector3f(0, 0, 1);
	normals[2] = FVector3f(0, 0, 1);
	normals[3] = FVector3f(0, 0, 1);

	TVertexInstanceAttributesRef<FVector2f> uvs = attributes.GetVertexInstanceUVs();

	uvs[0] = FVector2f(0, 0);
	uvs[1] = FVector2f(1, 0);
	uvs[2] = FVector2f(1, 1);
	uvs[3] = FVector2f(0, 1);

	mesh_desc.TriangulateMesh();

	// At least one material must be added
	UStaticMesh* mesh = NewObject<UStaticMesh>(actor, FName(TEXT("ProceduralStaticMesh")));
	mesh->GetStaticMaterials().Add(FStaticMaterial());

	UStaticMesh::FBuildMeshDescriptionsParams mdParams;
	mdParams.bBuildSimpleCollision = true;
	//mdParams.bFastBuild = true;

	// Build static mesh
	mesh->BuildFromMeshDescriptions({&mesh_desc}, mdParams);
	
	mesh_component->SetStaticMesh(mesh);

Strangely if I save my scene and restart, the static mesh becomes visible.
Is there something to call to refresh something in the editor?

Perhaps this can help undestand :


The mesh details is completly grey out, so I can not even edit the static mesh to debug it.

If I duplicate it, the duplicata is editable … :stuck_out_tongue:

I’m not sure, but don’t you have to Register() the component if you create it in runtime? I see you Register() attributes, but not mesh_component.
It might not be the case, I’m just speculating.

1 Like

I think the register of FStaticMeshAttributes has nothing to do with the one of component, but your are right, it seems to be very important to call RegisterComponent on dynamically created components!
Thanks.
So one problem stay, I cant edit the static mesh component content, everything is grey out with the property warning.

mesh_component->CreationMethod = EComponentCreationMethod::Instance (perhaps native is best for my case)
before the RegisterComponent seems to do the job to get it editable.

1 Like