BehaviorsSkeletonGroup.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <AzCore/Serialization/SerializeContext.h>
  9. #include <AzCore/std/algorithm.h>
  10. #include <AzCore/std/smart_ptr/make_shared.h>
  11. #include <SceneAPI/SceneCore/Containers/Scene.h>
  12. #include <SceneAPI/SceneCore/Containers/SceneGraph.h>
  13. #include <SceneAPI/SceneCore/Containers/Views/SceneGraphDownwardsIterator.h>
  14. #include <SceneAPI/SceneCore/Containers/Views/PairIterator.h>
  15. #include <SceneAPI/SceneCore/Containers/Utilities/Filters.h>
  16. #include <SceneAPI/SceneCore/Containers/Utilities/SceneGraphUtilities.h>
  17. #include <SceneAPI/SceneCore/DataTypes/GraphData/IBoneData.h>
  18. #include <SceneAPI/SceneCore/DataTypes/DataTypeUtilities.h>
  19. #include <SceneAPI/SceneCore/Utilities/SceneGraphSelector.h>
  20. #include <SceneAPI/SceneData/Groups/SkeletonGroup.h>
  21. #include <SceneAPI/SceneData/Behaviors/SkeletonGroup.h>
  22. #include <SceneAPI/SceneData/GraphData/RootBoneData.h>
  23. namespace AZ
  24. {
  25. namespace SceneAPI
  26. {
  27. namespace Behaviors
  28. {
  29. void SkeletonGroup::Activate()
  30. {
  31. }
  32. void SkeletonGroup::Deactivate()
  33. {
  34. }
  35. void SkeletonGroup::Reflect(ReflectContext* context)
  36. {
  37. SerializeContext* serializeContext = azrtti_cast<SerializeContext*>(context);
  38. if (serializeContext)
  39. {
  40. serializeContext->Class<SkeletonGroup, BehaviorComponent>()->Version(1);
  41. }
  42. }
  43. void SkeletonGroup::GetCategoryAssignments(CategoryRegistrationList& categories, const Containers::Scene& scene)
  44. {
  45. if (SceneHasSkeletonGroup(scene) || Utilities::DoesSceneGraphContainDataLike<DataTypes::IBoneData>(scene, false))
  46. {
  47. categories.emplace_back("Rigs", SceneData::SkeletonGroup::TYPEINFO_Uuid(), s_rigsPreferredTabOrder);
  48. }
  49. }
  50. void SkeletonGroup::InitializeObject(const Containers::Scene& scene, DataTypes::IManifestObject& target)
  51. {
  52. if (!m_isDefaultConstructing && target.RTTI_IsTypeOf(SceneData::SkeletonGroup::TYPEINFO_Uuid()))
  53. {
  54. SceneData::SkeletonGroup* group = azrtti_cast<SceneData::SkeletonGroup*>(&target);
  55. group->SetName(DataTypes::Utilities::CreateUniqueName<DataTypes::ISkeletonGroup>(scene.GetName(), scene.GetManifest()));
  56. const Containers::SceneGraph &graph = scene.GetGraph();
  57. auto contentStorage = graph.GetContentStorage();
  58. auto nameStorage = graph.GetNameStorage();
  59. auto nameContentView = Containers::Views::MakePairView(nameStorage, contentStorage);
  60. AZStd::string shallowestRootBoneName;
  61. auto graphDownwardsView = Containers::Views::MakeSceneGraphDownwardsView<Containers::Views::BreadthFirst>(graph, graph.GetRoot(), nameContentView.begin(), true);
  62. for (auto it = graphDownwardsView.begin(); it != graphDownwardsView.end(); ++it)
  63. {
  64. if (!it->second)
  65. {
  66. continue;
  67. }
  68. if (it->second->RTTI_IsTypeOf(AZ::SceneData::GraphData::RootBoneData::TYPEINFO_Uuid()))
  69. {
  70. shallowestRootBoneName = it->first.GetPath();
  71. break;
  72. }
  73. }
  74. group->SetSelectedRootBone(shallowestRootBoneName);
  75. }
  76. }
  77. Events::ProcessingResult SkeletonGroup::UpdateManifest(Containers::Scene& scene, ManifestAction action,
  78. RequestingApplication /*requester*/)
  79. {
  80. if (action == ManifestAction::ConstructDefault)
  81. {
  82. return BuildDefault(scene);
  83. }
  84. else if (action == ManifestAction::Update)
  85. {
  86. return UpdateSkeletonGroups(scene);
  87. }
  88. else
  89. {
  90. return Events::ProcessingResult::Ignored;
  91. }
  92. }
  93. Events::ProcessingResult SkeletonGroup::BuildDefault(Containers::Scene& scene)
  94. {
  95. if (SceneHasSkeletonGroup(scene))
  96. {
  97. return Events::ProcessingResult::Ignored;
  98. }
  99. const Containers::SceneGraph &graph = scene.GetGraph();
  100. auto contentStorage = graph.GetContentStorage();
  101. auto nameStorage = graph.GetNameStorage();
  102. auto nameContentView = Containers::Views::MakePairView(nameStorage, contentStorage);
  103. bool hasCreatedSkeletons = false;
  104. m_isDefaultConstructing = true;
  105. for (auto it = nameContentView.begin(); it != nameContentView.end(); ++it)
  106. {
  107. if (!it->second || !it->second->RTTI_IsTypeOf(AZ::SceneData::GraphData::RootBoneData::TYPEINFO_Uuid()))
  108. {
  109. continue;
  110. }
  111. // Check if this is a virtual type. There are no known virtual types supported by skeletons so this skeleton
  112. // pretends to be something that's not understood by this behavior, so skip it.
  113. Events::GraphMetaInfo::VirtualTypesSet virtualTypes;
  114. Events::GraphMetaInfoBus::Broadcast(&Events::GraphMetaInfoBus::Events::GetVirtualTypes, virtualTypes,
  115. scene, graph.ConvertToNodeIndex(it.GetFirstIterator()));
  116. if (!virtualTypes.empty())
  117. {
  118. continue;
  119. }
  120. AZStd::shared_ptr<SceneData::SkeletonGroup> group = AZStd::make_shared<SceneData::SkeletonGroup>();
  121. AZStd::string name = DataTypes::Utilities::CreateUniqueName<DataTypes::ISkeletonGroup>(scene.GetName(), it->first.GetName(), scene.GetManifest());
  122. // This is a group that's generated automatically so may not be saved to disk but would need to be recreated
  123. // in the same way again. To guarantee the same uuid, generate a stable one instead.
  124. group->OverrideId(DataTypes::Utilities::CreateStableUuid(scene, SceneData::SkeletonGroup::TYPEINFO_Uuid(), name));
  125. group->SetName(AZStd::move(name));
  126. group->SetSelectedRootBone(it->first.GetPath());
  127. Events::ManifestMetaInfoBus::Broadcast(&Events::ManifestMetaInfoBus::Events::InitializeObject, scene, *group);
  128. scene.GetManifest().AddEntry(AZStd::move(group));
  129. hasCreatedSkeletons = true;
  130. }
  131. m_isDefaultConstructing = false;
  132. return hasCreatedSkeletons ? Events::ProcessingResult::Success : Events::ProcessingResult::Ignored;
  133. }
  134. Events::ProcessingResult SkeletonGroup::UpdateSkeletonGroups(Containers::Scene& scene) const
  135. {
  136. bool updated = false;
  137. Containers::SceneManifest& manifest = scene.GetManifest();
  138. auto valueStorage = manifest.GetValueStorage();
  139. auto view = Containers::MakeDerivedFilterView<SceneData::SkeletonGroup>(valueStorage);
  140. for (SceneData::SkeletonGroup& group : view)
  141. {
  142. if (group.GetName().empty())
  143. {
  144. group.SetName(DataTypes::Utilities::CreateUniqueName<DataTypes::ISkeletonGroup>(scene.GetName(), scene.GetManifest()));
  145. updated = true;
  146. }
  147. if (group.GetId().IsNull())
  148. {
  149. // When the uuid is null it's likely because the manifest has been updated from an older version. Include the
  150. // name of the group as there could be multiple groups.
  151. group.OverrideId(DataTypes::Utilities::CreateStableUuid(scene, SceneData::SkeletonGroup::TYPEINFO_Uuid(), group.GetName()));
  152. updated = true;
  153. }
  154. }
  155. return updated ? Events::ProcessingResult::Success : Events::ProcessingResult::Ignored;
  156. }
  157. bool SkeletonGroup::SceneHasSkeletonGroup(const Containers::Scene& scene) const
  158. {
  159. const Containers::SceneManifest& manifest = scene.GetManifest();
  160. Containers::SceneManifest::ValueStorageConstData manifestData = manifest.GetValueStorage();
  161. auto skeletonGroup = AZStd::find_if(manifestData.begin(), manifestData.end(), Containers::DerivedTypeFilter<DataTypes::ISkeletonGroup>());
  162. return skeletonGroup != manifestData.end();
  163. }
  164. } // namespace Behaviors
  165. } // namespace SceneAPI
  166. } // namespace AZ