1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 /** \file
18  * \ingroup RNA
19  */
20 
21 #include <stdlib.h>
22 
23 #include "DNA_action_types.h"
24 #include "DNA_anim_types.h"
25 #include "DNA_scene_types.h"
26 
27 #include "BLI_utildefines.h"
28 
29 #include "BLT_translation.h"
30 
31 #include "MEM_guardedalloc.h"
32 
33 #include "RNA_access.h"
34 #include "RNA_define.h"
35 #include "RNA_enum_types.h"
36 
37 #include "rna_internal.h"
38 
39 #include "WM_types.h"
40 
41 #include "ED_keyframing.h"
42 
43 /* exported for use in API */
44 const EnumPropertyItem rna_enum_keyingset_path_grouping_items[] = {
45     {KSP_GROUP_NAMED, "NAMED", 0, "Named Group", ""},
46     {KSP_GROUP_NONE, "NONE", 0, "None", ""},
47     {KSP_GROUP_KSNAME, "KEYINGSET", 0, "Keying Set Name", ""},
48     {0, NULL, 0, NULL, NULL},
49 };
50 
51 /* It would be cool to get rid of this 'INSERTKEY_' prefix in 'py strings' values,
52  * but it would break existing
53  * exported keyingset... :/
54  */
55 const EnumPropertyItem rna_enum_keying_flag_items[] = {
56     {INSERTKEY_NEEDED,
57      "INSERTKEY_NEEDED",
58      0,
59      "Only Needed",
60      "Only insert keyframes where they're needed in the relevant F-Curves"},
61     {INSERTKEY_MATRIX,
62      "INSERTKEY_VISUAL",
63      0,
64      "Visual Keying",
65      "Insert keyframes based on 'visual transforms'"},
66     {INSERTKEY_XYZ2RGB,
67      "INSERTKEY_XYZ_TO_RGB",
68      0,
69      "XYZ=RGB Colors",
70      "Color for newly added transformation F-Curves (Location, Rotation, Scale) "
71      "and also Color is based on the transform axis"},
72     {0, NULL, 0, NULL, NULL},
73 };
74 
75 /* Contains additional flags suitable for use in Python API functions. */
76 const EnumPropertyItem rna_enum_keying_flag_items_api[] = {
77     {INSERTKEY_NEEDED,
78      "INSERTKEY_NEEDED",
79      0,
80      "Only Needed",
81      "Only insert keyframes where they're needed in the relevant F-Curves"},
82     {INSERTKEY_MATRIX,
83      "INSERTKEY_VISUAL",
84      0,
85      "Visual Keying",
86      "Insert keyframes based on 'visual transforms'"},
87     {INSERTKEY_XYZ2RGB,
88      "INSERTKEY_XYZ_TO_RGB",
89      0,
90      "XYZ=RGB Colors",
91      "Color for newly added transformation F-Curves (Location, Rotation, Scale) "
92      "and also Color is based on the transform axis"},
93     {INSERTKEY_REPLACE,
94      "INSERTKEY_REPLACE",
95      0,
96      "Replace Existing",
97      "Only replace existing keyframes"},
98     {INSERTKEY_AVAILABLE,
99      "INSERTKEY_AVAILABLE",
100      0,
101      "Only Available",
102      "Don't create F-Curves when they don't already exist"},
103     {INSERTKEY_CYCLE_AWARE,
104      "INSERTKEY_CYCLE_AWARE",
105      0,
106      "Cycle Aware Keying",
107      "When inserting into a curve with cyclic extrapolation, remap the keyframe inside "
108      "the cycle time range, and if changing an end key, also update the other one"},
109     {0, NULL, 0, NULL, NULL},
110 };
111 
112 #ifdef RNA_RUNTIME
113 
114 #  include "BLI_math_base.h"
115 
116 #  include "BKE_anim_data.h"
117 #  include "BKE_animsys.h"
118 #  include "BKE_fcurve.h"
119 #  include "BKE_nla.h"
120 
121 #  include "DEG_depsgraph.h"
122 #  include "DEG_depsgraph_build.h"
123 
124 #  include "DNA_object_types.h"
125 
126 #  include "ED_anim_api.h"
127 
128 #  include "WM_api.h"
129 
rna_AnimData_update(Main * bmain,Scene * UNUSED (scene),PointerRNA * ptr)130 static void rna_AnimData_update(Main *bmain, Scene *UNUSED(scene), PointerRNA *ptr)
131 {
132   ID *id = ptr->owner_id;
133 
134   ANIM_id_update(bmain, id);
135 }
136 
rna_AnimData_dependency_update(Main * bmain,Scene * scene,PointerRNA * ptr)137 static void rna_AnimData_dependency_update(Main *bmain, Scene *scene, PointerRNA *ptr)
138 {
139   DEG_relations_tag_update(bmain);
140 
141   rna_AnimData_update(bmain, scene, ptr);
142 }
143 
rna_AnimData_action_editable(PointerRNA * ptr,const char ** UNUSED (r_info))144 static int rna_AnimData_action_editable(PointerRNA *ptr, const char **UNUSED(r_info))
145 {
146   AnimData *adt = (AnimData *)ptr->data;
147   return BKE_animdata_action_editable(adt) ? PROP_EDITABLE : 0;
148 }
149 
rna_AnimData_action_set(PointerRNA * ptr,PointerRNA value,struct ReportList * UNUSED (reports))150 static void rna_AnimData_action_set(PointerRNA *ptr,
151                                     PointerRNA value,
152                                     struct ReportList *UNUSED(reports))
153 {
154   ID *ownerId = ptr->owner_id;
155 
156   /* set action */
157   BKE_animdata_set_action(NULL, ownerId, value.data);
158 }
159 
rna_AnimData_tweakmode_set(PointerRNA * ptr,const bool value)160 static void rna_AnimData_tweakmode_set(PointerRNA *ptr, const bool value)
161 {
162   AnimData *adt = (AnimData *)ptr->data;
163 
164   /* NOTE: technically we should also set/unset SCE_NLA_EDIT_ON flag on the
165    * scene which is used to make polling tests faster, but this flag is weak
166    * and can easily break e.g. by changing layer visibility. This needs to be
167    * dealt with at some point. */
168 
169   if (value) {
170     BKE_nla_tweakmode_enter(adt);
171   }
172   else {
173     BKE_nla_tweakmode_exit(adt);
174   }
175 }
176 
177 /* ****************************** */
178 
179 /* wrapper for poll callback */
RKS_POLL_rna_internal(KeyingSetInfo * ksi,bContext * C)180 static bool RKS_POLL_rna_internal(KeyingSetInfo *ksi, bContext *C)
181 {
182   extern FunctionRNA rna_KeyingSetInfo_poll_func;
183 
184   PointerRNA ptr;
185   ParameterList list;
186   FunctionRNA *func;
187   void *ret;
188   int ok;
189 
190   RNA_pointer_create(NULL, ksi->rna_ext.srna, ksi, &ptr);
191   func = &rna_KeyingSetInfo_poll_func; /* RNA_struct_find_function(&ptr, "poll"); */
192 
193   RNA_parameter_list_create(&list, &ptr, func);
194   {
195     /* hook up arguments */
196     RNA_parameter_set_lookup(&list, "ksi", &ksi);
197     RNA_parameter_set_lookup(&list, "context", &C);
198 
199     /* execute the function */
200     ksi->rna_ext.call(C, &ptr, func, &list);
201 
202     /* read the result */
203     RNA_parameter_get_lookup(&list, "ok", &ret);
204     ok = *(bool *)ret;
205   }
206   RNA_parameter_list_free(&list);
207 
208   return ok;
209 }
210 
211 /* wrapper for iterator callback */
RKS_ITER_rna_internal(KeyingSetInfo * ksi,bContext * C,KeyingSet * ks)212 static void RKS_ITER_rna_internal(KeyingSetInfo *ksi, bContext *C, KeyingSet *ks)
213 {
214   extern FunctionRNA rna_KeyingSetInfo_iterator_func;
215 
216   PointerRNA ptr;
217   ParameterList list;
218   FunctionRNA *func;
219 
220   RNA_pointer_create(NULL, ksi->rna_ext.srna, ksi, &ptr);
221   func = &rna_KeyingSetInfo_iterator_func; /* RNA_struct_find_function(&ptr, "poll"); */
222 
223   RNA_parameter_list_create(&list, &ptr, func);
224   {
225     /* hook up arguments */
226     RNA_parameter_set_lookup(&list, "ksi", &ksi);
227     RNA_parameter_set_lookup(&list, "context", &C);
228     RNA_parameter_set_lookup(&list, "ks", &ks);
229 
230     /* execute the function */
231     ksi->rna_ext.call(C, &ptr, func, &list);
232   }
233   RNA_parameter_list_free(&list);
234 }
235 
236 /* wrapper for generator callback */
RKS_GEN_rna_internal(KeyingSetInfo * ksi,bContext * C,KeyingSet * ks,PointerRNA * data)237 static void RKS_GEN_rna_internal(KeyingSetInfo *ksi, bContext *C, KeyingSet *ks, PointerRNA *data)
238 {
239   extern FunctionRNA rna_KeyingSetInfo_generate_func;
240 
241   PointerRNA ptr;
242   ParameterList list;
243   FunctionRNA *func;
244 
245   RNA_pointer_create(NULL, ksi->rna_ext.srna, ksi, &ptr);
246   func = &rna_KeyingSetInfo_generate_func; /* RNA_struct_find_generate(&ptr, "poll"); */
247 
248   RNA_parameter_list_create(&list, &ptr, func);
249   {
250     /* hook up arguments */
251     RNA_parameter_set_lookup(&list, "ksi", &ksi);
252     RNA_parameter_set_lookup(&list, "context", &C);
253     RNA_parameter_set_lookup(&list, "ks", &ks);
254     RNA_parameter_set_lookup(&list, "data", data);
255 
256     /* execute the function */
257     ksi->rna_ext.call(C, &ptr, func, &list);
258   }
259   RNA_parameter_list_free(&list);
260 }
261 
262 /* ------ */
263 
264 /* XXX: the exact purpose of this is not too clear...
265  * maybe we want to revise this at some point? */
rna_KeyingSetInfo_refine(PointerRNA * ptr)266 static StructRNA *rna_KeyingSetInfo_refine(PointerRNA *ptr)
267 {
268   KeyingSetInfo *ksi = (KeyingSetInfo *)ptr->data;
269   return (ksi->rna_ext.srna) ? ksi->rna_ext.srna : &RNA_KeyingSetInfo;
270 }
271 
rna_KeyingSetInfo_unregister(Main * bmain,StructRNA * type)272 static void rna_KeyingSetInfo_unregister(Main *bmain, StructRNA *type)
273 {
274   KeyingSetInfo *ksi = RNA_struct_blender_type_get(type);
275 
276   if (ksi == NULL) {
277     return;
278   }
279 
280   /* free RNA data referencing this */
281   RNA_struct_free_extension(type, &ksi->rna_ext);
282   RNA_struct_free(&BLENDER_RNA, type);
283 
284   WM_main_add_notifier(NC_WINDOW, NULL);
285 
286   /* unlink Blender-side data */
287   ANIM_keyingset_info_unregister(bmain, ksi);
288 }
289 
rna_KeyingSetInfo_register(Main * bmain,ReportList * reports,void * data,const char * identifier,StructValidateFunc validate,StructCallbackFunc call,StructFreeFunc free)290 static StructRNA *rna_KeyingSetInfo_register(Main *bmain,
291                                              ReportList *reports,
292                                              void *data,
293                                              const char *identifier,
294                                              StructValidateFunc validate,
295                                              StructCallbackFunc call,
296                                              StructFreeFunc free)
297 {
298   KeyingSetInfo dummyksi = {NULL};
299   KeyingSetInfo *ksi;
300   PointerRNA dummyptr = {NULL};
301   int have_function[3];
302 
303   /* setup dummy type info to store static properties in */
304   /* TODO: perhaps we want to get users to register
305    * as if they're using 'KeyingSet' directly instead? */
306   RNA_pointer_create(NULL, &RNA_KeyingSetInfo, &dummyksi, &dummyptr);
307 
308   /* validate the python class */
309   if (validate(&dummyptr, data, have_function) != 0) {
310     return NULL;
311   }
312 
313   if (strlen(identifier) >= sizeof(dummyksi.idname)) {
314     BKE_reportf(reports,
315                 RPT_ERROR,
316                 "Registering keying set info class: '%s' is too long, maximum length is %d",
317                 identifier,
318                 (int)sizeof(dummyksi.idname));
319     return NULL;
320   }
321 
322   /* check if we have registered this info before, and remove it */
323   ksi = ANIM_keyingset_info_find_name(dummyksi.idname);
324   if (ksi && ksi->rna_ext.srna) {
325     rna_KeyingSetInfo_unregister(bmain, ksi->rna_ext.srna);
326   }
327 
328   /* create a new KeyingSetInfo type */
329   ksi = MEM_mallocN(sizeof(KeyingSetInfo), "python keying set info");
330   memcpy(ksi, &dummyksi, sizeof(KeyingSetInfo));
331 
332   /* set RNA-extensions info */
333   ksi->rna_ext.srna = RNA_def_struct_ptr(&BLENDER_RNA, ksi->idname, &RNA_KeyingSetInfo);
334   ksi->rna_ext.data = data;
335   ksi->rna_ext.call = call;
336   ksi->rna_ext.free = free;
337   RNA_struct_blender_type_set(ksi->rna_ext.srna, ksi);
338 
339   /* set callbacks */
340   /* NOTE: we really should have all of these...  */
341   ksi->poll = (have_function[0]) ? RKS_POLL_rna_internal : NULL;
342   ksi->iter = (have_function[1]) ? RKS_ITER_rna_internal : NULL;
343   ksi->generate = (have_function[2]) ? RKS_GEN_rna_internal : NULL;
344 
345   /* add and register with other info as needed */
346   ANIM_keyingset_info_register(ksi);
347 
348   WM_main_add_notifier(NC_WINDOW, NULL);
349 
350   /* return the struct-rna added */
351   return ksi->rna_ext.srna;
352 }
353 
354 /* ****************************** */
355 
rna_ksPath_id_typef(PointerRNA * ptr)356 static StructRNA *rna_ksPath_id_typef(PointerRNA *ptr)
357 {
358   KS_Path *ksp = (KS_Path *)ptr->data;
359   return ID_code_to_RNA_type(ksp->idtype);
360 }
361 
rna_ksPath_id_editable(PointerRNA * ptr,const char ** UNUSED (r_info))362 static int rna_ksPath_id_editable(PointerRNA *ptr, const char **UNUSED(r_info))
363 {
364   KS_Path *ksp = (KS_Path *)ptr->data;
365   return (ksp->idtype) ? PROP_EDITABLE : 0;
366 }
367 
rna_ksPath_id_type_set(PointerRNA * ptr,int value)368 static void rna_ksPath_id_type_set(PointerRNA *ptr, int value)
369 {
370   KS_Path *data = (KS_Path *)(ptr->data);
371 
372   /* set the driver type, then clear the id-block if the type is invalid */
373   data->idtype = value;
374   if ((data->id) && (GS(data->id->name) != data->idtype)) {
375     data->id = NULL;
376   }
377 }
378 
rna_ksPath_RnaPath_get(PointerRNA * ptr,char * value)379 static void rna_ksPath_RnaPath_get(PointerRNA *ptr, char *value)
380 {
381   KS_Path *ksp = (KS_Path *)ptr->data;
382 
383   if (ksp->rna_path) {
384     strcpy(value, ksp->rna_path);
385   }
386   else {
387     value[0] = '\0';
388   }
389 }
390 
rna_ksPath_RnaPath_length(PointerRNA * ptr)391 static int rna_ksPath_RnaPath_length(PointerRNA *ptr)
392 {
393   KS_Path *ksp = (KS_Path *)ptr->data;
394 
395   if (ksp->rna_path) {
396     return strlen(ksp->rna_path);
397   }
398   else {
399     return 0;
400   }
401 }
402 
rna_ksPath_RnaPath_set(PointerRNA * ptr,const char * value)403 static void rna_ksPath_RnaPath_set(PointerRNA *ptr, const char *value)
404 {
405   KS_Path *ksp = (KS_Path *)ptr->data;
406 
407   if (ksp->rna_path) {
408     MEM_freeN(ksp->rna_path);
409   }
410 
411   if (value[0]) {
412     ksp->rna_path = BLI_strdup(value);
413   }
414   else {
415     ksp->rna_path = NULL;
416   }
417 }
418 
419 /* ****************************** */
420 
rna_KeyingSet_name_set(PointerRNA * ptr,const char * value)421 static void rna_KeyingSet_name_set(PointerRNA *ptr, const char *value)
422 {
423   KeyingSet *ks = (KeyingSet *)ptr->data;
424 
425   /* update names of corresponding groups if name changes */
426   if (!STREQ(ks->name, value)) {
427     KS_Path *ksp;
428 
429     for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
430       if ((ksp->groupmode == KSP_GROUP_KSNAME) && (ksp->id)) {
431         AnimData *adt = BKE_animdata_from_id(ksp->id);
432 
433         /* TODO: NLA strips? */
434         if (adt && adt->action) {
435           bActionGroup *agrp;
436 
437           /* lazy check - should really find the F-Curve for the affected path and check its group
438            * but this way should be faster and work well for most cases, as long as there are no
439            * conflicts
440            */
441           for (agrp = adt->action->groups.first; agrp; agrp = agrp->next) {
442             if (STREQ(ks->name, agrp->name)) {
443               /* there should only be one of these in the action, so can stop... */
444               BLI_strncpy(agrp->name, value, sizeof(agrp->name));
445               break;
446             }
447           }
448         }
449       }
450     }
451   }
452 
453   /* finally, update name to new value */
454   BLI_strncpy(ks->name, value, sizeof(ks->name));
455 }
456 
rna_KeyingSet_active_ksPath_editable(PointerRNA * ptr,const char ** UNUSED (r_info))457 static int rna_KeyingSet_active_ksPath_editable(PointerRNA *ptr, const char **UNUSED(r_info))
458 {
459   KeyingSet *ks = (KeyingSet *)ptr->data;
460 
461   /* only editable if there are some paths to change to */
462   return (BLI_listbase_is_empty(&ks->paths) == false) ? PROP_EDITABLE : 0;
463 }
464 
rna_KeyingSet_active_ksPath_get(PointerRNA * ptr)465 static PointerRNA rna_KeyingSet_active_ksPath_get(PointerRNA *ptr)
466 {
467   KeyingSet *ks = (KeyingSet *)ptr->data;
468   return rna_pointer_inherit_refine(
469       ptr, &RNA_KeyingSetPath, BLI_findlink(&ks->paths, ks->active_path - 1));
470 }
471 
rna_KeyingSet_active_ksPath_set(PointerRNA * ptr,PointerRNA value,struct ReportList * UNUSED (reports))472 static void rna_KeyingSet_active_ksPath_set(PointerRNA *ptr,
473                                             PointerRNA value,
474                                             struct ReportList *UNUSED(reports))
475 {
476   KeyingSet *ks = (KeyingSet *)ptr->data;
477   KS_Path *ksp = (KS_Path *)value.data;
478   ks->active_path = BLI_findindex(&ks->paths, ksp) + 1;
479 }
480 
rna_KeyingSet_active_ksPath_index_get(PointerRNA * ptr)481 static int rna_KeyingSet_active_ksPath_index_get(PointerRNA *ptr)
482 {
483   KeyingSet *ks = (KeyingSet *)ptr->data;
484   return MAX2(ks->active_path - 1, 0);
485 }
486 
rna_KeyingSet_active_ksPath_index_set(PointerRNA * ptr,int value)487 static void rna_KeyingSet_active_ksPath_index_set(PointerRNA *ptr, int value)
488 {
489   KeyingSet *ks = (KeyingSet *)ptr->data;
490   ks->active_path = value + 1;
491 }
492 
rna_KeyingSet_active_ksPath_index_range(PointerRNA * ptr,int * min,int * max,int * UNUSED (softmin),int * UNUSED (softmax))493 static void rna_KeyingSet_active_ksPath_index_range(
494     PointerRNA *ptr, int *min, int *max, int *UNUSED(softmin), int *UNUSED(softmax))
495 {
496   KeyingSet *ks = (KeyingSet *)ptr->data;
497 
498   *min = 0;
499   *max = max_ii(0, BLI_listbase_count(&ks->paths) - 1);
500 }
501 
rna_KeyingSet_typeinfo_get(PointerRNA * ptr)502 static PointerRNA rna_KeyingSet_typeinfo_get(PointerRNA *ptr)
503 {
504   KeyingSet *ks = (KeyingSet *)ptr->data;
505   KeyingSetInfo *ksi = NULL;
506 
507   /* keying set info is only for builtin Keying Sets */
508   if ((ks->flag & KEYINGSET_ABSOLUTE) == 0) {
509     ksi = ANIM_keyingset_info_find_name(ks->typeinfo);
510   }
511   return rna_pointer_inherit_refine(ptr, &RNA_KeyingSetInfo, ksi);
512 }
513 
rna_KeyingSet_paths_add(KeyingSet * keyingset,ReportList * reports,ID * id,const char rna_path[],int index,int group_method,const char group_name[])514 static KS_Path *rna_KeyingSet_paths_add(KeyingSet *keyingset,
515                                         ReportList *reports,
516                                         ID *id,
517                                         const char rna_path[],
518                                         int index,
519                                         int group_method,
520                                         const char group_name[])
521 {
522   KS_Path *ksp = NULL;
523   short flag = 0;
524 
525   /* Special case when index = -1, we key the whole array
526    * (as with other places where index is used). */
527   if (index == -1) {
528     flag |= KSP_FLAG_WHOLE_ARRAY;
529     index = 0;
530   }
531 
532   /* if data is valid, call the API function for this */
533   if (keyingset) {
534     ksp = BKE_keyingset_add_path(keyingset, id, group_name, rna_path, index, flag, group_method);
535     keyingset->active_path = BLI_listbase_count(&keyingset->paths);
536   }
537   else {
538     BKE_report(reports, RPT_ERROR, "Keying set path could not be added");
539   }
540 
541   /* return added path */
542   return ksp;
543 }
544 
rna_KeyingSet_paths_remove(KeyingSet * keyingset,ReportList * reports,PointerRNA * ksp_ptr)545 static void rna_KeyingSet_paths_remove(KeyingSet *keyingset,
546                                        ReportList *reports,
547                                        PointerRNA *ksp_ptr)
548 {
549   KS_Path *ksp = ksp_ptr->data;
550 
551   /* if data is valid, call the API function for this */
552   if ((keyingset && ksp) == false) {
553     BKE_report(reports, RPT_ERROR, "Keying set path could not be removed");
554     return;
555   }
556 
557   /* remove the active path from the KeyingSet */
558   BKE_keyingset_free_path(keyingset, ksp);
559   RNA_POINTER_INVALIDATE(ksp_ptr);
560 
561   /* the active path number will most likely have changed */
562   /* TODO: we should get more fancy and actually check if it was removed,
563    * but this will do for now */
564   keyingset->active_path = 0;
565 }
566 
rna_KeyingSet_paths_clear(KeyingSet * keyingset,ReportList * reports)567 static void rna_KeyingSet_paths_clear(KeyingSet *keyingset, ReportList *reports)
568 {
569   /* if data is valid, call the API function for this */
570   if (keyingset) {
571     KS_Path *ksp, *kspn;
572 
573     /* free each path as we go to avoid looping twice */
574     for (ksp = keyingset->paths.first; ksp; ksp = kspn) {
575       kspn = ksp->next;
576       BKE_keyingset_free_path(keyingset, ksp);
577     }
578 
579     /* reset the active path, since there aren't any left */
580     keyingset->active_path = 0;
581   }
582   else {
583     BKE_report(reports, RPT_ERROR, "Keying set paths could not be removed");
584   }
585 }
586 
587 /* needs wrapper function to push notifier */
rna_NlaTrack_new(ID * id,AnimData * adt,Main * bmain,bContext * C,NlaTrack * track)588 static NlaTrack *rna_NlaTrack_new(ID *id, AnimData *adt, Main *bmain, bContext *C, NlaTrack *track)
589 {
590   NlaTrack *new_track = BKE_nlatrack_add(adt, track);
591 
592   WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_ADDED, NULL);
593 
594   DEG_relations_tag_update(bmain);
595   DEG_id_tag_update_ex(bmain, id, ID_RECALC_ANIMATION | ID_RECALC_COPY_ON_WRITE);
596 
597   return new_track;
598 }
599 
rna_NlaTrack_remove(ID * id,AnimData * adt,Main * bmain,bContext * C,ReportList * reports,PointerRNA * track_ptr)600 static void rna_NlaTrack_remove(
601     ID *id, AnimData *adt, Main *bmain, bContext *C, ReportList *reports, PointerRNA *track_ptr)
602 {
603   NlaTrack *track = track_ptr->data;
604 
605   if (BLI_findindex(&adt->nla_tracks, track) == -1) {
606     BKE_reportf(reports, RPT_ERROR, "NlaTrack '%s' cannot be removed", track->name);
607     return;
608   }
609 
610   BKE_nlatrack_free(&adt->nla_tracks, track, true);
611   RNA_POINTER_INVALIDATE(track_ptr);
612 
613   WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_REMOVED, NULL);
614 
615   DEG_relations_tag_update(bmain);
616   DEG_id_tag_update_ex(bmain, id, ID_RECALC_ANIMATION | ID_RECALC_COPY_ON_WRITE);
617 }
618 
rna_NlaTrack_active_get(PointerRNA * ptr)619 static PointerRNA rna_NlaTrack_active_get(PointerRNA *ptr)
620 {
621   AnimData *adt = (AnimData *)ptr->data;
622   NlaTrack *track = BKE_nlatrack_find_active(&adt->nla_tracks);
623   return rna_pointer_inherit_refine(ptr, &RNA_NlaTrack, track);
624 }
625 
rna_NlaTrack_active_set(PointerRNA * ptr,PointerRNA value,struct ReportList * UNUSED (reports))626 static void rna_NlaTrack_active_set(PointerRNA *ptr,
627                                     PointerRNA value,
628                                     struct ReportList *UNUSED(reports))
629 {
630   AnimData *adt = (AnimData *)ptr->data;
631   NlaTrack *track = (NlaTrack *)value.data;
632   BKE_nlatrack_set_active(&adt->nla_tracks, track);
633 }
634 
rna_Driver_from_existing(AnimData * adt,bContext * C,FCurve * src_driver)635 static FCurve *rna_Driver_from_existing(AnimData *adt, bContext *C, FCurve *src_driver)
636 {
637   /* verify that we've got a driver to duplicate */
638   if (ELEM(NULL, src_driver, src_driver->driver)) {
639     BKE_report(CTX_wm_reports(C), RPT_ERROR, "No valid driver data to create copy of");
640     return NULL;
641   }
642   else {
643     /* just make a copy of the existing one and add to self */
644     FCurve *new_fcu = BKE_fcurve_copy(src_driver);
645 
646     /* XXX: if we impose any ordering on these someday, this will be problematic */
647     BLI_addtail(&adt->drivers, new_fcu);
648     return new_fcu;
649   }
650 }
651 
rna_Driver_new(ID * id,AnimData * adt,Main * bmain,ReportList * reports,const char * rna_path,int array_index)652 static FCurve *rna_Driver_new(
653     ID *id, AnimData *adt, Main *bmain, ReportList *reports, const char *rna_path, int array_index)
654 {
655   if (rna_path[0] == '\0') {
656     BKE_report(reports, RPT_ERROR, "F-Curve data path empty, invalid argument");
657     return NULL;
658   }
659 
660   if (BKE_fcurve_find(&adt->drivers, rna_path, array_index)) {
661     BKE_reportf(reports, RPT_ERROR, "Driver '%s[%d]' already exists", rna_path, array_index);
662     return NULL;
663   }
664 
665   FCurve *fcu = verify_driver_fcurve(id, rna_path, array_index, DRIVER_FCURVE_KEYFRAMES);
666   BLI_assert(fcu != NULL);
667 
668   DEG_relations_tag_update(bmain);
669 
670   return fcu;
671 }
672 
rna_Driver_remove(AnimData * adt,Main * bmain,ReportList * reports,FCurve * fcu)673 static void rna_Driver_remove(AnimData *adt, Main *bmain, ReportList *reports, FCurve *fcu)
674 {
675   if (!BLI_remlink_safe(&adt->drivers, fcu)) {
676     BKE_report(reports, RPT_ERROR, "Driver not found in this animation data");
677     return;
678   }
679   BKE_fcurve_free(fcu);
680   DEG_relations_tag_update(bmain);
681 }
682 
rna_Driver_find(AnimData * adt,ReportList * reports,const char * data_path,int index)683 static FCurve *rna_Driver_find(AnimData *adt,
684                                ReportList *reports,
685                                const char *data_path,
686                                int index)
687 {
688   if (data_path[0] == '\0') {
689     BKE_report(reports, RPT_ERROR, "F-Curve data path empty, invalid argument");
690     return NULL;
691   }
692 
693   /* Returns NULL if not found. */
694   return BKE_fcurve_find(&adt->drivers, data_path, index);
695 }
696 
rna_AnimaData_override_apply(Main * UNUSED (bmain),PointerRNA * ptr_dst,PointerRNA * ptr_src,PointerRNA * ptr_storage,PropertyRNA * prop_dst,PropertyRNA * prop_src,PropertyRNA * UNUSED (prop_storage),const int len_dst,const int len_src,const int len_storage,PointerRNA * UNUSED (ptr_item_dst),PointerRNA * UNUSED (ptr_item_src),PointerRNA * UNUSED (ptr_item_storage),IDOverrideLibraryPropertyOperation * opop)697 bool rna_AnimaData_override_apply(Main *UNUSED(bmain),
698                                   PointerRNA *ptr_dst,
699                                   PointerRNA *ptr_src,
700                                   PointerRNA *ptr_storage,
701                                   PropertyRNA *prop_dst,
702                                   PropertyRNA *prop_src,
703                                   PropertyRNA *UNUSED(prop_storage),
704                                   const int len_dst,
705                                   const int len_src,
706                                   const int len_storage,
707                                   PointerRNA *UNUSED(ptr_item_dst),
708                                   PointerRNA *UNUSED(ptr_item_src),
709                                   PointerRNA *UNUSED(ptr_item_storage),
710                                   IDOverrideLibraryPropertyOperation *opop)
711 {
712   BLI_assert(len_dst == len_src && (!ptr_storage || len_dst == len_storage) && len_dst == 0);
713   BLI_assert(opop->operation == IDOVERRIDE_LIBRARY_OP_REPLACE &&
714              "Unsupported RNA override operation on animdata pointer");
715   UNUSED_VARS_NDEBUG(ptr_storage, len_dst, len_src, len_storage, opop);
716 
717   /* AnimData is a special case, since you cannot edit/replace it, it's either existent or not. */
718   AnimData *adt_dst = RNA_property_pointer_get(ptr_dst, prop_dst).data;
719   AnimData *adt_src = RNA_property_pointer_get(ptr_src, prop_src).data;
720 
721   if (adt_dst == NULL && adt_src != NULL) {
722     /* Copy anim data from reference into final local ID. */
723     BKE_animdata_copy_id(NULL, ptr_dst->owner_id, ptr_src->owner_id, 0);
724     return true;
725   }
726   else if (adt_dst != NULL && adt_src == NULL) {
727     /* Override has cleared/removed anim data from its reference. */
728     BKE_animdata_free(ptr_dst->owner_id, true);
729     return true;
730   }
731 
732   return false;
733 }
734 
735 #else
736 
737 /* helper function for Keying Set -> keying settings */
rna_def_common_keying_flags(StructRNA * srna,short reg)738 static void rna_def_common_keying_flags(StructRNA *srna, short reg)
739 {
740   PropertyRNA *prop;
741 
742   /* override scene/userpref defaults? */
743   prop = RNA_def_property(srna, "use_insertkey_override_needed", PROP_BOOLEAN, PROP_NONE);
744   RNA_def_property_boolean_sdna(prop, NULL, "keyingoverride", INSERTKEY_NEEDED);
745   RNA_def_property_ui_text(prop,
746                            "Override Insert Keyframes Default- Only Needed",
747                            "Override default setting to only insert keyframes where they're "
748                            "needed in the relevant F-Curves");
749   if (reg) {
750     RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
751   }
752 
753   prop = RNA_def_property(srna, "use_insertkey_override_visual", PROP_BOOLEAN, PROP_NONE);
754   RNA_def_property_boolean_sdna(prop, NULL, "keyingoverride", INSERTKEY_MATRIX);
755   RNA_def_property_ui_text(
756       prop,
757       "Override Insert Keyframes Default - Visual",
758       "Override default setting to insert keyframes based on 'visual transforms'");
759   if (reg) {
760     RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
761   }
762 
763   prop = RNA_def_property(srna, "use_insertkey_override_xyz_to_rgb", PROP_BOOLEAN, PROP_NONE);
764   RNA_def_property_boolean_sdna(prop, NULL, "keyingoverride", INSERTKEY_XYZ2RGB);
765   RNA_def_property_ui_text(
766       prop,
767       "Override F-Curve Colors - XYZ to RGB",
768       "Override default setting to set color for newly added transformation F-Curves "
769       "(Location, Rotation, Scale) to be based on the transform axis");
770   if (reg) {
771     RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
772   }
773 
774   /* value to override defaults with */
775   prop = RNA_def_property(srna, "use_insertkey_needed", PROP_BOOLEAN, PROP_NONE);
776   RNA_def_property_boolean_sdna(prop, NULL, "keyingflag", INSERTKEY_NEEDED);
777   RNA_def_property_ui_text(prop,
778                            "Insert Keyframes - Only Needed",
779                            "Only insert keyframes where they're needed in the relevant F-Curves");
780   if (reg) {
781     RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
782   }
783 
784   prop = RNA_def_property(srna, "use_insertkey_visual", PROP_BOOLEAN, PROP_NONE);
785   RNA_def_property_boolean_sdna(prop, NULL, "keyingflag", INSERTKEY_MATRIX);
786   RNA_def_property_ui_text(
787       prop, "Insert Keyframes - Visual", "Insert keyframes based on 'visual transforms'");
788   if (reg) {
789     RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
790   }
791 
792   prop = RNA_def_property(srna, "use_insertkey_xyz_to_rgb", PROP_BOOLEAN, PROP_NONE);
793   RNA_def_property_boolean_sdna(prop, NULL, "keyingflag", INSERTKEY_XYZ2RGB);
794   RNA_def_property_ui_text(prop,
795                            "F-Curve Colors - XYZ to RGB",
796                            "Color for newly added transformation F-Curves (Location, Rotation, "
797                            "Scale) is based on the transform axis");
798   if (reg) {
799     RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
800   }
801 }
802 
803 /* --- */
804 
805 /* To avoid repeating it twice! */
806 #  define KEYINGSET_IDNAME_DOC \
807     "If this is set, the Keying Set gets a custom ID, otherwise it takes " \
808     "the name of the class used to define the Keying Set (for example, " \
809     "if the class name is \"BUILTIN_KSI_location\", and bl_idname is not " \
810     "set by the script, then bl_idname = \"BUILTIN_KSI_location\")"
811 
rna_def_keyingset_info(BlenderRNA * brna)812 static void rna_def_keyingset_info(BlenderRNA *brna)
813 {
814   StructRNA *srna;
815   PropertyRNA *prop;
816   FunctionRNA *func;
817   PropertyRNA *parm;
818 
819   srna = RNA_def_struct(brna, "KeyingSetInfo", NULL);
820   RNA_def_struct_sdna(srna, "KeyingSetInfo");
821   RNA_def_struct_ui_text(
822       srna, "Keying Set Info", "Callback function defines for builtin Keying Sets");
823   RNA_def_struct_refine_func(srna, "rna_KeyingSetInfo_refine");
824   RNA_def_struct_register_funcs(
825       srna, "rna_KeyingSetInfo_register", "rna_KeyingSetInfo_unregister", NULL);
826 
827   /* Properties --------------------- */
828 
829   RNA_define_verify_sdna(0); /* not in sdna */
830 
831   prop = RNA_def_property(srna, "bl_idname", PROP_STRING, PROP_NONE);
832   RNA_def_property_string_sdna(prop, NULL, "idname");
833   RNA_def_property_flag(prop, PROP_REGISTER);
834   RNA_def_property_ui_text(prop, "ID Name", KEYINGSET_IDNAME_DOC);
835 
836   prop = RNA_def_property(srna, "bl_label", PROP_STRING, PROP_NONE);
837   RNA_def_property_string_sdna(prop, NULL, "name");
838   RNA_def_property_ui_text(prop, "UI Name", "");
839   RNA_def_struct_name_property(srna, prop);
840   RNA_def_property_flag(prop, PROP_REGISTER);
841 
842   prop = RNA_def_property(srna, "bl_description", PROP_STRING, PROP_NONE);
843   RNA_def_property_string_sdna(prop, NULL, "description");
844   RNA_def_property_string_maxlength(prop, RNA_DYN_DESCR_MAX); /* else it uses the pointer size! */
845   RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
846   RNA_def_property_ui_text(prop, "Description", "A short description of the keying set");
847 
848   /* Regarding why we don't use rna_def_common_keying_flags() here:
849    * - Using it would keep this case in sync with the other places
850    *   where these options are exposed (which are optimized for being
851    *   used in the UI).
852    * - Unlike all the other places, this case is used for defining
853    *   new "built in" Keying Sets via the Python API. In that case,
854    *   it makes more sense to expose these in a way more similar to
855    *   other places featuring bl_idname/label/description (i.e. operators)
856    */
857   prop = RNA_def_property(srna, "bl_options", PROP_ENUM, PROP_NONE);
858   RNA_def_property_enum_sdna(prop, NULL, "keyingflag");
859   RNA_def_property_enum_items(prop, rna_enum_keying_flag_items);
860   RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL | PROP_ENUM_FLAG);
861   RNA_def_property_ui_text(prop, "Options", "Keying Set options to use when inserting keyframes");
862 
863   RNA_define_verify_sdna(1);
864 
865   /* Function Callbacks ------------- */
866   /* poll */
867   func = RNA_def_function(srna, "poll", NULL);
868   RNA_def_function_ui_description(func, "Test if Keying Set can be used or not");
869   RNA_def_function_flag(func, FUNC_REGISTER);
870   RNA_def_function_return(func, RNA_def_boolean(func, "ok", 1, "", ""));
871   parm = RNA_def_pointer(func, "context", "Context", "", "");
872   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
873 
874   /* iterator */
875   func = RNA_def_function(srna, "iterator", NULL);
876   RNA_def_function_ui_description(
877       func, "Call generate() on the structs which have properties to be keyframed");
878   RNA_def_function_flag(func, FUNC_REGISTER);
879   parm = RNA_def_pointer(func, "context", "Context", "", "");
880   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
881   parm = RNA_def_pointer(func, "ks", "KeyingSet", "", "");
882   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
883 
884   /* generate */
885   func = RNA_def_function(srna, "generate", NULL);
886   RNA_def_function_ui_description(
887       func, "Add Paths to the Keying Set to keyframe the properties of the given data");
888   RNA_def_function_flag(func, FUNC_REGISTER);
889   parm = RNA_def_pointer(func, "context", "Context", "", "");
890   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
891   parm = RNA_def_pointer(func, "ks", "KeyingSet", "", "");
892   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
893   parm = RNA_def_pointer(func, "data", "AnyType", "", "");
894   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED | PARM_RNAPTR);
895 }
896 
rna_def_keyingset_path(BlenderRNA * brna)897 static void rna_def_keyingset_path(BlenderRNA *brna)
898 {
899   StructRNA *srna;
900   PropertyRNA *prop;
901 
902   srna = RNA_def_struct(brna, "KeyingSetPath", NULL);
903   RNA_def_struct_sdna(srna, "KS_Path");
904   RNA_def_struct_ui_text(srna, "Keying Set Path", "Path to a setting for use in a Keying Set");
905 
906   /* ID */
907   prop = RNA_def_property(srna, "id", PROP_POINTER, PROP_NONE);
908   RNA_def_property_struct_type(prop, "ID");
909   RNA_def_property_flag(prop, PROP_EDITABLE);
910   RNA_def_property_editable_func(prop, "rna_ksPath_id_editable");
911   RNA_def_property_pointer_funcs(prop, NULL, NULL, "rna_ksPath_id_typef", NULL);
912   RNA_def_property_ui_text(prop,
913                            "ID-Block",
914                            "ID-Block that keyframes for Keying Set should be added to "
915                            "(for Absolute Keying Sets only)");
916   RNA_def_property_update(
917       prop, NC_SCENE | ND_KEYINGSET | NA_EDITED, NULL); /* XXX: maybe a bit too noisy */
918 
919   prop = RNA_def_property(srna, "id_type", PROP_ENUM, PROP_NONE);
920   RNA_def_property_enum_sdna(prop, NULL, "idtype");
921   RNA_def_property_enum_items(prop, rna_enum_id_type_items);
922   RNA_def_property_enum_default(prop, ID_OB);
923   RNA_def_property_enum_funcs(prop, NULL, "rna_ksPath_id_type_set", NULL);
924   RNA_def_property_ui_text(prop, "ID Type", "Type of ID-block that can be used");
925   RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_ID);
926   RNA_def_property_update(
927       prop, NC_SCENE | ND_KEYINGSET | NA_EDITED, NULL); /* XXX: maybe a bit too noisy */
928 
929   /* Group */
930   prop = RNA_def_property(srna, "group", PROP_STRING, PROP_NONE);
931   RNA_def_property_ui_text(
932       prop, "Group Name", "Name of Action Group to assign setting(s) for this path to");
933   RNA_def_property_update(
934       prop, NC_SCENE | ND_KEYINGSET | NA_EDITED, NULL); /* XXX: maybe a bit too noisy */
935 
936   /* Grouping */
937   prop = RNA_def_property(srna, "group_method", PROP_ENUM, PROP_NONE);
938   RNA_def_property_enum_sdna(prop, NULL, "groupmode");
939   RNA_def_property_enum_items(prop, rna_enum_keyingset_path_grouping_items);
940   RNA_def_property_ui_text(
941       prop, "Grouping Method", "Method used to define which Group-name to use");
942   RNA_def_property_update(
943       prop, NC_SCENE | ND_KEYINGSET | NA_EDITED, NULL); /* XXX: maybe a bit too noisy */
944 
945   /* Path + Array Index */
946   prop = RNA_def_property(srna, "data_path", PROP_STRING, PROP_NONE);
947   RNA_def_property_string_funcs(
948       prop, "rna_ksPath_RnaPath_get", "rna_ksPath_RnaPath_length", "rna_ksPath_RnaPath_set");
949   RNA_def_property_ui_text(prop, "Data Path", "Path to property setting");
950   RNA_def_struct_name_property(srna, prop); /* XXX this is the best indicator for now... */
951   RNA_def_property_update(prop, NC_SCENE | ND_KEYINGSET | NA_EDITED, NULL);
952 
953   /* called 'index' when given as function arg */
954   prop = RNA_def_property(srna, "array_index", PROP_INT, PROP_NONE);
955   RNA_def_property_ui_text(prop, "RNA Array Index", "Index to the specific setting if applicable");
956   RNA_def_property_update(
957       prop, NC_SCENE | ND_KEYINGSET | NA_EDITED, NULL); /* XXX: maybe a bit too noisy */
958 
959   /* Flags */
960   prop = RNA_def_property(srna, "use_entire_array", PROP_BOOLEAN, PROP_NONE);
961   RNA_def_property_boolean_sdna(prop, NULL, "flag", KSP_FLAG_WHOLE_ARRAY);
962   RNA_def_property_ui_text(
963       prop,
964       "Entire Array",
965       "When an 'array/vector' type is chosen (Location, Rotation, Color, etc.), "
966       "entire array is to be used");
967   RNA_def_property_update(
968       prop, NC_SCENE | ND_KEYINGSET | NA_EDITED, NULL); /* XXX: maybe a bit too noisy */
969 
970   /* Keyframing Settings */
971   rna_def_common_keying_flags(srna, 0);
972 }
973 
974 /* keyingset.paths */
rna_def_keyingset_paths(BlenderRNA * brna,PropertyRNA * cprop)975 static void rna_def_keyingset_paths(BlenderRNA *brna, PropertyRNA *cprop)
976 {
977   StructRNA *srna;
978 
979   FunctionRNA *func;
980   PropertyRNA *parm;
981 
982   PropertyRNA *prop;
983 
984   RNA_def_property_srna(cprop, "KeyingSetPaths");
985   srna = RNA_def_struct(brna, "KeyingSetPaths", NULL);
986   RNA_def_struct_sdna(srna, "KeyingSet");
987   RNA_def_struct_ui_text(srna, "Keying set paths", "Collection of keying set paths");
988 
989   /* Add Path */
990   func = RNA_def_function(srna, "add", "rna_KeyingSet_paths_add");
991   RNA_def_function_ui_description(func, "Add a new path for the Keying Set");
992   RNA_def_function_flag(func, FUNC_USE_REPORTS);
993   /* return arg */
994   parm = RNA_def_pointer(
995       func, "ksp", "KeyingSetPath", "New Path", "Path created and added to the Keying Set");
996   RNA_def_function_return(func, parm);
997   /* ID-block for target */
998   parm = RNA_def_pointer(
999       func, "target_id", "ID", "Target ID", "ID data-block for the destination");
1000   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1001   /* rna-path */
1002   /* XXX hopefully this is long enough */
1003   parm = RNA_def_string(
1004       func, "data_path", NULL, 256, "Data-Path", "RNA-Path to destination property");
1005   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1006   /* index (defaults to -1 for entire array) */
1007   RNA_def_int(func,
1008               "index",
1009               -1,
1010               -1,
1011               INT_MAX,
1012               "Index",
1013               "The index of the destination property (i.e. axis of Location/Rotation/etc.), "
1014               "or -1 for the entire array",
1015               0,
1016               INT_MAX);
1017   /* grouping */
1018   RNA_def_enum(func,
1019                "group_method",
1020                rna_enum_keyingset_path_grouping_items,
1021                KSP_GROUP_KSNAME,
1022                "Grouping Method",
1023                "Method used to define which Group-name to use");
1024   RNA_def_string(
1025       func,
1026       "group_name",
1027       NULL,
1028       64,
1029       "Group Name",
1030       "Name of Action Group to assign destination to (only if grouping mode is to use this name)");
1031 
1032   /* Remove Path */
1033   func = RNA_def_function(srna, "remove", "rna_KeyingSet_paths_remove");
1034   RNA_def_function_ui_description(func, "Remove the given path from the Keying Set");
1035   RNA_def_function_flag(func, FUNC_USE_REPORTS);
1036   /* path to remove */
1037   parm = RNA_def_pointer(func, "path", "KeyingSetPath", "Path", "");
1038   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED | PARM_RNAPTR);
1039   RNA_def_parameter_clear_flags(parm, PROP_THICK_WRAP, 0);
1040 
1041   /* Remove All Paths */
1042   func = RNA_def_function(srna, "clear", "rna_KeyingSet_paths_clear");
1043   RNA_def_function_ui_description(func, "Remove all the paths from the Keying Set");
1044   RNA_def_function_flag(func, FUNC_USE_REPORTS);
1045 
1046   prop = RNA_def_property(srna, "active", PROP_POINTER, PROP_NONE);
1047   RNA_def_property_struct_type(prop, "KeyingSetPath");
1048   RNA_def_property_flag(prop, PROP_EDITABLE);
1049   RNA_def_property_editable_func(prop, "rna_KeyingSet_active_ksPath_editable");
1050   RNA_def_property_pointer_funcs(
1051       prop, "rna_KeyingSet_active_ksPath_get", "rna_KeyingSet_active_ksPath_set", NULL, NULL);
1052   RNA_def_property_ui_text(
1053       prop, "Active Keying Set", "Active Keying Set used to insert/delete keyframes");
1054 
1055   prop = RNA_def_property(srna, "active_index", PROP_INT, PROP_NONE);
1056   RNA_def_property_int_sdna(prop, NULL, "active_path");
1057   RNA_def_property_int_funcs(prop,
1058                              "rna_KeyingSet_active_ksPath_index_get",
1059                              "rna_KeyingSet_active_ksPath_index_set",
1060                              "rna_KeyingSet_active_ksPath_index_range");
1061   RNA_def_property_ui_text(prop, "Active Path Index", "Current Keying Set index");
1062 }
1063 
rna_def_keyingset(BlenderRNA * brna)1064 static void rna_def_keyingset(BlenderRNA *brna)
1065 {
1066   StructRNA *srna;
1067   PropertyRNA *prop;
1068 
1069   srna = RNA_def_struct(brna, "KeyingSet", NULL);
1070   RNA_def_struct_ui_text(srna, "Keying Set", "Settings that should be keyframed together");
1071 
1072   /* Id/Label */
1073   prop = RNA_def_property(srna, "bl_idname", PROP_STRING, PROP_NONE);
1074   RNA_def_property_string_sdna(prop, NULL, "idname");
1075   RNA_def_property_flag(prop, PROP_REGISTER);
1076   RNA_def_property_ui_text(prop, "ID Name", KEYINGSET_IDNAME_DOC);
1077   /* NOTE: disabled, as ID name shouldn't be editable */
1078 #  if 0
1079   RNA_def_property_update(prop, NC_SCENE | ND_KEYINGSET | NA_RENAME, NULL);
1080 #  endif
1081 
1082   prop = RNA_def_property(srna, "bl_label", PROP_STRING, PROP_NONE);
1083   RNA_def_property_string_sdna(prop, NULL, "name");
1084   RNA_def_property_string_funcs(prop, NULL, NULL, "rna_KeyingSet_name_set");
1085   RNA_def_property_ui_text(prop, "UI Name", "");
1086   RNA_def_struct_ui_icon(srna, ICON_KEYINGSET);
1087   RNA_def_struct_name_property(srna, prop);
1088   RNA_def_property_update(prop, NC_SCENE | ND_KEYINGSET | NA_RENAME, NULL);
1089 
1090   prop = RNA_def_property(srna, "bl_description", PROP_STRING, PROP_NONE);
1091   RNA_def_property_string_sdna(prop, NULL, "description");
1092   RNA_def_property_string_maxlength(prop, RNA_DYN_DESCR_MAX); /* else it uses the pointer size! */
1093   RNA_def_property_ui_text(prop, "Description", "A short description of the keying set");
1094 
1095   /* KeyingSetInfo (Type Info) for Builtin Sets only  */
1096   prop = RNA_def_property(srna, "type_info", PROP_POINTER, PROP_NONE);
1097   RNA_def_property_struct_type(prop, "KeyingSetInfo");
1098   RNA_def_property_pointer_funcs(prop, "rna_KeyingSet_typeinfo_get", NULL, NULL, NULL);
1099   RNA_def_property_ui_text(
1100       prop, "Type Info", "Callback function defines for built-in Keying Sets");
1101 
1102   /* Paths */
1103   prop = RNA_def_property(srna, "paths", PROP_COLLECTION, PROP_NONE);
1104   RNA_def_property_collection_sdna(prop, NULL, "paths", NULL);
1105   RNA_def_property_struct_type(prop, "KeyingSetPath");
1106   RNA_def_property_ui_text(
1107       prop, "Paths", "Keying Set Paths to define settings that get keyframed together");
1108   rna_def_keyingset_paths(brna, prop);
1109 
1110   /* Flags */
1111   prop = RNA_def_property(srna, "is_path_absolute", PROP_BOOLEAN, PROP_NONE);
1112   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
1113   RNA_def_property_boolean_sdna(prop, NULL, "flag", KEYINGSET_ABSOLUTE);
1114   RNA_def_property_ui_text(prop,
1115                            "Absolute",
1116                            "Keying Set defines specific paths/settings to be keyframed "
1117                            "(i.e. is not reliant on context info)");
1118 
1119   /* Keyframing Flags */
1120   rna_def_common_keying_flags(srna, 0);
1121 
1122   /* Keying Set API */
1123   RNA_api_keyingset(srna);
1124 }
1125 
1126 #  undef KEYINGSET_IDNAME_DOC
1127 /* --- */
1128 
rna_api_animdata_nla_tracks(BlenderRNA * brna,PropertyRNA * cprop)1129 static void rna_api_animdata_nla_tracks(BlenderRNA *brna, PropertyRNA *cprop)
1130 {
1131   StructRNA *srna;
1132   PropertyRNA *parm;
1133   FunctionRNA *func;
1134 
1135   PropertyRNA *prop;
1136 
1137   RNA_def_property_srna(cprop, "NlaTracks");
1138   srna = RNA_def_struct(brna, "NlaTracks", NULL);
1139   RNA_def_struct_sdna(srna, "AnimData");
1140   RNA_def_struct_ui_text(srna, "NLA Tracks", "Collection of NLA Tracks");
1141 
1142   func = RNA_def_function(srna, "new", "rna_NlaTrack_new");
1143   RNA_def_function_flag(func, FUNC_USE_SELF_ID | FUNC_USE_MAIN | FUNC_USE_CONTEXT);
1144   RNA_def_function_ui_description(func, "Add a new NLA Track");
1145   RNA_def_pointer(func, "prev", "NlaTrack", "", "NLA Track to add the new one after");
1146   /* return type */
1147   parm = RNA_def_pointer(func, "track", "NlaTrack", "", "New NLA Track");
1148   RNA_def_function_return(func, parm);
1149 
1150   func = RNA_def_function(srna, "remove", "rna_NlaTrack_remove");
1151   RNA_def_function_flag(func,
1152                         FUNC_USE_SELF_ID | FUNC_USE_REPORTS | FUNC_USE_MAIN | FUNC_USE_CONTEXT);
1153   RNA_def_function_ui_description(func, "Remove a NLA Track");
1154   parm = RNA_def_pointer(func, "track", "NlaTrack", "", "NLA Track to remove");
1155   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED | PARM_RNAPTR);
1156   RNA_def_parameter_clear_flags(parm, PROP_THICK_WRAP, 0);
1157 
1158   prop = RNA_def_property(srna, "active", PROP_POINTER, PROP_NONE);
1159   RNA_def_property_struct_type(prop, "NlaTrack");
1160   RNA_def_property_pointer_funcs(
1161       prop, "rna_NlaTrack_active_get", "rna_NlaTrack_active_set", NULL, NULL);
1162   RNA_def_property_flag(prop, PROP_EDITABLE);
1163   RNA_def_property_ui_text(prop, "Active Track", "Active NLA Track");
1164   /* XXX: should (but doesn't) update the active track in the NLA window */
1165   RNA_def_property_update(prop, NC_ANIMATION | ND_NLA | NA_SELECTED, NULL);
1166 }
1167 
rna_api_animdata_drivers(BlenderRNA * brna,PropertyRNA * cprop)1168 static void rna_api_animdata_drivers(BlenderRNA *brna, PropertyRNA *cprop)
1169 {
1170   StructRNA *srna;
1171   PropertyRNA *parm;
1172   FunctionRNA *func;
1173 
1174   /* PropertyRNA *prop; */
1175 
1176   RNA_def_property_srna(cprop, "AnimDataDrivers");
1177   srna = RNA_def_struct(brna, "AnimDataDrivers", NULL);
1178   RNA_def_struct_sdna(srna, "AnimData");
1179   RNA_def_struct_ui_text(srna, "Drivers", "Collection of Driver F-Curves");
1180 
1181   /* Match: ActionFCurves.new/remove */
1182 
1183   /* AnimData.drivers.new(...) */
1184   func = RNA_def_function(srna, "new", "rna_Driver_new");
1185   RNA_def_function_flag(func, FUNC_USE_SELF_ID | FUNC_USE_REPORTS | FUNC_USE_MAIN);
1186   parm = RNA_def_string(func, "data_path", NULL, 0, "Data Path", "F-Curve data path to use");
1187   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1188   RNA_def_int(func, "index", 0, 0, INT_MAX, "Index", "Array index", 0, INT_MAX);
1189   /* return type */
1190   parm = RNA_def_pointer(func, "driver", "FCurve", "", "Newly Driver F-Curve");
1191   RNA_def_function_return(func, parm);
1192 
1193   /* AnimData.drivers.remove(...) */
1194   func = RNA_def_function(srna, "remove", "rna_Driver_remove");
1195   RNA_def_function_flag(func, FUNC_USE_REPORTS | FUNC_USE_MAIN);
1196   parm = RNA_def_pointer(func, "driver", "FCurve", "", "");
1197   RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED);
1198 
1199   /* AnimData.drivers.from_existing(...) */
1200   func = RNA_def_function(srna, "from_existing", "rna_Driver_from_existing");
1201   RNA_def_function_flag(func, FUNC_USE_CONTEXT);
1202   RNA_def_function_ui_description(func, "Add a new driver given an existing one");
1203   RNA_def_pointer(func,
1204                   "src_driver",
1205                   "FCurve",
1206                   "",
1207                   "Existing Driver F-Curve to use as template for a new one");
1208   /* return type */
1209   parm = RNA_def_pointer(func, "driver", "FCurve", "", "New Driver F-Curve");
1210   RNA_def_function_return(func, parm);
1211 
1212   /* AnimData.drivers.find(...) */
1213   func = RNA_def_function(srna, "find", "rna_Driver_find");
1214   RNA_def_function_ui_description(
1215       func,
1216       "Find a driver F-Curve. Note that this function performs a linear scan "
1217       "of all driver F-Curves.");
1218   RNA_def_function_flag(func, FUNC_USE_REPORTS);
1219   parm = RNA_def_string(func, "data_path", NULL, 0, "Data Path", "F-Curve data path");
1220   RNA_def_parameter_flags(parm, 0, PARM_REQUIRED);
1221   RNA_def_int(func, "index", 0, 0, INT_MAX, "Index", "Array index", 0, INT_MAX);
1222   /* return type */
1223   parm = RNA_def_pointer(
1224       func, "fcurve", "FCurve", "", "The found F-Curve, or None if it doesn't exist");
1225   RNA_def_function_return(func, parm);
1226 }
1227 
rna_def_animdata_common(StructRNA * srna)1228 void rna_def_animdata_common(StructRNA *srna)
1229 {
1230   PropertyRNA *prop;
1231 
1232   prop = RNA_def_property(srna, "animation_data", PROP_POINTER, PROP_NONE);
1233   RNA_def_property_pointer_sdna(prop, NULL, "adt");
1234   RNA_def_property_clear_flag(prop, PROP_EDITABLE);
1235   RNA_def_property_override_flag(prop, PROPOVERRIDE_OVERRIDABLE_LIBRARY);
1236   RNA_def_property_override_funcs(prop, NULL, NULL, "rna_AnimaData_override_apply");
1237   RNA_def_property_ui_text(prop, "Animation Data", "Animation data for this data-block");
1238 }
1239 
rna_def_animdata(BlenderRNA * brna)1240 static void rna_def_animdata(BlenderRNA *brna)
1241 {
1242   StructRNA *srna;
1243   PropertyRNA *prop;
1244 
1245   srna = RNA_def_struct(brna, "AnimData", NULL);
1246   RNA_def_struct_ui_text(srna, "Animation Data", "Animation data for data-block");
1247   RNA_def_struct_ui_icon(srna, ICON_ANIM_DATA);
1248 
1249   /* NLA */
1250   prop = RNA_def_property(srna, "nla_tracks", PROP_COLLECTION, PROP_NONE);
1251   RNA_def_property_collection_sdna(prop, NULL, "nla_tracks", NULL);
1252   RNA_def_property_struct_type(prop, "NlaTrack");
1253   RNA_def_property_ui_text(prop, "NLA Tracks", "NLA Tracks (i.e. Animation Layers)");
1254 
1255   rna_api_animdata_nla_tracks(brna, prop);
1256 
1257   /* Active Action */
1258   prop = RNA_def_property(srna, "action", PROP_POINTER, PROP_NONE);
1259   /* this flag as well as the dynamic test must be defined for this to be editable... */
1260   RNA_def_property_flag(prop, PROP_EDITABLE | PROP_ID_REFCOUNT);
1261   RNA_def_property_override_flag(prop, PROPOVERRIDE_OVERRIDABLE_LIBRARY);
1262   RNA_def_property_pointer_funcs(
1263       prop, NULL, "rna_AnimData_action_set", NULL, "rna_Action_id_poll");
1264   RNA_def_property_editable_func(prop, "rna_AnimData_action_editable");
1265   RNA_def_property_ui_text(prop, "Action", "Active Action for this data-block");
1266   RNA_def_property_update(prop, NC_ANIMATION | ND_NLA_ACTCHANGE, "rna_AnimData_dependency_update");
1267 
1268   /* Active Action Settings */
1269   prop = RNA_def_property(srna, "action_extrapolation", PROP_ENUM, PROP_NONE);
1270   RNA_def_property_enum_sdna(prop, NULL, "act_extendmode");
1271   RNA_def_property_enum_items(prop, rna_enum_nla_mode_extend_items);
1272   RNA_def_property_ui_text(
1273       prop,
1274       "Action Extrapolation",
1275       "Action to take for gaps past the Active Action's range (when evaluating with NLA)");
1276   RNA_def_property_update(prop, NC_ANIMATION | ND_NLA, "rna_AnimData_update");
1277 
1278   prop = RNA_def_property(srna, "action_blend_type", PROP_ENUM, PROP_NONE);
1279   RNA_def_property_enum_sdna(prop, NULL, "act_blendmode");
1280   RNA_def_property_enum_items(prop, rna_enum_nla_mode_blend_items);
1281   RNA_def_property_ui_text(
1282       prop,
1283       "Action Blending",
1284       "Method used for combining Active Action's result with result of NLA stack");
1285   RNA_def_property_update(prop, NC_ANIMATION | ND_NLA, "rna_AnimData_update"); /* this will do? */
1286 
1287   prop = RNA_def_property(srna, "action_influence", PROP_FLOAT, PROP_FACTOR);
1288   RNA_def_property_float_sdna(prop, NULL, "act_influence");
1289   RNA_def_property_float_default(prop, 1.0f);
1290   RNA_def_property_range(prop, 0.0f, 1.0f);
1291   RNA_def_property_ui_text(prop,
1292                            "Action Influence",
1293                            "Amount the Active Action contributes to the result of the NLA stack");
1294   RNA_def_property_update(prop, NC_ANIMATION | ND_NLA, "rna_AnimData_update"); /* this will do? */
1295 
1296   /* Drivers */
1297   prop = RNA_def_property(srna, "drivers", PROP_COLLECTION, PROP_NONE);
1298   RNA_def_property_collection_sdna(prop, NULL, "drivers", NULL);
1299   RNA_def_property_struct_type(prop, "FCurve");
1300   RNA_def_property_override_flag(prop, PROPOVERRIDE_OVERRIDABLE_LIBRARY);
1301   RNA_def_property_ui_text(prop, "Drivers", "The Drivers/Expressions for this data-block");
1302 
1303   rna_api_animdata_drivers(brna, prop);
1304 
1305   /* General Settings */
1306   prop = RNA_def_property(srna, "use_nla", PROP_BOOLEAN, PROP_NONE);
1307   RNA_def_property_boolean_negative_sdna(prop, NULL, "flag", ADT_NLA_EVAL_OFF);
1308   RNA_def_property_ui_text(
1309       prop, "NLA Evaluation Enabled", "NLA stack is evaluated when evaluating this block");
1310   RNA_def_property_update(prop, NC_ANIMATION | ND_NLA, "rna_AnimData_update"); /* this will do? */
1311 
1312   prop = RNA_def_property(srna, "use_tweak_mode", PROP_BOOLEAN, PROP_NONE);
1313   RNA_def_property_boolean_sdna(prop, NULL, "flag", ADT_NLA_EDIT_ON);
1314   RNA_def_property_boolean_funcs(prop, NULL, "rna_AnimData_tweakmode_set");
1315   RNA_def_property_ui_text(
1316       prop, "Use NLA Tweak Mode", "Whether to enable or disable tweak mode in NLA");
1317   RNA_def_property_update(prop, NC_ANIMATION | ND_NLA, "rna_AnimData_update");
1318 
1319   prop = RNA_def_property(srna, "use_pin", PROP_BOOLEAN, PROP_NONE);
1320   RNA_def_property_flag(prop, PROP_NO_DEG_UPDATE);
1321   RNA_def_property_boolean_sdna(prop, NULL, "flag", ADT_CURVES_ALWAYS_VISIBLE);
1322   RNA_def_property_ui_text(prop, "Pin in Graph Editor", "");
1323   RNA_def_property_update(prop, NC_ANIMATION | ND_ANIMCHAN | NA_EDITED, NULL);
1324 
1325   /* Animation Data API */
1326   RNA_api_animdata(srna);
1327 }
1328 
1329 /* --- */
1330 
RNA_def_animation(BlenderRNA * brna)1331 void RNA_def_animation(BlenderRNA *brna)
1332 {
1333   rna_def_animdata(brna);
1334 
1335   rna_def_keyingset(brna);
1336   rna_def_keyingset_path(brna);
1337   rna_def_keyingset_info(brna);
1338 }
1339 
1340 #endif
1341