4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2009 Blender Foundation, Joshua Leung
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): Joshua Leung (full recode)
27 * ***** END GPL LICENSE BLOCK *****
30 /** \file blender/editors/animation/keyingsets.c
31 * \ingroup edanimation
41 #include "MEM_guardedalloc.h"
43 #include "BLI_blenlib.h"
45 #include "BLI_dynstr.h"
46 #include "BLI_utildefines.h"
48 #include "DNA_anim_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_object_types.h"
53 #include "BKE_animsys.h"
54 #include "BKE_context.h"
55 #include "BKE_depsgraph.h"
56 #include "BKE_report.h"
58 #include "ED_keyframing.h"
59 #include "ED_screen.h"
61 #include "UI_interface.h"
62 #include "UI_resources.h"
67 #include "RNA_access.h"
68 #include "RNA_define.h"
69 #include "RNA_enum_types.h"
71 #include "anim_intern.h"
73 /* ************************************************** */
74 /* KEYING SETS - OPERATORS (for use in UI panels) */
75 /* These operators are really duplication of existing functionality, but just for completeness,
76 * they're here too, and will give the basic data needed...
79 /* poll callback for adding default KeyingSet */
80 static int keyingset_poll_default_add (bContext *C)
82 /* as long as there's an active Scene, it's fine */
83 return (CTX_data_scene(C) != NULL);
86 /* poll callback for editing active KeyingSet */
87 static int keyingset_poll_active_edit (bContext *C)
89 Scene *scene= CTX_data_scene(C);
94 /* there must be an active KeyingSet (and KeyingSets) */
95 return ((scene->active_keyingset > 0) && (scene->keyingsets.first));
98 /* poll callback for editing active KeyingSet Path */
99 static int keyingset_poll_activePath_edit (bContext *C)
101 Scene *scene= CTX_data_scene(C);
106 if (scene->active_keyingset <= 0)
109 ks= BLI_findlink(&scene->keyingsets, scene->active_keyingset-1);
111 /* there must be an active KeyingSet and an active path */
112 return ((ks) && (ks->paths.first) && (ks->active_path > 0));
116 /* Add a Default (Empty) Keying Set ------------------------- */
118 static int add_default_keyingset_exec (bContext *C, wmOperator *UNUSED(op))
120 Scene *scene= CTX_data_scene(C);
121 short flag=0, keyingflag=0;
124 * - absolute KeyingSets should be created by default
126 flag |= KEYINGSET_ABSOLUTE;
128 /* 2nd arg is 0 to indicate that we don't want to include autokeying mode related settings */
129 keyingflag = ANIM_get_keyframing_flags(scene, 0);
131 /* call the API func, and set the active keyingset index */
132 BKE_keyingset_add(&scene->keyingsets, NULL, flag, keyingflag);
134 scene->active_keyingset= BLI_countlist(&scene->keyingsets);
137 WM_event_add_notifier(C, NC_SCENE|ND_KEYINGSET, NULL);
139 return OPERATOR_FINISHED;
142 void ANIM_OT_keying_set_add (wmOperatorType *ot)
145 ot->name= "Add Empty Keying Set";
146 ot->idname= "ANIM_OT_keying_set_add";
147 ot->description= "Add a new (empty) Keying Set to the active Scene";
150 ot->exec= add_default_keyingset_exec;
151 ot->poll= keyingset_poll_default_add;
154 /* Remove 'Active' Keying Set ------------------------- */
156 static int remove_active_keyingset_exec (bContext *C, wmOperator *op)
158 Scene *scene= CTX_data_scene(C);
161 /* verify the Keying Set to use:
162 * - use the active one
163 * - return error if it doesn't exist
165 if (scene->active_keyingset == 0) {
166 BKE_report(op->reports, RPT_ERROR, "No active Keying Set to remove");
167 return OPERATOR_CANCELLED;
169 else if (scene->active_keyingset < 0) {
170 BKE_report(op->reports, RPT_ERROR, "Cannot remove built in Keying Set");
171 return OPERATOR_CANCELLED;
174 ks= BLI_findlink(&scene->keyingsets, scene->active_keyingset-1);
176 /* free KeyingSet's data, then remove it from the scene */
177 BKE_keyingset_free(ks);
178 BLI_freelinkN(&scene->keyingsets, ks);
180 /* the active one should now be the previously second-to-last one */
181 scene->active_keyingset--;
184 WM_event_add_notifier(C, NC_SCENE|ND_KEYINGSET, NULL);
186 return OPERATOR_FINISHED;
189 void ANIM_OT_keying_set_remove (wmOperatorType *ot)
192 ot->name= "Removed Active Keying Set";
193 ot->idname= "ANIM_OT_keying_set_remove";
194 ot->description= "Remove the active Keying Set";
197 ot->exec= remove_active_keyingset_exec;
198 ot->poll= keyingset_poll_active_edit;
201 /* Add Empty Keying Set Path ------------------------- */
203 static int add_empty_ks_path_exec (bContext *C, wmOperator *op)
205 Scene *scene= CTX_data_scene(C);
209 /* verify the Keying Set to use:
210 * - use the active one
211 * - return error if it doesn't exist
213 if (scene->active_keyingset == 0) {
214 BKE_report(op->reports, RPT_ERROR, "No active Keying Set to add empty path to");
215 return OPERATOR_CANCELLED;
218 ks= BLI_findlink(&scene->keyingsets, scene->active_keyingset-1);
220 /* don't use the API method for this, since that checks on values... */
221 ksp= MEM_callocN(sizeof(KS_Path), "KeyingSetPath Empty");
222 BLI_addtail(&ks->paths, ksp);
223 ks->active_path= BLI_countlist(&ks->paths);
225 ksp->groupmode= KSP_GROUP_KSNAME; // XXX?
228 return OPERATOR_FINISHED;
231 void ANIM_OT_keying_set_path_add (wmOperatorType *ot)
234 ot->name= "Add Empty Keying Set Path";
235 ot->idname= "ANIM_OT_keying_set_path_add";
236 ot->description= "Add empty path to active Keying Set";
239 ot->exec= add_empty_ks_path_exec;
240 ot->poll= keyingset_poll_active_edit;
243 /* Remove Active Keying Set Path ------------------------- */
245 static int remove_active_ks_path_exec (bContext *C, wmOperator *op)
247 Scene *scene= CTX_data_scene(C);
248 KeyingSet *ks= BLI_findlink(&scene->keyingsets, scene->active_keyingset-1);
250 /* if there is a KeyingSet, find the nominated path to remove */
252 KS_Path *ksp= BLI_findlink(&ks->paths, ks->active_path-1);
255 /* remove the active path from the KeyingSet */
256 BKE_keyingset_free_path(ks, ksp);
258 /* the active path should now be the previously second-to-last active one */
262 BKE_report(op->reports, RPT_ERROR, "No active Keying Set Path to remove");
263 return OPERATOR_CANCELLED;
267 BKE_report(op->reports, RPT_ERROR, "No active Keying Set to remove a path from");
268 return OPERATOR_CANCELLED;
271 return OPERATOR_FINISHED;
274 void ANIM_OT_keying_set_path_remove (wmOperatorType *ot)
277 ot->name= "Remove Active Keying Set Path";
278 ot->idname= "ANIM_OT_keying_set_path_remove";
279 ot->description= "Remove active Path from active Keying Set";
282 ot->exec= remove_active_ks_path_exec;
283 ot->poll= keyingset_poll_activePath_edit;
286 /* ************************************************** */
287 /* KEYING SETS - OPERATORS (for use in UI menus) */
289 /* Add to KeyingSet Button Operator ------------------------ */
291 static int add_keyingset_button_exec (bContext *C, wmOperator *op)
293 Main *bmain= CTX_data_main(C);
294 Scene *scene= CTX_data_scene(C);
295 KeyingSet *ks = NULL;
296 PropertyRNA *prop= NULL;
297 PointerRNA ptr= {{NULL}};
300 int index=0, pflag=0;
301 int all= RNA_boolean_get(op->ptr, "all");
303 /* verify the Keying Set to use:
304 * - use the active one for now (more control over this can be added later)
305 * - add a new one if it doesn't exist
307 if (scene->active_keyingset == 0) {
308 short flag=0, keyingflag=0;
311 * - absolute KeyingSets should be created by default
313 flag |= KEYINGSET_ABSOLUTE;
315 keyingflag |= ANIM_get_keyframing_flags(scene, 0);
317 if (IS_AUTOKEY_FLAG(scene, XYZ2RGB))
318 keyingflag |= INSERTKEY_XYZ2RGB;
320 /* call the API func, and set the active keyingset index */
321 ks= BKE_keyingset_add(&scene->keyingsets, "ButtonKeyingSet", flag, keyingflag);
323 scene->active_keyingset= BLI_countlist(&scene->keyingsets);
325 else if (scene->active_keyingset < 0) {
326 BKE_report(op->reports, RPT_ERROR, "Cannot add property to built in Keying Set");
327 return OPERATOR_CANCELLED;
330 ks= BLI_findlink(&scene->keyingsets, scene->active_keyingset-1);
332 /* try to add to keyingset using property retrieved from UI */
333 uiContextActiveProperty(C, &ptr, &prop, &index);
335 /* check if property is able to be added */
336 if (ptr.id.data && ptr.data && prop && RNA_property_animateable(&ptr, prop)) {
337 path= RNA_path_from_ID_to_property(&ptr, prop);
342 pflag |= KSP_FLAG_WHOLE_ARRAY;
344 /* we need to set the index for this to 0, even though it may break in some cases, this is
345 * necessary if we want the entire array for most cases to get included without the user
346 * having to worry about where they clicked
351 /* add path to this setting */
352 BKE_keyingset_add_path(ks, ptr.id.data, NULL, path, index, pflag, KSP_GROUP_KSNAME);
353 ks->active_path= BLI_countlist(&ks->paths);
356 /* free the temp path created */
363 DAG_ids_flush_update(bmain, 0);
365 /* for now, only send ND_KEYS for KeyingSets */
366 WM_event_add_notifier(C, NC_SCENE|ND_KEYINGSET, NULL);
369 return (success)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
372 void ANIM_OT_keyingset_button_add (wmOperatorType *ot)
375 ot->name= "Add to Keying Set";
376 ot->idname= "ANIM_OT_keyingset_button_add";
379 ot->exec= add_keyingset_button_exec;
383 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
386 RNA_def_boolean(ot->srna, "all", 1, "All", "Add all elements of the array to a Keying Set.");
389 /* Remove from KeyingSet Button Operator ------------------------ */
391 static int remove_keyingset_button_exec (bContext *C, wmOperator *op)
393 Main *bmain= CTX_data_main(C);
394 Scene *scene= CTX_data_scene(C);
395 KeyingSet *ks = NULL;
396 PropertyRNA *prop= NULL;
397 PointerRNA ptr= {{NULL}};
402 /* verify the Keying Set to use:
403 * - use the active one for now (more control over this can be added later)
404 * - return error if it doesn't exist
406 if (scene->active_keyingset == 0) {
407 BKE_report(op->reports, RPT_ERROR, "No active Keying Set to remove property from");
408 return OPERATOR_CANCELLED;
410 else if (scene->active_keyingset < 0) {
411 BKE_report(op->reports, RPT_ERROR, "Cannot remove property from built in Keying Set");
412 return OPERATOR_CANCELLED;
415 ks= BLI_findlink(&scene->keyingsets, scene->active_keyingset-1);
417 /* try to add to keyingset using property retrieved from UI */
418 uiContextActiveProperty(C, &ptr, &prop, &index);
420 if (ptr.id.data && ptr.data && prop) {
421 path= RNA_path_from_ID_to_property(&ptr, prop);
426 /* try to find a path matching this description */
427 ksp= BKE_keyingset_find_path(ks, ptr.id.data, ks->name, path, index, KSP_GROUP_KSNAME);
430 BKE_keyingset_free_path(ks, ksp);
434 /* free temp path used */
442 DAG_ids_flush_update(bmain, 0);
444 /* for now, only send ND_KEYS for KeyingSets */
445 WM_event_add_notifier(C, NC_SCENE|ND_KEYINGSET, NULL);
448 return (success)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
451 void ANIM_OT_keyingset_button_remove (wmOperatorType *ot)
454 ot->name= "Remove from Keying Set";
455 ot->idname= "ANIM_OT_keyingset_button_remove";
458 ot->exec= remove_keyingset_button_exec;
462 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
465 /* ******************************************* */
467 /* Change Active KeyingSet Operator ------------------------ */
468 /* This operator checks if a menu should be shown for choosing the KeyingSet to make the active one */
470 static int keyingset_active_menu_invoke (bContext *C, wmOperator *op, wmEvent *UNUSED(event))
472 /* call the menu, which will call this operator again, hence the cancelled */
473 ANIM_keying_sets_menu_setup(C, op->type->name, "ANIM_OT_keying_set_active_set");
474 return OPERATOR_CANCELLED;
477 static int keyingset_active_menu_exec (bContext *C, wmOperator *op)
479 Scene *scene= CTX_data_scene(C);
480 int type= RNA_int_get(op->ptr, "type");
482 /* simply set the scene's active keying set index, unless the type == 0
483 * (i.e. which happens if we want the current active to be maintained)
486 scene->active_keyingset= type;
489 WM_event_add_notifier(C, NC_SCENE|ND_KEYINGSET, NULL);
491 return OPERATOR_FINISHED;
494 void ANIM_OT_keying_set_active_set (wmOperatorType *ot)
497 ot->name= "Set Active Keying Set";
498 ot->idname= "ANIM_OT_keying_set_active_set";
501 ot->invoke= keyingset_active_menu_invoke;
502 ot->exec= keyingset_active_menu_exec;
503 ot->poll= ED_operator_areaactive;
506 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
509 * - here the type is int not enum, since many of the indices here are determined dynamically
511 RNA_def_int(ot->srna, "type", 0, INT_MIN, INT_MAX, "Keying Set Number", "Index (determined internally) of the Keying Set to use", 0, 1);
514 /* ******************************************* */
515 /* REGISTERED KEYING SETS */
517 /* Keying Set Type Info declarations */
518 static ListBase keyingset_type_infos = {NULL, NULL};
520 /* Built-In Keying Sets (referencing type infos)*/
521 ListBase builtin_keyingsets = {NULL, NULL};
523 /* --------------- */
525 /* Find KeyingSet type info given a name */
526 KeyingSetInfo *ANIM_keyingset_info_find_named (const char name[])
529 if ((name == NULL) || (name[0] == 0))
532 /* search by comparing names */
533 return BLI_findstring(&keyingset_type_infos, name, offsetof(KeyingSetInfo, idname));
536 /* Find builtin KeyingSet by name */
537 KeyingSet *ANIM_builtin_keyingset_get_named (KeyingSet *prevKS, const char name[])
539 KeyingSet *ks, *first=NULL;
541 /* sanity checks any name to check? */
545 /* get first KeyingSet to use */
546 if (prevKS && prevKS->next)
549 first= builtin_keyingsets.first;
551 /* loop over KeyingSets checking names */
552 for (ks= first; ks; ks= ks->next) {
553 if (strcmp(name, ks->name) == 0)
557 /* no matches found */
561 /* --------------- */
563 /* Add the given KeyingSetInfo to the list of type infos, and create an appropriate builtin set too */
564 void ANIM_keyingset_info_register (KeyingSetInfo *ksi)
568 /* create a new KeyingSet
569 * - inherit name and keyframing settings from the typeinfo
571 ks = BKE_keyingset_add(&builtin_keyingsets, ksi->name, 1, ksi->keyingflag);
573 /* link this KeyingSet with its typeinfo */
574 memcpy(&ks->typeinfo, ksi->idname, sizeof(ks->typeinfo));
576 /* add type-info to the list */
577 BLI_addtail(&keyingset_type_infos, ksi);
580 /* Remove the given KeyingSetInfo from the list of type infos, and also remove the builtin set if appropriate */
581 void ANIM_keyingset_info_unregister (Main *bmain, KeyingSetInfo *ksi)
585 /* find relevant builtin KeyingSets which use this, and remove them */
586 // TODO: this isn't done now, since unregister is really only used atm when we
587 // reload the scripts, which kindof defeats the purpose of "builtin"?
588 for (ks= builtin_keyingsets.first; ks; ks= ksn) {
591 /* remove if matching typeinfo name */
592 if (strcmp(ks->typeinfo, ksi->idname) == 0) {
594 BKE_keyingset_free(ks);
595 BLI_remlink(&builtin_keyingsets, ks);
597 for(scene= bmain->scene.first; scene; scene= scene->id.next)
598 BLI_remlink_safe(&scene->keyingsets, ks);
604 /* free the type info */
605 BLI_freelinkN(&keyingset_type_infos, ksi);
608 /* --------------- */
610 void ANIM_keyingset_infos_exit (void)
612 KeyingSetInfo *ksi, *next;
614 /* free type infos */
615 for (ksi=keyingset_type_infos.first; ksi; ksi=next) {
618 /* free extra RNA data, and remove from list */
620 ksi->ext.free(ksi->ext.data);
621 BLI_freelinkN(&keyingset_type_infos, ksi);
624 /* free builtin sets */
625 BKE_keyingsets_free(&builtin_keyingsets);
628 /* ******************************************* */
629 /* KEYING SETS API (for UI) */
631 /* Getters for Active/Indices ----------------------------- */
633 /* Get the active Keying Set for the Scene provided */
634 KeyingSet *ANIM_scene_get_active_keyingset (Scene *scene)
636 /* if no scene, we've got no hope of finding the Keying Set */
640 /* currently, there are several possibilities here:
641 * - 0: no active keying set
642 * - > 0: one of the user-defined Keying Sets, but indices start from 0 (hence the -1)
643 * - < 0: a builtin keying set
645 if (scene->active_keyingset > 0)
646 return BLI_findlink(&scene->keyingsets, scene->active_keyingset-1);
648 return BLI_findlink(&builtin_keyingsets, (-scene->active_keyingset)-1);
651 /* Get the index of the Keying Set provided, for the given Scene */
652 int ANIM_scene_get_keyingset_index (Scene *scene, KeyingSet *ks)
656 /* if no KeyingSet provided, have none */
660 /* check if the KeyingSet exists in scene list */
662 /* get index and if valid, return
663 * - (absolute) Scene KeyingSets are from (>= 1)
665 index = BLI_findindex(&scene->keyingsets, ks);
670 /* still here, so try builtins list too
671 * - builtins are from (<= -1)
672 * - none/invalid is (= 0)
674 index = BLI_findindex(&builtin_keyingsets, ks);
681 /* Get Keying Set to use for Auto-Keyframing some transforms */
682 KeyingSet *ANIM_get_keyingset_for_autokeying(Scene *scene, const char *tranformKSName)
684 /* get KeyingSet to use
685 * - use the active KeyingSet if defined (and user wants to use it for all autokeying),
686 * or otherwise key transforms only
688 if (IS_AUTOKEY_FLAG(scene, ONLYKEYINGSET) && (scene->active_keyingset))
689 return ANIM_scene_get_active_keyingset(scene);
690 else if (IS_AUTOKEY_FLAG(scene, INSERTAVAIL))
691 return ANIM_builtin_keyingset_get_named(NULL, "Available");
693 return ANIM_builtin_keyingset_get_named(NULL, tranformKSName);
696 /* Menu of All Keying Sets ----------------------------- */
698 /* Dynamically populate an enum of Keying Sets */
699 EnumPropertyItem *ANIM_keying_sets_enum_itemf (bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
701 Scene *scene = CTX_data_scene(C);
703 EnumPropertyItem *item= NULL, item_tmp= {0};
708 return DummyRNA_DEFAULT_items;
712 * - only include entry if it exists
714 if (scene->active_keyingset) {
715 /* active Keying Set */
716 item_tmp.identifier= item_tmp.name= "Active Keying Set";
718 RNA_enum_item_add(&item, &totitem, &item_tmp);
721 RNA_enum_item_add_separator(&item, &totitem);
726 /* user-defined Keying Sets
727 * - these are listed in the order in which they were defined for the active scene
729 if (scene->keyingsets.first) {
730 for (ks= scene->keyingsets.first; ks; ks= ks->next) {
731 if (ANIM_keyingset_context_ok_poll(C, ks)) {
732 item_tmp.identifier= item_tmp.name= ks->name;
734 RNA_enum_item_add(&item, &totitem, &item_tmp);
739 RNA_enum_item_add_separator(&item, &totitem);
742 /* builtin Keying Sets */
744 for (ks= builtin_keyingsets.first; ks; ks= ks->next) {
745 /* only show KeyingSet if context is suitable */
746 if (ANIM_keyingset_context_ok_poll(C, ks)) {
747 item_tmp.identifier= item_tmp.name= ks->name;
749 RNA_enum_item_add(&item, &totitem, &item_tmp);
753 RNA_enum_item_end(&item, &totitem);
759 /* Create (and show) a menu containing all the Keying Sets which can be used in the current context */
760 void ANIM_keying_sets_menu_setup (bContext *C, const char title[], const char op_name[])
762 Scene *scene= CTX_data_scene(C);
768 pup= uiPupMenuBegin(C, title, ICON_NONE);
769 layout= uiPupMenuLayout(pup);
772 * - only include entry if it exists
774 if (scene->active_keyingset) {
775 uiItemIntO(layout, "Active Keying Set", ICON_NONE, op_name, "type", i++);
781 /* user-defined Keying Sets
782 * - these are listed in the order in which they were defined for the active scene
784 if (scene->keyingsets.first) {
785 for (ks= scene->keyingsets.first; ks; ks= ks->next) {
786 if (ANIM_keyingset_context_ok_poll(C, ks))
787 uiItemIntO(layout, ks->name, ICON_NONE, op_name, "type", i++);
792 /* builtin Keying Sets */
794 for (ks= builtin_keyingsets.first; ks; ks= ks->next) {
795 /* only show KeyingSet if context is suitable */
796 if (ANIM_keyingset_context_ok_poll(C, ks))
797 uiItemEnumO_value(layout, ks->name, ICON_NONE, op_name, "type", i--);
800 uiPupMenuEnd(C, pup);
803 /* ******************************************* */
804 /* KEYFRAME MODIFICATION */
806 /* Polling API ----------------------------------------------- */
808 /* Check if KeyingSet can be used in the current context */
809 short ANIM_keyingset_context_ok_poll (bContext *C, KeyingSet *ks)
811 if ((ks->flag & KEYINGSET_ABSOLUTE) == 0) {
812 KeyingSetInfo *ksi = ANIM_keyingset_info_find_named(ks->typeinfo);
814 /* get the associated 'type info' for this KeyingSet */
817 // TODO: check for missing callbacks!
819 /* check if it can be used in the current context */
820 return (ksi->poll(ksi, C));
826 /* Special 'Overrides' Iterator for Relative KeyingSets ------ */
828 /* 'Data Sources' for relative Keying Set 'overrides'
829 * - this is basically a wrapper for PointerRNA's in a linked list
830 * - do not allow this to be accessed from outside for now
832 typedef struct tRKS_DSource {
833 struct tRKS_DSource *next, *prev;
834 PointerRNA ptr; /* the whole point of this exercise! */
838 /* Iterator used for overriding the behaviour of iterators defined for
839 * relative Keying Sets, with the main usage of this being operators
840 * requiring Auto Keyframing. Internal Use Only!
842 static void RKS_ITER_overrides_list (KeyingSetInfo *ksi, bContext *C, KeyingSet *ks, ListBase *dsources)
846 for (ds = dsources->first; ds; ds = ds->next) {
847 /* run generate callback on this data */
848 ksi->generate(ksi, C, ks, &ds->ptr);
852 /* Add new data source for relative Keying Sets */
853 void ANIM_relative_keyingset_add_source (ListBase *dsources, ID *id, StructRNA *srna, void *data)
858 * - we must have somewhere to output the data
859 * - we must have both srna+data (and with id too optionally), or id by itself only
861 if (dsources == NULL)
863 if (ELEM(NULL, srna, data) && (id == NULL))
866 /* allocate new elem, and add to the list */
867 ds = MEM_callocN(sizeof(tRKS_DSource), "tRKS_DSource");
868 BLI_addtail(dsources, ds);
870 /* depending on what data we have, create using ID or full pointer call */
872 RNA_pointer_create(id, srna, data, &ds->ptr);
874 RNA_id_pointer_create(id, &ds->ptr);
877 /* KeyingSet Operations (Insert/Delete Keyframes) ------------ */
879 /* Given a KeyingSet and context info (if required), modify keyframes for the channels specified
880 * by the KeyingSet. This takes into account many of the different combinations of using KeyingSets.
881 * Returns the number of channels that keyframes were added to
883 int ANIM_apply_keyingset (bContext *C, ListBase *dsources, bAction *act, KeyingSet *ks, short mode, float cfra)
885 Scene *scene= CTX_data_scene(C);
886 ReportList *reports = CTX_wm_reports(C);
888 int kflag=0, success= 0;
889 char *groupname= NULL;
895 /* get flags to use */
896 if (mode == MODIFYKEY_MODE_INSERT) {
897 /* use KeyingSet's flags as base */
898 kflag= ks->keyingflag;
900 /* suppliment with info from the context */
901 kflag |= ANIM_get_keyframing_flags(scene, 1);
903 else if (mode == MODIFYKEY_MODE_DELETE)
906 /* if relative Keying Sets, poll and build up the paths */
907 if ((ks->flag & KEYINGSET_ABSOLUTE) == 0) {
908 KeyingSetInfo *ksi = ANIM_keyingset_info_find_named(ks->typeinfo);
910 /* clear all existing paths
911 * NOTE: BKE_keyingset_free() frees all of the paths for the KeyingSet, but not the set itself
913 BKE_keyingset_free(ks);
915 /* get the associated 'type info' for this KeyingSet */
917 return MODIFYKEY_MISSING_TYPEINFO;
918 // TODO: check for missing callbacks!
920 /* check if it can be used in the current context */
921 if (ksi->poll(ksi, C)) {
922 /* if a list of data sources are provided, run a special iterator over them,
923 * otherwise, just continue per normal
926 RKS_ITER_overrides_list(ksi, C, ks, dsources);
928 ksi->iter(ksi, C, ks);
930 /* if we don't have any paths now, then this still qualifies as invalid context */
931 if (ks->paths.first == NULL)
932 return MODIFYKEY_INVALID_CONTEXT;
935 /* poll callback tells us that KeyingSet is useless in current context */
936 return MODIFYKEY_INVALID_CONTEXT;
940 /* apply the paths as specified in the KeyingSet now */
941 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
945 /* skip path if no ID pointer is specified */
946 if (ksp->id == NULL) {
947 BKE_reportf(reports, RPT_WARNING,
948 "Skipping path in Keying Set, as it has no ID (KS = '%s', Path = '%s'[%d])",
949 ks->name, ksp->rna_path, ksp->array_index);
953 /* since keying settings can be defined on the paths too, extend the path before using it */
954 kflag2 = (kflag | ksp->keyingflag);
956 /* get pointer to name of group to add channels to */
957 if (ksp->groupmode == KSP_GROUP_NONE)
959 else if (ksp->groupmode == KSP_GROUP_KSNAME)
962 groupname= ksp->group;
964 /* init arraylen and i - arraylen should be greater than i so that
965 * normal non-array entries get keyframed correctly
970 /* get length of array if whole array option is enabled */
971 if (ksp->flag & KSP_FLAG_WHOLE_ARRAY) {
972 PointerRNA id_ptr, ptr;
975 RNA_id_pointer_create(ksp->id, &id_ptr);
976 if (RNA_path_resolve(&id_ptr, ksp->rna_path, &ptr, &prop) && prop)
977 arraylen= RNA_property_array_length(&ptr, prop);
980 /* we should do at least one step */
984 /* for each possible index, perform operation
985 * - assume that arraylen is greater than index
987 for (; i < arraylen; i++) {
988 /* action to take depends on mode */
989 if (mode == MODIFYKEY_MODE_INSERT)
990 success += insert_keyframe(reports, ksp->id, act, groupname, ksp->rna_path, i, cfra, kflag2);
991 else if (mode == MODIFYKEY_MODE_DELETE)
992 success += delete_keyframe(reports, ksp->id, act, groupname, ksp->rna_path, i, cfra, kflag2);
995 /* set recalc-flags */
996 switch (GS(ksp->id->name)) {
997 case ID_OB: /* Object (or Object-Related) Keyframes */
999 Object *ob= (Object *)ksp->id;
1001 ob->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME; // XXX: only object transforms only?
1006 /* send notifiers for updates (this doesn't require context to work!) */
1007 WM_main_add_notifier(NC_ANIMATION|ND_KEYFRAME|NA_EDITED, NULL);
1010 /* return the number of channels successfully affected */
1014 /* ************************************************** */