how to import an obj file during run time?

I’m trying to load an OBJ file as a UStaticMesh at runtime in Unreal Engine using C++. How can I achieve this?

I have a C++ class RenderObject that starts a process and generates an OBJ file. I want to import that OBJ file into Unreal Engine and add it to a specific location in the scene at run time.

Can I load the OBJ file directly into the class and add it to the scene at a specific location? If so, how can I achieve this?

Here’s the code I have so far:

// Called when the game starts or when spawned
void RenderObject::BeginPlay()
{
	Super::BeginPlay();

	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("__3D Object__ Loading"));
	std::cout << "__3D Object__ Loading" << std::endl;
	    
    // Load the OBJ file as a UStaticMesh 
    FString ObjFilePath = "/testin_1_2.obj"; // Change this to the path of your OBJ file
    UStaticMesh* StaticMesh = LoadObject<UStaticMesh>(nullptr, *ObjFilePath);

    // Create a new StaticMeshActor
    AStaticMeshActor* StaticMeshActor = GetWorld()->SpawnActor<AStaticMeshActor>();

    // Set the static mesh of the StaticMeshActor
    StaticMeshActor->GetStaticMeshComponent()->SetStaticMesh(StaticMesh);

    // Set the location of the StaticMeshActor
    FVector Location = FVector(1000.f, 915.f, 3.f); // Change this to the location you want
    StaticMeshActor->SetActorLocation(Location);
	
}

I’m not sure if this is the correct way to achieve what I want. Can someone please guide me in the right direction?

Thanks in advance!

The engine doesn’t have a native way to load obj files at runtime. All the asset import logic like what would normally create a static mesh in the editor isn’t available at runtime.

Now… There are ways to create a UStaticMesh at runtime, as well as other options like RealtimeMeshComponent which I am the creator of, but out of the box the engine doesn’t have the actual loading code to read an obj file into anything useful.

I am working on extensions to the RMC that would allow this, along with many other formats, but they’re not done yet. There are other plugins around that can load obj files into ProceduralMeshComponent, but as a warning that’s not a very efficient component.

LoadObject will certainly not do what you’re hoping for here.

1 Like

Thanks a lot for the reply. I was considering buying your plugin. Can it be used for this purpose?

Any word on the progress of that?