EditorAnimGraphComponent.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include "EMotionFX_precompiled.h"
  8. #include <AzCore/Asset/AssetManager.h>
  9. #include <AzCore/Script/ScriptProperty.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  13. #include <EMotionFX/Source/Parameter/BoolParameter.h>
  14. #include <EMotionFX/Source/Parameter/FloatParameter.h>
  15. #include <EMotionFX/Source/Parameter/StringParameter.h>
  16. #include <EMotionFX/Source/Parameter/IntParameter.h>
  17. #include <EMotionFX/Tools/EMotionStudio/EMStudioSDK/Source/EMStudioManager.h>
  18. #include <EMotionFX/Tools/EMotionStudio/EMStudioSDK/Source/MainWindow.h>
  19. #include <Integration/ActorComponentBus.h>
  20. #include <Integration/Editor/Components/EditorAnimGraphComponent.h>
  21. #include <QApplication>
  22. namespace EMotionFX
  23. {
  24. namespace Integration
  25. {
  26. //////////////////////////////////////////////////////////////////////////
  27. void EditorAnimGraphComponent::Reflect(AZ::ReflectContext* context)
  28. {
  29. auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  30. if (serializeContext)
  31. {
  32. serializeContext->Class<EditorAnimGraphComponent, AzToolsFramework::Components::EditorComponentBase>()
  33. ->Version(2)
  34. ->Field("AnimGraphAsset", &EditorAnimGraphComponent::m_animGraphAsset)
  35. ->Field("MotionSetAsset", &EditorAnimGraphComponent::m_motionSetAsset)
  36. ->Field("ActiveMotionSetName", &EditorAnimGraphComponent::m_activeMotionSetName)
  37. ->Field("DebugVisualization", &EditorAnimGraphComponent::m_visualize)
  38. ->Field("ParameterDefaults", &EditorAnimGraphComponent::m_parameterDefaults)
  39. ;
  40. AZ::EditContext* editContext = serializeContext->GetEditContext();
  41. if (editContext)
  42. {
  43. editContext->Class<AnimGraphComponent::ParameterDefaults>(
  44. "Parameter Defaults", "Default values for anim graph parameters.")
  45. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  46. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  47. ->DataElement(AZ::Edit::UIHandlers::Button, &AnimGraphComponent::ParameterDefaults::m_parameters, "", "")
  48. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  49. ->Attribute(AZ::Edit::Attributes::ContainerCanBeModified, false)
  50. ;
  51. editContext->Class<EditorAnimGraphComponent>(
  52. "Anim Graph", "The Anim Graph component manages a set of assets that are built in the Animation Editor, including the animation graph, default parameter settings, and assigned motion set for the associated Actor")
  53. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  54. ->Attribute(AZ::Edit::Attributes::Category, "Animation")
  55. ->Attribute(AZ::Edit::Attributes::Icon, ":/EMotionFX/AnimGraphComponent.svg")
  56. ->Attribute(AZ::Edit::Attributes::PrimaryAssetType, azrtti_typeid<AnimGraphAsset>())
  57. ->Attribute(AZ::Edit::Attributes::ViewportIcon, ":/EMotionFX/AnimGraphComponent.svg")
  58. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game", 0x232b318c))
  59. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  60. ->Attribute(AZ::Edit::Attributes::HelpPageURL, "https://o3de.org/docs/user-guide/components/reference/animgraph/")
  61. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorAnimGraphComponent::m_motionSetAsset,
  62. "Motion set asset", "EMotion FX motion set asset to be loaded for this actor.")
  63. ->Attribute("EditButton", "")
  64. ->Attribute("EditDescription", "Open in Animation Editor")
  65. ->Attribute("EditCallback", &EditorAnimGraphComponent::LaunchAnimationEditor)
  66. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorAnimGraphComponent::OnMotionSetAssetSelected)
  67. ->DataElement(AZ_CRC("MotionSetName", 0xcf534ea6), &EditorAnimGraphComponent::m_activeMotionSetName, "Active motion set", "Motion set to use for this anim graph instance")
  68. ->Attribute(AZ_CRC("MotionSetAsset", 0xd4e88984), &EditorAnimGraphComponent::GetMotionAsset)
  69. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorAnimGraphComponent::m_visualize, "Debug visualization", "Enable this to allow the anim graph to render debug visualization. Enable debug rendering on anim graph nodes first.")
  70. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorAnimGraphComponent::m_animGraphAsset,
  71. "Anim graph", "EMotion FX anim graph to be assigned to this actor.")
  72. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorAnimGraphComponent::OnAnimGraphAssetSelected)
  73. ->Attribute("EditButton", "")
  74. ->Attribute("EditDescription", "Open in Animation Editor")
  75. ->Attribute("EditCallback", &EditorAnimGraphComponent::LaunchAnimationEditor)
  76. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorAnimGraphComponent::m_parameterDefaults,
  77. "Parameters", "Anim graph default parameter values.")
  78. ;
  79. }
  80. }
  81. }
  82. //////////////////////////////////////////////////////////////////////////
  83. EditorAnimGraphComponent::EditorAnimGraphComponent()
  84. {
  85. }
  86. //////////////////////////////////////////////////////////////////////////
  87. EditorAnimGraphComponent::~EditorAnimGraphComponent()
  88. {
  89. }
  90. //////////////////////////////////////////////////////////////////////////
  91. void EditorAnimGraphComponent::Activate()
  92. {
  93. // Refresh parameters in case anim graph asset changed since last session.
  94. OnAnimGraphAssetSelected();
  95. OnMotionSetAssetSelected();
  96. EditorAnimGraphComponentRequestBus::Handler::BusConnect(GetEntityId());
  97. }
  98. //////////////////////////////////////////////////////////////////////////
  99. void EditorAnimGraphComponent::Deactivate()
  100. {
  101. EditorAnimGraphComponentRequestBus::Handler::BusDisconnect();
  102. AZ::Data::AssetBus::MultiHandler::BusDisconnect();
  103. m_animGraphAsset.Release();
  104. m_motionSetAsset.Release();
  105. }
  106. void EditorAnimGraphComponent::LaunchAnimationEditor(const AZ::Data::AssetId& assetId, [[maybe_unused]] const AZ::Data::AssetType& assetType)
  107. {
  108. if (assetId.IsValid())
  109. {
  110. AZ::Data::AssetId actorAssetId;
  111. actorAssetId.SetInvalid();
  112. EditorActorComponentRequestBus::EventResult(actorAssetId, GetEntityId(), &EditorActorComponentRequestBus::Events::GetActorAssetId);
  113. // call to open must be done before LoadCharacter
  114. const char* panelName = EMStudio::MainWindow::GetEMotionFXPaneName();
  115. EBUS_EVENT(AzToolsFramework::EditorRequests::Bus, OpenViewPane, panelName);
  116. EMStudio::MainWindow* mainWindow = EMStudio::GetMainWindow();
  117. if (mainWindow)
  118. {
  119. mainWindow->LoadCharacter(actorAssetId, m_animGraphAsset.GetId(), m_motionSetAsset.GetId());
  120. mainWindow->show();
  121. mainWindow->LoadLayoutAfterShow();
  122. // Force the window to be fully loaded before loading
  123. // things. Remember that QMainWindow::show() doesn't
  124. // actually show anything syncronously. All it does is put
  125. // a QShowEvent onto the event queue. This call makes the
  126. // ShowEvent process, blocking until it is done.
  127. QApplication::instance()->processEvents(QEventLoop::ExcludeUserInputEvents);
  128. // After loading we want to activate based on what we have in this component (anim grah and motion set)
  129. // Only activate if we have a valid anim graph and a valid motion set. An empty m_motionSetName will use
  130. // the root motionset from the motion set asset
  131. if (m_animGraphAsset.IsReady() && m_motionSetAsset.IsReady())
  132. {
  133. AnimGraphAsset* animGraphAsset = m_animGraphAsset.GetAs<AnimGraphAsset>();
  134. AZ_Assert(animGraphAsset, "Expected anim graph asset");
  135. EMotionFX::AnimGraph* animGraph = animGraphAsset->GetAnimGraph();
  136. MotionSetAsset* motionSetAsset = m_motionSetAsset.GetAs<MotionSetAsset>();
  137. AZ_Assert(motionSetAsset, "Expected motion set asset");
  138. EMotionFX::MotionSet* rootMotionSet = motionSetAsset->m_emfxMotionSet.get();
  139. EMotionFX::MotionSet* motionSet = rootMotionSet;
  140. if (!m_activeMotionSetName.empty())
  141. {
  142. motionSet = rootMotionSet->RecursiveFindMotionSetByName(m_activeMotionSetName, true);
  143. if (!motionSet)
  144. {
  145. AZ_Warning("EMotionFX", false, "Failed to find motion set \"%s\" in motion set file %s.",
  146. m_activeMotionSetName.c_str(),
  147. rootMotionSet->GetName());
  148. motionSet = rootMotionSet;
  149. }
  150. }
  151. mainWindow->Activate(actorAssetId, animGraph, motionSet);
  152. }
  153. }
  154. }
  155. }
  156. //////////////////////////////////////////////////////////////////////////
  157. AZ::u32 EditorAnimGraphComponent::OnAnimGraphAssetSelected()
  158. {
  159. AZ::Data::AssetBus::MultiHandler::BusDisconnect();
  160. if (m_motionSetAsset.GetId().IsValid())
  161. {
  162. AZ::Data::AssetBus::MultiHandler::BusConnect(m_motionSetAsset.GetId());
  163. }
  164. if (m_animGraphAsset.GetId().IsValid())
  165. {
  166. AZ::Data::AssetBus::MultiHandler::BusConnect(m_animGraphAsset.GetId());
  167. m_animGraphAsset.QueueLoad();
  168. }
  169. else
  170. {
  171. m_parameterDefaults.m_parameters.clear();
  172. }
  173. return AZ::Edit::PropertyRefreshLevels::EntireTree;
  174. }
  175. AZ::u32 EditorAnimGraphComponent::OnMotionSetAssetSelected()
  176. {
  177. AZ::Data::AssetBus::MultiHandler::BusDisconnect();
  178. if (m_animGraphAsset.GetId().IsValid())
  179. {
  180. AZ::Data::AssetBus::MultiHandler::BusConnect(m_animGraphAsset.GetId());
  181. }
  182. if (m_motionSetAsset.GetId().IsValid())
  183. {
  184. AZ::Data::AssetBus::MultiHandler::BusConnect(m_motionSetAsset.GetId());
  185. m_motionSetAsset.QueueLoad();
  186. }
  187. return AZ::Edit::PropertyRefreshLevels::EntireTree;
  188. }
  189. //////////////////////////////////////////////////////////////////////////
  190. void EditorAnimGraphComponent::OnAssetReloaded(AZ::Data::Asset<AZ::Data::AssetData> asset)
  191. {
  192. // Re-process anim graph asset.
  193. OnAssetReady(asset);
  194. }
  195. //////////////////////////////////////////////////////////////////////////
  196. void EditorAnimGraphComponent::SetPrimaryAsset(const AZ::Data::AssetId& assetId)
  197. {
  198. AZ::Data::Asset<AnimGraphAsset> asset = AZ::Data::AssetManager::Instance().FindOrCreateAsset<AnimGraphAsset>(assetId, m_animGraphAsset.GetAutoLoadBehavior());
  199. if (asset)
  200. {
  201. m_animGraphAsset = asset;
  202. }
  203. }
  204. bool EditorAnimGraphComponent::IsSupportedScriptPropertyType(const ValueParameter* param) const
  205. {
  206. return (azrtti_istypeof<FloatParameter>(param) ||
  207. azrtti_istypeof<IntParameter>(param) ||
  208. azrtti_istypeof<BoolParameter>(param) ||
  209. azrtti_istypeof<StringParameter>(param));
  210. }
  211. //////////////////////////////////////////////////////////////////////////
  212. void EditorAnimGraphComponent::OnAssetReady(AZ::Data::Asset<AZ::Data::AssetData> asset)
  213. {
  214. AZ_Assert(asset == m_animGraphAsset || asset == m_motionSetAsset, "Unexpected asset");
  215. if (asset == m_animGraphAsset)
  216. {
  217. m_animGraphAsset = asset;
  218. AnimGraphAsset* data = m_animGraphAsset.GetAs<AnimGraphAsset>();
  219. if (!data)
  220. {
  221. return;
  222. }
  223. EMotionFX::AnimGraph* animGraph = data->GetAnimGraph();
  224. // Remove any parameters we have values for that are no longer in the anim graph.
  225. for (auto iter = m_parameterDefaults.m_parameters.begin(); iter != m_parameterDefaults.m_parameters.end(); )
  226. {
  227. const ValueParameter* valueParameter = animGraph->FindValueParameterByName((*iter)->m_name);
  228. if (!valueParameter || !IsSupportedScriptPropertyType(valueParameter))
  229. {
  230. delete *iter;
  231. iter = m_parameterDefaults.m_parameters.erase(iter);
  232. }
  233. else
  234. {
  235. ++iter;
  236. }
  237. }
  238. // Populate property array based on parameters found in the anim graph.
  239. const EMotionFX::ValueParameterVector& valueParameters = animGraph->RecursivelyGetValueParameters();
  240. for (const EMotionFX::ValueParameter* param : valueParameters)
  241. {
  242. const AZStd::string& paramName = param->GetName();
  243. // If we already have a value for this property, skip it.
  244. bool found = false;
  245. for (AZ::ScriptProperty* prop : m_parameterDefaults.m_parameters)
  246. {
  247. if (paramName == prop->m_name)
  248. {
  249. found = true;
  250. break;
  251. }
  252. }
  253. if (found)
  254. {
  255. continue;
  256. }
  257. // Based on the anim graph param type, create an appropriate script property for serialization and editing.
  258. if (azrtti_istypeof<EMotionFX::FloatParameter>(param))
  259. {
  260. const EMotionFX::FloatParameter* floatParam = static_cast<const EMotionFX::FloatParameter*>(param);
  261. m_parameterDefaults.m_parameters.emplace_back(aznew AZ::ScriptPropertyNumber(paramName.c_str(), floatParam->GetDefaultValue()));
  262. }
  263. else if (azrtti_istypeof<EMotionFX::IntParameter>(param))
  264. {
  265. const EMotionFX::IntParameter* intParam = static_cast<const EMotionFX::IntParameter*>(param);
  266. m_parameterDefaults.m_parameters.emplace_back(aznew AZ::ScriptPropertyNumber(paramName.c_str(), intParam->GetDefaultValue()));
  267. }
  268. else if (azrtti_istypeof<EMotionFX::BoolParameter>(param))
  269. {
  270. const EMotionFX::BoolParameter* boolParam = static_cast<const EMotionFX::BoolParameter*>(param);
  271. m_parameterDefaults.m_parameters.emplace_back(aznew AZ::ScriptPropertyBoolean(paramName.c_str(), boolParam->GetDefaultValue()));
  272. }
  273. else if (azrtti_istypeof<EMotionFX::StringParameter>(param))
  274. {
  275. const EMotionFX::StringParameter* stringParam = static_cast<const EMotionFX::StringParameter*>(param);
  276. m_parameterDefaults.m_parameters.emplace_back(aznew AZ::ScriptPropertyString(paramName.c_str(), stringParam->GetDefaultValue().c_str()));
  277. }
  278. else
  279. {
  280. AZ_Assert(!IsSupportedScriptPropertyType(param), "This value parameter of this type ('%s') should not be supported. Please update the IsSupportedScriptPropertyType() method.", param->GetTypeDisplayName());
  281. }
  282. }
  283. }
  284. else if (asset == m_motionSetAsset)
  285. {
  286. m_motionSetAsset = asset;
  287. const MotionSetAsset* data = m_motionSetAsset.GetAs<MotionSetAsset>();
  288. if (data)
  289. {
  290. const EMotionFX::MotionSet* rootMotionSet = data->m_emfxMotionSet.get();
  291. if (rootMotionSet)
  292. {
  293. if (m_activeMotionSetName.empty())
  294. {
  295. // if motion set name is empty, grab the root
  296. m_activeMotionSetName = rootMotionSet->GetName();
  297. }
  298. else
  299. {
  300. const EMotionFX::MotionSet* motionSet = rootMotionSet->RecursiveFindMotionSetByName(m_activeMotionSetName, /*isOwnedByRuntime = */true);
  301. if (!motionSet)
  302. {
  303. m_activeMotionSetName = rootMotionSet->GetName();
  304. }
  305. }
  306. }
  307. }
  308. }
  309. // Force-refresh the property grid.
  310. using namespace AzToolsFramework;
  311. EBUS_EVENT(ToolsApplicationEvents::Bus, InvalidatePropertyDisplay, Refresh_EntireTree);
  312. }
  313. const AZ::Data::AssetId& EditorAnimGraphComponent::GetAnimGraphAssetId()
  314. {
  315. return m_animGraphAsset.GetId();
  316. }
  317. const AZ::Data::AssetId& EditorAnimGraphComponent::GetMotionSetAssetId()
  318. {
  319. return m_motionSetAsset.GetId();
  320. }
  321. void EditorAnimGraphComponent::SetAnimGraphAssetId(const AZ::Data::AssetId& assetId)
  322. {
  323. m_animGraphAsset = AZ::Data::Asset<AnimGraphAsset>(assetId, azrtti_typeid<AnimGraphAsset>());
  324. }
  325. void EditorAnimGraphComponent::SetMotionSetAssetId(const AZ::Data::AssetId& assetId)
  326. {
  327. m_motionSetAsset = AZ::Data::Asset<MotionSetAsset>(assetId, azrtti_typeid<MotionSetAsset>());
  328. }
  329. //////////////////////////////////////////////////////////////////////////
  330. void EditorAnimGraphComponent::BuildGameEntity(AZ::Entity* gameEntity)
  331. {
  332. AnimGraphComponent::Configuration cfg;
  333. cfg.m_animGraphAsset = m_animGraphAsset;
  334. cfg.m_motionSetAsset = m_motionSetAsset;
  335. cfg.m_activeMotionSetName = m_activeMotionSetName;
  336. cfg.m_parameterDefaults = m_parameterDefaults;
  337. cfg.m_visualize = m_visualize;
  338. gameEntity->AddComponent(aznew AnimGraphComponent(&cfg));
  339. }
  340. } //namespace Integration
  341. } // namespace EMotionFX