CARLA
Loading...
Searching...
No Matches
PostProcessJsonUtils.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
8
9#include "Components/PostProcessComponent.h"
10#include "Components/SceneCaptureComponent2D.h"
11#include "HAL/FileManager.h"
12#include "HAL/IConsoleManager.h"
13#include "JsonObjectConverter.h"
14#include "Misc/FileHelper.h"
15#include "Misc/Paths.h"
16
17static TAutoConsoleVariable<FString> CVarCarlaPostProcessProfile(
18 TEXT("carla.PostProcess.Profile"),
19 TEXT("AutomotiveHDR"),
20 TEXT("Camera profile (Content/Carla/Config/PostProcess/<name>.json) loaded by RGB sensors ")
21 TEXT("spawned without post_process_profile (or with \"Default\")."),
22 ECVF_Default);
23
24static TAutoConsoleVariable<FString> CVarCarlaPostProcessViewportProfile(
25 TEXT("carla.PostProcess.ViewportProfile"),
26 TEXT("Cinematic"),
27 TEXT("Camera profile the sky rig's post process loads at BeginPlay: the PIE viewport, ")
28 TEXT("the spectator and the server window. Set it to the sensor profile to see what the ")
29 TEXT("RGB sensors see."),
30 ECVF_Default);
31
32static FString ProfileOrFallback(const TAutoConsoleVariable<FString>& CVar, const TCHAR* Fallback)
33{
34 const FString Name = CVar.GetValueOnGameThread();
35 return Name.IsEmpty() ? FString(Fallback) : Name;
36}
37
42
47
48FString UPostProcessJsonUtils::ResolveProfileName(const FString& Requested)
49{
50 // There is no Default.json any more: "Default" in any case (scripts written
51 // when it was a file) means the sensor default, like an empty name.
52 if (Requested.IsEmpty() || Requested.Equals(TEXT("Default"), ESearchCase::IgnoreCase))
53 return GetActiveProfileName();
54 return Requested;
55}
56
57bool UPostProcessJsonUtils::SaveAllPostProcessToJson(APostProcessVolume* Volume, const FString& FileName)
58{
60 Wrapper.Settings = Volume->Settings;
61
62 FString OutputString;
63 if (FJsonObjectConverter::UStructToJsonObjectString(Wrapper, OutputString))
64 {
65 FString FullPath = GetPostProcessConfigPath(FileName);
66 IFileManager::Get().MakeDirectory(*FPaths::GetPath(FullPath), true);
67 return FFileHelper::SaveStringToFile(OutputString, *FullPath);
68 }
69
70 return false;
71}
72
73bool UPostProcessJsonUtils::SaveAllPostProcessComponentToJson(UPostProcessComponent* Volume, const FString& FileName)
74{
76 Wrapper.Settings = Volume->Settings;
77
78 FString OutputString;
79 if (FJsonObjectConverter::UStructToJsonObjectString(Wrapper, OutputString))
80 {
81 FString FullPath = GetPostProcessConfigPath(FileName);
82 // Ensure the directory exists before saving
83 FString DirectoryPath = FPaths::GetPath(FullPath);
84 if (!IFileManager::Get().DirectoryExists(*DirectoryPath))
85 {
86 IFileManager::Get().MakeDirectory(*DirectoryPath, true);
87 }
88 return FFileHelper::SaveStringToFile(OutputString, *FullPath);
89 }
90
91 return false;
92}
93
94bool UPostProcessJsonUtils::LoadAllPostProcessFromJsonToPostProcessComponent(UPostProcessComponent* Volume, const FString& FileName)
95{
96 FString FullPath = GetPostProcessConfigPath(FileName);
97 FString InputString;
98
99 if (FFileHelper::LoadFileToString(InputString, *FullPath))
100 {
102 if (FJsonObjectConverter::JsonObjectStringToUStruct(InputString, &Wrapper, 0, 0))
103 {
104 Volume->Settings = Wrapper.Settings;
105 return true;
106 }
107 }
108 return false;
109}
110
111bool UPostProcessJsonUtils::LoadAllPostProcessFromJsonToPostProcessVolume(APostProcessVolume* Volume, const FString& FileName)
112{
113 FString FullPath = GetPostProcessConfigPath(FileName);
114 FString InputString;
115
116 if (FFileHelper::LoadFileToString(InputString, *FullPath))
117 {
119 if (FJsonObjectConverter::JsonObjectStringToUStruct(InputString, &Wrapper, 0, 0))
120 {
121 Volume->Settings = Wrapper.Settings;
122 return true;
123 }
124 }
125 return false;
126}
127
128
130{
131 FString FullPath = GetPostProcessConfigPath(FileName);
132 FString InputString;
133
134 if (FFileHelper::LoadFileToString(InputString, *FullPath))
135 {
137 if (FJsonObjectConverter::JsonObjectStringToUStruct(InputString, &Wrapper, 0, 0))
138 {
139 SensorCamera->PostProcessSettings = Wrapper.Settings;
140 return true;
141 }
142 }
143 return false;
144}
145
147{
148 TArray<FString> ProfileNames;
149 const FString ConfigDir = FPaths::ProjectContentDir() / TEXT("Carla/Config/PostProcess/");
150 IFileManager::Get().FindFiles(ProfileNames, *(ConfigDir / TEXT("*.json")), true, false);
151 for (FString& Name : ProfileNames)
152 {
153 Name = FPaths::GetBaseFilename(Name);
154 }
155 ProfileNames.Sort();
156 return ProfileNames;
157}
static TAutoConsoleVariable< FString > CVarCarlaPostProcessViewportProfile(TEXT("carla.PostProcess.ViewportProfile"), TEXT("Cinematic"), TEXT("RGB sensors see."), ECVF_Default)
static FString ProfileOrFallback(const TAutoConsoleVariable< FString > &CVar, const TCHAR *Fallback)
static TAutoConsoleVariable< FString > CVarCarlaPostProcessProfile(TEXT("carla.PostProcess.Profile"), TEXT("AutomotiveHDR"), TEXT("spawned without post_process_profile (or with \"Default\")."), ECVF_Default)
static FString ResolveProfileName(const FString &Requested)
static bool LoadAllPostProcessFromJsonToPostProcessComponent(UPostProcessComponent *Volume, const FString &FileName)
static bool SaveAllPostProcessComponentToJson(UPostProcessComponent *Volume, const FString &FileName)
static bool LoadAllPostProcessFromJsonToPostProcessVolume(APostProcessVolume *Volume, const FString &FileName)
static bool LoadAllPostProcessFromJsonToSceneCapture(USceneCaptureComponent2D *SensorCamera, const FString &FileName)
static FString GetPostProcessConfigPath(const FString &FileName)
static FString GetActiveProfileName()
static bool SaveAllPostProcessToJson(APostProcessVolume *Volume, const FString &FileName)
static TArray< FString > GetAvailablePostProcessProfileNames()
static FString GetViewportProfileName()