How can I get asset filesystem path by using python?

hi there :slight_smile:
I want to get the file system path of the selected asset in the content browser.

In C++, you can use FPackagePath::LongPackageNameToFilename to get the relative path of the asset and change it to absolute path. Is there a function like this in Python?
I want to get relative paths at once, like the LongPackageNameToFilename function, not parsing strings.

FString package_name = TEXT("/Game/FirstPersonCPP/Blueprints/FirstPersonCharacter");
FString DestRelFilename = FPackageName::LongPackageNameToFilename(package_name , ".uasset");

/* result is : "../../../../Users/test_user/Documents/Unreal Projects/MyProject/Content/FirstPersonCPP/Blueprints/FirstPersonCharacter.uasset"
*/

This is a tricky one, although this could be a solution.
First you will need to create a c++ utility functions class and stuff this in:

myutils.h
UFUNCTION(BlueprintCallable)
static FString GetFilePath(const FString& uepath);

myutils.cpp
FString UMyUtils::GetFilePath(const FString& uepath)
{	
    FString Filename = FPackageName::LongPackageNameToFilename(*uepath);
    Filename = FPaths::ConvertRelativePathToFull(*Filename);
    return Filename;
}

Then in python do something like:

def filepath(uasset):
	import re
	path = uasset.object_path
	asset_name = uasset.asset_name
	path = unreal.myutils.get_file_path(path) #returns a sometimes incorrect absolute path
	expr = re.compile(r"\.%s$" % asset_name)
	if expr.search(path):
		path = expr.sub("", path)
	return "%s.uasset" % path

The answer is actually pretty easy all you need is to use get_system_path function from SystemLibrary unreal.SystemLibrary — Unreal Python 4.27 (Experimental) documentation

2 Likes