Blender V5.3
animrig/intern/keyingsets.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 "ANIM_keyframing.hh"
10#include "ANIM_keyingsets.hh"
11
12#include "BKE_animsys.hh"
13#include "BKE_context.hh"
14#include "BKE_lib_id.hh"
15#include "BKE_main.hh"
16#include "BKE_report.hh"
17
18#include "BLI_listbase.hh"
19#include "BLI_string.hh"
20
21#include "DEG_depsgraph.hh"
22
23#include "DNA_anim_types.h"
24#include "DNA_scene_types.h"
25
26#include "RNA_access.hh"
27
28#include "WM_api.hh"
29
30namespace blender {
31
32/* Keying Set Type Info declarations. */
33static ListBaseT<KeyingSetInfo> keyingset_type_infos = {nullptr, nullptr};
34ListBaseT<KeyingSet> builtin_keyingsets = {nullptr, nullptr};
35
36namespace animrig {
37
39{
40 /* Create a new KeyingSet
41 * - inherit name and keyframing settings from the typeinfo
42 */
44 keyingset_info->idname,
45 keyingset_info->name,
47 keyingset_info->keyingflag);
48
49 /* Link this KeyingSet with its typeinfo. */
50 memcpy(&keyingset->typeinfo, keyingset_info->idname, sizeof(keyingset->typeinfo));
51
52 /* Copy description. */
53 STRNCPY(keyingset->description, keyingset_info->description);
54
55 /* Add type-info to the list. */
56 BLI_addtail(&keyingset_type_infos, keyingset_info);
57}
58
59void keyingset_info_unregister(Main *bmain, KeyingSetInfo *keyingset_info)
60{
61 /* Find relevant builtin KeyingSets which use this, and remove them. */
62 /* TODO: this isn't done now, since unregister is really only used at the moment when we
63 * reload the scripts, which kind of defeats the purpose of "builtin"? */
64 for (KeyingSet &keyingset : builtin_keyingsets.items_mutable()) {
65 /* Remove if matching typeinfo name. */
66 if (!STREQ(keyingset.typeinfo, keyingset_info->idname)) {
67 continue;
68 }
69 BKE_keyingset_free_paths(&keyingset);
70 BLI_remlink(&builtin_keyingsets, &keyingset);
71
72 for (Scene &scene : bmain->scenes) {
73 BLI_remlink_safe(&scene.keyingsets, &keyingset);
74 }
75
76 MEM_delete(&keyingset);
77 }
78
79 BLI_freelinkN(&keyingset_type_infos, keyingset_info);
80}
81
83{
84 /* Free type infos. */
85 for (KeyingSetInfo &keyingset_info : keyingset_type_infos.items_mutable()) {
86 /* Free extra RNA data, and remove from list. */
87 if (keyingset_info.rna_ext.free) {
88 keyingset_info.rna_ext.free(keyingset_info.rna_ext.data);
89 }
90 BLI_freelinkN(&keyingset_type_infos, &keyingset_info);
91 }
92
94}
95
96bool keyingset_find_id(KeyingSet *keyingset, ID *id)
97{
98 if (ELEM(nullptr, keyingset, id)) {
99 return false;
100 }
101
102 return BLI_findptr(&keyingset->paths, id, offsetof(KS_Path, id)) != nullptr;
103}
104
106{
107 if ((name == nullptr) || (name[0] == 0)) {
108 return nullptr;
109 }
110
111 /* Search by comparing names. */
112 return static_cast<KeyingSetInfo *>(
114}
115
117{
118 if (name[0] == 0) {
119 return nullptr;
120 }
121
122 /* Loop over KeyingSets checking names. */
123 for (KeyingSet &keyingset : builtin_keyingsets) {
124 if (STREQ(name, keyingset.idname)) {
125 return &keyingset;
126 }
127 }
128
129/* Complain about missing keying sets on debug builds. */
130#ifndef NDEBUG
131 printf("%s: '%s' not found\n", __func__, name);
132#endif
133
134 return nullptr;
135}
136
137KeyingSet *get_keyingset_for_autokeying(const Scene *scene, const char *transformKSName)
138{
139 /* Get KeyingSet to use
140 * - use the active KeyingSet if defined (and user wants to use it for all autokeying),
141 * or otherwise key transforms only
142 */
144 return scene_get_active_keyingset(scene);
145 }
146
148 return builtin_keyingset_get_named(ANIM_KS_AVAILABLE_ID);
149 }
150
151 return builtin_keyingset_get_named(transformKSName);
152}
153
155{
156 /* If no scene, we've got no hope of finding the Keying Set. */
157 if (scene == nullptr) {
158 return nullptr;
159 }
160
161 /* Currently, there are several possibilities here:
162 * - 0: no active keying set
163 * - > 0: one of the user-defined Keying Sets, but indices start from 0 (hence the -1)
164 * - < 0: a builtin keying set
165 */
166 if (scene->active_keyingset > 0) {
167 return static_cast<KeyingSet *>(BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1));
168 }
169 return static_cast<KeyingSet *>(
171}
172
174 ID *id,
175 StructRNA *srna,
176 void *data)
177{
178 if (ELEM(nullptr, srna, data, id)) {
179 return;
180 }
181 sources.append(RNA_pointer_create_discrete(id, srna, data));
182}
183
185{
186 if (id == nullptr) {
187 return;
188 }
189 sources.append(RNA_id_pointer_create(id));
190}
191
192/* Special 'Overrides' Iterator for Relative KeyingSets ------ */
193
194/* Iterator used for overriding the behavior of iterators defined for
195 * relative Keying Sets, with the main usage of this being operators
196 * requiring Auto Keyframing. Internal Use Only!
197 */
198static void RKS_ITER_overrides_list(KeyingSetInfo *keyingset_info,
199 bContext *C,
200 KeyingSet *keyingset,
201 Vector<PointerRNA> &sources)
202{
203 for (PointerRNA ptr : sources) {
204 /* Run generate callback on this data. */
205 keyingset_info->generate(keyingset_info, C, keyingset, &ptr);
206 }
207}
208
210{
211 if (keyingset == nullptr) {
213 }
214
215 /* If relative Keying Sets, poll and build up the paths. */
216 if (keyingset->flag & KEYINGSET_ABSOLUTE) {
218 }
219
220 KeyingSetInfo *keyingset_info = keyingset_info_find_name(keyingset->typeinfo);
221
222 /* Clear all existing paths
223 * NOTE: BKE_keyingset_free_paths() frees all of the paths for the KeyingSet, but not the set
224 * itself.
225 */
226 BKE_keyingset_free_paths(keyingset);
227
228 /* Get the associated 'type info' for this KeyingSet. */
229 if (keyingset_info == nullptr) {
231 }
232 /* TODO: check for missing callbacks! */
233
234 /* Check if it can be used in the current context. */
235 if (!keyingset_info->poll(keyingset_info, C)) {
236 /* Poll callback tells us that KeyingSet is useless in current context. */
237 /* FIXME: the poll callback needs to give us more info why. */
239 }
240
241 /* If a list of data sources are provided, run a special iterator over them,
242 * otherwise, just continue per normal.
243 */
244 if (sources != nullptr) {
245 RKS_ITER_overrides_list(keyingset_info, C, keyingset, *sources);
246 }
247 else {
248 keyingset_info->iter(keyingset_info, C, keyingset);
249 }
250
251 /* If we don't have any paths now, then this still qualifies as invalid context. */
252 /* FIXME: we need some error conditions (to be retrieved from the iterator why this failed!)
253 */
254 if (keyingset->paths.is_empty()) {
256 }
257
259}
260
261/* Determine which keying flags apply based on the override flags. */
263 const eInsertKeyFlags overrides,
264 const eInsertKeyFlags own_flags)
265{
266 /* Pass through all flags by default (i.e. even not explicitly listed ones). */
267 eInsertKeyFlags result = base_flags;
268
269/* The logic for whether a keying flag applies is as follows:
270 * - If the flag in question is set in "overrides", that means that the
271 * status of that flag in "own_flags" is used
272 * - If however the flag isn't set, then its value in "base_flags" is used
273 * instead (i.e. no override)
274 */
275#define APPLY_KEYINGFLAG_OVERRIDE(kflag) \
276 if (overrides & kflag) { \
277 result &= ~kflag; \
278 result |= (own_flags & kflag); \
279 }
280
281 /* Apply the flags one by one...
282 * (See rna_def_common_keying_flags() for the supported flags)
283 */
286
287#undef APPLY_KEYINGFLAG_OVERRIDE
288
289 return result;
290}
291
293 KS_Path *keyingset_path,
294 KeyingSet *keyingset,
295 const eInsertKeyFlags insert_key_flags,
296 const ModifyKeyMode mode,
297 const float frame)
298{
299 if (!keyingset_path->rna_path) {
300 /* In case the path is incomplete/not filled in by the user. */
301 return 0;
302 }
303 /* Since keying settings can be defined on the paths too,
304 * apply the settings for this path first. */
305 const eInsertKeyFlags path_insert_key_flags = keyingset_apply_keying_flags(
306 insert_key_flags,
307 eInsertKeyFlags(keyingset_path->keyingoverride),
308 eInsertKeyFlags(keyingset_path->keyingflag));
309
310 const char *groupname = nullptr;
311 /* Get pointer to name of group to add channels to. */
312 if (keyingset_path->groupmode == KSP_GROUP_NONE) {
313 groupname = nullptr;
314 }
315 else if (keyingset_path->groupmode == KSP_GROUP_KSNAME) {
316 groupname = keyingset->name;
317 }
318 else {
319 groupname = keyingset_path->group;
320 }
321
322 /* Init - array_length should be greater than array_index so that
323 * normal non-array entries get keyframed correctly.
324 */
325 int array_index = keyingset_path->array_index;
326 int array_length = array_index;
327
328 /* Get length of array if whole array option is enabled. */
329 if (keyingset_path->flag & KSP_FLAG_WHOLE_ARRAY) {
330 PointerRNA ptr;
331 PropertyRNA *prop;
332
333 PointerRNA id_ptr = RNA_id_pointer_create(keyingset_path->id);
334 if (RNA_path_resolve_property(&id_ptr, keyingset_path->rna_path, &ptr, &prop)) {
335 array_length = RNA_property_array_length(&ptr, prop);
336 /* Start from start of array, instead of the previously specified index - #48020 */
337 array_index = 0;
338 }
339 }
340
341 /* We should do at least one step. */
342 if (array_length == array_index) {
343 array_length++;
344 }
345
346 Main *bmain = CTX_data_main(C);
347 ReportList *reports = CTX_wm_reports(C);
348 Scene *scene = CTX_data_scene(C);
351 /* For each possible index, perform operation
352 * - Assume that array-length is greater than index. */
353 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
354 const AnimationEvalContext anim_eval_context = BKE_animsys_eval_context_construct(depsgraph,
355 frame);
356 int keyed_channels = 0;
357
358 CombinedKeyingResult combined_result;
359 for (; array_index < array_length; array_index++) {
360 if (mode == ModifyKeyMode::INSERT) {
361 const std::optional<StringRefNull> group = groupname ? std::optional(groupname) :
362 std::nullopt;
363 const std::optional<int> index = array_index >= 0 ? std::optional(array_index) :
364 std::nullopt;
365 PointerRNA id_rna_pointer = RNA_id_pointer_create(keyingset_path->id);
367 &id_rna_pointer,
368 group,
369 {{keyingset_path->rna_path, {}, index}},
370 std::nullopt,
371 anim_eval_context,
372 keytype,
373 path_insert_key_flags);
374 keyed_channels += result.get_count(SingleKeyingResult::SUCCESS);
375 combined_result.merge(result);
376 }
377 else if (mode == ModifyKeyMode::DELETE_KEY) {
378 RNAPath rna_path = {keyingset_path->rna_path, std::nullopt, array_index};
379 if (array_index < 0) {
380 rna_path.index = std::nullopt;
381 }
382 keyed_channels += delete_keyframe(bmain, reports, keyingset_path->id, rna_path, frame);
383 }
384 }
385
386 if (combined_result.get_count(SingleKeyingResult::SUCCESS) == 0) {
387 combined_result.generate_reports(reports);
388 }
389
390 switch (GS(keyingset_path->id->name)) {
391 case ID_OB: /* Object (or Object-Related) Keyframes */
392 {
393 Object *ob = reinterpret_cast<Object *>(keyingset_path->id);
394
395 /* XXX: only object transforms? */
397 break;
398 }
399 default:
401 break;
402 }
403
405
406 return keyed_channels;
407}
408
410 Vector<PointerRNA> *sources,
411 KeyingSet *keyingset,
412 const ModifyKeyMode mode,
413 const float cfra)
414{
415 if (keyingset == nullptr) {
416 return 0;
417 }
418
419 Scene *scene = CTX_data_scene(C);
420 const eInsertKeyFlags base_kflags = get_keyframing_flags(scene);
422 if (mode == ModifyKeyMode::INSERT) {
423 /* Use context settings as base. */
424 kflag = keyingset_apply_keying_flags(base_kflags,
426 eInsertKeyFlags(keyingset->keyingflag));
427 }
428 else if (mode == ModifyKeyMode::DELETE_KEY) {
429 kflag = INSERTKEY_NOFLAGS;
430 }
431
432 /* If relative Keying Sets, poll and build up the paths. */
433 {
434 const ModifyKeyReturn error = validate_keyingset(C, sources, keyingset);
435 if (error != ModifyKeyReturn::SUCCESS) {
436 BLI_assert(int(error) < 0);
437 return int(error);
438 }
439 }
440
441 Main *bmain = CTX_data_main(C);
442 ReportList *reports = CTX_wm_reports(C);
443 int keyed_channels = 0;
444
445 /* Apply the paths as specified in the KeyingSet now. */
446 for (KS_Path &keyingset_path : keyingset->paths) {
447 /* Skip path if no ID pointer is specified. */
448 if (keyingset_path.id == nullptr) {
449 BKE_reportf(reports,
451 "Skipping path in keying set, as it has no ID (KS = '%s', path = '%s[%d]')",
452 keyingset->name,
453 keyingset_path.rna_path,
454 keyingset_path.array_index);
455 continue;
456 }
457
458 if (!BKE_id_is_editable(bmain, keyingset_path.id)) {
459 continue;
460 }
461
462 keyed_channels += insert_key_to_keying_set_path(
463 C, &keyingset_path, keyingset, kflag, mode, cfra);
464 }
465
466 return keyed_channels;
467}
468
469} // namespace animrig
470} // namespace blender
Functions to insert, delete or modify keyframes.
Functionality to interact with keying sets.
#define C
Definition RandGen.cpp:29
#define NC_ANIMATION
Definition WM_types.hh:401
#define NA_ADDED
Definition WM_types.hh:636
#define ND_KEYFRAME
Definition WM_types.hh:537
#define APPLY_KEYINGFLAG_OVERRIDE(kflag)
#define ELEM(...)
#define STREQ(a, b)
void append(const T &value)
void merge(const CombinedKeyingResult &other)
int get_count(const SingleKeyingResult result) const
void generate_reports(ReportList *reports, eReportType report_level=RPT_ERROR)
#define offsetof(t, d)
void relative_keyingset_add_source(Vector< PointerRNA > &sources, ID *id, StructRNA *srna, void *data)
static int insert_key_to_keying_set_path(bContext *C, KS_Path *keyingset_path, KeyingSet *keyingset, const eInsertKeyFlags insert_key_flags, const ModifyKeyMode mode, const float frame)
KeyingSet * builtin_keyingset_get_named(const char name[])
KeyingSet * scene_get_active_keyingset(const Scene *scene)
static void RKS_ITER_overrides_list(KeyingSetInfo *keyingset_info, bContext *C, KeyingSet *keyingset, Vector< PointerRNA > &sources)
static eInsertKeyFlags keyingset_apply_keying_flags(const eInsertKeyFlags base_flags, const eInsertKeyFlags overrides, const eInsertKeyFlags own_flags)
KeyingSetInfo * keyingset_info_find_name(const char name[])
KeyingSet * get_keyingset_for_autokeying(const Scene *scene, const char *transformKSName)
int delete_keyframe(Main *bmain, ReportList *reports, ID *id, const RNAPath &rna_path, float cfra)
Main Delete Key-Framing API call.
ModifyKeyReturn validate_keyingset(bContext *C, Vector< PointerRNA > *sources, KeyingSet *keyingset)
void keyingset_info_register(KeyingSetInfo *keyingset_info)
void keyingset_info_unregister(Main *bmain, KeyingSetInfo *keyingset_info)
bool keyingset_find_id(KeyingSet *keyingset, ID *id)
bool is_keying_flag(const Scene *scene, eKeying_Flag flag)
int apply_keyingset(bContext *C, Vector< PointerRNA > *sources, KeyingSet *keyingset, ModifyKeyMode mode, float cfra)
eInsertKeyFlags get_keyframing_flags(Scene *scene)
CombinedKeyingResult insert_keyframes(Main *bmain, PointerRNA *struct_pointer, std::optional< StringRefNull > channel_group, const Span< RNAPath > rna_paths, std::optional< float > scene_frame, const AnimationEvalContext &anim_eval_context, eBezTriple_KeyframeType key_type, eInsertKeyFlags insert_key_flags)
Main key-frame insertion API.
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
void * BLI_findptr(const ListBase *listbase, const void *ptr, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:710
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
char * STRNCPY(char(&dst)[N], const char *src)
@ RPT_WARNING
Definition BKE_report.hh:40
PointerRNA RNA_id_pointer_create(ID *id)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
KeyingSet * BKE_keyingset_add(ListBaseT< KeyingSet > *list, const char idname[], const char name[], eKS_Settings flag, eInsertKeyFlags keyingflag)
Definition anim_sys.cc:137
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
void BKE_keyingset_free_paths(KeyingSet *ks)
Definition anim_sys.cc:273
AnimationEvalContext BKE_animsys_eval_context_construct(Depsgraph *depsgraph, float eval_time) ATTR_WARN_UNUSED_RESULT
Definition anim_sys.cc:602
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
static ListBaseT< KeyingSetInfo > keyingset_type_infos
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
ListBaseT< KeyingSet > builtin_keyingsets
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_ANIMATION_NO_FLUSH
Definition DNA_ID.h:1178
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
ID_Type GS(const char *name)
Definition DNA_ID.h:410
bool BLI_remlink_safe(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:161
Scene * CTX_data_scene(const bContext *C)
void BKE_keyingsets_free(ListBaseT< KeyingSet > *list)
Definition anim_sys.cc:289
@ AUTOKEY_FLAG_INSERTAVAILABLE
@ AUTOKEY_FLAG_ONLYKEYINGSET
ReportList * CTX_wm_reports(const bContext *C)
bool BKE_id_is_editable(const Main *bmain, const ID *id)
Definition lib_id.cc:2782
bool RNA_path_resolve_property(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
Definition rna_path.cc:578
@ KSP_FLAG_WHOLE_ARRAY
char name[258]
Definition DNA_ID.h:433
eKSP_Grouping groupmode
eInsertKeyFlags keyingflag
eInsertKeyFlags keyingoverride
eKSP_Settings flag
cbKeyingSet_Generate generate
cbKeyingSet_Iterator iter
eInsertKeyFlags keyingflag
eInsertKeyFlags keyingoverride
ListBaseT< KS_Path > paths
eInsertKeyFlags keyingflag
ListBaseT< Scene > scenes
Definition BKE_main.hh:386
ListBaseT< struct KeyingSet > keyingsets
struct ToolSettings * toolsettings