Blender V5.3
evaluation.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Developers
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "ANIM_evaluation.hh"
10
11#include "BKE_animsys.hh"
12#include "BKE_fcurve.hh"
13
14#include "BLI_map.hh"
15#include "BLI_math_base.hh"
16#include "BLI_task.hh"
17
18#include "CLG_log.h"
19
21
22namespace blender {
23
24static CLG_LogRef LOG = {"anim.evaluation"};
25
26namespace animrig {
27
28using namespace internal;
29
34void blend_layer_results(EvaluationResult &final_result,
35 const EvaluationResult &intermediate_result,
36 const Layer &current_layer);
37
44void apply_evaluation_result(const EvaluationResult &evaluation_result,
45 PointerRNA &animated_id_ptr,
46 bool flush_to_original);
47
49 Action &action,
50 const slot_handle_t slot_handle,
51 const AnimationEvalContext &anim_eval_context)
52{
54
55 /* Evaluate each layer in order. */
56 for (Layer *layer : action.layers()) {
57 if (layer->influence <= 0.0f) {
58 /* Don't bother evaluating layers without influence. */
59 continue;
60 }
61
62 EvaluationResult layer_result = evaluate_layer(
63 animated_id_ptr, action, *layer, slot_handle, anim_eval_context);
64 if (!layer_result) {
65 continue;
66 }
67
68 if (!result) {
69 /* Simple case: no results so far, so just use this layer as-is. There is
70 * nothing to blend/combine with, so ignore the influence and combination
71 * options. */
72 result = std::move(layer_result);
73 continue;
74 }
75
76 /* Complex case: blend this layer's result into combined result. */
77 blend_layer_results(result, layer_result, *layer);
78 }
79
80 return result;
81}
82
84 Action &action,
85 const slot_handle_t slot_handle,
86 const AnimationEvalContext &anim_eval_context,
87 const bool flush_to_original)
88{
89 EvaluationResult evaluation_result = evaluate_action(
90 animated_id_ptr, action, slot_handle, anim_eval_context);
91 if (!evaluation_result) {
92 return;
93 }
94
95 apply_evaluation_result(evaluation_result, animated_id_ptr, flush_to_original);
96}
97
98/* Copy of the same-named function in anim_sys.cc, with the check on action groups removed. */
99static bool is_fcurve_evaluatable(const FCurve *fcu)
100{
101 if (fcu->rna_path().is_empty()) {
102 return false;
103 }
104
105 /* Not checking for FCURVE_DISABLED here, because those FCurves may still be evaluatable for
106 * other users of the same slot. See #135666. This is safe to do since this function isn't called
107 * for drivers. */
108 if (fcu->flag & FCURVE_MUTED) {
109 return false;
110 }
111 if (BKE_fcurve_is_empty(fcu)) {
112 return false;
113 }
114 return true;
115}
116
117/* Copy of the same-named function in anim_sys.cc, but with the special handling for NLA strips
118 * removed. */
120{
121 *ptr_orig = *ptr;
122 /* Original note from anim_sys.cc:
123 * -----------
124 * NOTE: nlastrip_evaluate_controls() creates PointerRNA with ID of nullptr. Technically, this is
125 * not a valid pointer, but there are exceptions in various places of this file which handles
126 * such pointers.
127 * We do special trickery here as well, to quickly go from evaluated to original NlaStrip.
128 * -----------
129 * And this is all not ported to the new layered animation system. */
130 BLI_assert_msg(ptr->owner_id, "NLA support was not ported to the layered animation system");
131 ptr_orig->owner_id = ptr_orig->owner_id->orig_id;
132 ptr_orig->data = ptr_orig->owner_id;
133}
134
135/* Copy of the same-named function in anim_sys.cc. */
138 const int array_index,
139 const float value)
140{
141 PointerRNA ptr_orig;
143
144 PathResolvedRNA orig_anim_rna;
145 /* TODO(sergey): Should be possible to cache resolved path in dependency graph somehow. */
146 if (BKE_animsys_rna_path_resolve(&ptr_orig, rna_path, array_index, &orig_anim_rna)) {
147 BKE_animsys_write_to_rna_path(&orig_anim_rna, value);
148 }
149}
150
152 StripKeyframeData &strip_data,
153 const slot_handle_t slot_handle,
154 const AnimationEvalContext &offset_eval_context)
155{
156 Channelbag *channelbag_for_slot = strip_data.channelbag_for_slot(slot_handle);
157 if (!channelbag_for_slot) {
158 return {};
159 }
160
161 Span<FCurve *> fcurves = channelbag_for_slot->fcurves();
162 /* Stores true for FCurves with successfully resolved RNA paths. */
163 Array<bool> valid(fcurves.size(), false);
164 Array<float> results(fcurves.size());
165 Array<PathResolvedRNA> resolved_rna(fcurves.size());
166
167 /* RNA path resolution may access data for writing and is not safe to parallelize here. */
168 for (const int i : fcurves.index_range()) {
169 FCurve *fcu = fcurves[i];
170 if (!is_fcurve_evaluatable(fcu)) {
171 continue;
172 }
173
174 PathResolvedRNA &anim_rna = resolved_rna[i];
176 &animated_id_ptr, fcu->rna_path_parsed(), fcu->array_index, &anim_rna))
177 {
178 continue;
179 }
180
181 valid[i] = true;
182 }
183
184 threading::parallel_for(fcurves.index_range(), 512, [&](const IndexRange range) {
185 for (const int i : range) {
186 if (!valid[i]) {
187 continue;
188 }
189
190 FCurve *fcu = fcurves[i];
191 BLI_assert(fcu->driver == nullptr);
192 /* Not using calculate_fcurve because FCurves of channelbags are not drivers. */
193 results[i] = evaluate_fcurve(fcu, offset_eval_context.eval_time);
194 }
195 });
196
197 EvaluationResult evaluation_result;
198 evaluation_result.reserve(fcurves.size());
199 for (const int i : fcurves.index_range()) {
200 if (!valid[i]) {
201 continue;
202 }
203 /* This part is not threadsafe. */
204 evaluation_result.store(fcurves[i]->rna_path_parsed(),
205 fcurves[i]->array_index,
206 results[i],
207 std::move(resolved_rna[i]));
208 }
209
210 return evaluation_result;
211}
212
213void apply_evaluation_result(const EvaluationResult &evaluation_result,
214 PointerRNA &animated_id_ptr,
215 const bool flush_to_original)
216{
217 for (const auto &channel_result : evaluation_result.items()) {
218 const PropIdentifier &prop_ident = channel_result.key;
219 const AnimatedProperty &anim_prop = channel_result.value;
220 const float animated_value = anim_prop.value;
221 PathResolvedRNA anim_rna = anim_prop.prop_rna;
222
223 BKE_animsys_write_to_rna_path(&anim_rna, animated_value);
224
225 if (flush_to_original) {
226 /* Convert the StringRef to a `const char *`, as the rest of the RNA path handling code in
227 * BKE still uses `char *` instead of `StringRef`. */
229 &animated_id_ptr, prop_ident.rna_path, prop_ident.array_index, animated_value);
230 }
231 }
232}
233
235 Action &owning_action,
236 Strip &strip,
237 const slot_handle_t slot_handle,
238 const AnimationEvalContext &anim_eval_context)
239{
240 AnimationEvalContext offset_eval_context = anim_eval_context;
241 /* Positive offset means the entire strip is pushed "to the right", so
242 * evaluation needs to happen further "to the left". */
243 offset_eval_context.eval_time -= strip.frame_offset;
244
245 switch (strip.type()) {
247 StripKeyframeData &strip_data = strip.data<StripKeyframeData>(owning_action);
248 return evaluate_keyframe_data(animated_id_ptr, strip_data, slot_handle, offset_eval_context);
249 }
250 }
251
252 return {};
253}
254
256 const EvaluationResult &intermediate_result,
257 const Layer &current_layer)
258{
259 /* TODO?: store the layer results sequentially, so that we can step through
260 * them in parallel, instead of iterating over one and doing map lookups on
261 * the other. */
262
263 for (const auto &channel_result : intermediate_result.items()) {
264 const PropIdentifier &prop_ident = channel_result.key;
265 AnimatedProperty *last_prop = final_result.lookup_ptr(prop_ident);
266 const AnimatedProperty &anim_prop = channel_result.value;
267
268 if (!last_prop) {
269 /* Nothing to blend with, so just take (influence * value). */
270 final_result.store(prop_ident.rna_path,
271 prop_ident.array_index,
272 anim_prop.value * current_layer.influence,
273 anim_prop.prop_rna);
274 continue;
275 }
276
277 /* TODO: move this to a separate function. And write more smartness for rotations. */
278 switch (current_layer.mix_mode()) {
280 last_prop->value = anim_prop.value * current_layer.influence;
281 break;
283 last_prop->value = math::interpolate(
284 current_layer.influence, last_prop->value, anim_prop.value);
285 break;
287 last_prop->value += anim_prop.value * current_layer.influence;
288 break;
290 last_prop->value -= anim_prop.value * current_layer.influence;
291 break;
293 last_prop->value *= anim_prop.value * current_layer.influence;
294 break;
295 };
296 }
297}
298
299namespace internal {
300
302 Action &owning_action,
303 Layer &layer,
304 const slot_handle_t slot_handle,
305 const AnimationEvalContext &anim_eval_context)
306{
307 /* TODO: implement cross-blending between overlapping strips. For now, this is not supported.
308 * Instead, the first strong result is taken (see below), and if that is not available, the last
309 * weak result will be used.
310 *
311 * Weak result: obtained from evaluating the final frame of the strip.
312 * Strong result: any result that is not a weak result. */
313 EvaluationResult last_weak_result;
314
315 for (Strip *strip : layer.strips()) {
316 if (!strip->contains_frame(anim_eval_context.eval_time)) {
317 continue;
318 }
319
320 /* Cannot use const here because the std::move would not work otherwise. */
321 EvaluationResult strip_result = evaluate_strip(
322 animated_id_ptr, owning_action, *strip, slot_handle, anim_eval_context);
323 if (!strip_result) {
324 continue;
325 }
326
327 const bool is_weak_result = strip->is_last_frame(anim_eval_context.eval_time);
328 if (is_weak_result) {
329 /* Keep going until a strong result is found. */
330 last_weak_result = std::move(strip_result);
331 continue;
332 }
333
334 /* Found a strong result, just return it. */
335 return strip_result;
336 }
337
338 return last_weak_result;
339}
340
341} // namespace internal
342
343} // namespace animrig
344} // namespace blender
Layered Action evaluation.
#define BLI_assert_msg(a, msg)
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
Span< const Layer * > layers() const
Span< const FCurve * > fcurves() const
void reserve(const int64_t size)
EvaluationMap::ItemIterator items() const
void store(const ParsedRNAPathRef rna_path, const int array_index, const float value, PathResolvedRNA prop_rna)
const AnimatedProperty * lookup_ptr(const PropIdentifier &key) const
MixMode mix_mode() const
const Channelbag * channelbag_for_slot(const Slot &slot) const
const T & data(const Action &owning_action) const
#define layer
EvaluationResult evaluate_layer(PointerRNA &animated_id_ptr, Action &owning_action, Layer &layer, const slot_handle_t slot_handle, const AnimationEvalContext &anim_eval_context)
void evaluate_and_apply_action(PointerRNA &animated_id_ptr, Action &action, slot_handle_t slot_handle, const AnimationEvalContext &anim_eval_context, bool flush_to_original)
Definition evaluation.cc:83
decltype(blender::ActionSlot::handle) slot_handle_t
static bool is_fcurve_evaluatable(const FCurve *fcu)
Definition evaluation.cc:99
EvaluationResult evaluate_action(PointerRNA &animated_id_ptr, Action &action, slot_handle_t slot_handle, const AnimationEvalContext &anim_eval_context)
Definition evaluation.cc:48
static void animsys_construct_orig_pointer_rna(const PointerRNA *ptr, PointerRNA *ptr_orig)
static void animsys_write_orig_anim_rna(PointerRNA *ptr, const ParsedRNAPathRef rna_path, const int array_index, const float value)
void apply_evaluation_result(const EvaluationResult &evaluation_result, PointerRNA &animated_id_ptr, bool flush_to_original)
static EvaluationResult evaluate_keyframe_data(PointerRNA &animated_id_ptr, StripKeyframeData &strip_data, const slot_handle_t slot_handle, const AnimationEvalContext &offset_eval_context)
void blend_layer_results(EvaluationResult &final_result, const EvaluationResult &intermediate_result, const Layer &current_layer)
static EvaluationResult evaluate_strip(PointerRNA &animated_id_ptr, Action &owning_action, Strip &strip, const slot_handle_t slot_handle, const AnimationEvalContext &anim_eval_context)
T interpolate(const T &a, const T &b, const FactorT &t)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh Iterators.
Span< rna_path::Item > ParsedRNAPathRef
Definition RNA_path.hh:113
bool BKE_animsys_write_to_rna_path(PathResolvedRNA *anim_rna, float value, bool force_write=false)
Definition anim_sys.cc:461
static CLG_LogRef LOG
Definition logging.cpp:12
bool BKE_animsys_rna_path_resolve(PointerRNA *ptr, ParsedRNAPathRef rna_path, int array_index, PathResolvedRNA *r_result)
Definition anim_sys.cc:356
bool BKE_fcurve_is_empty(const FCurve *fcu)
eFCurve_Flags flag
struct ID * orig_id
Definition DNA_ID.h:502