animation_tree.cpp 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267
  1. /**************************************************************************/
  2. /* animation_tree.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "animation_tree.h"
  31. #include "animation_blend_tree.h"
  32. #include "core/config/engine.h"
  33. #include "scene/resources/animation.h"
  34. #include "scene/scene_string_names.h"
  35. #include "servers/audio/audio_stream.h"
  36. void AnimationNode::get_parameter_list(List<PropertyInfo> *r_list) const {
  37. Array parameters;
  38. if (GDVIRTUAL_CALL(_get_parameter_list, parameters)) {
  39. for (int i = 0; i < parameters.size(); i++) {
  40. Dictionary d = parameters[i];
  41. ERR_CONTINUE(d.is_empty());
  42. r_list->push_back(PropertyInfo::from_dict(d));
  43. }
  44. }
  45. }
  46. Variant AnimationNode::get_parameter_default_value(const StringName &p_parameter) const {
  47. Variant ret;
  48. GDVIRTUAL_CALL(_get_parameter_default_value, p_parameter, ret);
  49. return ret;
  50. }
  51. bool AnimationNode::is_parameter_read_only(const StringName &p_parameter) const {
  52. bool ret = false;
  53. GDVIRTUAL_CALL(_is_parameter_read_only, p_parameter, ret);
  54. return ret;
  55. }
  56. void AnimationNode::set_parameter(const StringName &p_name, const Variant &p_value) {
  57. if (is_testing) {
  58. return;
  59. }
  60. ERR_FAIL_COND(!state);
  61. ERR_FAIL_COND(!state->tree->property_parent_map.has(base_path));
  62. ERR_FAIL_COND(!state->tree->property_parent_map[base_path].has(p_name));
  63. StringName path = state->tree->property_parent_map[base_path][p_name];
  64. state->tree->property_map[path].first = p_value;
  65. }
  66. Variant AnimationNode::get_parameter(const StringName &p_name) const {
  67. ERR_FAIL_COND_V(!state, Variant());
  68. ERR_FAIL_COND_V(!state->tree->property_parent_map.has(base_path), Variant());
  69. ERR_FAIL_COND_V(!state->tree->property_parent_map[base_path].has(p_name), Variant());
  70. StringName path = state->tree->property_parent_map[base_path][p_name];
  71. return state->tree->property_map[path].first;
  72. }
  73. void AnimationNode::get_child_nodes(List<ChildNode> *r_child_nodes) {
  74. Dictionary cn;
  75. if (GDVIRTUAL_CALL(_get_child_nodes, cn)) {
  76. List<Variant> keys;
  77. cn.get_key_list(&keys);
  78. for (const Variant &E : keys) {
  79. ChildNode child;
  80. child.name = E;
  81. child.node = cn[E];
  82. r_child_nodes->push_back(child);
  83. }
  84. }
  85. }
  86. void AnimationNode::blend_animation(const StringName &p_animation, double p_time, double p_delta, bool p_seeked, bool p_is_external_seeking, real_t p_blend, Animation::LoopedFlag p_looped_flag) {
  87. ERR_FAIL_COND(!state);
  88. ERR_FAIL_COND(!state->player->has_animation(p_animation));
  89. Ref<Animation> animation = state->player->get_animation(p_animation);
  90. if (animation.is_null()) {
  91. AnimationNodeBlendTree *btree = Object::cast_to<AnimationNodeBlendTree>(parent);
  92. if (btree) {
  93. String node_name = btree->get_node_name(Ref<AnimationNodeAnimation>(this));
  94. make_invalid(vformat(RTR("In node '%s', invalid animation: '%s'."), node_name, p_animation));
  95. } else {
  96. make_invalid(vformat(RTR("Invalid animation: '%s'."), p_animation));
  97. }
  98. return;
  99. }
  100. ERR_FAIL_COND(!animation.is_valid());
  101. AnimationState anim_state;
  102. anim_state.blend = p_blend;
  103. anim_state.track_blends = blends;
  104. anim_state.delta = p_delta;
  105. anim_state.time = p_time;
  106. anim_state.animation = animation;
  107. anim_state.seeked = p_seeked;
  108. anim_state.looped_flag = p_looped_flag;
  109. anim_state.is_external_seeking = p_is_external_seeking;
  110. state->animation_states.push_back(anim_state);
  111. }
  112. double AnimationNode::_pre_process(const StringName &p_base_path, AnimationNode *p_parent, State *p_state, double p_time, bool p_seek, bool p_is_external_seeking, const Vector<StringName> &p_connections, bool p_test_only) {
  113. base_path = p_base_path;
  114. parent = p_parent;
  115. connections = p_connections;
  116. state = p_state;
  117. double t = process(p_time, p_seek, p_is_external_seeking, p_test_only);
  118. state = nullptr;
  119. parent = nullptr;
  120. base_path = StringName();
  121. connections.clear();
  122. return t;
  123. }
  124. AnimationTree *AnimationNode::get_animation_tree() const {
  125. ERR_FAIL_COND_V(!state, nullptr);
  126. return state->tree;
  127. }
  128. void AnimationNode::make_invalid(const String &p_reason) {
  129. ERR_FAIL_COND(!state);
  130. state->valid = false;
  131. if (!state->invalid_reasons.is_empty()) {
  132. state->invalid_reasons += "\n";
  133. }
  134. state->invalid_reasons += String::utf8("• ") + p_reason;
  135. }
  136. double AnimationNode::blend_input(int p_input, double p_time, bool p_seek, bool p_is_external_seeking, real_t p_blend, FilterAction p_filter, bool p_sync, bool p_test_only) {
  137. ERR_FAIL_INDEX_V(p_input, inputs.size(), 0);
  138. ERR_FAIL_COND_V(!state, 0);
  139. AnimationNodeBlendTree *blend_tree = Object::cast_to<AnimationNodeBlendTree>(parent);
  140. ERR_FAIL_COND_V(!blend_tree, 0);
  141. StringName node_name = connections[p_input];
  142. if (!blend_tree->has_node(node_name)) {
  143. String node_name2 = blend_tree->get_node_name(Ref<AnimationNode>(this));
  144. make_invalid(vformat(RTR("Nothing connected to input '%s' of node '%s'."), get_input_name(p_input), node_name2));
  145. return 0;
  146. }
  147. Ref<AnimationNode> node = blend_tree->get_node(node_name);
  148. //inputs.write[p_input].last_pass = state->last_pass;
  149. real_t activity = 0.0;
  150. double ret = _blend_node(node_name, blend_tree->get_node_connection_array(node_name), nullptr, node, p_time, p_seek, p_is_external_seeking, p_blend, p_filter, p_sync, &activity, p_test_only);
  151. Vector<AnimationTree::Activity> *activity_ptr = state->tree->input_activity_map.getptr(base_path);
  152. if (activity_ptr && p_input < activity_ptr->size()) {
  153. activity_ptr->write[p_input].last_pass = state->last_pass;
  154. activity_ptr->write[p_input].activity = activity;
  155. }
  156. return ret;
  157. }
  158. double AnimationNode::blend_node(const StringName &p_sub_path, Ref<AnimationNode> p_node, double p_time, bool p_seek, bool p_is_external_seeking, real_t p_blend, FilterAction p_filter, bool p_sync, bool p_test_only) {
  159. return _blend_node(p_sub_path, Vector<StringName>(), this, p_node, p_time, p_seek, p_is_external_seeking, p_blend, p_filter, p_sync, nullptr, p_test_only);
  160. }
  161. double AnimationNode::_blend_node(const StringName &p_subpath, const Vector<StringName> &p_connections, AnimationNode *p_new_parent, Ref<AnimationNode> p_node, double p_time, bool p_seek, bool p_is_external_seeking, real_t p_blend, FilterAction p_filter, bool p_sync, real_t *r_max, bool p_test_only) {
  162. ERR_FAIL_COND_V(!p_node.is_valid(), 0);
  163. ERR_FAIL_COND_V(!state, 0);
  164. int blend_count = blends.size();
  165. if (p_node->blends.size() != blend_count) {
  166. p_node->blends.resize(blend_count);
  167. }
  168. real_t *blendw = p_node->blends.ptrw();
  169. const real_t *blendr = blends.ptr();
  170. bool any_valid = false;
  171. if (has_filter() && is_filter_enabled() && p_filter != FILTER_IGNORE) {
  172. for (int i = 0; i < blend_count; i++) {
  173. blendw[i] = 0.0; //all to zero by default
  174. }
  175. for (const KeyValue<NodePath, bool> &E : filter) {
  176. if (!state->track_map.has(E.key)) {
  177. continue;
  178. }
  179. int idx = state->track_map[E.key];
  180. blendw[idx] = 1.0; //filtered goes to one
  181. }
  182. switch (p_filter) {
  183. case FILTER_IGNORE:
  184. break; //will not happen anyway
  185. case FILTER_PASS: {
  186. //values filtered pass, the rest don't
  187. for (int i = 0; i < blend_count; i++) {
  188. if (blendw[i] == 0) { //not filtered, does not pass
  189. continue;
  190. }
  191. blendw[i] = blendr[i] * p_blend;
  192. if (!Math::is_zero_approx(blendw[i])) {
  193. any_valid = true;
  194. }
  195. }
  196. } break;
  197. case FILTER_STOP: {
  198. //values filtered don't pass, the rest are blended
  199. for (int i = 0; i < blend_count; i++) {
  200. if (blendw[i] > 0) { //filtered, does not pass
  201. continue;
  202. }
  203. blendw[i] = blendr[i] * p_blend;
  204. if (!Math::is_zero_approx(blendw[i])) {
  205. any_valid = true;
  206. }
  207. }
  208. } break;
  209. case FILTER_BLEND: {
  210. //filtered values are blended, the rest are passed without blending
  211. for (int i = 0; i < blend_count; i++) {
  212. if (blendw[i] == 1.0) {
  213. blendw[i] = blendr[i] * p_blend; //filtered, blend
  214. } else {
  215. blendw[i] = blendr[i]; //not filtered, do not blend
  216. }
  217. if (!Math::is_zero_approx(blendw[i])) {
  218. any_valid = true;
  219. }
  220. }
  221. } break;
  222. }
  223. } else {
  224. for (int i = 0; i < blend_count; i++) {
  225. //regular blend
  226. blendw[i] = blendr[i] * p_blend;
  227. if (!Math::is_zero_approx(blendw[i])) {
  228. any_valid = true;
  229. }
  230. }
  231. }
  232. if (r_max) {
  233. *r_max = 0;
  234. for (int i = 0; i < blend_count; i++) {
  235. *r_max = MAX(*r_max, blendw[i]);
  236. }
  237. }
  238. String new_path;
  239. AnimationNode *new_parent;
  240. // This is the slowest part of processing, but as strings process in powers of 2, and the paths always exist, it will not result in that many allocations.
  241. if (p_new_parent) {
  242. new_parent = p_new_parent;
  243. new_path = String(base_path) + String(p_subpath) + "/";
  244. } else {
  245. ERR_FAIL_COND_V(!parent, 0);
  246. new_parent = parent;
  247. new_path = String(parent->base_path) + String(p_subpath) + "/";
  248. }
  249. // This process, which depends on p_sync is needed to process sync correctly in the case of
  250. // that a synced AnimationNodeSync exists under the un-synced AnimationNodeSync.
  251. if (!p_seek && !p_sync && !any_valid) {
  252. return p_node->_pre_process(new_path, new_parent, state, 0, p_seek, p_is_external_seeking, p_connections, p_test_only);
  253. }
  254. return p_node->_pre_process(new_path, new_parent, state, p_time, p_seek, p_is_external_seeking, p_connections, p_test_only);
  255. }
  256. String AnimationNode::get_caption() const {
  257. String ret = "Node";
  258. GDVIRTUAL_CALL(_get_caption, ret);
  259. return ret;
  260. }
  261. bool AnimationNode::add_input(const String &p_name) {
  262. //root nodes can't add inputs
  263. ERR_FAIL_COND_V(Object::cast_to<AnimationRootNode>(this) != nullptr, false);
  264. Input input;
  265. ERR_FAIL_COND_V(p_name.contains(".") || p_name.contains("/"), false);
  266. input.name = p_name;
  267. inputs.push_back(input);
  268. emit_changed();
  269. return true;
  270. }
  271. void AnimationNode::remove_input(int p_index) {
  272. ERR_FAIL_INDEX(p_index, inputs.size());
  273. inputs.remove_at(p_index);
  274. emit_changed();
  275. }
  276. bool AnimationNode::set_input_name(int p_input, const String &p_name) {
  277. ERR_FAIL_INDEX_V(p_input, inputs.size(), false);
  278. ERR_FAIL_COND_V(p_name.contains(".") || p_name.contains("/"), false);
  279. inputs.write[p_input].name = p_name;
  280. emit_changed();
  281. return true;
  282. }
  283. String AnimationNode::get_input_name(int p_input) const {
  284. ERR_FAIL_INDEX_V(p_input, inputs.size(), String());
  285. return inputs[p_input].name;
  286. }
  287. int AnimationNode::get_input_count() const {
  288. return inputs.size();
  289. }
  290. int AnimationNode::find_input(const String &p_name) const {
  291. int idx = -1;
  292. for (int i = 0; i < inputs.size(); i++) {
  293. if (inputs[i].name == p_name) {
  294. idx = i;
  295. break;
  296. }
  297. }
  298. return idx;
  299. }
  300. double AnimationNode::process(double p_time, bool p_seek, bool p_is_external_seeking, bool p_test_only) {
  301. is_testing = p_test_only;
  302. return _process(p_time, p_seek, p_is_external_seeking, p_test_only);
  303. }
  304. double AnimationNode::_process(double p_time, bool p_seek, bool p_is_external_seeking, bool p_test_only) {
  305. double ret = 0;
  306. GDVIRTUAL_CALL(_process, p_time, p_seek, p_is_external_seeking, p_test_only, ret);
  307. return ret;
  308. }
  309. void AnimationNode::set_filter_path(const NodePath &p_path, bool p_enable) {
  310. if (p_enable) {
  311. filter[p_path] = true;
  312. } else {
  313. filter.erase(p_path);
  314. }
  315. }
  316. void AnimationNode::set_filter_enabled(bool p_enable) {
  317. filter_enabled = p_enable;
  318. }
  319. bool AnimationNode::is_filter_enabled() const {
  320. return filter_enabled;
  321. }
  322. bool AnimationNode::is_path_filtered(const NodePath &p_path) const {
  323. return filter.has(p_path);
  324. }
  325. bool AnimationNode::has_filter() const {
  326. bool ret = false;
  327. GDVIRTUAL_CALL(_has_filter, ret);
  328. return ret;
  329. }
  330. Array AnimationNode::_get_filters() const {
  331. Array paths;
  332. for (const KeyValue<NodePath, bool> &E : filter) {
  333. paths.push_back(String(E.key)); //use strings, so sorting is possible
  334. }
  335. paths.sort(); //done so every time the scene is saved, it does not change
  336. return paths;
  337. }
  338. void AnimationNode::_set_filters(const Array &p_filters) {
  339. filter.clear();
  340. for (int i = 0; i < p_filters.size(); i++) {
  341. set_filter_path(p_filters[i], true);
  342. }
  343. }
  344. void AnimationNode::_validate_property(PropertyInfo &p_property) const {
  345. if (!has_filter() && (p_property.name == "filter_enabled" || p_property.name == "filters")) {
  346. p_property.usage = PROPERTY_USAGE_NONE;
  347. }
  348. }
  349. Ref<AnimationNode> AnimationNode::get_child_by_name(const StringName &p_name) const {
  350. Ref<AnimationNode> ret;
  351. GDVIRTUAL_CALL(_get_child_by_name, p_name, ret);
  352. return ret;
  353. }
  354. Ref<AnimationNode> AnimationNode::find_node_by_path(const String &p_name) const {
  355. Vector<String> split = p_name.split("/");
  356. Ref<AnimationNode> ret = const_cast<AnimationNode *>(this);
  357. for (int i = 0; i < split.size(); i++) {
  358. ret = ret->get_child_by_name(split[i]);
  359. if (!ret.is_valid()) {
  360. break;
  361. }
  362. }
  363. return ret;
  364. }
  365. void AnimationNode::_bind_methods() {
  366. ClassDB::bind_method(D_METHOD("add_input", "name"), &AnimationNode::add_input);
  367. ClassDB::bind_method(D_METHOD("remove_input", "index"), &AnimationNode::remove_input);
  368. ClassDB::bind_method(D_METHOD("set_input_name", "input", "name"), &AnimationNode::set_input_name);
  369. ClassDB::bind_method(D_METHOD("get_input_name", "input"), &AnimationNode::get_input_name);
  370. ClassDB::bind_method(D_METHOD("get_input_count"), &AnimationNode::get_input_count);
  371. ClassDB::bind_method(D_METHOD("find_input", "name"), &AnimationNode::find_input);
  372. ClassDB::bind_method(D_METHOD("set_filter_path", "path", "enable"), &AnimationNode::set_filter_path);
  373. ClassDB::bind_method(D_METHOD("is_path_filtered", "path"), &AnimationNode::is_path_filtered);
  374. ClassDB::bind_method(D_METHOD("set_filter_enabled", "enable"), &AnimationNode::set_filter_enabled);
  375. ClassDB::bind_method(D_METHOD("is_filter_enabled"), &AnimationNode::is_filter_enabled);
  376. ClassDB::bind_method(D_METHOD("_set_filters", "filters"), &AnimationNode::_set_filters);
  377. ClassDB::bind_method(D_METHOD("_get_filters"), &AnimationNode::_get_filters);
  378. ClassDB::bind_method(D_METHOD("blend_animation", "animation", "time", "delta", "seeked", "is_external_seeking", "blend", "looped_flag"), &AnimationNode::blend_animation, DEFVAL(Animation::LOOPED_FLAG_NONE));
  379. ClassDB::bind_method(D_METHOD("blend_node", "name", "node", "time", "seek", "is_external_seeking", "blend", "filter", "sync", "test_only"), &AnimationNode::blend_node, DEFVAL(FILTER_IGNORE), DEFVAL(true), DEFVAL(false));
  380. ClassDB::bind_method(D_METHOD("blend_input", "input_index", "time", "seek", "is_external_seeking", "blend", "filter", "sync", "test_only"), &AnimationNode::blend_input, DEFVAL(FILTER_IGNORE), DEFVAL(true), DEFVAL(false));
  381. ClassDB::bind_method(D_METHOD("set_parameter", "name", "value"), &AnimationNode::set_parameter);
  382. ClassDB::bind_method(D_METHOD("get_parameter", "name"), &AnimationNode::get_parameter);
  383. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_filter_enabled", "is_filter_enabled");
  384. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "filters", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_filters", "_get_filters");
  385. GDVIRTUAL_BIND(_get_child_nodes);
  386. GDVIRTUAL_BIND(_get_parameter_list);
  387. GDVIRTUAL_BIND(_get_child_by_name, "name");
  388. GDVIRTUAL_BIND(_get_parameter_default_value, "parameter");
  389. GDVIRTUAL_BIND(_is_parameter_read_only, "parameter");
  390. GDVIRTUAL_BIND(_process, "time", "seek", "is_external_seeking", "test_only");
  391. GDVIRTUAL_BIND(_get_caption);
  392. GDVIRTUAL_BIND(_has_filter);
  393. ADD_SIGNAL(MethodInfo("tree_changed"));
  394. ADD_SIGNAL(MethodInfo("animation_node_renamed", PropertyInfo(Variant::INT, "object_id"), PropertyInfo(Variant::STRING, "old_name"), PropertyInfo(Variant::STRING, "new_name")));
  395. ADD_SIGNAL(MethodInfo("animation_node_removed", PropertyInfo(Variant::INT, "object_id"), PropertyInfo(Variant::STRING, "name")));
  396. BIND_ENUM_CONSTANT(FILTER_IGNORE);
  397. BIND_ENUM_CONSTANT(FILTER_PASS);
  398. BIND_ENUM_CONSTANT(FILTER_STOP);
  399. BIND_ENUM_CONSTANT(FILTER_BLEND);
  400. }
  401. AnimationNode::AnimationNode() {
  402. }
  403. ////////////////////
  404. void AnimationRootNode::_tree_changed() {
  405. emit_signal(SNAME("tree_changed"));
  406. }
  407. void AnimationRootNode::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
  408. emit_signal(SNAME("animation_node_renamed"), p_oid, p_old_name, p_new_name);
  409. }
  410. void AnimationRootNode::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
  411. emit_signal(SNAME("animation_node_removed"), p_oid, p_node);
  412. }
  413. ////////////////////
  414. void AnimationTree::set_tree_root(const Ref<AnimationNode> &p_root) {
  415. if (root.is_valid()) {
  416. root->disconnect("tree_changed", callable_mp(this, &AnimationTree::_tree_changed));
  417. root->disconnect("animation_node_renamed", callable_mp(this, &AnimationTree::_animation_node_renamed));
  418. root->disconnect("animation_node_removed", callable_mp(this, &AnimationTree::_animation_node_removed));
  419. }
  420. root = p_root;
  421. if (root.is_valid()) {
  422. root->connect("tree_changed", callable_mp(this, &AnimationTree::_tree_changed));
  423. root->connect("animation_node_renamed", callable_mp(this, &AnimationTree::_animation_node_renamed));
  424. root->connect("animation_node_removed", callable_mp(this, &AnimationTree::_animation_node_removed));
  425. }
  426. properties_dirty = true;
  427. update_configuration_warnings();
  428. }
  429. Ref<AnimationNode> AnimationTree::get_tree_root() const {
  430. return root;
  431. }
  432. void AnimationTree::set_active(bool p_active) {
  433. if (active == p_active) {
  434. return;
  435. }
  436. active = p_active;
  437. started = active;
  438. if (process_callback == ANIMATION_PROCESS_IDLE) {
  439. set_process_internal(active);
  440. } else {
  441. set_physics_process_internal(active);
  442. }
  443. if (!active && is_inside_tree()) {
  444. _clear_caches();
  445. }
  446. }
  447. bool AnimationTree::is_active() const {
  448. return active;
  449. }
  450. void AnimationTree::set_process_callback(AnimationProcessCallback p_mode) {
  451. if (process_callback == p_mode) {
  452. return;
  453. }
  454. bool was_active = is_active();
  455. if (was_active) {
  456. set_active(false);
  457. }
  458. process_callback = p_mode;
  459. if (was_active) {
  460. set_active(true);
  461. }
  462. }
  463. AnimationTree::AnimationProcessCallback AnimationTree::get_process_callback() const {
  464. return process_callback;
  465. }
  466. void AnimationTree::_node_removed(Node *p_node) {
  467. cache_valid = false;
  468. }
  469. bool AnimationTree::_update_caches(AnimationPlayer *player) {
  470. setup_pass++;
  471. if (!player->has_node(player->get_root())) {
  472. ERR_PRINT("AnimationTree: AnimationPlayer root is invalid.");
  473. set_active(false);
  474. _clear_caches();
  475. return false;
  476. }
  477. Node *parent = player->get_node(player->get_root());
  478. List<StringName> sname;
  479. player->get_animation_list(&sname);
  480. root_motion_cache.loc = Vector3(0, 0, 0);
  481. root_motion_cache.rot = Quaternion(0, 0, 0, 1);
  482. root_motion_cache.scale = Vector3(1, 1, 1);
  483. Ref<Animation> reset_anim;
  484. bool has_reset_anim = player->has_animation(SceneStringNames::get_singleton()->RESET);
  485. if (has_reset_anim) {
  486. reset_anim = player->get_animation(SceneStringNames::get_singleton()->RESET);
  487. }
  488. for (const StringName &E : sname) {
  489. Ref<Animation> anim = player->get_animation(E);
  490. for (int i = 0; i < anim->get_track_count(); i++) {
  491. NodePath path = anim->track_get_path(i);
  492. Animation::TrackType track_type = anim->track_get_type(i);
  493. Animation::TrackType track_cache_type = track_type;
  494. if (track_cache_type == Animation::TYPE_POSITION_3D || track_cache_type == Animation::TYPE_ROTATION_3D || track_cache_type == Animation::TYPE_SCALE_3D) {
  495. track_cache_type = Animation::TYPE_POSITION_3D; //reference them as position3D tracks, even if they modify rotation or scale
  496. }
  497. TrackCache *track = nullptr;
  498. if (track_cache.has(path)) {
  499. track = track_cache.get(path);
  500. }
  501. //if not valid, delete track
  502. if (track && (track->type != track_cache_type || ObjectDB::get_instance(track->object_id) == nullptr)) {
  503. playing_caches.erase(track);
  504. memdelete(track);
  505. track_cache.erase(path);
  506. track = nullptr;
  507. }
  508. if (!track) {
  509. Ref<Resource> resource;
  510. Vector<StringName> leftover_path;
  511. Node *child = parent->get_node_and_resource(path, resource, leftover_path);
  512. if (!child) {
  513. ERR_PRINT("AnimationTree: '" + String(E) + "', couldn't resolve track: '" + String(path) + "'");
  514. continue;
  515. }
  516. if (!child->is_connected("tree_exited", callable_mp(this, &AnimationTree::_node_removed))) {
  517. child->connect("tree_exited", callable_mp(this, &AnimationTree::_node_removed).bind(child));
  518. }
  519. switch (track_type) {
  520. case Animation::TYPE_VALUE: {
  521. TrackCacheValue *track_value = memnew(TrackCacheValue);
  522. if (resource.is_valid()) {
  523. track_value->object = resource.ptr();
  524. } else {
  525. track_value->object = child;
  526. }
  527. track_value->is_discrete = anim->value_track_get_update_mode(i) == Animation::UPDATE_DISCRETE;
  528. track_value->is_using_angle = anim->track_get_interpolation_type(i) == Animation::INTERPOLATION_LINEAR_ANGLE || anim->track_get_interpolation_type(i) == Animation::INTERPOLATION_CUBIC_ANGLE;
  529. track_value->subpath = leftover_path;
  530. track_value->object_id = track_value->object->get_instance_id();
  531. track = track_value;
  532. // If a value track without a key is cached first, the initial value cannot be determined.
  533. // It is a corner case, but which may cause problems with blending.
  534. ERR_CONTINUE_MSG(anim->track_get_key_count(i) == 0, "AnimationTree: '" + String(E) + "', Value Track: '" + String(path) + "' must have at least one key to cache for blending.");
  535. track_value->init_value = anim->track_get_key_value(i, 0);
  536. track_value->init_value.zero();
  537. // If there is a Reset Animation, it takes precedence by overwriting.
  538. if (has_reset_anim) {
  539. int rt = reset_anim->find_track(path, track_type);
  540. if (rt >= 0 && reset_anim->track_get_key_count(rt) > 0) {
  541. track_value->init_value = reset_anim->track_get_key_value(rt, 0);
  542. }
  543. }
  544. } break;
  545. case Animation::TYPE_POSITION_3D:
  546. case Animation::TYPE_ROTATION_3D:
  547. case Animation::TYPE_SCALE_3D: {
  548. #ifndef _3D_DISABLED
  549. Node3D *node_3d = Object::cast_to<Node3D>(child);
  550. if (!node_3d) {
  551. ERR_PRINT("AnimationTree: '" + String(E) + "', transform track does not point to Node3D: '" + String(path) + "'");
  552. continue;
  553. }
  554. TrackCacheTransform *track_xform = memnew(TrackCacheTransform);
  555. track_xform->type = Animation::TYPE_POSITION_3D;
  556. track_xform->node_3d = node_3d;
  557. track_xform->skeleton = nullptr;
  558. track_xform->bone_idx = -1;
  559. bool has_rest = false;
  560. if (path.get_subname_count() == 1 && Object::cast_to<Skeleton3D>(node_3d)) {
  561. Skeleton3D *sk = Object::cast_to<Skeleton3D>(node_3d);
  562. track_xform->skeleton = sk;
  563. int bone_idx = sk->find_bone(path.get_subname(0));
  564. if (bone_idx != -1) {
  565. has_rest = true;
  566. track_xform->bone_idx = bone_idx;
  567. Transform3D rest = sk->get_bone_rest(bone_idx);
  568. track_xform->init_loc = rest.origin;
  569. track_xform->init_rot = rest.basis.get_rotation_quaternion();
  570. track_xform->init_scale = rest.basis.get_scale();
  571. }
  572. }
  573. track_xform->object = node_3d;
  574. track_xform->object_id = track_xform->object->get_instance_id();
  575. track = track_xform;
  576. switch (track_type) {
  577. case Animation::TYPE_POSITION_3D: {
  578. track_xform->loc_used = true;
  579. } break;
  580. case Animation::TYPE_ROTATION_3D: {
  581. track_xform->rot_used = true;
  582. } break;
  583. case Animation::TYPE_SCALE_3D: {
  584. track_xform->scale_used = true;
  585. } break;
  586. default: {
  587. }
  588. }
  589. // For non Skeleton3D bone animation.
  590. if (has_reset_anim && !has_rest) {
  591. int rt = reset_anim->find_track(path, track_type);
  592. if (rt >= 0 && reset_anim->track_get_key_count(rt) > 0) {
  593. switch (track_type) {
  594. case Animation::TYPE_POSITION_3D: {
  595. track_xform->init_loc = reset_anim->track_get_key_value(rt, 0);
  596. } break;
  597. case Animation::TYPE_ROTATION_3D: {
  598. track_xform->init_rot = reset_anim->track_get_key_value(rt, 0);
  599. } break;
  600. case Animation::TYPE_SCALE_3D: {
  601. track_xform->init_scale = reset_anim->track_get_key_value(rt, 0);
  602. } break;
  603. default: {
  604. }
  605. }
  606. }
  607. }
  608. #endif // _3D_DISABLED
  609. } break;
  610. case Animation::TYPE_BLEND_SHAPE: {
  611. #ifndef _3D_DISABLED
  612. if (path.get_subname_count() != 1) {
  613. ERR_PRINT("AnimationTree: '" + String(E) + "', blend shape track does not contain a blend shape subname: '" + String(path) + "'");
  614. continue;
  615. }
  616. MeshInstance3D *mesh_3d = Object::cast_to<MeshInstance3D>(child);
  617. if (!mesh_3d) {
  618. ERR_PRINT("AnimationTree: '" + String(E) + "', blend shape track does not point to MeshInstance3D: '" + String(path) + "'");
  619. continue;
  620. }
  621. StringName blend_shape_name = path.get_subname(0);
  622. int blend_shape_idx = mesh_3d->find_blend_shape_by_name(blend_shape_name);
  623. if (blend_shape_idx == -1) {
  624. ERR_PRINT("AnimationTree: '" + String(E) + "', blend shape track points to a non-existing name: '" + String(blend_shape_name) + "'");
  625. continue;
  626. }
  627. TrackCacheBlendShape *track_bshape = memnew(TrackCacheBlendShape);
  628. track_bshape->mesh_3d = mesh_3d;
  629. track_bshape->shape_index = blend_shape_idx;
  630. track_bshape->object = mesh_3d;
  631. track_bshape->object_id = mesh_3d->get_instance_id();
  632. track = track_bshape;
  633. if (has_reset_anim) {
  634. int rt = reset_anim->find_track(path, track_type);
  635. if (rt >= 0 && reset_anim->track_get_key_count(rt) > 0) {
  636. track_bshape->init_value = reset_anim->track_get_key_value(rt, 0);
  637. }
  638. }
  639. #endif
  640. } break;
  641. case Animation::TYPE_METHOD: {
  642. TrackCacheMethod *track_method = memnew(TrackCacheMethod);
  643. if (resource.is_valid()) {
  644. track_method->object = resource.ptr();
  645. } else {
  646. track_method->object = child;
  647. }
  648. track_method->object_id = track_method->object->get_instance_id();
  649. track = track_method;
  650. } break;
  651. case Animation::TYPE_BEZIER: {
  652. TrackCacheBezier *track_bezier = memnew(TrackCacheBezier);
  653. if (resource.is_valid()) {
  654. track_bezier->object = resource.ptr();
  655. } else {
  656. track_bezier->object = child;
  657. }
  658. track_bezier->subpath = leftover_path;
  659. track_bezier->object_id = track_bezier->object->get_instance_id();
  660. track = track_bezier;
  661. if (has_reset_anim) {
  662. int rt = reset_anim->find_track(path, track_type);
  663. if (rt >= 0 && reset_anim->track_get_key_count(rt) > 0) {
  664. track_bezier->init_value = (reset_anim->track_get_key_value(rt, 0).operator Array())[0];
  665. }
  666. }
  667. } break;
  668. case Animation::TYPE_AUDIO: {
  669. TrackCacheAudio *track_audio = memnew(TrackCacheAudio);
  670. track_audio->object = child;
  671. track_audio->object_id = track_audio->object->get_instance_id();
  672. track_audio->audio_stream.instantiate();
  673. track_audio->audio_stream->set_polyphony(audio_max_polyphony);
  674. track = track_audio;
  675. } break;
  676. case Animation::TYPE_ANIMATION: {
  677. TrackCacheAnimation *track_animation = memnew(TrackCacheAnimation);
  678. track_animation->object = child;
  679. track_animation->object_id = track_animation->object->get_instance_id();
  680. track = track_animation;
  681. } break;
  682. default: {
  683. ERR_PRINT("Animation corrupted (invalid track type)");
  684. continue;
  685. }
  686. }
  687. track_cache[path] = track;
  688. } else if (track_cache_type == Animation::TYPE_POSITION_3D) {
  689. TrackCacheTransform *track_xform = static_cast<TrackCacheTransform *>(track);
  690. if (track->setup_pass != setup_pass) {
  691. track_xform->loc_used = false;
  692. track_xform->rot_used = false;
  693. track_xform->scale_used = false;
  694. }
  695. switch (track_type) {
  696. case Animation::TYPE_POSITION_3D: {
  697. track_xform->loc_used = true;
  698. } break;
  699. case Animation::TYPE_ROTATION_3D: {
  700. track_xform->rot_used = true;
  701. } break;
  702. case Animation::TYPE_SCALE_3D: {
  703. track_xform->scale_used = true;
  704. } break;
  705. default: {
  706. }
  707. }
  708. } else if (track_cache_type == Animation::TYPE_VALUE) {
  709. // If it has at least one angle interpolation, it also uses angle interpolation for blending.
  710. TrackCacheValue *track_value = static_cast<TrackCacheValue *>(track);
  711. bool was_discrete = track_value->is_discrete;
  712. bool was_using_angle = track_value->is_using_angle;
  713. track_value->is_discrete |= anim->value_track_get_update_mode(i) == Animation::UPDATE_DISCRETE;
  714. track_value->is_using_angle |= anim->track_get_interpolation_type(i) == Animation::INTERPOLATION_LINEAR_ANGLE || anim->track_get_interpolation_type(i) == Animation::INTERPOLATION_CUBIC_ANGLE;
  715. if (was_discrete != track_value->is_discrete) {
  716. ERR_PRINT_ED("Value Track: " + String(path) + " with different update modes are blended. Blending prioritizes Discrete mode, so other update mode tracks will not be blended.");
  717. }
  718. if (was_using_angle != track_value->is_using_angle) {
  719. WARN_PRINT_ED("Value Track: " + String(path) + " with different interpolation types for rotation are blended. Blending prioritizes angle interpolation, so the blending result uses the shortest path referenced to the initial (RESET animation) value.");
  720. }
  721. }
  722. track->setup_pass = setup_pass;
  723. }
  724. }
  725. List<NodePath> to_delete;
  726. for (const KeyValue<NodePath, TrackCache *> &K : track_cache) {
  727. TrackCache *tc = track_cache[K.key];
  728. if (tc->setup_pass != setup_pass) {
  729. to_delete.push_back(K.key);
  730. }
  731. }
  732. while (to_delete.front()) {
  733. NodePath np = to_delete.front()->get();
  734. memdelete(track_cache[np]);
  735. track_cache.erase(np);
  736. to_delete.pop_front();
  737. }
  738. state.track_map.clear();
  739. int idx = 0;
  740. for (const KeyValue<NodePath, TrackCache *> &K : track_cache) {
  741. state.track_map[K.key] = idx;
  742. idx++;
  743. }
  744. state.track_count = idx;
  745. cache_valid = true;
  746. return true;
  747. }
  748. void AnimationTree::_animation_player_changed() {
  749. emit_signal(SNAME("animation_player_changed"));
  750. _clear_caches();
  751. }
  752. void AnimationTree::_clear_caches() {
  753. _clear_audio_streams();
  754. _clear_playing_caches();
  755. for (KeyValue<NodePath, TrackCache *> &K : track_cache) {
  756. memdelete(K.value);
  757. }
  758. track_cache.clear();
  759. cache_valid = false;
  760. }
  761. void AnimationTree::_clear_audio_streams() {
  762. for (int i = 0; i < playing_audio_stream_players.size(); i++) {
  763. playing_audio_stream_players[i]->call(SNAME("stop"));
  764. playing_audio_stream_players[i]->call(SNAME("set_stream"), Ref<AudioStream>());
  765. }
  766. playing_audio_stream_players.clear();
  767. }
  768. void AnimationTree::_clear_playing_caches() {
  769. for (const TrackCache *E : playing_caches) {
  770. if (ObjectDB::get_instance(E->object_id)) {
  771. E->object->call(SNAME("stop"));
  772. }
  773. }
  774. playing_caches.clear();
  775. }
  776. static void _call_object(Object *p_object, const StringName &p_method, const Vector<Variant> &p_params, bool p_deferred) {
  777. // Separate function to use alloca() more efficiently
  778. const Variant **argptrs = (const Variant **)alloca(sizeof(const Variant **) * p_params.size());
  779. const Variant *args = p_params.ptr();
  780. uint32_t argcount = p_params.size();
  781. for (uint32_t i = 0; i < argcount; i++) {
  782. argptrs[i] = &args[i];
  783. }
  784. if (p_deferred) {
  785. MessageQueue::get_singleton()->push_callp(p_object, p_method, argptrs, argcount);
  786. } else {
  787. Callable::CallError ce;
  788. p_object->callp(p_method, argptrs, argcount, ce);
  789. }
  790. }
  791. void AnimationTree::_process_graph(double p_delta) {
  792. _update_properties(); //if properties need updating, update them
  793. //check all tracks, see if they need modification
  794. root_motion_position = Vector3(0, 0, 0);
  795. root_motion_rotation = Quaternion(0, 0, 0, 1);
  796. root_motion_scale = Vector3(0, 0, 0);
  797. if (!root.is_valid()) {
  798. ERR_PRINT("AnimationTree: root AnimationNode is not set, disabling playback.");
  799. set_active(false);
  800. cache_valid = false;
  801. return;
  802. }
  803. if (!has_node(animation_player)) {
  804. ERR_PRINT("AnimationTree: no valid AnimationPlayer path set, disabling playback");
  805. set_active(false);
  806. cache_valid = false;
  807. return;
  808. }
  809. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(get_node(animation_player));
  810. ObjectID current_animation_player;
  811. if (player) {
  812. current_animation_player = player->get_instance_id();
  813. }
  814. if (last_animation_player != current_animation_player) {
  815. if (last_animation_player.is_valid()) {
  816. Object *old_player = ObjectDB::get_instance(last_animation_player);
  817. if (old_player) {
  818. old_player->disconnect("caches_cleared", callable_mp(this, &AnimationTree::_clear_caches));
  819. }
  820. }
  821. if (player) {
  822. player->connect("caches_cleared", callable_mp(this, &AnimationTree::_clear_caches));
  823. }
  824. last_animation_player = current_animation_player;
  825. }
  826. if (!player) {
  827. ERR_PRINT("AnimationTree: path points to a node not an AnimationPlayer, disabling playback");
  828. set_active(false);
  829. cache_valid = false;
  830. return;
  831. }
  832. if (!cache_valid) {
  833. if (!_update_caches(player)) {
  834. return;
  835. }
  836. }
  837. { //setup
  838. process_pass++;
  839. state.valid = true;
  840. state.invalid_reasons = "";
  841. state.animation_states.clear(); //will need to be re-created
  842. state.player = player;
  843. state.last_pass = process_pass;
  844. state.tree = this;
  845. // root source blends
  846. root->blends.resize(state.track_count);
  847. real_t *src_blendsw = root->blends.ptrw();
  848. for (int i = 0; i < state.track_count; i++) {
  849. src_blendsw[i] = 1.0; //by default all go to 1 for the root input
  850. }
  851. }
  852. //process
  853. {
  854. if (started) {
  855. //if started, seek
  856. root->_pre_process(SceneStringNames::get_singleton()->parameters_base_path, nullptr, &state, 0, true, false, Vector<StringName>());
  857. started = false;
  858. }
  859. root->_pre_process(SceneStringNames::get_singleton()->parameters_base_path, nullptr, &state, p_delta, false, false, Vector<StringName>());
  860. }
  861. if (!state.valid) {
  862. return; //state is not valid. do nothing.
  863. }
  864. // Init all value/transform/blend/bezier tracks that track_cache has.
  865. {
  866. for (const KeyValue<NodePath, TrackCache *> &K : track_cache) {
  867. TrackCache *track = K.value;
  868. switch (track->type) {
  869. case Animation::TYPE_POSITION_3D: {
  870. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  871. if (track->root_motion) {
  872. root_motion_cache.loc = Vector3(0, 0, 0);
  873. root_motion_cache.rot = Quaternion(0, 0, 0, 1);
  874. root_motion_cache.scale = Vector3(1, 1, 1);
  875. }
  876. t->loc = t->init_loc;
  877. t->rot = t->init_rot;
  878. t->scale = t->init_scale;
  879. } break;
  880. case Animation::TYPE_BLEND_SHAPE: {
  881. TrackCacheBlendShape *t = static_cast<TrackCacheBlendShape *>(track);
  882. t->value = t->init_value;
  883. } break;
  884. case Animation::TYPE_VALUE: {
  885. TrackCacheValue *t = static_cast<TrackCacheValue *>(track);
  886. t->value = t->init_value;
  887. } break;
  888. case Animation::TYPE_BEZIER: {
  889. TrackCacheBezier *t = static_cast<TrackCacheBezier *>(track);
  890. t->value = t->init_value;
  891. } break;
  892. case Animation::TYPE_AUDIO: {
  893. TrackCacheAudio *t = static_cast<TrackCacheAudio *>(track);
  894. for (KeyValue<ObjectID, PlayingAudioTrackInfo> &L : t->playing_streams) {
  895. PlayingAudioTrackInfo &track_info = L.value;
  896. track_info.volume = 0.0;
  897. }
  898. } break;
  899. default: {
  900. } break;
  901. }
  902. }
  903. }
  904. // Apply value/transform/blend/bezier blends to track caches and execute method/audio/animation tracks.
  905. {
  906. #ifdef TOOLS_ENABLED
  907. bool can_call = is_inside_tree() && !Engine::get_singleton()->is_editor_hint();
  908. #endif // TOOLS_ENABLED
  909. for (const AnimationNode::AnimationState &as : state.animation_states) {
  910. Ref<Animation> a = as.animation;
  911. double time = as.time;
  912. double delta = as.delta;
  913. real_t weight = as.blend;
  914. bool seeked = as.seeked;
  915. Animation::LoopedFlag looped_flag = as.looped_flag;
  916. bool is_external_seeking = as.is_external_seeking;
  917. bool backward = signbit(delta); // This flag is used by the root motion calculates or detecting the end of audio stream.
  918. #ifndef _3D_DISABLED
  919. bool calc_root = !seeked || is_external_seeking;
  920. #endif // _3D_DISABLED
  921. for (int i = 0; i < a->get_track_count(); i++) {
  922. if (!a->track_is_enabled(i)) {
  923. continue;
  924. }
  925. NodePath path = a->track_get_path(i);
  926. if (!track_cache.has(path)) {
  927. continue; // No path, but avoid error spamming.
  928. }
  929. TrackCache *track = track_cache[path];
  930. ERR_CONTINUE(!state.track_map.has(path));
  931. int blend_idx = state.track_map[path];
  932. ERR_CONTINUE(blend_idx < 0 || blend_idx >= state.track_count);
  933. real_t blend = (as.track_blends)[blend_idx] * weight;
  934. Animation::TrackType ttype = a->track_get_type(i);
  935. if (ttype != Animation::TYPE_POSITION_3D && ttype != Animation::TYPE_ROTATION_3D && ttype != Animation::TYPE_SCALE_3D && track->type != ttype) {
  936. //broken animation, but avoid error spamming
  937. continue;
  938. }
  939. track->root_motion = root_motion_track == path;
  940. switch (ttype) {
  941. case Animation::TYPE_POSITION_3D: {
  942. #ifndef _3D_DISABLED
  943. if (Math::is_zero_approx(blend)) {
  944. continue; // Nothing to blend.
  945. }
  946. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  947. if (track->root_motion && calc_root) {
  948. double prev_time = time - delta;
  949. if (!backward) {
  950. if (prev_time < 0) {
  951. switch (a->get_loop_mode()) {
  952. case Animation::LOOP_NONE: {
  953. prev_time = 0;
  954. } break;
  955. case Animation::LOOP_LINEAR: {
  956. prev_time = Math::fposmod(prev_time, (double)a->get_length());
  957. } break;
  958. case Animation::LOOP_PINGPONG: {
  959. prev_time = Math::pingpong(prev_time, (double)a->get_length());
  960. } break;
  961. default:
  962. break;
  963. }
  964. }
  965. } else {
  966. if (prev_time > a->get_length()) {
  967. switch (a->get_loop_mode()) {
  968. case Animation::LOOP_NONE: {
  969. prev_time = (double)a->get_length();
  970. } break;
  971. case Animation::LOOP_LINEAR: {
  972. prev_time = Math::fposmod(prev_time, (double)a->get_length());
  973. } break;
  974. case Animation::LOOP_PINGPONG: {
  975. prev_time = Math::pingpong(prev_time, (double)a->get_length());
  976. } break;
  977. default:
  978. break;
  979. }
  980. }
  981. }
  982. Vector3 loc[2];
  983. if (!backward) {
  984. if (prev_time > time) {
  985. Error err = a->try_position_track_interpolate(i, prev_time, &loc[0]);
  986. if (err != OK) {
  987. continue;
  988. }
  989. loc[0] = post_process_key_value(a, i, loc[0], t->object, t->bone_idx);
  990. a->try_position_track_interpolate(i, (double)a->get_length(), &loc[1]);
  991. loc[1] = post_process_key_value(a, i, loc[1], t->object, t->bone_idx);
  992. root_motion_cache.loc += (loc[1] - loc[0]) * blend;
  993. prev_time = 0;
  994. }
  995. } else {
  996. if (prev_time < time) {
  997. Error err = a->try_position_track_interpolate(i, prev_time, &loc[0]);
  998. if (err != OK) {
  999. continue;
  1000. }
  1001. loc[0] = post_process_key_value(a, i, loc[0], t->object, t->bone_idx);
  1002. a->try_position_track_interpolate(i, 0, &loc[1]);
  1003. loc[1] = post_process_key_value(a, i, loc[1], t->object, t->bone_idx);
  1004. root_motion_cache.loc += (loc[1] - loc[0]) * blend;
  1005. prev_time = (double)a->get_length();
  1006. }
  1007. }
  1008. Error err = a->try_position_track_interpolate(i, prev_time, &loc[0]);
  1009. if (err != OK) {
  1010. continue;
  1011. }
  1012. loc[0] = post_process_key_value(a, i, loc[0], t->object, t->bone_idx);
  1013. a->try_position_track_interpolate(i, time, &loc[1]);
  1014. loc[1] = post_process_key_value(a, i, loc[1], t->object, t->bone_idx);
  1015. root_motion_cache.loc += (loc[1] - loc[0]) * blend;
  1016. prev_time = !backward ? 0 : (double)a->get_length();
  1017. }
  1018. {
  1019. Vector3 loc;
  1020. Error err = a->try_position_track_interpolate(i, time, &loc);
  1021. if (err != OK) {
  1022. continue;
  1023. }
  1024. loc = post_process_key_value(a, i, loc, t->object, t->bone_idx);
  1025. t->loc += (loc - t->init_loc) * blend;
  1026. }
  1027. #endif // _3D_DISABLED
  1028. } break;
  1029. case Animation::TYPE_ROTATION_3D: {
  1030. #ifndef _3D_DISABLED
  1031. if (Math::is_zero_approx(blend)) {
  1032. continue; // Nothing to blend.
  1033. }
  1034. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  1035. if (track->root_motion && calc_root) {
  1036. double prev_time = time - delta;
  1037. if (!backward) {
  1038. if (prev_time < 0) {
  1039. switch (a->get_loop_mode()) {
  1040. case Animation::LOOP_NONE: {
  1041. prev_time = 0;
  1042. } break;
  1043. case Animation::LOOP_LINEAR: {
  1044. prev_time = Math::fposmod(prev_time, (double)a->get_length());
  1045. } break;
  1046. case Animation::LOOP_PINGPONG: {
  1047. prev_time = Math::pingpong(prev_time, (double)a->get_length());
  1048. } break;
  1049. default:
  1050. break;
  1051. }
  1052. }
  1053. } else {
  1054. if (prev_time > a->get_length()) {
  1055. switch (a->get_loop_mode()) {
  1056. case Animation::LOOP_NONE: {
  1057. prev_time = (double)a->get_length();
  1058. } break;
  1059. case Animation::LOOP_LINEAR: {
  1060. prev_time = Math::fposmod(prev_time, (double)a->get_length());
  1061. } break;
  1062. case Animation::LOOP_PINGPONG: {
  1063. prev_time = Math::pingpong(prev_time, (double)a->get_length());
  1064. } break;
  1065. default:
  1066. break;
  1067. }
  1068. }
  1069. }
  1070. Quaternion rot[2];
  1071. if (!backward) {
  1072. if (prev_time > time) {
  1073. Error err = a->try_rotation_track_interpolate(i, prev_time, &rot[0]);
  1074. if (err != OK) {
  1075. continue;
  1076. }
  1077. rot[0] = post_process_key_value(a, i, rot[0], t->object, t->bone_idx);
  1078. a->try_rotation_track_interpolate(i, (double)a->get_length(), &rot[1]);
  1079. rot[1] = post_process_key_value(a, i, rot[1], t->object, t->bone_idx);
  1080. root_motion_cache.rot = (root_motion_cache.rot * Quaternion().slerp(rot[0].inverse() * rot[1], blend)).normalized();
  1081. prev_time = 0;
  1082. }
  1083. } else {
  1084. if (prev_time < time) {
  1085. Error err = a->try_rotation_track_interpolate(i, prev_time, &rot[0]);
  1086. if (err != OK) {
  1087. continue;
  1088. }
  1089. rot[0] = post_process_key_value(a, i, rot[0], t->object, t->bone_idx);
  1090. a->try_rotation_track_interpolate(i, 0, &rot[1]);
  1091. root_motion_cache.rot = (root_motion_cache.rot * Quaternion().slerp(rot[0].inverse() * rot[1], blend)).normalized();
  1092. prev_time = (double)a->get_length();
  1093. }
  1094. }
  1095. Error err = a->try_rotation_track_interpolate(i, prev_time, &rot[0]);
  1096. if (err != OK) {
  1097. continue;
  1098. }
  1099. rot[0] = post_process_key_value(a, i, rot[0], t->object, t->bone_idx);
  1100. a->try_rotation_track_interpolate(i, time, &rot[1]);
  1101. rot[1] = post_process_key_value(a, i, rot[1], t->object, t->bone_idx);
  1102. root_motion_cache.rot = (root_motion_cache.rot * Quaternion().slerp(rot[0].inverse() * rot[1], blend)).normalized();
  1103. prev_time = !backward ? 0 : (double)a->get_length();
  1104. }
  1105. {
  1106. Quaternion rot;
  1107. Error err = a->try_rotation_track_interpolate(i, time, &rot);
  1108. if (err != OK) {
  1109. continue;
  1110. }
  1111. rot = post_process_key_value(a, i, rot, t->object, t->bone_idx);
  1112. t->rot = (t->rot * Quaternion().slerp(t->init_rot.inverse() * rot, blend)).normalized();
  1113. }
  1114. #endif // _3D_DISABLED
  1115. } break;
  1116. case Animation::TYPE_SCALE_3D: {
  1117. #ifndef _3D_DISABLED
  1118. if (Math::is_zero_approx(blend)) {
  1119. continue; // Nothing to blend.
  1120. }
  1121. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  1122. if (track->root_motion && calc_root) {
  1123. double prev_time = time - delta;
  1124. if (!backward) {
  1125. if (prev_time < 0) {
  1126. switch (a->get_loop_mode()) {
  1127. case Animation::LOOP_NONE: {
  1128. prev_time = 0;
  1129. } break;
  1130. case Animation::LOOP_LINEAR: {
  1131. prev_time = Math::fposmod(prev_time, (double)a->get_length());
  1132. } break;
  1133. case Animation::LOOP_PINGPONG: {
  1134. prev_time = Math::pingpong(prev_time, (double)a->get_length());
  1135. } break;
  1136. default:
  1137. break;
  1138. }
  1139. }
  1140. } else {
  1141. if (prev_time > a->get_length()) {
  1142. switch (a->get_loop_mode()) {
  1143. case Animation::LOOP_NONE: {
  1144. prev_time = (double)a->get_length();
  1145. } break;
  1146. case Animation::LOOP_LINEAR: {
  1147. prev_time = Math::fposmod(prev_time, (double)a->get_length());
  1148. } break;
  1149. case Animation::LOOP_PINGPONG: {
  1150. prev_time = Math::pingpong(prev_time, (double)a->get_length());
  1151. } break;
  1152. default:
  1153. break;
  1154. }
  1155. }
  1156. }
  1157. Vector3 scale[2];
  1158. if (!backward) {
  1159. if (prev_time > time) {
  1160. Error err = a->try_scale_track_interpolate(i, prev_time, &scale[0]);
  1161. if (err != OK) {
  1162. continue;
  1163. }
  1164. scale[0] = post_process_key_value(a, i, scale[0], t->object, t->bone_idx);
  1165. a->try_scale_track_interpolate(i, (double)a->get_length(), &scale[1]);
  1166. root_motion_cache.scale += (scale[1] - scale[0]) * blend;
  1167. scale[1] = post_process_key_value(a, i, scale[1], t->object, t->bone_idx);
  1168. prev_time = 0;
  1169. }
  1170. } else {
  1171. if (prev_time < time) {
  1172. Error err = a->try_scale_track_interpolate(i, prev_time, &scale[0]);
  1173. if (err != OK) {
  1174. continue;
  1175. }
  1176. scale[0] = post_process_key_value(a, i, scale[0], t->object, t->bone_idx);
  1177. a->try_scale_track_interpolate(i, 0, &scale[1]);
  1178. scale[1] = post_process_key_value(a, i, scale[1], t->object, t->bone_idx);
  1179. root_motion_cache.scale += (scale[1] - scale[0]) * blend;
  1180. prev_time = (double)a->get_length();
  1181. }
  1182. }
  1183. Error err = a->try_scale_track_interpolate(i, prev_time, &scale[0]);
  1184. if (err != OK) {
  1185. continue;
  1186. }
  1187. scale[0] = post_process_key_value(a, i, scale[0], t->object, t->bone_idx);
  1188. a->try_scale_track_interpolate(i, time, &scale[1]);
  1189. scale[1] = post_process_key_value(a, i, scale[1], t->object, t->bone_idx);
  1190. root_motion_cache.scale += (scale[1] - scale[0]) * blend;
  1191. prev_time = !backward ? 0 : (double)a->get_length();
  1192. }
  1193. {
  1194. Vector3 scale;
  1195. Error err = a->try_scale_track_interpolate(i, time, &scale);
  1196. if (err != OK) {
  1197. continue;
  1198. }
  1199. scale = post_process_key_value(a, i, scale, t->object, t->bone_idx);
  1200. t->scale += (scale - t->init_scale) * blend;
  1201. }
  1202. #endif // _3D_DISABLED
  1203. } break;
  1204. case Animation::TYPE_BLEND_SHAPE: {
  1205. #ifndef _3D_DISABLED
  1206. if (Math::is_zero_approx(blend)) {
  1207. continue; // Nothing to blend.
  1208. }
  1209. TrackCacheBlendShape *t = static_cast<TrackCacheBlendShape *>(track);
  1210. float value;
  1211. Error err = a->try_blend_shape_track_interpolate(i, time, &value);
  1212. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  1213. if (err != OK) {
  1214. continue;
  1215. }
  1216. value = post_process_key_value(a, i, value, t->object, t->shape_index);
  1217. t->value += (value - t->init_value) * blend;
  1218. #endif // _3D_DISABLED
  1219. } break;
  1220. case Animation::TYPE_VALUE: {
  1221. if (Math::is_zero_approx(blend)) {
  1222. continue; // Nothing to blend.
  1223. }
  1224. TrackCacheValue *t = static_cast<TrackCacheValue *>(track);
  1225. Animation::UpdateMode update_mode = a->value_track_get_update_mode(i);
  1226. if (update_mode == Animation::UPDATE_CONTINUOUS || update_mode == Animation::UPDATE_CAPTURE) {
  1227. Variant value = a->value_track_interpolate(i, time);
  1228. value = post_process_key_value(a, i, value, t->object);
  1229. if (value == Variant()) {
  1230. continue;
  1231. }
  1232. // Special case for angle interpolation.
  1233. if (t->is_using_angle) {
  1234. // For blending consistency, it prevents rotation of more than 180 degrees from init_value.
  1235. // This is the same as for Quaternion blends.
  1236. float rot_a = t->value;
  1237. float rot_b = value;
  1238. float rot_init = t->init_value;
  1239. rot_a = Math::fposmod(rot_a, (float)Math_TAU);
  1240. rot_b = Math::fposmod(rot_b, (float)Math_TAU);
  1241. rot_init = Math::fposmod(rot_init, (float)Math_TAU);
  1242. if (rot_init < Math_PI) {
  1243. rot_a = rot_a > rot_init + Math_PI ? rot_a - Math_TAU : rot_a;
  1244. rot_b = rot_b > rot_init + Math_PI ? rot_b - Math_TAU : rot_b;
  1245. } else {
  1246. rot_a = rot_a < rot_init - Math_PI ? rot_a + Math_TAU : rot_a;
  1247. rot_b = rot_b < rot_init - Math_PI ? rot_b + Math_TAU : rot_b;
  1248. }
  1249. t->value = Math::fposmod(rot_a + (rot_b - rot_init) * (float)blend, (float)Math_TAU);
  1250. } else {
  1251. if (t->init_value.get_type() == Variant::BOOL) {
  1252. value = Animation::subtract_variant(value.operator real_t(), t->init_value.operator real_t());
  1253. t->value = Animation::blend_variant(t->value.operator real_t(), value.operator real_t(), blend);
  1254. } else {
  1255. value = Animation::subtract_variant(value, t->init_value);
  1256. t->value = Animation::blend_variant(t->value, value, blend);
  1257. }
  1258. }
  1259. } else {
  1260. if (seeked) {
  1261. int idx = a->track_find_key(i, time, is_external_seeking ? Animation::FIND_MODE_NEAREST : Animation::FIND_MODE_EXACT);
  1262. if (idx < 0) {
  1263. continue;
  1264. }
  1265. Variant value = a->track_get_key_value(i, idx);
  1266. value = post_process_key_value(a, i, value, t->object);
  1267. t->object->set_indexed(t->subpath, value);
  1268. } else {
  1269. List<int> indices;
  1270. a->track_get_key_indices_in_range(i, time, delta, &indices, looped_flag);
  1271. for (int &F : indices) {
  1272. Variant value = a->track_get_key_value(i, F);
  1273. value = post_process_key_value(a, i, value, t->object);
  1274. t->object->set_indexed(t->subpath, value);
  1275. }
  1276. }
  1277. }
  1278. } break;
  1279. case Animation::TYPE_METHOD: {
  1280. #ifdef TOOLS_ENABLED
  1281. if (!can_call) {
  1282. continue;
  1283. }
  1284. #endif // TOOLS_ENABLED
  1285. if (Math::is_zero_approx(blend)) {
  1286. continue; // Nothing to blend.
  1287. }
  1288. TrackCacheMethod *t = static_cast<TrackCacheMethod *>(track);
  1289. if (seeked) {
  1290. int idx = a->track_find_key(i, time, is_external_seeking ? Animation::FIND_MODE_NEAREST : Animation::FIND_MODE_EXACT);
  1291. if (idx < 0) {
  1292. continue;
  1293. }
  1294. StringName method = a->method_track_get_name(i, idx);
  1295. Vector<Variant> params = a->method_track_get_params(i, idx);
  1296. _call_object(t->object, method, params, false);
  1297. } else {
  1298. List<int> indices;
  1299. a->track_get_key_indices_in_range(i, time, delta, &indices, looped_flag);
  1300. for (int &F : indices) {
  1301. StringName method = a->method_track_get_name(i, F);
  1302. Vector<Variant> params = a->method_track_get_params(i, F);
  1303. _call_object(t->object, method, params, true);
  1304. }
  1305. }
  1306. } break;
  1307. case Animation::TYPE_BEZIER: {
  1308. if (Math::is_zero_approx(blend)) {
  1309. continue; // Nothing to blend.
  1310. }
  1311. TrackCacheBezier *t = static_cast<TrackCacheBezier *>(track);
  1312. real_t bezier = a->bezier_track_interpolate(i, time);
  1313. bezier = post_process_key_value(a, i, bezier, t->object);
  1314. t->value += (bezier - t->init_value) * blend;
  1315. } break;
  1316. case Animation::TYPE_AUDIO: {
  1317. TrackCacheAudio *t = static_cast<TrackCacheAudio *>(track);
  1318. Node *asp = Object::cast_to<Node>(t->object);
  1319. if (!asp) {
  1320. t->playing_streams.clear();
  1321. continue;
  1322. }
  1323. ObjectID oid = a->get_instance_id();
  1324. if (!t->playing_streams.has(oid)) {
  1325. t->playing_streams[oid] = PlayingAudioTrackInfo();
  1326. }
  1327. // The end of audio should be observed even if the blend value is 0, build up the information and store to the cache for that.
  1328. PlayingAudioTrackInfo &track_info = t->playing_streams[oid];
  1329. track_info.length = a->get_length();
  1330. track_info.time = time;
  1331. track_info.volume += blend;
  1332. track_info.loop = a->get_loop_mode() != Animation::LOOP_NONE;
  1333. track_info.backward = backward;
  1334. track_info.use_blend = a->audio_track_is_use_blend(i);
  1335. HashMap<int, PlayingAudioStreamInfo> &map = track_info.stream_info;
  1336. // Find stream.
  1337. int idx = -1;
  1338. if (seeked) {
  1339. idx = a->track_find_key(i, time, is_external_seeking ? Animation::FIND_MODE_NEAREST : Animation::FIND_MODE_EXACT);
  1340. // Discard previous stream when seeking.
  1341. if (map.has(idx)) {
  1342. t->audio_stream_playback->stop_stream(map[idx].index);
  1343. map.erase(idx);
  1344. }
  1345. } else {
  1346. List<int> to_play;
  1347. a->track_get_key_indices_in_range(i, time, delta, &to_play, looped_flag);
  1348. if (to_play.size()) {
  1349. idx = to_play.back()->get();
  1350. }
  1351. }
  1352. if (idx < 0) {
  1353. continue;
  1354. }
  1355. // Play stream.
  1356. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  1357. if (stream.is_valid()) {
  1358. double start_ofs = a->audio_track_get_key_start_offset(i, idx);
  1359. double end_ofs = a->audio_track_get_key_end_offset(i, idx);
  1360. double len = stream->get_length();
  1361. if (seeked) {
  1362. start_ofs += time - a->track_get_key_time(i, idx);
  1363. }
  1364. if (t->object->call(SNAME("get_stream")) != t->audio_stream) {
  1365. t->object->call(SNAME("set_stream"), t->audio_stream);
  1366. t->audio_stream_playback.unref();
  1367. if (!playing_audio_stream_players.has(asp)) {
  1368. playing_audio_stream_players.push_back(asp);
  1369. }
  1370. }
  1371. if (!t->object->call(SNAME("is_playing"))) {
  1372. t->object->call(SNAME("play"));
  1373. }
  1374. if (!t->object->call(SNAME("has_stream_playback"))) {
  1375. t->audio_stream_playback.unref();
  1376. continue;
  1377. }
  1378. if (t->audio_stream_playback.is_null()) {
  1379. t->audio_stream_playback = t->object->call(SNAME("get_stream_playback"));
  1380. }
  1381. PlayingAudioStreamInfo pasi;
  1382. pasi.index = t->audio_stream_playback->play_stream(stream, start_ofs);
  1383. pasi.start = time;
  1384. if (len && end_ofs > 0) { // Force an end at a time.
  1385. pasi.len = len - start_ofs - end_ofs;
  1386. } else {
  1387. pasi.len = 0;
  1388. }
  1389. map[idx] = pasi;
  1390. }
  1391. } break;
  1392. case Animation::TYPE_ANIMATION: {
  1393. if (Math::is_zero_approx(blend)) {
  1394. continue; // Nothing to blend.
  1395. }
  1396. TrackCacheAnimation *t = static_cast<TrackCacheAnimation *>(track);
  1397. AnimationPlayer *player2 = Object::cast_to<AnimationPlayer>(t->object);
  1398. if (!player2) {
  1399. continue;
  1400. }
  1401. if (seeked) {
  1402. //seek
  1403. int idx = a->track_find_key(i, time, is_external_seeking ? Animation::FIND_MODE_NEAREST : Animation::FIND_MODE_EXACT);
  1404. if (idx < 0) {
  1405. continue;
  1406. }
  1407. double pos = a->track_get_key_time(i, idx);
  1408. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  1409. if (String(anim_name) == "[stop]" || !player2->has_animation(anim_name)) {
  1410. continue;
  1411. }
  1412. Ref<Animation> anim = player2->get_animation(anim_name);
  1413. double at_anim_pos = 0.0;
  1414. switch (anim->get_loop_mode()) {
  1415. case Animation::LOOP_NONE: {
  1416. at_anim_pos = MAX((double)anim->get_length(), time - pos); //seek to end
  1417. } break;
  1418. case Animation::LOOP_LINEAR: {
  1419. at_anim_pos = Math::fposmod(time - pos, (double)anim->get_length()); //seek to loop
  1420. } break;
  1421. case Animation::LOOP_PINGPONG: {
  1422. at_anim_pos = Math::pingpong(time - pos, (double)a->get_length());
  1423. } break;
  1424. default:
  1425. break;
  1426. }
  1427. if (player2->is_playing() || seeked) {
  1428. player2->seek(at_anim_pos);
  1429. player2->play(anim_name);
  1430. t->playing = true;
  1431. playing_caches.insert(t);
  1432. } else {
  1433. player2->set_assigned_animation(anim_name);
  1434. player2->seek(at_anim_pos, true);
  1435. }
  1436. } else {
  1437. //find stuff to play
  1438. List<int> to_play;
  1439. a->track_get_key_indices_in_range(i, time, delta, &to_play, looped_flag);
  1440. if (to_play.size()) {
  1441. int idx = to_play.back()->get();
  1442. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  1443. if (String(anim_name) == "[stop]" || !player2->has_animation(anim_name)) {
  1444. if (playing_caches.has(t)) {
  1445. playing_caches.erase(t);
  1446. player2->stop();
  1447. t->playing = false;
  1448. }
  1449. } else {
  1450. player2->play(anim_name);
  1451. t->playing = true;
  1452. playing_caches.insert(t);
  1453. }
  1454. }
  1455. }
  1456. } break;
  1457. }
  1458. }
  1459. }
  1460. }
  1461. {
  1462. // finally, set the tracks
  1463. for (const KeyValue<NodePath, TrackCache *> &K : track_cache) {
  1464. TrackCache *track = K.value;
  1465. switch (track->type) {
  1466. case Animation::TYPE_POSITION_3D: {
  1467. #ifndef _3D_DISABLED
  1468. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  1469. if (t->root_motion) {
  1470. root_motion_position = root_motion_cache.loc;
  1471. root_motion_rotation = root_motion_cache.rot;
  1472. root_motion_scale = root_motion_cache.scale - Vector3(1, 1, 1);
  1473. root_motion_position_accumulator = t->loc;
  1474. root_motion_rotation_accumulator = t->rot;
  1475. root_motion_scale_accumulator = t->scale;
  1476. } else if (t->skeleton && t->bone_idx >= 0) {
  1477. if (t->loc_used) {
  1478. t->skeleton->set_bone_pose_position(t->bone_idx, t->loc);
  1479. }
  1480. if (t->rot_used) {
  1481. t->skeleton->set_bone_pose_rotation(t->bone_idx, t->rot);
  1482. }
  1483. if (t->scale_used) {
  1484. t->skeleton->set_bone_pose_scale(t->bone_idx, t->scale);
  1485. }
  1486. } else if (!t->skeleton) {
  1487. if (t->loc_used) {
  1488. t->node_3d->set_position(t->loc);
  1489. }
  1490. if (t->rot_used) {
  1491. t->node_3d->set_rotation(t->rot.get_euler());
  1492. }
  1493. if (t->scale_used) {
  1494. t->node_3d->set_scale(t->scale);
  1495. }
  1496. }
  1497. #endif // _3D_DISABLED
  1498. } break;
  1499. case Animation::TYPE_BLEND_SHAPE: {
  1500. #ifndef _3D_DISABLED
  1501. TrackCacheBlendShape *t = static_cast<TrackCacheBlendShape *>(track);
  1502. if (t->mesh_3d) {
  1503. t->mesh_3d->set_blend_shape_value(t->shape_index, t->value);
  1504. }
  1505. #endif // _3D_DISABLED
  1506. } break;
  1507. case Animation::TYPE_VALUE: {
  1508. TrackCacheValue *t = static_cast<TrackCacheValue *>(track);
  1509. if (t->is_discrete) {
  1510. break; // Don't overwrite the value set by UPDATE_DISCRETE.
  1511. }
  1512. if (t->init_value.get_type() == Variant::BOOL) {
  1513. t->object->set_indexed(t->subpath, t->value.operator real_t() >= 0.5);
  1514. } else {
  1515. t->object->set_indexed(t->subpath, t->value);
  1516. }
  1517. } break;
  1518. case Animation::TYPE_BEZIER: {
  1519. TrackCacheBezier *t = static_cast<TrackCacheBezier *>(track);
  1520. t->object->set_indexed(t->subpath, t->value);
  1521. } break;
  1522. case Animation::TYPE_AUDIO: {
  1523. TrackCacheAudio *t = static_cast<TrackCacheAudio *>(track);
  1524. // Audio ending process.
  1525. LocalVector<ObjectID> erase_maps;
  1526. for (KeyValue<ObjectID, PlayingAudioTrackInfo> &L : t->playing_streams) {
  1527. PlayingAudioTrackInfo &track_info = L.value;
  1528. float db = Math::linear_to_db(track_info.use_blend ? track_info.volume : 1.0);
  1529. LocalVector<int> erase_streams;
  1530. HashMap<int, PlayingAudioStreamInfo> &map = track_info.stream_info;
  1531. for (const KeyValue<int, PlayingAudioStreamInfo> &M : map) {
  1532. PlayingAudioStreamInfo pasi = M.value;
  1533. bool stop = false;
  1534. if (!t->audio_stream_playback->is_stream_playing(pasi.index)) {
  1535. stop = true;
  1536. }
  1537. if (!track_info.loop) {
  1538. if (!track_info.backward) {
  1539. if (track_info.time < pasi.start) {
  1540. stop = true;
  1541. }
  1542. } else if (track_info.backward) {
  1543. if (track_info.time > pasi.start) {
  1544. stop = true;
  1545. }
  1546. }
  1547. }
  1548. if (pasi.len > 0) {
  1549. double len = 0.0;
  1550. if (!track_info.backward) {
  1551. len = pasi.start > track_info.time ? (track_info.length - pasi.start) + track_info.time : track_info.time - pasi.start;
  1552. } else {
  1553. len = pasi.start < track_info.time ? (track_info.length - track_info.time) + pasi.start : pasi.start - track_info.time;
  1554. }
  1555. if (len > pasi.len) {
  1556. stop = true;
  1557. }
  1558. }
  1559. if (stop) {
  1560. // Time to stop.
  1561. t->audio_stream_playback->stop_stream(pasi.index);
  1562. erase_streams.push_back(M.key);
  1563. } else {
  1564. t->audio_stream_playback->set_stream_volume(pasi.index, db);
  1565. }
  1566. }
  1567. for (uint32_t erase_idx = 0; erase_idx < erase_streams.size(); erase_idx++) {
  1568. map.erase(erase_streams[erase_idx]);
  1569. }
  1570. if (map.size() == 0) {
  1571. erase_maps.push_back(L.key);
  1572. }
  1573. }
  1574. for (uint32_t erase_idx = 0; erase_idx < erase_maps.size(); erase_idx++) {
  1575. t->playing_streams.erase(erase_maps[erase_idx]);
  1576. }
  1577. } break;
  1578. default: {
  1579. } //the rest don't matter
  1580. }
  1581. }
  1582. }
  1583. }
  1584. Variant AnimationTree::post_process_key_value(const Ref<Animation> &p_anim, int p_track, Variant p_value, const Object *p_object, int p_object_idx) {
  1585. Variant res;
  1586. if (GDVIRTUAL_CALL(_post_process_key_value, p_anim, p_track, p_value, const_cast<Object *>(p_object), p_object_idx, res)) {
  1587. return res;
  1588. }
  1589. return _post_process_key_value(p_anim, p_track, p_value, p_object, p_object_idx);
  1590. }
  1591. Variant AnimationTree::_post_process_key_value(const Ref<Animation> &p_anim, int p_track, Variant p_value, const Object *p_object, int p_object_idx) {
  1592. switch (p_anim->track_get_type(p_track)) {
  1593. #ifndef _3D_DISABLED
  1594. case Animation::TYPE_POSITION_3D: {
  1595. if (p_object_idx >= 0) {
  1596. const Skeleton3D *skel = Object::cast_to<Skeleton3D>(p_object);
  1597. return Vector3(p_value) * skel->get_motion_scale();
  1598. }
  1599. return p_value;
  1600. } break;
  1601. #endif // _3D_DISABLED
  1602. default: {
  1603. } break;
  1604. }
  1605. return p_value;
  1606. }
  1607. void AnimationTree::advance(double p_time) {
  1608. _process_graph(p_time);
  1609. }
  1610. void AnimationTree::_notification(int p_what) {
  1611. switch (p_what) {
  1612. case NOTIFICATION_ENTER_TREE: {
  1613. _setup_animation_player();
  1614. if (last_animation_player.is_valid()) {
  1615. Object *player = ObjectDB::get_instance(last_animation_player);
  1616. if (player) {
  1617. player->connect("caches_cleared", callable_mp(this, &AnimationTree::_clear_caches));
  1618. }
  1619. }
  1620. } break;
  1621. case NOTIFICATION_EXIT_TREE: {
  1622. _clear_caches();
  1623. if (last_animation_player.is_valid()) {
  1624. Object *player = ObjectDB::get_instance(last_animation_player);
  1625. if (player) {
  1626. player->disconnect("caches_cleared", callable_mp(this, &AnimationTree::_clear_caches));
  1627. }
  1628. }
  1629. } break;
  1630. case NOTIFICATION_INTERNAL_PROCESS: {
  1631. if (active && process_callback == ANIMATION_PROCESS_IDLE) {
  1632. _process_graph(get_process_delta_time());
  1633. }
  1634. } break;
  1635. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  1636. if (active && process_callback == ANIMATION_PROCESS_PHYSICS) {
  1637. _process_graph(get_physics_process_delta_time());
  1638. }
  1639. } break;
  1640. }
  1641. }
  1642. void AnimationTree::_setup_animation_player() {
  1643. if (!is_inside_tree()) {
  1644. return;
  1645. }
  1646. cache_valid = false;
  1647. AnimationPlayer *new_player = nullptr;
  1648. if (!animation_player.is_empty()) {
  1649. new_player = Object::cast_to<AnimationPlayer>(get_node_or_null(animation_player));
  1650. if (new_player && !new_player->is_connected("animation_list_changed", callable_mp(this, &AnimationTree::_animation_player_changed))) {
  1651. new_player->connect("animation_list_changed", callable_mp(this, &AnimationTree::_animation_player_changed));
  1652. }
  1653. }
  1654. if (new_player) {
  1655. if (!last_animation_player.is_valid()) {
  1656. // Animation player set newly.
  1657. emit_signal(SNAME("animation_player_changed"));
  1658. return;
  1659. } else if (last_animation_player == new_player->get_instance_id()) {
  1660. // Animation player isn't changed.
  1661. return;
  1662. }
  1663. } else if (!last_animation_player.is_valid()) {
  1664. // Animation player is being empty.
  1665. return;
  1666. }
  1667. AnimationPlayer *old_player = Object::cast_to<AnimationPlayer>(ObjectDB::get_instance(last_animation_player));
  1668. if (old_player && old_player->is_connected("animation_list_changed", callable_mp(this, &AnimationTree::_animation_player_changed))) {
  1669. old_player->disconnect("animation_list_changed", callable_mp(this, &AnimationTree::_animation_player_changed));
  1670. }
  1671. emit_signal(SNAME("animation_player_changed"));
  1672. }
  1673. void AnimationTree::set_animation_player(const NodePath &p_player) {
  1674. animation_player = p_player;
  1675. _setup_animation_player();
  1676. update_configuration_warnings();
  1677. }
  1678. NodePath AnimationTree::get_animation_player() const {
  1679. return animation_player;
  1680. }
  1681. void AnimationTree::set_advance_expression_base_node(const NodePath &p_advance_expression_base_node) {
  1682. advance_expression_base_node = p_advance_expression_base_node;
  1683. }
  1684. NodePath AnimationTree::get_advance_expression_base_node() const {
  1685. return advance_expression_base_node;
  1686. }
  1687. void AnimationTree::set_audio_max_polyphony(int p_audio_max_polyphony) {
  1688. ERR_FAIL_COND(p_audio_max_polyphony < 0 || p_audio_max_polyphony > 128);
  1689. audio_max_polyphony = p_audio_max_polyphony;
  1690. }
  1691. int AnimationTree::get_audio_max_polyphony() const {
  1692. return audio_max_polyphony;
  1693. }
  1694. bool AnimationTree::is_state_invalid() const {
  1695. return !state.valid;
  1696. }
  1697. String AnimationTree::get_invalid_state_reason() const {
  1698. return state.invalid_reasons;
  1699. }
  1700. uint64_t AnimationTree::get_last_process_pass() const {
  1701. return process_pass;
  1702. }
  1703. PackedStringArray AnimationTree::get_configuration_warnings() const {
  1704. PackedStringArray warnings = Node::get_configuration_warnings();
  1705. if (!root.is_valid()) {
  1706. warnings.push_back(RTR("No root AnimationNode for the graph is set."));
  1707. }
  1708. if (!has_node(animation_player)) {
  1709. warnings.push_back(RTR("Path to an AnimationPlayer node containing animations is not set."));
  1710. } else {
  1711. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(get_node(animation_player));
  1712. if (!player) {
  1713. warnings.push_back(RTR("Path set for AnimationPlayer does not lead to an AnimationPlayer node."));
  1714. } else if (!player->has_node(player->get_root())) {
  1715. warnings.push_back(RTR("The AnimationPlayer root node is not a valid node."));
  1716. }
  1717. }
  1718. return warnings;
  1719. }
  1720. void AnimationTree::set_root_motion_track(const NodePath &p_track) {
  1721. root_motion_track = p_track;
  1722. }
  1723. NodePath AnimationTree::get_root_motion_track() const {
  1724. return root_motion_track;
  1725. }
  1726. Vector3 AnimationTree::get_root_motion_position() const {
  1727. return root_motion_position;
  1728. }
  1729. Quaternion AnimationTree::get_root_motion_rotation() const {
  1730. return root_motion_rotation;
  1731. }
  1732. Vector3 AnimationTree::get_root_motion_scale() const {
  1733. return root_motion_scale;
  1734. }
  1735. Vector3 AnimationTree::get_root_motion_position_accumulator() const {
  1736. return root_motion_position_accumulator;
  1737. }
  1738. Quaternion AnimationTree::get_root_motion_rotation_accumulator() const {
  1739. return root_motion_rotation_accumulator;
  1740. }
  1741. Vector3 AnimationTree::get_root_motion_scale_accumulator() const {
  1742. return root_motion_scale_accumulator;
  1743. }
  1744. void AnimationTree::_tree_changed() {
  1745. if (properties_dirty) {
  1746. return;
  1747. }
  1748. call_deferred(SNAME("_update_properties"));
  1749. properties_dirty = true;
  1750. }
  1751. void AnimationTree::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
  1752. ERR_FAIL_COND(!property_reference_map.has(p_oid));
  1753. String base_path = property_reference_map[p_oid];
  1754. String old_base = base_path + p_old_name;
  1755. String new_base = base_path + p_new_name;
  1756. for (const PropertyInfo &E : properties) {
  1757. if (E.name.begins_with(old_base)) {
  1758. String new_name = E.name.replace_first(old_base, new_base);
  1759. property_map[new_name] = property_map[E.name];
  1760. property_map.erase(E.name);
  1761. }
  1762. }
  1763. //update tree second
  1764. properties_dirty = true;
  1765. _update_properties();
  1766. }
  1767. void AnimationTree::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
  1768. ERR_FAIL_COND(!property_reference_map.has(p_oid));
  1769. String base_path = String(property_reference_map[p_oid]) + String(p_node);
  1770. for (const PropertyInfo &E : properties) {
  1771. if (E.name.begins_with(base_path)) {
  1772. property_map.erase(E.name);
  1773. }
  1774. }
  1775. //update tree second
  1776. properties_dirty = true;
  1777. _update_properties();
  1778. }
  1779. void AnimationTree::_update_properties_for_node(const String &p_base_path, Ref<AnimationNode> node) {
  1780. ERR_FAIL_COND(node.is_null());
  1781. if (!property_parent_map.has(p_base_path)) {
  1782. property_parent_map[p_base_path] = HashMap<StringName, StringName>();
  1783. }
  1784. if (!property_reference_map.has(node->get_instance_id())) {
  1785. property_reference_map[node->get_instance_id()] = p_base_path;
  1786. }
  1787. if (node->get_input_count() && !input_activity_map.has(p_base_path)) {
  1788. Vector<Activity> activity;
  1789. for (int i = 0; i < node->get_input_count(); i++) {
  1790. Activity a;
  1791. a.activity = 0;
  1792. a.last_pass = 0;
  1793. activity.push_back(a);
  1794. }
  1795. input_activity_map[p_base_path] = activity;
  1796. input_activity_map_get[String(p_base_path).substr(0, String(p_base_path).length() - 1)] = &input_activity_map[p_base_path];
  1797. }
  1798. List<PropertyInfo> plist;
  1799. node->get_parameter_list(&plist);
  1800. for (PropertyInfo &pinfo : plist) {
  1801. StringName key = pinfo.name;
  1802. if (!property_map.has(p_base_path + key)) {
  1803. Pair<Variant, bool> param;
  1804. param.first = node->get_parameter_default_value(key);
  1805. param.second = node->is_parameter_read_only(key);
  1806. property_map[p_base_path + key] = param;
  1807. }
  1808. property_parent_map[p_base_path][key] = p_base_path + key;
  1809. pinfo.name = p_base_path + key;
  1810. properties.push_back(pinfo);
  1811. }
  1812. List<AnimationNode::ChildNode> children;
  1813. node->get_child_nodes(&children);
  1814. for (const AnimationNode::ChildNode &E : children) {
  1815. _update_properties_for_node(p_base_path + E.name + "/", E.node);
  1816. }
  1817. }
  1818. void AnimationTree::_update_properties() {
  1819. if (!properties_dirty) {
  1820. return;
  1821. }
  1822. properties.clear();
  1823. property_reference_map.clear();
  1824. property_parent_map.clear();
  1825. input_activity_map.clear();
  1826. input_activity_map_get.clear();
  1827. if (root.is_valid()) {
  1828. _update_properties_for_node(SceneStringNames::get_singleton()->parameters_base_path, root);
  1829. }
  1830. properties_dirty = false;
  1831. notify_property_list_changed();
  1832. }
  1833. bool AnimationTree::_set(const StringName &p_name, const Variant &p_value) {
  1834. if (properties_dirty) {
  1835. _update_properties();
  1836. }
  1837. if (property_map.has(p_name)) {
  1838. if (is_inside_tree() && property_map[p_name].second) {
  1839. return false; // Prevent to set property by user.
  1840. }
  1841. property_map[p_name].first = p_value;
  1842. return true;
  1843. }
  1844. return false;
  1845. }
  1846. bool AnimationTree::_get(const StringName &p_name, Variant &r_ret) const {
  1847. if (properties_dirty) {
  1848. const_cast<AnimationTree *>(this)->_update_properties();
  1849. }
  1850. if (property_map.has(p_name)) {
  1851. r_ret = property_map[p_name].first;
  1852. return true;
  1853. }
  1854. return false;
  1855. }
  1856. void AnimationTree::_get_property_list(List<PropertyInfo> *p_list) const {
  1857. if (properties_dirty) {
  1858. const_cast<AnimationTree *>(this)->_update_properties();
  1859. }
  1860. for (const PropertyInfo &E : properties) {
  1861. p_list->push_back(E);
  1862. }
  1863. }
  1864. real_t AnimationTree::get_connection_activity(const StringName &p_path, int p_connection) const {
  1865. if (!input_activity_map_get.has(p_path)) {
  1866. return 0;
  1867. }
  1868. const Vector<Activity> *activity = input_activity_map_get[p_path];
  1869. if (!activity || p_connection < 0 || p_connection >= activity->size()) {
  1870. return 0;
  1871. }
  1872. if ((*activity)[p_connection].last_pass != process_pass) {
  1873. return 0;
  1874. }
  1875. return (*activity)[p_connection].activity;
  1876. }
  1877. void AnimationTree::_bind_methods() {
  1878. ClassDB::bind_method(D_METHOD("set_active", "active"), &AnimationTree::set_active);
  1879. ClassDB::bind_method(D_METHOD("is_active"), &AnimationTree::is_active);
  1880. ClassDB::bind_method(D_METHOD("set_tree_root", "root"), &AnimationTree::set_tree_root);
  1881. ClassDB::bind_method(D_METHOD("get_tree_root"), &AnimationTree::get_tree_root);
  1882. ClassDB::bind_method(D_METHOD("set_process_callback", "mode"), &AnimationTree::set_process_callback);
  1883. ClassDB::bind_method(D_METHOD("get_process_callback"), &AnimationTree::get_process_callback);
  1884. ClassDB::bind_method(D_METHOD("set_animation_player", "root"), &AnimationTree::set_animation_player);
  1885. ClassDB::bind_method(D_METHOD("get_animation_player"), &AnimationTree::get_animation_player);
  1886. ClassDB::bind_method(D_METHOD("set_advance_expression_base_node", "node"), &AnimationTree::set_advance_expression_base_node);
  1887. ClassDB::bind_method(D_METHOD("get_advance_expression_base_node"), &AnimationTree::get_advance_expression_base_node);
  1888. ClassDB::bind_method(D_METHOD("set_root_motion_track", "path"), &AnimationTree::set_root_motion_track);
  1889. ClassDB::bind_method(D_METHOD("get_root_motion_track"), &AnimationTree::get_root_motion_track);
  1890. ClassDB::bind_method(D_METHOD("set_audio_max_polyphony", "max_polyphony"), &AnimationTree::set_audio_max_polyphony);
  1891. ClassDB::bind_method(D_METHOD("get_audio_max_polyphony"), &AnimationTree::get_audio_max_polyphony);
  1892. ClassDB::bind_method(D_METHOD("get_root_motion_position"), &AnimationTree::get_root_motion_position);
  1893. ClassDB::bind_method(D_METHOD("get_root_motion_rotation"), &AnimationTree::get_root_motion_rotation);
  1894. ClassDB::bind_method(D_METHOD("get_root_motion_scale"), &AnimationTree::get_root_motion_scale);
  1895. ClassDB::bind_method(D_METHOD("get_root_motion_position_accumulator"), &AnimationTree::get_root_motion_position_accumulator);
  1896. ClassDB::bind_method(D_METHOD("get_root_motion_rotation_accumulator"), &AnimationTree::get_root_motion_rotation_accumulator);
  1897. ClassDB::bind_method(D_METHOD("get_root_motion_scale_accumulator"), &AnimationTree::get_root_motion_scale_accumulator);
  1898. ClassDB::bind_method(D_METHOD("_update_properties"), &AnimationTree::_update_properties);
  1899. ClassDB::bind_method(D_METHOD("advance", "delta"), &AnimationTree::advance);
  1900. GDVIRTUAL_BIND(_post_process_key_value, "animation", "track", "value", "object", "object_idx");
  1901. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tree_root", PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode"), "set_tree_root", "get_tree_root");
  1902. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "anim_player", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "AnimationPlayer"), "set_animation_player", "get_animation_player");
  1903. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "advance_expression_base_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node"), "set_advance_expression_base_node", "get_advance_expression_base_node");
  1904. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "active"), "set_active", "is_active");
  1905. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_callback", PROPERTY_HINT_ENUM, "Physics,Idle,Manual"), "set_process_callback", "get_process_callback");
  1906. ADD_GROUP("Audio", "audio_");
  1907. ADD_PROPERTY(PropertyInfo(Variant::INT, "audio_max_polyphony", PROPERTY_HINT_RANGE, "1,127,1"), "set_audio_max_polyphony", "get_audio_max_polyphony");
  1908. ADD_GROUP("Root Motion", "root_motion_");
  1909. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "root_motion_track"), "set_root_motion_track", "get_root_motion_track");
  1910. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_PHYSICS);
  1911. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_IDLE);
  1912. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_MANUAL);
  1913. ADD_SIGNAL(MethodInfo("animation_player_changed"));
  1914. // Signals from AnimationNodes.
  1915. ADD_SIGNAL(MethodInfo("animation_started", PropertyInfo(Variant::STRING_NAME, "anim_name")));
  1916. ADD_SIGNAL(MethodInfo("animation_finished", PropertyInfo(Variant::STRING_NAME, "anim_name")));
  1917. }
  1918. AnimationTree::AnimationTree() {
  1919. }
  1920. AnimationTree::~AnimationTree() {
  1921. }