Fixed link errors. Added mesh merging functionality to the widgets. Interface to import USD props.

This commit is contained in:
Axel 2023-03-09 12:34:26 +01:00 committed by bernat
parent cd7ab57622
commit 5b600ed16b
7 changed files with 165 additions and 10 deletions

View File

@ -112,6 +112,10 @@
{
"Name": "CarlaTools",
"Enabled": true
},
{
"Name": "PythonScriptPlugin",
"Enabled": true
}
]
}

View File

@ -0,0 +1,92 @@
#!/usr/bin/env python
"""
Script to add new props to the prop factory and json file
"""
import unreal
import argparse
import json
# convert PropSize to string
def size_to_str(prop_size):
if args.size == unreal.PropSize.TINY:
return 'Tiny'
elif args.size == unreal.PropSize.SMALL:
return 'Small'
elif args.size == unreal.PropSize.MEDIUM:
return 'Medium'
elif args.size == unreal.PropSize.BIG:
return 'Big'
elif args.size == unreal.PropSize.HUGE:
return 'Huge'
else:
return 'Medium'
# convert string to PropSize
def str_to_size(prop_size):
if args.size == "tiny":
return unreal.PropSize.TINY
elif args.size == "small":
return unreal.PropSize.SMALL
elif args.size == "medium":
return unreal.PropSize.MEDIUM
elif args.size == "big":
return unreal.PropSize.BIG
elif args.size == "huge":
return unreal.PropSize.HUGE
else:
return unreal.PropSize.MEDIUM
argparser = argparse.ArgumentParser()
argparser.add_argument(
'-s', '--static_mesh_path',
metavar='S',
default='',
type=str,
help='Path to add to static mesh')
argparser.add_argument(
'-n', '--name',
metavar='N',
default='',
type=str,
help='prop name')
argparser.add_argument(
'--size',
metavar='Z',
default='',
type=str,
help='prop size')
args = argparser.parse_args()
# load prop and static mesh objects
prop_factory_path = '/Game/Carla/Blueprints/Props/PropFactory.PropFactory_C'
prop_factory_class = unreal.load_object(None, prop_factory_path)
prop_factory_default_object = unreal.get_default_object(prop_factory_class)
static_mesh = unreal.load_object(None, args.static_mesh_path)
definitions_map = prop_factory_default_object.get_editor_property("DefinitionsMap")
# generate the new field
new_prop_parameters = unreal.PropParameters()
new_prop_parameters.name = args.name
new_prop_parameters.mesh = static_mesh
new_prop_parameters.size = str_to_size(args.size)
prop_id = 'static.prop.' + args.name
if prop_id in definitions_map:
print("The prop is already present in the DefinitionsMap")
else:
# add new field
definitions_map[prop_id] = new_prop_parameters
unreal.EditorAssetLibrary.save_asset(prop_factory_path, False)
# add prop to Default.Package.json file list
prop_config_file_path = unreal.Paths.project_content_dir() + "Carla/Config/Default.Package.json"
json_file = open(prop_config_file_path, 'r')
config_json = json.load(json_file)
json_file.close()
prop_list = config_json['props']
prop_list.append(
{'name' : new_prop_parameters.name,
'path' : args.static_mesh_path,
'size' : size_to_str(new_prop_parameters.size)})
json_file = open(prop_config_file_path, 'w')
json_file.write(json.dumps(config_json, indent = 4, sort_keys=False))
json_file.close()

View File

@ -66,6 +66,7 @@ public class CarlaTools : ModuleRules
"Landscape",
"Foliage",
"FoliageEdit",
"MeshMergeUtilities",
"Carla",
"PhysXVehicles",
"Json",
@ -118,7 +119,10 @@ public class CarlaTools : ModuleRules
private void AddBoostLibs(string LibPath)
{
string [] files = Directory.GetFiles(LibPath, "*boost*.lib");
foreach (string file in files) PublicAdditionalLibraries.Add(file);
foreach (string file in files)
{
PublicAdditionalLibraries.Add(file);
}
}

View File

@ -1,21 +1,68 @@
// Copyright (c) 2023 Computer Vision Center (CVC) at the Universitat Autonoma de Barcelona (UAB). This work is licensed under the terms of the MIT license. For a copy, see <https://opensource.org/licenses/MIT>.
#include "USDImporterWidget.h"
#include "OmniverseUSDImporter.h"
#include "USDAlternateImporter.h"
#include "Kismet/GameplayStatics.h"
#include "Modules/ModuleManager.h"
#include "IMeshMergeUtilities.h"
#include "MeshMergeModule.h"
#include "Components/PrimitiveComponent.h"
void UUSDImporterWidget::ImportUSDProp(
const FString& USDPath, const FString& DestinationAssetPath)
const FString& USDPath, const FString& DestinationAssetPath, bool bAsBlueprint)
{
FOmniverseImportSettings Settings;
Settings.bImportUnusedReferences = false;
Settings.bImportAsBlueprint = true;
FOmniverseUSDImporter::LoadUSD(USDPath, DestinationAssetPath, Settings);
FUSDAlternateImporter::ImportUSD(USDPath, DestinationAssetPath, false, bAsBlueprint);
}
void UUSDImporterWidget::ImportUSDVehicle(
const FString& USDPath, const FString& DestinationAssetPath)
const FString& USDPath, const FString& DestinationAssetPath, bool bAsBlueprint)
{
}
AActor* UUSDImporterWidget::GetGeneratedBlueprint(UWorld* World, const FString& USDPath)
{
TArray<AActor*> Actors;
UGameplayStatics::GetAllActorsOfClass(World, AActor::StaticClass(), Actors);
FString USDFileName = FPaths::GetBaseFilename(USDPath, true);
UE_LOG(LogCarlaTools, Log, TEXT("Searching for name %s"), *USDFileName);
for (AActor* Actor : Actors)
{
if(Actor->GetName().Contains(USDFileName))
{
return Actor;
}
}
return nullptr;
}
bool UUSDImporterWidget::MergeStaticMeshComponents(
TArray<AActor*> Actors, const FString& DestMesh)
{
if (Actors.Num() == 0)
{
UE_LOG(LogCarlaTools, Error, TEXT("No actors for merge"));
return false;
}
UWorld* World = Actors[0]->GetWorld();
const IMeshMergeUtilities& MeshUtilities = FModuleManager::Get().LoadModuleChecked<IMeshMergeModule>("MeshMergeUtilities").GetUtilities();
TArray<UPrimitiveComponent*> ComponentsToMerge;
for(AActor* Actor : Actors)
{
TArray<UPrimitiveComponent*> ActorComponents;
Actor->GetComponents(ActorComponents, false);
ComponentsToMerge.Append(ActorComponents);
}
FMeshMergingSettings MeshMergeSettings;
TArray<UObject*> AssetsToSync;
const float ScreenAreaSize = TNumericLimits<float>::Max();
FVector NewLocation;
MeshUtilities.MergeComponentsToStaticMesh(ComponentsToMerge, World, MeshMergeSettings, nullptr, nullptr, DestMesh, AssetsToSync, NewLocation, ScreenAreaSize, true);
return true;
}
FString UUSDImporterWidget::GetFileName(FString& FullPath)
{
return FPaths::GetBaseFilename(FullPath, true);
}

View File

@ -14,6 +14,14 @@ class CARLATOOLS_API UUSDImporterWidget : public UUserWidget
public:
void ImportUSDProp(const FString& USDPath, const FString& DestinationAssetPath);
void ImportUSDVehicle(const FString& USDPath, const FString& DestinationAssetPath);
UFUNCTION(BlueprintCallable, Category="USD Importer")
void ImportUSDProp(const FString& USDPath, const FString& DestinationAssetPath, bool bAsBlueprint = true);
UFUNCTION(BlueprintCallable, Category="USD Importer")
void ImportUSDVehicle(const FString& USDPath, const FString& DestinationAssetPath, bool bAsBlueprint = true);
UFUNCTION(BlueprintCallable, Category="USD Importer")
static AActor* GetGeneratedBlueprint(UWorld* World, const FString& USDPath);
UFUNCTION(BlueprintCallable, Category="USD Importer")
static bool MergeStaticMeshComponents(TArray<AActor*> Actors, const FString& DestMesh);
UFUNCTION(BlueprintPure, Category="USD Importer")
static FString GetFileName(const FString& FullPath);
};