Renaming an asset is hell...

Spent probably 2 whole days on this topic, and digged very deep into the API doc, but still couldn’t get asset renaming working with the Epic Python Scripting plugin.




import unreal as ue

ar = ue.AssetRegistryHelpers.get_asset_registry()  

path = '/Game/Path/To/MyAsset'  

new_path = '/Game/Path/To/MyNewAsset'  

ad = ar.get_asset_by_object_path(path)  

rnd = ue.AssetRenameData(asset=ad.get_asset(), new_package_path='/Game/Path/To', new_name='MyNewAsset')  

rnds = ue.Array(ue.AssetRenameData)  

rnds.append(rnd)  

at = ue.AssetToolsHelpers().get_asset_tools()  

ue.EditorAssetLibrary.save_asset(path, only_if_is_dirty=False)    

ue.load_asset(path)  

at.rename_assets(rnds)  

ue.EditorAssetLibrary.save_asset(new_path, only_if_is_dirty=False)


This code keeps giving me errors:


  

DataValidation: Error: /Game/Path/To/MyNewAsset contains invalid data.  

DataValidation: Error: Data validation FAILED. Files Checked: 1, Passed: 0, Failed: 1, Skipped: 0, Unable to validate: 0



I got the idea that in Unreal a bunch of things can share the same name. So I expect that renaming is not just a string manipulation but involves tracing and dependency tracking …

But this workflow is crazy.

I had to resort to the 20tab Python plugin on github, and the following code does the trick for me.




import unreal_engine as ue  

assets = ue.get_selected_assets()  

for a in assets:      

    old_name = a.get_name()      

    full_name = a.get_full_name()      

    full_path = full_name.split(' ', 1)[1]      

    asset_path = full_path.split('.', 1)[0]      


    a.save_package()      

    ue.rename_asset(full_path, 'MyNewAsset')      

    a.get_outer().set_name('/Game/Path/To/MyNewAsset')      

    a.save_package()



Still quite cumbersome IMO, but at least it gets the job done.

Am I missing something in the Epic Python API or is it just such a daunting task to rename?

P.S., I’m fairly new to both scripting APIs.

Alright, figured it out. I should’ve used high-level API EditorAssetLibrary instead of the low-level
AssetRegistryHelpers

Renaming assets can cause various issues… read here:

@DarkS474 Thanks for the pointer. So I figured the correct workflow would be:

  • Rename an asset;
  • Run the project-wise command: UE4Editor.exe <GameName or uproject> -run=ResavePackages -fixupredirects -autocheckout -projectonly -unattended;
  • Checkin to SCM.

Correct?

You can use special tool for renaming like MORT:

It could rename your assets in the ue4 project without issues. And this tool could work with actors too.