animation_player.cpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  1. /**************************************************************************/
  2. /* animation_player.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "editor/editor_undo_redo_manager.h"
  38. #include "scene/2d/skeleton_2d.h"
  39. void AnimatedValuesBackup::update_skeletons() {
  40. for (int i = 0; i < entries.size(); i++) {
  41. if (entries[i].bone_idx != -1) {
  42. // 3D bone
  43. Object::cast_to<Skeleton3D>(entries[i].object)->notification(Skeleton3D::NOTIFICATION_UPDATE_SKELETON);
  44. } else {
  45. Bone2D *bone = Object::cast_to<Bone2D>(entries[i].object);
  46. if (bone && bone->skeleton) {
  47. // 2D bone
  48. bone->skeleton->_update_transform();
  49. }
  50. }
  51. }
  52. }
  53. void AnimatedValuesBackup::restore() const {
  54. for (int i = 0; i < entries.size(); i++) {
  55. const AnimatedValuesBackup::Entry *entry = &entries[i];
  56. if (entry->bone_idx == -1) {
  57. entry->object->set_indexed(entry->subpath, entry->value);
  58. } else {
  59. Array arr = entry->value;
  60. if (arr.size() == 3) {
  61. Object::cast_to<Skeleton3D>(entry->object)->set_bone_pose_position(entry->bone_idx, arr[0]);
  62. Object::cast_to<Skeleton3D>(entry->object)->set_bone_pose_rotation(entry->bone_idx, arr[1]);
  63. Object::cast_to<Skeleton3D>(entry->object)->set_bone_pose_scale(entry->bone_idx, arr[2]);
  64. }
  65. }
  66. }
  67. }
  68. void AnimatedValuesBackup::_bind_methods() {
  69. ClassDB::bind_method(D_METHOD("restore"), &AnimatedValuesBackup::restore);
  70. }
  71. #endif
  72. bool AnimationPlayer::_set(const StringName &p_name, const Variant &p_value) {
  73. String name = p_name;
  74. if (name.begins_with("playback/play")) { // bw compatibility
  75. set_current_animation(p_value);
  76. } else if (name.begins_with("anims/")) {
  77. // Backwards compatibility with 3.x, add them to "default" library.
  78. String which = name.get_slicec('/', 1);
  79. Ref<Animation> anim = p_value;
  80. Ref<AnimationLibrary> al;
  81. if (!has_animation_library(StringName())) {
  82. al.instantiate();
  83. add_animation_library(StringName(), al);
  84. } else {
  85. al = get_animation_library(StringName());
  86. }
  87. al->add_animation(which, anim);
  88. } else if (name.begins_with("libraries")) {
  89. Dictionary d = p_value;
  90. while (animation_libraries.size()) {
  91. remove_animation_library(animation_libraries[0].name);
  92. }
  93. List<Variant> keys;
  94. d.get_key_list(&keys);
  95. for (const Variant &K : keys) {
  96. StringName lib_name = K;
  97. Ref<AnimationLibrary> lib = d[lib_name];
  98. add_animation_library(lib_name, lib);
  99. }
  100. emit_signal("animation_libraries_updated");
  101. } else if (name.begins_with("next/")) {
  102. String which = name.get_slicec('/', 1);
  103. animation_set_next(which, p_value);
  104. } else if (p_name == SceneStringNames::get_singleton()->blend_times) {
  105. Array array = p_value;
  106. int len = array.size();
  107. ERR_FAIL_COND_V(len % 3, false);
  108. for (int i = 0; i < len / 3; i++) {
  109. StringName from = array[i * 3 + 0];
  110. StringName to = array[i * 3 + 1];
  111. float time = array[i * 3 + 2];
  112. set_blend_time(from, to, time);
  113. }
  114. } else {
  115. return false;
  116. }
  117. return true;
  118. }
  119. bool AnimationPlayer::_get(const StringName &p_name, Variant &r_ret) const {
  120. String name = p_name;
  121. if (name == "playback/play") { // bw compatibility
  122. r_ret = get_current_animation();
  123. } else if (name.begins_with("libraries")) {
  124. Dictionary d;
  125. for (const AnimationLibraryData &lib : animation_libraries) {
  126. d[lib.name] = lib.library;
  127. }
  128. r_ret = d;
  129. } else if (name.begins_with("next/")) {
  130. String which = name.get_slicec('/', 1);
  131. r_ret = animation_get_next(which);
  132. } else if (name == "blend_times") {
  133. Vector<BlendKey> keys;
  134. for (const KeyValue<BlendKey, double> &E : blend_times) {
  135. keys.ordered_insert(E.key);
  136. }
  137. Array array;
  138. for (int i = 0; i < keys.size(); i++) {
  139. array.push_back(keys[i].from);
  140. array.push_back(keys[i].to);
  141. array.push_back(blend_times.get(keys[i]));
  142. }
  143. r_ret = array;
  144. } else {
  145. return false;
  146. }
  147. return true;
  148. }
  149. void AnimationPlayer::_validate_property(PropertyInfo &p_property) const {
  150. if (p_property.name == "current_animation") {
  151. List<String> names;
  152. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  153. names.push_back(E.key);
  154. }
  155. names.sort();
  156. names.push_front("[stop]");
  157. String hint;
  158. for (List<String>::Element *E = names.front(); E; E = E->next()) {
  159. if (E != names.front()) {
  160. hint += ",";
  161. }
  162. hint += E->get();
  163. }
  164. p_property.hint_string = hint;
  165. }
  166. }
  167. void AnimationPlayer::_get_property_list(List<PropertyInfo> *p_list) const {
  168. List<PropertyInfo> anim_names;
  169. anim_names.push_back(PropertyInfo(Variant::DICTIONARY, "libraries"));
  170. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  171. if (E.value.next != StringName()) {
  172. anim_names.push_back(PropertyInfo(Variant::STRING, "next/" + String(E.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  173. }
  174. }
  175. anim_names.sort();
  176. for (const PropertyInfo &E : anim_names) {
  177. p_list->push_back(E);
  178. }
  179. p_list->push_back(PropertyInfo(Variant::ARRAY, "blend_times", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
  180. }
  181. void AnimationPlayer::advance(double p_time) {
  182. _animation_process(p_time);
  183. }
  184. void AnimationPlayer::_notification(int p_what) {
  185. switch (p_what) {
  186. case NOTIFICATION_ENTER_TREE: {
  187. if (!processing) {
  188. //make sure that a previous process state was not saved
  189. //only process if "processing" is set
  190. set_physics_process_internal(false);
  191. set_process_internal(false);
  192. }
  193. clear_caches();
  194. } break;
  195. case NOTIFICATION_READY: {
  196. if (!Engine::get_singleton()->is_editor_hint() && animation_set.has(autoplay)) {
  197. play(autoplay);
  198. _animation_process(0);
  199. }
  200. } break;
  201. case NOTIFICATION_INTERNAL_PROCESS: {
  202. if (process_callback == ANIMATION_PROCESS_PHYSICS) {
  203. break;
  204. }
  205. if (processing) {
  206. _animation_process(get_process_delta_time());
  207. }
  208. } break;
  209. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  210. if (process_callback == ANIMATION_PROCESS_IDLE) {
  211. break;
  212. }
  213. if (processing) {
  214. _animation_process(get_physics_process_delta_time());
  215. }
  216. } break;
  217. case NOTIFICATION_EXIT_TREE: {
  218. clear_caches();
  219. } break;
  220. }
  221. }
  222. void AnimationPlayer::_ensure_node_caches(AnimationData *p_anim, Node *p_root_override) {
  223. // Already cached?
  224. if (p_anim->node_cache.size() == p_anim->animation->get_track_count()) {
  225. return;
  226. }
  227. Node *parent = p_root_override ? p_root_override : get_node(root);
  228. ERR_FAIL_COND(!parent);
  229. Animation *a = p_anim->animation.operator->();
  230. p_anim->node_cache.resize(a->get_track_count());
  231. setup_pass++;
  232. for (int i = 0; i < a->get_track_count(); i++) {
  233. p_anim->node_cache.write[i] = nullptr;
  234. if (!a->track_is_enabled(i)) {
  235. continue;
  236. }
  237. Ref<Resource> resource;
  238. Vector<StringName> leftover_path;
  239. Node *child = parent->get_node_and_resource(a->track_get_path(i), resource, leftover_path);
  240. 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
  241. ObjectID id = resource.is_valid() ? resource->get_instance_id() : child->get_instance_id();
  242. int bone_idx = -1;
  243. int blend_shape_idx = -1;
  244. #ifndef _3D_DISABLED
  245. if (a->track_get_path(i).get_subname_count() == 1 && Object::cast_to<Skeleton3D>(child)) {
  246. Skeleton3D *sk = Object::cast_to<Skeleton3D>(child);
  247. bone_idx = sk->find_bone(a->track_get_path(i).get_subname(0));
  248. if (bone_idx == -1) {
  249. continue;
  250. }
  251. }
  252. if (a->track_get_type(i) == Animation::TYPE_BLEND_SHAPE) {
  253. MeshInstance3D *mi_3d = Object::cast_to<MeshInstance3D>(child);
  254. if (!mi_3d) {
  255. continue;
  256. }
  257. if (a->track_get_path(i).get_subname_count() != 1) {
  258. continue;
  259. }
  260. blend_shape_idx = mi_3d->find_blend_shape_by_name(a->track_get_path(i).get_subname(0));
  261. if (blend_shape_idx == -1) {
  262. continue;
  263. }
  264. }
  265. #endif // _3D_DISABLED
  266. if (!child->is_connected("tree_exiting", callable_mp(this, &AnimationPlayer::_node_removed))) {
  267. child->connect("tree_exiting", callable_mp(this, &AnimationPlayer::_node_removed).bind(child), CONNECT_ONE_SHOT);
  268. }
  269. TrackNodeCacheKey key;
  270. key.id = id;
  271. key.bone_idx = bone_idx;
  272. key.blend_shape_idx = blend_shape_idx;
  273. if (!node_cache_map.has(key)) {
  274. node_cache_map[key] = TrackNodeCache();
  275. }
  276. TrackNodeCache *node_cache = &node_cache_map[key];
  277. p_anim->node_cache.write[i] = node_cache;
  278. node_cache->path = a->track_get_path(i);
  279. node_cache->node = child;
  280. node_cache->resource = resource;
  281. node_cache->node_2d = Object::cast_to<Node2D>(child);
  282. #ifndef _3D_DISABLED
  283. 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) {
  284. // special cases and caches for transform tracks
  285. if (node_cache->last_setup_pass != setup_pass) {
  286. node_cache->loc_used = false;
  287. node_cache->rot_used = false;
  288. node_cache->scale_used = false;
  289. }
  290. // cache node_3d
  291. node_cache->node_3d = Object::cast_to<Node3D>(child);
  292. // cache skeleton
  293. node_cache->skeleton = Object::cast_to<Skeleton3D>(child);
  294. if (node_cache->skeleton) {
  295. if (a->track_get_path(i).get_subname_count() == 1) {
  296. StringName bone_name = a->track_get_path(i).get_subname(0);
  297. node_cache->bone_idx = node_cache->skeleton->find_bone(bone_name);
  298. if (node_cache->bone_idx < 0) {
  299. // broken track (nonexistent bone)
  300. node_cache->skeleton = nullptr;
  301. node_cache->node_3d = nullptr;
  302. ERR_CONTINUE(node_cache->bone_idx < 0);
  303. }
  304. Transform3D rest = node_cache->skeleton->get_bone_rest(bone_idx);
  305. node_cache->init_loc = rest.origin;
  306. node_cache->init_rot = rest.basis.get_rotation_quaternion();
  307. node_cache->init_scale = rest.basis.get_scale();
  308. } else {
  309. // Not a skeleton, the node can be accessed with the node_3d member.
  310. node_cache->skeleton = nullptr;
  311. }
  312. }
  313. switch (a->track_get_type(i)) {
  314. case Animation::TYPE_POSITION_3D: {
  315. node_cache->loc_used = true;
  316. } break;
  317. case Animation::TYPE_ROTATION_3D: {
  318. node_cache->rot_used = true;
  319. } break;
  320. case Animation::TYPE_SCALE_3D: {
  321. node_cache->scale_used = true;
  322. } break;
  323. default: {
  324. }
  325. }
  326. }
  327. if (a->track_get_type(i) == Animation::TYPE_BLEND_SHAPE) {
  328. // special cases and caches for transform tracks
  329. node_cache->node_blend_shape = Object::cast_to<MeshInstance3D>(child);
  330. node_cache->blend_shape_idx = blend_shape_idx;
  331. }
  332. #endif // _3D_DISABLED
  333. if (a->track_get_type(i) == Animation::TYPE_VALUE) {
  334. if (!node_cache->property_anim.has(a->track_get_path(i).get_concatenated_subnames())) {
  335. TrackNodeCache::PropertyAnim pa;
  336. pa.subpath = leftover_path;
  337. pa.object = resource.is_valid() ? (Object *)resource.ptr() : (Object *)child;
  338. pa.special = SP_NONE;
  339. pa.owner = p_anim->node_cache[i];
  340. if (false && node_cache->node_2d) {
  341. if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_pos) {
  342. pa.special = SP_NODE2D_POS;
  343. } else if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_rot) {
  344. pa.special = SP_NODE2D_ROT;
  345. } else if (leftover_path.size() == 1 && leftover_path[0] == SceneStringNames::get_singleton()->transform_scale) {
  346. pa.special = SP_NODE2D_SCALE;
  347. }
  348. }
  349. node_cache->property_anim[a->track_get_path(i).get_concatenated_subnames()] = pa;
  350. }
  351. }
  352. if (a->track_get_type(i) == Animation::TYPE_BEZIER && leftover_path.size()) {
  353. if (!node_cache->bezier_anim.has(a->track_get_path(i).get_concatenated_subnames())) {
  354. TrackNodeCache::BezierAnim ba;
  355. ba.bezier_property = leftover_path;
  356. ba.object = resource.is_valid() ? (Object *)resource.ptr() : (Object *)child;
  357. ba.owner = p_anim->node_cache[i];
  358. node_cache->bezier_anim[a->track_get_path(i).get_concatenated_subnames()] = ba;
  359. }
  360. }
  361. node_cache->last_setup_pass = setup_pass;
  362. }
  363. }
  364. static void _call_object(Object *p_object, const StringName &p_method, const Vector<Variant> &p_params, bool p_deferred) {
  365. // Separate function to use alloca() more efficiently
  366. const Variant **argptrs = (const Variant **)alloca(sizeof(const Variant **) * p_params.size());
  367. const Variant *args = p_params.ptr();
  368. uint32_t argcount = p_params.size();
  369. for (uint32_t i = 0; i < argcount; i++) {
  370. argptrs[i] = &args[i];
  371. }
  372. if (p_deferred) {
  373. MessageQueue::get_singleton()->push_callp(p_object, p_method, argptrs, argcount);
  374. } else {
  375. Callable::CallError ce;
  376. p_object->callp(p_method, argptrs, argcount, ce);
  377. }
  378. }
  379. Variant AnimationPlayer::_post_process_key_value(const Ref<Animation> &p_anim, int p_track, Variant p_value, const Object *p_object, int p_object_idx) {
  380. switch (p_anim->track_get_type(p_track)) {
  381. #ifndef _3D_DISABLED
  382. case Animation::TYPE_POSITION_3D: {
  383. if (p_object_idx >= 0) {
  384. const Skeleton3D *skel = Object::cast_to<Skeleton3D>(p_object);
  385. return Vector3(p_value) * skel->get_motion_scale();
  386. }
  387. return p_value;
  388. } break;
  389. #endif // _3D_DISABLED
  390. default: {
  391. } break;
  392. }
  393. return p_value;
  394. }
  395. void AnimationPlayer::_animation_process_animation(AnimationData *p_anim, double p_prev_time, double p_time, double p_delta, float p_interp, bool p_is_current, bool p_seeked, bool p_started, Animation::LoopedFlag p_looped_flag) {
  396. _ensure_node_caches(p_anim);
  397. ERR_FAIL_COND(p_anim->node_cache.size() != p_anim->animation->get_track_count());
  398. Animation *a = p_anim->animation.operator->();
  399. bool can_call = is_inside_tree() && !Engine::get_singleton()->is_editor_hint();
  400. bool backward = signbit(p_delta);
  401. for (int i = 0; i < a->get_track_count(); i++) {
  402. // If an animation changes this animation (or it animates itself)
  403. // we need to recreate our animation cache
  404. if (p_anim->node_cache.size() != a->get_track_count()) {
  405. _ensure_node_caches(p_anim);
  406. }
  407. TrackNodeCache *nc = p_anim->node_cache[i];
  408. if (!nc) {
  409. continue; // no node cache for this track, skip it
  410. }
  411. if (!a->track_is_enabled(i)) {
  412. continue; // do nothing if the track is disabled
  413. }
  414. if (a->track_get_key_count(i) == 0) {
  415. continue; // do nothing if track is empty
  416. }
  417. switch (a->track_get_type(i)) {
  418. case Animation::TYPE_POSITION_3D: {
  419. #ifndef _3D_DISABLED
  420. if (!nc->node_3d) {
  421. continue;
  422. }
  423. Vector3 loc;
  424. Error err = a->position_track_interpolate(i, p_time, &loc);
  425. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  426. if (err != OK) {
  427. continue;
  428. }
  429. loc = _post_process_key_value(a, i, loc, nc->node_3d, nc->bone_idx);
  430. if (nc->accum_pass != accum_pass) {
  431. ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
  432. cache_update[cache_update_size++] = nc;
  433. nc->accum_pass = accum_pass;
  434. nc->loc_accum = loc;
  435. nc->rot_accum = nc->init_rot;
  436. nc->scale_accum = nc->init_scale;
  437. } else {
  438. nc->loc_accum = nc->loc_accum.lerp(loc, p_interp);
  439. }
  440. #endif // _3D_DISABLED
  441. } break;
  442. case Animation::TYPE_ROTATION_3D: {
  443. #ifndef _3D_DISABLED
  444. if (!nc->node_3d) {
  445. continue;
  446. }
  447. Quaternion rot;
  448. Error err = a->rotation_track_interpolate(i, p_time, &rot);
  449. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  450. if (err != OK) {
  451. continue;
  452. }
  453. rot = _post_process_key_value(a, i, rot, nc->node_3d, nc->bone_idx);
  454. if (nc->accum_pass != accum_pass) {
  455. ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
  456. cache_update[cache_update_size++] = nc;
  457. nc->accum_pass = accum_pass;
  458. nc->loc_accum = nc->init_loc;
  459. nc->rot_accum = rot;
  460. nc->scale_accum = nc->init_scale;
  461. } else {
  462. nc->rot_accum = nc->rot_accum.slerp(rot, p_interp);
  463. }
  464. #endif // _3D_DISABLED
  465. } break;
  466. case Animation::TYPE_SCALE_3D: {
  467. #ifndef _3D_DISABLED
  468. if (!nc->node_3d) {
  469. continue;
  470. }
  471. Vector3 scale;
  472. Error err = a->scale_track_interpolate(i, p_time, &scale);
  473. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  474. if (err != OK) {
  475. continue;
  476. }
  477. scale = _post_process_key_value(a, i, scale, nc->node_3d, nc->bone_idx);
  478. if (nc->accum_pass != accum_pass) {
  479. ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
  480. cache_update[cache_update_size++] = nc;
  481. nc->accum_pass = accum_pass;
  482. nc->loc_accum = nc->init_loc;
  483. nc->rot_accum = nc->init_rot;
  484. nc->scale_accum = scale;
  485. } else {
  486. nc->scale_accum = nc->scale_accum.lerp(scale, p_interp);
  487. }
  488. #endif // _3D_DISABLED
  489. } break;
  490. case Animation::TYPE_BLEND_SHAPE: {
  491. #ifndef _3D_DISABLED
  492. if (!nc->node_blend_shape) {
  493. continue;
  494. }
  495. float blend;
  496. Error err = a->blend_shape_track_interpolate(i, p_time, &blend);
  497. //ERR_CONTINUE(err!=OK); //used for testing, should be removed
  498. if (err != OK) {
  499. continue;
  500. }
  501. blend = _post_process_key_value(a, i, blend, nc->node_blend_shape, nc->blend_shape_idx);
  502. if (nc->accum_pass != accum_pass) {
  503. ERR_CONTINUE(cache_update_size >= NODE_CACHE_UPDATE_MAX);
  504. nc->accum_pass = accum_pass;
  505. cache_update[cache_update_size++] = nc;
  506. nc->blend_shape_accum = blend;
  507. } else {
  508. nc->blend_shape_accum = Math::lerp(nc->blend_shape_accum, blend, p_interp);
  509. }
  510. #endif // _3D_DISABLED
  511. } break;
  512. case Animation::TYPE_VALUE: {
  513. if (!nc->node) {
  514. continue;
  515. }
  516. //StringName property=a->track_get_path(i).get_property();
  517. HashMap<StringName, TrackNodeCache::PropertyAnim>::Iterator E = nc->property_anim.find(a->track_get_path(i).get_concatenated_subnames());
  518. ERR_CONTINUE(!E); //should it continue, or create a new one?
  519. TrackNodeCache::PropertyAnim *pa = &E->value;
  520. Animation::UpdateMode update_mode = a->value_track_get_update_mode(i);
  521. if (update_mode == Animation::UPDATE_CAPTURE) {
  522. if (p_started || pa->capture == Variant()) {
  523. pa->capture = pa->object->get_indexed(pa->subpath);
  524. }
  525. int key_count = a->track_get_key_count(i);
  526. if (key_count == 0) {
  527. continue; //eeh not worth it
  528. }
  529. double first_key_time = a->track_get_key_time(i, 0);
  530. double transition = 1.0;
  531. int first_key = 0;
  532. if (first_key_time == 0.0) {
  533. //ignore, use for transition
  534. if (key_count == 1) {
  535. continue; //with one key we can't do anything
  536. }
  537. transition = (double)a->track_get_key_transition(i, 0);
  538. first_key_time = a->track_get_key_time(i, 1);
  539. first_key = 1;
  540. }
  541. if (p_time < first_key_time) {
  542. double c = Math::ease(p_time / first_key_time, transition);
  543. Variant first_value = a->track_get_key_value(i, first_key);
  544. first_value = _post_process_key_value(a, i, first_value, nc->node);
  545. Variant interp_value = Animation::interpolate_variant(pa->capture, first_value, c);
  546. if (pa->accum_pass != accum_pass) {
  547. ERR_CONTINUE(cache_update_prop_size >= NODE_CACHE_UPDATE_MAX);
  548. cache_update_prop[cache_update_prop_size++] = pa;
  549. pa->value_accum = interp_value;
  550. pa->accum_pass = accum_pass;
  551. } else {
  552. pa->value_accum = Animation::interpolate_variant(pa->value_accum, interp_value, p_interp);
  553. }
  554. continue; //handled
  555. }
  556. }
  557. if (update_mode == Animation::UPDATE_CONTINUOUS || update_mode == Animation::UPDATE_CAPTURE) {
  558. Variant value = a->value_track_interpolate(i, p_time);
  559. if (value == Variant()) {
  560. continue;
  561. }
  562. value = _post_process_key_value(a, i, value, nc->node);
  563. if (pa->accum_pass != accum_pass) {
  564. ERR_CONTINUE(cache_update_prop_size >= NODE_CACHE_UPDATE_MAX);
  565. cache_update_prop[cache_update_prop_size++] = pa;
  566. pa->value_accum = value;
  567. pa->accum_pass = accum_pass;
  568. } else {
  569. pa->value_accum = Animation::interpolate_variant(pa->value_accum, value, p_interp);
  570. }
  571. } else {
  572. List<int> indices;
  573. if (p_seeked) {
  574. int found_key = a->track_find_key(i, p_time);
  575. if (found_key >= 0) {
  576. indices.push_back(found_key);
  577. }
  578. } else {
  579. if (p_started) {
  580. int first_key = a->track_find_key(i, p_prev_time, Animation::FIND_MODE_EXACT);
  581. if (first_key >= 0) {
  582. indices.push_back(first_key);
  583. }
  584. }
  585. a->track_get_key_indices_in_range(i, p_time, p_delta, &indices, p_looped_flag);
  586. }
  587. for (int &F : indices) {
  588. Variant value = a->track_get_key_value(i, F);
  589. value = _post_process_key_value(a, i, value, nc->node);
  590. switch (pa->special) {
  591. case SP_NONE: {
  592. bool valid;
  593. pa->object->set_indexed(pa->subpath, value, &valid); //you are not speshul
  594. #ifdef DEBUG_ENABLED
  595. if (!valid) {
  596. ERR_PRINT("Failed setting track value '" + String(pa->owner->path) + "'. Check if the property exists or the type of key is valid. Animation '" + a->get_name() + "' at node '" + get_path() + "'.");
  597. }
  598. #endif
  599. } break;
  600. case SP_NODE2D_POS: {
  601. #ifdef DEBUG_ENABLED
  602. if (value.get_type() != Variant::VECTOR2) {
  603. 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() + "'.");
  604. }
  605. #endif
  606. static_cast<Node2D *>(pa->object)->set_position(value);
  607. } break;
  608. case SP_NODE2D_ROT: {
  609. #ifdef DEBUG_ENABLED
  610. if (value.is_num()) {
  611. 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() + "'.");
  612. }
  613. #endif
  614. static_cast<Node2D *>(pa->object)->set_rotation((double)value);
  615. } break;
  616. case SP_NODE2D_SCALE: {
  617. #ifdef DEBUG_ENABLED
  618. if (value.get_type() != Variant::VECTOR2) {
  619. 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() + "'.");
  620. }
  621. #endif
  622. static_cast<Node2D *>(pa->object)->set_scale(value);
  623. } break;
  624. }
  625. }
  626. }
  627. } break;
  628. case Animation::TYPE_METHOD: {
  629. if (!nc->node || is_stopping) {
  630. continue;
  631. }
  632. if (!p_is_current) {
  633. break;
  634. }
  635. List<int> indices;
  636. if (p_seeked) {
  637. int found_key = a->track_find_key(i, p_time);
  638. if (found_key >= 0) {
  639. indices.push_back(found_key);
  640. }
  641. } else {
  642. if (p_started) {
  643. int first_key = a->track_find_key(i, p_prev_time, Animation::FIND_MODE_EXACT);
  644. if (first_key >= 0) {
  645. indices.push_back(first_key);
  646. }
  647. }
  648. a->track_get_key_indices_in_range(i, p_time, p_delta, &indices, p_looped_flag);
  649. }
  650. for (int &E : indices) {
  651. StringName method = a->method_track_get_name(i, E);
  652. Vector<Variant> params = a->method_track_get_params(i, E);
  653. #ifdef DEBUG_ENABLED
  654. if (!nc->node->has_method(method)) {
  655. ERR_PRINT("Invalid method call '" + method + "'. '" + a->get_name() + "' at node '" + get_path() + "'.");
  656. }
  657. #endif
  658. if (can_call) {
  659. _call_object(nc->node, method, params, method_call_mode == ANIMATION_METHOD_CALL_DEFERRED);
  660. }
  661. }
  662. } break;
  663. case Animation::TYPE_BEZIER: {
  664. if (!nc->node) {
  665. continue;
  666. }
  667. HashMap<StringName, TrackNodeCache::BezierAnim>::Iterator E = nc->bezier_anim.find(a->track_get_path(i).get_concatenated_subnames());
  668. ERR_CONTINUE(!E); //should it continue, or create a new one?
  669. TrackNodeCache::BezierAnim *ba = &E->value;
  670. real_t bezier = a->bezier_track_interpolate(i, p_time);
  671. bezier = _post_process_key_value(a, i, bezier, nc->node);
  672. if (ba->accum_pass != accum_pass) {
  673. ERR_CONTINUE(cache_update_bezier_size >= NODE_CACHE_UPDATE_MAX);
  674. cache_update_bezier[cache_update_bezier_size++] = ba;
  675. ba->bezier_accum = bezier;
  676. ba->accum_pass = accum_pass;
  677. } else {
  678. ba->bezier_accum = Math::lerp(ba->bezier_accum, (float)bezier, p_interp);
  679. }
  680. } break;
  681. case Animation::TYPE_AUDIO: {
  682. if (!nc->node || is_stopping) {
  683. continue;
  684. }
  685. if (p_seeked) {
  686. //find whatever should be playing
  687. int idx = a->track_find_key(i, p_time);
  688. if (idx < 0) {
  689. continue;
  690. }
  691. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  692. if (!stream.is_valid()) {
  693. nc->node->call(SNAME("stop"));
  694. nc->audio_playing = false;
  695. playing_caches.erase(nc);
  696. } else {
  697. float start_ofs = a->audio_track_get_key_start_offset(i, idx);
  698. start_ofs += p_time - a->track_get_key_time(i, idx);
  699. float end_ofs = a->audio_track_get_key_end_offset(i, idx);
  700. float len = stream->get_length();
  701. if (start_ofs > len - end_ofs) {
  702. nc->node->call(SNAME("stop"));
  703. nc->audio_playing = false;
  704. playing_caches.erase(nc);
  705. continue;
  706. }
  707. nc->node->call(SNAME("set_stream"), stream);
  708. nc->node->call(SNAME("play"), start_ofs);
  709. nc->audio_playing = true;
  710. playing_caches.insert(nc);
  711. if (len && end_ofs > 0) { //force an end at a time
  712. nc->audio_len = len - start_ofs - end_ofs;
  713. } else {
  714. nc->audio_len = 0;
  715. }
  716. nc->audio_start = p_time;
  717. }
  718. } else {
  719. //find stuff to play
  720. List<int> to_play;
  721. if (p_started) {
  722. int first_key = a->track_find_key(i, p_prev_time, Animation::FIND_MODE_EXACT);
  723. if (first_key >= 0) {
  724. to_play.push_back(first_key);
  725. }
  726. }
  727. a->track_get_key_indices_in_range(i, p_time, p_delta, &to_play, p_looped_flag);
  728. if (to_play.size()) {
  729. int idx = to_play.back()->get();
  730. Ref<AudioStream> stream = a->audio_track_get_key_stream(i, idx);
  731. if (!stream.is_valid()) {
  732. nc->node->call(SNAME("stop"));
  733. nc->audio_playing = false;
  734. playing_caches.erase(nc);
  735. } else {
  736. float start_ofs = a->audio_track_get_key_start_offset(i, idx);
  737. float end_ofs = a->audio_track_get_key_end_offset(i, idx);
  738. float len = stream->get_length();
  739. nc->node->call(SNAME("set_stream"), stream);
  740. nc->node->call(SNAME("play"), start_ofs);
  741. nc->audio_playing = true;
  742. playing_caches.insert(nc);
  743. if (len && end_ofs > 0) { //force an end at a time
  744. nc->audio_len = len - start_ofs - end_ofs;
  745. } else {
  746. nc->audio_len = 0;
  747. }
  748. nc->audio_start = p_time;
  749. }
  750. } else if (nc->audio_playing) {
  751. bool loop = a->get_loop_mode() != Animation::LOOP_NONE;
  752. bool stop = false;
  753. if (!loop) {
  754. if ((p_time < nc->audio_start && !backward) || (p_time > nc->audio_start && backward)) {
  755. stop = true;
  756. }
  757. } else if (nc->audio_len > 0) {
  758. float len = nc->audio_start > p_time ? (a->get_length() - nc->audio_start) + p_time : p_time - nc->audio_start;
  759. if (len > nc->audio_len) {
  760. stop = true;
  761. }
  762. }
  763. if (stop) {
  764. //time to stop
  765. nc->node->call(SNAME("stop"));
  766. nc->audio_playing = false;
  767. playing_caches.erase(nc);
  768. }
  769. }
  770. }
  771. } break;
  772. case Animation::TYPE_ANIMATION: {
  773. if (is_stopping) {
  774. continue;
  775. }
  776. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(nc->node);
  777. if (!player) {
  778. continue;
  779. }
  780. if (p_seeked) {
  781. //seek
  782. int idx = a->track_find_key(i, p_time);
  783. if (idx < 0) {
  784. continue;
  785. }
  786. double pos = a->track_get_key_time(i, idx);
  787. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  788. if (String(anim_name) == "[stop]" || !player->has_animation(anim_name)) {
  789. continue;
  790. }
  791. Ref<Animation> anim = player->get_animation(anim_name);
  792. double at_anim_pos = 0.0;
  793. switch (anim->get_loop_mode()) {
  794. case Animation::LOOP_NONE: {
  795. at_anim_pos = MIN((double)anim->get_length(), p_time - pos); //seek to end
  796. } break;
  797. case Animation::LOOP_LINEAR: {
  798. at_anim_pos = Math::fposmod(p_time - pos, (double)anim->get_length()); //seek to loop
  799. } break;
  800. case Animation::LOOP_PINGPONG: {
  801. at_anim_pos = Math::pingpong(p_time - pos, (double)anim->get_length());
  802. } break;
  803. default:
  804. break;
  805. }
  806. if (player->is_playing()) {
  807. player->seek(at_anim_pos);
  808. player->play(anim_name);
  809. nc->animation_playing = true;
  810. playing_caches.insert(nc);
  811. } else {
  812. player->set_assigned_animation(anim_name);
  813. player->seek(at_anim_pos, true);
  814. }
  815. } else {
  816. //find stuff to play
  817. List<int> to_play;
  818. if (p_started) {
  819. int first_key = a->track_find_key(i, p_prev_time, Animation::FIND_MODE_EXACT);
  820. if (first_key >= 0) {
  821. to_play.push_back(first_key);
  822. }
  823. }
  824. a->track_get_key_indices_in_range(i, p_time, p_delta, &to_play, p_looped_flag);
  825. if (to_play.size()) {
  826. int idx = to_play.back()->get();
  827. StringName anim_name = a->animation_track_get_key_animation(i, idx);
  828. if (String(anim_name) == "[stop]" || !player->has_animation(anim_name)) {
  829. if (playing_caches.has(nc)) {
  830. playing_caches.erase(nc);
  831. player->stop();
  832. nc->animation_playing = false;
  833. }
  834. } else {
  835. player->seek(0.0);
  836. player->play(anim_name);
  837. nc->animation_playing = true;
  838. playing_caches.insert(nc);
  839. }
  840. }
  841. }
  842. } break;
  843. }
  844. }
  845. }
  846. void AnimationPlayer::_animation_process_data(PlaybackData &cd, double p_delta, float p_blend, bool p_seeked, bool p_started) {
  847. double delta = p_delta * speed_scale * cd.speed_scale;
  848. double next_pos = cd.pos + delta;
  849. bool backwards = signbit(delta); // Negative zero means playing backwards too.
  850. real_t len = cd.from->animation->get_length();
  851. Animation::LoopedFlag looped_flag = Animation::LOOPED_FLAG_NONE;
  852. switch (cd.from->animation->get_loop_mode()) {
  853. case Animation::LOOP_NONE: {
  854. if (next_pos < 0) {
  855. next_pos = 0;
  856. } else if (next_pos > len) {
  857. next_pos = len;
  858. }
  859. delta = next_pos - cd.pos; // Fix delta (after determination of backwards because negative zero is lost here).
  860. } break;
  861. case Animation::LOOP_LINEAR: {
  862. if (next_pos < 0 && cd.pos >= 0) {
  863. looped_flag = Animation::LOOPED_FLAG_START;
  864. }
  865. if (next_pos > len && cd.pos <= len) {
  866. looped_flag = Animation::LOOPED_FLAG_END;
  867. }
  868. next_pos = Math::fposmod(next_pos, (double)len);
  869. } break;
  870. case Animation::LOOP_PINGPONG: {
  871. if (next_pos < 0 && cd.pos >= 0) {
  872. cd.speed_scale *= -1.0;
  873. looped_flag = Animation::LOOPED_FLAG_START;
  874. }
  875. if (next_pos > len && cd.pos <= len) {
  876. cd.speed_scale *= -1.0;
  877. looped_flag = Animation::LOOPED_FLAG_END;
  878. }
  879. next_pos = Math::pingpong(next_pos, (double)len);
  880. } break;
  881. default:
  882. break;
  883. }
  884. double prev_pos = cd.pos; // The animation may be changed during process, so it is safer that the state is changed before process.
  885. cd.pos = next_pos;
  886. AnimationData *prev_from = cd.from;
  887. _animation_process_animation(cd.from, prev_pos, cd.pos, delta, p_blend, &cd == &playback.current, p_seeked, p_started, looped_flag);
  888. // End detection.
  889. if (cd.from->animation->get_loop_mode() == Animation::LOOP_NONE) {
  890. if (prev_from != playback.current.from) {
  891. return; // Animation has been changed in the process (may be caused by method track), abort process.
  892. }
  893. if (!backwards && prev_pos <= len && next_pos == len) {
  894. // Playback finished.
  895. end_reached = true;
  896. end_notify = prev_pos < len; // Notify only if not already at the end.
  897. }
  898. if (backwards && prev_pos >= 0 && next_pos == 0) {
  899. // Playback finished.
  900. end_reached = true;
  901. end_notify = prev_pos > 0; // Notify only if not already at the beginning.
  902. }
  903. }
  904. }
  905. void AnimationPlayer::_animation_process2(double p_delta, bool p_started) {
  906. Playback &c = playback;
  907. accum_pass++;
  908. bool seeked = c.seeked; // The animation may be changed during process, so it is safer that the state is changed before process.
  909. if (p_delta != 0) {
  910. c.seeked = false;
  911. }
  912. float blend = 1.0; // First animation we play at 100% blend
  913. List<Blend>::Element *next = NULL;
  914. for (List<Blend>::Element *E = c.blend.front(); E; E = next) {
  915. Blend &b = E->get();
  916. // Note: There may be issues if an animation event triggers an animation change while this blend is active,
  917. // so it is best to use "deferred" calls instead of "immediate" for animation events that can trigger new animations.
  918. _animation_process_data(b.data, p_delta, blend, false, false);
  919. blend = 1.0 - b.blend_left / b.blend_time; // This is how much to blend the NEXT animation
  920. b.blend_left -= Math::absf(speed_scale * p_delta);
  921. next = E->next();
  922. if (b.blend_left < 0) {
  923. // If the blend of this has finished, we need to remove ALL the previous blends
  924. List<Blend>::Element *prev;
  925. while (E) {
  926. prev = E->prev();
  927. c.blend.erase(E);
  928. E = prev;
  929. }
  930. }
  931. }
  932. _animation_process_data(c.current, p_delta, blend, seeked, p_started);
  933. }
  934. void AnimationPlayer::_animation_update_transforms() {
  935. {
  936. Transform3D t;
  937. for (int i = 0; i < cache_update_size; i++) {
  938. TrackNodeCache *nc = cache_update[i];
  939. ERR_CONTINUE(nc->accum_pass != accum_pass);
  940. #ifndef _3D_DISABLED
  941. if (nc->skeleton && nc->bone_idx >= 0) {
  942. if (nc->loc_used) {
  943. nc->skeleton->set_bone_pose_position(nc->bone_idx, nc->loc_accum);
  944. }
  945. if (nc->rot_used) {
  946. nc->skeleton->set_bone_pose_rotation(nc->bone_idx, nc->rot_accum);
  947. }
  948. if (nc->scale_used) {
  949. nc->skeleton->set_bone_pose_scale(nc->bone_idx, nc->scale_accum);
  950. }
  951. } else if (nc->node_blend_shape) {
  952. nc->node_blend_shape->set_blend_shape_value(nc->blend_shape_idx, nc->blend_shape_accum);
  953. } else if (nc->node_3d) {
  954. if (nc->loc_used) {
  955. nc->node_3d->set_position(nc->loc_accum);
  956. }
  957. if (nc->rot_used) {
  958. nc->node_3d->set_rotation(nc->rot_accum.get_euler());
  959. }
  960. if (nc->scale_used) {
  961. nc->node_3d->set_scale(nc->scale_accum);
  962. }
  963. }
  964. #endif // _3D_DISABLED
  965. }
  966. }
  967. for (int i = 0; i < cache_update_prop_size; i++) {
  968. TrackNodeCache::PropertyAnim *pa = cache_update_prop[i];
  969. ERR_CONTINUE(pa->accum_pass != accum_pass);
  970. switch (pa->special) {
  971. case SP_NONE: {
  972. bool valid;
  973. pa->object->set_indexed(pa->subpath, pa->value_accum, &valid); //you are not speshul
  974. #ifdef DEBUG_ENABLED
  975. if (!valid) {
  976. // Get subpath as string for printing the error
  977. // Cannot use `String::join(Vector<String>)` because this is a vector of StringName
  978. String key_debug;
  979. if (pa->subpath.size() > 0) {
  980. key_debug = pa->subpath[0];
  981. for (int subpath_index = 1; subpath_index < pa->subpath.size(); ++subpath_index) {
  982. key_debug += ".";
  983. key_debug += pa->subpath[subpath_index];
  984. }
  985. }
  986. ERR_PRINT("Failed setting key '" + key_debug +
  987. "' at time " + rtos(playback.current.pos) +
  988. " in Animation '" + get_current_animation() +
  989. "' at Node '" + get_path() +
  990. "', Track '" + String(pa->owner->path) +
  991. "'. Check if the property exists or the type of key is right for the property.");
  992. }
  993. #endif
  994. } break;
  995. case SP_NODE2D_POS: {
  996. #ifdef DEBUG_ENABLED
  997. if (pa->value_accum.get_type() != Variant::VECTOR2) {
  998. 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()");
  999. }
  1000. #endif
  1001. static_cast<Node2D *>(pa->object)->set_position(pa->value_accum);
  1002. } break;
  1003. case SP_NODE2D_ROT: {
  1004. #ifdef DEBUG_ENABLED
  1005. if (pa->value_accum.is_num()) {
  1006. 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");
  1007. }
  1008. #endif
  1009. static_cast<Node2D *>(pa->object)->set_rotation(Math::deg_to_rad((double)pa->value_accum));
  1010. } break;
  1011. case SP_NODE2D_SCALE: {
  1012. #ifdef DEBUG_ENABLED
  1013. if (pa->value_accum.get_type() != Variant::VECTOR2) {
  1014. 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()");
  1015. }
  1016. #endif
  1017. static_cast<Node2D *>(pa->object)->set_scale(pa->value_accum);
  1018. } break;
  1019. }
  1020. }
  1021. for (int i = 0; i < cache_update_bezier_size; i++) {
  1022. TrackNodeCache::BezierAnim *ba = cache_update_bezier[i];
  1023. ERR_CONTINUE(ba->accum_pass != accum_pass);
  1024. ba->object->set_indexed(ba->bezier_property, ba->bezier_accum);
  1025. }
  1026. }
  1027. void AnimationPlayer::_animation_process(double p_delta) {
  1028. if (playback.current.from) {
  1029. end_reached = false;
  1030. end_notify = false;
  1031. bool started = playback.started; // The animation may be changed during process, so it is safer that the state is changed before process.
  1032. if (playback.started) {
  1033. playback.started = false;
  1034. }
  1035. cache_update_size = 0;
  1036. cache_update_prop_size = 0;
  1037. cache_update_bezier_size = 0;
  1038. AnimationData *prev_from = playback.current.from;
  1039. _animation_process2(p_delta, started);
  1040. if (prev_from != playback.current.from) {
  1041. return; // Animation has been changed in the process (may be caused by method track), abort process.
  1042. }
  1043. _animation_update_transforms();
  1044. if (end_reached) {
  1045. if (queued.size()) {
  1046. String old = playback.assigned;
  1047. play(queued.front()->get());
  1048. String new_name = playback.assigned;
  1049. queued.pop_front();
  1050. if (end_notify) {
  1051. emit_signal(SceneStringNames::get_singleton()->animation_changed, old, new_name);
  1052. }
  1053. } else {
  1054. playing = false;
  1055. _set_process(false);
  1056. if (end_notify) {
  1057. emit_signal(SceneStringNames::get_singleton()->animation_finished, playback.assigned);
  1058. if (movie_quit_on_finish && OS::get_singleton()->has_feature("movie")) {
  1059. print_line(vformat("Movie Maker mode is enabled. Quitting on animation finish as requested by: %s", get_path()));
  1060. get_tree()->quit();
  1061. }
  1062. }
  1063. }
  1064. end_reached = false;
  1065. }
  1066. } else {
  1067. _set_process(false);
  1068. }
  1069. }
  1070. void AnimationPlayer::_animation_set_cache_update() {
  1071. // Relatively fast function to update all animations.
  1072. animation_set_update_pass++;
  1073. bool clear_cache_needed = false;
  1074. // Update changed and add otherwise
  1075. for (const AnimationLibraryData &lib : animation_libraries) {
  1076. for (const KeyValue<StringName, Ref<Animation>> &K : lib.library->animations) {
  1077. StringName key = lib.name == StringName() ? K.key : StringName(String(lib.name) + "/" + String(K.key));
  1078. if (!animation_set.has(key)) {
  1079. AnimationData ad;
  1080. ad.animation = K.value;
  1081. ad.animation_library = lib.name;
  1082. ad.name = key;
  1083. ad.last_update = animation_set_update_pass;
  1084. animation_set.insert(ad.name, ad);
  1085. } else {
  1086. AnimationData &ad = animation_set[key];
  1087. if (ad.last_update != animation_set_update_pass) {
  1088. // Was not updated, update. If the animation is duplicated, the second one will be ignored.
  1089. if (ad.animation != K.value || ad.animation_library != lib.name) {
  1090. // Animation changed, update and clear caches.
  1091. clear_cache_needed = true;
  1092. ad.animation = K.value;
  1093. ad.animation_library = lib.name;
  1094. }
  1095. ad.last_update = animation_set_update_pass;
  1096. }
  1097. }
  1098. }
  1099. }
  1100. // Check removed
  1101. List<StringName> to_erase;
  1102. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  1103. if (E.value.last_update != animation_set_update_pass) {
  1104. // Was not updated, must be erased
  1105. to_erase.push_back(E.key);
  1106. clear_cache_needed = true;
  1107. }
  1108. }
  1109. while (to_erase.size()) {
  1110. animation_set.erase(to_erase.front()->get());
  1111. to_erase.pop_front();
  1112. }
  1113. if (clear_cache_needed) {
  1114. // If something was modified or removed, caches need to be cleared
  1115. clear_caches();
  1116. }
  1117. emit_signal(SNAME("animation_list_changed"));
  1118. }
  1119. void AnimationPlayer::_animation_added(const StringName &p_name, const StringName &p_library) {
  1120. _animation_set_cache_update();
  1121. }
  1122. void AnimationPlayer::_animation_removed(const StringName &p_name, const StringName &p_library) {
  1123. StringName name = p_library == StringName() ? p_name : StringName(String(p_library) + "/" + String(p_name));
  1124. if (!animation_set.has(name)) {
  1125. return; // No need to update because not the one from the library being used.
  1126. }
  1127. _animation_set_cache_update();
  1128. // Erase blends if needed
  1129. List<BlendKey> to_erase;
  1130. for (const KeyValue<BlendKey, double> &E : blend_times) {
  1131. BlendKey bk = E.key;
  1132. if (bk.from == name || bk.to == name) {
  1133. to_erase.push_back(bk);
  1134. }
  1135. }
  1136. while (to_erase.size()) {
  1137. blend_times.erase(to_erase.front()->get());
  1138. to_erase.pop_front();
  1139. }
  1140. }
  1141. void AnimationPlayer::_rename_animation(const StringName &p_from_name, const StringName &p_to_name) {
  1142. // Rename autoplay or blends if needed.
  1143. List<BlendKey> to_erase;
  1144. HashMap<BlendKey, double, BlendKey> to_insert;
  1145. for (const KeyValue<BlendKey, double> &E : blend_times) {
  1146. BlendKey bk = E.key;
  1147. BlendKey new_bk = bk;
  1148. bool erase = false;
  1149. if (bk.from == p_from_name) {
  1150. new_bk.from = p_to_name;
  1151. erase = true;
  1152. }
  1153. if (bk.to == p_from_name) {
  1154. new_bk.to = p_to_name;
  1155. erase = true;
  1156. }
  1157. if (erase) {
  1158. to_erase.push_back(bk);
  1159. to_insert[new_bk] = E.value;
  1160. }
  1161. }
  1162. while (to_erase.size()) {
  1163. blend_times.erase(to_erase.front()->get());
  1164. to_erase.pop_front();
  1165. }
  1166. while (to_insert.size()) {
  1167. blend_times[to_insert.begin()->key] = to_insert.begin()->value;
  1168. to_insert.remove(to_insert.begin());
  1169. }
  1170. if (autoplay == p_from_name) {
  1171. autoplay = p_to_name;
  1172. }
  1173. }
  1174. void AnimationPlayer::_animation_renamed(const StringName &p_name, const StringName &p_to_name, const StringName &p_library) {
  1175. StringName from_name = p_library == StringName() ? p_name : StringName(String(p_library) + "/" + String(p_name));
  1176. StringName to_name = p_library == StringName() ? p_to_name : StringName(String(p_library) + "/" + String(p_to_name));
  1177. if (!animation_set.has(from_name)) {
  1178. return; // No need to update because not the one from the library being used.
  1179. }
  1180. _animation_set_cache_update();
  1181. _rename_animation(from_name, to_name);
  1182. }
  1183. Error AnimationPlayer::add_animation_library(const StringName &p_name, const Ref<AnimationLibrary> &p_animation_library) {
  1184. ERR_FAIL_COND_V(p_animation_library.is_null(), ERR_INVALID_PARAMETER);
  1185. #ifdef DEBUG_ENABLED
  1186. ERR_FAIL_COND_V_MSG(String(p_name).contains("/") || String(p_name).contains(":") || String(p_name).contains(",") || String(p_name).contains("["), ERR_INVALID_PARAMETER, "Invalid animation name: " + String(p_name) + ".");
  1187. #endif
  1188. int insert_pos = 0;
  1189. for (const AnimationLibraryData &lib : animation_libraries) {
  1190. ERR_FAIL_COND_V_MSG(lib.name == p_name, ERR_ALREADY_EXISTS, "Can't add animation library twice with name: " + String(p_name));
  1191. ERR_FAIL_COND_V_MSG(lib.library == p_animation_library, ERR_ALREADY_EXISTS, "Can't add animation library twice (adding as '" + p_name.operator String() + "', exists as '" + lib.name.operator String() + "'.");
  1192. if (lib.name.operator String() >= p_name.operator String()) {
  1193. break;
  1194. }
  1195. insert_pos++;
  1196. }
  1197. AnimationLibraryData ald;
  1198. ald.name = p_name;
  1199. ald.library = p_animation_library;
  1200. animation_libraries.insert(insert_pos, ald);
  1201. ald.library->connect(SNAME("animation_added"), callable_mp(this, &AnimationPlayer::_animation_added).bind(p_name));
  1202. ald.library->connect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_removed).bind(p_name));
  1203. ald.library->connect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed).bind(p_name));
  1204. ald.library->connect(SNAME("animation_changed"), callable_mp(this, &AnimationPlayer::_animation_changed));
  1205. _animation_set_cache_update();
  1206. notify_property_list_changed();
  1207. return OK;
  1208. }
  1209. void AnimationPlayer::remove_animation_library(const StringName &p_name) {
  1210. int at_pos = -1;
  1211. for (uint32_t i = 0; i < animation_libraries.size(); i++) {
  1212. if (animation_libraries[i].name == p_name) {
  1213. at_pos = i;
  1214. break;
  1215. }
  1216. }
  1217. ERR_FAIL_COND(at_pos == -1);
  1218. animation_libraries[at_pos].library->disconnect(SNAME("animation_added"), callable_mp(this, &AnimationPlayer::_animation_added));
  1219. animation_libraries[at_pos].library->disconnect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_removed));
  1220. animation_libraries[at_pos].library->disconnect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed));
  1221. animation_libraries[at_pos].library->disconnect(SNAME("animation_changed"), callable_mp(this, &AnimationPlayer::_animation_changed));
  1222. stop();
  1223. animation_libraries.remove_at(at_pos);
  1224. _animation_set_cache_update();
  1225. notify_property_list_changed();
  1226. }
  1227. void AnimationPlayer::rename_animation_library(const StringName &p_name, const StringName &p_new_name) {
  1228. if (p_name == p_new_name) {
  1229. return;
  1230. }
  1231. #ifdef DEBUG_ENABLED
  1232. ERR_FAIL_COND_MSG(String(p_new_name).contains("/") || String(p_new_name).contains(":") || String(p_new_name).contains(",") || String(p_new_name).contains("["), "Invalid animation library name: " + String(p_new_name) + ".");
  1233. #endif
  1234. bool found = false;
  1235. for (AnimationLibraryData &lib : animation_libraries) {
  1236. ERR_FAIL_COND_MSG(lib.name == p_new_name, "Can't rename animation library to another existing name: " + String(p_new_name));
  1237. if (lib.name == p_name) {
  1238. found = true;
  1239. lib.name = p_new_name;
  1240. // rename connections
  1241. lib.library->disconnect(SNAME("animation_added"), callable_mp(this, &AnimationPlayer::_animation_added));
  1242. lib.library->disconnect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_removed));
  1243. lib.library->disconnect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed));
  1244. lib.library->connect(SNAME("animation_added"), callable_mp(this, &AnimationPlayer::_animation_added).bind(p_new_name));
  1245. lib.library->connect(SNAME("animation_removed"), callable_mp(this, &AnimationPlayer::_animation_removed).bind(p_new_name));
  1246. lib.library->connect(SNAME("animation_renamed"), callable_mp(this, &AnimationPlayer::_animation_renamed).bind(p_new_name));
  1247. for (const KeyValue<StringName, Ref<Animation>> &K : lib.library->animations) {
  1248. StringName old_name = p_name == StringName() ? K.key : StringName(String(p_name) + "/" + String(K.key));
  1249. StringName new_name = p_new_name == StringName() ? K.key : StringName(String(p_new_name) + "/" + String(K.key));
  1250. _rename_animation(old_name, new_name);
  1251. }
  1252. }
  1253. }
  1254. ERR_FAIL_COND(!found);
  1255. stop();
  1256. animation_libraries.sort(); // Must keep alphabetical order.
  1257. _animation_set_cache_update(); // Update cache.
  1258. notify_property_list_changed();
  1259. }
  1260. bool AnimationPlayer::has_animation_library(const StringName &p_name) const {
  1261. for (const AnimationLibraryData &lib : animation_libraries) {
  1262. if (lib.name == p_name) {
  1263. return true;
  1264. }
  1265. }
  1266. return false;
  1267. }
  1268. Ref<AnimationLibrary> AnimationPlayer::get_animation_library(const StringName &p_name) const {
  1269. for (const AnimationLibraryData &lib : animation_libraries) {
  1270. if (lib.name == p_name) {
  1271. return lib.library;
  1272. }
  1273. }
  1274. ERR_FAIL_V(Ref<AnimationLibrary>());
  1275. }
  1276. TypedArray<StringName> AnimationPlayer::_get_animation_library_list() const {
  1277. TypedArray<StringName> ret;
  1278. for (const AnimationLibraryData &lib : animation_libraries) {
  1279. ret.push_back(lib.name);
  1280. }
  1281. return ret;
  1282. }
  1283. void AnimationPlayer::get_animation_library_list(List<StringName> *p_libraries) const {
  1284. for (const AnimationLibraryData &lib : animation_libraries) {
  1285. p_libraries->push_back(lib.name);
  1286. }
  1287. }
  1288. bool AnimationPlayer::has_animation(const StringName &p_name) const {
  1289. return animation_set.has(p_name);
  1290. }
  1291. Ref<Animation> AnimationPlayer::get_animation(const StringName &p_name) const {
  1292. ERR_FAIL_COND_V_MSG(!animation_set.has(p_name), Ref<Animation>(), vformat("Animation not found: \"%s\".", p_name));
  1293. const AnimationData &anim_data = animation_set[p_name];
  1294. return anim_data.animation;
  1295. }
  1296. void AnimationPlayer::get_animation_list(List<StringName> *p_animations) const {
  1297. List<String> anims;
  1298. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  1299. anims.push_back(E.key);
  1300. }
  1301. anims.sort();
  1302. for (const String &E : anims) {
  1303. p_animations->push_back(E);
  1304. }
  1305. }
  1306. void AnimationPlayer::set_blend_time(const StringName &p_animation1, const StringName &p_animation2, double p_time) {
  1307. ERR_FAIL_COND_MSG(!animation_set.has(p_animation1), vformat("Animation not found: %s.", p_animation1));
  1308. ERR_FAIL_COND_MSG(!animation_set.has(p_animation2), vformat("Animation not found: %s.", p_animation2));
  1309. ERR_FAIL_COND_MSG(p_time < 0, "Blend time cannot be smaller than 0.");
  1310. BlendKey bk;
  1311. bk.from = p_animation1;
  1312. bk.to = p_animation2;
  1313. if (p_time == 0) {
  1314. blend_times.erase(bk);
  1315. } else {
  1316. blend_times[bk] = p_time;
  1317. }
  1318. }
  1319. double AnimationPlayer::get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const {
  1320. BlendKey bk;
  1321. bk.from = p_animation1;
  1322. bk.to = p_animation2;
  1323. if (blend_times.has(bk)) {
  1324. return blend_times[bk];
  1325. } else {
  1326. return 0;
  1327. }
  1328. }
  1329. void AnimationPlayer::queue(const StringName &p_name) {
  1330. if (!is_playing()) {
  1331. play(p_name);
  1332. } else {
  1333. queued.push_back(p_name);
  1334. }
  1335. }
  1336. Vector<String> AnimationPlayer::get_queue() {
  1337. Vector<String> ret;
  1338. for (const StringName &E : queued) {
  1339. ret.push_back(E);
  1340. }
  1341. return ret;
  1342. }
  1343. void AnimationPlayer::clear_queue() {
  1344. queued.clear();
  1345. }
  1346. void AnimationPlayer::play_backwards(const StringName &p_name, double p_custom_blend) {
  1347. play(p_name, p_custom_blend, -1, true);
  1348. }
  1349. void AnimationPlayer::play(const StringName &p_name, double p_custom_blend, float p_custom_scale, bool p_from_end) {
  1350. StringName name = p_name;
  1351. if (String(name) == "") {
  1352. name = playback.assigned;
  1353. }
  1354. ERR_FAIL_COND_MSG(!animation_set.has(name), vformat("Animation not found: %s.", name));
  1355. Playback &c = playback;
  1356. if (c.current.from) {
  1357. double blend_time = 0.0;
  1358. // find if it can blend
  1359. BlendKey bk;
  1360. bk.from = c.current.from->name;
  1361. bk.to = name;
  1362. if (p_custom_blend >= 0) {
  1363. blend_time = p_custom_blend;
  1364. } else if (blend_times.has(bk)) {
  1365. blend_time = blend_times[bk];
  1366. } else {
  1367. bk.from = "*";
  1368. if (blend_times.has(bk)) {
  1369. blend_time = blend_times[bk];
  1370. } else {
  1371. bk.from = c.current.from->name;
  1372. bk.to = "*";
  1373. if (blend_times.has(bk)) {
  1374. blend_time = blend_times[bk];
  1375. }
  1376. }
  1377. }
  1378. if (p_custom_blend < 0 && blend_time == 0 && default_blend_time) {
  1379. blend_time = default_blend_time;
  1380. }
  1381. if (blend_time > 0) {
  1382. Blend b;
  1383. b.data = c.current;
  1384. b.blend_time = b.blend_left = blend_time;
  1385. c.blend.push_back(b);
  1386. } else {
  1387. c.blend.clear();
  1388. }
  1389. }
  1390. if (get_current_animation() != p_name) {
  1391. _stop_playing_caches(false);
  1392. }
  1393. c.current.from = &animation_set[name];
  1394. if (c.assigned != name) { // reset
  1395. c.current.pos = p_from_end ? c.current.from->animation->get_length() : 0;
  1396. } else {
  1397. if (p_from_end && c.current.pos == 0) {
  1398. // Animation reset BUT played backwards, set position to the end
  1399. c.current.pos = c.current.from->animation->get_length();
  1400. } else if (!p_from_end && c.current.pos == c.current.from->animation->get_length()) {
  1401. // Animation resumed but already ended, set position to the beginning
  1402. c.current.pos = 0;
  1403. }
  1404. }
  1405. c.current.speed_scale = p_custom_scale;
  1406. c.assigned = name;
  1407. c.seeked = false;
  1408. c.started = true;
  1409. if (!end_reached) {
  1410. queued.clear();
  1411. }
  1412. _set_process(true); // always process when starting an animation
  1413. playing = true;
  1414. emit_signal(SceneStringNames::get_singleton()->animation_started, c.assigned);
  1415. if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) {
  1416. return; // no next in this case
  1417. }
  1418. StringName next = animation_get_next(p_name);
  1419. if (next != StringName() && animation_set.has(next)) {
  1420. queue(next);
  1421. }
  1422. }
  1423. bool AnimationPlayer::is_playing() const {
  1424. return playing;
  1425. }
  1426. void AnimationPlayer::set_current_animation(const String &p_anim) {
  1427. if (p_anim == "[stop]" || p_anim.is_empty()) {
  1428. stop();
  1429. } else if (!is_playing() || playback.assigned != p_anim) {
  1430. play(p_anim);
  1431. } else {
  1432. // Same animation, do not replay from start
  1433. }
  1434. }
  1435. String AnimationPlayer::get_current_animation() const {
  1436. return (is_playing() ? playback.assigned : "");
  1437. }
  1438. void AnimationPlayer::set_assigned_animation(const String &p_anim) {
  1439. if (is_playing()) {
  1440. play(p_anim);
  1441. } else {
  1442. ERR_FAIL_COND_MSG(!animation_set.has(p_anim), vformat("Animation not found: %s.", p_anim));
  1443. playback.current.pos = 0;
  1444. playback.current.from = &animation_set[p_anim];
  1445. playback.assigned = p_anim;
  1446. }
  1447. }
  1448. String AnimationPlayer::get_assigned_animation() const {
  1449. return playback.assigned;
  1450. }
  1451. void AnimationPlayer::pause() {
  1452. _stop_internal(false, false);
  1453. }
  1454. void AnimationPlayer::stop(bool p_keep_state) {
  1455. _stop_internal(true, p_keep_state);
  1456. }
  1457. void AnimationPlayer::set_speed_scale(float p_speed) {
  1458. speed_scale = p_speed;
  1459. }
  1460. float AnimationPlayer::get_speed_scale() const {
  1461. return speed_scale;
  1462. }
  1463. float AnimationPlayer::get_playing_speed() const {
  1464. if (!playing) {
  1465. return 0;
  1466. }
  1467. return speed_scale * playback.current.speed_scale;
  1468. }
  1469. void AnimationPlayer::seek(double p_time, bool p_update) {
  1470. if (!playback.current.from) {
  1471. if (playback.assigned) {
  1472. ERR_FAIL_COND_MSG(!animation_set.has(playback.assigned), vformat("Animation not found: %s.", playback.assigned));
  1473. playback.current.from = &animation_set[playback.assigned];
  1474. }
  1475. ERR_FAIL_COND(!playback.current.from);
  1476. }
  1477. playback.current.pos = p_time;
  1478. playback.seeked = true;
  1479. if (p_update) {
  1480. _animation_process(0);
  1481. }
  1482. }
  1483. void AnimationPlayer::seek_delta(double p_time, double p_delta) {
  1484. if (!playback.current.from) {
  1485. if (playback.assigned) {
  1486. ERR_FAIL_COND_MSG(!animation_set.has(playback.assigned), vformat("Animation not found: %s.", playback.assigned));
  1487. playback.current.from = &animation_set[playback.assigned];
  1488. }
  1489. ERR_FAIL_COND(!playback.current.from);
  1490. }
  1491. playback.current.pos = p_time - p_delta;
  1492. if (speed_scale != 0.0) {
  1493. p_delta /= speed_scale;
  1494. }
  1495. _animation_process(p_delta);
  1496. //playback.current.pos=p_time;
  1497. }
  1498. bool AnimationPlayer::is_valid() const {
  1499. return (playback.current.from);
  1500. }
  1501. double AnimationPlayer::get_current_animation_position() const {
  1502. ERR_FAIL_COND_V_MSG(!playback.current.from, 0, "AnimationPlayer has no current animation");
  1503. return playback.current.pos;
  1504. }
  1505. double AnimationPlayer::get_current_animation_length() const {
  1506. ERR_FAIL_COND_V_MSG(!playback.current.from, 0, "AnimationPlayer has no current animation");
  1507. return playback.current.from->animation->get_length();
  1508. }
  1509. void AnimationPlayer::_animation_changed(const StringName &p_name) {
  1510. clear_caches();
  1511. if (is_playing()) {
  1512. playback.seeked = true; //need to restart stuff, like audio
  1513. }
  1514. }
  1515. void AnimationPlayer::_stop_playing_caches(bool p_reset) {
  1516. for (TrackNodeCache *E : playing_caches) {
  1517. if (E->node && E->audio_playing) {
  1518. E->node->call(SNAME("stop"));
  1519. }
  1520. if (E->node && E->animation_playing) {
  1521. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(E->node);
  1522. if (!player) {
  1523. continue;
  1524. }
  1525. if (p_reset) {
  1526. player->stop();
  1527. } else {
  1528. player->pause();
  1529. }
  1530. }
  1531. }
  1532. playing_caches.clear();
  1533. }
  1534. void AnimationPlayer::_node_removed(Node *p_node) {
  1535. clear_caches(); // nodes contained here are being removed, clear the caches
  1536. }
  1537. void AnimationPlayer::clear_caches() {
  1538. _stop_playing_caches(true);
  1539. node_cache_map.clear();
  1540. for (KeyValue<StringName, AnimationData> &E : animation_set) {
  1541. E.value.node_cache.clear();
  1542. }
  1543. cache_update_size = 0;
  1544. cache_update_prop_size = 0;
  1545. cache_update_bezier_size = 0;
  1546. emit_signal(SNAME("caches_cleared"));
  1547. }
  1548. void AnimationPlayer::set_active(bool p_active) {
  1549. if (active == p_active) {
  1550. return;
  1551. }
  1552. active = p_active;
  1553. _set_process(processing, true);
  1554. }
  1555. bool AnimationPlayer::is_active() const {
  1556. return active;
  1557. }
  1558. StringName AnimationPlayer::find_animation(const Ref<Animation> &p_animation) const {
  1559. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  1560. if (E.value.animation == p_animation) {
  1561. return E.key;
  1562. }
  1563. }
  1564. return StringName();
  1565. }
  1566. StringName AnimationPlayer::find_animation_library(const Ref<Animation> &p_animation) const {
  1567. for (const KeyValue<StringName, AnimationData> &E : animation_set) {
  1568. if (E.value.animation == p_animation) {
  1569. return E.value.animation_library;
  1570. }
  1571. }
  1572. return StringName();
  1573. }
  1574. void AnimationPlayer::set_autoplay(const String &p_name) {
  1575. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
  1576. WARN_PRINT("Setting autoplay after the node has been added to the scene has no effect.");
  1577. }
  1578. autoplay = p_name;
  1579. }
  1580. String AnimationPlayer::get_autoplay() const {
  1581. return autoplay;
  1582. }
  1583. void AnimationPlayer::set_reset_on_save_enabled(bool p_enabled) {
  1584. reset_on_save = p_enabled;
  1585. }
  1586. bool AnimationPlayer::is_reset_on_save_enabled() const {
  1587. return reset_on_save;
  1588. }
  1589. void AnimationPlayer::set_process_callback(AnimationProcessCallback p_mode) {
  1590. if (process_callback == p_mode) {
  1591. return;
  1592. }
  1593. bool pr = processing;
  1594. if (pr) {
  1595. _set_process(false);
  1596. }
  1597. process_callback = p_mode;
  1598. if (pr) {
  1599. _set_process(true);
  1600. }
  1601. }
  1602. AnimationPlayer::AnimationProcessCallback AnimationPlayer::get_process_callback() const {
  1603. return process_callback;
  1604. }
  1605. void AnimationPlayer::set_method_call_mode(AnimationMethodCallMode p_mode) {
  1606. method_call_mode = p_mode;
  1607. }
  1608. AnimationPlayer::AnimationMethodCallMode AnimationPlayer::get_method_call_mode() const {
  1609. return method_call_mode;
  1610. }
  1611. void AnimationPlayer::set_movie_quit_on_finish_enabled(bool p_enabled) {
  1612. movie_quit_on_finish = p_enabled;
  1613. }
  1614. bool AnimationPlayer::is_movie_quit_on_finish_enabled() const {
  1615. return movie_quit_on_finish;
  1616. }
  1617. void AnimationPlayer::_set_process(bool p_process, bool p_force) {
  1618. if (processing == p_process && !p_force) {
  1619. return;
  1620. }
  1621. switch (process_callback) {
  1622. case ANIMATION_PROCESS_PHYSICS:
  1623. set_physics_process_internal(p_process && active);
  1624. break;
  1625. case ANIMATION_PROCESS_IDLE:
  1626. set_process_internal(p_process && active);
  1627. break;
  1628. case ANIMATION_PROCESS_MANUAL:
  1629. break;
  1630. }
  1631. processing = p_process;
  1632. }
  1633. void AnimationPlayer::_stop_internal(bool p_reset, bool p_keep_state) {
  1634. _stop_playing_caches(p_reset);
  1635. Playback &c = playback;
  1636. c.blend.clear();
  1637. if (p_reset) {
  1638. if (p_keep_state) {
  1639. c.current.pos = 0;
  1640. } else {
  1641. is_stopping = true;
  1642. seek(0, true);
  1643. is_stopping = false;
  1644. }
  1645. c.current.from = nullptr;
  1646. c.current.speed_scale = 1;
  1647. }
  1648. _set_process(false);
  1649. queued.clear();
  1650. playing = false;
  1651. }
  1652. void AnimationPlayer::animation_set_next(const StringName &p_animation, const StringName &p_next) {
  1653. ERR_FAIL_COND_MSG(!animation_set.has(p_animation), vformat("Animation not found: %s.", p_animation));
  1654. animation_set[p_animation].next = p_next;
  1655. }
  1656. StringName AnimationPlayer::animation_get_next(const StringName &p_animation) const {
  1657. if (!animation_set.has(p_animation)) {
  1658. return StringName();
  1659. }
  1660. return animation_set[p_animation].next;
  1661. }
  1662. void AnimationPlayer::set_default_blend_time(double p_default) {
  1663. default_blend_time = p_default;
  1664. }
  1665. double AnimationPlayer::get_default_blend_time() const {
  1666. return default_blend_time;
  1667. }
  1668. void AnimationPlayer::set_root(const NodePath &p_root) {
  1669. root = p_root;
  1670. clear_caches();
  1671. }
  1672. NodePath AnimationPlayer::get_root() const {
  1673. return root;
  1674. }
  1675. void AnimationPlayer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1676. String pf = p_function;
  1677. if (p_idx == 0 && (p_function == "play" || p_function == "play_backwards" || p_function == "has_animation" || p_function == "queue")) {
  1678. List<StringName> al;
  1679. get_animation_list(&al);
  1680. for (const StringName &name : al) {
  1681. r_options->push_back(String(name).quote());
  1682. }
  1683. }
  1684. Node::get_argument_options(p_function, p_idx, r_options);
  1685. }
  1686. #ifdef TOOLS_ENABLED
  1687. Ref<AnimatedValuesBackup> AnimationPlayer::backup_animated_values(Node *p_root_override) {
  1688. Ref<AnimatedValuesBackup> backup;
  1689. if (!playback.current.from) {
  1690. return backup;
  1691. }
  1692. _ensure_node_caches(playback.current.from, p_root_override);
  1693. backup.instantiate();
  1694. for (int i = 0; i < playback.current.from->node_cache.size(); i++) {
  1695. TrackNodeCache *nc = playback.current.from->node_cache[i];
  1696. if (!nc) {
  1697. continue;
  1698. }
  1699. if (nc->skeleton) {
  1700. if (nc->bone_idx == -1) {
  1701. continue;
  1702. }
  1703. AnimatedValuesBackup::Entry entry;
  1704. entry.object = nc->skeleton;
  1705. entry.bone_idx = nc->bone_idx;
  1706. Array arr;
  1707. arr.resize(3);
  1708. arr[0] = nc->skeleton->get_bone_pose_position(nc->bone_idx);
  1709. arr[1] = nc->skeleton->get_bone_pose_rotation(nc->bone_idx);
  1710. arr[2] = nc->skeleton->get_bone_pose_scale(nc->bone_idx);
  1711. entry.value = nc;
  1712. backup->entries.push_back(entry);
  1713. } else {
  1714. if (nc->node_3d) {
  1715. AnimatedValuesBackup::Entry entry;
  1716. entry.object = nc->node_3d;
  1717. entry.subpath.push_back("transform");
  1718. entry.value = nc->node_3d->get_transform();
  1719. entry.bone_idx = -1;
  1720. backup->entries.push_back(entry);
  1721. } else {
  1722. for (const KeyValue<StringName, TrackNodeCache::PropertyAnim> &E : nc->property_anim) {
  1723. AnimatedValuesBackup::Entry entry;
  1724. entry.object = E.value.object;
  1725. entry.subpath = E.value.subpath;
  1726. bool valid;
  1727. entry.value = E.value.object->get_indexed(E.value.subpath, &valid);
  1728. entry.bone_idx = -1;
  1729. if (valid) {
  1730. backup->entries.push_back(entry);
  1731. }
  1732. }
  1733. }
  1734. }
  1735. }
  1736. return backup;
  1737. }
  1738. Ref<AnimatedValuesBackup> AnimationPlayer::apply_reset(bool p_user_initiated) {
  1739. ERR_FAIL_COND_V(!can_apply_reset(), Ref<AnimatedValuesBackup>());
  1740. Ref<Animation> reset_anim = animation_set[SceneStringNames::get_singleton()->RESET].animation;
  1741. ERR_FAIL_COND_V(reset_anim.is_null(), Ref<AnimatedValuesBackup>());
  1742. Node *root_node = get_node_or_null(root);
  1743. ERR_FAIL_COND_V(!root_node, Ref<AnimatedValuesBackup>());
  1744. AnimationPlayer *aux_player = memnew(AnimationPlayer);
  1745. EditorNode::get_singleton()->add_child(aux_player);
  1746. Ref<AnimationLibrary> al;
  1747. al.instantiate();
  1748. al->add_animation(SceneStringNames::get_singleton()->RESET, reset_anim);
  1749. aux_player->add_animation_library("", al);
  1750. aux_player->set_assigned_animation(SceneStringNames::get_singleton()->RESET);
  1751. // Forcing the use of the original root because the scene where original player belongs may be not the active one
  1752. Ref<AnimatedValuesBackup> old_values = aux_player->backup_animated_values(get_node(get_root()));
  1753. aux_player->seek(0.0f, true);
  1754. aux_player->queue_free();
  1755. if (p_user_initiated) {
  1756. Ref<AnimatedValuesBackup> new_values = aux_player->backup_animated_values();
  1757. old_values->restore();
  1758. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  1759. ur->create_action(TTR("Animation Apply Reset"));
  1760. ur->add_do_method(new_values.ptr(), "restore");
  1761. ur->add_undo_method(old_values.ptr(), "restore");
  1762. ur->commit_action();
  1763. }
  1764. return old_values;
  1765. }
  1766. bool AnimationPlayer::can_apply_reset() const {
  1767. return has_animation(SceneStringNames::get_singleton()->RESET) && playback.assigned != SceneStringNames::get_singleton()->RESET;
  1768. }
  1769. #endif // TOOLS_ENABLED
  1770. void AnimationPlayer::_bind_methods() {
  1771. ClassDB::bind_method(D_METHOD("add_animation_library", "name", "library"), &AnimationPlayer::add_animation_library);
  1772. ClassDB::bind_method(D_METHOD("remove_animation_library", "name"), &AnimationPlayer::remove_animation_library);
  1773. ClassDB::bind_method(D_METHOD("rename_animation_library", "name", "newname"), &AnimationPlayer::rename_animation_library);
  1774. ClassDB::bind_method(D_METHOD("has_animation_library", "name"), &AnimationPlayer::has_animation_library);
  1775. ClassDB::bind_method(D_METHOD("get_animation_library", "name"), &AnimationPlayer::get_animation_library);
  1776. ClassDB::bind_method(D_METHOD("get_animation_library_list"), &AnimationPlayer::_get_animation_library_list);
  1777. ClassDB::bind_method(D_METHOD("has_animation", "name"), &AnimationPlayer::has_animation);
  1778. ClassDB::bind_method(D_METHOD("get_animation", "name"), &AnimationPlayer::get_animation);
  1779. ClassDB::bind_method(D_METHOD("get_animation_list"), &AnimationPlayer::_get_animation_list);
  1780. ClassDB::bind_method(D_METHOD("animation_set_next", "anim_from", "anim_to"), &AnimationPlayer::animation_set_next);
  1781. ClassDB::bind_method(D_METHOD("animation_get_next", "anim_from"), &AnimationPlayer::animation_get_next);
  1782. ClassDB::bind_method(D_METHOD("set_blend_time", "anim_from", "anim_to", "sec"), &AnimationPlayer::set_blend_time);
  1783. ClassDB::bind_method(D_METHOD("get_blend_time", "anim_from", "anim_to"), &AnimationPlayer::get_blend_time);
  1784. ClassDB::bind_method(D_METHOD("set_default_blend_time", "sec"), &AnimationPlayer::set_default_blend_time);
  1785. ClassDB::bind_method(D_METHOD("get_default_blend_time"), &AnimationPlayer::get_default_blend_time);
  1786. ClassDB::bind_method(D_METHOD("play", "name", "custom_blend", "custom_speed", "from_end"), &AnimationPlayer::play, DEFVAL(""), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false));
  1787. ClassDB::bind_method(D_METHOD("play_backwards", "name", "custom_blend"), &AnimationPlayer::play_backwards, DEFVAL(""), DEFVAL(-1));
  1788. ClassDB::bind_method(D_METHOD("pause"), &AnimationPlayer::pause);
  1789. ClassDB::bind_method(D_METHOD("stop", "keep_state"), &AnimationPlayer::stop, DEFVAL(false));
  1790. ClassDB::bind_method(D_METHOD("is_playing"), &AnimationPlayer::is_playing);
  1791. ClassDB::bind_method(D_METHOD("set_current_animation", "anim"), &AnimationPlayer::set_current_animation);
  1792. ClassDB::bind_method(D_METHOD("get_current_animation"), &AnimationPlayer::get_current_animation);
  1793. ClassDB::bind_method(D_METHOD("set_assigned_animation", "anim"), &AnimationPlayer::set_assigned_animation);
  1794. ClassDB::bind_method(D_METHOD("get_assigned_animation"), &AnimationPlayer::get_assigned_animation);
  1795. ClassDB::bind_method(D_METHOD("queue", "name"), &AnimationPlayer::queue);
  1796. ClassDB::bind_method(D_METHOD("get_queue"), &AnimationPlayer::get_queue);
  1797. ClassDB::bind_method(D_METHOD("clear_queue"), &AnimationPlayer::clear_queue);
  1798. ClassDB::bind_method(D_METHOD("set_active", "active"), &AnimationPlayer::set_active);
  1799. ClassDB::bind_method(D_METHOD("is_active"), &AnimationPlayer::is_active);
  1800. ClassDB::bind_method(D_METHOD("set_speed_scale", "speed"), &AnimationPlayer::set_speed_scale);
  1801. ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimationPlayer::get_speed_scale);
  1802. ClassDB::bind_method(D_METHOD("get_playing_speed"), &AnimationPlayer::get_playing_speed);
  1803. ClassDB::bind_method(D_METHOD("set_autoplay", "name"), &AnimationPlayer::set_autoplay);
  1804. ClassDB::bind_method(D_METHOD("get_autoplay"), &AnimationPlayer::get_autoplay);
  1805. ClassDB::bind_method(D_METHOD("set_reset_on_save_enabled", "enabled"), &AnimationPlayer::set_reset_on_save_enabled);
  1806. ClassDB::bind_method(D_METHOD("is_reset_on_save_enabled"), &AnimationPlayer::is_reset_on_save_enabled);
  1807. ClassDB::bind_method(D_METHOD("set_root", "path"), &AnimationPlayer::set_root);
  1808. ClassDB::bind_method(D_METHOD("get_root"), &AnimationPlayer::get_root);
  1809. ClassDB::bind_method(D_METHOD("find_animation", "animation"), &AnimationPlayer::find_animation);
  1810. ClassDB::bind_method(D_METHOD("find_animation_library", "animation"), &AnimationPlayer::find_animation_library);
  1811. ClassDB::bind_method(D_METHOD("clear_caches"), &AnimationPlayer::clear_caches);
  1812. ClassDB::bind_method(D_METHOD("set_process_callback", "mode"), &AnimationPlayer::set_process_callback);
  1813. ClassDB::bind_method(D_METHOD("get_process_callback"), &AnimationPlayer::get_process_callback);
  1814. ClassDB::bind_method(D_METHOD("set_method_call_mode", "mode"), &AnimationPlayer::set_method_call_mode);
  1815. ClassDB::bind_method(D_METHOD("get_method_call_mode"), &AnimationPlayer::get_method_call_mode);
  1816. ClassDB::bind_method(D_METHOD("set_movie_quit_on_finish_enabled", "enabled"), &AnimationPlayer::set_movie_quit_on_finish_enabled);
  1817. ClassDB::bind_method(D_METHOD("is_movie_quit_on_finish_enabled"), &AnimationPlayer::is_movie_quit_on_finish_enabled);
  1818. ClassDB::bind_method(D_METHOD("get_current_animation_position"), &AnimationPlayer::get_current_animation_position);
  1819. ClassDB::bind_method(D_METHOD("get_current_animation_length"), &AnimationPlayer::get_current_animation_length);
  1820. ClassDB::bind_method(D_METHOD("seek", "seconds", "update"), &AnimationPlayer::seek, DEFVAL(false));
  1821. ClassDB::bind_method(D_METHOD("advance", "delta"), &AnimationPlayer::advance);
  1822. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "root_node"), "set_root", "get_root");
  1823. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "current_animation", PROPERTY_HINT_ENUM, "", PROPERTY_USAGE_EDITOR), "set_current_animation", "get_current_animation");
  1824. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "assigned_animation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_assigned_animation", "get_assigned_animation");
  1825. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "autoplay", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_autoplay", "get_autoplay");
  1826. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reset_on_save", PROPERTY_HINT_NONE, ""), "set_reset_on_save_enabled", "is_reset_on_save_enabled");
  1827. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_length", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_current_animation_length");
  1828. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_current_animation_position");
  1829. ADD_GROUP("Playback Options", "playback_");
  1830. ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_process_mode", PROPERTY_HINT_ENUM, "Physics,Idle,Manual"), "set_process_callback", "get_process_callback");
  1831. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_default_blend_time", PROPERTY_HINT_RANGE, "0,4096,0.01,suffix:s"), "set_default_blend_time", "get_default_blend_time");
  1832. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playback_active", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_active", "is_active");
  1833. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_speed", PROPERTY_HINT_RANGE, "-64,64,0.01"), "set_speed_scale", "get_speed_scale");
  1834. ADD_PROPERTY(PropertyInfo(Variant::INT, "method_call_mode", PROPERTY_HINT_ENUM, "Deferred,Immediate"), "set_method_call_mode", "get_method_call_mode");
  1835. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "movie_quit_on_finish"), "set_movie_quit_on_finish_enabled", "is_movie_quit_on_finish_enabled");
  1836. ADD_SIGNAL(MethodInfo("animation_finished", PropertyInfo(Variant::STRING_NAME, "anim_name")));
  1837. ADD_SIGNAL(MethodInfo("animation_changed", PropertyInfo(Variant::STRING_NAME, "old_name"), PropertyInfo(Variant::STRING_NAME, "new_name")));
  1838. ADD_SIGNAL(MethodInfo("animation_started", PropertyInfo(Variant::STRING_NAME, "anim_name")));
  1839. ADD_SIGNAL(MethodInfo("animation_list_changed"));
  1840. ADD_SIGNAL(MethodInfo("animation_libraries_updated"));
  1841. ADD_SIGNAL(MethodInfo("caches_cleared"));
  1842. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_PHYSICS);
  1843. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_IDLE);
  1844. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_MANUAL);
  1845. BIND_ENUM_CONSTANT(ANIMATION_METHOD_CALL_DEFERRED);
  1846. BIND_ENUM_CONSTANT(ANIMATION_METHOD_CALL_IMMEDIATE);
  1847. }
  1848. AnimationPlayer::AnimationPlayer() {
  1849. root = SceneStringNames::get_singleton()->path_pp;
  1850. }
  1851. AnimationPlayer::~AnimationPlayer() {
  1852. }