# UTexture\* to widget image, fails UTexture2D cast

**URL:** <https://forums.unrealengine.com/t/utexture-to-widget-image-fails-utexture2d-cast/1176194>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, question\
**Created:** [May 23, 2023, 6:28pm UTC](https://forums.unrealengine.com/t/utexture-to-widget-image-fails-utexture2d-cast/1176194 "2023-05-23T18:28:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![NCJBM](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@NCJBM](https://forums.unrealengine.com/u/NCJBM)\
**Post date:** [May 23, 2023, 6:28pm UTC](https://forums.unrealengine.com/t/utexture-to-widget-image-fails-utexture2d-cast/1176194/1 "2023-05-23T18:28:12Z")

</div>

Hi

So i’m trying to send the output render from Composure to my actor. I’ve got a class called PointerCompositingOutput which is a subclass of UCompositingElementOutput, where i can put my class as the output for Composure. It overrides the function responsible for handling the final render, and i’m setting the variable in my pawn object reference. I’m getting my reference to my actor in my scene through a TSoftObjectPtr.

In the BP of the pawn that PointerCompositingOutput sets, i did a GetMemorySize print result is 33177600. And printing IsValid, it returns false for a frame then returns true.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/6/1/a61dd48b8026be4dbee5d244bc40312b1b7cf15b.png)  
But when pressing play, i don’t see my image. In my widget im checking if it’s valid, if it’s not valid it uses an image from the content browser. The debug image works and does show up  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/b/a/cba10bca61eb366425e4c0309d17eaae54656445.png)

Within my function, there are 2 if statements, it will either set the UTexture to the pawn or save it as an exr file. I can verify that the images it outputs is indeed the image i expected to see.  
Within the if statement that sets the variable on my pawn, I’ve also bypassed my bool and have it set the variable every tick, but it doesn’t change the result.

cpp function override that handles composure UTexture

```auto
void UNcPointerCompositingOutput::RelayOutput_Implementation(UTexture* RenderResult, UComposurePostProcessingPassProxy* PostProcessProxy)
{
	
	ComposureRender = RenderResult;
	UWorld* World = GetWorld();

	UTexture* OutputImage = RenderResult;
	
	//Set variable of Pawn
	if (TargetActor.IsValid() && IsValid(RenderResult) && !WriteExrFiles && !stopSetting)
	{
		TargetActor->setTextureVal(ComposureRender);
		stopSetting = true;
		UE_LOG(LogTemp, Display, TEXT("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!its been set!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"));
	}
	
	//save Texture as EXR. Code copied from CompositingElementsOutput.cpp
	if (World && World->IsGameWorld() && WriteExrFiles) // Check if its in play mode or in editor mode
	{
		if (OutputImage && !OutputDirectiory.Path.IsEmpty() && !FilenameFormat.IsEmpty())
		{
			bool bShouldCapture = false;
			if (FrameNumber == 0)
			{
				bShouldCapture = true;
			}
			else
			{
				SecondsSinceLastCapture += FApp::GetDeltaTime();
				bShouldCapture = SecondsSinceLastCapture >= OutputFrameRate.AsInterval();
			}

			if (bShouldCapture)
			{
				FImageWriteOptions WriteOptions;
				WriteOptions.Format = EDesiredImageFormat::EXR;
				WriteOptions.bOverwriteFile = true;
				WriteOptions.bAsync = true;
				WriteOptions.CompressionQuality = (int32)Compression;

				const FString FrameIdString = FString::Printf(TEXT("%04d"), FrameNumber);
				FString Filename = FilenameFormat.Replace(TEXT("{frame}"), *FrameIdString);

				UImageWriteBlueprintLibrary::ExportToDisk(OutputImage, OutputDirectiory.Path / Filename, WriteOptions);

				++FrameNumber;
				SecondsSinceLastCapture = 0.0;
			}
		}
		else
		{
			InternalReset();
		}
	}
}

```

pawns setTextureVal function

```auto
void AVirtualProductionPawn::setTextureVal(UTexture* Tex)
{
	ComposureOutputRenderUTexture = Tex;
}

```

I’m very lost on this as i’ve spent nearly 2 days trying to figure this out, it also seems to fail when i cast the UTexture to UTexture2D in my pawns BP. Is there some formatting the UTexture that i could be missing?

Ultimately i’m looking to make my UTexture into a TArray as outlined in this glorious post.

> [@How to read pixels form a UTexture?](https://forums.unrealengine.com/t/how-to-read-pixels-form-a-utexture/563009):
>
> My question is as the title says. I have seen examples of how to read pixels from a Utexture2D, but cant find a way to get the pixel data from a UTexture. PS. The reason i want to do this, is because i want to convert a UTexture to UTexture2D.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [May 23, 2023, 6:51pm UTC](https://forums.unrealengine.com/t/utexture-to-widget-image-fails-utexture2d-cast/1176194/2 "2023-05-23T18:51:34Z")

</div>

> [@NCJBM](#):
>
> `TargetActor`

Did a quick test on begin play of an actor where Tex is a UTexture.

```auto
	if (Tex != nullptr) {
		if (UTexture2D* tex2d = Cast<UTexture2D>(Tex)) {
			UE_LOG(LogTemp, Warning, TEXT("Conversion ok"));
		}
		else {
			UE_LOG(LogTemp, Error, TEXT("Conversion failed"));
		}
	}

```

The cast works no problem. Try adding a nullptr test of what you are trying to convert to make sure your not converting an uninitialized texture.

---

<div class="post-metadata">

**Author:** ![NCJBM](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@NCJBM](https://forums.unrealengine.com/u/NCJBM)\
**Post date:** [May 23, 2023, 7:46pm UTC](https://forums.unrealengine.com/t/utexture-to-widget-image-fails-utexture2d-cast/1176194/3 "2023-05-23T19:46:19Z")

</div>

Hey again 3dRaven! This ones a real headscratcher.

So here’s a screenshot of that result and here’s where those prints are being called from.

- RelayOutput\_Implementation printing after it passes the if (updated if to check for nullptr)
- Pawn Blueprint printing GetMemorySize
- Pawn Blueprint printing is valid
- Pawn Blueprint printing Cast To Texture2D failed cast
- Pawn C++ SetTextureValue on failed cast.  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/2/c/42cd8d40696c0687d0ed49ee86819d1ec98b4579.png)

the settexture function  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/8/5/a85318eee0f19a781bf99315bb8617a2195333d6.png)

I’ve also added != nullptr check when it sets the variable and passing through the RenderResult.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/4/0/440e79a93fb75f06146d30c754661d46a30cb655.png)

But also if the variable is a nullptr, should the memory value be 33MB?

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [May 23, 2023, 7:57pm UTC](https://forums.unrealengine.com/t/utexture-to-widget-image-fails-utexture2d-cast/1176194/4 "2023-05-23T19:57:15Z")

</div>

Try setting a breakpoint where you are setting the UTexture and where you are assigning  
ComposureOutputRenderUTexture = Tex;  
Put a break point at the start of the function and see if your can hover over Tex once the breakpoint hits and see what it’s value is.

Seeing that the input value is of type UTexture it shouldn’t fail.

No a nullptr wouldn’t be 33MB.

I wonder if `WriteOptions.bAsync = true;` isn’t messing with the output.

With most async functions you would have a callback to continue further progress.

Could it be that the export is not finished while you advance to the next part of the code?

Edit:

After checking FImageWriteOptions there is a OnComplete delegate like i suspected. You should hook it up to a function and move the rest of the code there, or turn off async.

Right now your code is continuing before the write to disk is ready.

[OnComplete](https://docs.unrealengine.com/5.2/en-US/API/Runtime/ImageWriteQueue/FImageWriteOptions/OnComplete)

---

<div class="post-metadata">

**Author:** ![NCJBM](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@NCJBM](https://forums.unrealengine.com/u/NCJBM)\
**Post date:** [May 23, 2023, 8:15pm UTC](https://forums.unrealengine.com/t/utexture-to-widget-image-fails-utexture2d-cast/1176194/5 "2023-05-23T20:15:38Z")

</div>

So with the break points it looks like it’s all there.

BUT, i can’t believe i didnt do this before as i assumed the function override was saying it’s true class. Running the variable through the Get Class blueprint node is returning a TextureRenderTarget2D.

As for the OnComplete delegate. If i was sending this straight to my Widget to display the image, would it need that delegate, i thought it would try and put the image on the widget as soon as the variable updates. If it was an actual UTexture and not a TextureRenderTarget2D.  
I assume this is needed if i’m writing to disk and not trying to pass the data around and display in different areas?

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [May 23, 2023, 8:19pm UTC](https://forums.unrealengine.com/t/utexture-to-widget-image-fails-utexture2d-cast/1176194/6 "2023-05-23T20:19:35Z")

</div>

```auto

UTexture2D* UImageFunctionLibrary::CreateTexture(UTextureRenderTarget2D* TextureRenderTarget) {

	// Creates Texture2D to store TextureRenderTarget content
	UTexture2D* Texture = UTexture2D::CreateTransient(TextureRenderTarget->SizeX, TextureRenderTarget->SizeY, PF_B8G8R8A8);
#if WITH_EDITORONLY_DATA
	Texture->MipGenSettings = TMGS_NoMipmaps;
#endif
	Texture->SRGB = TextureRenderTarget->SRGB;

	// Read the pixels from the RenderTarget and store them in a FColor array
	TArray<FColor> SurfData;
	FRenderTarget* RenderTarget = TextureRenderTarget->GameThread_GetRenderTargetResource();
	RenderTarget->ReadPixels(SurfData);

	// Lock and copies the data between the textures
	void* TextureData = Texture->GetPlatformData()->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
	const int32 TextureDataSize = SurfData.Num() * 4;
	FMemory::Memcpy(TextureData, SurfData.GetData(), TextureDataSize);
	Texture->GetPlatformData()->Mips[0].BulkData.Unlock();
	// Apply Texture changes to GPU memory
	Texture->UpdateResource();
	return Texture;
}

```

Here’s an old function of mine to convert UTextureRenderTarget2D to a UTexture2D. Might be useful.

---

<div class="post-metadata">

**Author:** ![NCJBM](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@NCJBM](https://forums.unrealengine.com/u/NCJBM)\
**Post date:** [May 23, 2023, 9:06pm UTC](https://forums.unrealengine.com/t/utexture-to-widget-image-fails-utexture2d-cast/1176194/7 "2023-05-23T21:06:03Z")

</div>

Wow that code is great. Thanks so much, my jaw dropped checking this! This seems to potentially kill 2 birds with 1 stone as to what i’m trying to do.  
i think it’s working as it’s setting the class now as a UTexture2D and the data seems right when querying the XY size.

Wanted to double check though. As i’m taking this UTexture2D and setting the image variable inside my widget which has an “Image” in the canvas panel. Would i expect to see the texture on this as it’s updating every tick? Using the Widget reflector i can see that there is a Texure transient that it’s pointing too and every now and again the little preview window shows an image! I wanted to see the Texture2D image in the Widget as a way to verify that it is indeed all there and correct.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [May 23, 2023, 9:09pm UTC](https://forums.unrealengine.com/t/utexture-to-widget-image-fails-utexture2d-cast/1176194/8 "2023-05-23T21:09:01Z")

</div>

It would be better if the update to the widget was event driven. Only update it when the texture changes.

You could do a custom class based on uuserwidget with a bindwidget image exposed to c++. You could then access it from within your code.

---

<div class="post-metadata">

**Author:** ![NCJBM](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@NCJBM](https://forums.unrealengine.com/u/NCJBM)\
**Post date:** [May 23, 2023, 9:28pm UTC](https://forums.unrealengine.com/t/utexture-to-widget-image-fails-utexture2d-cast/1176194/9 "2023-05-23T21:28:32Z")

</div>

So to emulate a event i got the engine time code seconds%10 and Frames%15 so it only has a small amount of time to update. I do now see a white square.

May there be other ways to view the Texture2D to validate?

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [May 23, 2023, 9:34pm UTC](https://forums.unrealengine.com/t/utexture-to-widget-image-fails-utexture2d-cast/1176194/10 "2023-05-23T21:34:51Z")

</div>

You could temporarily pass in an actor class and set it’s material slot 0 with a new dynamic material. You would need to change the dynamic materials texture parameter and pass in the Utexture.

A bit of a bother but it would work.

Or go the route of passing in the custom uuserwidget with the bound image parameter and set brush from texture on it with the new UTexture.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [June 22, 2023, 9:35pm UTC](https://forums.unrealengine.com/t/utexture-to-widget-image-fails-utexture2d-cast/1176194/11 "2023-06-22T21:35:09Z")

</div>

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