Skip to content

GetPath component to real path

using custom regex

cpp
    /** for skeletal */
    // USkeletalMeshComponent* skeletalComponent = Cast<USkeletalMeshComponent>(MyActor->GetComponentByClass(USkeletalMeshComponent::StaticClass()));
    UStaticMeshComponent* meshComponent = Cast<UStaticMeshComponent>(MyActor->GetComponentByClass(UStaticMeshComponent::StaticClass()));
    
    FString rootContentFolder = FPaths::ConvertRelativePathToFull(FPaths::ProjectContentDir());
    
    // Convert /Game/MyAssetFolder/MyBlueprint.MyBlueprint
    // to MyAssetFolder/MyBlueprint
    FString patternString(TEXT("(?<=\\/Game\\/)(.*\\.)"));
    FRegexPattern pattern(patternString);

    /** for skeletal */
    // skeletalComponent->SkeletalMesh.GetPath()
    FRegexMatcher matcher(pattern, meshComponent->GetStaticMesh().GetPath());

    if(matcher.FindNext())
    {
        // to <rootContentFolder>/MyAssetFolder/MyBlueprint.uasset
        FString finalPath = FPaths::Combine(rootContentFolder, FString::Printf(TEXT("%s.uasset"), *matcher.GetCaptureGroup(1)));

        // not necessary but windows path separator is '\'
        FString finalPathNormalized = finalPath.Replace(TEXT("/"), FWindowsPlatformMisc::GetDefaultPathSeparator());
    }