Asset Path ↔ File Path

Switching between an Unreal Asset path and the Systems File path on disk

In order to get the Asset Path for a File, or a File for an Asset Path, we'll need to use the PackageTools module. There are some quirks to be aware of, but it's pretty reliable.

Systems File Path -> Unreal Asset Path

Getting the Unreal Asset path is fairly straight forward, if the resulting asset path is valid it means the asset does exist in the current Unreal Project:

# The windows file path - your path goes here
file_path = "D:/projects/MyUnrealProject/Content/asset/path/demo.uasset"

# If the file path does exist in this Project, get its asset path:
asset_path = unreal.PackageTools.filename_to_package_name(file_path)
 
# Check if the asset path was found & exists
if asset_path and EditorAssetSubsystem.does_asset_exist(asset_path):
    print(f"found valid asset: {asset_path}")

Unreal Asset Path -> Systems File Path

Getting the File Path is a bit more involved, using the function as-is isn't always enough:

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

asset_path = "/Game/some/asset/path"

# This uses pathlib.Path().resolve() to clean the file path,
# otherwise 
file_path = Path(unreal.PackageTools.package_name_to_filename(asset_path)).resolve()
print(f"found valid file path: {file_path} ({file_path.exists()})")

If the extension is missing from the result, one option we can use is to scan the folder for any matches:

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

def get_file_path_for_asset_path(asset_path) -> 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

This function will:

  1. Get the file path from the PackageTools function

  2. If valid, return the result

  3. otherwise, find and return the first file in the folder with our exact file name

Last updated