Hello,
I am just starting out on Unreal and C++, and am trying to expose to blueprints the UMaterial::GetUsedTextures function. My intent is to create a blueprint node that inputs a material and outputs a texture array.
I was following Rama’s documentation on this - but I just can’t seem to get it right.
I’ve created a blueprintfunction library, titled “GetUsedTextures” (Probably not the best idea to name my function library the same as the function I want to expose.)
Heres my code, if anyone could help me out that would be great! Thank you!
Header
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Material.h"
#include "GetUsedTextures.generated.h"
/**
*
*/
UCLASS()
class UNREALVRTEMPLATE_API UGetUsedTextures : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
UFUNCTION(BlueprintCallable, Category = CustomBlueprint)
array TexturesFunction(TArray < UTextures * >& OutTextures, UMaterial* GrabbingMaterial);
}
CPP
#include "UnrealVRTemplate.h"
#include "Material.h"
#include "GetUsedTextures.h"
array GetUsedTextures::TexturesFunction(TArray < UTextures * >& OutTextures, UMaterial* GrabbingMaterial)
{
UMaterial::GetUsedTextures(TArray <UTexture *> & OutTextures);
return OutTextures;
}
Thank you!
Working some more on it - believe I’ve made progress. Searching through the code I found PrimitiveComponent.h uses the same function I want to use. I copied its code but I’m still kicking up an error - I believe it has something to do with my UFunction.
Getting only one error now (yay). (:Error: TheFunctions.h(21) : Error: Cannot find class ‘’, to resolve delegate ‘Type’).
h file
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Materials/Material.h"
#include "TheFunctions.generated.h"
/**
*
*/
UCLASS()
class UNREALVRTEMPLATE_API UTheFunctions : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
virtual void GetUsedMaterials(TArray<UMaterialInterface*>& OutMaterials, bool bGetDebugMaterials = false) const {}
UFUNCTION(BlueprintPure, Category = "CustomBlueprints,Materials")
void GetUsedTextures(TArray<UTexture*>& OutTextures, EMaterialQualityLevel::Type QualityLevel);
};
cpp
#include "UnrealVRTemplate.h"
#include "Materials/Material.h"
#include "TheFunctions.h"
void UTheFunctions::GetUsedTextures(TArray<UTexture*>& OutTextures, EMaterialQualityLevel::Type QualityLevel)
{
// Get the used materials so we can get their textures
TArray<UMaterialInterface*> UsedMaterials;
GetUsedMaterials(UsedMaterials);
TArray<UTexture*> UsedTextures;
for (int32 MatIndex = 0; MatIndex < UsedMaterials.Num(); ++MatIndex)
{
// Ensure we don't have any NULL elements.
if (UsedMaterials[MatIndex])
{
auto World = GetWorld();
UsedTextures.Reset();
UsedMaterials[MatIndex]->GetUsedTextures(UsedTextures, QualityLevel, false, World ? World->FeatureLevel : GMaxRHIFeatureLevel, false);
for (int32 TextureIndex = 0; TextureIndex<UsedTextures.Num(); TextureIndex++)
{
OutTextures.AddUnique(UsedTextures[TextureIndex]);
}
}
}
}
UFUNCTION(BlueprintCallable, Category = “CustomFunctions”)
static void GetUsedTextures(UMaterial* Material, TArray<UTexture*>& OutTextures);
void UTheFunctions::GetUsedTextures(UMaterial* Material, TArray<UTexture*>& OutTextures)
{
//adjust if needed
Material->GetUsedTextures(OutTextures, EMaterialQualityLevel::High, true, ERHIFeatureLevel::SM5, true);
}
Nachtmahr,
Thank you so much for your help! That answer worked perfectly and clued me into referencing functions.
Cheers!
Hello again - could anyone point me in the right direction?