DatasmithExportBasicSample.cpp
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
#include "DatasmithExportBasicSample.h"
#include "DatasmithExportHelper.h"
#include "DatasmithExportOptions.h"
#include "DatasmithMesh.h"
#include "DatasmithMeshExporter.h"
#include "DatasmithSceneExporter.h"
#include "DatasmithSceneFactory.h"
#include "GenericPlatform/GenericPlatformFile.h"
#include "HAL/PlatformFilemanager.h"
#include "Misc/Paths.h"
void FDatasmithExportBasicSample::ExportSample()
{
// Set the export options
FDatasmithExportOptions::LightmapUV = EDSExportLightmapUV::Always;
// Create the Datasmith scene
FDatasmithSceneExporter DatasmithSceneExporter;
DatasmithSceneExporter.SetName( TEXT("BasicSample") );
DatasmithSceneExporter.SetOutputPath( *FPaths::Combine( FPaths::ProjectSavedDir(), TEXT("BasicSample") ) );
IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
PlatformFile.CreateDirectory( DatasmithSceneExporter.GetOutputPath() );
TSharedRef< IDatasmithScene > DatasmithScene = FDatasmithSceneFactory::CreateScene( TEXT("BasicSample") );
DatasmithScene->SetHost( TEXT("UE") );
FDatasmithMesh BaseMesh;
FDatasmithExportHelper::CreateSimpleBox( BaseMesh );
// Add LOD
FDatasmithMesh LODMesh;
FDatasmithExportHelper::CreateSimplePyramid(LODMesh);
BaseMesh.AddLOD(LODMesh);
FDatasmithMeshExporter MeshExporter;
TSharedPtr< IDatasmithMeshElement > MeshElement = MeshExporter.ExportToUObject( DatasmithSceneExporter.GetAssetsOutputPath(), TEXT("BoxMesh"), BaseMesh, nullptr, EDSExportLightmapUV::Always );
// Add the mesh to the DatasmithScene
DatasmithScene->AddMesh( MeshElement );
// Create the materials
TSharedPtr< IDatasmithMaterialElement > RedMaterial = FDatasmithSceneFactory::CreateMaterial( TEXT("Red Material") );
TSharedPtr< IDatasmithShaderElement > RedShader = FDatasmithSceneFactory::CreateShader( TEXT("Red Shader") );
RedShader->GetDiffuseComp()->AddSurface( FLinearColor( 0.8f, 0.f, 0.f ) );
RedMaterial->AddShader( RedShader );
DatasmithScene->AddMaterial( RedMaterial );
TSharedPtr< IDatasmithMaterialElement > GreenMaterial = FDatasmithSceneFactory::CreateMaterial( TEXT("Green Material") );
TSharedPtr< IDatasmithShaderElement > GreenShader = FDatasmithSceneFactory::CreateShader( TEXT("Green Shader") );
GreenShader->GetDiffuseComp()->AddSurface( FLinearColor( 0.f, 0.8f, 0.f ) );
GreenMaterial->AddShader( GreenShader );
DatasmithScene->AddMaterial( GreenMaterial );
TSharedPtr< IDatasmithMaterialElement > BlueMaterial = FDatasmithSceneFactory::CreateMaterial( TEXT("Blue Material") );
TSharedPtr< IDatasmithShaderElement > BlueShader = FDatasmithSceneFactory::CreateShader( TEXT("Blue Shader") );
BlueShader->GetDiffuseComp()->AddSurface( FLinearColor( 0.f, 0.f, 0.8f ) );
BlueMaterial->AddShader( BlueShader );
DatasmithScene->AddMaterial( BlueMaterial );
// Create the actor mesh
TSharedPtr< IDatasmithMeshActorElement > MeshActorRed = FDatasmithSceneFactory::CreateMeshActor( TEXT("Sample Actor Red") );
MeshActorRed->SetStaticMeshPathName( TEXT("BoxMesh") ); // Assign the geometry with index 0 (the box) to this actor
MeshActorRed->AddMaterialOverride( TEXT("Red Material"), -1); // Since we want a unique material on our actor we'll use -1 as id
MeshActorRed->SetTranslation( FVector( 0.f, 0.f, 100.f ) );
MeshActorRed->SetScale( FVector::OneVector );
MeshActorRed->SetRotation( FQuat::Identity );
DatasmithScene->AddActor( MeshActorRed );
TSharedPtr< IDatasmithMeshActorElement > MeshActorGreenBlue = FDatasmithSceneFactory::CreateMeshActor( TEXT("Sample Actor Green & Blue") );
MeshActorGreenBlue->SetStaticMeshPathName( TEXT("BoxMesh") ); // Assign the geometry with index 0 (the box) to this actor
MeshActorGreenBlue->AddMaterialOverride( TEXT("Green Material"), 0);
MeshActorGreenBlue->AddMaterialOverride( TEXT("Blue Material"), 1);
MeshActorGreenBlue->SetTranslation( FVector( 0.f, 200.f, 100.f ) );
MeshActorGreenBlue->SetScale( FVector( 0.75f, 0.75f, 0.75f ) );
MeshActorGreenBlue->SetRotation( FRotator( 0.f, 45.f, 0.f ).Quaternion() );
DatasmithScene->AddActor( MeshActorGreenBlue );
// Add metadata to actor
TSharedRef< IDatasmithMetaDataElement > MetaDataElement = FDatasmithSceneFactory::CreateMetaData(TEXT("MetaData For Actor Red"));
MetaDataElement->SetAssociatedElement(MeshActorRed);
TSharedRef< IDatasmithKeyValueProperty > KeyValueProperty = FDatasmithSceneFactory::CreateKeyValueProperty(TEXT("Color"));
KeyValueProperty->SetValue(TEXT("Red"));
KeyValueProperty->SetPropertyType(EDatasmithKeyValuePropertyType::String);
MetaDataElement->AddProperty(KeyValueProperty);
DatasmithScene->AddMetaData(MetaDataElement);
// Create custom type actors
TSharedRef< IDatasmithCustomActorElement > BlueprintActor = FDatasmithSceneFactory::CreateCustomActor( TEXT("Blueprint Actor") );
BlueprintActor->SetClassOrPathName( TEXT("/DatasmithContent/Datasmith/DatasmithArealightMesh") );
BlueprintActor->SetTranslation( FVector( 500.f, 0.f, 100.f ) );
TSharedRef< IDatasmithCustomActorElement > CustomClassActor = FDatasmithSceneFactory::CreateCustomActor( TEXT("Custom Class Actor") );
CustomClassActor->SetClassOrPathName( TEXT("/Script/DatasmithContent.DatasmithAreaLightActor") );
CustomClassActor->SetTranslation( FVector( 500.f, 0.f, 200.f ) );
TSharedRef< IDatasmithKeyValueProperty > IntensityProperty = FDatasmithSceneFactory::CreateKeyValueProperty( TEXT("Intensity") );
IntensityProperty->SetPropertyType( EDatasmithKeyValuePropertyType::Float );
IntensityProperty->SetValue( TEXT("1337.4") );
BlueprintActor->AddProperty( IntensityProperty );
CustomClassActor->AddProperty( IntensityProperty );
TSharedRef< IDatasmithKeyValueProperty > HiddenInGameProperty = FDatasmithSceneFactory::CreateKeyValueProperty( TEXT("bHidden") );
HiddenInGameProperty->SetPropertyType( EDatasmithKeyValuePropertyType::Bool );
HiddenInGameProperty->SetValue( TEXT("true") );
BlueprintActor->AddProperty( HiddenInGameProperty );
CustomClassActor->AddProperty( HiddenInGameProperty );
TSharedRef< IDatasmithKeyValueProperty > ColorProperty = FDatasmithSceneFactory::CreateKeyValueProperty( TEXT("Color") );
ColorProperty->SetPropertyType( EDatasmithKeyValuePropertyType::Color );
ColorProperty->SetValue( *FLinearColor( 1.f, 1.f, 0.f, 0.f ).ToString() );
BlueprintActor->AddProperty( ColorProperty );
CustomClassActor->AddProperty( ColorProperty );
TSharedRef< IDatasmithKeyValueProperty > DimensionsProperty = FDatasmithSceneFactory::CreateKeyValueProperty( TEXT("Dimensions") );
DimensionsProperty->SetPropertyType( EDatasmithKeyValuePropertyType::Vector );
DimensionsProperty->SetValue( *FVector2D( 1000.f, 500.f ).ToString() );
BlueprintActor->AddProperty( DimensionsProperty );
CustomClassActor->AddProperty( DimensionsProperty );
DatasmithScene->AddActor( BlueprintActor );
DatasmithScene->AddActor( CustomClassActor );
// Export
DatasmithSceneExporter.Export( DatasmithScene );
}
FDatasmithMeshExporter::ExportToUObject
TSharedPtr< IDatasmithMeshElement > ExportToUObject(const TCHAR *Filepath, const TCHAR *Filename, FDatasmithMesh &Mesh, FDatasmithMesh *CollisionMesh, EDSExportLightmapUV LightmapUV)
Exports the DatasmithMesh as a UObject.
FDatasmithMeshExporter
Definition: DatasmithMeshExporter.h:12
FDatasmithExportOptions::LightmapUV
static EDSExportLightmapUV LightmapUV
Creation of automatic flatten projection of UVs to be used on lightmaps.
Definition: DatasmithExportOptions.h:35
FDatasmithSceneExporter::Export
void Export(TSharedRef< IDatasmithScene > DatasmithScene, bool bCleanupUnusedElements=true)
Exports the entire scene.
FDatasmithMesh
Definition: DatasmithMesh.h:6
FDatasmithSceneExporter
This is the export for a DatasmithScene.
Definition: DatasmithSceneExporter.h:20
FDatasmithSceneExporter::SetOutputPath
void SetOutputPath(const TCHAR *InOutputPath)
Sets the output path to where this scene will be exporter.
FDatasmithSceneExporter::GetAssetsOutputPath
const TCHAR * GetAssetsOutputPath() const
Gets the path to the assets output folder.
FDatasmithSceneExporter::SetName
void SetName(const TCHAR *InName)
Sets the name of the scene to export.
FDatasmithMesh::AddLOD
void AddLOD(const FDatasmithMesh &InLODMesh)
Adds a LOD mesh to this base LOD mesh.