/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace AZ { namespace SceneAPI { namespace Behaviors { const int SkeletonGroup::s_rigsPreferredTabOrder = 1; void SkeletonGroup::Activate() { #ifdef ENABLE_LEGACY_ANIMATION Events::ManifestMetaInfoBus::Handler::BusConnect(); Events::AssetImportRequestBus::Handler::BusConnect(); #endif // ENABLE_LEGACY_ANIMATION } void SkeletonGroup::Deactivate() { #ifdef ENABLE_LEGACY_ANIMATION Events::AssetImportRequestBus::Handler::BusDisconnect(); Events::ManifestMetaInfoBus::Handler::BusDisconnect(); #endif // ENABLE_LEGACY_ANIMATION } void SkeletonGroup::Reflect(ReflectContext* context) { SerializeContext* serializeContext = azrtti_cast(context); if (serializeContext) { serializeContext->Class()->Version(1); } } void SkeletonGroup::GetCategoryAssignments(CategoryRegistrationList& categories, const Containers::Scene& scene) { if (SceneHasSkeletonGroup(scene) || Utilities::DoesSceneGraphContainDataLike(scene, false)) { categories.emplace_back("Rigs", SceneData::SkeletonGroup::TYPEINFO_Uuid(), s_rigsPreferredTabOrder); } } void SkeletonGroup::InitializeObject(const Containers::Scene& scene, DataTypes::IManifestObject& target) { if (!m_isDefaultConstructing && target.RTTI_IsTypeOf(SceneData::SkeletonGroup::TYPEINFO_Uuid())) { SceneData::SkeletonGroup* group = azrtti_cast(&target); group->SetName(DataTypes::Utilities::CreateUniqueName(scene.GetName(), scene.GetManifest())); const Containers::SceneGraph &graph = scene.GetGraph(); auto contentStorage = graph.GetContentStorage(); auto nameStorage = graph.GetNameStorage(); auto nameContentView = Containers::Views::MakePairView(nameStorage, contentStorage); AZStd::string shallowestRootBoneName; auto graphDownwardsView = Containers::Views::MakeSceneGraphDownwardsView(graph, graph.GetRoot(), nameContentView.begin(), true); for (auto it = graphDownwardsView.begin(); it != graphDownwardsView.end(); ++it) { if (!it->second) { continue; } if (it->second->RTTI_IsTypeOf(AZ::SceneData::GraphData::RootBoneData::TYPEINFO_Uuid())) { shallowestRootBoneName = it->first.GetPath(); break; } } group->SetSelectedRootBone(shallowestRootBoneName); } } Events::ProcessingResult SkeletonGroup::UpdateManifest(Containers::Scene& scene, ManifestAction action, RequestingApplication /*requester*/) { if (action == ManifestAction::ConstructDefault) { return BuildDefault(scene); } else if (action == ManifestAction::Update) { return UpdateSkeletonGroups(scene); } else { return Events::ProcessingResult::Ignored; } } Events::ProcessingResult SkeletonGroup::BuildDefault(Containers::Scene& scene) { if (SceneHasSkeletonGroup(scene)) { return Events::ProcessingResult::Ignored; } const Containers::SceneGraph &graph = scene.GetGraph(); auto contentStorage = graph.GetContentStorage(); auto nameStorage = graph.GetNameStorage(); auto nameContentView = Containers::Views::MakePairView(nameStorage, contentStorage); bool hasCreatedSkeletons = false; m_isDefaultConstructing = true; for (auto it = nameContentView.begin(); it != nameContentView.end(); ++it) { if (!it->second || !it->second->RTTI_IsTypeOf(AZ::SceneData::GraphData::RootBoneData::TYPEINFO_Uuid())) { continue; } // Check if this is a virtual type. There are no known virtual types supported by skeletons so this skeleton // pretends to be something that's not understood by this behavior, so skip it. AZStd::set virtualTypes; Events::GraphMetaInfoBus::Broadcast(&Events::GraphMetaInfoBus::Events::GetVirtualTypes, virtualTypes, scene, graph.ConvertToNodeIndex(it.GetFirstIterator())); if (!virtualTypes.empty()) { continue; } AZStd::shared_ptr group = AZStd::make_shared(); AZStd::string name = DataTypes::Utilities::CreateUniqueName(scene.GetName(), it->first.GetName(), scene.GetManifest()); // This is a group that's generated automatically so may not be saved to disk but would need to be recreated // in the same way again. To guarantee the same uuid, generate a stable one instead. group->OverrideId(DataTypes::Utilities::CreateStableUuid(scene, SceneData::SkeletonGroup::TYPEINFO_Uuid(), name)); group->SetName(AZStd::move(name)); group->SetSelectedRootBone(it->first.GetPath()); Events::ManifestMetaInfoBus::Broadcast(&Events::ManifestMetaInfoBus::Events::InitializeObject, scene, *group); scene.GetManifest().AddEntry(AZStd::move(group)); hasCreatedSkeletons = true; } m_isDefaultConstructing = false; return hasCreatedSkeletons ? Events::ProcessingResult::Success : Events::ProcessingResult::Ignored; } Events::ProcessingResult SkeletonGroup::UpdateSkeletonGroups(Containers::Scene& scene) const { bool updated = false; Containers::SceneManifest& manifest = scene.GetManifest(); auto valueStorage = manifest.GetValueStorage(); auto view = Containers::MakeDerivedFilterView(valueStorage); for (SceneData::SkeletonGroup& group : view) { if (group.GetName().empty()) { group.SetName(DataTypes::Utilities::CreateUniqueName(scene.GetName(), scene.GetManifest())); updated = true; } if (group.GetId().IsNull()) { // When the uuid is null it's likely because the manifest has been updated from an older version. Include the // name of the group as there could be multiple groups. group.OverrideId(DataTypes::Utilities::CreateStableUuid(scene, SceneData::SkeletonGroup::TYPEINFO_Uuid(), group.GetName())); updated = true; } } return updated ? Events::ProcessingResult::Success : Events::ProcessingResult::Ignored; } bool SkeletonGroup::SceneHasSkeletonGroup(const Containers::Scene& scene) const { const Containers::SceneManifest& manifest = scene.GetManifest(); Containers::SceneManifest::ValueStorageConstData manifestData = manifest.GetValueStorage(); auto skeletonGroup = AZStd::find_if(manifestData.begin(), manifestData.end(), Containers::DerivedTypeFilter()); return skeletonGroup != manifestData.end(); } } // namespace Behaviors } // namespace SceneAPI } // namespace AZ