animation_player.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  1. /*************************************************************************/
  2. /* animation_player.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_player.h"
  31. #include "core/config/engine.h"
  32. #include "core/object/message_queue.h"
  33. #include "scene/scene_string_names.h"
  34. #include "servers/audio/audio_stream.h"
  35. #ifdef TOOLS_ENABLED
  36. #include "editor/editor_node.h"
  37. #include "scene/2d/skeleton_2d.h"
  38. void AnimatedValuesBackup::update_skeletons() {
  39. for (int i = 0; i < entries.size(); i++) {
  40. if (entries[i].bone_idx != -1) {
  41. // 3D bone
  42. Object::cast_to<Skeleton3D>(entries[i].object)->notification(Skeleton3D::NOTIFICATION_UPDATE_SKELETON);
  43. } else {
  44. Bone2D *bone = Object::cast_to<Bone2D>(entries[i].object);
  45. if (bone && bone->skeleton) {
  46. // 2D bone
  47. bone->skeleton->_update_transform();
  48. }
  49. }
  50. }
  51. }
  52. void AnimatedValuesBackup::restore() const {
  53. for (int i = 0; i < entries.size(); i++) {
  54. const AnimatedValuesBackup::Entry *entry = &entries[i];
  55. if (entry->bone_idx == -1) {
  56. entry->object->set_indexed(entry->subpath, entry->value);
  57. } else {
  58. Array arr = entry->value;
  59. if (arr.size() == 3) {
  60. Object::cast_to<Skeleton3D>(entry->object)->set_bone_pose_position(entry->bone_idx, arr[0]);
  61. Object::cast_to<Skeleton3D>(entry->object)->set_bone_pose_rotation(entry->bone_idx, arr[1]);
  62. Object::cast_to<Skeleton3D>(entry->object)->set_bone_pose_scale(entry->bone_idx, arr[0]);
  63. }
  64. }
  65. }
  66. }
  67. void AnimatedValuesBackup::_bind_methods() {
  68. ClassDB::bind_method(D_METHOD("restore"), &AnimatedValuesBackup::restore);
  69. }
  70. #endif
  71. bool AnimationPlayer::_set(const StringName &p_name, const Variant &p_value) {
  72. String name = p_name;
  73. if (name.begins_with("playback/play")) { // bw compatibility
  74. set_current_animation(p_value);
  75. } else if (name.begins_with("anims/")) {
  76. String which = name.get_slicec('/', 1);
  77. add_animation(which, p_value);
  78. } else if (name.begins_with("next/")) {
  79. String which = name.get_slicec('/', 1);
  80. animation_set_next(which, p_value);
  81. } else if (p_name == SceneStringNames::get_singleton()->blend_times) {
  82. Array array = p_value;
  83. int len = array.size();
  84. ERR_FAIL_COND_V(len % 3, false);
  85. for (int i = 0; i < len / 3; i++) {
  86. StringName from = array[i * 3 + 0];
  87. StringName to = array[i * 3 + 1];
  88. float time = array[i * 3 + 2];
  89. set_blend_time(from, to, time);
  90. }
  91. } else {
  92. return false;
  93. }
  94. return true;
  95. }
  96. bool AnimationPlayer::_get(const StringName &p_name, Variant &r_ret) const {
  97. String name = p_name;
  98. if (name == "playback/play") { // bw compatibility
  99. r_ret = get_current_animation();
  100. } else if (name.begins_with("anims/")) {
  101. String which = name.get_slicec('/', 1);
  102. r_ret = get_animation(which);
  103. } else if (name.begins_with("next/")) {
  104. String which = name.get_slicec('/', 1);
  105. r_ret = animation_get_next(which);
  106. } else if (name == "blend_times") {
  107. Vector<BlendKey> keys;
  108. for (const KeyValue<BlendKey, float> &E : blend_times) {
  109. keys.ordered_insert(E.key);
  110. }
  111. Array array;
  112. for (int i = 0; i < keys.size(); i++) {
  113. array.push_back(keys[i].from);
  114. array.push_back(keys[i].to);
  115. array.push_back(blend_times[keys[i]]);
  116. }
  117. r_ret = array;
  118. } else {
  119. return false;
  120. }
  121. return true;
  122. }
  123. void AnimationPlayer::_validate_property(PropertyInfo &property) const {
  124. if (property.name == "current_animation") {
  125. List<String> names;
  126. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  127. names.push_back(E.key);
  128. }
  129. names.sort();
  130. names.push_front("[stop]");
  131. String hint;
  132. for (List<String>::Element *E = names.front(); E; E = E->next()) {
  133. if (E != names.front()) {
  134. hint += ",";
  135. }
  136. hint += E->get();
  137. }
  138. property.hint_string = hint;
  139. }
  140. Node::_validate_property(property);
  141. }
  142. void AnimationPlayer::_get_property_list(List<PropertyInfo> *p_list) const {
  143. List<PropertyInfo> anim_names;
  144. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  145. anim_names.push_back(PropertyInfo(Variant::OBJECT, "anims/" + String(E.key), PROPERTY_HINT_RESOURCE_TYPE, "Animation", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE));
  146. if (E.value.next != StringName()) {
  147. anim_names.push_back(PropertyInfo(Variant::STRING, "next/" + String(E.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  148. }
  149. }
  150. anim_names.sort();
  151. for (const PropertyInfo &E : anim_names) {
  152. p_list->push_back(E);
  153. }
  154. p_list->push_back(PropertyInfo(Variant::ARRAY, "blend_times", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  155. }
  156. void AnimationPlayer::advance(float p_time) {
  157. _animation_process(p_time);
  158. }
  159. void AnimationPlayer::_notification(int p_what) {
  160. switch (p_what) {
  161. case NOTIFICATION_ENTER_TREE: {
  162. if (!processing) {
  163. //make sure that a previous process state was not saved
  164. //only process if "processing" is set
  165. set_physics_process_internal(false);
  166. set_process_internal(false);
  167. }
  168. //_set_process(false);
  169. clear_caches();
  170. } break;
  171. case NOTIFICATION_READY: {
  172. if (!Engine::get_singleton()->is_editor_hint() && animation_set.has(autoplay)) {
  173. play(autoplay);
  174. _animation_process(0);
  175. }
  176. } break;
  177. case NOTIFICATION_INTERNAL_PROCESS: {
  178. if (process_callback == ANIMATION_PROCESS_PHYSICS) {
  179. break;
  180. }
  181. if (processing) {
  182. _animation_process(get_process_delta_time());
  183. }
  184. } break;
  185. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  186. if (process_callback == ANIMATION_PROCESS_IDLE) {
  187. break;
  188. }
  189. if (processing) {
  190. _animation_process(get_physics_process_delta_time());
  191. }
  192. } break;
  193. case NOTIFICATION_EXIT_TREE: {
  194. clear_caches();
  195. } break;
  196. }
  197. }
  198. void AnimationPlayer::_ensure_node_caches(AnimationData *p_anim, Node *p_root_override) {
  199. // Already cached?
  200. if (p_anim->node_cache.size() == p_anim->animation->get_track_count()) {
  201. return;
  202. }
  203. Node *parent = p_root_override ? p_root_override : get_node(root);
  204. ERR_FAIL_COND(!parent);
  205. Animation *a = p_anim->animation.operator->();
  206. p_anim->node_cache.resize(a->get_track_count());
  207. setup_pass++;
  208. for (int i = 0; i < a->get_track_count(); i++) {
  209. p_anim->node_cache.write[i] = nullptr;
  210. RES resource;
  211. Vector<StringName> leftover_path;
  212. Node *child = parent->get_node_and_resource(a->track_get_path(i), resource, leftover_path);
  213. ERR_CONTINUE_MSG(!child, "On Animation: '" + p_anim->name + "', couldn't resolve track: '" + String(a->track_get_path(i)) + "'."); // couldn't find the child node
  214. ObjectID id = resource.is_valid() ? resource->get_instance_id() : child->get_instance_id();
  215. int bone_idx = -1;
  216. int blend_shape_idx = -1;
  217. #ifndef _3D_DISABLED
  218. if (a->track_get_path(i).get_subname_count() == 1 && Object::cast_to<Skeleton3D>(child)) {
  219. Skeleton3D *sk = Object::cast_to<Skeleton3D>(child);
  220. bone_idx = sk->find_bone(a->track_get_path(i).get_subname(0));
  221. if (bone_idx == -1) {
  222. continue;
  223. }
  224. }
  225. if (a->track_get_type(i) == Animation::TYPE_BLEND_SHAPE) {
  226. MeshInstance3D *mi_3d = Object::cast_to<MeshInstance3D>(child);
  227. if (!mi_3d) {
  228. continue;
  229. }
  230. if (a->track_get_path(i).get_subname_count() != 1) {
  231. continue;
  232. }
  233. blend_shape_idx = mi_3d->find_blend_shape_by_name(a->track_get_path(i).get_subname(0));
  234. if (blend_shape_idx == -1) {
  235. continue;
  236. }
  237. }
  238. #endif // _3D_DISABLED
  239. {
  240. if (!child->is_connected("tree_exiting", callable_mp(this, &AnimationPlayer::_node_removed))) {
  241. child->connect("tree_exiting", callable_mp(this, &AnimationPlayer::_node_removed), make_binds(child), CONNECT_ONESHOT);
  242. }
  243. }
  244. TrackNodeCacheKey key;
  245. key.id = id;
  246. key.bone_idx = bone_idx;
  247. key.blend_shape_idx = blend_shape_idx;
  248. if (!node_cache_map.has(key)) {
  249. node_cache_map[key] = TrackNodeCache();
  250. }
  251. TrackNodeCache *node_cache = &node_cache_map[key];
  252. p_anim->node_cache.write[i] = node_cache;
  253. node_cache->path = a->track_get_path(i);
  254. node_cache->node = child;
  255. node_cache->resource = resource;
  256. node_cache->node_2d = Object::cast_to<Node2D>(child);
  257. #ifndef _3D_DISABLED
  258. if (a->track_get_type(i) == Animation::TYPE_POSITION_3D || a->track_get_type(i) == Animation::TYPE_ROTATION_3D || a->track_get_type(i) == Animation::TYPE_SCALE_3D) {
  259. // special cases and caches for transform tracks
  260. if (node_cache->last_setup_pass != setup_pass) {
  261. node_cache->loc_used = false;
  262. node_cache->rot_used = false;
  263. node_cache->scale_used = false;
  264. }
  265. // cache node_3d
  266. node_cache->node_3d = Object::cast_to<Node3D>(child);
  267. // cache skeleton
  268. node_cache->skeleton = Object::cast_to<Skeleton3D>(child);
  269. if (node_cache->skeleton) {
  270. if (a->track_get_path(i).get_subname_count() == 1) {
  271. StringName bone_name = a->track_get_path(i).get_subname(0);
  272. node_cache->bone_idx = node_cache->skeleton->find_bone(bone_name);
  273. if (node_cache->bone_idx < 0) {
  274. // broken track (nonexistent bone)
  275. node_cache->skeleton = nullptr;
  276. node_cache->node_3d = nullptr;
  277. ERR_CONTINUE(node_cache->bone_idx < 0);
  278. }
  279. } else {
  280. // no property, just use spatialnode
  281. node_cache->skeleton = nullptr;
  282. }
  283. }
  284. switch (a->track_get_type(i)) {
  285. case Animation::TYPE_POSITION_3D: {
  286. node_cache->loc_used = true;
  287. } break;
  288. case Animation::TYPE_ROTATION_3D: {
  289. node_cache->rot_used = true;
  290. } break;
  291. case Animation::TYPE_SCALE_3D: {
  292. node_cache->scale_used = true;
  293. } break;
  294. default: {
  295. }
  296. }
  297. }
  298. if (a->track_get_type(i) == Animation::TYPE_BLEND_SHAPE) {
  299. // special cases and caches for transform tracks
  300. node_cache->node_blend_shape = Object::cast_to<MeshInstance3D>(child);
  301. node_cache->blend_shape_idx = blend_shape_idx;
  302. }
  303. #endif // _3D_DISABLED
  304. if (a->track_get_type(i) == Animation::TYPE_VALUE) {
  305. if (!node_cache->property_anim.has(a->track_get_path(i).get_concatenated_subnames())) {
  306. TrackNodeCache::PropertyAnim pa;
  307. pa.subpath = leftover_path;
  308. pa.object = resource.is_valid() ? (Object *)resource.ptr() : (Object *)child;
  309. pa.special = SP_NONE;
  310. pa.owner = p_anim->node_cache[i];
  311. if (false && node_cache->node_2d) {
  312. if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_pos) {
  313. pa.special = SP_NODE2D_POS;
  314. } else if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_rot) {
  315. pa.special = SP_NODE2D_ROT;
  316. } else if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_scale) {
  317. pa.special = SP_NODE2D_SCALE;
  318. }
  319. }
  320. node_cache->property_anim[a->track_get_path(i).get_concatenated_subnames()] = pa;
  321. }
  322. }
  323. if (a->track_get_type(i) == Animation::TYPE_BEZIER && leftover_path.size()) {
  324. if (!node_cache->bezier_anim.has(a->track_get_path(i).get_concatenated_subnames())) {
  325. TrackNodeCache::BezierAnim ba;
  326. ba.bezier_property = leftover_path;
  327. ba.object = resource.is_valid() ? (Object *)resource.ptr() : (Object *)child;
  328. ba.owner = p_anim->node_cache[i];
  329. node_cache->bezier_anim[a->track_get_path(i).get_concatenated_subnames()] = ba;
  330. }
  331. }
  332. node_cache->last_setup_pass = setup_pass;
  333. }
  334. }
  335. void AnimationPlayer::_animation_process_animation(AnimationData *p_anim, double p_time, double p_delta, float p_interp, bool p_is_current, bool p_seeked, bool p_started) {
  336. _ensure_node_caches(p_anim);
  337. ERR_FAIL_COND(p_anim->node_cache.size() != p_anim->animation->get_track_count());
  338. Animation *a = p_anim->animation.operator->();
  339. bool can_call = is_inside_tree() && !Engine::get_singleton()->is_editor_hint();
  340. for (int i = 0; i < a->get_track_count(); i++) {
  341. // If an animation changes this animation (or it animates itself)
  342. // we need to recreate our animation cache
  343. if (p_anim->node_cache.size() != a->get_track_count()) {
  344. _ensure_node_caches(p_anim);
  345. }
  346. TrackNodeCache *nc = p_anim->node_cache[i];
  347. if (!nc) {
  348. continue; // no node cache for this track, skip it
  349. }
  350. if (!a->track_is_enabled(i)) {
  351. continue; // do nothing if the track is disabled
  352. }
  353. if (a->track_get_key_count(i) == 0) {
  354. continue; // do nothing if track is empty
  355. }
  356. switch (a->track_get_type(i)) {
  357. case Animation::TYPE_POSITION_3D: {
  358. #ifndef _3D_DISABLED
  359. if (!nc->node_3d) {
  360. continue;
  361. }
  362. Vector3 loc;
  363. Error err = a->position_track_interpolate(i, p_time, &loc);
  364. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  365. if (err != OK) {
  366. continue;
  367. }
  368. if (nc->accum_pass != accum_pass) {
  369. ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
  370. cache_update[cache_update_size++] = nc;
  371. nc->accum_pass = accum_pass;
  372. nc->loc_accum = loc;
  373. nc->rot_accum = Quaternion();
  374. nc->scale_accum = Vector3();
  375. } else {
  376. nc->loc_accum = nc->loc_accum.lerp(loc, p_interp);
  377. }
  378. #endif // _3D_DISABLED
  379. } break;
  380. case Animation::TYPE_ROTATION_3D: {
  381. #ifndef _3D_DISABLED
  382. if (!nc->node_3d) {
  383. continue;
  384. }
  385. Quaternion rot;
  386. Error err = a->rotation_track_interpolate(i, p_time, &rot);
  387. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  388. if (err != OK) {
  389. continue;
  390. }
  391. if (nc->accum_pass != accum_pass) {
  392. ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
  393. cache_update[cache_update_size++] = nc;
  394. nc->accum_pass = accum_pass;
  395. nc->loc_accum = Vector3();
  396. nc->rot_accum = rot;
  397. nc->scale_accum = Vector3();
  398. } else {
  399. nc->rot_accum = nc->rot_accum.slerp(rot, p_interp);
  400. }
  401. #endif // _3D_DISABLED
  402. } break;
  403. case Animation::TYPE_SCALE_3D: {
  404. #ifndef _3D_DISABLED
  405. if (!nc->node_3d) {
  406. continue;
  407. }
  408. Vector3 scale;
  409. Error err = a->scale_track_interpolate(i, p_time, &scale);
  410. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  411. if (err != OK) {
  412. continue;
  413. }
  414. if (nc->accum_pass != accum_pass) {
  415. ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
  416. cache_update[cache_update_size++] = nc;
  417. nc->accum_pass = accum_pass;
  418. nc->loc_accum = Vector3();
  419. nc->rot_accum = Quaternion();
  420. nc->scale_accum = scale;
  421. } else {
  422. nc->scale_accum = nc->scale_accum.lerp(scale, p_interp);
  423. }
  424. #endif // _3D_DISABLED
  425. } break;
  426. case Animation::TYPE_BLEND_SHAPE: {
  427. #ifndef _3D_DISABLED
  428. if (!nc->node_blend_shape) {
  429. continue;
  430. }
  431. float blend;
  432. Error err = a->blend_shape_track_interpolate(i, p_time, &blend);
  433. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  434. if (err != OK) {
  435. continue;
  436. }
  437. if (nc->accum_pass != accum_pass) {
  438. ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
  439. nc->accum_pass = accum_pass;
  440. cache_update[cache_update_size++] = nc;
  441. nc->blend_shape_accum = blend;
  442. } else {
  443. nc->blend_shape_accum = Math::lerp(nc->blend_shape_accum, blend, p_interp);
  444. }
  445. #endif // _3D_DISABLED
  446. } break;
  447. case Animation::TYPE_VALUE: {
  448. if (!nc->node) {
  449. continue;
  450. }
  451. //StringName property=a->track_get_path(i).get_property();
  452. Map<StringName, TrackNodeCache::PropertyAnim>::Element *E = nc->property_anim.find(a->track_get_path(i).get_concatenated_subnames());
  453. ERR_CONTINUE(!E); //should it continue, or create a new one?
  454. TrackNodeCache::PropertyAnim *pa = &E->get();
  455. Animation::UpdateMode update_mode = a->value_track_get_update_mode(i);
  456. if (update_mode == Animation::UPDATE_CAPTURE) {
  457. if (p_started || pa->capture == Variant()) {
  458. pa->capture = pa->object->get_indexed(pa->subpath);
  459. }
  460. int key_count = a->track_get_key_count(i);
  461. if (key_count == 0) {
  462. continue; //eeh not worth it
  463. }
  464. float first_key_time = a->track_get_key_time(i, 0);
  465. float transition = 1.0;
  466. int first_key = 0;
  467. if (first_key_time == 0.0) {
  468. //ignore, use for transition
  469. if (key_count == 1) {
  470. continue; //with one key we can't do anything
  471. }
  472. transition = a->track_get_key_transition(i, 0);
  473. first_key_time = a->track_get_key_time(i, 1);
  474. first_key = 1;
  475. }
  476. if (p_time < first_key_time) {
  477. float c = Math::ease(p_time / first_key_time, transition);
  478. Variant first_value = a->track_get_key_value(i, first_key);
  479. Variant interp_value;
  480. Variant::interpolate(pa->capture, first_value, c, interp_value);
  481. if (pa->accum_pass != accum_pass) {
  482. ERR_CONTINUE(cache_update_prop_size >= NODE_CACHE_UPDATE_MAX);
  483. cache_update_prop[cache_update_prop_size++] = pa;
  484. pa->value_accum = interp_value;
  485. pa->accum_pass = accum_pass;
  486. } else {
  487. Variant::interpolate(pa->value_accum, interp_value, p_interp, pa->value_accum);
  488. }
  489. continue; //handled
  490. }
  491. }
  492. if (update_mode == Animation::UPDATE_CONTINUOUS || update_mode == Animation::UPDATE_CAPTURE || (p_delta == 0 && update_mode == Animation::UPDATE_DISCRETE)) { //delta == 0 means seek
  493. Variant value = a->value_track_interpolate(i, p_time);
  494. if (value == Variant()) {
  495. continue;
  496. }
  497. //thanks to trigger mode, this should be solved now..
  498. /*
  499. if (p_delta==0 && value.get_type()==Variant::STRING)
  500. continue; // doing this with strings is messy, should find another way
  501. */
  502. if (pa->accum_pass != accum_pass) {
  503. ERR_CONTINUE(cache_update_prop_size >= NODE_CACHE_UPDATE_MAX);
  504. cache_update_prop[cache_update_prop_size++] = pa;
  505. pa->value_accum = value;
  506. pa->accum_pass = accum_pass;
  507. } else {
  508. Variant::interpolate(pa->value_accum, value, p_interp, pa->value_accum);
  509. }
  510. } else if (p_is_current && p_delta != 0) {
  511. List<int> indices;
  512. a->value_track_get_key_indices(i, p_time, p_delta, &indices);
  513. for (int &F : indices) {
  514. Variant value = a->track_get_key_value(i, F);
  515. switch (pa->special) {
  516. case SP_NONE: {
  517. bool valid;
  518. pa->object->set_indexed(pa->subpath, value, &valid); //you are not speshul
  519. #ifdef DEBUG_ENABLED
  520. if (!valid) {
  521. ERR_PRINT("Failed setting track value '" + String(pa->owner->path) + "'. Check if property exists or the type of key is valid. Animation '" + a->get_name() + "' at node '" + get_path() + "'.");
  522. }
  523. #endif
  524. } break;
  525. case SP_NODE2D_POS: {
  526. #ifdef DEBUG_ENABLED
  527. if (value.get_type() != Variant::VECTOR2) {
  528. ERR_PRINT("Position key at time " + rtos(p_time) + " in Animation Track '" + String(pa->owner->path) + "' not of type Vector2(). Animation '" + a->get_name() + "' at node '" + get_path() + "'.");
  529. }
  530. #endif
  531. static_cast<Node2D *>(pa->object)->set_position(value);
  532. } break;
  533. case SP_NODE2D_ROT: {
  534. #ifdef DEBUG_ENABLED
  535. if (value.is_num()) {
  536. ERR_PRINT("Rotation key at time " + rtos(p_time) + " in Animation Track '" + String(pa->owner->path) + "' not numerical. Animation '" + a->get_name() + "' at node '" + get_path() + "'.");
  537. }
  538. #endif
  539. static_cast<Node2D *>(pa->object)->set_rotation((double)value);
  540. } break;
  541. case SP_NODE2D_SCALE: {
  542. #ifdef DEBUG_ENABLED
  543. if (value.get_type() != Variant::VECTOR2) {
  544. ERR_PRINT("Scale key at time " + rtos(p_time) + " in Animation Track '" + String(pa->owner->path) + "' not of type Vector2()." + a->get_name() + "' at node '" + get_path() + "'.");
  545. }
  546. #endif
  547. static_cast<Node2D *>(pa->object)->set_scale(value);
  548. } break;
  549. }
  550. }
  551. }
  552. } break;
  553. case Animation::TYPE_METHOD: {
  554. if (!nc->node) {
  555. continue;
  556. }
  557. if (p_delta == 0) {
  558. continue;
  559. }
  560. if (!p_is_current) {
  561. break;
  562. }
  563. List<int> indices;
  564. a->method_track_get_key_indices(i, p_time, p_delta, &indices);
  565. for (int &E : indices) {
  566. StringName method = a->method_track_get_name(i, E);
  567. Vector<Variant> params = a->method_track_get_params(i, E);
  568. int s = params.size();
  569. ERR_CONTINUE(s > VARIANT_ARG_MAX);
  570. #ifdef DEBUG_ENABLED
  571. if (!nc->node->has_method(method)) {
  572. ERR_PRINT("Invalid method call '" + method + "'. '" + a->get_name() + "' at node '" + get_path() + "'.");
  573. }
  574. #endif
  575. static_assert(VARIANT_ARG_MAX == 8, "This code needs to be updated if VARIANT_ARG_MAX != 8");
  576. if (can_call) {
  577. if (method_call_mode == ANIMATION_METHOD_CALL_DEFERRED) {
  578. MessageQueue::get_singleton()->push_call(
  579. nc->node,
  580. method,
  581. s >= 1 ? params[0] : Variant(),
  582. s >= 2 ? params[1] : Variant(),
  583. s >= 3 ? params[2] : Variant(),
  584. s >= 4 ? params[3] : Variant(),
  585. s >= 5 ? params[4] : Variant(),
  586. s >= 6 ? params[5] : Variant(),
  587. s >= 7 ? params[6] : Variant(),
  588. s >= 8 ? params[7] : Variant());
  589. } else {
  590. nc->node->call(
  591. method,
  592. s >= 1 ? params[0] : Variant(),
  593. s >= 2 ? params[1] : Variant(),
  594. s >= 3 ? params[2] : Variant(),
  595. s >= 4 ? params[3] : Variant(),
  596. s >= 5 ? params[4] : Variant(),
  597. s >= 6 ? params[5] : Variant(),
  598. s >= 7 ? params[6] : Variant(),
  599. s >= 8 ? params[7] : Variant());
  600. }
  601. }
  602. }
  603. } break;
  604. case Animation::TYPE_BEZIER: {
  605. if (!nc->node) {
  606. continue;
  607. }
  608. Map<StringName, TrackNodeCache::BezierAnim>::Element *E = nc->bezier_anim.find(a->track_get_path(i).get_concatenated_subnames());
  609. ERR_CONTINUE(!E); //should it continue, or create a new one?
  610. TrackNodeCache::BezierAnim *ba = &E->get();
  611. float bezier = a->bezier_track_interpolate(i, p_time);
  612. if (ba->accum_pass != accum_pass) {
  613. ERR_CONTINUE(cache_update_bezier_size >= NODE_CACHE_UPDATE_MAX);
  614. cache_update_bezier[cache_update_bezier_size++] = ba;
  615. ba->bezier_accum = bezier;
  616. ba->accum_pass = accum_pass;
  617. } else {
  618. ba->bezier_accum = Math::lerp(ba->bezier_accum, bezier, p_interp);
  619. }
  620. } break;
  621. case Animation::TYPE_AUDIO: {
  622. if (!nc->node) {
  623. continue;
  624. }
  625. if (p_delta == 0) {
  626. continue;
  627. }
  628. if (p_seeked) {
  629. //find whatever should be playing
  630. int idx = a->track_find_key(i, p_time);
  631. if (idx < 0) {
  632. continue;
  633. }
  634. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  635. if (!stream.is_valid()) {
  636. nc->node->call("stop");
  637. nc->audio_playing = false;
  638. playing_caches.erase(nc);
  639. } else {
  640. float start_ofs = a->audio_track_get_key_start_offset(i, idx);
  641. start_ofs += p_time - a->track_get_key_time(i, idx);
  642. float end_ofs = a->audio_track_get_key_end_offset(i, idx);
  643. float len = stream->get_length();
  644. if (start_ofs > len - end_ofs) {
  645. nc->node->call("stop");
  646. nc->audio_playing = false;
  647. playing_caches.erase(nc);
  648. continue;
  649. }
  650. nc->node->call("set_stream", stream);
  651. nc->node->call("play", start_ofs);
  652. nc->audio_playing = true;
  653. playing_caches.insert(nc);
  654. if (len && end_ofs > 0) { //force an end at a time
  655. nc->audio_len = len - start_ofs - end_ofs;
  656. } else {
  657. nc->audio_len = 0;
  658. }
  659. nc->audio_start = p_time;
  660. }
  661. } else {
  662. //find stuff to play
  663. List<int> to_play;
  664. a->track_get_key_indices_in_range(i, p_time, p_delta, &to_play);
  665. if (to_play.size()) {
  666. int idx = to_play.back()->get();
  667. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  668. if (!stream.is_valid()) {
  669. nc->node->call("stop");
  670. nc->audio_playing = false;
  671. playing_caches.erase(nc);
  672. } else {
  673. float start_ofs = a->audio_track_get_key_start_offset(i, idx);
  674. float end_ofs = a->audio_track_get_key_end_offset(i, idx);
  675. float len = stream->get_length();
  676. nc->node->call("set_stream", stream);
  677. nc->node->call("play", start_ofs);
  678. nc->audio_playing = true;
  679. playing_caches.insert(nc);
  680. if (len && end_ofs > 0) { //force an end at a time
  681. nc->audio_len = len - start_ofs - end_ofs;
  682. } else {
  683. nc->audio_len = 0;
  684. }
  685. nc->audio_start = p_time;
  686. }
  687. } else if (nc->audio_playing) {
  688. bool loop = a->has_loop();
  689. bool stop = false;
  690. if (!loop && p_time < nc->audio_start) {
  691. stop = true;
  692. } else if (nc->audio_len > 0) {
  693. float len = nc->audio_start > p_time ? (a->get_length() - nc->audio_start) + p_time : p_time - nc->audio_start;
  694. if (len > nc->audio_len) {
  695. stop = true;
  696. }
  697. }
  698. if (stop) {
  699. //time to stop
  700. nc->node->call("stop");
  701. nc->audio_playing = false;
  702. playing_caches.erase(nc);
  703. }
  704. }
  705. }
  706. } break;
  707. case Animation::TYPE_ANIMATION: {
  708. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(nc->node);
  709. if (!player) {
  710. continue;
  711. }
  712. if (p_delta == 0 || p_seeked) {
  713. //seek
  714. int idx = a->track_find_key(i, p_time);
  715. if (idx < 0) {
  716. continue;
  717. }
  718. double pos = a->track_get_key_time(i, idx);
  719. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  720. if (String(anim_name) == "[stop]" || !player->has_animation(anim_name)) {
  721. continue;
  722. }
  723. Ref<Animation> anim = player->get_animation(anim_name);
  724. double at_anim_pos;
  725. if (anim->has_loop()) {
  726. at_anim_pos = Math::fposmod(p_time - pos, (double)anim->get_length()); //seek to loop
  727. } else {
  728. at_anim_pos = MIN((double)anim->get_length(), p_time - pos); //seek to end
  729. }
  730. if (player->is_playing() || p_seeked) {
  731. player->play(anim_name);
  732. player->seek(at_anim_pos);
  733. nc->animation_playing = true;
  734. playing_caches.insert(nc);
  735. } else {
  736. player->set_assigned_animation(anim_name);
  737. player->seek(at_anim_pos, true);
  738. }
  739. } else {
  740. //find stuff to play
  741. List<int> to_play;
  742. a->track_get_key_indices_in_range(i, p_time, p_delta, &to_play);
  743. if (to_play.size()) {
  744. int idx = to_play.back()->get();
  745. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  746. if (String(anim_name) == "[stop]" || !player->has_animation(anim_name)) {
  747. if (playing_caches.has(nc)) {
  748. playing_caches.erase(nc);
  749. player->stop();
  750. nc->animation_playing = false;
  751. }
  752. } else {
  753. player->play(anim_name);
  754. player->seek(0.0, true);
  755. nc->animation_playing = true;
  756. playing_caches.insert(nc);
  757. }
  758. }
  759. }
  760. } break;
  761. }
  762. }
  763. }
  764. void AnimationPlayer::_animation_process_data(PlaybackData &cd, double p_delta, float p_blend, bool p_seeked, bool p_started) {
  765. double delta = p_delta * speed_scale * cd.speed_scale;
  766. double next_pos = cd.pos + delta;
  767. real_t len = cd.from->animation->get_length();
  768. bool loop = cd.from->animation->has_loop();
  769. if (!loop) {
  770. if (next_pos < 0) {
  771. next_pos = 0;
  772. } else if (next_pos > len) {
  773. next_pos = len;
  774. }
  775. bool backwards = signbit(delta); // Negative zero means playing backwards too
  776. delta = next_pos - cd.pos; // Fix delta (after determination of backwards because negative zero is lost here)
  777. if (&cd == &playback.current) {
  778. if (!backwards && cd.pos <= len && next_pos == len) {
  779. //playback finished
  780. end_reached = true;
  781. end_notify = cd.pos < len; // Notify only if not already at the end
  782. }
  783. if (backwards && cd.pos >= 0 && next_pos == 0) {
  784. //playback finished
  785. end_reached = true;
  786. end_notify = cd.pos > 0; // Notify only if not already at the beginning
  787. }
  788. }
  789. } else {
  790. double looped_next_pos = Math::fposmod(next_pos, (double)len);
  791. if (looped_next_pos == 0 && next_pos != 0) {
  792. // Loop multiples of the length to it, rather than 0
  793. // so state at time=length is previewable in the editor
  794. next_pos = len;
  795. } else {
  796. next_pos = looped_next_pos;
  797. }
  798. }
  799. cd.pos = next_pos;
  800. _animation_process_animation(cd.from, cd.pos, delta, p_blend, &cd == &playback.current, p_seeked, p_started);
  801. }
  802. void AnimationPlayer::_animation_process2(double p_delta, bool p_started) {
  803. Playback &c = playback;
  804. accum_pass++;
  805. _animation_process_data(c.current, p_delta, 1.0f, c.seeked && p_delta != 0, p_started);
  806. if (p_delta != 0) {
  807. c.seeked = false;
  808. }
  809. List<Blend>::Element *prev = nullptr;
  810. for (List<Blend>::Element *E = c.blend.back(); E; E = prev) {
  811. Blend &b = E->get();
  812. float blend = b.blend_left / b.blend_time;
  813. _animation_process_data(b.data, p_delta, blend, false, false);
  814. b.blend_left -= Math::absf(speed_scale * p_delta);
  815. prev = E->prev();
  816. if (b.blend_left < 0) {
  817. c.blend.erase(E);
  818. }
  819. }
  820. }
  821. void AnimationPlayer::_animation_update_transforms() {
  822. {
  823. Transform3D t;
  824. for (int i = 0; i < cache_update_size; i++) {
  825. TrackNodeCache *nc = cache_update[i];
  826. ERR_CONTINUE(nc->accum_pass != accum_pass);
  827. #ifndef _3D_DISABLED
  828. if (nc->skeleton && nc->bone_idx >= 0) {
  829. if (nc->loc_used) {
  830. nc->skeleton->set_bone_pose_position(nc->bone_idx, nc->loc_accum);
  831. }
  832. if (nc->rot_used) {
  833. nc->skeleton->set_bone_pose_rotation(nc->bone_idx, nc->rot_accum);
  834. }
  835. if (nc->scale_used) {
  836. nc->skeleton->set_bone_pose_scale(nc->bone_idx, nc->scale_accum);
  837. }
  838. } else if (nc->node_blend_shape) {
  839. nc->node_blend_shape->set_blend_shape_value(nc->blend_shape_idx, nc->blend_shape_accum);
  840. } else if (nc->node_3d) {
  841. if (nc->loc_used) {
  842. nc->node_3d->set_position(nc->loc_accum);
  843. }
  844. if (nc->rot_used) {
  845. nc->node_3d->set_rotation(nc->rot_accum.get_euler());
  846. }
  847. if (nc->scale_used) {
  848. nc->node_3d->set_scale(nc->scale_accum);
  849. }
  850. }
  851. #endif // _3D_DISABLED
  852. }
  853. }
  854. cache_update_size = 0;
  855. for (int i = 0; i < cache_update_prop_size; i++) {
  856. TrackNodeCache::PropertyAnim *pa = cache_update_prop[i];
  857. ERR_CONTINUE(pa->accum_pass != accum_pass);
  858. switch (pa->special) {
  859. case SP_NONE: {
  860. bool valid;
  861. pa->object->set_indexed(pa->subpath, pa->value_accum, &valid); //you are not speshul
  862. #ifdef DEBUG_ENABLED
  863. if (!valid) {
  864. ERR_PRINT("Failed setting key at time " + rtos(playback.current.pos) + " in Animation '" + get_current_animation() + "' at Node '" + get_path() + "', Track '" + String(pa->owner->path) + "'. Check if property exists or the type of key is right for the property");
  865. }
  866. #endif
  867. } break;
  868. case SP_NODE2D_POS: {
  869. #ifdef DEBUG_ENABLED
  870. if (pa->value_accum.get_type() != Variant::VECTOR2) {
  871. ERR_PRINT("Position key at time " + rtos(playback.current.pos) + " in Animation '" + get_current_animation() + "' at Node '" + get_path() + "', Track '" + String(pa->owner->path) + "' not of type Vector2()");
  872. }
  873. #endif
  874. static_cast<Node2D *>(pa->object)->set_position(pa->value_accum);
  875. } break;
  876. case SP_NODE2D_ROT: {
  877. #ifdef DEBUG_ENABLED
  878. if (pa->value_accum.is_num()) {
  879. ERR_PRINT("Rotation key at time " + rtos(playback.current.pos) + " in Animation '" + get_current_animation() + "' at Node '" + get_path() + "', Track '" + String(pa->owner->path) + "' not numerical");
  880. }
  881. #endif
  882. static_cast<Node2D *>(pa->object)->set_rotation(Math::deg2rad((double)pa->value_accum));
  883. } break;
  884. case SP_NODE2D_SCALE: {
  885. #ifdef DEBUG_ENABLED
  886. if (pa->value_accum.get_type() != Variant::VECTOR2) {
  887. ERR_PRINT("Scale key at time " + rtos(playback.current.pos) + " in Animation '" + get_current_animation() + "' at Node '" + get_path() + "', Track '" + String(pa->owner->path) + "' not of type Vector2()");
  888. }
  889. #endif
  890. static_cast<Node2D *>(pa->object)->set_scale(pa->value_accum);
  891. } break;
  892. }
  893. }
  894. cache_update_prop_size = 0;
  895. for (int i = 0; i < cache_update_bezier_size; i++) {
  896. TrackNodeCache::BezierAnim *ba = cache_update_bezier[i];
  897. ERR_CONTINUE(ba->accum_pass != accum_pass);
  898. ba->object->set_indexed(ba->bezier_property, ba->bezier_accum);
  899. }
  900. cache_update_bezier_size = 0;
  901. }
  902. void AnimationPlayer::_animation_process(double p_delta) {
  903. if (playback.current.from) {
  904. end_reached = false;
  905. end_notify = false;
  906. _animation_process2(p_delta, playback.started);
  907. if (playback.started) {
  908. playback.started = false;
  909. }
  910. _animation_update_transforms();
  911. if (end_reached) {
  912. if (queued.size()) {
  913. String old = playback.assigned;
  914. play(queued.front()->get());
  915. String new_name = playback.assigned;
  916. queued.pop_front();
  917. if (end_notify) {
  918. emit_signal(SceneStringNames::get_singleton()->animation_changed, old, new_name);
  919. }
  920. } else {
  921. //stop();
  922. playing = false;
  923. _set_process(false);
  924. if (end_notify) {
  925. emit_signal(SceneStringNames::get_singleton()->animation_finished, playback.assigned);
  926. }
  927. }
  928. end_reached = false;
  929. }
  930. } else {
  931. _set_process(false);
  932. }
  933. }
  934. Error AnimationPlayer::add_animation(const StringName &p_name, const Ref<Animation> &p_animation) {
  935. #ifdef DEBUG_ENABLED
  936. ERR_FAIL_COND_V_MSG(String(p_name).find("/") != -1 || String(p_name).find(":") != -1 || String(p_name).find(",") != -1 || String(p_name).find("[") != -1, ERR_INVALID_PARAMETER, "Invalid animation name: " + String(p_name) + ".");
  937. #endif
  938. ERR_FAIL_COND_V(p_animation.is_null(), ERR_INVALID_PARAMETER);
  939. if (animation_set.has(p_name)) {
  940. _unref_anim(animation_set[p_name].animation);
  941. animation_set[p_name].animation = p_animation;
  942. clear_caches();
  943. } else {
  944. AnimationData ad;
  945. ad.animation = p_animation;
  946. ad.name = p_name;
  947. animation_set[p_name] = ad;
  948. }
  949. _ref_anim(p_animation);
  950. notify_property_list_changed();
  951. return OK;
  952. }
  953. void AnimationPlayer::remove_animation(const StringName &p_name) {
  954. ERR_FAIL_COND(!animation_set.has(p_name));
  955. stop();
  956. _unref_anim(animation_set[p_name].animation);
  957. animation_set.erase(p_name);
  958. clear_caches();
  959. notify_property_list_changed();
  960. }
  961. void AnimationPlayer::_ref_anim(const Ref<Animation> &p_anim) {
  962. Ref<Animation>(p_anim)->connect(SceneStringNames::get_singleton()->tracks_changed, callable_mp(this, &AnimationPlayer::_animation_changed), varray(), CONNECT_REFERENCE_COUNTED);
  963. }
  964. void AnimationPlayer::_unref_anim(const Ref<Animation> &p_anim) {
  965. Ref<Animation>(p_anim)->disconnect(SceneStringNames::get_singleton()->tracks_changed, callable_mp(this, &AnimationPlayer::_animation_changed));
  966. }
  967. void AnimationPlayer::rename_animation(const StringName &p_name, const StringName &p_new_name) {
  968. ERR_FAIL_COND(!animation_set.has(p_name));
  969. ERR_FAIL_COND(String(p_new_name).find("/") != -1 || String(p_new_name).find(":") != -1);
  970. ERR_FAIL_COND(animation_set.has(p_new_name));
  971. stop();
  972. AnimationData ad = animation_set[p_name];
  973. ad.name = p_new_name;
  974. animation_set.erase(p_name);
  975. animation_set[p_new_name] = ad;
  976. List<BlendKey> to_erase;
  977. Map<BlendKey, float> to_insert;
  978. for (const KeyValue<BlendKey, float> &E : blend_times) {
  979. BlendKey bk = E.key;
  980. BlendKey new_bk = bk;
  981. bool erase = false;
  982. if (bk.from == p_name) {
  983. new_bk.from = p_new_name;
  984. erase = true;
  985. }
  986. if (bk.to == p_name) {
  987. new_bk.to = p_new_name;
  988. erase = true;
  989. }
  990. if (erase) {
  991. to_erase.push_back(bk);
  992. to_insert[new_bk] = E.value;
  993. }
  994. }
  995. while (to_erase.size()) {
  996. blend_times.erase(to_erase.front()->get());
  997. to_erase.pop_front();
  998. }
  999. while (to_insert.size()) {
  1000. blend_times[to_insert.front()->key()] = to_insert.front()->get();
  1001. to_insert.erase(to_insert.front());
  1002. }
  1003. if (autoplay == p_name) {
  1004. autoplay = p_new_name;
  1005. }
  1006. clear_caches();
  1007. notify_property_list_changed();
  1008. }
  1009. bool AnimationPlayer::has_animation(const StringName &p_name) const {
  1010. return animation_set.has(p_name);
  1011. }
  1012. Ref<Animation> AnimationPlayer::get_animation(const StringName &p_name) const {
  1013. ERR_FAIL_COND_V(!animation_set.has(p_name), Ref<Animation>());
  1014. const AnimationData &data = animation_set[p_name];
  1015. return data.animation;
  1016. }
  1017. void AnimationPlayer::get_animation_list(List<StringName> *p_animations) const {
  1018. List<String> anims;
  1019. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  1020. anims.push_back(E.key);
  1021. }
  1022. anims.sort();
  1023. for (const String &E : anims) {
  1024. p_animations->push_back(E);
  1025. }
  1026. }
  1027. void AnimationPlayer::set_blend_time(const StringName &p_animation1, const StringName &p_animation2, float p_time) {
  1028. ERR_FAIL_COND(!animation_set.has(p_animation1));
  1029. ERR_FAIL_COND(!animation_set.has(p_animation2));
  1030. ERR_FAIL_COND_MSG(p_time < 0, "Blend time cannot be smaller than 0.");
  1031. BlendKey bk;
  1032. bk.from = p_animation1;
  1033. bk.to = p_animation2;
  1034. if (p_time == 0) {
  1035. blend_times.erase(bk);
  1036. } else {
  1037. blend_times[bk] = p_time;
  1038. }
  1039. }
  1040. float AnimationPlayer::get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const {
  1041. BlendKey bk;
  1042. bk.from = p_animation1;
  1043. bk.to = p_animation2;
  1044. if (blend_times.has(bk)) {
  1045. return blend_times[bk];
  1046. } else {
  1047. return 0;
  1048. }
  1049. }
  1050. void AnimationPlayer::queue(const StringName &p_name) {
  1051. if (!is_playing()) {
  1052. play(p_name);
  1053. } else {
  1054. queued.push_back(p_name);
  1055. }
  1056. }
  1057. Vector<String> AnimationPlayer::get_queue() {
  1058. Vector<String> ret;
  1059. for (const StringName &E : queued) {
  1060. ret.push_back(E);
  1061. }
  1062. return ret;
  1063. }
  1064. void AnimationPlayer::clear_queue() {
  1065. queued.clear();
  1066. }
  1067. void AnimationPlayer::play_backwards(const StringName &p_name, float p_custom_blend) {
  1068. play(p_name, p_custom_blend, -1, true);
  1069. }
  1070. void AnimationPlayer::play(const StringName &p_name, float p_custom_blend, float p_custom_scale, bool p_from_end) {
  1071. StringName name = p_name;
  1072. if (String(name) == "") {
  1073. name = playback.assigned;
  1074. }
  1075. ERR_FAIL_COND_MSG(!animation_set.has(name), "Animation not found: " + name + ".");
  1076. Playback &c = playback;
  1077. if (c.current.from) {
  1078. float blend_time = 0.0;
  1079. // find if it can blend
  1080. BlendKey bk;
  1081. bk.from = c.current.from->name;
  1082. bk.to = name;
  1083. if (p_custom_blend >= 0) {
  1084. blend_time = p_custom_blend;
  1085. } else if (blend_times.has(bk)) {
  1086. blend_time = blend_times[bk];
  1087. } else {
  1088. bk.from = "*";
  1089. if (blend_times.has(bk)) {
  1090. blend_time = blend_times[bk];
  1091. } else {
  1092. bk.from = c.current.from->name;
  1093. bk.to = "*";
  1094. if (blend_times.has(bk)) {
  1095. blend_time = blend_times[bk];
  1096. }
  1097. }
  1098. }
  1099. if (p_custom_blend < 0 && blend_time == 0 && default_blend_time) {
  1100. blend_time = default_blend_time;
  1101. }
  1102. if (blend_time > 0) {
  1103. Blend b;
  1104. b.data = c.current;
  1105. b.blend_time = b.blend_left = blend_time;
  1106. c.blend.push_back(b);
  1107. }
  1108. }
  1109. if (get_current_animation() != p_name) {
  1110. _stop_playing_caches();
  1111. }
  1112. c.current.from = &animation_set[name];
  1113. if (c.assigned != name) { // reset
  1114. c.current.pos = p_from_end ? c.current.from->animation->get_length() : 0;
  1115. } else {
  1116. if (p_from_end && c.current.pos == 0) {
  1117. // Animation reset BUT played backwards, set position to the end
  1118. c.current.pos = c.current.from->animation->get_length();
  1119. } else if (!p_from_end && c.current.pos == c.current.from->animation->get_length()) {
  1120. // Animation resumed but already ended, set position to the beginning
  1121. c.current.pos = 0;
  1122. }
  1123. }
  1124. c.current.speed_scale = p_custom_scale;
  1125. c.assigned = name;
  1126. c.seeked = false;
  1127. c.started = true;
  1128. if (!end_reached) {
  1129. queued.clear();
  1130. }
  1131. _set_process(true); // always process when starting an animation
  1132. playing = true;
  1133. emit_signal(SceneStringNames::get_singleton()->animation_started, c.assigned);
  1134. if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) {
  1135. return; // no next in this case
  1136. }
  1137. StringName next = animation_get_next(p_name);
  1138. if (next != StringName() && animation_set.has(next)) {
  1139. queue(next);
  1140. }
  1141. }
  1142. bool AnimationPlayer::is_playing() const {
  1143. return playing;
  1144. }
  1145. void AnimationPlayer::set_current_animation(const String &p_anim) {
  1146. if (p_anim == "[stop]" || p_anim == "") {
  1147. stop();
  1148. } else if (!is_playing() || playback.assigned != p_anim) {
  1149. play(p_anim);
  1150. } else {
  1151. // Same animation, do not replay from start
  1152. }
  1153. }
  1154. String AnimationPlayer::get_current_animation() const {
  1155. return (is_playing() ? playback.assigned : "");
  1156. }
  1157. void AnimationPlayer::set_assigned_animation(const String &p_anim) {
  1158. if (is_playing()) {
  1159. play(p_anim);
  1160. } else {
  1161. ERR_FAIL_COND(!animation_set.has(p_anim));
  1162. playback.current.pos = 0;
  1163. playback.current.from = &animation_set[p_anim];
  1164. playback.assigned = p_anim;
  1165. }
  1166. }
  1167. String AnimationPlayer::get_assigned_animation() const {
  1168. return playback.assigned;
  1169. }
  1170. void AnimationPlayer::stop(bool p_reset) {
  1171. _stop_playing_caches();
  1172. Playback &c = playback;
  1173. c.blend.clear();
  1174. if (p_reset) {
  1175. c.current.from = nullptr;
  1176. c.current.speed_scale = 1;
  1177. c.current.pos = 0;
  1178. }
  1179. _set_process(false);
  1180. queued.clear();
  1181. playing = false;
  1182. }
  1183. void AnimationPlayer::set_speed_scale(float p_speed) {
  1184. speed_scale = p_speed;
  1185. }
  1186. float AnimationPlayer::get_speed_scale() const {
  1187. return speed_scale;
  1188. }
  1189. float AnimationPlayer::get_playing_speed() const {
  1190. if (!playing) {
  1191. return 0;
  1192. }
  1193. return speed_scale * playback.current.speed_scale;
  1194. }
  1195. void AnimationPlayer::seek(double p_time, bool p_update) {
  1196. if (!playback.current.from) {
  1197. if (playback.assigned) {
  1198. ERR_FAIL_COND(!animation_set.has(playback.assigned));
  1199. playback.current.from = &animation_set[playback.assigned];
  1200. }
  1201. ERR_FAIL_COND(!playback.current.from);
  1202. }
  1203. playback.current.pos = p_time;
  1204. playback.seeked = true;
  1205. if (p_update) {
  1206. _animation_process(0);
  1207. }
  1208. }
  1209. void AnimationPlayer::seek_delta(double p_time, float p_delta) {
  1210. if (!playback.current.from) {
  1211. if (playback.assigned) {
  1212. ERR_FAIL_COND(!animation_set.has(playback.assigned));
  1213. playback.current.from = &animation_set[playback.assigned];
  1214. }
  1215. ERR_FAIL_COND(!playback.current.from);
  1216. }
  1217. playback.current.pos = p_time - p_delta;
  1218. if (speed_scale != 0.0) {
  1219. p_delta /= speed_scale;
  1220. }
  1221. _animation_process(p_delta);
  1222. //playback.current.pos=p_time;
  1223. }
  1224. bool AnimationPlayer::is_valid() const {
  1225. return (playback.current.from);
  1226. }
  1227. float AnimationPlayer::get_current_animation_position() const {
  1228. ERR_FAIL_COND_V_MSG(!playback.current.from, 0, "AnimationPlayer has no current animation");
  1229. return playback.current.pos;
  1230. }
  1231. float AnimationPlayer::get_current_animation_length() const {
  1232. ERR_FAIL_COND_V_MSG(!playback.current.from, 0, "AnimationPlayer has no current animation");
  1233. return playback.current.from->animation->get_length();
  1234. }
  1235. void AnimationPlayer::_animation_changed() {
  1236. clear_caches();
  1237. emit_signal(SNAME("caches_cleared"));
  1238. if (is_playing()) {
  1239. playback.seeked = true; //need to restart stuff, like audio
  1240. }
  1241. }
  1242. void AnimationPlayer::_stop_playing_caches() {
  1243. for (Set<TrackNodeCache *>::Element *E = playing_caches.front(); E; E = E->next()) {
  1244. if (E->get()->node && E->get()->audio_playing) {
  1245. E->get()->node->call("stop");
  1246. }
  1247. if (E->get()->node && E->get()->animation_playing) {
  1248. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(E->get()->node);
  1249. if (!player) {
  1250. continue;
  1251. }
  1252. player->stop();
  1253. }
  1254. }
  1255. playing_caches.clear();
  1256. }
  1257. void AnimationPlayer::_node_removed(Node *p_node) {
  1258. clear_caches(); // nodes contained here are being removed, clear the caches
  1259. }
  1260. void AnimationPlayer::clear_caches() {
  1261. _stop_playing_caches();
  1262. node_cache_map.clear();
  1263. for (KeyValue<StringName, AnimationData> &E : animation_set) {
  1264. E.value.node_cache.clear();
  1265. }
  1266. cache_update_size = 0;
  1267. cache_update_prop_size = 0;
  1268. cache_update_bezier_size = 0;
  1269. }
  1270. void AnimationPlayer::set_active(bool p_active) {
  1271. if (active == p_active) {
  1272. return;
  1273. }
  1274. active = p_active;
  1275. _set_process(processing, true);
  1276. }
  1277. bool AnimationPlayer::is_active() const {
  1278. return active;
  1279. }
  1280. StringName AnimationPlayer::find_animation(const Ref<Animation> &p_animation) const {
  1281. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  1282. if (E.value.animation == p_animation) {
  1283. return E.key;
  1284. }
  1285. }
  1286. return "";
  1287. }
  1288. void AnimationPlayer::set_autoplay(const String &p_name) {
  1289. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
  1290. WARN_PRINT("Setting autoplay after the node has been added to the scene has no effect.");
  1291. }
  1292. autoplay = p_name;
  1293. }
  1294. String AnimationPlayer::get_autoplay() const {
  1295. return autoplay;
  1296. }
  1297. void AnimationPlayer::set_reset_on_save_enabled(bool p_enabled) {
  1298. reset_on_save = p_enabled;
  1299. }
  1300. bool AnimationPlayer::is_reset_on_save_enabled() const {
  1301. return reset_on_save;
  1302. }
  1303. void AnimationPlayer::set_process_callback(AnimationProcessCallback p_mode) {
  1304. if (process_callback == p_mode) {
  1305. return;
  1306. }
  1307. bool pr = processing;
  1308. if (pr) {
  1309. _set_process(false);
  1310. }
  1311. process_callback = p_mode;
  1312. if (pr) {
  1313. _set_process(true);
  1314. }
  1315. }
  1316. AnimationPlayer::AnimationProcessCallback AnimationPlayer::get_process_callback() const {
  1317. return process_callback;
  1318. }
  1319. void AnimationPlayer::set_method_call_mode(AnimationMethodCallMode p_mode) {
  1320. method_call_mode = p_mode;
  1321. }
  1322. AnimationPlayer::AnimationMethodCallMode AnimationPlayer::get_method_call_mode() const {
  1323. return method_call_mode;
  1324. }
  1325. void AnimationPlayer::_set_process(bool p_process, bool p_force) {
  1326. if (processing == p_process && !p_force) {
  1327. return;
  1328. }
  1329. switch (process_callback) {
  1330. case ANIMATION_PROCESS_PHYSICS:
  1331. set_physics_process_internal(p_process && active);
  1332. break;
  1333. case ANIMATION_PROCESS_IDLE:
  1334. set_process_internal(p_process && active);
  1335. break;
  1336. case ANIMATION_PROCESS_MANUAL:
  1337. break;
  1338. }
  1339. processing = p_process;
  1340. }
  1341. void AnimationPlayer::animation_set_next(const StringName &p_animation, const StringName &p_next) {
  1342. ERR_FAIL_COND(!animation_set.has(p_animation));
  1343. animation_set[p_animation].next = p_next;
  1344. }
  1345. StringName AnimationPlayer::animation_get_next(const StringName &p_animation) const {
  1346. if (!animation_set.has(p_animation)) {
  1347. return StringName();
  1348. }
  1349. return animation_set[p_animation].next;
  1350. }
  1351. void AnimationPlayer::set_default_blend_time(float p_default) {
  1352. default_blend_time = p_default;
  1353. }
  1354. float AnimationPlayer::get_default_blend_time() const {
  1355. return default_blend_time;
  1356. }
  1357. void AnimationPlayer::set_root(const NodePath &p_root) {
  1358. root = p_root;
  1359. clear_caches();
  1360. }
  1361. NodePath AnimationPlayer::get_root() const {
  1362. return root;
  1363. }
  1364. void AnimationPlayer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1365. String pf = p_function;
  1366. if (p_idx == 0 && (p_function == "play" || p_function == "play_backwards" || p_function == "remove_animation" || p_function == "has_animation" || p_function == "queue")) {
  1367. List<StringName> al;
  1368. get_animation_list(&al);
  1369. for (const StringName &name : al) {
  1370. r_options->push_back(String(name).quote());
  1371. }
  1372. }
  1373. Node::get_argument_options(p_function, p_idx, r_options);
  1374. }
  1375. #ifdef TOOLS_ENABLED
  1376. Ref<AnimatedValuesBackup> AnimationPlayer::backup_animated_values(Node *p_root_override) {
  1377. Ref<AnimatedValuesBackup> backup;
  1378. if (!playback.current.from) {
  1379. return backup;
  1380. }
  1381. _ensure_node_caches(playback.current.from, p_root_override);
  1382. backup.instantiate();
  1383. for (int i = 0; i < playback.current.from->node_cache.size(); i++) {
  1384. TrackNodeCache *nc = playback.current.from->node_cache[i];
  1385. if (!nc) {
  1386. continue;
  1387. }
  1388. if (nc->skeleton) {
  1389. if (nc->bone_idx == -1) {
  1390. continue;
  1391. }
  1392. AnimatedValuesBackup::Entry entry;
  1393. entry.object = nc->skeleton;
  1394. entry.bone_idx = nc->bone_idx;
  1395. Array arr;
  1396. arr.resize(3);
  1397. arr[0] = nc->skeleton->get_bone_pose_position(nc->bone_idx);
  1398. arr[1] = nc->skeleton->get_bone_pose_rotation(nc->bone_idx);
  1399. arr[2] = nc->skeleton->get_bone_pose_scale(nc->bone_idx);
  1400. entry.value = nc;
  1401. backup->entries.push_back(entry);
  1402. } else {
  1403. if (nc->node_3d) {
  1404. AnimatedValuesBackup::Entry entry;
  1405. entry.object = nc->node_3d;
  1406. entry.subpath.push_back("transform");
  1407. entry.value = nc->node_3d->get_transform();
  1408. entry.bone_idx = -1;
  1409. backup->entries.push_back(entry);
  1410. } else {
  1411. for (const KeyValue<StringName, TrackNodeCache::PropertyAnim> &E : nc->property_anim) {
  1412. AnimatedValuesBackup::Entry entry;
  1413. entry.object = E.value.object;
  1414. entry.subpath = E.value.subpath;
  1415. bool valid;
  1416. entry.value = E.value.object->get_indexed(E.value.subpath, &valid);
  1417. entry.bone_idx = -1;
  1418. if (valid) {
  1419. backup->entries.push_back(entry);
  1420. }
  1421. }
  1422. }
  1423. }
  1424. }
  1425. return backup;
  1426. }
  1427. Ref<AnimatedValuesBackup> AnimationPlayer::apply_reset(bool p_user_initiated) {
  1428. ERR_FAIL_COND_V(!can_apply_reset(), Ref<AnimatedValuesBackup>());
  1429. Ref<Animation> reset_anim = animation_set["RESET"].animation;
  1430. ERR_FAIL_COND_V(reset_anim.is_null(), Ref<AnimatedValuesBackup>());
  1431. Node *root_node = get_node_or_null(root);
  1432. ERR_FAIL_COND_V(!root_node, Ref<AnimatedValuesBackup>());
  1433. AnimationPlayer *aux_player = memnew(AnimationPlayer);
  1434. EditorNode::get_singleton()->add_child(aux_player);
  1435. aux_player->add_animation("RESET", reset_anim);
  1436. aux_player->set_assigned_animation("RESET");
  1437. // Forcing the use of the original root because the scene where original player belongs may be not the active one
  1438. Node *root = get_node(get_root());
  1439. Ref<AnimatedValuesBackup> old_values = aux_player->backup_animated_values(root);
  1440. aux_player->seek(0.0f, true);
  1441. aux_player->queue_delete();
  1442. if (p_user_initiated) {
  1443. Ref<AnimatedValuesBackup> new_values = aux_player->backup_animated_values();
  1444. old_values->restore();
  1445. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  1446. ur->create_action(TTR("Anim Apply Reset"));
  1447. ur->add_do_method(new_values.ptr(), "restore");
  1448. ur->add_undo_method(old_values.ptr(), "restore");
  1449. ur->commit_action();
  1450. }
  1451. return old_values;
  1452. }
  1453. bool AnimationPlayer::can_apply_reset() const {
  1454. return has_animation("RESET") && playback.assigned != StringName("RESET");
  1455. }
  1456. #endif
  1457. void AnimationPlayer::_bind_methods() {
  1458. ClassDB::bind_method(D_METHOD("add_animation", "name", "animation"), &AnimationPlayer::add_animation);
  1459. ClassDB::bind_method(D_METHOD("remove_animation", "name"), &AnimationPlayer::remove_animation);
  1460. ClassDB::bind_method(D_METHOD("rename_animation", "name", "newname"), &AnimationPlayer::rename_animation);
  1461. ClassDB::bind_method(D_METHOD("has_animation", "name"), &AnimationPlayer::has_animation);
  1462. ClassDB::bind_method(D_METHOD("get_animation", "name"), &AnimationPlayer::get_animation);
  1463. ClassDB::bind_method(D_METHOD("get_animation_list"), &AnimationPlayer::_get_animation_list);
  1464. ClassDB::bind_method(D_METHOD("animation_set_next", "anim_from", "anim_to"), &AnimationPlayer::animation_set_next);
  1465. ClassDB::bind_method(D_METHOD("animation_get_next", "anim_from"), &AnimationPlayer::animation_get_next);
  1466. ClassDB::bind_method(D_METHOD("set_blend_time", "anim_from", "anim_to", "sec"), &AnimationPlayer::set_blend_time);
  1467. ClassDB::bind_method(D_METHOD("get_blend_time", "anim_from", "anim_to"), &AnimationPlayer::get_blend_time);
  1468. ClassDB::bind_method(D_METHOD("set_default_blend_time", "sec"), &AnimationPlayer::set_default_blend_time);
  1469. ClassDB::bind_method(D_METHOD("get_default_blend_time"), &AnimationPlayer::get_default_blend_time);
  1470. ClassDB::bind_method(D_METHOD("play", "name", "custom_blend", "custom_speed", "from_end"), &AnimationPlayer::play, DEFVAL(""), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false));
  1471. ClassDB::bind_method(D_METHOD("play_backwards", "name", "custom_blend"), &AnimationPlayer::play_backwards, DEFVAL(""), DEFVAL(-1));
  1472. ClassDB::bind_method(D_METHOD("stop", "reset"), &AnimationPlayer::stop, DEFVAL(true));
  1473. ClassDB::bind_method(D_METHOD("is_playing"), &AnimationPlayer::is_playing);
  1474. ClassDB::bind_method(D_METHOD("set_current_animation", "anim"), &AnimationPlayer::set_current_animation);
  1475. ClassDB::bind_method(D_METHOD("get_current_animation"), &AnimationPlayer::get_current_animation);
  1476. ClassDB::bind_method(D_METHOD("set_assigned_animation", "anim"), &AnimationPlayer::set_assigned_animation);
  1477. ClassDB::bind_method(D_METHOD("get_assigned_animation"), &AnimationPlayer::get_assigned_animation);
  1478. ClassDB::bind_method(D_METHOD("queue", "name"), &AnimationPlayer::queue);
  1479. ClassDB::bind_method(D_METHOD("get_queue"), &AnimationPlayer::get_queue);
  1480. ClassDB::bind_method(D_METHOD("clear_queue"), &AnimationPlayer::clear_queue);
  1481. ClassDB::bind_method(D_METHOD("set_active", "active"), &AnimationPlayer::set_active);
  1482. ClassDB::bind_method(D_METHOD("is_active"), &AnimationPlayer::is_active);
  1483. ClassDB::bind_method(D_METHOD("set_speed_scale", "speed"), &AnimationPlayer::set_speed_scale);
  1484. ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimationPlayer::get_speed_scale);
  1485. ClassDB::bind_method(D_METHOD("get_playing_speed"), &AnimationPlayer::get_playing_speed);
  1486. ClassDB::bind_method(D_METHOD("set_autoplay", "name"), &AnimationPlayer::set_autoplay);
  1487. ClassDB::bind_method(D_METHOD("get_autoplay"), &AnimationPlayer::get_autoplay);
  1488. ClassDB::bind_method(D_METHOD("set_reset_on_save_enabled", "enabled"), &AnimationPlayer::set_reset_on_save_enabled);
  1489. ClassDB::bind_method(D_METHOD("is_reset_on_save_enabled"), &AnimationPlayer::is_reset_on_save_enabled);
  1490. ClassDB::bind_method(D_METHOD("set_root", "path"), &AnimationPlayer::set_root);
  1491. ClassDB::bind_method(D_METHOD("get_root"), &AnimationPlayer::get_root);
  1492. ClassDB::bind_method(D_METHOD("find_animation", "animation"), &AnimationPlayer::find_animation);
  1493. ClassDB::bind_method(D_METHOD("clear_caches"), &AnimationPlayer::clear_caches);
  1494. ClassDB::bind_method(D_METHOD("set_process_callback", "mode"), &AnimationPlayer::set_process_callback);
  1495. ClassDB::bind_method(D_METHOD("get_process_callback"), &AnimationPlayer::get_process_callback);
  1496. ClassDB::bind_method(D_METHOD("set_method_call_mode", "mode"), &AnimationPlayer::set_method_call_mode);
  1497. ClassDB::bind_method(D_METHOD("get_method_call_mode"), &AnimationPlayer::get_method_call_mode);
  1498. ClassDB::bind_method(D_METHOD("get_current_animation_position"), &AnimationPlayer::get_current_animation_position);
  1499. ClassDB::bind_method(D_METHOD("get_current_animation_length"), &AnimationPlayer::get_current_animation_length);
  1500. ClassDB::bind_method(D_METHOD("seek", "seconds", "update"), &AnimationPlayer::seek, DEFVAL(false));
  1501. ClassDB::bind_method(D_METHOD("advance", "delta"), &AnimationPlayer::advance);
  1502. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "root_node"), "set_root", "get_root");
  1503. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "current_animation", PROPERTY_HINT_ENUM, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ANIMATE_AS_TRIGGER), "set_current_animation", "get_current_animation");
  1504. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "assigned_animation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_assigned_animation", "get_assigned_animation");
  1505. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "autoplay", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_autoplay", "get_autoplay");
  1506. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reset_on_save", PROPERTY_HINT_NONE, ""), "set_reset_on_save_enabled", "is_reset_on_save_enabled");
  1507. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_length", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_current_animation_length");
  1508. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_current_animation_position");
  1509. ADD_GROUP("Playback Options", "playback_");
  1510. ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_process_mode", PROPERTY_HINT_ENUM, "Physics,Idle,Manual"), "set_process_callback", "get_process_callback");
  1511. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_default_blend_time", PROPERTY_HINT_RANGE, "0,4096,0.01"), "set_default_blend_time", "get_default_blend_time");
  1512. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playback_active", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_active", "is_active");
  1513. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_speed", PROPERTY_HINT_RANGE, "-64,64,0.01"), "set_speed_scale", "get_speed_scale");
  1514. ADD_PROPERTY(PropertyInfo(Variant::INT, "method_call_mode", PROPERTY_HINT_ENUM, "Deferred,Immediate"), "set_method_call_mode", "get_method_call_mode");
  1515. ADD_SIGNAL(MethodInfo("animation_finished", PropertyInfo(Variant::STRING_NAME, "anim_name")));
  1516. ADD_SIGNAL(MethodInfo("animation_changed", PropertyInfo(Variant::STRING_NAME, "old_name"), PropertyInfo(Variant::STRING_NAME, "new_name")));
  1517. ADD_SIGNAL(MethodInfo("animation_started", PropertyInfo(Variant::STRING_NAME, "anim_name")));
  1518. ADD_SIGNAL(MethodInfo("caches_cleared"));
  1519. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_PHYSICS);
  1520. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_IDLE);
  1521. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_MANUAL);
  1522. BIND_ENUM_CONSTANT(ANIMATION_METHOD_CALL_DEFERRED);
  1523. BIND_ENUM_CONSTANT(ANIMATION_METHOD_CALL_IMMEDIATE);
  1524. }
  1525. AnimationPlayer::AnimationPlayer() {
  1526. root = SceneStringNames::get_singleton()->path_pp;
  1527. }
  1528. AnimationPlayer::~AnimationPlayer() {
  1529. }