Python Module Example

The Asset code examples as one big module-style example

import unreal

EditorActorSubsystem = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)
LevelEditorSubsystem = unreal.get_editor_subsystem(unreal.LevelEditorSubsystem)
UnrealEditorSubsystem = unreal.get_editor_subsystem(unreal.UnrealEditorSubsystem)

from pathlib import Path
EditorAssetSubsystem = unreal.get_editor_subsystem(unreal.EditorAssetSubsystem)


def get_asset_path_for_file_path(file_path):
    """Get the asset path for the provided file path"""
    if not Path(file_path).exists():
        unreal.log_warning(f"Provided File Path does not exist: {file_path}")
        return
        
    # If the file path does exist in this Project, get its asset path:
    asset_path = unreal.PackageTools.filename_to_package_name(str(file_path))
    if not asset_path:
        unreal.log_warning(f"Could not find an Asset in this UE Project for {file_path}")
    
    return asset_path
    

def get_file_path_for_asset_path(asset_path) -> Path:
    """Get the file path for the provided asset path"""
    file_path = Path(
        unreal.PackageTools.package_name_to_filename(asset_path)
    ).resolve()
    if file_path.exists():
        return file_path
    
    for test_path in file_path.parent.glob(f"{file_path.name}.*"):
        return test_path 
    
    unreal.log_warning(f"Could not find a valid system path for {asset_path}")
    return None


# Demo Code to Run

# Load asset
asset_path = "/Game/some/asset/path"
asset = unreal.load_asset(asset_path)

# New instance of an asset - this is useful for
# creating the list of entry data for ListViews in Editor Utility Widgets
asset_instance = unreal.new_object(asset.generated_class())

# Default Object - useful for changing the defaults on Blueprint Assets
default_object = unreal.get_default_object(asset.generated_class())

# Call a BP function / event defined a Blueprint Asset's BP Graph and get its return
value = default_object.call_method("function_with_return")

Last updated