Plugin - Get all materials in current project

Hi, I want to offer my users to pick a material from a dropdown list OR let them assign a material from the content browser. To both requirements I cannot find any solution (Neither to finding all materials in my project or just in one folder nor to display a material field in my plugin).

Can you give me any hint on how to do this?

Do you want them to do this in the editor? Or in your game?

In the editor.

Simply creating a property on a struct/class will allow the user to select the assets available in your project. For materials, use MaterialInterface, not Material.

In c++, you’ll need to make it a UPROPERTY with the ‘EditAnywhere’ flag.

Hi TTaM, thanks for your answer. Unfortunately I cannot use UPROPERTY in a plugin tab in Slate. For an actor sure, I’d simple chose a UPROPERTY to edit a material.

Slate? Oh, well, I’ll leave that to the experts.

push, any ideas? I don’t care if I show the materials just by names in a drop down or in an image in my slate widget. My problem is that I do not know how to determine all materials from the project in a widget. There must be some easy to use kind of function for that case, right?

Oh yes, I remember now. You can do this…

GetObjectsOfClass( UMaterialInterface::StaticClass(), … );

You might also need to use a UObjectLibrary to load all the materials, if they aren’t loaded automatically.

TTaM, you are great! With your help I managed to write a function to get all materials from the editor.

TArray<UMaterialInstance*>* RoadNetworkWidget::getMaterialInstancesFromPath(FString _path) {

	TArray<UMaterialInstance*>* result = new TArray<UMaterialInstance*>();

	UObjectLibrary *lib = UObjectLibrary::CreateLibrary(UMaterialInstance::StaticClass(), false, true);
	UE_LOG(LogTemp, Warning, TEXT("Searching for material instances in /Game/Materials..."));
	lib->LoadAssetDataFromPath(TEXT("/Game/Materials"));
	TArray<FAssetData> assetData;
	lib->GetAssetDataList(assetData);
	UE_LOG(LogTemp, Warning, TEXT("Found %d"), assetData.Num());

	for (FAssetData asset : assetData) {
		UMaterialInstance* mi = Cast<UMaterialInstance>(asset.GetAsset());
		if (mi) {
			UE_LOG(LogTemp, Warning, TEXT("Material instance %s"), *mi->GetName());
			result->Add(mi);
		}
	}
	return result;
}

Is this possible to do with BP?

Yes it is: http://api.unrealengine.com/INT/API/Runtime/Engine/Engine/UObjectLibrary/LoadBlueprintAssetDataFromPaths/index.html