2021-06-02 15:58:00 -04:00
// Copyright Epic Games, Inc. All Rights Reserved.
# include "ModelingObjectsCreationAPI.h"
# include "InteractiveToolManager.h"
# include "ContextObjectStore.h"
# include "Misc/Paths.h"
# include "Components/StaticMeshComponent.h"
# include "Engine/StaticMesh.h"
2023-03-07 12:04:54 -05:00
# include "ModelingComponentsSettings.h"
2022-11-30 10:38:46 -05:00
# include "DynamicMesh/NonManifoldMappingSupport.h"
2021-06-02 15:58:00 -04:00
2022-09-28 01:06:15 -04:00
# include UE_INLINE_GENERATED_CPP_BY_NAME(ModelingObjectsCreationAPI)
2023-03-07 12:04:54 -05:00
// if set to 1, then we do not default-initialize our new mesh object parameters based on the Modeling Components Settings (the Modeling Tools' Project Settings)
static TAutoConsoleVariable < bool > CVarConstructMeshObjectsWithoutModelingComponentSettings (
TEXT ( " modeling.CreateMesh.IgnoreProjectSettings " ) ,
false ,
TEXT ( " If enabled, do not use the preferences set in Modeling Tools' Project Settings when constructing new mesh objects " ) ) ;
2021-06-02 15:58:00 -04:00
2023-03-07 12:04:54 -05:00
FCreateMeshObjectParams : : FCreateMeshObjectParams ( bool bConstructWithDefaultModelingComponentSettings )
{
if ( bConstructWithDefaultModelingComponentSettings & &
! CVarConstructMeshObjectsWithoutModelingComponentSettings . GetValueOnGameThread ( ) )
{
UModelingComponentsSettings : : ApplyDefaultsToCreateMeshObjectParams ( * this ) ;
}
}
2021-06-02 15:58:00 -04:00
void FCreateMeshObjectParams : : SetMesh ( FMeshDescription & & MeshDescriptionIn )
{
MeshDescription = MoveTemp ( MeshDescriptionIn ) ;
MeshType = ECreateMeshObjectSourceMeshType : : MeshDescription ;
}
void FCreateMeshObjectParams : : SetMesh ( const UE : : Geometry : : FDynamicMesh3 * DynamicMeshIn )
{
DynamicMesh = * DynamicMeshIn ;
MeshType = ECreateMeshObjectSourceMeshType : : DynamicMesh ;
2022-11-30 10:38:46 -05:00
if ( DynamicMesh )
{
UE : : Geometry : : FNonManifoldMappingSupport : : RemoveAllNonManifoldMappingData ( * DynamicMesh ) ;
}
2021-06-02 15:58:00 -04:00
}
void FCreateMeshObjectParams : : SetMesh ( UE : : Geometry : : FDynamicMesh3 & & DynamicMeshIn )
{
DynamicMesh = MoveTemp ( DynamicMeshIn ) ;
MeshType = ECreateMeshObjectSourceMeshType : : DynamicMesh ;
2022-11-30 10:38:46 -05:00
if ( DynamicMesh )
{
UE : : Geometry : : FNonManifoldMappingSupport : : RemoveAllNonManifoldMappingData ( * DynamicMesh ) ;
}
2021-06-02 15:58:00 -04:00
}
FCreateMeshObjectResult UE : : Modeling : : CreateMeshObject ( UInteractiveToolManager * ToolManager , FCreateMeshObjectParams & & CreateMeshParams )
{
if ( ensure ( ToolManager ) )
{
UModelingObjectsCreationAPI * UseAPI = ToolManager - > GetContextObjectStore ( ) - > FindContext < UModelingObjectsCreationAPI > ( ) ;
if ( UseAPI )
{
if ( UseAPI - > HasMoveVariants ( ) )
{
return UseAPI - > CreateMeshObject ( MoveTemp ( CreateMeshParams ) ) ;
}
else
{
return UseAPI - > CreateMeshObject ( CreateMeshParams ) ;
}
}
}
return FCreateMeshObjectResult { ECreateModelingObjectResult : : Failed_NoAPIFound } ;
}
FCreateTextureObjectResult UE : : Modeling : : CreateTextureObject ( UInteractiveToolManager * ToolManager , FCreateTextureObjectParams & & CreateTexParams )
{
if ( ensure ( ToolManager ) )
{
UModelingObjectsCreationAPI * UseAPI = ToolManager - > GetContextObjectStore ( ) - > FindContext < UModelingObjectsCreationAPI > ( ) ;
if ( UseAPI )
{
if ( UseAPI - > HasMoveVariants ( ) )
{
return UseAPI - > CreateTextureObject ( MoveTemp ( CreateTexParams ) ) ;
}
else
{
return UseAPI - > CreateTextureObject ( CreateTexParams ) ;
}
}
}
return FCreateTextureObjectResult { ECreateModelingObjectResult : : Failed_NoAPIFound } ;
}
2023-05-19 13:50:08 -04:00
FCreateMaterialObjectResult UE : : Modeling : : CreateMaterialObject ( UInteractiveToolManager * ToolManager , FCreateMaterialObjectParams & & CreateMaterialParams )
{
if ( ensure ( ToolManager ) )
{
UModelingObjectsCreationAPI * UseAPI = ToolManager - > GetContextObjectStore ( ) - > FindContext < UModelingObjectsCreationAPI > ( ) ;
if ( UseAPI )
{
if ( UseAPI - > HasMoveVariants ( ) )
{
return UseAPI - > CreateMaterialObject ( MoveTemp ( CreateMaterialParams ) ) ;
}
else
{
return UseAPI - > CreateMaterialObject ( CreateMaterialParams ) ;
}
}
}
return FCreateMaterialObjectResult { ECreateModelingObjectResult : : Failed_NoAPIFound } ;
}
2023-06-13 17:05:45 -04:00
FCreateActorResult UE : : Modeling : : CreateNewActor ( UInteractiveToolManager * ToolManager , FCreateActorParams & & CreateActorParams )
{
if ( ensure ( ToolManager ) )
{
UModelingObjectsCreationAPI * UseAPI = ToolManager - > GetContextObjectStore ( ) - > FindContext < UModelingObjectsCreationAPI > ( ) ;
if ( UseAPI )
{
if ( UseAPI - > HasMoveVariants ( ) )
{
return UseAPI - > CreateNewActor ( MoveTemp ( CreateActorParams ) ) ;
}
else
{
return UseAPI - > CreateNewActor ( CreateActorParams ) ;
}
}
}
return FCreateActorResult { ECreateModelingObjectResult : : Failed_NoAPIFound } ;
}
2023-05-19 13:50:08 -04:00
2021-06-02 15:58:00 -04:00
FString UE : : Modeling : : GetComponentAssetBaseName ( UPrimitiveComponent * Component , bool bRemoveAutoGeneratedSuffixes )
{
if ( ! ensure ( Component ! = nullptr ) )
{
return TEXT ( " InvalidComponent " ) ;
}
2021-08-17 09:50:06 -04:00
// default to the Actor Name (or Label in Editor) if this is the unique root component, otherwise use component name
FString ResultName = ( Component - > GetOwner ( ) - > GetRootComponent ( ) = = Component ) ?
Component - > GetOwner ( ) - > GetActorNameOrLabel ( ) : Component - > GetName ( ) ;
2021-06-02 15:58:00 -04:00
if ( bRemoveAutoGeneratedSuffixes )
{
ResultName = UE : : Modeling : : StripGeneratedAssetSuffixFromName ( ResultName ) ;
}
2021-08-17 09:50:06 -04:00
// If this is a static mesh component, get the asset name instead.
2021-06-02 15:58:00 -04:00
UStaticMeshComponent * StaticMeshComponent = Cast < UStaticMeshComponent > ( Component ) ;
if ( StaticMeshComponent ! = nullptr )
{
UStaticMesh * SourceMesh = StaticMeshComponent - > GetStaticMesh ( ) ;
if ( SourceMesh )
{
FString AssetName = FPaths : : GetBaseFilename ( SourceMesh - > GetName ( ) ) ;
ResultName = ( bRemoveAutoGeneratedSuffixes ) ? UE : : Modeling : : StripGeneratedAssetSuffixFromName ( AssetName ) : AssetName ;
}
}
return ResultName ;
}
FString UE : : Modeling : : StripGeneratedAssetSuffixFromName ( FString InputName )
{
// find final '_'
int32 Index ;
if ( ! InputName . FindLastChar ( ' _ ' , Index ) )
{
return InputName ;
}
// check that remaining characters are hex digits (from UUID)
int32 Len = InputName . Len ( ) ;
int32 Count = 0 , Letters = 0 , Numbers = 0 ;
for ( int32 k = Index + 1 ; k < Len ; + + k )
{
if ( FChar : : IsHexDigit ( InputName [ k ] ) = = false )
{
return InputName ;
}
Count + + ;
if ( FChar : : IsDigit ( InputName [ k ] ) )
{
Numbers + + ;
}
else
{
Letters + + ;
}
}
// currently assuming appended UUID is at least 8 characters
if ( Numbers = = 0 | | Letters = = 0 | | Count < 8 )
{
return InputName ;
}
return InputName . Left ( Index ) ;
}
2021-10-07 22:25:54 -04:00
FString UE : : Modeling : : GenerateRandomShortHexString ( int32 NumChars )
{
int32 FailCount = 0 ;
while ( FailCount + + < 10 )
{
FGuid Guid = FGuid : : NewGuid ( ) ;
FString GuidString = Guid . ToString ( EGuidFormats : : UniqueObjectGuid ) . ToUpper ( ) ;
FString Result ;
int32 Digits = 0 , Letters = 0 ;
for ( int32 k = 0 ; k < GuidString . Len ( ) ; + + k )
{
TCHAR Character = GuidString [ k ] ;
if ( FChar : : IsHexDigit ( Character ) )
{
Result . AppendChar ( Character ) ;
if ( FChar : : IsDigit ( Character ) )
{
Digits + + ;
}
else
{
Letters + + ;
}
}
if ( Result . Len ( ) = = NumChars )
{
if ( Digits > 0 & & Letters > 0 )
{
return Result ;
}
break ; // exit loop
}
}
}
return TEXT ( " BADGUID1 " ) ;
2022-09-28 01:06:15 -04:00
}