Fix [#20773] NODE ANIMATION: Animating node values broken
[blender.git] / source / blender / editors / interface / interface_anim.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "MEM_guardedalloc.h"
7
8 #include "DNA_anim_types.h"
9 #include "DNA_scene_types.h"
10 #include "DNA_screen_types.h"
11
12 #include "BLI_listbase.h"
13 #include "BLI_string.h"
14
15 #include "BKE_animsys.h"
16 #include "BKE_context.h"
17 #include "BKE_fcurve.h"
18
19 #include "RNA_access.h"
20 #include "RNA_types.h"
21
22 #include "ED_keyframing.h"
23
24 #include "UI_interface.h"
25
26 #include "WM_api.h"
27 #include "WM_types.h"
28
29 #include "interface_intern.h"
30
31 static FCurve *ui_but_get_fcurve(uiBut *but, bAction **action, int *driven)
32 {
33         return rna_get_fcurve(&but->rnapoin, but->rnaprop, but->rnaindex, action, driven);
34 }
35
36 void ui_but_anim_flag(uiBut *but, float cfra)
37 {
38         FCurve *fcu;
39         int driven;
40
41         but->flag &= ~(UI_BUT_ANIMATED|UI_BUT_ANIMATED_KEY|UI_BUT_DRIVEN);
42
43         fcu= ui_but_get_fcurve(but, NULL, &driven);
44
45         if(fcu) {
46                 if(!driven) {
47                         but->flag |= UI_BUT_ANIMATED;
48                         
49                         if(fcurve_frame_has_keyframe(fcu, cfra, 0))
50                                 but->flag |= UI_BUT_ANIMATED_KEY;
51                 }
52                 else {
53                         but->flag |= UI_BUT_DRIVEN;
54                 }
55         }
56 }
57
58 int ui_but_anim_expression_get(uiBut *but, char *str, int maxlen)
59 {
60         FCurve *fcu;
61         ChannelDriver *driver;
62         int driven;
63
64         fcu= ui_but_get_fcurve(but, NULL, &driven);
65
66         if(fcu && driven) {
67                 driver= fcu->driver;
68
69                 if(driver && driver->type == DRIVER_TYPE_PYTHON) {
70                         BLI_strncpy(str, driver->expression, maxlen);
71                         return 1;
72                 }
73         }
74
75         return 0;
76 }
77
78 int ui_but_anim_expression_set(uiBut *but, const char *str)
79 {
80         FCurve *fcu;
81         ChannelDriver *driver;
82         int driven;
83
84         fcu= ui_but_get_fcurve(but, NULL, &driven);
85
86         if(fcu && driven) {
87                 driver= fcu->driver;
88
89                 if(driver && driver->type == DRIVER_TYPE_PYTHON) {
90                         BLI_strncpy(driver->expression, str, sizeof(driver->expression));
91                         return 1;
92                 }
93         }
94
95         return 0;
96 }
97
98 void ui_but_anim_autokey(bContext *C, uiBut *but, Scene *scene, float cfra)
99 {
100         ID *id;
101         bAction *action;
102         FCurve *fcu;
103         int driven;
104
105         fcu= ui_but_get_fcurve(but, &action, &driven);
106
107         if(fcu && !driven) {
108                 id= but->rnapoin.id.data;
109                 
110                 // TODO: this should probably respect the keyingset only option for anim
111                 if(autokeyframe_cfra_can_key(scene, id)) {
112                         short flag = ANIM_get_keyframing_flags(scene, 1);
113                         
114                         fcu->flag &= ~FCURVE_SELECTED;
115                         insert_keyframe(id, action, ((fcu->grp)?(fcu->grp->name):(NULL)), fcu->rna_path, fcu->array_index, cfra, flag);
116                         WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME_EDIT, NULL);
117                 }
118         }
119 }
120
121 void uiAnimContextProperty(const bContext *C, struct PointerRNA *ptr, struct PropertyRNA **prop, int *index)
122 {
123         ARegion *ar= CTX_wm_region(C);
124         uiBlock *block;
125         uiBut *but;
126
127         memset(ptr, 0, sizeof(*ptr));
128         *prop= NULL;
129         *index= 0;
130
131         if(ar) {
132                 for(block=ar->uiblocks.first; block; block=block->next) {
133                         for(but=block->buttons.first; but; but= but->next) {
134                                 if((but->active || but->flag & UI_BUT_LAST_ACTIVE) && but->rnapoin.id.data) {
135                                         *ptr= but->rnapoin;
136                                         *prop= but->rnaprop;
137                                         *index= but->rnaindex;
138                                         return;
139                                 }
140                         }
141                 }
142         }
143 }
144
145 void ui_but_anim_insert_keyframe(bContext *C)
146 {
147         /* this operator calls uiAnimContextProperty above */
148         WM_operator_name_call(C, "ANIM_OT_keyframe_insert_button", WM_OP_INVOKE_DEFAULT, NULL);
149 }
150
151 void ui_but_anim_delete_keyframe(bContext *C)
152 {
153         /* this operator calls uiAnimContextProperty above */
154         WM_operator_name_call(C, "ANIM_OT_keyframe_delete_button", WM_OP_INVOKE_DEFAULT, NULL);
155 }
156
157 void ui_but_anim_add_driver(bContext *C)
158 {
159         /* this operator calls uiAnimContextProperty above */
160         WM_operator_name_call(C, "ANIM_OT_driver_button_add", WM_OP_INVOKE_DEFAULT, NULL);
161 }
162
163 void ui_but_anim_remove_driver(bContext *C)
164 {
165         /* this operator calls uiAnimContextProperty above */
166         WM_operator_name_call(C, "ANIM_OT_driver_button_remove", WM_OP_INVOKE_DEFAULT, NULL);
167 }
168
169 void ui_but_anim_copy_driver(bContext *C)
170 {
171         /* this operator calls uiAnimContextProperty above */
172         WM_operator_name_call(C, "ANIM_OT_copy_driver_button", WM_OP_INVOKE_DEFAULT, NULL);
173 }
174
175 void ui_but_anim_paste_driver(bContext *C)
176 {
177         /* this operator calls uiAnimContextProperty above */
178         WM_operator_name_call(C, "ANIM_OT_paste_driver_button", WM_OP_INVOKE_DEFAULT, NULL);
179 }
180
181 void ui_but_anim_add_keyingset(bContext *C)
182 {
183         /* this operator calls uiAnimContextProperty above */
184         WM_operator_name_call(C, "ANIM_OT_keyingset_button_add", WM_OP_INVOKE_DEFAULT, NULL);
185 }
186
187 void ui_but_anim_remove_keyingset(bContext *C)
188 {
189         /* this operator calls uiAnimContextProperty above */
190         WM_operator_name_call(C, "ANIM_OT_keyingset_button_remove", WM_OP_INVOKE_DEFAULT, NULL);
191 }