Creating new uasset files in code/CPP?

Hi,

I’m curious if there is a way to generate a lot of uasset files automatically for my project? Ideally this would be a commandlet or a button I can create on the editor to do this (I have no intention of doing this at runtime in the game!)

Basically, I have a lot of translated dialogue data and audio files (thousands) that are supplied to me as audio files and with a nice JSON object of who is saying each line and the text etc.
I’d quite like to automate importing the audio files, then creating a series of DialogueWave objects with data filled in based on the JSON file I supply (the correct Dialogue Voice speaker etc.).

Ideally, I’d run UE4-EditorCmd.exe -run=MyAutoGenCommandlet -input=MyDialogueDatabase.json (or something) to create all the DialogueWave assets automatically, then our build system could commit all the new DialogueWave files ready for the team to use. I’ve been going through the documentation trying to work out how to create uassets and write them to the content browser/disk, but not had any luck.

Thanks!

This is excellent! Thank you very much.
Gave it a quick go and managed to get one asset to popup, so looks like this is the way to go!
:slight_smile:

I created an import factory to do this. Basically I have a text file that contains all the assets I want to create and I read these in and do the following:

FString PackageName = TEXT( "/Game/SomePackageLocation/NewAssetName" );
UPackage* Package = CreatePackage( NULL, *PackageName );

UMyAsset* NewAsset = new( Package, FName( "MyAsset" ), Flags | RF_Public ) UMyAsset( FPostConstructInitializeProperties( ) );

if( NewAsset != NULL )
{
   // Fill in the assets data here
}

FAssetRegistryModule::AssetCreated( NewAsset );
NewAsset->MarkPackageDirty( );

And after the import factory has finished, I will have a bunch of new assets and all I have to do is hit the save button.

Could you tell me how to use UMyAsset class in example please? Thanks so much.

You would have an actor or component that has a variable UMyClass* Asset; or something along those lines, then you can expose to BP and select the asset originally imported. Or you could do a LoadObject( NULL, TEXT( “/Game/SomePackageLocation/NewAssetName” ) ); (Pointing to the location of the created asset from above) during PostLoad of the actor

Thanks so much for your reply.
But, so, now I could only create new uasset for object in Content Browser of Unreal (like path: “/Game/SomePackageLocation/NewAssetName”).
Example, may I create new texture in Content Browser from a file in Windows Explorer (like “D:\test.psd” or “D:\test.png”…) by C++?
Thanks you so much, again.

I have just been made some C++ code to put a mesh with material into specify position in map, but this mesh and material must be imported in Content Browser of Unreal (import by: click import in UE → choose file → like path: “/Engine/EditorMeshes/ColorCalibrator/SM_ColorCalibrator.SM_ColorCalibrator” and “/Engine/EditorMeshes/ColorCalibrator/M_GreyBall.M_GreyBall’”).

I want to get this mesh from direct path in disk (like: “D:\test.fbx”, “D:\test.png”, “D: test.psd”… instead by path in Content Browser.

Can u help me, please? Thank you so so so much :smiley:

I think “new” should be replaced by the UE4 UObject::NewObject function, but not 100%

Hi, where do you put these code ? thanks a lot~

UTestingAsset is a UDataAsset.

#include "AssetRegistryModule.h"

void ATCharacter::TestAssetSave( )
{
	FString AssetPath = FString("../../../Playground/Content/Dev/");
	FString PackagePath = FString("/Game/Dev/TestingAsset");

	UPackage *Package = CreatePackage(nullptr, *PackagePath);
	UTestingAsset* TestAsset = NewObject<UTestingAsset>(Package, UTestingAsset::StaticClass(), *FString("TestingAsset"), EObjectFlags::RF_Public | EObjectFlags::RF_Standalone);

	TestAsset->TestFloat = 3.14;
	TestAsset->TestInt = 12;
	TestAsset->TestString = FString("Testing this string");

	FAssetRegistryModule::AssetCreated(TestAsset);
	TestAsset->MarkPackageDirty();

	FString FilePath = FString::Printf(TEXT("%s%s%s"), *AssetPath, *FString("TestingAsset"), *FPackageName::GetAssetPackageExtension());
	bool bSuccess = UPackage::SavePackage(Package, TestAsset, EObjectFlags::RF_Public | EObjectFlags::RF_Standalone, *FilePath);

	UE_LOG(LogTemp, Warning, TEXT("Saved Package: %s"), bSuccess ? TEXT("True") : TEXT("False"));	
}

Okay, NONE of the solutions I found only where helpful for automatically saving the Asset, so here is my 50th cents… The relative path that needs to be used for Package saving is quite confusing, but I found a good way of saving assets through their packages with the following snippet:

Creating the Asset should be well covered by the previous answers. Just make sure to mark the Package with FullyLoad() if the asset is ready to be saved, otherwise Unreal will throw an error because the file isn’t loaded, thus cannot be saved, etc.