animation_tree.cpp 38 KB

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