2
0

animation_tree.cpp 43 KB

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