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_depsgraph.h"
52 #include "BKE_report.h"
54 #include "ED_keyframing.h"
55 #include "ED_screen.h"
57 #include "UI_interface.h"
58 #include "UI_resources.h"
63 #include "RNA_access.h"
64 #include "RNA_define.h"
65 #include "RNA_enum_types.h"
67 #include "anim_intern.h"
69 /* ************************************************** */
70 /* KEYING SETS - OPERATORS (for use in UI panels) */
71 /* These operators are really duplication of existing functionality, but just for completeness,
72 * they're here too, and will give the basic data needed...
75 /* poll callback for adding default KeyingSet */
76 static int keyingset_poll_default_add(bContext *C)
78 /* as long as there's an active Scene, it's fine */
79 return (CTX_data_scene(C) != NULL);
82 /* poll callback for editing active KeyingSet */
83 static int keyingset_poll_active_edit(bContext *C)
85 Scene *scene = CTX_data_scene(C);
90 /* there must be an active KeyingSet (and KeyingSets) */
91 return ((scene->active_keyingset > 0) && (scene->keyingsets.first));
94 /* poll callback for editing active KeyingSet Path */
95 static int keyingset_poll_activePath_edit(bContext *C)
97 Scene *scene = CTX_data_scene(C);
102 if (scene->active_keyingset <= 0)
105 ks = BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
107 /* there must be an active KeyingSet and an active path */
108 return ((ks) && (ks->paths.first) && (ks->active_path > 0));
112 /* Add a Default (Empty) Keying Set ------------------------- */
114 static int add_default_keyingset_exec(bContext *C, wmOperator *UNUSED(op))
116 Scene *scene = CTX_data_scene(C);
117 short flag = 0, keyingflag = 0;
120 * - absolute KeyingSets should be created by default
122 flag |= KEYINGSET_ABSOLUTE;
124 /* 2nd arg is 0 to indicate that we don't want to include autokeying mode related settings */
125 keyingflag = ANIM_get_keyframing_flags(scene, 0);
127 /* call the API func, and set the active keyingset index */
128 BKE_keyingset_add(&scene->keyingsets, NULL, NULL, flag, keyingflag);
130 scene->active_keyingset = BLI_listbase_count(&scene->keyingsets);
133 WM_event_add_notifier(C, NC_SCENE | ND_KEYINGSET, NULL);
135 return OPERATOR_FINISHED;
138 void ANIM_OT_keying_set_add(wmOperatorType *ot)
141 ot->name = "Add Empty Keying Set";
142 ot->idname = "ANIM_OT_keying_set_add";
143 ot->description = "Add a new (empty) Keying Set to the active Scene";
146 ot->exec = add_default_keyingset_exec;
147 ot->poll = keyingset_poll_default_add;
150 /* Remove 'Active' Keying Set ------------------------- */
152 static int remove_active_keyingset_exec(bContext *C, wmOperator *op)
154 Scene *scene = CTX_data_scene(C);
157 /* verify the Keying Set to use:
158 * - use the active one
159 * - return error if it doesn't exist
161 if (scene->active_keyingset == 0) {
162 BKE_report(op->reports, RPT_ERROR, "No active keying set to remove");
163 return OPERATOR_CANCELLED;
165 else if (scene->active_keyingset < 0) {
166 BKE_report(op->reports, RPT_ERROR, "Cannot remove built in keying set");
167 return OPERATOR_CANCELLED;
170 ks = BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
172 /* free KeyingSet's data, then remove it from the scene */
173 BKE_keyingset_free(ks);
174 BLI_freelinkN(&scene->keyingsets, ks);
176 /* the active one should now be the previously second-to-last one */
177 scene->active_keyingset--;
180 WM_event_add_notifier(C, NC_SCENE | ND_KEYINGSET, NULL);
182 return OPERATOR_FINISHED;
185 void ANIM_OT_keying_set_remove(wmOperatorType *ot)
188 ot->name = "Remove Active Keying Set";
189 ot->idname = "ANIM_OT_keying_set_remove";
190 ot->description = "Remove the active Keying Set";
193 ot->exec = remove_active_keyingset_exec;
194 ot->poll = keyingset_poll_active_edit;
197 /* Add Empty Keying Set Path ------------------------- */
199 static int add_empty_ks_path_exec(bContext *C, wmOperator *op)
201 Scene *scene = CTX_data_scene(C);
205 /* verify the Keying Set to use:
206 * - use the active one
207 * - return error if it doesn't exist
209 if (scene->active_keyingset == 0) {
210 BKE_report(op->reports, RPT_ERROR, "No active keying set to add empty path to");
211 return OPERATOR_CANCELLED;
214 ks = BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
216 /* don't use the API method for this, since that checks on values... */
217 ksp = MEM_callocN(sizeof(KS_Path), "KeyingSetPath Empty");
218 BLI_addtail(&ks->paths, ksp);
219 ks->active_path = BLI_listbase_count(&ks->paths);
221 ksp->groupmode = KSP_GROUP_KSNAME; // XXX?
223 ksp->flag = KSP_FLAG_WHOLE_ARRAY;
225 return OPERATOR_FINISHED;
228 void ANIM_OT_keying_set_path_add(wmOperatorType *ot)
231 ot->name = "Add Empty Keying Set Path";
232 ot->idname = "ANIM_OT_keying_set_path_add";
233 ot->description = "Add empty path to active Keying Set";
236 ot->exec = add_empty_ks_path_exec;
237 ot->poll = keyingset_poll_active_edit;
240 /* Remove Active Keying Set Path ------------------------- */
242 static int remove_active_ks_path_exec(bContext *C, wmOperator *op)
244 Scene *scene = CTX_data_scene(C);
245 KeyingSet *ks = BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
247 /* if there is a KeyingSet, find the nominated path to remove */
249 KS_Path *ksp = BLI_findlink(&ks->paths, ks->active_path - 1);
252 /* remove the active path from the KeyingSet */
253 BKE_keyingset_free_path(ks, ksp);
255 /* the active path should now be the previously second-to-last active one */
259 BKE_report(op->reports, RPT_ERROR, "No active keying set path to remove");
260 return OPERATOR_CANCELLED;
264 BKE_report(op->reports, RPT_ERROR, "No active keying set to remove a path from");
265 return OPERATOR_CANCELLED;
268 return OPERATOR_FINISHED;
271 void ANIM_OT_keying_set_path_remove(wmOperatorType *ot)
274 ot->name = "Remove Active Keying Set Path";
275 ot->idname = "ANIM_OT_keying_set_path_remove";
276 ot->description = "Remove active Path from active Keying Set";
279 ot->exec = remove_active_ks_path_exec;
280 ot->poll = keyingset_poll_activePath_edit;
283 /* ************************************************** */
284 /* KEYING SETS - OPERATORS (for use in UI menus) */
286 /* Add to KeyingSet Button Operator ------------------------ */
288 static int add_keyingset_button_exec(bContext *C, wmOperator *op)
290 Scene *scene = CTX_data_scene(C);
291 KeyingSet *ks = NULL;
292 PropertyRNA *prop = NULL;
293 PointerRNA ptr = {{NULL}};
296 int index = 0, pflag = 0;
297 const bool all = RNA_boolean_get(op->ptr, "all");
299 /* verify the Keying Set to use:
300 * - use the active one for now (more control over this can be added later)
301 * - add a new one if it doesn't exist
303 if (scene->active_keyingset == 0) {
304 short flag = 0, keyingflag = 0;
307 * - absolute KeyingSets should be created by default
309 flag |= KEYINGSET_ABSOLUTE;
311 keyingflag |= ANIM_get_keyframing_flags(scene, 0);
313 if (IS_AUTOKEY_FLAG(scene, XYZ2RGB))
314 keyingflag |= INSERTKEY_XYZ2RGB;
316 /* call the API func, and set the active keyingset index */
317 ks = BKE_keyingset_add(&scene->keyingsets, "ButtonKeyingSet", "Button Keying Set", flag, keyingflag);
319 scene->active_keyingset = BLI_listbase_count(&scene->keyingsets);
321 else if (scene->active_keyingset < 0) {
322 BKE_report(op->reports, RPT_ERROR, "Cannot add property to built in keying set");
323 return OPERATOR_CANCELLED;
326 ks = BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
329 /* try to add to keyingset using property retrieved from UI */
330 UI_context_active_but_prop_get(C, &ptr, &prop, &index);
332 /* check if property is able to be added */
333 if (ptr.id.data && ptr.data && prop && RNA_property_animateable(&ptr, prop)) {
334 path = RNA_path_from_ID_to_property(&ptr, prop);
339 pflag |= KSP_FLAG_WHOLE_ARRAY;
341 /* we need to set the index for this to 0, even though it may break in some cases, this is
342 * necessary if we want the entire array for most cases to get included without the user
343 * having to worry about where they clicked
348 /* add path to this setting */
349 BKE_keyingset_add_path(ks, ptr.id.data, NULL, path, index, pflag, KSP_GROUP_KSNAME);
350 ks->active_path = BLI_listbase_count(&ks->paths);
353 /* free the temp path created */
360 WM_event_add_notifier(C, NC_SCENE | ND_KEYINGSET, NULL);
362 /* show notification/report header, so that users notice that something changed */
363 BKE_reportf(op->reports, RPT_INFO, "Property added to Keying Set: '%s'", ks->name);
366 return (success) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
369 void ANIM_OT_keyingset_button_add(wmOperatorType *ot)
372 ot->name = "Add to Keying Set";
373 ot->idname = "ANIM_OT_keyingset_button_add";
374 ot->description = "Add current UI-active property to current keying set";
377 ot->exec = add_keyingset_button_exec;
381 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
384 RNA_def_boolean(ot->srna, "all", 1, "All", "Add all elements of the array to a Keying Set");
387 /* Remove from KeyingSet Button Operator ------------------------ */
389 static int remove_keyingset_button_exec(bContext *C, wmOperator *op)
391 Scene *scene = CTX_data_scene(C);
392 KeyingSet *ks = NULL;
393 PropertyRNA *prop = NULL;
394 PointerRNA ptr = {{NULL}};
399 /* verify the Keying Set to use:
400 * - use the active one for now (more control over this can be added later)
401 * - return error if it doesn't exist
403 if (scene->active_keyingset == 0) {
404 BKE_report(op->reports, RPT_ERROR, "No active keying set to remove property from");
405 return OPERATOR_CANCELLED;
407 else if (scene->active_keyingset < 0) {
408 BKE_report(op->reports, RPT_ERROR, "Cannot remove property from built in keying set");
409 return OPERATOR_CANCELLED;
412 ks = BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
415 /* try to add to keyingset using property retrieved from UI */
416 UI_context_active_but_prop_get(C, &ptr, &prop, &index);
418 if (ptr.id.data && ptr.data && prop) {
419 path = RNA_path_from_ID_to_property(&ptr, prop);
424 /* try to find a path matching this description */
425 ksp = BKE_keyingset_find_path(ks, ptr.id.data, ks->name, path, index, KSP_GROUP_KSNAME);
428 BKE_keyingset_free_path(ks, ksp);
432 /* free temp path used */
440 WM_event_add_notifier(C, NC_SCENE | ND_KEYINGSET, NULL);
443 BKE_report(op->reports, RPT_INFO, "Property removed from Keying Set");
446 return (success) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
449 void ANIM_OT_keyingset_button_remove(wmOperatorType *ot)
452 ot->name = "Remove from Keying Set";
453 ot->idname = "ANIM_OT_keyingset_button_remove";
454 ot->description = "Remove current UI-active property from current keying set";
457 ot->exec = remove_keyingset_button_exec;
461 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
464 /* ******************************************* */
466 /* Change Active KeyingSet Operator ------------------------ */
467 /* This operator checks if a menu should be shown for choosing the KeyingSet to make the active one */
469 static int keyingset_active_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
474 /* call the menu, which will call this operator again, hence the canceled */
475 pup = UI_popup_menu_begin(C, op->type->name, ICON_NONE);
476 layout = UI_popup_menu_layout(pup);
477 uiItemsEnumO(layout, "ANIM_OT_keying_set_active_set", "type");
478 UI_popup_menu_end(C, pup);
480 return OPERATOR_INTERFACE;
483 static int keyingset_active_menu_exec(bContext *C, wmOperator *op)
485 Scene *scene = CTX_data_scene(C);
486 int type = RNA_enum_get(op->ptr, "type");
488 /* If type == 0, it will deselect any active keying set. */
489 scene->active_keyingset = type;
492 WM_event_add_notifier(C, NC_SCENE | ND_KEYINGSET, NULL);
494 return OPERATOR_FINISHED;
497 void ANIM_OT_keying_set_active_set(wmOperatorType *ot)
502 ot->name = "Set Active Keying Set";
503 ot->idname = "ANIM_OT_keying_set_active_set";
504 ot->description = "Select a new keying set as the active one";
507 ot->invoke = keyingset_active_menu_invoke;
508 ot->exec = keyingset_active_menu_exec;
509 ot->poll = ED_operator_areaactive;
512 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
514 /* keyingset to use (dynamic enum) */
515 prop = RNA_def_enum(ot->srna, "type", DummyRNA_DEFAULT_items, 0, "Keying Set", "The Keying Set to use");
516 RNA_def_enum_funcs(prop, ANIM_keying_sets_enum_itemf);
517 /* RNA_def_property_flag(prop, PROP_HIDDEN);*/
520 /* ******************************************* */
521 /* REGISTERED KEYING SETS */
523 /* Keying Set Type Info declarations */
524 static ListBase keyingset_type_infos = {NULL, NULL};
526 /* Built-In Keying Sets (referencing type infos)*/
527 ListBase builtin_keyingsets = {NULL, NULL};
529 /* --------------- */
531 /* Find KeyingSet type info given a name */
532 KeyingSetInfo *ANIM_keyingset_info_find_name(const char name[])
535 if ((name == NULL) || (name[0] == 0))
538 /* search by comparing names */
539 return BLI_findstring(&keyingset_type_infos, name, offsetof(KeyingSetInfo, idname));
542 /* Find builtin KeyingSet by name */
543 KeyingSet *ANIM_builtin_keyingset_get_named(KeyingSet *prevKS, const char name[])
545 KeyingSet *ks, *first = NULL;
547 /* sanity checks any name to check? */
551 /* get first KeyingSet to use */
552 if (prevKS && prevKS->next)
553 first = prevKS->next;
555 first = builtin_keyingsets.first;
557 /* loop over KeyingSets checking names */
558 for (ks = first; ks; ks = ks->next) {
559 if (STREQ(name, ks->idname))
563 /* complain about missing keying sets on debug builds */
565 printf("%s: '%s' not found\n", __func__, name);
568 /* no matches found */
572 /* --------------- */
574 /* Add the given KeyingSetInfo to the list of type infos, and create an appropriate builtin set too */
575 void ANIM_keyingset_info_register(KeyingSetInfo *ksi)
579 /* create a new KeyingSet
580 * - inherit name and keyframing settings from the typeinfo
582 ks = BKE_keyingset_add(&builtin_keyingsets, ksi->idname, ksi->name, 1, ksi->keyingflag);
584 /* link this KeyingSet with its typeinfo */
585 memcpy(&ks->typeinfo, ksi->idname, sizeof(ks->typeinfo));
587 /* Copy description... */
588 BLI_strncpy(ks->description, ksi->description, sizeof(ks->description));
590 /* add type-info to the list */
591 BLI_addtail(&keyingset_type_infos, ksi);
594 /* Remove the given KeyingSetInfo from the list of type infos, and also remove the builtin set if appropriate */
595 void ANIM_keyingset_info_unregister(Main *bmain, KeyingSetInfo *ksi)
599 /* find relevant builtin KeyingSets which use this, and remove them */
600 /* TODO: this isn't done now, since unregister is really only used atm when we
601 * reload the scripts, which kindof defeats the purpose of "builtin"? */
602 for (ks = builtin_keyingsets.first; ks; ks = ksn) {
605 /* remove if matching typeinfo name */
606 if (STREQ(ks->typeinfo, ksi->idname)) {
608 BKE_keyingset_free(ks);
609 BLI_remlink(&builtin_keyingsets, ks);
611 for (scene = bmain->scene.first; scene; scene = scene->id.next)
612 BLI_remlink_safe(&scene->keyingsets, ks);
618 /* free the type info */
619 BLI_freelinkN(&keyingset_type_infos, ksi);
622 /* --------------- */
624 void ANIM_keyingset_infos_exit(void)
626 KeyingSetInfo *ksi, *next;
628 /* free type infos */
629 for (ksi = keyingset_type_infos.first; ksi; ksi = next) {
632 /* free extra RNA data, and remove from list */
634 ksi->ext.free(ksi->ext.data);
635 BLI_freelinkN(&keyingset_type_infos, ksi);
638 /* free builtin sets */
639 BKE_keyingsets_free(&builtin_keyingsets);
642 /* Check if the ID appears in the paths specified by the KeyingSet */
643 bool ANIM_keyingset_find_id(KeyingSet *ks, ID *id)
646 if (ELEM(NULL, ks, id))
649 return BLI_findptr(&ks->paths, id, offsetof(KS_Path, id)) != NULL;
652 /* ******************************************* */
653 /* KEYING SETS API (for UI) */
655 /* Getters for Active/Indices ----------------------------- */
657 /* Get the active Keying Set for the Scene provided */
658 KeyingSet *ANIM_scene_get_active_keyingset(Scene *scene)
660 /* if no scene, we've got no hope of finding the Keying Set */
664 /* currently, there are several possibilities here:
665 * - 0: no active keying set
666 * - > 0: one of the user-defined Keying Sets, but indices start from 0 (hence the -1)
667 * - < 0: a builtin keying set
669 if (scene->active_keyingset > 0)
670 return BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1);
672 return BLI_findlink(&builtin_keyingsets, (-scene->active_keyingset) - 1);
675 /* Get the index of the Keying Set provided, for the given Scene */
676 int ANIM_scene_get_keyingset_index(Scene *scene, KeyingSet *ks)
680 /* if no KeyingSet provided, have none */
684 /* check if the KeyingSet exists in scene list */
686 /* get index and if valid, return
687 * - (absolute) Scene KeyingSets are from (>= 1)
689 index = BLI_findindex(&scene->keyingsets, ks);
694 /* still here, so try builtins list too
695 * - builtins are from (<= -1)
696 * - none/invalid is (= 0)
698 index = BLI_findindex(&builtin_keyingsets, ks);
705 /* Get Keying Set to use for Auto-Keyframing some transforms */
706 KeyingSet *ANIM_get_keyingset_for_autokeying(Scene *scene, const char *tranformKSName)
708 /* get KeyingSet to use
709 * - use the active KeyingSet if defined (and user wants to use it for all autokeying),
710 * or otherwise key transforms only
712 if (IS_AUTOKEY_FLAG(scene, ONLYKEYINGSET) && (scene->active_keyingset))
713 return ANIM_scene_get_active_keyingset(scene);
714 else if (IS_AUTOKEY_FLAG(scene, INSERTAVAIL))
715 return ANIM_builtin_keyingset_get_named(NULL, ANIM_KS_AVAILABLE_ID);
717 return ANIM_builtin_keyingset_get_named(NULL, tranformKSName);
720 /* Menu of All Keying Sets ----------------------------- */
722 /* Dynamically populate an enum of Keying Sets */
723 EnumPropertyItem *ANIM_keying_sets_enum_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
725 Scene *scene = CTX_data_scene(C);
727 EnumPropertyItem *item = NULL, item_tmp = {0};
732 return DummyRNA_DEFAULT_items;
736 * - only include entry if it exists
738 if (scene->active_keyingset) {
739 /* active Keying Set */
740 item_tmp.identifier = "__ACTIVE__";
741 item_tmp.name = "Active Keying Set";
743 RNA_enum_item_add(&item, &totitem, &item_tmp);
746 RNA_enum_item_add_separator(&item, &totitem);
751 /* user-defined Keying Sets
752 * - these are listed in the order in which they were defined for the active scene
754 if (scene->keyingsets.first) {
755 for (ks = scene->keyingsets.first; ks; ks = ks->next, i++) {
756 if (ANIM_keyingset_context_ok_poll(C, ks)) {
757 item_tmp.identifier = ks->idname;
758 item_tmp.name = ks->name;
759 item_tmp.description = ks->description;
761 RNA_enum_item_add(&item, &totitem, &item_tmp);
766 RNA_enum_item_add_separator(&item, &totitem);
769 /* builtin Keying Sets */
771 for (ks = builtin_keyingsets.first; ks; ks = ks->next, i--) {
772 /* only show KeyingSet if context is suitable */
773 if (ANIM_keyingset_context_ok_poll(C, ks)) {
774 item_tmp.identifier = ks->idname;
775 item_tmp.name = ks->name;
776 item_tmp.description = ks->description;
778 RNA_enum_item_add(&item, &totitem, &item_tmp);
782 RNA_enum_item_end(&item, &totitem);
788 /* ******************************************* */
789 /* KEYFRAME MODIFICATION */
791 /* Polling API ----------------------------------------------- */
793 /* Check if KeyingSet can be used in the current context */
794 bool ANIM_keyingset_context_ok_poll(bContext *C, KeyingSet *ks)
796 if ((ks->flag & KEYINGSET_ABSOLUTE) == 0) {
797 KeyingSetInfo *ksi = ANIM_keyingset_info_find_name(ks->typeinfo);
799 /* get the associated 'type info' for this KeyingSet */
802 /* TODO: check for missing callbacks! */
804 /* check if it can be used in the current context */
805 return (ksi->poll(ksi, C));
811 /* Special 'Overrides' Iterator for Relative KeyingSets ------ */
813 /* 'Data Sources' for relative Keying Set 'overrides'
814 * - this is basically a wrapper for PointerRNA's in a linked list
815 * - do not allow this to be accessed from outside for now
817 typedef struct tRKS_DSource {
818 struct tRKS_DSource *next, *prev;
819 PointerRNA ptr; /* the whole point of this exercise! */
823 /* Iterator used for overriding the behavior of iterators defined for
824 * relative Keying Sets, with the main usage of this being operators
825 * requiring Auto Keyframing. Internal Use Only!
827 static void RKS_ITER_overrides_list(KeyingSetInfo *ksi, bContext *C, KeyingSet *ks, ListBase *dsources)
831 for (ds = dsources->first; ds; ds = ds->next) {
832 /* run generate callback on this data */
833 ksi->generate(ksi, C, ks, &ds->ptr);
837 /* Add new data source for relative Keying Sets */
838 void ANIM_relative_keyingset_add_source(ListBase *dsources, ID *id, StructRNA *srna, void *data)
843 * - we must have somewhere to output the data
844 * - we must have both srna+data (and with id too optionally), or id by itself only
846 if (dsources == NULL)
848 if (ELEM(NULL, srna, data) && (id == NULL))
851 /* allocate new elem, and add to the list */
852 ds = MEM_callocN(sizeof(tRKS_DSource), "tRKS_DSource");
853 BLI_addtail(dsources, ds);
855 /* depending on what data we have, create using ID or full pointer call */
857 RNA_pointer_create(id, srna, data, &ds->ptr);
859 RNA_id_pointer_create(id, &ds->ptr);
862 /* KeyingSet Operations (Insert/Delete Keyframes) ------------ */
864 /* Given a KeyingSet and context info, validate Keying Set's paths.
865 * This is only really necessary with relative/built-in KeyingSets
866 * where their list of paths is dynamically generated based on the
867 * current context info.
869 * Returns 0 if succeeded, otherwise an error code: eModifyKey_Returns
871 short ANIM_validate_keyingset(bContext *C, ListBase *dsources, KeyingSet *ks)
877 /* if relative Keying Sets, poll and build up the paths */
878 if ((ks->flag & KEYINGSET_ABSOLUTE) == 0) {
879 KeyingSetInfo *ksi = ANIM_keyingset_info_find_name(ks->typeinfo);
881 /* clear all existing paths
882 * NOTE: BKE_keyingset_free() frees all of the paths for the KeyingSet, but not the set itself
884 BKE_keyingset_free(ks);
886 /* get the associated 'type info' for this KeyingSet */
888 return MODIFYKEY_MISSING_TYPEINFO;
889 /* TODO: check for missing callbacks! */
891 /* check if it can be used in the current context */
892 if (ksi->poll(ksi, C)) {
893 /* if a list of data sources are provided, run a special iterator over them,
894 * otherwise, just continue per normal
897 RKS_ITER_overrides_list(ksi, C, ks, dsources);
899 ksi->iter(ksi, C, ks);
901 /* if we don't have any paths now, then this still qualifies as invalid context */
902 // FIXME: we need some error conditions (to be retrieved from the iterator why this failed!)
903 if (BLI_listbase_is_empty(&ks->paths))
904 return MODIFYKEY_INVALID_CONTEXT;
907 /* poll callback tells us that KeyingSet is useless in current context */
908 // FIXME: the poll callback needs to give us more info why
909 return MODIFYKEY_INVALID_CONTEXT;
913 /* succeeded; return 0 to tag error free */
917 /* Determine which keying flags apply based on the override flags */
918 static short keyingset_apply_keying_flags(const short base_flags, const short overrides, const short own_flags)
922 /* The logic for whether a keying flag applies is as follows:
923 * - If the flag in question is set in "overrides", that means that the
924 * status of that flag in "own_flags" is used
925 * - If however the flag isn't set, then its value in "base_flags" is used
926 * instead (i.e. no override)
928 #define APPLY_KEYINGFLAG_OVERRIDE(kflag) \
929 if (overrides & kflag) { \
930 result |= (own_flags & kflag); \
933 result |= (base_flags & kflag); \
936 /* Apply the flags one by one...
937 * (See rna_def_common_keying_flags() for the supported flags)
939 APPLY_KEYINGFLAG_OVERRIDE(INSERTKEY_NEEDED)
940 APPLY_KEYINGFLAG_OVERRIDE(INSERTKEY_MATRIX)
941 APPLY_KEYINGFLAG_OVERRIDE(INSERTKEY_XYZ2RGB)
943 #undef APPLY_KEYINGFLAG_OVERRIDE
948 /* Given a KeyingSet and context info (if required), modify keyframes for the channels specified
949 * by the KeyingSet. This takes into account many of the different combinations of using KeyingSets.
950 * Returns the number of channels that keyframes were added to
952 int ANIM_apply_keyingset(bContext *C, ListBase *dsources, bAction *act, KeyingSet *ks, short mode, float cfra)
954 Scene *scene = CTX_data_scene(C);
955 ReportList *reports = CTX_wm_reports(C);
957 const short base_kflags = ANIM_get_keyframing_flags(scene, 1);
958 const char *groupname = NULL;
959 short kflag = 0, success = 0;
960 char keytype = scene->toolsettings->keyframe_type;
966 /* get flags to use */
967 if (mode == MODIFYKEY_MODE_INSERT) {
968 /* use context settings as base */
969 kflag = keyingset_apply_keying_flags(base_kflags, ks->keyingoverride, ks->keyingflag);
971 else if (mode == MODIFYKEY_MODE_DELETE)
974 /* if relative Keying Sets, poll and build up the paths */
975 success = ANIM_validate_keyingset(C, dsources, ks);
978 /* return error code if failed */
982 /* apply the paths as specified in the KeyingSet now */
983 for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
987 /* skip path if no ID pointer is specified */
988 if (ksp->id == NULL) {
989 BKE_reportf(reports, RPT_WARNING,
990 "Skipping path in keying set, as it has no ID (KS = '%s', path = '%s[%d]')",
991 ks->name, ksp->rna_path, ksp->array_index);
995 /* since keying settings can be defined on the paths too, apply the settings for this path first */
996 kflag2 = keyingset_apply_keying_flags(kflag, ksp->keyingoverride, ksp->keyingflag);
998 /* get pointer to name of group to add channels to */
999 if (ksp->groupmode == KSP_GROUP_NONE)
1001 else if (ksp->groupmode == KSP_GROUP_KSNAME)
1002 groupname = ks->name;
1004 groupname = ksp->group;
1006 /* init arraylen and i - arraylen should be greater than i so that
1007 * normal non-array entries get keyframed correctly
1009 i = ksp->array_index;
1012 /* get length of array if whole array option is enabled */
1013 if (ksp->flag & KSP_FLAG_WHOLE_ARRAY) {
1014 PointerRNA id_ptr, ptr;
1017 RNA_id_pointer_create(ksp->id, &id_ptr);
1018 if (RNA_path_resolve_property(&id_ptr, ksp->rna_path, &ptr, &prop)) {
1019 arraylen = RNA_property_array_length(&ptr, prop);
1020 i = 0; /* start from start of array, instead of the previously specified index - T48020 */
1024 /* we should do at least one step */
1028 /* for each possible index, perform operation
1029 * - assume that arraylen is greater than index
1031 for (; i < arraylen; i++) {
1032 /* action to take depends on mode */
1033 if (mode == MODIFYKEY_MODE_INSERT)
1034 success += insert_keyframe(reports, ksp->id, act, groupname, ksp->rna_path, i, cfra, keytype, kflag2);
1035 else if (mode == MODIFYKEY_MODE_DELETE)
1036 success += delete_keyframe(reports, ksp->id, act, groupname, ksp->rna_path, i, cfra, kflag2);
1039 /* set recalc-flags */
1040 switch (GS(ksp->id->name)) {
1041 case ID_OB: /* Object (or Object-Related) Keyframes */
1043 Object *ob = (Object *)ksp->id;
1045 // XXX: only object transforms?
1046 DAG_id_tag_update(&ob->id, OB_RECALC_OB | OB_RECALC_DATA | OB_RECALC_TIME);
1051 /* send notifiers for updates (this doesn't require context to work!) */
1052 WM_main_add_notifier(NC_ANIMATION | ND_KEYFRAME | NA_ADDED, NULL);
1055 /* return the number of channels successfully affected */
1059 /* ************************************************** */