Rename assets in C++ from plugin

Hello!

I have created a plugin for UE4. The main puprose of it is to rename assets inside project due to some external logic. I am using next code, but instead of renaming I get two similar assets with same names.

I am renaming from TestImage1 to TestImage2. Result is on the screenshot.

// Get the asset "TestImage1"
FAssetRegistryModule& AssetRegistryModule = FModuleManager::GetModuleChecked<FAssetRegistryModule>("AssetRegistry");
FAssetData XAssetData = AssetRegistryModule.Get().GetAssetByObjectPath(TEXT("/Game/TestImage1"));
UObject* Asset = XAssetData.GetAsset();
// Rename "TestImage1" into "TestImage2"
RenameAsset(Asset, TEXT("TestImage2"));

// Renaming method
void StandAloneModule::RenameAsset(UObject* Asset, const FString& NewName)
{
	FAssetToolsModule& AssetToolsModule = FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools");
	TArray<FAssetRenameData> AssetsAndNames;
	const FString PackagePath = FPackageName::GetLongPackagePath(Asset->GetOutermost()->GetName());
	new(AssetsAndNames)FAssetRenameData(Asset, PackagePath, NewName);
	AssetToolsModule.Get().RenameAssets(AssetsAndNames);

	FAssetRegistryModule::AssetRenamed(Asset, TEXT("/Game/TestImage1"));
	Asset->MarkPackageDirty();
	Asset->GetOuter()->MarkPackageDirty();
}

Hi RenovatioNova,

I’ve never tried to do this myself, but I believe the reason that it isn’t changing in the editor is due to the editor having two different names for most things, maybe 3. Normally there is the actual name of the variable or asset, then there is a friendly name and/or a display name. I’m not sure if DisplayName or FriendlyName is something than can be set for these assets, but it could be something similar to change the name of the asset in the editor itself.

Hope this helps

Thank you for a quick response!
But why do duplicates appear? The sequence was next:

  1. Only one TestImage1 asset in folder
  2. Call to rename method
  3. Second asset appear in folder (and first does not disapper)

I have copied renaming code from ContentBrowserUtils::RenameAsset from UnrealEngine/Engine/Source/Editor/ContentBrowser/Private/ContentBrowserUtils.cpp.

Hi RenovatioNova,

I actually managed to reproduce the issue itself without your plugin and simply renaming assets in the editor. It seems like it is an issue with the renaming itself and not your plugin, since you are copying the same code. I’ve reported it in our database. It’s possible that it is by design, as the code isn’t intended to be used in the way that you’re using it, but it is possible that it will be fixed. For your reference, the bug number is UE-16437. I’ll be sure to update you with any information that surfaces about this issue.

Have a nice day

Thank you very much for a quick response! Is there any kind of workaround? My task is: rename/move/create/delete assets (saving all references when possible inside project) due to some external logic.

Other than using some different code, which I’m unsure of exactly what you need to do due to my lack in the development of plugins, I can’t think of anything that you could use as a workaround.

Hi,

This plugin (MORT) will help you to easy rename actors\objects in the World Outliner and Content Browser without bugs:
https://www.unrealengine.com/marketplace/multi-objects-renaming-tool

this is the youtube video: https://youtu.be/UhEdy13AA-k

Features:

  • Find and replace
  • Rename and Numerate
  • Prefix
  • Suffix

This still hasn’t been fixed on unreals side. Copying still bugged, it creates a duplicate, doesn’t rename the ID only the file. Then unreal has issues because there are 2 files with the same ID. (the display name)

Did you try the MORT plugin? It does not has issues that you mentioned.

Do you have new clues on this issue? I got the same problem using the Python scripting, obviously a binding to what you’ve been using.

After waiting for the asset to load, using UEditorAssetSubsystem::RenameAsset worked.

FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry");

if (AssetRegistryModule.Get().IsLoadingAssets())
{
	AssetRegistryModule.Get().OnFilesLoaded().AddRaw(this, &RenameAsset);
	return;
}

RenameAsset();
void RenameAsset()
{
	auto editorAssetSubsystem = GEditor->GetEditorSubsystem<UEditorAssetSubsystem>();
	editorAssetSubsystem->RenameAsset(oldPath, newPath);
}
1 Like