BehaviorsSkeletonGroup.cpp 9.1 KB

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