Skip to content

Commit 06d105e

Browse files
committed
Merge pull request #91437 from TokageItLab/auto-capture
Add `auto_capture` option to AnimationPlayer
2 parents 6ad0a1f + 36abb55 commit 06d105e

File tree

7 files changed

+113
-65
lines changed

7 files changed

+113
-65
lines changed

doc/classes/Animation.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,9 @@
595595
</method>
596596
</methods>
597597
<members>
598+
<member name="capture_included" type="bool" setter="_set_capture_included" getter="is_capture_included" default="false">
599+
Returns [code]true[/code] if the capture track is included. This is a cached readonly value for performance.
600+
</member>
598601
<member name="length" type="float" setter="set_length" getter="get_length" default="1.0">
599602
The total length of the animation (in seconds).
600603
[b]Note:[/b] Length is not delimited by the last key, as this one may be before or after the end to ensure correct interpolation and looping.
@@ -658,7 +661,7 @@
658661
Update at the keyframes.
659662
</constant>
660663
<constant name="UPDATE_CAPTURE" value="2" enum="UpdateMode">
661-
Same as [constant UPDATE_CONTINUOUS] but works as a flag to capture the value of the current object and perform interpolation in some methods. See also [method AnimationMixer.capture] and [method AnimationPlayer.play_with_capture].
664+
Same as [constant UPDATE_CONTINUOUS] but works as a flag to capture the value of the current object and perform interpolation in some methods. See also [method AnimationMixer.capture], [member AnimationPlayer.playback_auto_capture], and [method AnimationPlayer.play_with_capture].
662665
</constant>
663666
<constant name="LOOP_NONE" value="0" enum="LoopMode">
664667
At both ends of the animation, the animation will stop playing.

doc/classes/AnimationPlayer.xml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,21 @@
112112
</method>
113113
<method name="play_with_capture">
114114
<return type="void" />
115-
<param index="0" name="name" type="StringName" />
115+
<param index="0" name="name" type="StringName" default="&amp;&quot;&quot;" />
116116
<param index="1" name="duration" type="float" default="-1.0" />
117117
<param index="2" name="custom_blend" type="float" default="-1" />
118118
<param index="3" name="custom_speed" type="float" default="1.0" />
119119
<param index="4" name="from_end" type="bool" default="false" />
120120
<param index="5" name="trans_type" type="int" enum="Tween.TransitionType" default="0" />
121121
<param index="6" name="ease_type" type="int" enum="Tween.EaseType" default="0" />
122122
<description>
123-
See [method AnimationMixer.capture]. It is almost the same as the following:
123+
See also [method AnimationMixer.capture].
124+
You can use this method to use more detailed options for capture than those performed by [member playback_auto_capture]. When [member playback_auto_capture] is [code]false[/code], this method is almost the same as the following:
124125
[codeblock]
125126
capture(name, duration, trans_type, ease_type)
126127
play(name, custom_blend, custom_speed, from_end)
127128
[/codeblock]
128-
If name is blank, it specifies [member assigned_animation].
129+
If [param name] is blank, it specifies [member assigned_animation].
129130
If [param duration] is a negative value, the duration is set to the interval between the current position and the first key, when [param from_end] is [code]true[/code], uses the interval between the current position and the last key instead.
130131
[b]Note:[/b] The [param duration] takes [member speed_scale] into account, but [param custom_speed] does not, because the capture cache is interpolated with the blend result and the result may contain multiple animations.
131132
</description>
@@ -210,6 +211,9 @@
210211
If [code]true[/code] and the engine is running in Movie Maker mode (see [MovieWriter]), exits the engine with [method SceneTree.quit] as soon as an animation is done playing in this [AnimationPlayer]. A message is printed when the engine quits for this reason.
211212
[b]Note:[/b] This obeys the same logic as the [signal AnimationMixer.animation_finished] signal, so it will not quit the engine if the animation is set to be looping.
212213
</member>
214+
<member name="playback_auto_capture" type="bool" setter="set_auto_capture" getter="is_auto_capture" default="true">
215+
If [code]true[/code], performs [method AnimationMixer.capture] before playback automatically. This means just [method play_with_capture] is executed with default arguments instead of [method play].
216+
</member>
213217
<member name="playback_default_blend_time" type="float" setter="set_default_blend_time" getter="get_default_blend_time" default="0.0">
214218
The default time in which to blend animations. Ranges from 0 to 4096 with 0.01 precision.
215219
</member>

scene/animation/animation_mixer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ Ref<AnimatedValuesBackup> AnimationMixer::apply_reset(bool p_user_initiated) {
20932093
void AnimationMixer::capture(const StringName &p_name, double p_duration, Tween::TransitionType p_trans_type, Tween::EaseType p_ease_type) {
20942094
ERR_FAIL_COND(!active);
20952095
ERR_FAIL_COND(!has_animation(p_name));
2096-
ERR_FAIL_COND(Math::is_zero_approx(p_duration));
2096+
ERR_FAIL_COND(p_duration <= 0);
20972097
Ref<Animation> reference_animation = get_animation(p_name);
20982098

20992099
if (!cache_valid) {

scene/animation/animation_player.cpp

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -370,73 +370,21 @@ void AnimationPlayer::play_backwards(const StringName &p_name, double p_custom_b
370370
play(p_name, p_custom_blend, -1, true);
371371
}
372372

373-
void AnimationPlayer::play_with_capture(const StringName &p_name, double p_duration, double p_custom_blend, float p_custom_scale, bool p_from_end, Tween::TransitionType p_trans_type, Tween::EaseType p_ease_type) {
374-
StringName name = p_name;
375-
if (name == StringName()) {
376-
name = playback.assigned;
377-
}
378-
379-
if (signbit(p_duration)) {
380-
double max_dur = 0;
381-
Ref<Animation> anim = get_animation(name);
382-
if (anim.is_valid()) {
383-
double current_pos = playback.current.pos;
384-
if (playback.assigned != name) {
385-
current_pos = p_from_end ? anim->get_length() : 0;
386-
}
387-
for (int i = 0; i < anim->get_track_count(); i++) {
388-
if (anim->track_get_type(i) != Animation::TYPE_VALUE) {
389-
continue;
390-
}
391-
if (anim->value_track_get_update_mode(i) != Animation::UPDATE_CAPTURE) {
392-
continue;
393-
}
394-
if (anim->track_get_key_count(i) == 0) {
395-
continue;
396-
}
397-
max_dur = MAX(max_dur, p_from_end ? current_pos - anim->track_get_key_time(i, anim->track_get_key_count(i) - 1) : anim->track_get_key_time(i, 0) - current_pos);
398-
}
399-
}
400-
p_duration = max_dur;
373+
void AnimationPlayer::play(const StringName &p_name, double p_custom_blend, float p_custom_scale, bool p_from_end) {
374+
if (auto_capture) {
375+
play_with_capture(p_name, -1.0, p_custom_blend, p_custom_scale, p_from_end);
376+
} else {
377+
_play(p_name, p_custom_blend, p_custom_scale, p_from_end);
401378
}
402-
403-
capture(name, p_duration, p_trans_type, p_ease_type);
404-
play(name, p_custom_blend, p_custom_scale, p_from_end);
405379
}
406380

407-
void AnimationPlayer::play(const StringName &p_name, double p_custom_blend, float p_custom_scale, bool p_from_end) {
381+
void AnimationPlayer::_play(const StringName &p_name, double p_custom_blend, float p_custom_scale, bool p_from_end) {
408382
StringName name = p_name;
409383

410384
if (name == StringName()) {
411385
name = playback.assigned;
412386
}
413387

414-
#ifdef TOOLS_ENABLED
415-
if (!Engine::get_singleton()->is_editor_hint()) {
416-
bool warn_enabled = false;
417-
if (capture_cache.animation.is_null()) {
418-
Ref<Animation> anim = get_animation(name);
419-
if (anim.is_valid()) {
420-
for (int i = 0; i < anim->get_track_count(); i++) {
421-
if (anim->track_get_type(i) != Animation::TYPE_VALUE) {
422-
continue;
423-
}
424-
if (anim->value_track_get_update_mode(i) != Animation::UPDATE_CAPTURE) {
425-
continue;
426-
}
427-
if (anim->track_get_key_count(i) == 0) {
428-
continue;
429-
}
430-
warn_enabled = true;
431-
}
432-
}
433-
}
434-
if (warn_enabled) {
435-
WARN_PRINT_ONCE_ED("Capture track found. If you want to interpolate animation with captured frame, you can use play_with_capture() instead of play().");
436-
}
437-
}
438-
#endif
439-
440388
ERR_FAIL_COND_MSG(!animation_set.has(name), vformat("Animation not found: %s.", name));
441389

442390
Playback &c = playback;
@@ -525,6 +473,47 @@ void AnimationPlayer::play(const StringName &p_name, double p_custom_blend, floa
525473
}
526474
}
527475

476+
void AnimationPlayer::_capture(const StringName &p_name, bool p_from_end, double p_duration, Tween::TransitionType p_trans_type, Tween::EaseType p_ease_type) {
477+
StringName name = p_name;
478+
if (name == StringName()) {
479+
name = playback.assigned;
480+
}
481+
482+
Ref<Animation> anim = get_animation(name);
483+
if (anim.is_null() || !anim->is_capture_included()) {
484+
return;
485+
}
486+
if (signbit(p_duration)) {
487+
double max_dur = 0;
488+
double current_pos = playback.current.pos;
489+
if (playback.assigned != name) {
490+
current_pos = p_from_end ? anim->get_length() : 0;
491+
}
492+
for (int i = 0; i < anim->get_track_count(); i++) {
493+
if (anim->track_get_type(i) != Animation::TYPE_VALUE) {
494+
continue;
495+
}
496+
if (anim->value_track_get_update_mode(i) != Animation::UPDATE_CAPTURE) {
497+
continue;
498+
}
499+
if (anim->track_get_key_count(i) == 0) {
500+
continue;
501+
}
502+
max_dur = MAX(max_dur, p_from_end ? current_pos - anim->track_get_key_time(i, anim->track_get_key_count(i) - 1) : anim->track_get_key_time(i, 0) - current_pos);
503+
}
504+
p_duration = max_dur;
505+
}
506+
if (Math::is_zero_approx(p_duration)) {
507+
return;
508+
}
509+
capture(name, p_duration, p_trans_type, p_ease_type);
510+
}
511+
512+
void AnimationPlayer::play_with_capture(const StringName &p_name, double p_duration, double p_custom_blend, float p_custom_scale, bool p_from_end, Tween::TransitionType p_trans_type, Tween::EaseType p_ease_type) {
513+
_capture(p_name, p_from_end, p_duration, p_trans_type, p_ease_type);
514+
_play(p_name, p_custom_blend, p_custom_scale, p_from_end);
515+
}
516+
528517
bool AnimationPlayer::is_playing() const {
529518
return playing;
530519
}
@@ -725,6 +714,14 @@ double AnimationPlayer::get_blend_time(const StringName &p_animation1, const Str
725714
}
726715
}
727716

717+
void AnimationPlayer::set_auto_capture(bool p_auto_capture) {
718+
auto_capture = p_auto_capture;
719+
}
720+
721+
bool AnimationPlayer::is_auto_capture() const {
722+
return auto_capture;
723+
}
724+
728725
#ifdef TOOLS_ENABLED
729726
void AnimationPlayer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
730727
const String pf = p_function;
@@ -815,9 +812,12 @@ void AnimationPlayer::_bind_methods() {
815812
ClassDB::bind_method(D_METHOD("set_default_blend_time", "sec"), &AnimationPlayer::set_default_blend_time);
816813
ClassDB::bind_method(D_METHOD("get_default_blend_time"), &AnimationPlayer::get_default_blend_time);
817814

815+
ClassDB::bind_method(D_METHOD("set_auto_capture", "auto_capture"), &AnimationPlayer::set_auto_capture);
816+
ClassDB::bind_method(D_METHOD("is_auto_capture"), &AnimationPlayer::is_auto_capture);
817+
818818
ClassDB::bind_method(D_METHOD("play", "name", "custom_blend", "custom_speed", "from_end"), &AnimationPlayer::play, DEFVAL(StringName()), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false));
819819
ClassDB::bind_method(D_METHOD("play_backwards", "name", "custom_blend"), &AnimationPlayer::play_backwards, DEFVAL(StringName()), DEFVAL(-1));
820-
ClassDB::bind_method(D_METHOD("play_with_capture", "name", "duration", "custom_blend", "custom_speed", "from_end", "trans_type", "ease_type"), &AnimationPlayer::play_with_capture, DEFVAL(-1.0), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false), DEFVAL(Tween::TRANS_LINEAR), DEFVAL(Tween::EASE_IN));
820+
ClassDB::bind_method(D_METHOD("play_with_capture", "name", "duration", "custom_blend", "custom_speed", "from_end", "trans_type", "ease_type"), &AnimationPlayer::play_with_capture, DEFVAL(StringName()), DEFVAL(-1.0), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false), DEFVAL(Tween::TRANS_LINEAR), DEFVAL(Tween::EASE_IN));
821821
ClassDB::bind_method(D_METHOD("pause"), &AnimationPlayer::pause);
822822
ClassDB::bind_method(D_METHOD("stop", "keep_state"), &AnimationPlayer::stop, DEFVAL(false));
823823
ClassDB::bind_method(D_METHOD("is_playing"), &AnimationPlayer::is_playing);
@@ -855,6 +855,7 @@ void AnimationPlayer::_bind_methods() {
855855
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_current_animation_position");
856856

857857
ADD_GROUP("Playback Options", "playback_");
858+
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playback_auto_capture"), "set_auto_capture", "is_auto_capture");
858859
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");
859860

860861
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "-4,4,0.001,or_less,or_greater"), "set_speed_scale", "get_speed_scale");

scene/animation/animation_player.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class AnimationPlayer : public AnimationMixer {
5656

5757
float speed_scale = 1.0;
5858
double default_blend_time = 0.0;
59+
bool auto_capture = true;
5960
bool is_stopping = false;
6061

6162
struct PlaybackData {
@@ -108,6 +109,8 @@ class AnimationPlayer : public AnimationMixer {
108109
bool reset_on_save = true;
109110
bool movie_quit_on_finish = false;
110111

112+
void _play(const StringName &p_name, double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
113+
void _capture(const StringName &p_name, bool p_from_end = false, double p_duration = -1.0, Tween::TransitionType p_trans_type = Tween::TRANS_LINEAR, Tween::EaseType p_ease_type = Tween::EASE_IN);
111114
void _process_playback_data(PlaybackData &cd, double p_delta, float p_blend, bool p_seeked, bool p_started, bool p_is_current = false);
112115
void _blend_playback_data(double p_delta, bool p_started);
113116
void _stop_internal(bool p_reset, bool p_keep_state);
@@ -158,9 +161,12 @@ class AnimationPlayer : public AnimationMixer {
158161
void set_default_blend_time(double p_default);
159162
double get_default_blend_time() const;
160163

164+
void set_auto_capture(bool p_auto_capture);
165+
bool is_auto_capture() const;
166+
161167
void play(const StringName &p_name = StringName(), double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
162168
void play_backwards(const StringName &p_name = StringName(), double p_custom_blend = -1);
163-
void play_with_capture(const StringName &p_name, double p_duration = -1.0, double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false, Tween::TransitionType p_trans_type = Tween::TRANS_LINEAR, Tween::EaseType p_ease_type = Tween::EASE_IN);
169+
void play_with_capture(const StringName &p_name = StringName(), double p_duration = -1.0, double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false, Tween::TransitionType p_trans_type = Tween::TRANS_LINEAR, Tween::EaseType p_ease_type = Tween::EASE_IN);
164170
void queue(const StringName &p_name);
165171
Vector<String> get_queue();
166172
void clear_queue();

scene/resources/animation.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ bool Animation::_set(const StringName &p_name, const Variant &p_value) {
247247
}
248248
vt->update_mode = UpdateMode(um);
249249
}
250+
capture_included = capture_included || (vt->update_mode == UPDATE_CAPTURE);
250251

251252
Vector<real_t> times = d["times"];
252253
Array values = d["values"];
@@ -966,6 +967,28 @@ void Animation::remove_track(int p_track) {
966967
memdelete(t);
967968
tracks.remove_at(p_track);
968969
emit_changed();
970+
_check_capture_included();
971+
}
972+
973+
void Animation::set_capture_included(bool p_capture_included) {
974+
capture_included = p_capture_included;
975+
}
976+
977+
bool Animation::is_capture_included() const {
978+
return capture_included;
979+
}
980+
981+
void Animation::_check_capture_included() {
982+
capture_included = false;
983+
for (int i = 0; i < tracks.size(); i++) {
984+
if (tracks[i]->type == TYPE_VALUE) {
985+
ValueTrack *vt = static_cast<ValueTrack *>(tracks[i]);
986+
if (vt->update_mode == UPDATE_CAPTURE) {
987+
capture_included = true;
988+
break;
989+
}
990+
}
991+
}
969992
}
970993

971994
int Animation::get_track_count() const {
@@ -2681,6 +2704,8 @@ void Animation::value_track_set_update_mode(int p_track, UpdateMode p_mode) {
26812704

26822705
ValueTrack *vt = static_cast<ValueTrack *>(t);
26832706
vt->update_mode = p_mode;
2707+
2708+
capture_included = capture_included || (p_mode == UPDATE_CAPTURE);
26842709
emit_changed();
26852710
}
26862711

@@ -3870,9 +3895,13 @@ void Animation::_bind_methods() {
38703895

38713896
ClassDB::bind_method(D_METHOD("compress", "page_size", "fps", "split_tolerance"), &Animation::compress, DEFVAL(8192), DEFVAL(120), DEFVAL(4.0));
38723897

3898+
ClassDB::bind_method(D_METHOD("_set_capture_included", "capture_included"), &Animation::set_capture_included);
3899+
ClassDB::bind_method(D_METHOD("is_capture_included"), &Animation::is_capture_included);
3900+
38733901
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "0.001,99999,0.001,suffix:s"), "set_length", "get_length");
38743902
ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "None,Linear,Ping-Pong"), "set_loop_mode", "get_loop_mode");
38753903
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step", PROPERTY_HINT_RANGE, "0,4096,0.001,suffix:s"), "set_step", "get_step");
3904+
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "capture_included", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_READ_ONLY | PROPERTY_USAGE_NO_EDITOR), "_set_capture_included", "is_capture_included");
38763905

38773906
BIND_ENUM_CONSTANT(TYPE_VALUE);
38783907
BIND_ENUM_CONSTANT(TYPE_POSITION_3D);

scene/resources/animation.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ class Animation : public Resource {
268268
double length = 1.0;
269269
real_t step = 1.0 / 30;
270270
LoopMode loop_mode = LOOP_NONE;
271+
bool capture_included = false;
272+
void _check_capture_included();
271273

272274
void _track_update_hash(int p_track);
273275

@@ -392,6 +394,9 @@ class Animation : public Resource {
392394
int add_track(TrackType p_type, int p_at_pos = -1);
393395
void remove_track(int p_track);
394396

397+
void set_capture_included(bool p_capture_included);
398+
bool is_capture_included() const;
399+
395400
int get_track_count() const;
396401
TrackType track_get_type(int p_track) const;
397402

0 commit comments

Comments
 (0)