Loading UAssets from c++

I have a directory structure Game/Content/OldWest/VOL7/Meshes which contains uasset meshes. I am trying to load them dynamically through code (this is my first attempt). But I am unable to get the following code to load any UObjects. Any ideas, thanks?

    FString path = /*FPaths::ProjectContentDir() + */TEXT("/Content/OldWest/VOL7/Meshes/");

    UObjectLibrary* library = UObjectLibrary::CreateLibrary(UObject::StaticClass(), false, true);
    library->bHasBlueprintClasses = true;
    //library->AddToRoot();
    library->LoadAssetDataFromPath(path);
    library->LoadAssetsFromAssetData();

    TArray<UObject*> LoadedAssets;
    library->GetObjects(LoadedAssets);

    for (UObject* Asset : LoadedAssets)
    {
        if (Asset)
        {
            UE_LOG(LogTemp, Log, TEXT("Loaded Asset: %s"), *Asset->GetName());
        }
    }

Hey @Commander_Nimrod

The actual path for /Content is /Game, so you should replace β€œ/Content/OldWest/VOL7/Meshes/” with β€œ/Game/OldWest/VOL7/Meshes/”.

You can also do something like this to get all assets in a folder:


#include "LevelEditor.h"
#include "AssetRegistry/AssetRegistryModule.h"
#include "Engine/Blueprint.h"

// -------------
FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry");
IAssetRegistry& AssetRegistry = AssetRegistryModule.Get();

FARFilter Filter;
Filter.bRecursiveClasses = true;
Filter.bRecursivePaths = true;

// Get only Blueprint Assets
// You can modify this to get only StaticMesh or all the types you want if you want to filter it
Filter.ClassPaths.Add(UBlueprint::StaticClass()->GetClassPathName()); 

// Add your folder path
Filter.PackagePaths.Add("/Game/OldWest/VOL7/Meshes/");

// Get the assets and do stuff
TArray<FAssetData> AssetList;
AssetRegistry.GetAssets(Filter, AssetList);

for (const FAssetData& Asset : AssetList)
{
	// Do stuff with the assets...
}

Thanks for responding, is there anything else I need to do to the data? I can’t get anything in the AssetList.

You can use Filter.ClassPaths.Add(UStaticMesh::StaticClass()->GetClassPathName()); to get Static Mesh assets, or at least that should work. You can also not specify any ClassPath and that should get all assets in the folder regardless of the type.

That worked thanks.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.