Keying Sets: Added options to add/remove properties from the active Keying Set to...
[blender.git] / source / blender / editors / animation / keyingsets.c
index f81f57d526a84934570b7ed2844df5e302978363..a91a9bfa911001c7258fb37427dd26c7b10762d8 100644 (file)
 
 #include "anim_intern.h"
 
+/* ************************************************** */
+/* KEYING SETS - OPERATORS (for use in UI menus) */
+
+/* Add to KeyingSet Button Operator ------------------------ */
+
+static int add_keyingset_button_exec (bContext *C, wmOperator *op)
+{
+       Scene *scene= CTX_data_scene(C);
+       KeyingSet *ks = NULL;
+       PropertyRNA *prop= NULL;
+       PointerRNA ptr;
+       char *path = NULL;
+       short success= 0;
+       int index=0, pflag=0;
+       int all= RNA_boolean_get(op->ptr, "all");
+       
+       /* verify the Keying Set to use:
+        *      - use the active one for now (more control over this can be added later)
+        *      - add a new one if it doesn't exist 
+        */
+       if (scene->active_keyingset == 0) {
+               short flag=0, keyingflag=0;
+               
+               /* validate flags 
+                *      - absolute KeyingSets should be created by default
+                */
+               flag |= KEYINGSET_ABSOLUTE;
+               
+               if (IS_AUTOKEY_FLAG(AUTOMATKEY)) 
+                       keyingflag |= INSERTKEY_MATRIX;
+               if (IS_AUTOKEY_FLAG(INSERTNEEDED)) 
+                       keyingflag |= INSERTKEY_NEEDED;
+                       
+               /* call the API func, and set the active keyingset index */
+               ks= BKE_keyingset_add(&scene->keyingsets, "ButtonKeyingSet", flag, keyingflag);
+               
+               scene->active_keyingset= BLI_countlist(&scene->keyingsets);
+       }
+       else
+               ks= BLI_findlink(&scene->keyingsets, scene->active_keyingset-1);
+       
+       /* try to add to keyingset using property retrieved from UI */
+       memset(&ptr, 0, sizeof(PointerRNA));
+       uiAnimContextProperty(C, &ptr, &prop, &index);
+       
+       /* check if property is able to be added */
+       if (ptr.data && prop && RNA_property_animateable(ptr.data, prop)) {
+               path= RNA_path_from_ID_to_property(&ptr, prop);
+               
+               if (path) {
+                       /* set flags */
+                       if (all) 
+                               pflag |= KSP_FLAG_WHOLE_ARRAY;
+                               
+                       /* add path to this setting */
+                       BKE_keyingset_add_destination(ks, ptr.id.data, NULL, path, index, pflag, KSP_GROUP_KSNAME);
+                       
+                       /* free the temp path created */
+                       MEM_freeN(path);
+               }
+       }
+       
+       if (success) {
+               /* send updates */
+               ED_anim_dag_flush_update(C);    
+               
+               /* for now, only send ND_KEYS for KeyingSets */
+               WM_event_add_notifier(C, ND_KEYS, NULL);
+       }
+       
+       return (success)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
+}
+
+void ANIM_OT_add_keyingset_button (wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Add to Keying Set";
+       ot->idname= "ANIM_OT_add_keyingset_button";
+       
+       /* callbacks */
+       ot->exec= add_keyingset_button_exec; 
+       //op->poll= ???
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+
+       /* properties */
+       RNA_def_boolean(ot->srna, "all", 1, "All", "Add all elements of the array to a Keying Set.");
+}
+
+/* Remove from KeyingSet Button Operator ------------------------ */
+
+static int remove_keyingset_button_exec (bContext *C, wmOperator *op)
+{
+       Scene *scene= CTX_data_scene(C);
+       KeyingSet *ks = NULL;
+       PropertyRNA *prop= NULL;
+       PointerRNA ptr;
+       char *path = NULL;
+       short success= 0;
+       int index=0;
+       
+       /* verify the Keying Set to use:
+        *      - use the active one for now (more control over this can be added later)
+        *      - return error if it doesn't exist
+        */
+       if (scene->active_keyingset == 0) {
+               BKE_report(op->reports, RPT_ERROR, "No active Keying Set to remove property from");
+               return OPERATOR_CANCELLED;
+       }
+       else
+               ks= BLI_findlink(&scene->keyingsets, scene->active_keyingset-1);
+       
+       /* try to add to keyingset using property retrieved from UI */
+       memset(&ptr, 0, sizeof(PointerRNA));
+       uiAnimContextProperty(C, &ptr, &prop, &index);
+
+       if (ptr.data && prop) {
+               path= RNA_path_from_ID_to_property(&ptr, prop);
+               
+               if (path) {
+                       KS_Path *ksp;
+                       
+                       /* try to find a path matching this description */
+                       ksp= BKE_keyingset_find_destination(ks, ptr.id.data, ks->name, path, index, KSP_GROUP_KSNAME);
+                       
+                       if (ksp) {
+                               /* just free it... */
+                               MEM_freeN(ksp->rna_path);
+                               BLI_freelinkN(&ks->paths, ksp);
+                               
+                               success= 1;
+                       }
+                       
+                       /* free temp path used */
+                       MEM_freeN(path);
+               }
+       }
+       
+       
+       if (success) {
+               /* send updates */
+               ED_anim_dag_flush_update(C);    
+               
+               /* for now, only send ND_KEYS for KeyingSets */
+               WM_event_add_notifier(C, ND_KEYS, NULL);
+       }
+       
+       return (success)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
+}
+
+void ANIM_OT_remove_keyingset_button (wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name= "Remove from Keying Set";
+       ot->idname= "ANIM_OT_remove_keyingset_button";
+       
+       /* callbacks */
+       ot->exec= remove_keyingset_button_exec; 
+       //op->poll= ???
+       
+       /* flags */
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
 /* ************************************************** */
 /* KEYING SETS - EDITING API  */