SceneSerializationHandler.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <SceneBuilder/SceneSerializationHandler.h>
  9. #include <AssetBuilderSDK/AssetBuilderSDK.h>
  10. #include <AzCore/IO/SystemFile.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/Serialization/EditContextConstants.inl>
  13. #include <AzCore/std/algorithm.h>
  14. #include <AzCore/std/string/conversions.h>
  15. #include <AzFramework/StringFunc/StringFunc.h>
  16. #include <AzToolsFramework/API/EditorAssetSystemAPI.h>
  17. #include <AzToolsFramework/Debug/TraceContext.h>
  18. #include <SceneAPI/SceneCore/Events/AssetImportRequest.h>
  19. #include <SceneAPI/SceneCore/Utilities/Reporting.h>
  20. #include <SceneAPI/SceneCore/Components/LoadingComponent.h>
  21. namespace SceneBuilder
  22. {
  23. void SceneSerializationHandler::Activate()
  24. {
  25. BusConnect();
  26. }
  27. void SceneSerializationHandler::Deactivate()
  28. {
  29. BusDisconnect();
  30. }
  31. void SceneSerializationHandler::Reflect(AZ::ReflectContext* context)
  32. {
  33. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  34. if (serializeContext)
  35. {
  36. serializeContext->Class<SceneSerializationHandler, AZ::Component>()->Version(1)
  37. ->Attribute(AZ::Edit::Attributes::SystemComponentTags, AZStd::vector<AZ::Crc32>({ AssetBuilderSDK::ComponentTags::AssetBuilder }))
  38. ;
  39. }
  40. }
  41. AZStd::shared_ptr<AZ::SceneAPI::Containers::Scene> SceneSerializationHandler::LoadScene(
  42. const AZStd::string& filePath,
  43. AZ::Uuid sceneSourceGuid,
  44. const AZStd::string& watchFolder
  45. )
  46. {
  47. namespace Utilities = AZ::SceneAPI::Utilities;
  48. using AZ::SceneAPI::Events::AssetImportRequest;
  49. AZ_TraceContext("File", filePath);
  50. if (sceneSourceGuid.IsNull())
  51. {
  52. AZ_TracePrintf(Utilities::ErrorWindow, "Invalid source guid for the scene file.");
  53. return nullptr;
  54. }
  55. if (AZ::SceneAPI::Events::AssetImportRequest::IsManifestExtension(filePath.c_str()))
  56. {
  57. AZ_TracePrintf(Utilities::ErrorWindow, "Provided path contains the manifest path, not the path to the source file.");
  58. return nullptr;
  59. }
  60. if (!AZ::SceneAPI::Events::AssetImportRequest::IsSceneFileExtension(filePath.c_str()))
  61. {
  62. AZ_TracePrintf(Utilities::ErrorWindow, "Provided path doesn't contain an extension supported by the SceneAPI.");
  63. return nullptr;
  64. }
  65. if (AzFramework::StringFunc::Path::IsRelative(filePath.c_str()))
  66. {
  67. AZ_TracePrintf(Utilities::ErrorWindow, "Given file path is relative where an absolute path was expected.");
  68. return nullptr;
  69. }
  70. if (!AZ::IO::SystemFile::Exists(filePath.c_str()))
  71. {
  72. AZ_TracePrintf(Utilities::ErrorWindow, "No file exists at given source path.");
  73. return nullptr;
  74. }
  75. AZStd::shared_ptr<AZ::SceneAPI::Containers::Scene> scene = AssetImportRequest::LoadSceneFromVerifiedPath(
  76. filePath, sceneSourceGuid, AssetImportRequest::RequestingApplication::AssetProcessor,
  77. AZ::SceneAPI::SceneCore::LoadingComponent::TYPEINFO_Uuid(), watchFolder);
  78. if (!scene)
  79. {
  80. AZ_TracePrintf(Utilities::ErrorWindow, "Failed to load the requested scene.");
  81. return nullptr;
  82. }
  83. return scene;
  84. }
  85. } // namespace SceneBuilder