|
| 1 | +// Copyright (c) 2019 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 | + |
| 7 | +#include "MoveAssetsCommandlet.h" |
| 8 | + |
| 9 | +UMoveAssetsCommandlet::UMoveAssetsCommandlet() |
| 10 | +{ |
| 11 | + IsClient = false; |
| 12 | + IsEditor = true; |
| 13 | + IsServer = false; |
| 14 | + LogToConsole = true; |
| 15 | +} |
| 16 | +#if WITH_EDITORONLY_DATA |
| 17 | + |
| 18 | +// NOTE: Assets imported from a map FBX will be classified for semantic |
| 19 | +// segmentation as ROAD, ROADLINES AND TERRAIN based on the asset name |
| 20 | +// defined in RoadRunner. These tags will be used for moving the meshes |
| 21 | +// and for specifying the path to these meshes when spawning them in a world. |
| 22 | +namespace SSTags { |
| 23 | + // Carla Semantic Segmentation Folder Tags |
| 24 | + static const FString ROAD = TEXT("Roads"); |
| 25 | + static const FString ROADLINES = TEXT("RoadLines"); |
| 26 | + static const FString TERRAIN = TEXT("Terrain"); |
| 27 | + |
| 28 | + // RoadRunner Tags |
| 29 | + static const FString R_ROAD = TEXT("RoadNode"); |
| 30 | + static const FString R_TERRAIN = TEXT("Terrain"); |
| 31 | + static const FString R_MARKING = TEXT("MarkingNode"); |
| 32 | +} |
| 33 | + |
| 34 | +FMovePackageParams UMoveAssetsCommandlet::ParseParams(const FString &InParams) const |
| 35 | +{ |
| 36 | + TArray<FString> Tokens; |
| 37 | + TArray<FString> Params; |
| 38 | + |
| 39 | + ParseCommandLine(*InParams, Tokens, Params); |
| 40 | + |
| 41 | + // Parse and store package name |
| 42 | + FMovePackageParams PackageParams; |
| 43 | + FParse::Value(*InParams, TEXT("PackageName="), PackageParams.Name); |
| 44 | + |
| 45 | + // Parse and store maps name in an array |
| 46 | + FString Maps; |
| 47 | + FParse::Value(*InParams, TEXT("Maps="), Maps); |
| 48 | + |
| 49 | + TArray<FString> MapNames; |
| 50 | + Maps.ParseIntoArray(MapNames, TEXT(" "), true); |
| 51 | + |
| 52 | + PackageParams.MapNames = MapNames; |
| 53 | + |
| 54 | + return PackageParams; |
| 55 | +} |
| 56 | + |
| 57 | +void UMoveAssetsCommandlet::MoveAssets(const FMovePackageParams &PackageParams) |
| 58 | +{ |
| 59 | + // Create a library instance for loading all the assets |
| 60 | + AssetsObjectLibrary = UObjectLibrary::CreateLibrary(UStaticMesh::StaticClass(), false, GIsEditor); |
| 61 | + AssetsObjectLibrary->AddToRoot(); |
| 62 | + |
| 63 | + // Start loading all the assets in the library and classify them for semantic |
| 64 | + // segmentation |
| 65 | + for (const auto &Map : PackageParams.MapNames) |
| 66 | + { |
| 67 | + MoveAssetsFromMapForSemanticSegmentation(PackageParams.Name, Map); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +void MoveFiles(const TArray<UObject *> &Assets, const FString &DestPath) |
| 72 | +{ |
| 73 | + check(DestPath.Len() > 0); |
| 74 | + |
| 75 | + FAssetToolsModule &AssetToolsModule = FModuleManager::LoadModuleChecked<FAssetToolsModule>("AssetTools"); |
| 76 | + TArray<FAssetRenameData> AssetsAndNames; |
| 77 | + for (auto AssetIt = Assets.CreateConstIterator(); AssetIt; ++AssetIt) |
| 78 | + { |
| 79 | + UObject *Asset = *AssetIt; |
| 80 | + |
| 81 | + if (!ensure(Asset)) |
| 82 | + { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + new(AssetsAndNames) FAssetRenameData(Asset, DestPath, Asset->GetName()); |
| 87 | + } |
| 88 | + |
| 89 | + if (AssetsAndNames.Num() > 0) |
| 90 | + { |
| 91 | + AssetToolsModule.Get().RenameAssetsWithDialog(AssetsAndNames); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +void UMoveAssetsCommandlet::MoveAssetsFromMapForSemanticSegmentation( |
| 96 | + const FString &PackageName, |
| 97 | + const FString &MapName) |
| 98 | +{ |
| 99 | + // Prepare a UObjectLibrary for moving assets |
| 100 | + const FString SrcPath = TEXT("/Game/") + PackageName + TEXT("/Maps/") + MapName; |
| 101 | + AssetsObjectLibrary->LoadAssetDataFromPath(*SrcPath); |
| 102 | + AssetsObjectLibrary->LoadAssetsFromAssetData(); |
| 103 | + |
| 104 | + // Load Assets to move |
| 105 | + MapContents.Empty(); |
| 106 | + AssetsObjectLibrary->GetAssetDataList(MapContents); |
| 107 | + AssetsObjectLibrary->ClearLoaded(); |
| 108 | + |
| 109 | + TArray<FString> DestinationPaths = {SSTags::ROAD, SSTags::ROADLINES, SSTags::TERRAIN}; |
| 110 | + |
| 111 | + // Init Map with keys |
| 112 | + TMap<FString, TArray<UObject *>> AssetDataMap; |
| 113 | + for (const auto &Paths : DestinationPaths) |
| 114 | + { |
| 115 | + AssetDataMap.Add(Paths, {}); |
| 116 | + } |
| 117 | + |
| 118 | + for (const auto &MapAsset : MapContents) |
| 119 | + { |
| 120 | + // Get AssetName |
| 121 | + UStaticMesh *MeshAsset = CastChecked<UStaticMesh>(MapAsset.GetAsset()); |
| 122 | + FString ObjectName = MeshAsset->GetName(); |
| 123 | + |
| 124 | + FString AssetName; |
| 125 | + MapAsset.AssetName.ToString(AssetName); |
| 126 | + |
| 127 | + if (SrcPath.Len()) |
| 128 | + { |
| 129 | + |
| 130 | + const FString CurrentPackageName = MeshAsset->GetOutermost()->GetName(); |
| 131 | + |
| 132 | + if (!ensure(CurrentPackageName.StartsWith(SrcPath))) |
| 133 | + { |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + // Bind between tags and classify assets according to semantic |
| 138 | + // segmentation |
| 139 | + if (AssetName.Contains(SSTags::R_ROAD)) |
| 140 | + { |
| 141 | + AssetDataMap[SSTags::ROAD].Add(MeshAsset); |
| 142 | + } |
| 143 | + else if (AssetName.Contains(SSTags::R_MARKING)) |
| 144 | + { |
| 145 | + AssetDataMap[SSTags::ROADLINES].Add(MeshAsset); |
| 146 | + } |
| 147 | + else if (AssetName.Contains(SSTags::R_TERRAIN)) |
| 148 | + { |
| 149 | + AssetDataMap[SSTags::TERRAIN].Add(MeshAsset); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // Move assets to correspoding semantic segmentation folders |
| 155 | + for (const auto &Elem : AssetDataMap) |
| 156 | + { |
| 157 | + FString DestPath = TEXT("/Game/") + PackageName + TEXT("/Static/") + Elem.Key + "/" + MapName; |
| 158 | + MoveFiles(Elem.Value, DestPath); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +int32 UMoveAssetsCommandlet::Main(const FString &Params) |
| 163 | +{ |
| 164 | + FMovePackageParams PackageParams = ParseParams(Params); |
| 165 | + |
| 166 | + MoveAssets(PackageParams); |
| 167 | + |
| 168 | + return 0; |
| 169 | +} |
| 170 | +#endif |
0 commit comments