Blender V5.3
animrig/intern/nla.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_bit_vector.hh"
10#include "BLI_dynstr.hh"
11
12#include "BKE_animsys.hh"
13#include "BKE_fcurve.hh"
14
15#include "RNA_access.hh"
16#include "RNA_types.hh"
17
18#include "BLT_translation.hh"
19
20#include "ANIM_keyframing.hh"
21#include "ANIM_nla.hh"
22
23namespace blender::animrig::nla {
24
25bool assign_action(NlaStrip &strip, Action &action, ID &animated_id)
26{
28 animated_id, &action, strip.act, strip.action_slot_handle, strip.last_slot_identifier))
29 {
30 return false;
31 }
32
33 /* For the NLA, the auto slot selection gets one more fallback option (compared to the generic
34 * code). This is to support the following scenario:
35 *
36 * - Python script creates an Action, and adds some F-Curves via the legacy API.
37 * - This creates a slot 'XXSlot'.
38 * - The script creates multiple NLA strips for that Action.
39 * - The desired result is that these strips get the same Slot assigned as well.
40 *
41 * The generic code doesn't work for this. The first strip assignment would see the slot
42 * `XXSlot`, and because it has never been used, just use it. This would change its name to, for
43 * example, `OBSlot`. The second strip assignment would not see a 'virgin' slot, and thus not
44 * auto-select `OBSlot`. This behavior makes sense when assigning Actions in the Action editor
45 * (it shouldn't automatically pick the first slot of matching ID type), but for the NLA I
46 * (Sybren) feel that it could be a bit more 'enthusiastic' in auto-picking a slot.
47 */
48 if (strip.action_slot_handle == Slot::unassigned && action.slots().size() == 1) {
49 Slot *first_slot = action.slot(0);
50 if (first_slot->is_suitable_for(animated_id)) {
51 const ActionSlotAssignmentResult result = assign_action_slot(strip, first_slot, animated_id);
53 "Assigning a slot that we know is suitable should work");
55 }
56 }
57
58 /* Regardless of slot auto-selection, the Action assignment worked just fine. */
59 return true;
60}
61
62void unassign_action(NlaStrip &strip, ID &animated_id)
63{
64 const bool ok = generic_assign_action(
65 animated_id, nullptr, strip.act, strip.action_slot_handle, strip.last_slot_identifier);
66 BLI_assert_msg(ok, "Un-assigning an Action from an NLA strip should always work.");
68}
69
71 Slot *slot_to_assign,
72 ID &animated_id)
73{
74 BLI_assert(strip.act);
75
76 return generic_assign_action_slot(slot_to_assign,
77 animated_id,
78 strip.act,
81}
82
84 const slot_handle_t slot_handle,
85 ID &animated_id)
86{
87 BLI_assert(strip.act);
88
89 Action &action = strip.act->wrap();
90 Slot *slot_to_assign = action.slot_for_handle(slot_handle);
91
92 return assign_action_slot(strip, slot_to_assign, animated_id);
93}
94
95/* Check indices that were intended to be remapped and report any failed remaps. */
97 const PointerRNA &ptr,
98 const PropertyRNA *prop,
99 const int index,
100 const int count,
101 const bool force_all,
102 const BitSpan successful_remaps)
103{
104
105 DynStr *ds_failed_indices = BLI_dynstr_new();
106
107 int total_failed = 0;
108 for (int i = 0; i < count; i++) {
109 const bool cur_index_evaluated = ELEM(index, i, -1) || force_all;
110 if (!cur_index_evaluated) {
111 /* `values[i]` was never intended to be remapped. */
112 continue;
113 }
114
115 if (successful_remaps[i]) {
116 /* `values[i]` successfully remapped. */
117 continue;
118 }
119
120 total_failed++;
121 /* Report that `values[i]` were intended to be remapped but failed remapping process. */
122 BLI_dynstr_appendf(ds_failed_indices, "%d, ", i);
123 }
124
125 if (total_failed == 0) {
126 BLI_dynstr_free(ds_failed_indices);
127 return;
128 }
129
130 char *str_failed_indices = BLI_dynstr_get_cstring(ds_failed_indices);
131 BLI_dynstr_free(ds_failed_indices);
132
133 BKE_reportf(reports,
135 "Could not insert %i keyframe(s) due to zero NLA influence, base value, or value "
136 "remapping failed: %s.%s for indices [%s]",
137 total_failed,
138 ptr.owner_id->name,
140 str_failed_indices);
141
142 MEM_delete(str_failed_indices);
143}
144
146 const MutableSpan<float> values,
147 const int index,
148 PointerRNA &ptr,
149 PropertyRNA &prop,
150 NlaKeyframingContext *nla_context,
151 const AnimationEvalContext *anim_eval_context,
152 ReportList *reports,
153 bool *force_all)
154{
155 BitVector<> successful_remaps(values.size(), false);
157 nla_context, &ptr, &prop, values, index, anim_eval_context, force_all, successful_remaps);
159 reports, ptr, &prop, index, values.size(), false, successful_remaps);
160 return successful_remaps;
161}
162
164 PointerRNA ptr,
165 PropertyRNA *prop,
166 FCurve *fcu,
167 const AnimationEvalContext *anim_eval_context,
169 NlaKeyframingContext *nla_context,
170 eInsertKeyFlags flag)
171{
172 if (fcu == nullptr) {
173 BKE_report(reports, RPT_ERROR, "No F-Curve to add keyframes to");
174 return false;
175 }
176
177 if (!BKE_fcurve_is_keyframable(*fcu)) {
178 BKE_report(reports, RPT_ERROR, "FCurve is not keyable. Cannot insert keyframes");
179 return false;
180 }
181
182 if (!ptr) {
184 reports, RPT_ERROR, "No RNA pointer available to retrieve values for keyframing from");
185 return false;
186 }
187
188 if (prop == nullptr) {
189 PointerRNA tmp_ptr;
190
191 if (RNA_path_resolve_property(&ptr, fcu->rna_path_parsed(), &tmp_ptr, &prop) == false) {
192 const char *idname = (ptr.owner_id) ? ptr.owner_id->name : RPT_("<No ID pointer>");
193
194 BKE_reportf(reports,
195 RPT_ERROR,
196 "Could not insert keyframe, as RNA path is invalid for the given ID (ID = %s, "
197 "path = %s)",
198 idname,
199 fcu->rna_path().c_str());
200 return false;
201 }
202
203 /* Property found, so overwrite 'ptr' to make later code easier. */
204 ptr = tmp_ptr;
205 }
206
207 /* Update F-Curve flags to ensure proper behavior for property type. */
209
210 const int index = fcu->array_index;
211 const bool visual_keyframing = flag & INSERTKEY_MATRIX;
212 Vector<float> values = get_property_values(&ptr, prop, visual_keyframing);
213
215 values.as_mutable_span(),
216 index,
217 ptr,
218 *prop,
219 nla_context,
220 anim_eval_context,
221 reports,
222 nullptr);
223
224 float current_value = 0.0f;
225 if (index >= 0 && index < values.size()) {
226 current_value = values[index];
227 }
228
229 /* This happens if NLA rejects this insertion. */
230 if (!successful_remaps[index]) {
231 return false;
232 }
233
235 settings.keyframe_type = keytype;
236
238 fcu, {anim_eval_context->eval_time, current_value}, settings, flag);
239
241 BKE_reportf(reports,
242 RPT_ERROR,
243 "Failed to insert keys on F-Curve with path '%s[%d]', ensure that it is not "
244 "locked or sampled, and try removing F-Modifiers",
245 fcu->rna_path().c_str(),
246 fcu->array_index);
247 }
249}
250
251} // namespace blender::animrig::nla
Functions to insert, delete or modify keyframes.
A dynamically sized string ADT.
#define RPT_(msgid)
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
constexpr int64_t size() const
Definition BLI_span.hh:499
int64_t size() const
MutableSpan< T > as_mutable_span()
Span< const Slot * > slots() const
const Slot * slot(int64_t index) const
Slot * slot_for_handle(slot_handle_t handle)
bool is_suitable_for(const ID &animated_id) const
static constexpr slot_handle_t unassigned
void unassign_action(NlaStrip &strip, ID &animated_id)
static void get_keyframe_values_create_reports(ReportList *reports, const PointerRNA &ptr, const PropertyRNA *prop, const int index, const int count, const bool force_all, const BitSpan successful_remaps)
ActionSlotAssignmentResult assign_action_slot_handle(NlaStrip &strip, slot_handle_t slot_handle, ID &animated_id)
bool assign_action(NlaStrip &strip, Action &action, ID &animated_id)
ActionSlotAssignmentResult assign_action_slot(NlaStrip &strip, Slot *slot_to_assign, ID &animated_id)
bool insert_keyframe_direct(ReportList *reports, PointerRNA ptr, PropertyRNA *prop, FCurve *fcu, const AnimationEvalContext *anim_eval_context, eBezTriple_KeyframeType keytype, NlaKeyframingContext *nla_context, eInsertKeyFlags flag)
static BitVector nla_map_keyframe_values_and_generate_reports(const MutableSpan< float > values, const int index, PointerRNA &ptr, PropertyRNA &prop, NlaKeyframingContext *nla_context, const AnimationEvalContext *anim_eval_context, ReportList *reports, bool *force_all)
decltype(blender::ActionSlot::handle) slot_handle_t
KeyframeSettings get_keyframe_settings(bool from_userprefs)
void update_autoflags_fcurve_direct(FCurve *fcu, PropertyType prop_type)
Vector< float > get_property_values(PointerRNA *ptr, PropertyRNA *prop, bool visual_key)
bool generic_assign_action(ID &animated_id, bAction *action_to_assign, bAction *&action_ptr_ref, slot_handle_t &slot_handle_ref, char *slot_identifier)
ActionSlotAssignmentResult generic_assign_action_slot(Slot *slot_to_assign, ID &animated_id, bAction *&action_ptr_ref, slot_handle_t &slot_handle_ref, char *slot_identifier)
SingleKeyingResult insert_vert_fcurve(FCurve *fcu, const float2 position, const KeyframeSettings &settings, eInsertKeyFlags flag)
Main Key-framing API call.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BLI_dynstr_appendf(DynStr *__restrict ds, const char *__restrict format,...) ATTR_PRINTF_FORMAT(2
@ RPT_WARNING
Definition BKE_report.hh:40
void BKE_animsys_nla_remap_keyframe_values(NlaKeyframingContext *context, PointerRNA *prop_ptr, PropertyRNA *prop, const MutableSpan< float > values, int index, const AnimationEvalContext *anim_eval_context, bool *r_force_all, BitVector<> &r_values_mask)
Definition anim_sys.cc:3524
bool BKE_fcurve_is_keyframable(const FCurve &fcu)
void BLI_dynstr_free(DynStr *ds) ATTR_NONNULL()
PropertyType RNA_property_type(PropertyRNA *prop)
@ INSERTKEY_NO_USERPREF
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
char * BLI_dynstr_get_cstring(const DynStr *ds) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
DynStr * BLI_dynstr_new() ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_dynstr.cc:39
bool RNA_path_resolve_property(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
Definition rna_path.cc:578
const char * RNA_property_ui_name(const PropertyRNA *prop, const PointerRNA *ptr)
char name[258]
Definition DNA_ID.h:433
char last_slot_identifier[258]
eBezTriple_KeyframeType keyframe_type