How to create a UTexture2D outside of game thread?

I’m working on a plugin for the LeapMotion, an input device. It has its own thread for callback when a frame of data is available. On the event of a frame of data I grab an image (named Image below) from the Leap SDK and return it as a UTexture2D.

	Data = UTexture2D::CreateTransient(Image.width(), Image.height(), PF_G8);
	Data->UpdateResource();

	int8 *MipData = static_cast<int8*>(
		Data->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE)
	);

	SIZE_T DataSize = Image.width() * Image.height();
	FMemory::Memcpy(MipData, Image.data(), DataSize);

	Data->PlatformData->Mips[0].BulkData.Unlock();
	Data->UpdateResource(); //fails an assertion that its not IsInGameThread 

Unfortunately the second Data->UpdateResource() thread fails the assertion that its not called from within the game thread. How do I make this work so a struct returned from the plugin can contain a UTexture2D? Live code in question here if you want to see it in context:

https://github.com/jaxzin/UnrealEngine/blob/add-images/Engine/Plugins/LeapBlueprintSupport/Source/LeapBlueprintSupport/Classes/LeapFrameData.h#L649

Unfortunately I’ve tried that already without success. It still assumes you’re executing it from the game thread.

Great ideas, thanks! I started going down the path of triggering the conversion on Event Tick which I’m assuming is the game thread. But I think I’ll need to switch FLeapImage from a struct to a class so I can execute the UTexture2D creation in a getter method. Not sure yet if UPROPERTY and C++ can do getter methods. I’ve been out of C++ for a long time.

I believe this wiki page says you need to basically just copy and paste this function (since it’s implementation in the main engine is inaccessible from anywhere else): A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums

I don’t suppose there’s a way to pass the data as a parameter (or set of parameters) to some sort of a dynamic material instance (via the blueprint system perhaps?)

Or hey, if you extend Tickable to the leap listener component, or activate the texture transfer with an Event Tick in blueprints, that might activate it from the game thread. It would be asynchronous from receiving the images, but I suppose that’s the nature of inter thread communication :confused:

Made some progress by moving the conversion into a UFUNCTION that I can call from an Event Tick. But for some reason LeapImage.Data doesn’t have the right data when the function is called.

https://github.com/jaxzin/UnrealEngine/blob/b7cbc7917ddbec644812a13c0d12f7aee5855741/Engine/Plugins/LeapBlueprintSupport/Source/LeapBlueprintSupport/Private/LeapInputComponent.cpp#L125