Unreal Engine - Renaming Tool
Unreal Engine - Renaming Tool

Unreal Engine - Renaming Tool

Created on:
Team Size: 1
Time Frame: 2 days
Tool Used: Python/UE5

Introduction

During my studies, I worked for projects at Styles Studio SàRL. During that time I sometimes had to integrate objects from divers origins into Unreal projects. Since the naming conventions weren’t strictly established I often had to rename objects and folder.

Renaming a unique object isn’t a problem, but when there are thousands it can be time consuming. For that reason I decided to create a script that would automatically rename object according to the desired input.

Installation

For the installation it is possible to follow Unreal Engines documentation about scripting with python. It gives a basic understanding of how to procede.

To avoid having to call a script with its absolute path it is possible to “drag and drop” it in the Engines folder. Following the “normal” installation it should be located in the following path:
C:\Program Files\Epic Games\UE_{version}\Engine\Plugins\Experimental\PythonScriptPlugin\Content\Python

Renaming script

As stated previously, the idea is to rename all the selected objects and replace particles of their name.

Unreal Engine materials with bad naming

To do so it is possible to use the code given below

Code

import unreal
import sys

def rename_assets(search_pattern, replace_pattern):

    # instances of unreal classes
    system_lib = unreal.SystemLibrary()
    editor_util = unreal.EditorUtilityLibrary()
    string_lib = unreal.StringLibrary()

    # gets all the assets selected in editor
    selected_assets = editor_util.get_selected_assets()
    # gets the number of assets
    num_assets = len(selected_assets)

    replaced = 0

    # logs the amount of assets selected
    unreal.log("Selected {} asset/s".format(num_assets))

    # renames the assets
    for asset in selected_assets:
        asset_name = system_lib.get_object_name(asset)

        unreal.log(asset_name)
        if string_lib.contains(asset_name, search_pattern, use_case=False):
            replaced_name = string_lib.replace(asset_name, search_pattern, replace_pattern)
            editor_util.rename_asset(asset, replaced_name)

            replaced += 1
            unreal.log("Replaced {} with {}".format(asset_name, replaced_name))
        else:
            unreal.log("{} did not match the search pattern, was skipped".format(asset_name))
    
    unreal.log("Repalced {} of {} assets".format(replaced, num_assets))

# function call definition
rename_assets(sys.argv[1], sys.argv[2])

With this code placed in the correct folder (see: Installation) it is possible to run it by typing: “rename_asset.py {the search patterns}\ {the replacement}” in the console after selecting the object to operate on.

UE Materials & Python Console

The Result should be the following:

UE Materials & Python Console

Consolidation script

This second script uses the “consolidate_assets” for Unreal Engine, it is used to merge duplicate objects contained in folder it is used by typing: “consolidate_to_file.py {file destination}\ ”.

Code

import unreal
import sys
import os

editor_util = unreal.EditorUtilityLibrary()
editor_asset_lib = unreal.EditorAssetLibrary()
system_lib = unreal.SystemLibrary()
string_lib = unreal.StringLibrary()

def consolidate_to_file(TargetConsolidationFile):
#Get selected assets
    selected_assets = editor_util.get_selected_assets()

    for i in range(len(selected_assets)):

        #Get the first (only take one) OBJECT
        asset = []
        asset.append(selected_assets[i])
        unreal.log(asset)

        #object NAME
        name = asset[0].get_fname()
        unreal.log(name)

        path = asset[0].get_path_name()
        unreal.log(path)

        #result = source_path + "/Texture/{}".format(name) + ".uasset"
        #unreal.log(result)

        x = path.split("/")
        finalString = ""
        for i in range(len(x)-1):
            finalString += x[i] + "/"

        finalString += "{}".format(TargetConsolidationFile) + "/{}".format(name)
        #finalString = x[0] + "/"+ x[1] + "/" + x[2] + "/" + x[3] + "/Textures/{}".format(name)
        unreal.log(finalString)

        asset_to_consolidate_to = editor_asset_lib.find_asset_data(finalString).get_asset()
        unreal.log(asset_to_consolidate_to)

        editor_asset_lib.consolidate_assets(asset_to_consolidate_to, asset)

consolidate_to_file(sys.argv[1])
© 2025 Samuel Styles