Add ModelPrefab to imported model prefab for reimporting functionality

This commit is contained in:
Wojtek Figat
2023-12-07 10:25:45 +01:00
parent 74b77bfa4c
commit cb92110976
9 changed files with 179 additions and 36 deletions

View File

@@ -165,20 +165,7 @@ bool CreateAssetContext::AllocateChunk(int32 index)
void CreateAssetContext::AddMeta(JsonWriter& writer) const
{
writer.JKEY("ImportPath");
if (AssetsImportingManager::UseImportPathRelative && !FileSystem::IsRelative(InputPath)
#if PLATFORM_WINDOWS
// Import path from other drive should be stored as absolute on Windows to prevent issues
&& InputPath.Length() > 2 && Globals::ProjectFolder.Length() > 2 && InputPath[0] == Globals::ProjectFolder[0]
#endif
)
{
const String relativePath = FileSystem::ConvertAbsolutePathToRelative(Globals::ProjectFolder, InputPath);
writer.String(relativePath);
}
else
{
writer.String(InputPath);
}
writer.String(AssetsImportingManager::GetImportPath(InputPath));
writer.JKEY("ImportUsername");
writer.String(Platform::GetUserName());
}
@@ -304,6 +291,20 @@ bool AssetsImportingManager::ImportIfEdited(const StringView& inputPath, const S
return false;
}
String AssetsImportingManager::GetImportPath(const String& path)
{
if (UseImportPathRelative && !FileSystem::IsRelative(path)
#if PLATFORM_WINDOWS
// Import path from other drive should be stored as absolute on Windows to prevent issues
&& path.Length() > 2 && Globals::ProjectFolder.Length() > 2 && path[0] == Globals::ProjectFolder[0]
#endif
)
{
return FileSystem::ConvertAbsolutePathToRelative(Globals::ProjectFolder, path);
}
return path;
}
bool AssetsImportingManager::Create(const Function<CreateAssetResult(CreateAssetContext&)>& callback, const StringView& inputPath, const StringView& outputPath, Guid& assetId, void* arg)
{
PROFILE_CPU();

View File

@@ -236,6 +236,9 @@ public:
return ImportIfEdited(inputPath, outputPath, id, arg);
}
// Converts source files path into the relative format if enabled by the project settings. Result path can be stored in asset for reimports.
static String GetImportPath(const String& path);
private:
static bool Create(const CreateAssetFunction& callback, const StringView& inputPath, const StringView& outputPath, Guid& assetId, void* arg);
};

View File

@@ -19,6 +19,7 @@
#include "Engine/Level/Actors/StaticModel.h"
#include "Engine/Level/Prefabs/Prefab.h"
#include "Engine/Level/Prefabs/PrefabManager.h"
#include "Engine/Level/Scripts/ModelPrefab.h"
#include "Engine/Platform/FileSystem.h"
#include "Engine/Utilities/RectPack.h"
#include "Engine/Profiler/ProfilerCPU.h"
@@ -699,7 +700,26 @@ CreateAssetResult ImportModel::CreatePrefab(CreateAssetContext& context, ModelDa
}
}
ASSERT_LOW_LAYER(rootActor);
// TODO: add PrefabModel script for asset reimporting
{
// Add script with import options
auto* modelPrefabScript = New<ModelPrefab>();
modelPrefabScript->SetParent(rootActor);
modelPrefabScript->ImportPath = AssetsImportingManager::GetImportPath(context.InputPath);
modelPrefabScript->ImportOptions = options;
// Link with existing prefab instance
if (prefab)
{
for (const auto& i : prefab->ObjectsCache)
{
if (i.Value->GetTypeHandle() == modelPrefabScript->GetTypeHandle())
{
modelPrefabScript->LinkPrefab(i.Value->GetPrefabID(), i.Value->GetPrefabObjectID());
break;
}
}
}
}
// Create prefab instead of native asset
bool failed;

View File

@@ -16,8 +16,9 @@
#if !BUILD_RELEASE || USE_EDITOR
#include "Engine/Level/Level.h"
#include "Engine/Threading/Threading.h"
#include "Engine/Level/Components/MissingScript.h"
#include "Engine/Level/Scripts/MissingScript.h"
#endif
#include "Engine/Level/Scripts/ModelPrefab.h"
#if USE_EDITOR
@@ -46,6 +47,11 @@ void MissingScript::SetReferenceScript(const ScriptingObjectReference<Script>& v
#endif
ModelPrefab::ModelPrefab(const SpawnParams& params)
: Script(params)
{
}
SceneObjectsFactory::Context::Context(ISerializeModifier* modifier)
: Modifier(modifier)
{

View File

@@ -0,0 +1,29 @@
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Scripting/Script.h"
#if USE_EDITOR
#include "Engine/Tools/ModelTool/ModelTool.h"
#endif
/// <summary>
/// Actor script component that handled model prefabs importing and setup.
/// </summary>
API_CLASS(Attributes="HideInEditor") class FLAXENGINE_API ModelPrefab : public Script
{
API_AUTO_SERIALIZATION();
DECLARE_SCRIPTING_TYPE(ModelPrefab);
#if USE_EDITOR
/// <summary>
/// Source model file path (absolute or relative to the project).
/// </summary>
API_FIELD(Attributes="ReadOnly") String ImportPath;
/// <summary>
/// Model file import settings.
/// </summary>
API_FIELD() ModelTool::Options ImportOptions;
#endif
};