2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2009 Blender Foundation, Joshua Leung
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): Joshua Leung (full recode)
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/animation/keyingsets.c
29 * \ingroup edanimation
39 #include "MEM_guardedalloc.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_utildefines.h"
44 #include "DNA_anim_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_object_types.h"
49 #include "BKE_animsys.h"
50 #include "BKE_context.h"
51 #include "BKE_report.h"
53 #include "DEG_depsgraph.h"
55 #include "ED_keyframing.h"
56 #include "ED_screen.h"
58 #include "UI_interface.h"
59 #include "UI_resources.h"
64 #include "RNA_access.h"
65 #include "RNA_define.h"
66 #include "RNA_enum_types.h"
68 #include "anim_intern.h"
70 /* ************************************************** */
71 /* KEYING SETS - OPERATORS (for use in UI panels) */
72 /* These operators are really duplication of existing functionality, but just for completeness,
73 * they're here too, and will give the basic data needed...
76 /* poll callback for adding default KeyingSet */
77 static int keyingset_poll_default_add(bContext *C)
79 /* as long as there's an active Scene, it's fine */
80 return (CTX_data_scene(C) != NULL);
83 /* poll callback for editing active KeyingSet */
84 static int keyingset_poll_active_edit(bContext *C)
86 Scene *scene = CTX_data_scene(C);
91 /* there must be an active KeyingSet (and KeyingSets) */
92 return ((scene->active_keyingset > 0) && (scene->keyingsets.first));
95 /* poll callback for editing active KeyingSet Path */
96 static int keyingset_poll_activePath_edit(bContext *C)
98 Scene *scene = CTX_data_scene(C);
103 if (scene->active_keyingset <= 0)
106 ks = BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
108 /* there must be an active KeyingSet and an active path */
109 return ((ks) && (ks->paths.first) && (ks->active_path > 0));
113 /* Add a Default (Empty) Keying Set ------------------------- */
115 static int add_default_keyingset_exec(bContext *C, wmOperator *UNUSED(op))
117 Scene *scene = CTX_data_scene(C);
118 short flag = 0, keyingflag = 0;
121 * - absolute KeyingSets should be created by default
123 flag |= KEYINGSET_ABSOLUTE;
125 /* 2nd arg is 0 to indicate that we don't want to include autokeying mode related settings */
126 keyingflag = ANIM_get_keyframing_flags(scene, 0);
128 /* call the API func, and set the active keyingset index */
129 BKE_keyingset_add(&scene->keyingsets, NULL, NULL, flag, keyingflag);
131 scene->active_keyingset = BLI_listbase_count(&scene->keyingsets);
134 WM_event_add_notifier(C, NC_SCENE | ND_KEYINGSET, NULL);
136 return OPERATOR_FINISHED;
139 void ANIM_OT_keying_set_add(wmOperatorType *ot)
142 ot->name = "Add Empty Keying Set";
143 ot->idname = "ANIM_OT_keying_set_add";
144 ot->description = "Add a new (empty) Keying Set to the active Scene";
147 ot->exec = add_default_keyingset_exec;
148 ot->poll = keyingset_poll_default_add;
151 /* Remove 'Active' Keying Set ------------------------- */
153 static int remove_active_keyingset_exec(bContext *C, wmOperator *op)
155 Scene *scene = CTX_data_scene(C);
158 /* verify the Keying Set to use:
159 * - use the active one
160 * - return error if it doesn't exist
162 if (scene->active_keyingset == 0) {
163 BKE_report(op->reports, RPT_ERROR, "No active keying set to remove");
164 return OPERATOR_CANCELLED;
166 else if (scene->active_keyingset < 0) {
167 BKE_report(op->reports, RPT_ERROR, "Cannot remove built in keying set");
168 return OPERATOR_CANCELLED;
171 ks = BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
173 /* free KeyingSet's data, then remove it from the scene */
174 BKE_keyingset_free(ks);
175 BLI_freelinkN(&scene->keyingsets, ks);
177 /* the active one should now be the previously second-to-last one */
178 scene->active_keyingset--;
181 WM_event_add_notifier(C, NC_SCENE | ND_KEYINGSET, NULL);
183 return OPERATOR_FINISHED;
186 void ANIM_OT_keying_set_remove(wmOperatorType *ot)
189 ot->name = "Remove Active Keying Set";
190 ot->idname = "ANIM_OT_keying_set_remove";
191 ot->description = "Remove the active Keying Set";
194 ot->exec = remove_active_keyingset_exec;
195 ot->poll = keyingset_poll_active_edit;
198 /* Add Empty Keying Set Path ------------------------- */
200 static int add_empty_ks_path_exec(bContext *C, wmOperator *op)
202 Scene *scene = CTX_data_scene(C);
206 /* verify the Keying Set to use:
207 * - use the active one
208 * - return error if it doesn't exist
210 if (scene->active_keyingset == 0) {
211 BKE_report(op->reports, RPT_ERROR, "No active keying set to add empty path to");
212 return OPERATOR_CANCELLED;
215 ks = BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
217 /* don't use the API method for this, since that checks on values... */
218 ksp = MEM_callocN(sizeof(KS_Path), "KeyingSetPath Empty");
219 BLI_addtail(&ks->paths, ksp);
220 ks->active_path = BLI_listbase_count(&ks->paths);
222 ksp->groupmode = KSP_GROUP_KSNAME; // XXX?
224 ksp->flag = KSP_FLAG_WHOLE_ARRAY;
226 return OPERATOR_FINISHED;
229 void ANIM_OT_keying_set_path_add(wmOperatorType *ot)
232 ot->name = "Add Empty Keying Set Path";
233 ot->idname = "ANIM_OT_keying_set_path_add";
234 ot->description = "Add empty path to active Keying Set";
237 ot->exec = add_empty_ks_path_exec;
238 ot->poll = keyingset_poll_active_edit;
241 /* Remove Active Keying Set Path ------------------------- */
243 static int remove_active_ks_path_exec(bContext *C, wmOperator *op)
245 Scene *scene = CTX_data_scene(C);
246 KeyingSet *ks = BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
248 /* if there is a KeyingSet, find the nominated path to remove */
250 KS_Path *ksp = BLI_findlink(&ks->paths, ks->active_path - 1);
253 /* remove the active path from the KeyingSet */
254 BKE_keyingset_free_path(ks, ksp);
256 /* the active path should now be the previously second-to-last active one */
260 BKE_report(op->reports, RPT_ERROR, "No active keying set path to remove");
261 return OPERATOR_CANCELLED;
265 BKE_report(op->reports, RPT_ERROR, "No active keying set to remove a path from");
266 return OPERATOR_CANCELLED;
269 return OPERATOR_FINISHED;
272 void ANIM_OT_keying_set_path_remove(wmOperatorType *ot)
275 ot->name = "Remove Active Keying Set Path";
276 ot->idname = "ANIM_OT_keying_set_path_remove";
277 ot->description = "Remove active Path from active Keying Set";
280 ot->exec = remove_active_ks_path_exec;
281 ot->poll = keyingset_poll_activePath_edit;
284 /* ************************************************** */
285 /* KEYING SETS - OPERATORS (for use in UI menus) */
287 /* Add to KeyingSet Button Operator ------------------------ */
289 static int add_keyingset_button_exec(bContext *C, wmOperator *op)
291 Scene *scene = CTX_data_scene(C);
292 KeyingSet *ks = NULL;
293 PropertyRNA *prop = NULL;
294 PointerRNA ptr = {{NULL}};
297 int index = 0, pflag = 0;
298 const bool all = RNA_boolean_get(op->ptr, "all");
300 /* try to add to keyingset using property retrieved from UI */
301 if (!UI_context_active_but_prop_get(C, &ptr, &prop, &index)) {
302 /* pass event on if no active button found */
303 return (OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH);
306 /* verify the Keying Set to use:
307 * - use the active one for now (more control over this can be added later)
308 * - add a new one if it doesn't exist
310 if (scene->active_keyingset == 0) {
311 short flag = 0, keyingflag = 0;
314 * - absolute KeyingSets should be created by default
316 flag |= KEYINGSET_ABSOLUTE;
318 keyingflag |= ANIM_get_keyframing_flags(scene, 0);
320 if (IS_AUTOKEY_FLAG(scene, XYZ2RGB))
321 keyingflag |= INSERTKEY_XYZ2RGB;
323 /* call the API func, and set the active keyingset index */
324 ks = BKE_keyingset_add(&scene->keyingsets, "ButtonKeyingSet", "Button Keying Set", flag, keyingflag);
326 scene->active_keyingset = BLI_listbase_count(&scene->keyingsets);
328 else if (scene->active_keyingset < 0) {
329 BKE_report(op->reports, RPT_ERROR, "Cannot add property to built in keying set");
330 return OPERATOR_CANCELLED;
333 ks = BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
336 /* check if property is able to be added */
337 if (ptr.id.data && ptr.data && prop && RNA_property_animateable(&ptr, prop)) {
338 path = RNA_path_from_ID_to_property(&ptr, prop);
343 pflag |= KSP_FLAG_WHOLE_ARRAY;
345 /* we need to set the index for this to 0, even though it may break in some cases, this is
346 * necessary if we want the entire array for most cases to get included without the user
347 * having to worry about where they clicked
352 /* add path to this setting */
353 BKE_keyingset_add_path(ks, ptr.id.data, NULL, path, index, pflag, KSP_GROUP_KSNAME);
354 ks->active_path = BLI_listbase_count(&ks->paths);
357 /* free the temp path created */
364 WM_event_add_notifier(C, NC_SCENE | ND_KEYINGSET, NULL);
366 /* show notification/report header, so that users notice that something changed */
367 BKE_reportf(op->reports, RPT_INFO, "Property added to Keying Set: '%s'", ks->name);
370 return (success) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
373 void ANIM_OT_keyingset_button_add(wmOperatorType *ot)
376 ot->name = "Add to Keying Set";
377 ot->idname = "ANIM_OT_keyingset_button_add";
378 ot->description = "Add current UI-active property to current keying set";
381 ot->exec = add_keyingset_button_exec;
385 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
388 RNA_def_boolean(ot->srna, "all", 1, "All", "Add all elements of the array to a Keying Set");
391 /* Remove from KeyingSet Button Operator ------------------------ */
393 static int remove_keyingset_button_exec(bContext *C, wmOperator *op)
395 Scene *scene = CTX_data_scene(C);
396 KeyingSet *ks = NULL;
397 PropertyRNA *prop = NULL;
398 PointerRNA ptr = {{NULL}};
403 /* try to add to keyingset using property retrieved from UI */
404 if (UI_context_active_but_prop_get(C, &ptr, &prop, &index)) {
405 /* pass event on if no active button found */
406 return (OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH);
409 /* verify the Keying Set to use:
410 * - use the active one for now (more control over this can be added later)
411 * - return error if it doesn't exist
413 if (scene->active_keyingset == 0) {
414 BKE_report(op->reports, RPT_ERROR, "No active keying set to remove property from");
415 return OPERATOR_CANCELLED;
417 else if (scene->active_keyingset < 0) {
418 BKE_report(op->reports, RPT_ERROR, "Cannot remove property from built in keying set");
419 return OPERATOR_CANCELLED;
422 ks = BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
425 if (ptr.id.data && ptr.data && prop) {
426 path = RNA_path_from_ID_to_property(&ptr, prop);
431 /* try to find a path matching this description */
432 ksp = BKE_keyingset_find_path(ks, ptr.id.data, ks->name, path, index, KSP_GROUP_KSNAME);
435 BKE_keyingset_free_path(ks, ksp);
439 /* free temp path used */
447 WM_event_add_notifier(C, NC_SCENE | ND_KEYINGSET, NULL);
450 BKE_report(op->reports, RPT_INFO, "Property removed from Keying Set");
453 return (success) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
456 void ANIM_OT_keyingset_button_remove(wmOperatorType *ot)
459 ot->name = "Remove from Keying Set";
460 ot->idname = "ANIM_OT_keyingset_button_remove";
461 ot->description = "Remove current UI-active property from current keying set";
464 ot->exec = remove_keyingset_button_exec;
468 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
471 /* ******************************************* */
473 /* Change Active KeyingSet Operator ------------------------ */
474 /* This operator checks if a menu should be shown for choosing the KeyingSet to make the active one */
476 static int keyingset_active_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
481 /* call the menu, which will call this operator again, hence the canceled */
482 pup = UI_popup_menu_begin(C, op->type->name, ICON_NONE);
483 layout = UI_popup_menu_layout(pup);
484 uiItemsEnumO(layout, "ANIM_OT_keying_set_active_set", "type");
485 UI_popup_menu_end(C, pup);
487 return OPERATOR_INTERFACE;
490 static int keyingset_active_menu_exec(bContext *C, wmOperator *op)
492 Scene *scene = CTX_data_scene(C);
493 int type = RNA_enum_get(op->ptr, "type");
495 /* If type == 0, it will deselect any active keying set. */
496 scene->active_keyingset = type;
499 WM_event_add_notifier(C, NC_SCENE | ND_KEYINGSET, NULL);
501 return OPERATOR_FINISHED;
504 void ANIM_OT_keying_set_active_set(wmOperatorType *ot)
509 ot->name = "Set Active Keying Set";
510 ot->idname = "ANIM_OT_keying_set_active_set";
511 ot->description = "Select a new keying set as the active one";
514 ot->invoke = keyingset_active_menu_invoke;
515 ot->exec = keyingset_active_menu_exec;
516 ot->poll = ED_operator_areaactive;
519 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
521 /* keyingset to use (dynamic enum) */
522 prop = RNA_def_enum(ot->srna, "type", DummyRNA_DEFAULT_items, 0, "Keying Set", "The Keying Set to use");
523 RNA_def_enum_funcs(prop, ANIM_keying_sets_enum_itemf);
524 /* RNA_def_property_flag(prop, PROP_HIDDEN);*/
527 /* ******************************************* */
528 /* REGISTERED KEYING SETS */
530 /* Keying Set Type Info declarations */
531 static ListBase keyingset_type_infos = {NULL, NULL};
533 /* Built-In Keying Sets (referencing type infos)*/
534 ListBase builtin_keyingsets = {NULL, NULL};
536 /* --------------- */
538 /* Find KeyingSet type info given a name */
539 KeyingSetInfo *ANIM_keyingset_info_find_name(const char name[])
542 if ((name == NULL) || (name[0] == 0))
545 /* search by comparing names */
546 return BLI_findstring(&keyingset_type_infos, name, offsetof(KeyingSetInfo, idname));
549 /* Find builtin KeyingSet by name */
550 KeyingSet *ANIM_builtin_keyingset_get_named(KeyingSet *prevKS, const char name[])
552 KeyingSet *ks, *first = NULL;
554 /* sanity checks any name to check? */
558 /* get first KeyingSet to use */
559 if (prevKS && prevKS->next)
560 first = prevKS->next;
562 first = builtin_keyingsets.first;
564 /* loop over KeyingSets checking names */
565 for (ks = first; ks; ks = ks->next) {
566 if (STREQ(name, ks->idname))
570 /* complain about missing keying sets on debug builds */
572 printf("%s: '%s' not found\n", __func__, name);
575 /* no matches found */
579 /* --------------- */
581 /* Add the given KeyingSetInfo to the list of type infos, and create an appropriate builtin set too */
582 void ANIM_keyingset_info_register(KeyingSetInfo *ksi)
586 /* create a new KeyingSet
587 * - inherit name and keyframing settings from the typeinfo
589 ks = BKE_keyingset_add(&builtin_keyingsets, ksi->idname, ksi->name, 1, ksi->keyingflag);
591 /* link this KeyingSet with its typeinfo */
592 memcpy(&ks->typeinfo, ksi->idname, sizeof(ks->typeinfo));
594 /* Copy description... */
595 BLI_strncpy(ks->description, ksi->description, sizeof(ks->description));
597 /* add type-info to the list */
598 BLI_addtail(&keyingset_type_infos, ksi);
601 /* Remove the given KeyingSetInfo from the list of type infos, and also remove the builtin set if appropriate */
602 void ANIM_keyingset_info_unregister(Main *bmain, KeyingSetInfo *ksi)
606 /* find relevant builtin KeyingSets which use this, and remove them */
607 /* TODO: this isn't done now, since unregister is really only used atm when we
608 * reload the scripts, which kindof defeats the purpose of "builtin"? */
609 for (ks = builtin_keyingsets.first; ks; ks = ksn) {
612 /* remove if matching typeinfo name */
613 if (STREQ(ks->typeinfo, ksi->idname)) {
615 BKE_keyingset_free(ks);
616 BLI_remlink(&builtin_keyingsets, ks);
618 for (scene = bmain->scene.first; scene; scene = scene->id.next)
619 BLI_remlink_safe(&scene->keyingsets, ks);
625 /* free the type info */
626 BLI_freelinkN(&keyingset_type_infos, ksi);
629 /* --------------- */
631 void ANIM_keyingset_infos_exit(void)
633 KeyingSetInfo *ksi, *next;
635 /* free type infos */
636 for (ksi = keyingset_type_infos.first; ksi; ksi = next) {
639 /* free extra RNA data, and remove from list */
641 ksi->ext.free(ksi->ext.data);
642 BLI_freelinkN(&keyingset_type_infos, ksi);
645 /* free builtin sets */
646 BKE_keyingsets_free(&builtin_keyingsets);
649 /* Check if the ID appears in the paths specified by the KeyingSet */
650 bool ANIM_keyingset_find_id(KeyingSet *ks, ID *id)
653 if (ELEM(NULL, ks, id))
656 return BLI_findptr(&ks->paths, id, offsetof(KS_Path, id)) != NULL;
659 /* ******************************************* */
660 /* KEYING SETS API (for UI) */
662 /* Getters for Active/Indices ----------------------------- */
664 /* Get the active Keying Set for the Scene provided */
665 KeyingSet *ANIM_scene_get_active_keyingset(Scene *scene)
667 /* if no scene, we've got no hope of finding the Keying Set */
671 /* currently, there are several possibilities here:
672 * - 0: no active keying set
673 * - > 0: one of the user-defined Keying Sets, but indices start from 0 (hence the -1)
674 * - < 0: a builtin keying set
676 if (scene->active_keyingset > 0)
677 return BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
679 return BLI_findlink(&builtin_keyingsets, (-scene->active_keyingset) - 1);
682 /* Get the index of the Keying Set provided, for the given Scene */
683 int ANIM_scene_get_keyingset_index(Scene *scene, KeyingSet *ks)
687 /* if no KeyingSet provided, have none */
691 /* check if the KeyingSet exists in scene list */
693 /* get index and if valid, return
694 * - (absolute) Scene KeyingSets are from (>= 1)
696 index = BLI_findindex(&scene->keyingsets, ks);
701 /* still here, so try builtins list too
702 * - builtins are from (<= -1)
703 * - none/invalid is (= 0)
705 index = BLI_findindex(&builtin_keyingsets, ks);
712 /* Get Keying Set to use for Auto-Keyframing some transforms */
713 KeyingSet *ANIM_get_keyingset_for_autokeying(Scene *scene, const char *tranformKSName)
715 /* get KeyingSet to use
716 * - use the active KeyingSet if defined (and user wants to use it for all autokeying),
717 * or otherwise key transforms only
719 if (IS_AUTOKEY_FLAG(scene, ONLYKEYINGSET) && (scene->active_keyingset))
720 return ANIM_scene_get_active_keyingset(scene);
721 else if (IS_AUTOKEY_FLAG(scene, INSERTAVAIL))
722 return ANIM_builtin_keyingset_get_named(NULL, ANIM_KS_AVAILABLE_ID);
724 return ANIM_builtin_keyingset_get_named(NULL, tranformKSName);
727 /* Menu of All Keying Sets ----------------------------- */
729 /* Dynamically populate an enum of Keying Sets */
730 EnumPropertyItem *ANIM_keying_sets_enum_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
732 Scene *scene = CTX_data_scene(C);
734 EnumPropertyItem *item = NULL, item_tmp = {0};
739 return DummyRNA_DEFAULT_items;
743 * - only include entry if it exists
745 if (scene->active_keyingset) {
746 /* active Keying Set */
747 item_tmp.identifier = "__ACTIVE__";
748 item_tmp.name = "Active Keying Set";
750 RNA_enum_item_add(&item, &totitem, &item_tmp);
753 RNA_enum_item_add_separator(&item, &totitem);
758 /* user-defined Keying Sets
759 * - these are listed in the order in which they were defined for the active scene
761 if (scene->keyingsets.first) {
762 for (ks = scene->keyingsets.first; ks; ks = ks->next, i++) {
763 if (ANIM_keyingset_context_ok_poll(C, ks)) {
764 item_tmp.identifier = ks->idname;
765 item_tmp.name = ks->name;
766 item_tmp.description = ks->description;
768 RNA_enum_item_add(&item, &totitem, &item_tmp);
773 RNA_enum_item_add_separator(&item, &totitem);
776 /* builtin Keying Sets */
778 for (ks = builtin_keyingsets.first; ks; ks = ks->next, i--) {
779 /* only show KeyingSet if context is suitable */
780 if (ANIM_keyingset_context_ok_poll(C, ks)) {
781 item_tmp.identifier = ks->idname;
782 item_tmp.name = ks->name;
783 item_tmp.description = ks->description;
785 RNA_enum_item_add(&item, &totitem, &item_tmp);
789 RNA_enum_item_end(&item, &totitem);
795 /* ******************************************* */
796 /* KEYFRAME MODIFICATION */
798 /* Polling API ----------------------------------------------- */
800 /* Check if KeyingSet can be used in the current context */
801 bool ANIM_keyingset_context_ok_poll(bContext *C, KeyingSet *ks)
803 if ((ks->flag & KEYINGSET_ABSOLUTE) == 0) {
804 KeyingSetInfo *ksi = ANIM_keyingset_info_find_name(ks->typeinfo);
806 /* get the associated 'type info' for this KeyingSet */
809 /* TODO: check for missing callbacks! */
811 /* check if it can be used in the current context */
812 return (ksi->poll(ksi, C));
818 /* Special 'Overrides' Iterator for Relative KeyingSets ------ */
820 /* 'Data Sources' for relative Keying Set 'overrides'
821 * - this is basically a wrapper for PointerRNA's in a linked list
822 * - do not allow this to be accessed from outside for now
824 typedef struct tRKS_DSource {
825 struct tRKS_DSource *next, *prev;
826 PointerRNA ptr; /* the whole point of this exercise! */
830 /* Iterator used for overriding the behavior of iterators defined for
831 * relative Keying Sets, with the main usage of this being operators
832 * requiring Auto Keyframing. Internal Use Only!
834 static void RKS_ITER_overrides_list(KeyingSetInfo *ksi, bContext *C, KeyingSet *ks, ListBase *dsources)
838 for (ds = dsources->first; ds; ds = ds->next) {
839 /* run generate callback on this data */
840 ksi->generate(ksi, C, ks, &ds->ptr);
844 /* Add new data source for relative Keying Sets */
845 void ANIM_relative_keyingset_add_source(ListBase *dsources, ID *id, StructRNA *srna, void *data)
850 * - we must have somewhere to output the data
851 * - we must have both srna+data (and with id too optionally), or id by itself only
853 if (dsources == NULL)
855 if (ELEM(NULL, srna, data) && (id == NULL))
858 /* allocate new elem, and add to the list */
859 ds = MEM_callocN(sizeof(tRKS_DSource), "tRKS_DSource");
860 BLI_addtail(dsources, ds);
862 /* depending on what data we have, create using ID or full pointer call */
864 RNA_pointer_create(id, srna, data, &ds->ptr);
866 RNA_id_pointer_create(id, &ds->ptr);
869 /* KeyingSet Operations (Insert/Delete Keyframes) ------------ */
871 /* Given a KeyingSet and context info, validate Keying Set's paths.
872 * This is only really necessary with relative/built-in KeyingSets
873 * where their list of paths is dynamically generated based on the
874 * current context info.
876 * Returns 0 if succeeded, otherwise an error code: eModifyKey_Returns
878 short ANIM_validate_keyingset(bContext *C, ListBase *dsources, KeyingSet *ks)
884 /* if relative Keying Sets, poll and build up the paths */
885 if ((ks->flag & KEYINGSET_ABSOLUTE) == 0) {
886 KeyingSetInfo *ksi = ANIM_keyingset_info_find_name(ks->typeinfo);
888 /* clear all existing paths
889 * NOTE: BKE_keyingset_free() frees all of the paths for the KeyingSet, but not the set itself
891 BKE_keyingset_free(ks);
893 /* get the associated 'type info' for this KeyingSet */
895 return MODIFYKEY_MISSING_TYPEINFO;
896 /* TODO: check for missing callbacks! */
898 /* check if it can be used in the current context */
899 if (ksi->poll(ksi, C)) {
900 /* if a list of data sources are provided, run a special iterator over them,
901 * otherwise, just continue per normal
904 RKS_ITER_overrides_list(ksi, C, ks, dsources);
906 ksi->iter(ksi, C, ks);
908 /* if we don't have any paths now, then this still qualifies as invalid context */
909 // FIXME: we need some error conditions (to be retrieved from the iterator why this failed!)
910 if (BLI_listbase_is_empty(&ks->paths))
911 return MODIFYKEY_INVALID_CONTEXT;
914 /* poll callback tells us that KeyingSet is useless in current context */
915 // FIXME: the poll callback needs to give us more info why
916 return MODIFYKEY_INVALID_CONTEXT;
920 /* succeeded; return 0 to tag error free */
924 /* Determine which keying flags apply based on the override flags */
925 static short keyingset_apply_keying_flags(const short base_flags, const short overrides, const short own_flags)
929 /* The logic for whether a keying flag applies is as follows:
930 * - If the flag in question is set in "overrides", that means that the
931 * status of that flag in "own_flags" is used
932 * - If however the flag isn't set, then its value in "base_flags" is used
933 * instead (i.e. no override)
935 #define APPLY_KEYINGFLAG_OVERRIDE(kflag) \
936 if (overrides & kflag) { \
937 result |= (own_flags & kflag); \
940 result |= (base_flags & kflag); \
943 /* Apply the flags one by one...
944 * (See rna_def_common_keying_flags() for the supported flags)
946 APPLY_KEYINGFLAG_OVERRIDE(INSERTKEY_NEEDED)
947 APPLY_KEYINGFLAG_OVERRIDE(INSERTKEY_MATRIX)
948 APPLY_KEYINGFLAG_OVERRIDE(INSERTKEY_XYZ2RGB)
950 #undef APPLY_KEYINGFLAG_OVERRIDE
955 /* Given a KeyingSet and context info (if required), modify keyframes for the channels specified
956 * by the KeyingSet. This takes into account many of the different combinations of using KeyingSets.
957 * Returns the number of channels that keyframes were added to
959 int ANIM_apply_keyingset(bContext *C, ListBase *dsources, bAction *act, KeyingSet *ks, short mode, float cfra)
961 Scene *scene = CTX_data_scene(C);
962 ReportList *reports = CTX_wm_reports(C);
964 const short base_kflags = ANIM_get_keyframing_flags(scene, 1);
965 const char *groupname = NULL;
966 short kflag = 0, success = 0;
967 char keytype = scene->toolsettings->keyframe_type;
973 /* get flags to use */
974 if (mode == MODIFYKEY_MODE_INSERT) {
975 /* use context settings as base */
976 kflag = keyingset_apply_keying_flags(base_kflags, ks->keyingoverride, ks->keyingflag);
978 else if (mode == MODIFYKEY_MODE_DELETE)
981 /* if relative Keying Sets, poll and build up the paths */
982 success = ANIM_validate_keyingset(C, dsources, ks);
985 /* return error code if failed */
989 /* apply the paths as specified in the KeyingSet now */
990 for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
994 /* skip path if no ID pointer is specified */
995 if (ksp->id == NULL) {
996 BKE_reportf(reports, RPT_WARNING,
997 "Skipping path in keying set, as it has no ID (KS = '%s', path = '%s[%d]')",
998 ks->name, ksp->rna_path, ksp->array_index);
1002 /* since keying settings can be defined on the paths too, apply the settings for this path first */
1003 kflag2 = keyingset_apply_keying_flags(kflag, ksp->keyingoverride, ksp->keyingflag);
1005 /* get pointer to name of group to add channels to */
1006 if (ksp->groupmode == KSP_GROUP_NONE)
1008 else if (ksp->groupmode == KSP_GROUP_KSNAME)
1009 groupname = ks->name;
1011 groupname = ksp->group;
1013 /* init arraylen and i - arraylen should be greater than i so that
1014 * normal non-array entries get keyframed correctly
1016 i = ksp->array_index;
1019 /* get length of array if whole array option is enabled */
1020 if (ksp->flag & KSP_FLAG_WHOLE_ARRAY) {
1021 PointerRNA id_ptr, ptr;
1024 RNA_id_pointer_create(ksp->id, &id_ptr);
1025 if (RNA_path_resolve_property(&id_ptr, ksp->rna_path, &ptr, &prop)) {
1026 arraylen = RNA_property_array_length(&ptr, prop);
1027 i = 0; /* start from start of array, instead of the previously specified index - T48020 */
1031 /* we should do at least one step */
1035 /* for each possible index, perform operation
1036 * - assume that arraylen is greater than index
1038 for (; i < arraylen; i++) {
1039 /* action to take depends on mode */
1040 if (mode == MODIFYKEY_MODE_INSERT)
1041 success += insert_keyframe(reports, ksp->id, act, groupname, ksp->rna_path, i, cfra, keytype, kflag2);
1042 else if (mode == MODIFYKEY_MODE_DELETE)
1043 success += delete_keyframe(reports, ksp->id, act, groupname, ksp->rna_path, i, cfra, kflag2);
1046 /* set recalc-flags */
1047 switch (GS(ksp->id->name)) {
1048 case ID_OB: /* Object (or Object-Related) Keyframes */
1050 Object *ob = (Object *)ksp->id;
1052 // XXX: only object transforms?
1053 DEG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1060 /* send notifiers for updates (this doesn't require context to work!) */
1061 WM_main_add_notifier(NC_ANIMATION | ND_KEYFRAME | NA_ADDED, NULL);
1064 /* return the number of channels successfully affected */
1068 /* ************************************************** */