animation_tree.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. /*************************************************************************/
  2. /* animation_tree.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/engine.h"
  33. #include "core/method_bind_ext.gen.inc"
  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. if (get_script_instance()) {
  38. Array parameters = get_script_instance()->call("get_parameter_list");
  39. for (int i = 0; i < parameters.size(); i++) {
  40. Dictionary d = parameters[i];
  41. ERR_CONTINUE(d.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. if (get_script_instance()) {
  48. return get_script_instance()->call("get_parameter_default_value");
  49. }
  50. return Variant();
  51. }
  52. void AnimationNode::set_parameter(const StringName &p_name, const Variant &p_value) {
  53. ERR_FAIL_COND(!state);
  54. ERR_FAIL_COND(!state->tree->property_parent_map.has(base_path));
  55. ERR_FAIL_COND(!state->tree->property_parent_map[base_path].has(p_name));
  56. StringName path = state->tree->property_parent_map[base_path][p_name];
  57. state->tree->property_map[path] = p_value;
  58. }
  59. Variant AnimationNode::get_parameter(const StringName &p_name) const {
  60. ERR_FAIL_COND_V(!state, Variant());
  61. ERR_FAIL_COND_V(!state->tree->property_parent_map.has(base_path), Variant());
  62. ERR_FAIL_COND_V(!state->tree->property_parent_map[base_path].has(p_name), Variant());
  63. StringName path = state->tree->property_parent_map[base_path][p_name];
  64. return state->tree->property_map[path];
  65. }
  66. void AnimationNode::get_child_nodes(List<ChildNode> *r_child_nodes) {
  67. if (get_script_instance()) {
  68. Dictionary cn = get_script_instance()->call("get_child_nodes");
  69. List<Variant> keys;
  70. cn.get_key_list(&keys);
  71. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  72. ChildNode child;
  73. child.name = E->get();
  74. child.node = cn[E->get()];
  75. r_child_nodes->push_back(child);
  76. }
  77. }
  78. }
  79. void AnimationNode::blend_animation(const StringName &p_animation, float p_time, float p_delta, bool p_seeked, float p_blend) {
  80. ERR_FAIL_COND(!state);
  81. ERR_FAIL_COND(!state->player->has_animation(p_animation));
  82. Ref<Animation> animation = state->player->get_animation(p_animation);
  83. if (animation.is_null()) {
  84. AnimationNodeBlendTree *btree = Object::cast_to<AnimationNodeBlendTree>(parent);
  85. if (btree) {
  86. String name = btree->get_node_name(Ref<AnimationNodeAnimation>(this));
  87. make_invalid(vformat(RTR("In node '%s', invalid animation: '%s'."), name, p_animation));
  88. } else {
  89. make_invalid(vformat(RTR("Invalid animation: '%s'."), p_animation));
  90. }
  91. return;
  92. }
  93. ERR_FAIL_COND(!animation.is_valid());
  94. AnimationState anim_state;
  95. anim_state.blend = p_blend;
  96. anim_state.track_blends = &blends;
  97. anim_state.delta = p_delta;
  98. anim_state.time = p_time;
  99. anim_state.animation = animation;
  100. anim_state.seeked = p_seeked;
  101. state->animation_states.push_back(anim_state);
  102. }
  103. float AnimationNode::_pre_process(const StringName &p_base_path, AnimationNode *p_parent, State *p_state, float p_time, bool p_seek, const Vector<StringName> &p_connections) {
  104. base_path = p_base_path;
  105. parent = p_parent;
  106. connections = p_connections;
  107. state = p_state;
  108. float t = process(p_time, p_seek);
  109. state = NULL;
  110. parent = NULL;
  111. base_path = StringName();
  112. connections.clear();
  113. return t;
  114. }
  115. void AnimationNode::make_invalid(const String &p_reason) {
  116. ERR_FAIL_COND(!state);
  117. state->valid = false;
  118. if (state->invalid_reasons != String()) {
  119. state->invalid_reasons += "\n";
  120. }
  121. state->invalid_reasons += "- " + p_reason;
  122. }
  123. float AnimationNode::blend_input(int p_input, float p_time, bool p_seek, float p_blend, FilterAction p_filter, bool p_optimize) {
  124. ERR_FAIL_INDEX_V(p_input, inputs.size(), 0);
  125. ERR_FAIL_COND_V(!state, 0);
  126. AnimationNodeBlendTree *blend_tree = Object::cast_to<AnimationNodeBlendTree>(parent);
  127. ERR_FAIL_COND_V(!blend_tree, 0);
  128. StringName node_name = connections[p_input];
  129. if (!blend_tree->has_node(node_name)) {
  130. String name = blend_tree->get_node_name(Ref<AnimationNode>(this));
  131. make_invalid(vformat(RTR("Nothing connected to input '%s' of node '%s'."), get_input_name(p_input), name));
  132. return 0;
  133. }
  134. Ref<AnimationNode> node = blend_tree->get_node(node_name);
  135. //inputs.write[p_input].last_pass = state->last_pass;
  136. float activity = 0;
  137. float ret = _blend_node(node_name, blend_tree->get_node_connection_array(node_name), NULL, node, p_time, p_seek, p_blend, p_filter, p_optimize, &activity);
  138. Vector<AnimationTree::Activity> *activity_ptr = state->tree->input_activity_map.getptr(base_path);
  139. if (activity_ptr && p_input < activity_ptr->size()) {
  140. activity_ptr->write[p_input].last_pass = state->last_pass;
  141. activity_ptr->write[p_input].activity = activity;
  142. }
  143. return ret;
  144. }
  145. float AnimationNode::blend_node(const StringName &p_sub_path, Ref<AnimationNode> p_node, float p_time, bool p_seek, float p_blend, FilterAction p_filter, bool p_optimize) {
  146. return _blend_node(p_sub_path, Vector<StringName>(), this, p_node, p_time, p_seek, p_blend, p_filter, p_optimize);
  147. }
  148. float AnimationNode::_blend_node(const StringName &p_subpath, const Vector<StringName> &p_connections, AnimationNode *p_new_parent, Ref<AnimationNode> p_node, float p_time, bool p_seek, float p_blend, FilterAction p_filter, bool p_optimize, float *r_max) {
  149. ERR_FAIL_COND_V(!p_node.is_valid(), 0);
  150. ERR_FAIL_COND_V(!state, 0);
  151. int blend_count = blends.size();
  152. if (p_node->blends.size() != blend_count) {
  153. p_node->blends.resize(blend_count);
  154. }
  155. float *blendw = p_node->blends.ptrw();
  156. const float *blendr = blends.ptr();
  157. bool any_valid = false;
  158. if (has_filter() && is_filter_enabled() && p_filter != FILTER_IGNORE) {
  159. for (int i = 0; i < blend_count; i++) {
  160. blendw[i] = 0.0; //all to zero by default
  161. }
  162. const NodePath *K = NULL;
  163. while ((K = filter.next(K))) {
  164. if (!state->track_map.has(*K)) {
  165. continue;
  166. }
  167. int idx = state->track_map[*K];
  168. blendw[idx] = 1.0; //filtered goes to one
  169. }
  170. switch (p_filter) {
  171. case FILTER_IGNORE:
  172. break; //will not happen anyway
  173. case FILTER_PASS: {
  174. //values filtered pass, the rest don't
  175. for (int i = 0; i < blend_count; i++) {
  176. if (blendw[i] == 0) //not filtered, does not pass
  177. continue;
  178. blendw[i] = blendr[i] * p_blend;
  179. if (blendw[i] > CMP_EPSILON) {
  180. any_valid = true;
  181. }
  182. }
  183. } break;
  184. case FILTER_STOP: {
  185. //values filtered don't pass, the rest are blended
  186. for (int i = 0; i < blend_count; i++) {
  187. if (blendw[i] > 0) //filtered, does not pass
  188. continue;
  189. blendw[i] = blendr[i] * p_blend;
  190. if (blendw[i] > CMP_EPSILON) {
  191. any_valid = true;
  192. }
  193. }
  194. } break;
  195. case FILTER_BLEND: {
  196. //filtered values are blended, the rest are passed without blending
  197. for (int i = 0; i < blend_count; i++) {
  198. if (blendw[i] == 1.0) {
  199. blendw[i] = blendr[i] * p_blend; //filtered, blend
  200. } else {
  201. blendw[i] = blendr[i]; //not filtered, do not blend
  202. }
  203. if (blendw[i] > CMP_EPSILON) {
  204. any_valid = true;
  205. }
  206. }
  207. } break;
  208. }
  209. } else {
  210. for (int i = 0; i < blend_count; i++) {
  211. //regular blend
  212. blendw[i] = blendr[i] * p_blend;
  213. if (blendw[i] > CMP_EPSILON) {
  214. any_valid = true;
  215. }
  216. }
  217. }
  218. if (r_max) {
  219. *r_max = 0;
  220. for (int i = 0; i < blend_count; i++) {
  221. *r_max = MAX(*r_max, blendw[i]);
  222. }
  223. }
  224. if (!p_seek && p_optimize && !any_valid) //pointless to go on, all are zero
  225. return 0;
  226. String new_path;
  227. AnimationNode *new_parent;
  228. //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
  229. if (p_new_parent) {
  230. new_parent = p_new_parent;
  231. new_path = String(base_path) + String(p_subpath) + "/";
  232. } else {
  233. ERR_FAIL_COND_V(!parent, 0);
  234. new_parent = parent;
  235. new_path = String(parent->base_path) + String(p_subpath) + "/";
  236. }
  237. return p_node->_pre_process(new_path, new_parent, state, p_time, p_seek, p_connections);
  238. }
  239. int AnimationNode::get_input_count() const {
  240. return inputs.size();
  241. }
  242. String AnimationNode::get_input_name(int p_input) {
  243. ERR_FAIL_INDEX_V(p_input, inputs.size(), String());
  244. return inputs[p_input].name;
  245. }
  246. String AnimationNode::get_caption() const {
  247. if (get_script_instance()) {
  248. return get_script_instance()->call("get_caption");
  249. }
  250. return "Node";
  251. }
  252. void AnimationNode::add_input(const String &p_name) {
  253. //root nodes can't add inputs
  254. ERR_FAIL_COND(Object::cast_to<AnimationRootNode>(this) != NULL)
  255. Input input;
  256. ERR_FAIL_COND(p_name.find(".") != -1 || p_name.find("/") != -1);
  257. input.name = p_name;
  258. inputs.push_back(input);
  259. emit_changed();
  260. }
  261. void AnimationNode::set_input_name(int p_input, const String &p_name) {
  262. ERR_FAIL_INDEX(p_input, inputs.size());
  263. ERR_FAIL_COND(p_name.find(".") != -1 || p_name.find("/") != -1);
  264. inputs.write[p_input].name = p_name;
  265. emit_changed();
  266. }
  267. void AnimationNode::remove_input(int p_index) {
  268. ERR_FAIL_INDEX(p_index, inputs.size());
  269. inputs.remove(p_index);
  270. emit_changed();
  271. }
  272. float AnimationNode::process(float p_time, bool p_seek) {
  273. if (get_script_instance()) {
  274. return get_script_instance()->call("process", p_time, p_seek);
  275. }
  276. return 0;
  277. }
  278. void AnimationNode::set_filter_path(const NodePath &p_path, bool p_enable) {
  279. if (p_enable) {
  280. filter[p_path] = true;
  281. } else {
  282. filter.erase(p_path);
  283. }
  284. }
  285. void AnimationNode::set_filter_enabled(bool p_enable) {
  286. filter_enabled = p_enable;
  287. }
  288. bool AnimationNode::is_filter_enabled() const {
  289. return filter_enabled;
  290. }
  291. bool AnimationNode::is_path_filtered(const NodePath &p_path) const {
  292. return filter.has(p_path);
  293. }
  294. bool AnimationNode::has_filter() const {
  295. return false;
  296. }
  297. Array AnimationNode::_get_filters() const {
  298. Array paths;
  299. const NodePath *K = NULL;
  300. while ((K = filter.next(K))) {
  301. paths.push_back(String(*K)); //use strings, so sorting is possible
  302. }
  303. paths.sort(); //done so every time the scene is saved, it does not change
  304. return paths;
  305. }
  306. void AnimationNode::_set_filters(const Array &p_filters) {
  307. filter.clear();
  308. for (int i = 0; i < p_filters.size(); i++) {
  309. set_filter_path(p_filters[i], true);
  310. }
  311. }
  312. void AnimationNode::_validate_property(PropertyInfo &property) const {
  313. if (!has_filter() && (property.name == "filter_enabled" || property.name == "filters")) {
  314. property.usage = 0;
  315. }
  316. }
  317. Ref<AnimationNode> AnimationNode::get_child_by_name(const StringName &p_name) {
  318. if (get_script_instance()) {
  319. return get_script_instance()->call("get_child_by_name");
  320. }
  321. return Ref<AnimationNode>();
  322. }
  323. void AnimationNode::_bind_methods() {
  324. ClassDB::bind_method(D_METHOD("get_input_count"), &AnimationNode::get_input_count);
  325. ClassDB::bind_method(D_METHOD("get_input_name", "input"), &AnimationNode::get_input_name);
  326. ClassDB::bind_method(D_METHOD("add_input", "name"), &AnimationNode::add_input);
  327. ClassDB::bind_method(D_METHOD("remove_input", "index"), &AnimationNode::remove_input);
  328. ClassDB::bind_method(D_METHOD("set_filter_path", "path", "enable"), &AnimationNode::set_filter_path);
  329. ClassDB::bind_method(D_METHOD("is_path_filtered", "path"), &AnimationNode::is_path_filtered);
  330. ClassDB::bind_method(D_METHOD("set_filter_enabled", "enable"), &AnimationNode::set_filter_enabled);
  331. ClassDB::bind_method(D_METHOD("is_filter_enabled"), &AnimationNode::is_filter_enabled);
  332. ClassDB::bind_method(D_METHOD("_set_filters", "filters"), &AnimationNode::_set_filters);
  333. ClassDB::bind_method(D_METHOD("_get_filters"), &AnimationNode::_get_filters);
  334. ClassDB::bind_method(D_METHOD("blend_animation", "animation", "time", "delta", "seeked", "blend"), &AnimationNode::blend_animation);
  335. ClassDB::bind_method(D_METHOD("blend_node", "name", "node", "time", "seek", "blend", "filter", "optimize"), &AnimationNode::blend_node, DEFVAL(FILTER_IGNORE), DEFVAL(true));
  336. ClassDB::bind_method(D_METHOD("blend_input", "input_index", "time", "seek", "blend", "filter", "optimize"), &AnimationNode::blend_input, DEFVAL(FILTER_IGNORE), DEFVAL(true));
  337. ClassDB::bind_method(D_METHOD("set_parameter", "name", "value"), &AnimationNode::set_parameter);
  338. ClassDB::bind_method(D_METHOD("get_parameter", "name"), &AnimationNode::get_parameter);
  339. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_filter_enabled", "is_filter_enabled");
  340. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "filters", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_filters", "_get_filters");
  341. BIND_VMETHOD(MethodInfo(Variant::DICTIONARY, "get_child_nodes"));
  342. BIND_VMETHOD(MethodInfo(Variant::ARRAY, "get_parameter_list"));
  343. BIND_VMETHOD(MethodInfo(Variant::OBJECT, "get_child_by_name", PropertyInfo(Variant::STRING, "name")));
  344. {
  345. MethodInfo mi = MethodInfo(Variant::NIL, "get_parameter_default_value", PropertyInfo(Variant::STRING, "name"));
  346. mi.return_val.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  347. BIND_VMETHOD(mi);
  348. }
  349. BIND_VMETHOD(MethodInfo("process", PropertyInfo(Variant::REAL, "time"), PropertyInfo(Variant::BOOL, "seek")));
  350. BIND_VMETHOD(MethodInfo(Variant::STRING, "get_caption"));
  351. BIND_VMETHOD(MethodInfo(Variant::STRING, "has_filter"));
  352. ADD_SIGNAL(MethodInfo("removed_from_graph"));
  353. ADD_SIGNAL(MethodInfo("tree_changed"));
  354. BIND_ENUM_CONSTANT(FILTER_IGNORE);
  355. BIND_ENUM_CONSTANT(FILTER_PASS);
  356. BIND_ENUM_CONSTANT(FILTER_STOP);
  357. BIND_ENUM_CONSTANT(FILTER_BLEND);
  358. }
  359. AnimationNode::AnimationNode() {
  360. state = NULL;
  361. parent = NULL;
  362. filter_enabled = false;
  363. }
  364. ////////////////////
  365. void AnimationTree::set_tree_root(const Ref<AnimationNode> &p_root) {
  366. if (root.is_valid()) {
  367. root->disconnect("tree_changed", this, "_tree_changed");
  368. }
  369. root = p_root;
  370. if (root.is_valid()) {
  371. root->connect("tree_changed", this, "_tree_changed");
  372. }
  373. properties_dirty = true;
  374. update_configuration_warning();
  375. }
  376. Ref<AnimationNode> AnimationTree::get_tree_root() const {
  377. return root;
  378. }
  379. void AnimationTree::set_active(bool p_active) {
  380. if (active == p_active)
  381. return;
  382. active = p_active;
  383. started = active;
  384. if (process_mode == ANIMATION_PROCESS_IDLE) {
  385. set_process_internal(active);
  386. } else {
  387. set_physics_process_internal(active);
  388. }
  389. if (!active && is_inside_tree()) {
  390. for (Set<TrackCache *>::Element *E = playing_caches.front(); E; E = E->next()) {
  391. if (ObjectDB::get_instance(E->get()->object_id)) {
  392. E->get()->object->call("stop");
  393. }
  394. }
  395. playing_caches.clear();
  396. }
  397. }
  398. bool AnimationTree::is_active() const {
  399. return active;
  400. }
  401. void AnimationTree::set_process_mode(AnimationProcessMode p_mode) {
  402. if (process_mode == p_mode)
  403. return;
  404. bool was_active = is_active();
  405. if (was_active) {
  406. set_active(false);
  407. }
  408. process_mode = p_mode;
  409. if (was_active) {
  410. set_active(true);
  411. }
  412. }
  413. AnimationTree::AnimationProcessMode AnimationTree::get_process_mode() const {
  414. return process_mode;
  415. }
  416. void AnimationTree::_node_removed(Node *p_node) {
  417. cache_valid = false;
  418. }
  419. bool AnimationTree::_update_caches(AnimationPlayer *player) {
  420. setup_pass++;
  421. if (!player->has_node(player->get_root())) {
  422. ERR_PRINT("AnimationTree: AnimationPlayer root is invalid.");
  423. set_active(false);
  424. return false;
  425. }
  426. Node *parent = player->get_node(player->get_root());
  427. List<StringName> sname;
  428. player->get_animation_list(&sname);
  429. for (List<StringName>::Element *E = sname.front(); E; E = E->next()) {
  430. Ref<Animation> anim = player->get_animation(E->get());
  431. for (int i = 0; i < anim->get_track_count(); i++) {
  432. NodePath path = anim->track_get_path(i);
  433. Animation::TrackType track_type = anim->track_get_type(i);
  434. TrackCache *track = NULL;
  435. if (track_cache.has(path)) {
  436. track = track_cache.get(path);
  437. }
  438. //if not valid, delete track
  439. if (track && (track->type != track_type || ObjectDB::get_instance(track->object_id) == NULL)) {
  440. playing_caches.erase(track);
  441. memdelete(track);
  442. track_cache.erase(path);
  443. track = NULL;
  444. }
  445. if (!track) {
  446. RES resource;
  447. Vector<StringName> leftover_path;
  448. Node *child = parent->get_node_and_resource(path, resource, leftover_path);
  449. if (!child) {
  450. ERR_PRINTS("AnimationTree: '" + String(E->get()) + "', couldn't resolve track: '" + String(path) + "'");
  451. continue;
  452. }
  453. if (!child->is_connected("tree_exited", this, "_node_removed")) {
  454. child->connect("tree_exited", this, "_node_removed", varray(child));
  455. }
  456. switch (track_type) {
  457. case Animation::TYPE_VALUE: {
  458. TrackCacheValue *track_value = memnew(TrackCacheValue);
  459. if (resource.is_valid()) {
  460. track_value->object = resource.ptr();
  461. } else {
  462. track_value->object = child;
  463. }
  464. track_value->subpath = leftover_path;
  465. track_value->object_id = track_value->object->get_instance_id();
  466. track = track_value;
  467. } break;
  468. case Animation::TYPE_TRANSFORM: {
  469. Spatial *spatial = Object::cast_to<Spatial>(child);
  470. if (!spatial) {
  471. ERR_PRINTS("AnimationTree: '" + String(E->get()) + "', transform track does not point to spatial: '" + String(path) + "'");
  472. continue;
  473. }
  474. TrackCacheTransform *track_xform = memnew(TrackCacheTransform);
  475. track_xform->spatial = spatial;
  476. track_xform->skeleton = NULL;
  477. track_xform->bone_idx = -1;
  478. if (path.get_subname_count() == 1 && Object::cast_to<Skeleton>(spatial)) {
  479. Skeleton *sk = Object::cast_to<Skeleton>(spatial);
  480. int bone_idx = sk->find_bone(path.get_subname(0));
  481. if (bone_idx != -1 && !sk->is_bone_ignore_animation(bone_idx)) {
  482. track_xform->skeleton = sk;
  483. track_xform->bone_idx = bone_idx;
  484. }
  485. }
  486. track_xform->object = spatial;
  487. track_xform->object_id = track_xform->object->get_instance_id();
  488. track = track_xform;
  489. } break;
  490. case Animation::TYPE_METHOD: {
  491. TrackCacheMethod *track_method = memnew(TrackCacheMethod);
  492. if (resource.is_valid()) {
  493. track_method->object = resource.ptr();
  494. } else {
  495. track_method->object = child;
  496. }
  497. track_method->object_id = track_method->object->get_instance_id();
  498. track = track_method;
  499. } break;
  500. case Animation::TYPE_BEZIER: {
  501. TrackCacheBezier *track_bezier = memnew(TrackCacheBezier);
  502. if (resource.is_valid()) {
  503. track_bezier->object = resource.ptr();
  504. } else {
  505. track_bezier->object = child;
  506. }
  507. track_bezier->subpath = leftover_path;
  508. track_bezier->object_id = track_bezier->object->get_instance_id();
  509. track = track_bezier;
  510. } break;
  511. case Animation::TYPE_AUDIO: {
  512. TrackCacheAudio *track_audio = memnew(TrackCacheAudio);
  513. track_audio->object = child;
  514. track_audio->object_id = track_audio->object->get_instance_id();
  515. track = track_audio;
  516. } break;
  517. case Animation::TYPE_ANIMATION: {
  518. TrackCacheAnimation *track_animation = memnew(TrackCacheAnimation);
  519. track_animation->object = child;
  520. track_animation->object_id = track_animation->object->get_instance_id();
  521. track = track_animation;
  522. } break;
  523. }
  524. track_cache[path] = track;
  525. }
  526. track->setup_pass = setup_pass;
  527. }
  528. }
  529. List<NodePath> to_delete;
  530. const NodePath *K = NULL;
  531. while ((K = track_cache.next(K))) {
  532. TrackCache *tc = track_cache[*K];
  533. if (tc->setup_pass != setup_pass) {
  534. to_delete.push_back(*K);
  535. }
  536. }
  537. while (to_delete.front()) {
  538. NodePath np = to_delete.front()->get();
  539. memdelete(track_cache[np]);
  540. track_cache.erase(np);
  541. to_delete.pop_front();
  542. }
  543. state.track_map.clear();
  544. K = NULL;
  545. int idx = 0;
  546. while ((K = track_cache.next(K))) {
  547. state.track_map[*K] = idx;
  548. idx++;
  549. }
  550. state.track_count = idx;
  551. cache_valid = true;
  552. return true;
  553. }
  554. void AnimationTree::_clear_caches() {
  555. const NodePath *K = NULL;
  556. while ((K = track_cache.next(K))) {
  557. memdelete(track_cache[*K]);
  558. }
  559. playing_caches.clear();
  560. track_cache.clear();
  561. cache_valid = false;
  562. }
  563. void AnimationTree::_process_graph(float p_delta) {
  564. _update_properties(); //if properties need updating, update them
  565. //check all tracks, see if they need modification
  566. root_motion_transform = Transform();
  567. if (!root.is_valid()) {
  568. ERR_PRINT("AnimationTree: root AnimationNode is not set, disabling playback.");
  569. set_active(false);
  570. cache_valid = false;
  571. return;
  572. }
  573. if (!has_node(animation_player)) {
  574. ERR_PRINT("AnimationTree: no valid AnimationPlayer path set, disabling playback");
  575. set_active(false);
  576. cache_valid = false;
  577. return;
  578. }
  579. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(get_node(animation_player));
  580. ObjectID current_animation_player = 0;
  581. if (player) {
  582. current_animation_player = player->get_instance_id();
  583. }
  584. if (last_animation_player != current_animation_player) {
  585. if (last_animation_player) {
  586. Object *old_player = ObjectDB::get_instance(last_animation_player);
  587. if (old_player) {
  588. old_player->disconnect("caches_cleared", this, "_clear_caches");
  589. }
  590. }
  591. if (player) {
  592. player->connect("caches_cleared", this, "_clear_caches");
  593. }
  594. last_animation_player = current_animation_player;
  595. }
  596. if (!player) {
  597. ERR_PRINT("AnimationTree: path points to a node not an AnimationPlayer, disabling playback");
  598. set_active(false);
  599. cache_valid = false;
  600. return;
  601. }
  602. if (!cache_valid) {
  603. if (!_update_caches(player)) {
  604. return;
  605. }
  606. }
  607. { //setup
  608. process_pass++;
  609. state.valid = true;
  610. state.invalid_reasons = "";
  611. state.animation_states.clear(); //will need to be re-created
  612. state.valid = true;
  613. state.player = player;
  614. state.last_pass = process_pass;
  615. state.tree = this;
  616. // root source blends
  617. root->blends.resize(state.track_count);
  618. float *src_blendsw = root->blends.ptrw();
  619. for (int i = 0; i < state.track_count; i++) {
  620. src_blendsw[i] = 1.0; //by default all go to 1 for the root input
  621. }
  622. }
  623. //process
  624. {
  625. if (started) {
  626. //if started, seek
  627. root->_pre_process(SceneStringNames::get_singleton()->parameters_base_path, NULL, &state, 0, true, Vector<StringName>());
  628. started = false;
  629. }
  630. root->_pre_process(SceneStringNames::get_singleton()->parameters_base_path, NULL, &state, p_delta, false, Vector<StringName>());
  631. }
  632. if (!state.valid) {
  633. return; //state is not valid. do nothing.
  634. }
  635. //apply value/transform/bezier blends to track caches and execute method/audio/animation tracks
  636. {
  637. bool can_call = is_inside_tree() && !Engine::get_singleton()->is_editor_hint();
  638. for (List<AnimationNode::AnimationState>::Element *E = state.animation_states.front(); E; E = E->next()) {
  639. const AnimationNode::AnimationState &as = E->get();
  640. Ref<Animation> a = as.animation;
  641. float time = as.time;
  642. float delta = as.delta;
  643. bool seeked = as.seeked;
  644. for (int i = 0; i < a->get_track_count(); i++) {
  645. NodePath path = a->track_get_path(i);
  646. TrackCache *track = track_cache[path];
  647. if (track->type != a->track_get_type(i)) {
  648. continue; //may happen should not
  649. }
  650. track->root_motion = root_motion_track == path;
  651. ERR_CONTINUE(!state.track_map.has(path));
  652. int blend_idx = state.track_map[path];
  653. ERR_CONTINUE(blend_idx < 0 || blend_idx >= state.track_count);
  654. float blend = (*as.track_blends)[blend_idx];
  655. if (blend < CMP_EPSILON)
  656. continue; //nothing to blend
  657. switch (track->type) {
  658. case Animation::TYPE_TRANSFORM: {
  659. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  660. if (t->process_pass != process_pass) {
  661. t->process_pass = process_pass;
  662. t->loc = Vector3();
  663. t->rot = Quat();
  664. t->rot_blend_accum = 0;
  665. t->scale = Vector3();
  666. }
  667. if (track->root_motion) {
  668. float prev_time = time - delta;
  669. if (prev_time < 0) {
  670. if (!a->has_loop()) {
  671. prev_time = 0;
  672. } else {
  673. prev_time = a->get_length() + prev_time;
  674. }
  675. }
  676. Vector3 loc[2];
  677. Quat rot[2];
  678. Vector3 scale[2];
  679. if (prev_time > time) {
  680. Error err = a->transform_track_interpolate(i, prev_time, &loc[0], &rot[0], &scale[0]);
  681. if (err != OK) {
  682. continue;
  683. }
  684. a->transform_track_interpolate(i, a->get_length(), &loc[1], &rot[1], &scale[1]);
  685. t->loc += (loc[1] - loc[0]) * blend;
  686. t->scale += (scale[1] - scale[0]) * blend;
  687. Quat q = Quat().slerp(rot[0].normalized().inverse() * rot[1].normalized(), blend).normalized();
  688. t->rot = (t->rot * q).normalized();
  689. prev_time = 0;
  690. }
  691. Error err = a->transform_track_interpolate(i, prev_time, &loc[0], &rot[0], &scale[0]);
  692. if (err != OK) {
  693. continue;
  694. }
  695. a->transform_track_interpolate(i, time, &loc[1], &rot[1], &scale[1]);
  696. t->loc += (loc[1] - loc[0]) * blend;
  697. t->scale += (scale[1] - scale[0]) * blend;
  698. Quat q = Quat().slerp(rot[0].normalized().inverse() * rot[1].normalized(), blend).normalized();
  699. t->rot = (t->rot * q).normalized();
  700. prev_time = 0;
  701. } else {
  702. Vector3 loc;
  703. Quat rot;
  704. Vector3 scale;
  705. Error err = a->transform_track_interpolate(i, time, &loc, &rot, &scale);
  706. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  707. scale -= Vector3(1.0, 1.0, 1.0); //helps make it work properly with Add nodes
  708. if (err != OK)
  709. continue;
  710. t->loc = t->loc.linear_interpolate(loc, blend);
  711. if (t->rot_blend_accum == 0) {
  712. t->rot = rot;
  713. t->rot_blend_accum = blend;
  714. } else {
  715. float rot_total = t->rot_blend_accum + blend;
  716. t->rot = rot.slerp(t->rot, t->rot_blend_accum / rot_total).normalized();
  717. t->rot_blend_accum = rot_total;
  718. }
  719. t->scale = t->scale.linear_interpolate(scale, blend);
  720. }
  721. } break;
  722. case Animation::TYPE_VALUE: {
  723. TrackCacheValue *t = static_cast<TrackCacheValue *>(track);
  724. Animation::UpdateMode update_mode = a->value_track_get_update_mode(i);
  725. if (update_mode == Animation::UPDATE_CONTINUOUS || update_mode == Animation::UPDATE_CAPTURE) { //delta == 0 means seek
  726. Variant value = a->value_track_interpolate(i, time);
  727. if (value == Variant())
  728. continue;
  729. if (t->process_pass != process_pass) {
  730. Variant::CallError ce;
  731. t->value = Variant::construct(value.get_type(), NULL, 0, ce); //reset
  732. t->process_pass = process_pass;
  733. }
  734. Variant::interpolate(t->value, value, blend, t->value);
  735. } else if (delta != 0) {
  736. List<int> indices;
  737. a->value_track_get_key_indices(i, time, delta, &indices);
  738. for (List<int>::Element *F = indices.front(); F; F = F->next()) {
  739. Variant value = a->track_get_key_value(i, F->get());
  740. t->object->set_indexed(t->subpath, value);
  741. }
  742. }
  743. } break;
  744. case Animation::TYPE_METHOD: {
  745. if (delta == 0) {
  746. continue;
  747. }
  748. TrackCacheMethod *t = static_cast<TrackCacheMethod *>(track);
  749. List<int> indices;
  750. a->method_track_get_key_indices(i, time, delta, &indices);
  751. for (List<int>::Element *E = indices.front(); E; E = E->next()) {
  752. StringName method = a->method_track_get_name(i, E->get());
  753. Vector<Variant> params = a->method_track_get_params(i, E->get());
  754. int s = params.size();
  755. ERR_CONTINUE(s > VARIANT_ARG_MAX);
  756. if (can_call) {
  757. t->object->call_deferred(
  758. method,
  759. s >= 1 ? params[0] : Variant(),
  760. s >= 2 ? params[1] : Variant(),
  761. s >= 3 ? params[2] : Variant(),
  762. s >= 4 ? params[3] : Variant(),
  763. s >= 5 ? params[4] : Variant());
  764. }
  765. }
  766. } break;
  767. case Animation::TYPE_BEZIER: {
  768. TrackCacheBezier *t = static_cast<TrackCacheBezier *>(track);
  769. float bezier = a->bezier_track_interpolate(i, time);
  770. if (t->process_pass != process_pass) {
  771. t->value = 0;
  772. t->process_pass = process_pass;
  773. }
  774. t->value = Math::lerp(t->value, bezier, blend);
  775. } break;
  776. case Animation::TYPE_AUDIO: {
  777. TrackCacheAudio *t = static_cast<TrackCacheAudio *>(track);
  778. if (seeked) {
  779. //find whathever should be playing
  780. int idx = a->track_find_key(i, time);
  781. if (idx < 0)
  782. continue;
  783. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  784. if (!stream.is_valid()) {
  785. t->object->call("stop");
  786. t->playing = false;
  787. playing_caches.erase(t);
  788. } else {
  789. float start_ofs = a->audio_track_get_key_start_offset(i, idx);
  790. start_ofs += time - a->track_get_key_time(i, idx);
  791. float end_ofs = a->audio_track_get_key_end_offset(i, idx);
  792. float len = stream->get_length();
  793. if (start_ofs > len - end_ofs) {
  794. t->object->call("stop");
  795. t->playing = false;
  796. playing_caches.erase(t);
  797. continue;
  798. }
  799. t->object->call("set_stream", stream);
  800. t->object->call("play", start_ofs);
  801. t->playing = true;
  802. playing_caches.insert(t);
  803. if (len && end_ofs > 0) { //force a end at a time
  804. t->len = len - start_ofs - end_ofs;
  805. } else {
  806. t->len = 0;
  807. }
  808. t->start = time;
  809. }
  810. } else {
  811. //find stuff to play
  812. List<int> to_play;
  813. a->track_get_key_indices_in_range(i, time, delta, &to_play);
  814. if (to_play.size()) {
  815. int idx = to_play.back()->get();
  816. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  817. if (!stream.is_valid()) {
  818. t->object->call("stop");
  819. t->playing = false;
  820. playing_caches.erase(t);
  821. } else {
  822. float start_ofs = a->audio_track_get_key_start_offset(i, idx);
  823. float end_ofs = a->audio_track_get_key_end_offset(i, idx);
  824. float len = stream->get_length();
  825. t->object->call("set_stream", stream);
  826. t->object->call("play", start_ofs);
  827. t->playing = true;
  828. playing_caches.insert(t);
  829. if (len && end_ofs > 0) { //force a end at a time
  830. t->len = len - start_ofs - end_ofs;
  831. } else {
  832. t->len = 0;
  833. }
  834. t->start = time;
  835. }
  836. } else if (t->playing) {
  837. bool loop = a->has_loop();
  838. bool stop = false;
  839. if (!loop && time < t->start) {
  840. stop = true;
  841. } else if (t->len > 0) {
  842. float len = t->start > time ? (a->get_length() - t->start) + time : time - t->start;
  843. if (len > t->len) {
  844. stop = true;
  845. }
  846. }
  847. if (stop) {
  848. //time to stop
  849. t->object->call("stop");
  850. t->playing = false;
  851. playing_caches.erase(t);
  852. }
  853. }
  854. }
  855. float db = Math::linear2db(MAX(blend, 0.00001));
  856. if (t->object->has_method("set_unit_db")) {
  857. t->object->call("set_unit_db", db);
  858. } else {
  859. t->object->call("set_volume_db", db);
  860. }
  861. } break;
  862. case Animation::TYPE_ANIMATION: {
  863. TrackCacheAnimation *t = static_cast<TrackCacheAnimation *>(track);
  864. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(t->object);
  865. if (!player)
  866. continue;
  867. if (delta == 0 || seeked) {
  868. //seek
  869. int idx = a->track_find_key(i, time);
  870. if (idx < 0)
  871. continue;
  872. float pos = a->track_get_key_time(i, idx);
  873. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  874. if (String(anim_name) == "[stop]" || !player->has_animation(anim_name))
  875. continue;
  876. Ref<Animation> anim = player->get_animation(anim_name);
  877. float at_anim_pos;
  878. if (anim->has_loop()) {
  879. at_anim_pos = Math::fposmod(time - pos, anim->get_length()); //seek to loop
  880. } else {
  881. at_anim_pos = MAX(anim->get_length(), time - pos); //seek to end
  882. }
  883. if (player->is_playing() || seeked) {
  884. player->play(anim_name);
  885. player->seek(at_anim_pos);
  886. t->playing = true;
  887. playing_caches.insert(t);
  888. } else {
  889. player->set_assigned_animation(anim_name);
  890. player->seek(at_anim_pos, true);
  891. }
  892. } else {
  893. //find stuff to play
  894. List<int> to_play;
  895. a->track_get_key_indices_in_range(i, time, delta, &to_play);
  896. if (to_play.size()) {
  897. int idx = to_play.back()->get();
  898. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  899. if (String(anim_name) == "[stop]" || !player->has_animation(anim_name)) {
  900. if (playing_caches.has(t)) {
  901. playing_caches.erase(t);
  902. player->stop();
  903. t->playing = false;
  904. }
  905. } else {
  906. player->play(anim_name);
  907. t->playing = true;
  908. playing_caches.insert(t);
  909. }
  910. }
  911. }
  912. } break;
  913. }
  914. }
  915. }
  916. }
  917. {
  918. // finally, set the tracks
  919. const NodePath *K = NULL;
  920. while ((K = track_cache.next(K))) {
  921. TrackCache *track = track_cache[*K];
  922. if (track->process_pass != process_pass)
  923. continue; //not processed, ignore
  924. switch (track->type) {
  925. case Animation::TYPE_TRANSFORM: {
  926. TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track);
  927. Transform xform;
  928. xform.origin = t->loc;
  929. t->scale += Vector3(1.0, 1.0, 1.0); //helps make it work properly with Add nodes and root motion
  930. xform.basis.set_quat_scale(t->rot, t->scale);
  931. if (t->root_motion) {
  932. root_motion_transform = xform;
  933. if (t->skeleton && t->bone_idx >= 0) {
  934. root_motion_transform = (t->skeleton->get_bone_rest(t->bone_idx) * root_motion_transform) * t->skeleton->get_bone_rest(t->bone_idx).affine_inverse();
  935. }
  936. } else if (t->skeleton && t->bone_idx >= 0) {
  937. t->skeleton->set_bone_pose(t->bone_idx, xform);
  938. } else {
  939. t->spatial->set_transform(xform);
  940. }
  941. } break;
  942. case Animation::TYPE_VALUE: {
  943. TrackCacheValue *t = static_cast<TrackCacheValue *>(track);
  944. t->object->set_indexed(t->subpath, t->value);
  945. } break;
  946. case Animation::TYPE_BEZIER: {
  947. TrackCacheBezier *t = static_cast<TrackCacheBezier *>(track);
  948. t->object->set_indexed(t->subpath, t->value);
  949. } break;
  950. default: {} //the rest don't matter
  951. }
  952. }
  953. }
  954. }
  955. void AnimationTree::advance(float p_time) {
  956. _process_graph(p_time);
  957. }
  958. void AnimationTree::_notification(int p_what) {
  959. if (active && p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS && process_mode == ANIMATION_PROCESS_PHYSICS) {
  960. _process_graph(get_physics_process_delta_time());
  961. }
  962. if (active && p_what == NOTIFICATION_INTERNAL_PROCESS && process_mode == ANIMATION_PROCESS_IDLE) {
  963. _process_graph(get_process_delta_time());
  964. }
  965. if (p_what == NOTIFICATION_EXIT_TREE) {
  966. _clear_caches();
  967. if (last_animation_player) {
  968. Object *old_player = ObjectDB::get_instance(last_animation_player);
  969. if (old_player) {
  970. old_player->disconnect("caches_cleared", this, "_clear_caches");
  971. }
  972. }
  973. }
  974. }
  975. void AnimationTree::set_animation_player(const NodePath &p_player) {
  976. animation_player = p_player;
  977. update_configuration_warning();
  978. }
  979. NodePath AnimationTree::get_animation_player() const {
  980. return animation_player;
  981. }
  982. bool AnimationTree::is_state_invalid() const {
  983. return !state.valid;
  984. }
  985. String AnimationTree::get_invalid_state_reason() const {
  986. return state.invalid_reasons;
  987. }
  988. uint64_t AnimationTree::get_last_process_pass() const {
  989. return process_pass;
  990. }
  991. String AnimationTree::get_configuration_warning() const {
  992. String warning = Node::get_configuration_warning();
  993. if (!root.is_valid()) {
  994. if (warning != String()) {
  995. warning += "\n";
  996. }
  997. warning += TTR("A root AnimationNode for the graph is not set.");
  998. }
  999. if (!has_node(animation_player)) {
  1000. if (warning != String()) {
  1001. warning += "\n";
  1002. }
  1003. warning += TTR("Path to an AnimationPlayer node containing animations is not set.");
  1004. return warning;
  1005. }
  1006. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(get_node(animation_player));
  1007. if (!player) {
  1008. if (warning != String()) {
  1009. warning += "\n";
  1010. }
  1011. warning += TTR("Path set for AnimationPlayer does not lead to an AnimationPlayer node.");
  1012. return warning;
  1013. }
  1014. if (!player->has_node(player->get_root())) {
  1015. if (warning != String()) {
  1016. warning += "\n";
  1017. }
  1018. warning += TTR("AnimationPlayer root is not a valid node.");
  1019. return warning;
  1020. }
  1021. return warning;
  1022. }
  1023. void AnimationTree::set_root_motion_track(const NodePath &p_track) {
  1024. root_motion_track = p_track;
  1025. }
  1026. NodePath AnimationTree::get_root_motion_track() const {
  1027. return root_motion_track;
  1028. }
  1029. Transform AnimationTree::get_root_motion_transform() const {
  1030. return root_motion_transform;
  1031. }
  1032. void AnimationTree::_tree_changed() {
  1033. if (properties_dirty) {
  1034. return;
  1035. }
  1036. call_deferred("_update_properties");
  1037. properties_dirty = true;
  1038. }
  1039. void AnimationTree::_update_properties_for_node(const String &p_base_path, Ref<AnimationNode> node) {
  1040. if (!property_parent_map.has(p_base_path)) {
  1041. property_parent_map[p_base_path] = HashMap<StringName, StringName>();
  1042. }
  1043. if (node->get_input_count() && !input_activity_map.has(p_base_path)) {
  1044. Vector<Activity> activity;
  1045. for (int i = 0; i < node->get_input_count(); i++) {
  1046. Activity a;
  1047. a.last_pass = 0;
  1048. activity.push_back(a);
  1049. }
  1050. input_activity_map[p_base_path] = activity;
  1051. input_activity_map_get[String(p_base_path).substr(0, String(p_base_path).length() - 1)] = &input_activity_map[p_base_path];
  1052. }
  1053. List<PropertyInfo> plist;
  1054. node->get_parameter_list(&plist);
  1055. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1056. PropertyInfo pinfo = E->get();
  1057. StringName key = pinfo.name;
  1058. if (!property_map.has(p_base_path + key)) {
  1059. property_map[p_base_path + key] = node->get_parameter_default_value(key);
  1060. }
  1061. property_parent_map[p_base_path][key] = p_base_path + key;
  1062. pinfo.name = p_base_path + key;
  1063. properties.push_back(pinfo);
  1064. }
  1065. List<AnimationNode::ChildNode> children;
  1066. node->get_child_nodes(&children);
  1067. for (List<AnimationNode::ChildNode>::Element *E = children.front(); E; E = E->next()) {
  1068. _update_properties_for_node(p_base_path + E->get().name + "/", E->get().node);
  1069. }
  1070. }
  1071. void AnimationTree::_update_properties() {
  1072. if (!properties_dirty) {
  1073. return;
  1074. }
  1075. properties.clear();
  1076. property_parent_map.clear();
  1077. input_activity_map.clear();
  1078. input_activity_map_get.clear();
  1079. if (root.is_valid()) {
  1080. _update_properties_for_node(SceneStringNames::get_singleton()->parameters_base_path, root);
  1081. }
  1082. properties_dirty = false;
  1083. _change_notify();
  1084. }
  1085. bool AnimationTree::_set(const StringName &p_name, const Variant &p_value) {
  1086. if (properties_dirty) {
  1087. _update_properties();
  1088. }
  1089. if (property_map.has(p_name)) {
  1090. property_map[p_name] = p_value;
  1091. #ifdef TOOLS_ENABLED
  1092. _change_notify(p_name.operator String().utf8().get_data());
  1093. #endif
  1094. return true;
  1095. }
  1096. return false;
  1097. }
  1098. bool AnimationTree::_get(const StringName &p_name, Variant &r_ret) const {
  1099. if (properties_dirty) {
  1100. const_cast<AnimationTree *>(this)->_update_properties();
  1101. }
  1102. if (property_map.has(p_name)) {
  1103. r_ret = property_map[p_name];
  1104. return true;
  1105. }
  1106. return false;
  1107. }
  1108. void AnimationTree::_get_property_list(List<PropertyInfo> *p_list) const {
  1109. if (properties_dirty) {
  1110. const_cast<AnimationTree *>(this)->_update_properties();
  1111. }
  1112. for (const List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  1113. p_list->push_back(E->get());
  1114. }
  1115. }
  1116. void AnimationTree::rename_parameter(const String &p_base, const String &p_new_base) {
  1117. //rename values first
  1118. for (const List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  1119. if (E->get().name.begins_with(p_base)) {
  1120. String new_name = E->get().name.replace_first(p_base, p_new_base);
  1121. property_map[new_name] = property_map[E->get().name];
  1122. }
  1123. }
  1124. //update tree second
  1125. properties_dirty = true;
  1126. _update_properties();
  1127. }
  1128. float AnimationTree::get_connection_activity(const StringName &p_path, int p_connection) const {
  1129. if (!input_activity_map_get.has(p_path)) {
  1130. return 0;
  1131. }
  1132. const Vector<Activity> *activity = input_activity_map_get[p_path];
  1133. if (!activity || p_connection < 0 || p_connection >= activity->size()) {
  1134. return 0;
  1135. }
  1136. if ((*activity)[p_connection].last_pass != process_pass) {
  1137. return 0;
  1138. }
  1139. return (*activity)[p_connection].activity;
  1140. }
  1141. void AnimationTree::_bind_methods() {
  1142. ClassDB::bind_method(D_METHOD("set_active", "active"), &AnimationTree::set_active);
  1143. ClassDB::bind_method(D_METHOD("is_active"), &AnimationTree::is_active);
  1144. ClassDB::bind_method(D_METHOD("set_tree_root", "root"), &AnimationTree::set_tree_root);
  1145. ClassDB::bind_method(D_METHOD("get_tree_root"), &AnimationTree::get_tree_root);
  1146. ClassDB::bind_method(D_METHOD("set_process_mode", "mode"), &AnimationTree::set_process_mode);
  1147. ClassDB::bind_method(D_METHOD("get_process_mode"), &AnimationTree::get_process_mode);
  1148. ClassDB::bind_method(D_METHOD("set_animation_player", "root"), &AnimationTree::set_animation_player);
  1149. ClassDB::bind_method(D_METHOD("get_animation_player"), &AnimationTree::get_animation_player);
  1150. ClassDB::bind_method(D_METHOD("set_root_motion_track", "path"), &AnimationTree::set_root_motion_track);
  1151. ClassDB::bind_method(D_METHOD("get_root_motion_track"), &AnimationTree::get_root_motion_track);
  1152. ClassDB::bind_method(D_METHOD("get_root_motion_transform"), &AnimationTree::get_root_motion_transform);
  1153. ClassDB::bind_method(D_METHOD("_tree_changed"), &AnimationTree::_tree_changed);
  1154. ClassDB::bind_method(D_METHOD("_update_properties"), &AnimationTree::_update_properties);
  1155. ClassDB::bind_method(D_METHOD("rename_parameter", "old_name", "new_name"), &AnimationTree::rename_parameter);
  1156. ClassDB::bind_method(D_METHOD("advance", "delta"), &AnimationTree::advance);
  1157. ClassDB::bind_method(D_METHOD("_node_removed"), &AnimationTree::_node_removed);
  1158. ClassDB::bind_method(D_METHOD("_clear_caches"), &AnimationTree::_clear_caches);
  1159. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tree_root", PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode"), "set_tree_root", "get_tree_root");
  1160. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "anim_player", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "AnimationPlayer"), "set_animation_player", "get_animation_player");
  1161. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "active"), "set_active", "is_active");
  1162. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Physics,Idle,Manual"), "set_process_mode", "get_process_mode");
  1163. ADD_GROUP("Root Motion", "root_motion_");
  1164. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "root_motion_track"), "set_root_motion_track", "get_root_motion_track");
  1165. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_PHYSICS);
  1166. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_IDLE);
  1167. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_MANUAL);
  1168. }
  1169. AnimationTree::AnimationTree() {
  1170. process_mode = ANIMATION_PROCESS_IDLE;
  1171. active = false;
  1172. cache_valid = false;
  1173. setup_pass = 1;
  1174. started = true;
  1175. properties_dirty = true;
  1176. last_animation_player = 0;
  1177. }
  1178. AnimationTree::~AnimationTree() {
  1179. }