Automating Unreal Engine editor with Python

Unreal Engine gives you several options to create custom tools for automating tasks in Unreal Editor. You can write C++ code, create Editor Utility Blueprints or write Python. While Blueprints are cool, Python is industry standard for writing pipeline tools and in my opinion is much convenient than doing things with Blueprint nodes in many cases – like dealing with filenames and connecting to things outside Unreal Editor.

There are a lot of things you could add to Unreal Editor using editor Blueprints or Python – avoiding repeating manual tasks, automating level building, automating asset management and implementing content creation pipeline. One of the most common things that benefit from automation is dealing with large amount of assets. For example – automatically creating material instances for your meshes with correct texture parameters assigned or automatically renaming your assets according to specific naming convention.

Unreal Editor allows you to run python scripts directly from Console or from editor File menu. But much cooler way is to use Editor Utility Blueprints to call your python scripts. Editor Utility Blueprints (or Editor Widget Blueprints if you need fancy UI) are great ways to extent Unreal Editor functionality – there are several useful parent classes available for you:

  • ActorActionUtility  – add custom right click menu functions when working with Actors in levels
  • AssetActionUtility – add custom right click menu functions when working with assets in Content Browser
  • EditorValidatorBase – create custom data validators when importing assets (i.e. ensuring proper naming etc.)
  • several others

In this article I will quickly show how to use Python to do asset processing.

1. Create new Editor Utility Blueprint in the Content Browser.

2. Choose AssetActionUtility as a parent class for your new Blueprint

3. Click create New function in the left sidebar and name it. Then add Execute Python Command node and connect it’s exec port to function node. Type name of your python script file in the Python Command parameter. You can add optional command line parameters at the end if needed (for example, param1 param2). You can use Blueprint string operation nodes to build this parameter string dynamically in Blueprint based on context if needed.

4. Create your python script and save it under Content/Python/ under your Project’s folder. (This is one of several locations Unreal Editor uses to find python scripts).

Other locations you can use according to UE docs:

  • The Content/Python sub-folder under your Project’s folder.
  • The Content/Python sub-folder in the main Unreal Engine installation.
  • The Content/Python sub-folder under each enabled Plugin’s folder.
  • The Documents/UnrealEngine/Python folder inside your user directory. For example, on Windows 10, this is equivalent to C:/Users/Username/Documents/UnrealEngine/Python

Here is a sample Python code:

import unreal
unreal.log("Hello Unreal!")

#Get a list of selected assets in Content Browser
sel_assets = unreal.EditorUtilityLibrary.get_selected_assets()

for sa in sel_assets:    
    asset_name = sa.get_name()    
    asset_folder = unreal.Paths.get_path(sa.get_path_name())

    #Check if this asset is a Texture. Then check if it's name starts with T_. If not, prepend T_ to the name.
    if sa.get_class().get_name() == "Texture2D":
        if not asset_name.startswith("T_"):
            if not unreal.EditorAssetLibrary.rename_asset(sa.get_path_name(), asset_folder + "/T_" + asset_name):
                unreal.log_error("Could not rename: " + sa_get_path_name())

5. Right click on selected assets in Content Browser and choose your action from the Scripted Actions menu.

One Comment

Post A Comment