From 6583178a4c4a8332ee9b35a49a8e7bf203d18836 Mon Sep 17 00:00:00 2001 From: rhongAMZ <69218254+rhongAMZ@users.noreply.github.com> Date: Fri, 23 Apr 2021 09:54:09 -0700 Subject: [PATCH] EMotionFX: Removing the Motions after corresponding Motionset is removed from the active Animgraph crashes the Editor (#276) LYN-3200 EMotionFX: Removing the Motions after corresponding Motionset is removed from the active Animgraph crashes the Editor --- .../CommandSystem/Source/MotionSetCommands.cpp | 18 ++++++++++-------- .../Code/EMotionFX/Source/AnimGraphManager.cpp | 2 +- .../Code/EMotionFX/Source/AnimGraphManager.h | 2 +- .../Code/EMotionFX/Source/BlendSpace1DNode.cpp | 5 +++++ .../Code/EMotionFX/Source/BlendSpace2DNode.cpp | 7 +++++++ 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Gems/EMotionFX/Code/EMotionFX/CommandSystem/Source/MotionSetCommands.cpp b/Gems/EMotionFX/Code/EMotionFX/CommandSystem/Source/MotionSetCommands.cpp index ea64cec116..54780b2ad0 100644 --- a/Gems/EMotionFX/Code/EMotionFX/CommandSystem/Source/MotionSetCommands.cpp +++ b/Gems/EMotionFX/Code/EMotionFX/CommandSystem/Source/MotionSetCommands.cpp @@ -177,7 +177,7 @@ namespace CommandSystem } // Update unique datas for all anim graph instances using the given motion set. - EMotionFX::GetAnimGraphManager().UpdateInstancesUniqueDataUsingMotionSet(motionSet); + EMotionFX::GetAnimGraphManager().InvalidateInstanceUniqueDataUsingMotionSet(motionSet); // Mark the workspace as dirty mOldWorkspaceDirtyFlag = GetCommandManager()->GetWorkspaceDirtyFlag(); @@ -260,7 +260,12 @@ namespace CommandSystem const AZStd::string commandString = AZStd::string::format("AdjustMotionSet -motionSetID %i -dirtyFlag true", mOldParentSetID); GetCommandManager()->ExecuteCommandInsideCommand(commandString, outResult); } - + + // Update unique datas for all anim graph instances using the given motion set. + // After removing a motion set, the used motion set from an anim graph instance will be reset. If we call this function after + // RemoveMotionSet, the anim graph instance would hold a nullptr for motion set, and wouldn't be invalidated. + EMotionFX::GetAnimGraphManager().InvalidateInstanceUniqueDataUsingMotionSet(motionSet); + // Destroy the motion set. EMotionFX::GetMotionManager().RemoveMotionSet(motionSet, true); @@ -278,9 +283,6 @@ namespace CommandSystem animGraph->RecursiveReinit(); } - // Update unique datas for all anim graph instances using the given motion set. - EMotionFX::GetAnimGraphManager().UpdateInstancesUniqueDataUsingMotionSet(motionSet); - // Mark the workspace as dirty. mOldWorkspaceDirtyFlag = GetCommandManager()->GetWorkspaceDirtyFlag(); GetCommandManager()->SetWorkspaceDirtyFlag(true); @@ -487,7 +489,7 @@ namespace CommandSystem } // Update unique datas for all anim graph instances using the given motion set. - EMotionFX::GetAnimGraphManager().UpdateInstancesUniqueDataUsingMotionSet(motionSet); + EMotionFX::GetAnimGraphManager().InvalidateInstanceUniqueDataUsingMotionSet(motionSet); // Return the id of the newly created motion set. AZStd::to_string(outResult, motionSet->GetID()); @@ -610,7 +612,7 @@ namespace CommandSystem } // Update unique datas for all anim graph instances using the given motion set. - EMotionFX::GetAnimGraphManager().UpdateInstancesUniqueDataUsingMotionSet(motionSet); + EMotionFX::GetAnimGraphManager().InvalidateInstanceUniqueDataUsingMotionSet(motionSet); // Check if we were able to remove all requested motion entries. if (!failedToRemoveMotionIdsString.empty()) @@ -806,7 +808,7 @@ namespace CommandSystem } // Update unique datas for all anim graph instances using the given motion set. - EMotionFX::GetAnimGraphManager().UpdateInstancesUniqueDataUsingMotionSet(motionSet); + EMotionFX::GetAnimGraphManager().InvalidateInstanceUniqueDataUsingMotionSet(motionSet); // Set the dirty flag. const AZStd::string command = AZStd::string::format("AdjustMotionSet -motionSetID %i -dirtyFlag true", motionSetID); diff --git a/Gems/EMotionFX/Code/EMotionFX/Source/AnimGraphManager.cpp b/Gems/EMotionFX/Code/EMotionFX/Source/AnimGraphManager.cpp index be091c471f..982c5c7a08 100644 --- a/Gems/EMotionFX/Code/EMotionFX/Source/AnimGraphManager.cpp +++ b/Gems/EMotionFX/Code/EMotionFX/Source/AnimGraphManager.cpp @@ -327,7 +327,7 @@ namespace EMotionFX } } - void AnimGraphManager::UpdateInstancesUniqueDataUsingMotionSet(EMotionFX::MotionSet* motionSet) + void AnimGraphManager::InvalidateInstanceUniqueDataUsingMotionSet(EMotionFX::MotionSet* motionSet) { // Update unique datas for all anim graph instances that use the given motion set. for (EMotionFX::AnimGraphInstance* animGraphInstance : mAnimGraphInstances) diff --git a/Gems/EMotionFX/Code/EMotionFX/Source/AnimGraphManager.h b/Gems/EMotionFX/Code/EMotionFX/Source/AnimGraphManager.h index 3650336c70..4b1a405ae0 100644 --- a/Gems/EMotionFX/Code/EMotionFX/Source/AnimGraphManager.h +++ b/Gems/EMotionFX/Code/EMotionFX/Source/AnimGraphManager.h @@ -66,7 +66,7 @@ namespace EMotionFX bool RemoveAnimGraphInstance(AnimGraphInstance* animGraphInstance, bool delFromMemory = true); void RemoveAnimGraphInstances(AnimGraph* animGraph, bool delFromMemory = true); void RemoveAllAnimGraphInstances(bool delFromMemory = true); - void UpdateInstancesUniqueDataUsingMotionSet(EMotionFX::MotionSet* motionSet); + void InvalidateInstanceUniqueDataUsingMotionSet(EMotionFX::MotionSet* motionSet); size_t GetNumAnimGraphInstances() const { MCore::LockGuardRecursive lock(mAnimGraphInstanceLock); return mAnimGraphInstances.size(); } AnimGraphInstance* GetAnimGraphInstance(size_t index) const { MCore::LockGuardRecursive lock(mAnimGraphInstanceLock); return mAnimGraphInstances[index]; } diff --git a/Gems/EMotionFX/Code/EMotionFX/Source/BlendSpace1DNode.cpp b/Gems/EMotionFX/Code/EMotionFX/Source/BlendSpace1DNode.cpp index 91b15fd170..3be06772ed 100644 --- a/Gems/EMotionFX/Code/EMotionFX/Source/BlendSpace1DNode.cpp +++ b/Gems/EMotionFX/Code/EMotionFX/Source/BlendSpace1DNode.cpp @@ -75,6 +75,11 @@ namespace EMotionFX void BlendSpace1DNode::UniqueData::Reset() { + BlendSpaceNode::ClearMotionInfos(m_motionInfos); + m_currentSegment.m_segmentIndex = MCORE_INVALIDINDEX32; + m_motionCoordinates.clear(); + m_sortedMotions.clear(); + Invalidate(); } diff --git a/Gems/EMotionFX/Code/EMotionFX/Source/BlendSpace2DNode.cpp b/Gems/EMotionFX/Code/EMotionFX/Source/BlendSpace2DNode.cpp index e2e839b2f5..db12bbfc22 100644 --- a/Gems/EMotionFX/Code/EMotionFX/Source/BlendSpace2DNode.cpp +++ b/Gems/EMotionFX/Code/EMotionFX/Source/BlendSpace2DNode.cpp @@ -152,6 +152,13 @@ namespace EMotionFX void BlendSpace2DNode::UniqueData::Reset() { + BlendSpaceNode::ClearMotionInfos(m_motionInfos); + m_currentTriangle.m_triangleIndex = MCORE_INVALIDINDEX32; + m_currentEdge.m_edgeIndex = MCORE_INVALIDINDEX32; + m_motionCoordinates.clear(); + m_normMotionPositions.clear(); + m_blendInfos.clear(); + Invalidate(); }