animation_tree.cpp 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  1. /*************************************************************************/
  2. /* animation_tree.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. if (GDVIRTUAL_CALL(_get_parameter_default_value, p_parameter, ret)) {
  49. return ret;
  50. }
  51. return Variant();
  52. }
  53. void AnimationNode::set_parameter(const StringName &p_name, const Variant &p_value) {
  54. ERR_FAIL_COND(!state);
  55. ERR_FAIL_COND(!state->tree->property_parent_map.has(base_path));
  56. ERR_FAIL_COND(!state->tree->property_parent_map[base_path].has(p_name));
  57. StringName path = state->tree->property_parent_map[base_path][p_name];
  58. state->tree->property_map[path] = p_value;
  59. }
  60. Variant AnimationNode::get_parameter(const StringName &p_name) const {
  61. ERR_FAIL_COND_V(!state, Variant());
  62. ERR_FAIL_COND_V(!state->tree->property_parent_map.has(base_path), Variant());
  63. ERR_FAIL_COND_V(!state->tree->property_parent_map[base_path].has(p_name), Variant());
  64. StringName path = state->tree->property_parent_map[base_path][p_name];
  65. return state->tree->property_map[path];
  66. }
  67. void AnimationNode::get_child_nodes(List<ChildNode> *r_child_nodes) {
  68. Dictionary cn;
  69. if (GDVIRTUAL_CALL(_get_child_nodes, cn)) {
  70. List<Variant> keys;
  71. cn.get_key_list(&keys);
  72. for (const Variant &E : keys) {
  73. ChildNode child;
  74. child.name = E;
  75. child.node = cn[E];
  76. r_child_nodes->push_back(child);
  77. }
  78. }
  79. }
  80. void AnimationNode::blend_animation(const StringName &p_animation, double p_time, double p_delta, bool p_seeked, bool p_seek_root, real_t p_blend, int p_pingponged) {
  81. ERR_FAIL_COND(!state);
  82. ERR_FAIL_COND(!state->player->has_animation(p_animation));
  83. Ref<Animation> animation = state->player->get_animation(p_animation);
  84. if (animation.is_null()) {
  85. AnimationNodeBlendTree *btree = Object::cast_to<AnimationNodeBlendTree>(parent);
  86. if (btree) {
  87. String name = btree->get_node_name(Ref<AnimationNodeAnimation>(this));
  88. make_invalid(vformat(RTR("In node '%s', invalid animation: '%s'."), name, p_animation));
  89. } else {
  90. make_invalid(vformat(RTR("Invalid animation: '%s'."), p_animation));
  91. }
  92. return;
  93. }
  94. ERR_FAIL_COND(!animation.is_valid());
  95. AnimationState anim_state;
  96. anim_state.blend = p_blend;
  97. anim_state.track_blends = &blends;
  98. anim_state.delta = p_delta;
  99. anim_state.time = p_time;
  100. anim_state.animation = animation;
  101. anim_state.seeked = p_seeked;
  102. anim_state.pingponged = p_pingponged;
  103. anim_state.seek_root = p_seek_root;
  104. state->animation_states.push_back(anim_state);
  105. }
  106. double AnimationNode::_pre_process(const StringName &p_base_path, AnimationNode *p_parent, State *p_state, double p_time, bool p_seek, bool p_seek_root, const Vector<StringName> &p_connections) {
  107. base_path = p_base_path;
  108. parent = p_parent;
  109. connections = p_connections;
  110. state = p_state;
  111. double t = process(p_time, p_seek, p_seek_root);
  112. state = nullptr;
  113. parent = nullptr;
  114. base_path = StringName();
  115. connections.clear();
  116. return t;
  117. }
  118. void AnimationNode::make_invalid(const String &p_reason) {
  119. ERR_FAIL_COND(!state);
  120. state->valid = false;
  121. if (!state->invalid_reasons.is_empty()) {
  122. state->invalid_reasons += "\n";
  123. }
  124. state->invalid_reasons += String::utf8("• ") + p_reason;
  125. }
  126. double AnimationNode::blend_input(int p_input, double p_time, bool p_seek, bool p_seek_root, real_t p_blend, FilterAction p_filter, bool p_optimize) {
  127. ERR_FAIL_INDEX_V(p_input, inputs.size(), 0);
  128. ERR_FAIL_COND_V(!state, 0);
  129. AnimationNodeBlendTree *blend_tree = Object::cast_to<AnimationNodeBlendTree>(parent);
  130. ERR_FAIL_COND_V(!blend_tree, 0);
  131. StringName node_name = connections[p_input];
  132. if (!blend_tree->has_node(node_name)) {
  133. String name = blend_tree->get_node_name(Ref<AnimationNode>(this));
  134. make_invalid(vformat(RTR("Nothing connected to input '%s' of node '%s'."), get_input_name(p_input), name));
  135. return 0;
  136. }
  137. Ref<AnimationNode> node = blend_tree->get_node(node_name);
  138. //inputs.write[p_input].last_pass = state->last_pass;
  139. real_t activity = 0.0;
  140. double ret = _blend_node(node_name, blend_tree->get_node_connection_array(node_name), nullptr, node, p_time, p_seek, p_seek_root, p_blend, p_filter, p_optimize, &activity);
  141. Vector<AnimationTree::Activity> *activity_ptr = state->tree->input_activity_map.getptr(base_path);
  142. if (activity_ptr && p_input < activity_ptr->size()) {
  143. activity_ptr->write[p_input].last_pass = state->last_pass;
  144. activity_ptr->write[p_input].activity = activity;
  145. }
  146. return ret;
  147. }
  148. double AnimationNode::blend_node(const StringName &p_sub_path, Ref<AnimationNode> p_node, double p_time, bool p_seek, bool p_seek_root, real_t p_blend, FilterAction p_filter, bool p_optimize) {
  149. return _blend_node(p_sub_path, Vector<StringName>(), this, p_node, p_time, p_seek, p_seek_root, p_blend, p_filter, p_optimize);
  150. }
  151. 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_seek_root, real_t p_blend, FilterAction p_filter, bool p_optimize, real_t *r_max) {
  152. ERR_FAIL_COND_V(!p_node.is_valid(), 0);
  153. ERR_FAIL_COND_V(!state, 0);
  154. int blend_count = blends.size();
  155. if (p_node->blends.size() != blend_count) {
  156. p_node->blends.resize(blend_count);
  157. }
  158. real_t *blendw = p_node->blends.ptrw();
  159. const real_t *blendr = blends.ptr();
  160. bool any_valid = false;
  161. if (has_filter() && is_filter_enabled() && p_filter != FILTER_IGNORE) {
  162. for (int i = 0; i < blend_count; i++) {
  163. blendw[i] = 0.0; //all to zero by default
  164. }
  165. for (const KeyValue<NodePath, bool> &E : filter) {
  166. if (!state->track_map.has(E.key)) {
  167. continue;
  168. }
  169. int idx = state->track_map[E.key];
  170. blendw[idx] = 1.0; //filtered goes to one
  171. }
  172. switch (p_filter) {
  173. case FILTER_IGNORE:
  174. break; //will not happen anyway
  175. case FILTER_PASS: {
  176. //values filtered pass, the rest don't
  177. for (int i = 0; i < blend_count; i++) {
  178. if (blendw[i] == 0) { //not filtered, does not pass
  179. continue;
  180. }
  181. blendw[i] = blendr[i] * p_blend;
  182. if (blendw[i] > CMP_EPSILON) {
  183. any_valid = true;
  184. }
  185. }
  186. } break;
  187. case FILTER_STOP: {
  188. //values filtered don't pass, the rest are blended
  189. for (int i = 0; i < blend_count; i++) {
  190. if (blendw[i] > 0) { //filtered, does not pass
  191. continue;
  192. }
  193. blendw[i] = blendr[i] * p_blend;
  194. if (blendw[i] > CMP_EPSILON) {
  195. any_valid = true;
  196. }
  197. }
  198. } break;
  199. case FILTER_BLEND: {
  200. //filtered values are blended, the rest are passed without blending
  201. for (int i = 0; i < blend_count; i++) {
  202. if (blendw[i] == 1.0) {
  203. blendw[i] = blendr[i] * p_blend; //filtered, blend
  204. } else {
  205. blendw[i] = blendr[i]; //not filtered, do not blend
  206. }
  207. if (blendw[i] > CMP_EPSILON) {
  208. any_valid = true;
  209. }
  210. }
  211. } break;
  212. }
  213. } else {
  214. for (int i = 0; i < blend_count; i++) {
  215. //regular blend
  216. blendw[i] = blendr[i] * p_blend;
  217. if (blendw[i] > CMP_EPSILON) {
  218. any_valid = true;
  219. }
  220. }
  221. }
  222. if (r_max) {
  223. *r_max = 0;
  224. for (int i = 0; i < blend_count; i++) {
  225. *r_max = MAX(*r_max, blendw[i]);
  226. }
  227. }
  228. String new_path;
  229. AnimationNode *new_parent;
  230. //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
  231. if (p_new_parent) {
  232. new_parent = p_new_parent;
  233. new_path = String(base_path) + String(p_subpath) + "/";
  234. } else {
  235. ERR_FAIL_COND_V(!parent, 0);
  236. new_parent = parent;
  237. new_path = String(parent->base_path) + String(p_subpath) + "/";
  238. }
  239. if (!p_seek && p_optimize && !any_valid) {
  240. return p_node->_pre_process(new_path, new_parent, state, 0, p_seek, p_seek_root, p_connections);
  241. }
  242. return p_node->_pre_process(new_path, new_parent, state, p_time, p_seek, p_seek_root, p_connections);
  243. }
  244. int AnimationNode::get_input_count() const {
  245. return inputs.size();
  246. }
  247. String AnimationNode::get_input_name(int p_input) {
  248. ERR_FAIL_INDEX_V(p_input, inputs.size(), String());
  249. return inputs[p_input].name;
  250. }
  251. String AnimationNode::get_caption() const {
  252. String ret;
  253. if (GDVIRTUAL_CALL(_get_caption, ret)) {
  254. return ret;
  255. }
  256. return "Node";
  257. }
  258. void AnimationNode::add_input(const String &p_name) {
  259. //root nodes can't add inputs
  260. ERR_FAIL_COND(Object::cast_to<AnimationRootNode>(this) != nullptr);
  261. Input input;
  262. ERR_FAIL_COND(p_name.contains(".") || p_name.contains("/"));
  263. input.name = p_name;
  264. inputs.push_back(input);
  265. emit_changed();
  266. }
  267. void AnimationNode::set_input_name(int p_input, const String &p_name) {
  268. ERR_FAIL_INDEX(p_input, inputs.size());
  269. ERR_FAIL_COND(p_name.contains(".") || p_name.contains("/"));
  270. inputs.write[p_input].name = p_name;
  271. emit_changed();
  272. }
  273. void AnimationNode::remove_input(int p_index) {
  274. ERR_FAIL_INDEX(p_index, inputs.size());
  275. inputs.remove_at(p_index);
  276. emit_changed();
  277. }
  278. double AnimationNode::process(double p_time, bool p_seek, bool p_seek_root) {
  279. double ret;
  280. if (GDVIRTUAL_CALL(_process, p_time, p_seek, p_seek_root, ret)) {
  281. return ret;
  282. }
  283. return 0;
  284. }
  285. void AnimationNode::set_filter_path(const NodePath &p_path, bool p_enable) {
  286. if (p_enable) {
  287. filter[p_path] = true;
  288. } else {
  289. filter.erase(p_path);
  290. }
  291. }
  292. void AnimationNode::set_filter_enabled(bool p_enable) {
  293. filter_enabled = p_enable;
  294. }
  295. bool AnimationNode::is_filter_enabled() const {
  296. return filter_enabled;
  297. }
  298. bool AnimationNode::is_path_filtered(const NodePath &p_path) const {
  299. return filter.has(p_path);
  300. }
  301. bool AnimationNode::has_filter() const {
  302. bool ret;
  303. if (GDVIRTUAL_CALL(_has_filter, ret)) {
  304. return ret;
  305. }
  306. return false;
  307. }
  308. Array AnimationNode::_get_filters() const {
  309. Array paths;
  310. for (const KeyValue<NodePath, bool> &E : filter) {
  311. paths.push_back(String(E.key)); //use strings, so sorting is possible
  312. }
  313. paths.sort(); //done so every time the scene is saved, it does not change
  314. return paths;
  315. }
  316. void AnimationNode::_set_filters(const Array &p_filters) {
  317. filter.clear();
  318. for (int i = 0; i < p_filters.size(); i++) {
  319. set_filter_path(p_filters[i], true);
  320. }
  321. }
  322. void AnimationNode::_validate_property(PropertyInfo &property) const {
  323. if (!has_filter() && (property.name == "filter_enabled" || property.name == "filters")) {
  324. property.usage = PROPERTY_USAGE_NONE;
  325. }
  326. }
  327. Ref<AnimationNode> AnimationNode::get_child_by_name(const StringName &p_name) {
  328. Ref<AnimationNode> ret;
  329. if (GDVIRTUAL_CALL(_get_child_by_name, p_name, ret)) {
  330. return ret;
  331. }
  332. return Ref<AnimationNode>();
  333. }
  334. void AnimationNode::_bind_methods() {
  335. ClassDB::bind_method(D_METHOD("get_input_count"), &AnimationNode::get_input_count);
  336. ClassDB::bind_method(D_METHOD("get_input_name", "input"), &AnimationNode::get_input_name);
  337. ClassDB::bind_method(D_METHOD("add_input", "name"), &AnimationNode::add_input);
  338. ClassDB::bind_method(D_METHOD("remove_input", "index"), &AnimationNode::remove_input);
  339. ClassDB::bind_method(D_METHOD("set_filter_path", "path", "enable"), &AnimationNode::set_filter_path);
  340. ClassDB::bind_method(D_METHOD("is_path_filtered", "path"), &AnimationNode::is_path_filtered);
  341. ClassDB::bind_method(D_METHOD("set_filter_enabled", "enable"), &AnimationNode::set_filter_enabled);
  342. ClassDB::bind_method(D_METHOD("is_filter_enabled"), &AnimationNode::is_filter_enabled);
  343. ClassDB::bind_method(D_METHOD("_set_filters", "filters"), &AnimationNode::_set_filters);
  344. ClassDB::bind_method(D_METHOD("_get_filters"), &AnimationNode::_get_filters);
  345. ClassDB::bind_method(D_METHOD("blend_animation", "animation", "time", "delta", "seeked", "seek_root", "blend", "pingponged"), &AnimationNode::blend_animation, DEFVAL(0));
  346. ClassDB::bind_method(D_METHOD("blend_node", "name", "node", "time", "seek", "seek_root", "blend", "filter", "optimize"), &AnimationNode::blend_node, DEFVAL(FILTER_IGNORE), DEFVAL(true));
  347. ClassDB::bind_method(D_METHOD("blend_input", "input_index", "time", "seek", "seek_root", "blend", "filter", "optimize"), &AnimationNode::blend_input, DEFVAL(FILTER_IGNORE), DEFVAL(true));
  348. ClassDB::bind_method(D_METHOD("set_parameter", "name", "value"), &AnimationNode::set_parameter);
  349. ClassDB::bind_method(D_METHOD("get_parameter", "name"), &AnimationNode::get_parameter);
  350. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_filter_enabled", "is_filter_enabled");
  351. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "filters", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_filters", "_get_filters");
  352. GDVIRTUAL_BIND(_get_child_nodes);
  353. GDVIRTUAL_BIND(_get_parameter_list);
  354. GDVIRTUAL_BIND(_get_child_by_name, "name");
  355. GDVIRTUAL_BIND(_get_parameter_default_value, "parameter");
  356. GDVIRTUAL_BIND(_process, "time", "seek", "seek_root");
  357. GDVIRTUAL_BIND(_get_caption);
  358. GDVIRTUAL_BIND(_has_filter);
  359. ADD_SIGNAL(MethodInfo("removed_from_graph"));
  360. ADD_SIGNAL(MethodInfo("tree_changed"));
  361. BIND_ENUM_CONSTANT(FILTER_IGNORE);
  362. BIND_ENUM_CONSTANT(FILTER_PASS);
  363. BIND_ENUM_CONSTANT(FILTER_STOP);
  364. BIND_ENUM_CONSTANT(FILTER_BLEND);
  365. }
  366. AnimationNode::AnimationNode() {
  367. }
  368. ////////////////////
  369. void AnimationTree::set_tree_root(const Ref<AnimationNode> &p_root) {
  370. if (root.is_valid()) {
  371. root->disconnect("tree_changed", callable_mp(this, &AnimationTree::_tree_changed));
  372. }
  373. root = p_root;
  374. if (root.is_valid()) {
  375. root->connect("tree_changed", callable_mp(this, &AnimationTree::_tree_changed));
  376. }
  377. properties_dirty = true;
  378. update_configuration_warnings();
  379. }
  380. Ref<AnimationNode> AnimationTree::get_tree_root() const {
  381. return root;
  382. }
  383. void AnimationTree::set_active(bool p_active) {
  384. if (active == p_active) {
  385. return;
  386. }
  387. active = p_active;
  388. started = active;
  389. if (process_callback == ANIMATION_PROCESS_IDLE) {
  390. set_process_internal(active);
  391. } else {
  392. set_physics_process_internal(active);
  393. }
  394. if (!active && is_inside_tree()) {
  395. for (const TrackCache *E : playing_caches) {
  396. if (ObjectDB::get_instance(E->object_id)) {
  397. E->object->call(SNAME("stop"));
  398. }
  399. }
  400. playing_caches.clear();
  401. }
  402. }
  403. bool AnimationTree::is_active() const {
  404. return active;
  405. }
  406. void AnimationTree::set_process_callback(AnimationProcessCallback p_mode) {
  407. if (process_callback == p_mode) {
  408. return;
  409. }
  410. bool was_active = is_active();
  411. if (was_active) {
  412. set_active(false);
  413. }
  414. process_callback = p_mode;
  415. if (was_active) {
  416. set_active(true);
  417. }
  418. }
  419. AnimationTree::AnimationProcessCallback AnimationTree::get_process_callback() const {
  420. return process_callback;
  421. }
  422. void AnimationTree::_node_removed(Node *p_node) {
  423. cache_valid = false;
  424. }
  425. bool AnimationTree::_update_caches(AnimationPlayer *player) {
  426. setup_pass++;
  427. if (!player->has_node(player->get_root())) {
  428. ERR_PRINT("AnimationTree: AnimationPlayer root is invalid.");
  429. set_active(false);
  430. return false;
  431. }
  432. Node *parent = player->get_node(player->get_root());
  433. List<StringName> sname;
  434. player->get_animation_list(&sname);
  435. Ref<Animation> reset_anim;
  436. bool has_reset_anim = player->has_animation(SceneStringNames::get_singleton()->RESET);
  437. if (has_reset_anim) {
  438. reset_anim = player->get_animation(SceneStringNames::get_singleton()->RESET);
  439. }
  440. for (const StringName &E : sname) {
  441. Ref<Animation> anim = player->get_animation(E);
  442. for (int i = 0; i < anim->get_track_count(); i++) {
  443. NodePath path = anim->track_get_path(i);
  444. Animation::TrackType track_type = anim->track_get_type(i);
  445. Animation::TrackType track_cache_type = track_type;
  446. if (track_cache_type == Animation::TYPE_POSITION_3D || track_cache_type == Animation::TYPE_ROTATION_3D || track_cache_type == Animation::TYPE_SCALE_3D) {
  447. track_cache_type = Animation::TYPE_POSITION_3D; //reference them as position3D tracks, even if they modify rotation or scale
  448. }
  449. TrackCache *track = nullptr;
  450. if (track_cache.has(path)) {
  451. track = track_cache.get(path);
  452. }
  453. //if not valid, delete track
  454. if (track && (track->type != track_cache_type || ObjectDB::get_instance(track->object_id) == nullptr)) {
  455. playing_caches.erase(track);
  456. memdelete(track);
  457. track_cache.erase(path);
  458. track = nullptr;
  459. }
  460. if (!track) {
  461. Ref<Resource> resource;
  462. Vector<StringName> leftover_path;
  463. Node *child = parent->get_node_and_resource(path, resource, leftover_path);
  464. if (!child) {
  465. ERR_PRINT("AnimationTree: '" + String(E) + "', couldn't resolve track: '" + String(path) + "'");
  466. continue;
  467. }
  468. if (!child->is_connected("tree_exited", callable_mp(this, &AnimationTree::_node_removed))) {
  469. child->connect("tree_exited", callable_mp(this, &AnimationTree::_node_removed), varray(child));
  470. }
  471. switch (track_type) {
  472. case Animation::TYPE_VALUE: {
  473. TrackCacheValue *track_value = memnew(TrackCacheValue);
  474. if (resource.is_valid()) {
  475. track_value->object = resource.ptr();
  476. } else {
  477. track_value->object = child;
  478. }
  479. track_value->subpath = leftover_path;
  480. track_value->object_id = track_value->object->get_instance_id();
  481. track = track_value;
  482. if (has_reset_anim) {
  483. int rt = reset_anim->find_track(path, track_type);
  484. if (rt >= 0 && reset_anim->track_get_key_count(rt) > 0) {
  485. track_value->init_value = reset_anim->track_get_key_value(rt, 0);
  486. }
  487. }
  488. } break;
  489. case Animation::TYPE_POSITION_3D:
  490. case Animation::TYPE_ROTATION_3D:
  491. case Animation::TYPE_SCALE_3D: {
  492. #ifndef _3D_DISABLED
  493. Node3D *node_3d = Object::cast_to<Node3D>(child);
  494. if (!node_3d) {
  495. ERR_PRINT("AnimationTree: '" + String(E) + "', transform track does not point to Node3D: '" + String(path) + "'");
  496. continue;
  497. }
  498. TrackCacheTransform *track_xform = memnew(TrackCacheTransform);
  499. track_xform->type = Animation::TYPE_POSITION_3D;
  500. track_xform->node_3d = node_3d;
  501. track_xform->skeleton = nullptr;
  502. track_xform->bone_idx = -1;
  503. bool has_rest = false;
  504. if (path.get_subname_count() == 1 && Object::cast_to<Skeleton3D>(node_3d)) {
  505. Skeleton3D *sk = Object::cast_to<Skeleton3D>(node_3d);
  506. track_xform->skeleton = sk;
  507. int bone_idx = sk->find_bone(path.get_subname(0));
  508. if (bone_idx != -1) {
  509. has_rest = true;
  510. track_xform->bone_idx = bone_idx;
  511. Transform3D rest = sk->get_bone_rest(bone_idx);
  512. track_xform->init_loc = rest.origin;
  513. track_xform->init_rot = rest.basis.get_rotation_quaternion();
  514. track_xform->init_scale = rest.basis.get_scale();
  515. }
  516. }
  517. track_xform->object = node_3d;
  518. track_xform->object_id = track_xform->object->get_instance_id();
  519. track = track_xform;
  520. switch (track_type) {
  521. case Animation::TYPE_POSITION_3D: {
  522. track_xform->loc_used = true;
  523. } break;
  524. case Animation::TYPE_ROTATION_3D: {
  525. track_xform->rot_used = true;
  526. } break;
  527. case Animation::TYPE_SCALE_3D: {
  528. track_xform->scale_used = true;
  529. } break;
  530. default: {
  531. }
  532. }
  533. // For non Skeleton3D bone animation.
  534. if (has_reset_anim && !has_rest) {
  535. int rt = reset_anim->find_track(path, track_type);
  536. if (rt >= 0 && reset_anim->track_get_key_count(rt) > 0) {
  537. switch (track_type) {
  538. case Animation::TYPE_POSITION_3D: {
  539. track_xform->init_loc = reset_anim->track_get_key_value(rt, 0);
  540. } break;
  541. case Animation::TYPE_ROTATION_3D: {
  542. track_xform->init_rot = reset_anim->track_get_key_value(rt, 0);
  543. } break;
  544. case Animation::TYPE_SCALE_3D: {
  545. track_xform->init_scale = reset_anim->track_get_key_value(rt, 0);
  546. } break;
  547. default: {
  548. }
  549. }
  550. }
  551. }
  552. #endif // _3D_DISABLED
  553. } break;
  554. case Animation::TYPE_BLEND_SHAPE: {
  555. #ifndef _3D_DISABLED
  556. if (path.get_subname_count() != 1) {
  557. ERR_PRINT("AnimationTree: '" + String(E) + "', blend shape track does not contain a blend shape subname: '" + String(path) + "'");
  558. continue;
  559. }
  560. MeshInstance3D *mesh_3d = Object::cast_to<MeshInstance3D>(child);
  561. if (!mesh_3d) {
  562. ERR_PRINT("AnimationTree: '" + String(E) + "', blend shape track does not point to MeshInstance3D: '" + String(path) + "'");
  563. continue;
  564. }
  565. StringName blend_shape_name = path.get_subname(0);
  566. int blend_shape_idx = mesh_3d->find_blend_shape_by_name(blend_shape_name);
  567. if (blend_shape_idx == -1) {
  568. ERR_PRINT("AnimationTree: '" + String(E) + "', blend shape track points to a non-existing name: '" + String(blend_shape_name) + "'");
  569. continue;
  570. }
  571. TrackCacheBlendShape *track_bshape = memnew(TrackCacheBlendShape);
  572. track_bshape->mesh_3d = mesh_3d;
  573. track_bshape->shape_index = blend_shape_idx;
  574. track_bshape->object = mesh_3d;
  575. track_bshape->object_id = mesh_3d->get_instance_id();
  576. track = track_bshape;
  577. if (has_reset_anim) {
  578. int rt = reset_anim->find_track(path, track_type);
  579. if (rt >= 0 && reset_anim->track_get_key_count(rt) > 0) {
  580. track_bshape->init_value = reset_anim->track_get_key_value(rt, 0);
  581. }
  582. }
  583. #endif
  584. } break;
  585. case Animation::TYPE_METHOD: {
  586. TrackCacheMethod *track_method = memnew(TrackCacheMethod);
  587. if (resource.is_valid()) {
  588. track_method->object = resource.ptr();
  589. } else {
  590. track_method->object = child;
  591. }
  592. track_method->object_id = track_method->object->get_instance_id();
  593. track = track_method;
  594. } break;
  595. case Animation::TYPE_BEZIER: {
  596. TrackCacheBezier *track_bezier = memnew(TrackCacheBezier);
  597. if (resource.is_valid()) {
  598. track_bezier->object = resource.ptr();
  599. } else {
  600. track_bezier->object = child;
  601. }
  602. track_bezier->subpath = leftover_path;
  603. track_bezier->object_id = track_bezier->object->get_instance_id();
  604. track = track_bezier;
  605. if (has_reset_anim) {
  606. int rt = reset_anim->find_track(path, track_type);
  607. if (rt >= 0 && reset_anim->track_get_key_count(rt) > 0) {
  608. track_bezier->init_value = reset_anim->track_get_key_value(rt, 0);
  609. }
  610. }
  611. } break;
  612. case Animation::TYPE_AUDIO: {
  613. TrackCacheAudio *track_audio = memnew(TrackCacheAudio);
  614. track_audio->object = child;
  615. track_audio->object_id = track_audio->object->get_instance_id();
  616. track = track_audio;
  617. } break;
  618. case Animation::TYPE_ANIMATION: {
  619. TrackCacheAnimation *track_animation = memnew(TrackCacheAnimation);
  620. track_animation->object = child;
  621. track_animation->object_id = track_animation->object->get_instance_id();
  622. track = track_animation;
  623. } break;
  624. default: {
  625. ERR_PRINT("Animation corrupted (invalid track type)");
  626. continue;
  627. }
  628. }
  629. track_cache[path] = track;
  630. } else if (track_cache_type == Animation::TYPE_POSITION_3D) {
  631. TrackCacheTransform *track_xform = static_cast<TrackCacheTransform *>(track);
  632. if (track->setup_pass != setup_pass) {
  633. track_xform->loc_used = false;
  634. track_xform->rot_used = false;
  635. track_xform->scale_used = false;
  636. }
  637. switch (track_type) {
  638. case Animation::TYPE_POSITION_3D: {
  639. track_xform->loc_used = true;
  640. } break;
  641. case Animation::TYPE_ROTATION_3D: {
  642. track_xform->rot_used = true;
  643. } break;
  644. case Animation::TYPE_SCALE_3D: {
  645. track_xform->scale_used = true;
  646. } break;
  647. default: {
  648. }
  649. }
  650. }
  651. track->setup_pass = setup_pass;
  652. }
  653. }
  654. List<NodePath> to_delete;
  655. for (const KeyValue<NodePath, TrackCache *> &K : track_cache) {
  656. TrackCache *tc = track_cache[K.key];
  657. if (tc->setup_pass != setup_pass) {
  658. to_delete.push_back(K.key);
  659. }
  660. }
  661. while (to_delete.front()) {
  662. NodePath np = to_delete.front()->get();
  663. memdelete(track_cache[np]);
  664. track_cache.erase(np);
  665. to_delete.pop_front();
  666. }
  667. state.track_map.clear();
  668. int idx = 0;
  669. for (const KeyValue<NodePath, TrackCache *> &K : track_cache) {
  670. state.track_map[K.key] = idx;
  671. idx++;
  672. }
  673. state.track_count = idx;
  674. cache_valid = true;
  675. return true;
  676. }
  677. void AnimationTree::_clear_caches() {
  678. for (KeyValue<NodePath, TrackCache *> &K : track_cache) {
  679. memdelete(K.value);
  680. }
  681. playing_caches.clear();
  682. track_cache.clear();
  683. cache_valid = false;
  684. }
  685. static void _call_object(Object *p_object, const StringName &p_method, const Vector<Variant> &p_params, bool p_deferred) {
  686. // Separate function to use alloca() more efficiently
  687. const Variant **argptrs = (const Variant **)alloca(sizeof(const Variant **) * p_params.size());
  688. const Variant *args = p_params.ptr();
  689. uint32_t argcount = p_params.size();
  690. for (uint32_t i = 0; i < argcount; i++) {
  691. argptrs[i] = &args[i];
  692. }
  693. if (p_deferred) {
  694. MessageQueue::get_singleton()->push_callp(p_object, p_method, argptrs, argcount);
  695. } else {
  696. Callable::CallError ce;
  697. p_object->callp(p_method, argptrs, argcount, ce);
  698. }
  699. }
  700. void AnimationTree::_process_graph(double p_delta) {
  701. _update_properties(); //if properties need updating, update them
  702. //check all tracks, see if they need modification
  703. root_motion_transform = Transform3D();
  704. if (!root.is_valid()) {
  705. ERR_PRINT("AnimationTree: root AnimationNode is not set, disabling playback.");
  706. set_active(false);
  707. cache_valid = false;
  708. return;
  709. }
  710. if (!has_node(animation_player)) {
  711. ERR_PRINT("AnimationTree: no valid AnimationPlayer path set, disabling playback");
  712. set_active(false);
  713. cache_valid = false;
  714. return;
  715. }
  716. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(get_node(animation_player));
  717. ObjectID current_animation_player;
  718. if (player) {
  719. current_animation_player = player->get_instance_id();
  720. }
  721. if (last_animation_player != current_animation_player) {
  722. if (last_animation_player.is_valid()) {
  723. Object *old_player = ObjectDB::get_instance(last_animation_player);
  724. if (old_player) {
  725. old_player->disconnect("caches_cleared", callable_mp(this, &AnimationTree::_clear_caches));
  726. }
  727. }
  728. if (player) {
  729. player->connect("caches_cleared", callable_mp(this, &AnimationTree::_clear_caches));
  730. }
  731. last_animation_player = current_animation_player;
  732. }
  733. if (!player) {
  734. ERR_PRINT("AnimationTree: path points to a node not an AnimationPlayer, disabling playback");
  735. set_active(false);
  736. cache_valid = false;
  737. return;
  738. }
  739. if (!cache_valid) {
  740. if (!_update_caches(player)) {
  741. return;
  742. }
  743. }
  744. { //setup
  745. process_pass++;
  746. state.valid = true;
  747. state.invalid_reasons = "";
  748. state.animation_states.clear(); //will need to be re-created
  749. state.valid = true;
  750. state.player = player;
  751. state.last_pass = process_pass;
  752. state.tree = this;
  753. // root source blends
  754. root->blends.resize(state.track_count);
  755. real_t *src_blendsw = root->blends.ptrw();
  756. for (int i = 0; i < state.track_count; i++) {
  757. src_blendsw[i] = 1.0; //by default all go to 1 for the root input
  758. }
  759. }
  760. //process
  761. {
  762. if (started) {
  763. //if started, seek
  764. root->_pre_process(SceneStringNames::get_singleton()->parameters_base_path, nullptr, &state, 0, true, false, Vector<StringName>());
  765. started = false;
  766. }
  767. root->_pre_process(SceneStringNames::get_singleton()->parameters_base_path, nullptr, &state, p_delta, false, false, Vector<StringName>());
  768. }
  769. if (!state.valid) {
  770. return; //state is not valid. do nothing.
  771. }
  772. //apply value/transform/bezier blends to track caches and execute method/audio/animation tracks
  773. {
  774. bool can_call = is_inside_tree() && !Engine::get_singleton()->is_editor_hint();
  775. for (const AnimationNode::AnimationState &as : state.animation_states) {
  776. Ref<Animation> a = as.animation;
  777. double time = as.time;
  778. double delta = as.delta;
  779. real_t weight = as.blend;
  780. bool seeked = as.seeked;
  781. int pingponged = as.pingponged;
  782. #ifndef _3D_DISABLED
  783. bool backward = signbit(delta);
  784. bool calc_root = !seeked || as.seek_root;
  785. #endif // _3D_DISABLED
  786. for (int i = 0; i < a->get_track_count(); i++) {
  787. NodePath path = a->track_get_path(i);
  788. ERR_CONTINUE(!track_cache.has(path));
  789. TrackCache *track = track_cache[path];
  790. Animation::TrackType ttype = a->track_get_type(i);
  791. if (ttype != Animation::TYPE_POSITION_3D && ttype != Animation::TYPE_ROTATION_3D && ttype != Animation::TYPE_SCALE_3D && track->type != ttype) {
  792. //broken animation, but avoid error spamming
  793. continue;
  794. }
  795. track->root_motion = root_motion_track == path;
  796. ERR_CONTINUE(!state.track_map.has(path));
  797. int blend_idx = state.track_map[path];
  798. ERR_CONTINUE(blend_idx < 0 || blend_idx >= state.track_count);
  799. real_t blend = (*as.track_blends)[blend_idx] * weight;
  800. switch (ttype) {
  801. case Animation::TYPE_POSITION_3D: {
  802. #ifndef _3D_DISABLED
  803. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  804. if (track->root_motion && calc_root) {
  805. if (t->process_pass != process_pass) {
  806. t->process_pass = process_pass;
  807. t->loc = Vector3(0, 0, 0);
  808. t->rot = Quaternion(0, 0, 0, 1);
  809. t->scale = Vector3(0, 0, 0);
  810. }
  811. double prev_time = time - delta;
  812. if (!backward) {
  813. if (prev_time < 0) {
  814. switch (a->get_loop_mode()) {
  815. case Animation::LOOP_NONE: {
  816. prev_time = 0;
  817. } break;
  818. case Animation::LOOP_LINEAR: {
  819. prev_time = Math::fposmod(prev_time, (double)a->get_length());
  820. } break;
  821. case Animation::LOOP_PINGPONG: {
  822. prev_time = Math::pingpong(prev_time, (double)a->get_length());
  823. } break;
  824. default:
  825. break;
  826. }
  827. }
  828. } else {
  829. if (prev_time > a->get_length()) {
  830. switch (a->get_loop_mode()) {
  831. case Animation::LOOP_NONE: {
  832. prev_time = (double)a->get_length();
  833. } break;
  834. case Animation::LOOP_LINEAR: {
  835. prev_time = Math::fposmod(prev_time, (double)a->get_length());
  836. } break;
  837. case Animation::LOOP_PINGPONG: {
  838. prev_time = Math::pingpong(prev_time, (double)a->get_length());
  839. } break;
  840. default:
  841. break;
  842. }
  843. }
  844. }
  845. Vector3 loc[2];
  846. if (!backward) {
  847. if (prev_time > time) {
  848. Error err = a->position_track_interpolate(i, prev_time, &loc[0]);
  849. if (err != OK) {
  850. continue;
  851. }
  852. a->position_track_interpolate(i, (double)a->get_length(), &loc[1]);
  853. t->loc += (loc[1] - loc[0]) * blend;
  854. prev_time = 0;
  855. }
  856. } else {
  857. if (prev_time < time) {
  858. Error err = a->position_track_interpolate(i, prev_time, &loc[0]);
  859. if (err != OK) {
  860. continue;
  861. }
  862. a->position_track_interpolate(i, 0, &loc[1]);
  863. t->loc += (loc[1] - loc[0]) * blend;
  864. prev_time = (double)a->get_length();
  865. }
  866. }
  867. Error err = a->position_track_interpolate(i, prev_time, &loc[0]);
  868. if (err != OK) {
  869. continue;
  870. }
  871. a->position_track_interpolate(i, time, &loc[1]);
  872. t->loc += (loc[1] - loc[0]) * blend;
  873. prev_time = !backward ? 0 : (double)a->get_length();
  874. } else {
  875. if (t->process_pass != process_pass) {
  876. t->process_pass = process_pass;
  877. t->loc = t->init_loc;
  878. t->rot = t->init_rot;
  879. t->scale = t->init_scale;
  880. }
  881. Vector3 loc;
  882. Error err = a->position_track_interpolate(i, time, &loc);
  883. if (err != OK) {
  884. continue;
  885. }
  886. t->loc += (loc - t->init_loc) * blend;
  887. }
  888. #endif // _3D_DISABLED
  889. } break;
  890. case Animation::TYPE_ROTATION_3D: {
  891. #ifndef _3D_DISABLED
  892. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  893. if (track->root_motion && calc_root) {
  894. if (t->process_pass != process_pass) {
  895. t->process_pass = process_pass;
  896. t->loc = Vector3(0, 0, 0);
  897. t->rot = Quaternion(0, 0, 0, 1);
  898. t->scale = Vector3(0, 0, 0);
  899. }
  900. double prev_time = time - delta;
  901. if (!backward) {
  902. if (prev_time < 0) {
  903. switch (a->get_loop_mode()) {
  904. case Animation::LOOP_NONE: {
  905. prev_time = 0;
  906. } break;
  907. case Animation::LOOP_LINEAR: {
  908. prev_time = Math::fposmod(prev_time, (double)a->get_length());
  909. } break;
  910. case Animation::LOOP_PINGPONG: {
  911. prev_time = Math::pingpong(prev_time, (double)a->get_length());
  912. } break;
  913. default:
  914. break;
  915. }
  916. }
  917. } else {
  918. if (prev_time > a->get_length()) {
  919. switch (a->get_loop_mode()) {
  920. case Animation::LOOP_NONE: {
  921. prev_time = (double)a->get_length();
  922. } break;
  923. case Animation::LOOP_LINEAR: {
  924. prev_time = Math::fposmod(prev_time, (double)a->get_length());
  925. } break;
  926. case Animation::LOOP_PINGPONG: {
  927. prev_time = Math::pingpong(prev_time, (double)a->get_length());
  928. } break;
  929. default:
  930. break;
  931. }
  932. }
  933. }
  934. Quaternion rot[2];
  935. if (!backward) {
  936. if (prev_time > time) {
  937. Error err = a->rotation_track_interpolate(i, prev_time, &rot[0]);
  938. if (err != OK) {
  939. continue;
  940. }
  941. a->rotation_track_interpolate(i, (double)a->get_length(), &rot[1]);
  942. t->rot = (t->rot * Quaternion().slerp(rot[0].inverse() * rot[1], blend)).normalized();
  943. prev_time = 0;
  944. }
  945. } else {
  946. if (prev_time < time) {
  947. Error err = a->rotation_track_interpolate(i, prev_time, &rot[0]);
  948. if (err != OK) {
  949. continue;
  950. }
  951. a->rotation_track_interpolate(i, 0, &rot[1]);
  952. t->rot = (t->rot * Quaternion().slerp(rot[0].inverse() * rot[1], blend)).normalized();
  953. prev_time = (double)a->get_length();
  954. }
  955. }
  956. Error err = a->rotation_track_interpolate(i, prev_time, &rot[0]);
  957. if (err != OK) {
  958. continue;
  959. }
  960. a->rotation_track_interpolate(i, time, &rot[1]);
  961. t->rot = (t->rot * Quaternion().slerp(rot[0].inverse() * rot[1], blend)).normalized();
  962. prev_time = !backward ? 0 : (double)a->get_length();
  963. } else {
  964. if (t->process_pass != process_pass) {
  965. t->process_pass = process_pass;
  966. t->loc = t->init_loc;
  967. t->rot = t->init_rot;
  968. t->scale = t->init_scale;
  969. }
  970. Quaternion rot;
  971. Error err = a->rotation_track_interpolate(i, time, &rot);
  972. if (err != OK) {
  973. continue;
  974. }
  975. t->rot = (t->rot * Quaternion().slerp(t->init_rot.inverse() * rot, blend)).normalized();
  976. }
  977. #endif // _3D_DISABLED
  978. } break;
  979. case Animation::TYPE_SCALE_3D: {
  980. #ifndef _3D_DISABLED
  981. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  982. if (track->root_motion && calc_root) {
  983. if (t->process_pass != process_pass) {
  984. t->process_pass = process_pass;
  985. t->loc = Vector3(0, 0, 0);
  986. t->rot = Quaternion(0, 0, 0, 1);
  987. t->scale = Vector3(0, 0, 0);
  988. }
  989. double prev_time = time - delta;
  990. if (!backward) {
  991. if (prev_time < 0) {
  992. switch (a->get_loop_mode()) {
  993. case Animation::LOOP_NONE: {
  994. prev_time = 0;
  995. } break;
  996. case Animation::LOOP_LINEAR: {
  997. prev_time = Math::fposmod(prev_time, (double)a->get_length());
  998. } break;
  999. case Animation::LOOP_PINGPONG: {
  1000. prev_time = Math::pingpong(prev_time, (double)a->get_length());
  1001. } break;
  1002. default:
  1003. break;
  1004. }
  1005. }
  1006. } else {
  1007. if (prev_time > a->get_length()) {
  1008. switch (a->get_loop_mode()) {
  1009. case Animation::LOOP_NONE: {
  1010. prev_time = (double)a->get_length();
  1011. } break;
  1012. case Animation::LOOP_LINEAR: {
  1013. prev_time = Math::fposmod(prev_time, (double)a->get_length());
  1014. } break;
  1015. case Animation::LOOP_PINGPONG: {
  1016. prev_time = Math::pingpong(prev_time, (double)a->get_length());
  1017. } break;
  1018. default:
  1019. break;
  1020. }
  1021. }
  1022. }
  1023. Vector3 scale[2];
  1024. if (!backward) {
  1025. if (prev_time > time) {
  1026. Error err = a->scale_track_interpolate(i, prev_time, &scale[0]);
  1027. if (err != OK) {
  1028. continue;
  1029. }
  1030. a->scale_track_interpolate(i, (double)a->get_length(), &scale[1]);
  1031. t->scale += (scale[1] - scale[0]) * blend;
  1032. prev_time = 0;
  1033. }
  1034. } else {
  1035. if (prev_time < time) {
  1036. Error err = a->scale_track_interpolate(i, prev_time, &scale[0]);
  1037. if (err != OK) {
  1038. continue;
  1039. }
  1040. a->scale_track_interpolate(i, 0, &scale[1]);
  1041. t->scale += (scale[1] - scale[0]) * blend;
  1042. prev_time = (double)a->get_length();
  1043. }
  1044. }
  1045. Error err = a->scale_track_interpolate(i, prev_time, &scale[0]);
  1046. if (err != OK) {
  1047. continue;
  1048. }
  1049. a->scale_track_interpolate(i, time, &scale[1]);
  1050. t->scale += (scale[1] - scale[0]) * blend;
  1051. prev_time = !backward ? 0 : (double)a->get_length();
  1052. } else {
  1053. if (t->process_pass != process_pass) {
  1054. t->process_pass = process_pass;
  1055. t->loc = t->init_loc;
  1056. t->rot = t->init_rot;
  1057. t->scale = t->init_scale;
  1058. }
  1059. Vector3 scale;
  1060. Error err = a->scale_track_interpolate(i, time, &scale);
  1061. if (err != OK) {
  1062. continue;
  1063. }
  1064. t->scale += (scale - t->init_scale) * blend;
  1065. }
  1066. #endif // _3D_DISABLED
  1067. } break;
  1068. case Animation::TYPE_BLEND_SHAPE: {
  1069. #ifndef _3D_DISABLED
  1070. TrackCacheBlendShape *t = static_cast<TrackCacheBlendShape *>(track);
  1071. if (t->process_pass != process_pass) {
  1072. t->process_pass = process_pass;
  1073. t->value = t->init_value;
  1074. }
  1075. float value;
  1076. Error err = a->blend_shape_track_interpolate(i, time, &value);
  1077. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  1078. if (err != OK) {
  1079. continue;
  1080. }
  1081. t->value += (value - t->init_value) * blend;
  1082. #endif // _3D_DISABLED
  1083. } break;
  1084. case Animation::TYPE_VALUE: {
  1085. TrackCacheValue *t = static_cast<TrackCacheValue *>(track);
  1086. Animation::UpdateMode update_mode = a->value_track_get_update_mode(i);
  1087. if (update_mode == Animation::UPDATE_CONTINUOUS || update_mode == Animation::UPDATE_CAPTURE) {
  1088. Variant value = a->value_track_interpolate(i, time);
  1089. if (value == Variant()) {
  1090. continue;
  1091. }
  1092. if (t->process_pass != process_pass) {
  1093. t->process_pass = process_pass;
  1094. if (!t->init_value) {
  1095. t->init_value = value;
  1096. t->init_value.zero();
  1097. }
  1098. t->value = t->init_value;
  1099. }
  1100. Variant::sub(value, t->init_value, value);
  1101. Variant::blend(t->value, value, blend, t->value);
  1102. } else {
  1103. if (blend < CMP_EPSILON) {
  1104. continue; //nothing to blend
  1105. }
  1106. List<int> indices;
  1107. a->value_track_get_key_indices(i, time, delta, &indices, pingponged);
  1108. for (int &F : indices) {
  1109. Variant value = a->track_get_key_value(i, F);
  1110. t->object->set_indexed(t->subpath, value);
  1111. }
  1112. }
  1113. } break;
  1114. case Animation::TYPE_METHOD: {
  1115. if (blend < CMP_EPSILON) {
  1116. continue; //nothing to blend
  1117. }
  1118. if (!seeked && Math::is_zero_approx(delta)) {
  1119. continue;
  1120. }
  1121. TrackCacheMethod *t = static_cast<TrackCacheMethod *>(track);
  1122. List<int> indices;
  1123. a->method_track_get_key_indices(i, time, delta, &indices, pingponged);
  1124. for (int &F : indices) {
  1125. StringName method = a->method_track_get_name(i, F);
  1126. Vector<Variant> params = a->method_track_get_params(i, F);
  1127. if (can_call) {
  1128. _call_object(t->object, method, params, true);
  1129. }
  1130. }
  1131. } break;
  1132. case Animation::TYPE_BEZIER: {
  1133. TrackCacheBezier *t = static_cast<TrackCacheBezier *>(track);
  1134. real_t bezier = a->bezier_track_interpolate(i, time);
  1135. if (t->process_pass != process_pass) {
  1136. t->process_pass = process_pass;
  1137. t->value = t->init_value;
  1138. }
  1139. t->value += (bezier - t->init_value) * blend;
  1140. } break;
  1141. case Animation::TYPE_AUDIO: {
  1142. if (blend < CMP_EPSILON) {
  1143. continue; //nothing to blend
  1144. }
  1145. TrackCacheAudio *t = static_cast<TrackCacheAudio *>(track);
  1146. if (seeked) {
  1147. //find whatever should be playing
  1148. int idx = a->track_find_key(i, time);
  1149. if (idx < 0) {
  1150. continue;
  1151. }
  1152. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  1153. if (!stream.is_valid()) {
  1154. t->object->call(SNAME("stop"));
  1155. t->playing = false;
  1156. playing_caches.erase(t);
  1157. } else {
  1158. double start_ofs = a->audio_track_get_key_start_offset(i, idx);
  1159. start_ofs += time - a->track_get_key_time(i, idx);
  1160. double end_ofs = a->audio_track_get_key_end_offset(i, idx);
  1161. double len = stream->get_length();
  1162. if (start_ofs > len - end_ofs) {
  1163. t->object->call(SNAME("stop"));
  1164. t->playing = false;
  1165. playing_caches.erase(t);
  1166. continue;
  1167. }
  1168. t->object->call(SNAME("set_stream"), stream);
  1169. t->object->call(SNAME("play"), start_ofs);
  1170. t->playing = true;
  1171. playing_caches.insert(t);
  1172. if (len && end_ofs > 0) { //force an end at a time
  1173. t->len = len - start_ofs - end_ofs;
  1174. } else {
  1175. t->len = 0;
  1176. }
  1177. t->start = time;
  1178. }
  1179. } else {
  1180. //find stuff to play
  1181. List<int> to_play;
  1182. a->track_get_key_indices_in_range(i, time, delta, &to_play, pingponged);
  1183. if (to_play.size()) {
  1184. int idx = to_play.back()->get();
  1185. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  1186. if (!stream.is_valid()) {
  1187. t->object->call(SNAME("stop"));
  1188. t->playing = false;
  1189. playing_caches.erase(t);
  1190. } else {
  1191. double start_ofs = a->audio_track_get_key_start_offset(i, idx);
  1192. double end_ofs = a->audio_track_get_key_end_offset(i, idx);
  1193. double len = stream->get_length();
  1194. t->object->call(SNAME("set_stream"), stream);
  1195. t->object->call(SNAME("play"), start_ofs);
  1196. t->playing = true;
  1197. playing_caches.insert(t);
  1198. if (len && end_ofs > 0) { //force an end at a time
  1199. t->len = len - start_ofs - end_ofs;
  1200. } else {
  1201. t->len = 0;
  1202. }
  1203. t->start = time;
  1204. }
  1205. } else if (t->playing) {
  1206. bool loop = a->get_loop_mode() != Animation::LOOP_NONE;
  1207. bool stop = false;
  1208. if (!loop) {
  1209. if (delta > 0) {
  1210. if (time < t->start) {
  1211. stop = true;
  1212. }
  1213. } else if (delta < 0) {
  1214. if (time > t->start) {
  1215. stop = true;
  1216. }
  1217. }
  1218. } else if (t->len > 0) {
  1219. double len = t->start > time ? (a->get_length() - t->start) + time : time - t->start;
  1220. if (len > t->len) {
  1221. stop = true;
  1222. }
  1223. }
  1224. if (stop) {
  1225. //time to stop
  1226. t->object->call(SNAME("stop"));
  1227. t->playing = false;
  1228. playing_caches.erase(t);
  1229. }
  1230. }
  1231. }
  1232. real_t db = Math::linear2db(MAX(blend, 0.00001));
  1233. if (t->object->has_method(SNAME("set_unit_db"))) {
  1234. t->object->call(SNAME("set_unit_db"), db);
  1235. } else {
  1236. t->object->call(SNAME("set_volume_db"), db);
  1237. }
  1238. } break;
  1239. case Animation::TYPE_ANIMATION: {
  1240. if (blend < CMP_EPSILON) {
  1241. continue; //nothing to blend
  1242. }
  1243. TrackCacheAnimation *t = static_cast<TrackCacheAnimation *>(track);
  1244. AnimationPlayer *player2 = Object::cast_to<AnimationPlayer>(t->object);
  1245. if (!player2) {
  1246. continue;
  1247. }
  1248. if (seeked) {
  1249. //seek
  1250. int idx = a->track_find_key(i, time);
  1251. if (idx < 0) {
  1252. continue;
  1253. }
  1254. double pos = a->track_get_key_time(i, idx);
  1255. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  1256. if (String(anim_name) == "[stop]" || !player2->has_animation(anim_name)) {
  1257. continue;
  1258. }
  1259. Ref<Animation> anim = player2->get_animation(anim_name);
  1260. double at_anim_pos = 0.0;
  1261. switch (anim->get_loop_mode()) {
  1262. case Animation::LOOP_NONE: {
  1263. at_anim_pos = MAX((double)anim->get_length(), time - pos); //seek to end
  1264. } break;
  1265. case Animation::LOOP_LINEAR: {
  1266. at_anim_pos = Math::fposmod(time - pos, (double)anim->get_length()); //seek to loop
  1267. } break;
  1268. case Animation::LOOP_PINGPONG: {
  1269. at_anim_pos = Math::pingpong(time - pos, (double)a->get_length());
  1270. } break;
  1271. default:
  1272. break;
  1273. }
  1274. if (player2->is_playing() || seeked) {
  1275. player2->play(anim_name);
  1276. player2->seek(at_anim_pos);
  1277. t->playing = true;
  1278. playing_caches.insert(t);
  1279. } else {
  1280. player2->set_assigned_animation(anim_name);
  1281. player2->seek(at_anim_pos, true);
  1282. }
  1283. } else {
  1284. //find stuff to play
  1285. List<int> to_play;
  1286. a->track_get_key_indices_in_range(i, time, delta, &to_play, pingponged);
  1287. if (to_play.size()) {
  1288. int idx = to_play.back()->get();
  1289. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  1290. if (String(anim_name) == "[stop]" || !player2->has_animation(anim_name)) {
  1291. if (playing_caches.has(t)) {
  1292. playing_caches.erase(t);
  1293. player2->stop();
  1294. t->playing = false;
  1295. }
  1296. } else {
  1297. player2->play(anim_name);
  1298. t->playing = true;
  1299. playing_caches.insert(t);
  1300. }
  1301. }
  1302. }
  1303. } break;
  1304. }
  1305. }
  1306. }
  1307. }
  1308. {
  1309. // finally, set the tracks
  1310. for (const KeyValue<NodePath, TrackCache *> &K : track_cache) {
  1311. TrackCache *track = K.value;
  1312. if (track->process_pass != process_pass) {
  1313. continue; //not processed, ignore
  1314. }
  1315. switch (track->type) {
  1316. case Animation::TYPE_POSITION_3D: {
  1317. #ifndef _3D_DISABLED
  1318. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  1319. if (t->root_motion) {
  1320. Transform3D xform;
  1321. xform.origin = t->loc;
  1322. xform.basis.set_quaternion_scale(t->rot, Vector3(1, 1, 1) + t->scale);
  1323. root_motion_transform = xform;
  1324. } else if (t->skeleton && t->bone_idx >= 0) {
  1325. if (t->loc_used) {
  1326. t->skeleton->set_bone_pose_position(t->bone_idx, t->loc);
  1327. }
  1328. if (t->rot_used) {
  1329. t->skeleton->set_bone_pose_rotation(t->bone_idx, t->rot);
  1330. }
  1331. if (t->scale_used) {
  1332. t->skeleton->set_bone_pose_scale(t->bone_idx, t->scale);
  1333. }
  1334. } else if (!t->skeleton) {
  1335. if (t->loc_used) {
  1336. t->node_3d->set_position(t->loc);
  1337. }
  1338. if (t->rot_used) {
  1339. t->node_3d->set_rotation(t->rot.get_euler());
  1340. }
  1341. if (t->scale_used) {
  1342. t->node_3d->set_scale(t->scale);
  1343. }
  1344. }
  1345. #endif // _3D_DISABLED
  1346. } break;
  1347. case Animation::TYPE_BLEND_SHAPE: {
  1348. #ifndef _3D_DISABLED
  1349. TrackCacheBlendShape *t = static_cast<TrackCacheBlendShape *>(track);
  1350. if (t->mesh_3d) {
  1351. t->mesh_3d->set_blend_shape_value(t->shape_index, t->value);
  1352. }
  1353. #endif // _3D_DISABLED
  1354. } break;
  1355. case Animation::TYPE_VALUE: {
  1356. TrackCacheValue *t = static_cast<TrackCacheValue *>(track);
  1357. t->object->set_indexed(t->subpath, t->value);
  1358. } break;
  1359. case Animation::TYPE_BEZIER: {
  1360. TrackCacheBezier *t = static_cast<TrackCacheBezier *>(track);
  1361. t->object->set_indexed(t->subpath, t->value);
  1362. } break;
  1363. default: {
  1364. } //the rest don't matter
  1365. }
  1366. }
  1367. }
  1368. }
  1369. void AnimationTree::advance(real_t p_time) {
  1370. _process_graph(p_time);
  1371. }
  1372. void AnimationTree::_notification(int p_what) {
  1373. switch (p_what) {
  1374. case NOTIFICATION_ENTER_TREE: {
  1375. if (last_animation_player.is_valid()) {
  1376. Object *player = ObjectDB::get_instance(last_animation_player);
  1377. if (player) {
  1378. player->connect("caches_cleared", callable_mp(this, &AnimationTree::_clear_caches));
  1379. }
  1380. }
  1381. } break;
  1382. case NOTIFICATION_EXIT_TREE: {
  1383. _clear_caches();
  1384. if (last_animation_player.is_valid()) {
  1385. Object *player = ObjectDB::get_instance(last_animation_player);
  1386. if (player) {
  1387. player->disconnect("caches_cleared", callable_mp(this, &AnimationTree::_clear_caches));
  1388. }
  1389. }
  1390. } break;
  1391. case NOTIFICATION_INTERNAL_PROCESS: {
  1392. if (active && process_callback == ANIMATION_PROCESS_IDLE) {
  1393. _process_graph(get_process_delta_time());
  1394. }
  1395. } break;
  1396. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  1397. if (active && process_callback == ANIMATION_PROCESS_PHYSICS) {
  1398. _process_graph(get_physics_process_delta_time());
  1399. }
  1400. } break;
  1401. }
  1402. }
  1403. void AnimationTree::set_animation_player(const NodePath &p_player) {
  1404. animation_player = p_player;
  1405. update_configuration_warnings();
  1406. }
  1407. NodePath AnimationTree::get_animation_player() const {
  1408. return animation_player;
  1409. }
  1410. bool AnimationTree::is_state_invalid() const {
  1411. return !state.valid;
  1412. }
  1413. String AnimationTree::get_invalid_state_reason() const {
  1414. return state.invalid_reasons;
  1415. }
  1416. uint64_t AnimationTree::get_last_process_pass() const {
  1417. return process_pass;
  1418. }
  1419. TypedArray<String> AnimationTree::get_configuration_warnings() const {
  1420. TypedArray<String> warnings = Node::get_configuration_warnings();
  1421. if (!root.is_valid()) {
  1422. warnings.push_back(RTR("No root AnimationNode for the graph is set."));
  1423. }
  1424. if (!has_node(animation_player)) {
  1425. warnings.push_back(RTR("Path to an AnimationPlayer node containing animations is not set."));
  1426. } else {
  1427. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(get_node(animation_player));
  1428. if (!player) {
  1429. warnings.push_back(RTR("Path set for AnimationPlayer does not lead to an AnimationPlayer node."));
  1430. } else if (!player->has_node(player->get_root())) {
  1431. warnings.push_back(RTR("The AnimationPlayer root node is not a valid node."));
  1432. }
  1433. }
  1434. return warnings;
  1435. }
  1436. void AnimationTree::set_root_motion_track(const NodePath &p_track) {
  1437. root_motion_track = p_track;
  1438. }
  1439. NodePath AnimationTree::get_root_motion_track() const {
  1440. return root_motion_track;
  1441. }
  1442. Transform3D AnimationTree::get_root_motion_transform() const {
  1443. return root_motion_transform;
  1444. }
  1445. void AnimationTree::_tree_changed() {
  1446. if (properties_dirty) {
  1447. return;
  1448. }
  1449. call_deferred(SNAME("_update_properties"));
  1450. properties_dirty = true;
  1451. }
  1452. void AnimationTree::_update_properties_for_node(const String &p_base_path, Ref<AnimationNode> node) {
  1453. ERR_FAIL_COND(node.is_null());
  1454. if (!property_parent_map.has(p_base_path)) {
  1455. property_parent_map[p_base_path] = HashMap<StringName, StringName>();
  1456. }
  1457. if (node->get_input_count() && !input_activity_map.has(p_base_path)) {
  1458. Vector<Activity> activity;
  1459. for (int i = 0; i < node->get_input_count(); i++) {
  1460. Activity a;
  1461. a.activity = 0;
  1462. a.last_pass = 0;
  1463. activity.push_back(a);
  1464. }
  1465. input_activity_map[p_base_path] = activity;
  1466. input_activity_map_get[String(p_base_path).substr(0, String(p_base_path).length() - 1)] = &input_activity_map[p_base_path];
  1467. }
  1468. List<PropertyInfo> plist;
  1469. node->get_parameter_list(&plist);
  1470. for (PropertyInfo &pinfo : plist) {
  1471. StringName key = pinfo.name;
  1472. if (!property_map.has(p_base_path + key)) {
  1473. property_map[p_base_path + key] = node->get_parameter_default_value(key);
  1474. }
  1475. property_parent_map[p_base_path][key] = p_base_path + key;
  1476. pinfo.name = p_base_path + key;
  1477. properties.push_back(pinfo);
  1478. }
  1479. List<AnimationNode::ChildNode> children;
  1480. node->get_child_nodes(&children);
  1481. for (const AnimationNode::ChildNode &E : children) {
  1482. _update_properties_for_node(p_base_path + E.name + "/", E.node);
  1483. }
  1484. }
  1485. void AnimationTree::_update_properties() {
  1486. if (!properties_dirty) {
  1487. return;
  1488. }
  1489. properties.clear();
  1490. property_parent_map.clear();
  1491. input_activity_map.clear();
  1492. input_activity_map_get.clear();
  1493. if (root.is_valid()) {
  1494. _update_properties_for_node(SceneStringNames::get_singleton()->parameters_base_path, root);
  1495. }
  1496. properties_dirty = false;
  1497. notify_property_list_changed();
  1498. }
  1499. bool AnimationTree::_set(const StringName &p_name, const Variant &p_value) {
  1500. if (properties_dirty) {
  1501. _update_properties();
  1502. }
  1503. if (property_map.has(p_name)) {
  1504. property_map[p_name] = p_value;
  1505. return true;
  1506. }
  1507. return false;
  1508. }
  1509. bool AnimationTree::_get(const StringName &p_name, Variant &r_ret) const {
  1510. if (properties_dirty) {
  1511. const_cast<AnimationTree *>(this)->_update_properties();
  1512. }
  1513. if (property_map.has(p_name)) {
  1514. r_ret = property_map[p_name];
  1515. return true;
  1516. }
  1517. return false;
  1518. }
  1519. void AnimationTree::_get_property_list(List<PropertyInfo> *p_list) const {
  1520. if (properties_dirty) {
  1521. const_cast<AnimationTree *>(this)->_update_properties();
  1522. }
  1523. for (const PropertyInfo &E : properties) {
  1524. p_list->push_back(E);
  1525. }
  1526. }
  1527. void AnimationTree::rename_parameter(const String &p_base, const String &p_new_base) {
  1528. //rename values first
  1529. for (const PropertyInfo &E : properties) {
  1530. if (E.name.begins_with(p_base)) {
  1531. String new_name = E.name.replace_first(p_base, p_new_base);
  1532. property_map[new_name] = property_map[E.name];
  1533. }
  1534. }
  1535. //update tree second
  1536. properties_dirty = true;
  1537. _update_properties();
  1538. }
  1539. real_t AnimationTree::get_connection_activity(const StringName &p_path, int p_connection) const {
  1540. if (!input_activity_map_get.has(p_path)) {
  1541. return 0;
  1542. }
  1543. const Vector<Activity> *activity = input_activity_map_get[p_path];
  1544. if (!activity || p_connection < 0 || p_connection >= activity->size()) {
  1545. return 0;
  1546. }
  1547. if ((*activity)[p_connection].last_pass != process_pass) {
  1548. return 0;
  1549. }
  1550. return (*activity)[p_connection].activity;
  1551. }
  1552. void AnimationTree::_bind_methods() {
  1553. ClassDB::bind_method(D_METHOD("set_active", "active"), &AnimationTree::set_active);
  1554. ClassDB::bind_method(D_METHOD("is_active"), &AnimationTree::is_active);
  1555. ClassDB::bind_method(D_METHOD("set_tree_root", "root"), &AnimationTree::set_tree_root);
  1556. ClassDB::bind_method(D_METHOD("get_tree_root"), &AnimationTree::get_tree_root);
  1557. ClassDB::bind_method(D_METHOD("set_process_callback", "mode"), &AnimationTree::set_process_callback);
  1558. ClassDB::bind_method(D_METHOD("get_process_callback"), &AnimationTree::get_process_callback);
  1559. ClassDB::bind_method(D_METHOD("set_animation_player", "root"), &AnimationTree::set_animation_player);
  1560. ClassDB::bind_method(D_METHOD("get_animation_player"), &AnimationTree::get_animation_player);
  1561. ClassDB::bind_method(D_METHOD("set_root_motion_track", "path"), &AnimationTree::set_root_motion_track);
  1562. ClassDB::bind_method(D_METHOD("get_root_motion_track"), &AnimationTree::get_root_motion_track);
  1563. ClassDB::bind_method(D_METHOD("get_root_motion_transform"), &AnimationTree::get_root_motion_transform);
  1564. ClassDB::bind_method(D_METHOD("_update_properties"), &AnimationTree::_update_properties);
  1565. ClassDB::bind_method(D_METHOD("rename_parameter", "old_name", "new_name"), &AnimationTree::rename_parameter);
  1566. ClassDB::bind_method(D_METHOD("advance", "delta"), &AnimationTree::advance);
  1567. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tree_root", PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode"), "set_tree_root", "get_tree_root");
  1568. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "anim_player", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "AnimationPlayer"), "set_animation_player", "get_animation_player");
  1569. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "active"), "set_active", "is_active");
  1570. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_callback", PROPERTY_HINT_ENUM, "Physics,Idle,Manual"), "set_process_callback", "get_process_callback");
  1571. ADD_GROUP("Root Motion", "root_motion_");
  1572. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "root_motion_track"), "set_root_motion_track", "get_root_motion_track");
  1573. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_PHYSICS);
  1574. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_IDLE);
  1575. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_MANUAL);
  1576. }
  1577. AnimationTree::AnimationTree() {
  1578. }
  1579. AnimationTree::~AnimationTree() {
  1580. }