Agrarsense
EditorUtilities.cpp
Go to the documentation of this file.
1// Copyright (c) 2023 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
6#include "EditorUtilities.h"
7
8#if WITH_EDITOR
9#include "Editor/UnrealEdEngine.h"
10#include "Editor.h"
11#include "AssetToolsModule.h"
12#include "ContentBrowserModule.h"
13#include "IContentBrowserSingleton.h"
14#include "ObjectTools.h"
15#include "UObject/Package.h"
16#include "TextureResource.h"
17#include "Engine/Texture2D.h"
18#include "AssetRegistry/AssetRegistryModule.h"
19#include "AssetRegistry/AssetData.h"
20#include "Modules/ModuleManager.h"
21#include "UObject/SavePackage.h"
22#include "HAL/UnrealMemory.h"
23#endif
24
26{
27#if WITH_EDITOR
28 // Access the selected assets in the Content Browser
29 TArray<FAssetData> SelectedAssets;
30 FContentBrowserModule& ContentBrowserModule = FModuleManager::LoadModuleChecked<FContentBrowserModule>(TEXT("ContentBrowser"));
31 IContentBrowserSingleton& ContentBrowserSingleton = ContentBrowserModule.Get();
32 ContentBrowserSingleton.Get().GetSelectedAssets(SelectedAssets);
33
34 for (const FAssetData& Asset : SelectedAssets)
35 {
37 }
38
39 UE_LOG(LogTemp, Warning, TEXT("EditorUtilities.cpp: Export complete."));
40#endif
41}
42
44{
45#if WITH_EDITOR
46 int32 pathSeparatorIdx;
47 FAssetData obj = Asset;
48 FString GamePath = obj.GetAsset()->GetPathName();
49 FString AssetName;
50 int32 pathEnd;
51 if (GamePath.FindLastChar('/', pathEnd))
52 {
53 ++pathEnd;
54 AssetName += GamePath;
55 AssetName.RightChopInline(pathEnd);
56 int32 extensionIdx;
57 if (AssetName.FindChar('.', extensionIdx))
58 {
59 AssetName.LeftInline(extensionIdx);
60 GamePath.LeftInline(pathEnd);
61 FString Suffix = "T_";
62 FString NameWithSuffix = Suffix + AssetName;
63 AssetName = NameWithSuffix;
64 }
65 else
66 {
67 AssetName = "T_Thumbnail";
68 }
69
70 if (AssetName.FindChar('/', pathSeparatorIdx))
71 {
72 // TextureName should not have any path separators in it
73 return;
74 }
75
76 FObjectThumbnail* thumb = ThumbnailTools::GenerateThumbnailForObjectToSaveToDisk(obj.GetAsset());
77 if (!thumb)
78 {
79 return;
80 }
81
82 FString PackageName = TEXT("/Game/Agrarsense/Misc/ExportedThumbnailTextures/");
83 if (!PackageName.EndsWith("/"))
84 {
85 PackageName += "/";
86 }
87 PackageName += AssetName;
88
89 UPackage* Package = CreatePackage(*PackageName);
90 Package->FullyLoad();
91
92 UTexture2D* NewTexture = NewObject<UTexture2D>(Package, *AssetName, RF_Public | RF_Standalone | RF_MarkAsRootSet);
93 NewTexture->AddToRoot();
94 FTexturePlatformData* platformData = new FTexturePlatformData();
95 platformData->SizeX = thumb->GetImageWidth();
96 platformData->SizeY = thumb->GetImageHeight();
97 //platformData->NumSlices = 1;
98 platformData->PixelFormat = EPixelFormat::PF_B8G8R8A8;
99 NewTexture->SetPlatformData(platformData);
100
101 FTexture2DMipMap* Mip = new FTexture2DMipMap();
102 platformData->Mips.Add(Mip);
103 Mip->SizeX = thumb->GetImageWidth();
104 Mip->SizeY = thumb->GetImageHeight();
105
106 Mip->BulkData.Lock(LOCK_READ_WRITE);
107 uint8* TextureData = (uint8*)Mip->BulkData.Realloc(thumb->GetUncompressedImageData().Num() * 4);
108 FMemory::Memcpy(TextureData, thumb->GetUncompressedImageData().GetData(), thumb->GetUncompressedImageData().Num());
109 Mip->BulkData.Unlock();
110
111 NewTexture->Source.Init(thumb->GetImageWidth(), thumb->GetImageHeight(), 1, 1, ETextureSourceFormat::TSF_BGRA8, thumb->GetUncompressedImageData().GetData());
112 NewTexture->LODGroup = TEXTUREGROUP_UI;
113 NewTexture->UpdateResource();
114 Package->MarkPackageDirty();
115 Package->FullyLoad();
116 FAssetRegistryModule::AssetCreated(NewTexture);
117
118 FSavePackageArgs SaveArgs;
119 SaveArgs.TopLevelFlags = EObjectFlags::RF_Public | EObjectFlags::RF_Standalone;
120 SaveArgs.SaveFlags = SAVE_NoError;
121 SaveArgs.bForceByteSwapping = true;
122 FString PackageFileName = FPackageName::LongPackageNameToFilename(PackageName, FPackageName::GetAssetPackageExtension());
123 UPackage::SavePackage(Package, NewTexture, *PackageFileName, SaveArgs);
124 }
125#endif
126}
127
128#include "Components/BoxComponent.h"
129#if WITH_EDITOR
130#include "Editor.h"
131#include "EditorLevelUtils.h"
132#include "ScopedTransaction.h"
133#include "EngineUtils.h"
134#include "Engine/Selection.h"
135#include "Components/StaticMeshComponent.h"
136#include "Landscape.h"
137#include "LandscapeProxy.h"
138#endif
139
141{
142#if WITH_EDITOR
143 if (!Area || !Area->GetWorld() || !Area->GetWorld()->IsEditorWorld())
144 {
145 return;
146 }
147
148 // Get the box world-space bounds
149 const FTransform BoxTransform = Area->GetComponentTransform();
150 const FVector BoxExtent = Area->GetScaledBoxExtent();
151 const FVector BoxOrigin = BoxTransform.GetLocation();
152 const FBox BoxBounds = FBox::BuildAABB(BoxOrigin, BoxExtent);
153
154 const FScopedTransaction Transaction(NSLOCTEXT("UnrealEd", "DeleteOverlappingActors", "Delete Overlapping Actors"));
155
156 for (TActorIterator<AActor> It(Area->GetWorld()); It; ++It)
157 {
158 AActor* Actor = *It;
159
160 if (!Actor || Actor == Area->GetOwner())
161 {
162 continue;
163 }
164
165 // Skip if landscape
166 if (Actor->IsA<ALandscape>() || Actor->IsA<ALandscapeProxy>())
167 {
168 continue;
169 }
170
171 // Skip if no StaticMeshComponent
172 bool bHasStaticMesh = false;
173 TArray<UStaticMeshComponent*> StaticMeshComponents;
174 Actor->GetComponents<UStaticMeshComponent>(StaticMeshComponents);
175 for (UStaticMeshComponent* Comp : StaticMeshComponents)
176 {
177 if (Comp && Comp->GetStaticMesh())
178 {
179 bHasStaticMesh = true;
180 break;
181 }
182 }
183
184 if (!bHasStaticMesh)
185 {
186 continue;
187 }
188
189 const FBox ActorBounds = Actor->GetComponentsBoundingBox(true);
190 if (BoxBounds.Intersect(ActorBounds))
191 {
192 Actor->Modify(); // For undo
193 GEditor->GetEditorWorldContext().World()->DestroyActor(Actor, true, false);
194 }
195 }
196#endif
197}
static void DestroyOverlappingActorsWithMeshEditor(UBoxComponent *Area)
static void CreateThumbnailTextureFromAsset(const FAssetData &Asset)
static void ExportThumbnailTextureFromSelectedAssets()