Monday, 19 May 2025

Python in UE5

 TO start, go into the unreal plugins, look for python. Turn on Python Editor Script Plugin. Go into settings and look for python, make sure developer mode is turned on (you will have to restart unreal).


    Go into your unreal project, inside intermediate>PythonStub, you'll find unreal.py file

    Make a new visual studio project, copy and paste this unreal.py file into your new project. 

    Make a new python file named logger.py. Import the unreal file and right out a log statement. This will help so that visual studio will autofill the unreal functions. 

import unreal

unreal.log("Tech Art Rocks!")

    Save this file. Then go to your unreal project. At the bottom right you should see a place to write commands. You can change this to python instead. 

    Copy and paste the file path that you saved the logger.py file, then add \logger.py to it and hit enter.

    You should see in your output log the text written out. 

    You can also log warnings and errors. 

unreal.log_warning('this is a warning')

unreal.log_error('this is error')

Press up on the keyboard while in the text box to go back to a previous command. 

If you want to have scripts that automatically load on startup, go to project settings, 


    Restart Unreal to see the script automatically run when it starts up again. 

    If you want to add more paths to the default PYTHON_PATH in your unreal project, you can also do that in the project settings using the 'additional paths' parameter.


    This will need to restart unreal again to apply the settings.
    Want to install other packages to your python like numpy? Go to this file location

C:\Program Files\Epic Games\UE_5.4\Engine\Binaries\ThirdParty\Python3\Win64

Open up a command prompt and change to this directory. 


    We're going to be using pip to install numpy


    This should have installed numpy and you can use it to perform matrix multiplication. 

    In unreal python, you have UObjects and they've got properties you can get/set them. You can see the documentation here. 

Here's a script that prints the name, path name, full name, and class type of selected objects in editor:

import unreal


#decorator

@unreal.uclass()

class EditorUtils(unreal.GlobalEditorUtilityBase):

    pass


selectedAssets = EditorUtils().get_selected_assets()


for asset in selectedAssets:

    unreal.log(asset.get_full_name())

    unreal.log(asset.get_fname())

    unreal.log(asset.get_path_name())

    unreal.log(asset.get_class())

    unreal.log(type(asset))

    unreal.log('#####################')

    A way of repurposing a class by taking an existing class out of context, and using it how you want it.
Say you wanted to make a blueprint with a python script. Unreal python has 'factorys' where you create assets. So if you want a blueprint, you need to make a BlueprintFactory. 
    Example script to make several blueprints depending on the parameters you give
import unreal
import sys

blueprintName = "AStreamingBlueprint"       #name of the blueprint
blueprintPath = "/Game/MyClassBlueprints"   #where we will put it

createdAssetsCount = int(sys.argv[1])       #gives us the first parameter the user will input
createdAssetName = str(sys.argv[2])         #gives us the second parameter the user will input

factory = unreal.BlueprintFactory()         #factory to make blueprint
factory.set_editor_property("parent_class", unreal.GameMode)  #changing the parent class

assetTools = unreal.AssetToolsHelpers.get_asset_tools()
for i in range(createdAssetsCount):
    assetName = f'{createdAssetName}_{i:02}'
    unreal.log(assetName)
    myAsset = assetTools.create_asset(assetName, blueprintPath, None, factory)      #creating the blueprint
    unreal.EditorAssetLibrary.save_loaded_asset(myAsset)        #will save your asset, it will not save automatically

    Putting 'createAssetWithArgs.py 3 FUBAR' in the command will create 3 blueprints. 



No comments:

Post a Comment

Common Art Sprint #4 Delivery

   Responsible for:  updating the jinroh model with the newest model  updating gun model with new uvs adding ik controls onto the ammo gunbe...