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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 *****
36 #include "MEM_guardedalloc.h"
38 #include "BLI_blenlib.h"
39 #include "BLI_arithb.h"
40 #include "BLI_dynstr.h"
42 #include "DNA_anim_types.h"
43 #include "DNA_action_types.h"
44 #include "DNA_armature_types.h"
45 #include "DNA_constraint_types.h"
46 #include "DNA_key_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_material_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_userdef_types.h"
51 #include "DNA_windowmanager_types.h"
53 #include "BKE_animsys.h"
54 #include "BKE_action.h"
55 #include "BKE_constraint.h"
56 #include "BKE_fcurve.h"
57 #include "BKE_utildefines.h"
58 #include "BKE_context.h"
59 #include "BKE_report.h"
61 #include "BKE_material.h"
63 #include "ED_anim_api.h"
64 #include "ED_keyframing.h"
65 #include "ED_keyframes_edit.h"
66 #include "ED_screen.h"
69 #include "UI_interface.h"
74 #include "RNA_access.h"
75 #include "RNA_define.h"
76 #include "RNA_types.h"
78 #include "anim_intern.h"
80 /* ************************************************** */
81 /* KEYING SETS - EDITING API */
83 /* UI API --------------------------------------------- */
85 /* Build menu-string of available keying-sets (allocates memory for string)
86 * NOTE: mode must not be longer than 64 chars
88 char *ANIM_build_keyingsets_menu (ListBase *list, short for_edit)
90 DynStr *pupds= BLI_dynstr_new();
97 BLI_dynstr_append(pupds, "Keying Sets%t|");
99 /* add dummy entries for none-active */
101 BLI_dynstr_append(pupds, "Add New%x-1|");
102 BLI_dynstr_append(pupds, " %x0|");
105 BLI_dynstr_append(pupds, "No Keying Set%x0|");
107 /* loop through keyingsets, adding them */
108 for (ks=list->first, i=1; ks; ks=ks->next, i++) {
110 BLI_dynstr_append(pupds, "KS: ");
112 BLI_dynstr_append(pupds, ks->name);
113 BLI_snprintf( buf, 64, "%%x%d%s", i, ((ks->next)?"|":"") );
114 BLI_dynstr_append(pupds, buf);
117 /* convert to normal MEM_malloc'd string */
118 str= BLI_dynstr_get_cstring(pupds);
119 BLI_dynstr_free(pupds);
125 /* ******************************************* */
126 /* KEYING SETS - BUILTIN */
128 #if 0 // XXX old keyingsets code based on adrcodes... to be restored in due course
130 /* ------------- KeyingSet Defines ------------ */
131 /* Note: these must all be named with the defks_* prefix, otherwise the template macro will not work! */
133 /* macro for defining keyingset contexts */
134 #define KSC_TEMPLATE(ctx_name) {&defks_##ctx_name[0], NULL, sizeof(defks_##ctx_name)/sizeof(bKeyingSet)}
138 /* check if option not available for deleting keys */
139 static short incl_non_del_keys (bKeyingSet *ks, const char mode[])
141 /* as optimisation, assume that it is sufficient to check only first letter
142 * of mode (int comparison should be faster than string!)
144 //if (strcmp(mode, "Delete")==0)
145 if (mode && mode[0]=='D')
151 /* Object KeyingSets ------ */
153 /* check if include shapekey entry */
154 static short incl_v3d_ob_shapekey (bKeyingSet *ks, const char mode[])
156 //Object *ob= (G.obedit)? (G.obedit) : (OBACT); // XXX
163 /* not available for delete mode */
164 if (strcmp(mode, "Delete")==0)
167 /* check if is geom object that can get shapekeys */
170 case OB_MESH: newname= "Mesh"; break;
171 case OB_CURVE: newname= "Curve"; break;
172 case OB_SURF: newname= "Surface"; break;
173 case OB_LATTICE: newname= "Lattice"; break;
180 /* if ks is shapekey entry (this could be callled for separator before too!) */
182 BLI_strncpy(ks->name, newname, sizeof(ks->name));
184 /* if it gets here, it's ok */
188 /* array for object keyingset defines */
189 bKeyingSet defks_v3d_object[] =
191 /* include_cb, adrcode-getter, name, blocktype, flag, chan_num, adrcodes */
192 {NULL, "Loc", ID_OB, 0, 3, {OB_LOC_X,OB_LOC_Y,OB_LOC_Z}},
193 {NULL, "Rot", ID_OB, 0, 3, {OB_ROT_X,OB_ROT_Y,OB_ROT_Z}},
194 {NULL, "Scale", ID_OB, 0, 3, {OB_SIZE_X,OB_SIZE_Y,OB_SIZE_Z}},
196 {NULL, "%l", 0, -1, 0, {0}}, // separator
198 {NULL, "LocRot", ID_OB, 0, 6,
199 {OB_LOC_X,OB_LOC_Y,OB_LOC_Z,
200 OB_ROT_X,OB_ROT_Y,OB_ROT_Z}},
202 {NULL, "LocScale", ID_OB, 0, 6,
203 {OB_LOC_X,OB_LOC_Y,OB_LOC_Z,
204 OB_SIZE_X,OB_SIZE_Y,OB_SIZE_Z}},
206 {NULL, "LocRotScale", ID_OB, 0, 9,
207 {OB_LOC_X,OB_LOC_Y,OB_LOC_Z,
208 OB_ROT_X,OB_ROT_Y,OB_ROT_Z,
209 OB_SIZE_X,OB_SIZE_Y,OB_SIZE_Z}},
211 {NULL, "RotScale", ID_OB, 0, 6,
212 {OB_ROT_X,OB_ROT_Y,OB_ROT_Z,
213 OB_SIZE_X,OB_SIZE_Y,OB_SIZE_Z}},
215 {incl_non_del_keys, "%l", 0, -1, 0, {0}}, // separator
217 {incl_non_del_keys, "VisualLoc", ID_OB, INSERTKEY_MATRIX, 3, {OB_LOC_X,OB_LOC_Y,OB_LOC_Z}},
218 {incl_non_del_keys, "VisualRot", ID_OB, INSERTKEY_MATRIX, 3, {OB_ROT_X,OB_ROT_Y,OB_ROT_Z}},
220 {incl_non_del_keys, "VisualLocRot", ID_OB, INSERTKEY_MATRIX, 6,
221 {OB_LOC_X,OB_LOC_Y,OB_LOC_Z,
222 OB_ROT_X,OB_ROT_Y,OB_ROT_Z}},
224 {NULL, "%l", 0, -1, 0, {0}}, // separator
226 {NULL, "Layer", ID_OB, 0, 1, {OB_LAY}}, // icky option...
227 {NULL, "Available", ID_OB, -2, 0, {0}},
229 {incl_v3d_ob_shapekey, "%l%l", 0, -1, 0, {0}}, // separator (linked to shapekey entry)
230 {incl_v3d_ob_shapekey, "<ShapeKey>", ID_OB, -3, 0, {0}}
233 /* PoseChannel KeyingSets ------ */
235 /* array for posechannel keyingset defines */
236 bKeyingSet defks_v3d_pchan[] =
238 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
239 {NULL, "Loc", ID_PO, 0, 3, {AC_LOC_X,AC_LOC_Y,AC_LOC_Z}},
240 {NULL, "Rot", ID_PO, COMMONKEY_PCHANROT, 1, {KAG_CHAN_EXTEND}},
241 {NULL, "Scale", ID_PO, 0, 3, {AC_SIZE_X,AC_SIZE_Y,AC_SIZE_Z}},
243 {NULL, "%l", 0, -1, 0, {0}}, // separator
245 {NULL, "LocRot", ID_PO, COMMONKEY_PCHANROT, 4,
246 {AC_LOC_X,AC_LOC_Y,AC_LOC_Z,
249 {NULL, "LocScale", ID_PO, 0, 6,
250 {AC_LOC_X,AC_LOC_Y,AC_LOC_Z,
251 AC_SIZE_X,AC_SIZE_Y,AC_SIZE_Z}},
253 {NULL, "LocRotScale", ID_PO, COMMONKEY_PCHANROT, 7,
254 {AC_LOC_X,AC_LOC_Y,AC_LOC_Z,AC_SIZE_X,AC_SIZE_Y,AC_SIZE_Z,
257 {NULL, "RotScale", ID_PO, 0, 4,
258 {AC_SIZE_X,AC_SIZE_Y,AC_SIZE_Z,
261 {incl_non_del_keys, "%l", 0, -1, 0, {0}}, // separator
263 {incl_non_del_keys, "VisualLoc", ID_PO, INSERTKEY_MATRIX, 3, {AC_LOC_X,AC_LOC_Y,AC_LOC_Z}},
264 {incl_non_del_keys, "VisualRot", ID_PO, INSERTKEY_MATRIX|COMMONKEY_PCHANROT, 1, {KAG_CHAN_EXTEND}},
266 {incl_non_del_keys, "VisualLocRot", ID_PO, INSERTKEY_MATRIX|COMMONKEY_PCHANROT, 4,
267 {AC_LOC_X,AC_LOC_Y,AC_LOC_Z, KAG_CHAN_EXTEND}},
269 {NULL, "%l", 0, -1, 0, {0}}, // separator
271 {NULL, "Available", ID_PO, -2, 0, {0}}
274 /* Material KeyingSets ------ */
276 /* array for material keyingset defines */
277 bKeyingSet defks_buts_shading_mat[] =
279 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
280 {NULL, "RGB", ID_MA, 0, 3, {MA_COL_R,MA_COL_G,MA_COL_B}},
281 {NULL, "Alpha", ID_MA, 0, 1, {MA_ALPHA}},
282 {NULL, "Halo Size", ID_MA, 0, 1, {MA_HASIZE}},
283 {NULL, "Mode", ID_MA, 0, 1, {MA_MODE}}, // evil bitflags
285 {NULL, "%l", 0, -1, 0, {0}}, // separator
287 {NULL, "All Color", ID_MA, 0, 18,
288 {MA_COL_R,MA_COL_G,MA_COL_B,
289 MA_ALPHA,MA_HASIZE, MA_MODE,
290 MA_SPEC_R,MA_SPEC_G,MA_SPEC_B,
291 MA_REF,MA_EMIT,MA_AMB,MA_SPEC,MA_HARD,
292 MA_MODE,MA_TRANSLU,MA_ADD}},
294 {NULL, "All Mirror", ID_MA, 0, 5,
295 {MA_RAYM,MA_FRESMIR,MA_FRESMIRI,
296 MA_FRESTRA,MA_FRESTRAI}},
298 {NULL, "%l", 0, -1, 0, {0}}, // separator
300 {NULL, "Ofs", ID_MA, COMMONKEY_ADDMAP, 3, {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z}},
301 {NULL, "Size", ID_MA, COMMONKEY_ADDMAP, 3, {MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z}},
303 {NULL, "All Mapping", ID_MA, COMMONKEY_ADDMAP, 14,
304 {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z,
305 MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z,
306 MAP_R,MAP_G,MAP_B,MAP_DVAR,
307 MAP_COLF,MAP_NORF,MAP_VARF,MAP_DISP}},
309 {NULL, "%l", 0, -1, 0, {0}}, // separator
311 {NULL, "Available", ID_MA, -2, 0, {0}}
314 /* World KeyingSets ------ */
316 /* array for world keyingset defines */
317 bKeyingSet defks_buts_shading_wo[] =
319 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
320 {NULL, "Zenith RGB", ID_WO, 0, 3, {WO_ZEN_R,WO_ZEN_G,WO_ZEN_B}},
321 {NULL, "Horizon RGB", ID_WO, 0, 3, {WO_HOR_R,WO_HOR_G,WO_HOR_B}},
323 {NULL, "%l", 0, -1, 0, {0}}, // separator
325 {NULL, "Mist", ID_WO, 0, 4, {WO_MISI,WO_MISTDI,WO_MISTSTA,WO_MISTHI}},
326 {NULL, "Stars", ID_WO, 0, 5, {WO_STAR_R,WO_STAR_G,WO_STAR_B,WO_STARDIST,WO_STARSIZE}},
329 {NULL, "%l", 0, -1, 0, {0}}, // separator
331 {NULL, "Ofs", ID_WO, COMMONKEY_ADDMAP, 3, {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z}},
332 {NULL, "Size", ID_WO, COMMONKEY_ADDMAP, 3, {MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z}},
334 {NULL, "All Mapping", ID_WO, COMMONKEY_ADDMAP, 14,
335 {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z,
336 MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z,
337 MAP_R,MAP_G,MAP_B,MAP_DVAR,
338 MAP_COLF,MAP_NORF,MAP_VARF,MAP_DISP}},
340 {NULL, "%l", 0, -1, 0, {0}}, // separator
342 {NULL, "Available", ID_WO, -2, 0, {0}}
345 /* Lamp KeyingSets ------ */
347 /* array for lamp keyingset defines */
348 bKeyingSet defks_buts_shading_la[] =
350 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
351 {NULL, "RGB", ID_LA, 0, 3, {LA_COL_R,LA_COL_G,LA_COL_B}},
352 {NULL, "Energy", ID_LA, 0, 1, {LA_ENERGY}},
353 {NULL, "Spot Size", ID_LA, 0, 1, {LA_SPOTSI}},
355 {NULL, "%l", 0, -1, 0, {0}}, // separator
357 {NULL, "Ofs", ID_LA, COMMONKEY_ADDMAP, 3, {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z}},
358 {NULL, "Size", ID_LA, COMMONKEY_ADDMAP, 3, {MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z}},
360 {NULL, "All Mapping", ID_LA, COMMONKEY_ADDMAP, 14,
361 {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z,
362 MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z,
363 MAP_R,MAP_G,MAP_B,MAP_DVAR,
364 MAP_COLF,MAP_NORF,MAP_VARF,MAP_DISP}},
366 {NULL, "%l", 0, -1, 0, {0}}, // separator
368 {NULL, "Available", ID_LA, -2, 0, {0}}
371 /* Texture KeyingSets ------ */
373 /* array for texture keyingset defines */
374 bKeyingSet defks_buts_shading_tex[] =
376 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
377 {NULL, "Clouds", ID_TE, 0, 5,
378 {TE_NSIZE,TE_NDEPTH,TE_NTYPE,
379 TE_MG_TYP,TE_N_BAS1}},
381 {NULL, "Marble", ID_TE, 0, 7,
382 {TE_NSIZE,TE_NDEPTH,TE_NTYPE,
383 TE_TURB,TE_MG_TYP,TE_N_BAS1,TE_N_BAS2}},
385 {NULL, "Stucci", ID_TE, 0, 5,
386 {TE_NSIZE,TE_NTYPE,TE_TURB,
387 TE_MG_TYP,TE_N_BAS1}},
389 {NULL, "Wood", ID_TE, 0, 6,
390 {TE_NSIZE,TE_NTYPE,TE_TURB,
391 TE_MG_TYP,TE_N_BAS1,TE_N_BAS2}},
393 {NULL, "Magic", ID_TE, 0, 2, {TE_NDEPTH,TE_TURB}},
395 {NULL, "Blend", ID_TE, 0, 1, {TE_MG_TYP}},
397 {NULL, "Musgrave", ID_TE, 0, 6,
398 {TE_MG_TYP,TE_MGH,TE_MG_LAC,
399 TE_MG_OCT,TE_MG_OFF,TE_MG_GAIN}},
401 {NULL, "Voronoi", ID_TE, 0, 9,
402 {TE_VNW1,TE_VNW2,TE_VNW3,TE_VNW4,
403 TE_VNMEXP,TE_VN_DISTM,TE_VN_COLT,
406 {NULL, "Distorted Noise", ID_TE, 0, 4,
407 {TE_MG_OCT,TE_MG_OFF,TE_MG_GAIN,TE_DISTA}},
409 {NULL, "Color Filter", ID_TE, 0, 5,
410 {TE_COL_R,TE_COL_G,TE_COL_B,TE_BRIGHT,TE_CONTRA}},
412 {NULL, "%l", 0, -1, 0, {0}}, // separator
414 {NULL, "Available", ID_TE, -2, 0, {0}}
417 /* Object Buttons KeyingSets ------ */
419 /* check if include particles entry */
420 static short incl_buts_ob (bKeyingSet *ks, const char mode[])
422 //Object *ob= OBACT; // xxx
424 /* only if object is mesh type */
426 if(ob==NULL) return 0;
427 return (ob->type == OB_MESH);
430 /* array for texture keyingset defines */
431 bKeyingSet defks_buts_object[] =
433 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
434 {incl_buts_ob, "Surface Damping", ID_OB, 0, 1, {OB_PD_SDAMP}},
435 {incl_buts_ob, "Random Damping", ID_OB, 0, 1, {OB_PD_RDAMP}},
436 {incl_buts_ob, "Permeability", ID_OB, 0, 1, {OB_PD_PERM}},
438 {NULL, "%l", 0, -1, 0, {0}}, // separator
440 {NULL, "Force Strength", ID_OB, 0, 1, {OB_PD_FSTR}},
441 {NULL, "Force Falloff", ID_OB, 0, 1, {OB_PD_FFALL}},
443 {NULL, "%l", 0, -1, 0, {0}}, // separator
445 {NULL, "Available", ID_OB, -2, 0, {0}} // this will include ob-transforms too!
448 /* Camera Buttons KeyingSets ------ */
450 /* check if include internal-renderer entry */
451 static short incl_buts_cam1 (bKeyingSet *ks, const char mode[])
453 Scene *scene= NULL; // FIXME this will cause a crash, but we need an extra arg first!
454 /* only if renderer is internal renderer */
455 return (scene->r.renderer==R_INTERN);
458 /* check if include external-renderer entry */
459 static short incl_buts_cam2 (bKeyingSet *ks, const char mode[])
461 Scene *scene= NULL; // FIXME this will cause a crash, but we need an extra arg first!
462 /* only if renderer is internal renderer */
463 return (scene->r.renderer!=R_INTERN);
466 /* array for camera keyingset defines */
467 bKeyingSet defks_buts_cam[] =
469 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
470 {NULL, "Lens", ID_CA, 0, 1, {CAM_LENS}},
471 {NULL, "Clipping", ID_CA, 0, 2, {CAM_STA,CAM_END}},
472 {NULL, "Focal Distance", ID_CA, 0, 1, {CAM_YF_FDIST}},
474 {NULL, "%l", 0, -1, 0, {0}}, // separator
477 {incl_buts_cam2, "Aperture", ID_CA, 0, 1, {CAM_YF_APERT}},
478 {incl_buts_cam1, "Viewplane Shift", ID_CA, 0, 2, {CAM_SHIFT_X,CAM_SHIFT_Y}},
480 {NULL, "%l", 0, -1, 0, {0}}, // separator
482 {NULL, "Available", ID_CA, -2, 0, {0}}
487 /* Keying Context Defines - Must keep in sync with enumeration (eKS_Contexts) */
488 bKeyingContext ks_contexts[] =
490 KSC_TEMPLATE(v3d_object),
491 KSC_TEMPLATE(v3d_pchan),
493 KSC_TEMPLATE(buts_shading_mat),
494 KSC_TEMPLATE(buts_shading_wo),
495 KSC_TEMPLATE(buts_shading_la),
496 KSC_TEMPLATE(buts_shading_tex),
498 KSC_TEMPLATE(buts_object),
499 KSC_TEMPLATE(buts_cam)
502 /* Keying Context Enumeration - Must keep in sync with definitions*/
503 typedef enum eKS_Contexts {
515 /* make sure this last one remains untouched! */
520 #endif // XXX old keyingsets code based on adrcodes... to be restored in due course
522 /* Macros for Declaring KeyingSets ------------------- */
524 /* A note about this system for declaring built-in Keying Sets:
525 * One may ask, "What is the purpose of all of these macros and static arrays?" and
526 * "Why not call the KeyingSets API defined in BKE_animsys.h?". The answer is two-fold.
528 * 1) Firstly, we use static arrays of struct definitions instead of function calls, as
529 * it reduces the start-up overhead and allocated-memory footprint of Blender. If we called
530 * the KeyingSets API to build these sets, the overhead of checking for unique names, allocating
531 * memory for each and every path and KeyingSet, scattered around in RAM, all of which would increase
532 * the startup time (which is totally unacceptable) and could lead to fragmentation+slower access times.
533 * 2) Since we aren't using function calls, we need a nice way of defining these KeyingSets in a way which
534 * is easily readable and less prone to breakage from changes to the underlying struct definitions. Further,
535 * adding additional entries SHOULD NOT require custom code to be written to access these new entries/sets.
536 * Therefore, here we have a system with nice, human-readable statements via macros, and static arrays which
537 * are linked together using more special macros + struct definitions, allowing for such a generic + simple
538 * initialisation function (init_builtin_keyingsets()) compared with that of something like the Nodes system.
540 * -- Joshua Leung, April 2009
543 /* Struct type for declaring builtin KeyingSets in as entries in static arrays*/
544 typedef struct bBuiltinKeyingSet {
545 KeyingSet ks; /* the KeyingSet to build */
546 int tot; /* the total number of paths defined */
547 KS_Path paths[64]; /* the paths for the KeyingSet to use */
550 /* WARNING: the following macros must be kept in sync with the
551 * struct definitions in DNA_anim_types.h!
554 /* macro for defining a builtin KeyingSet */
555 #define BI_KS_DEFINE_BEGIN(name, keyingflag) \
556 {{NULL, NULL, {NULL, NULL}, name, KEYINGSET_BUILTIN, keyingflag},
558 /* macro to finish defining a builtin KeyingSet */
559 #define BI_KS_DEFINE_END \
562 /* macro to start defining paths for a builtin KeyingSet */
563 #define BI_KS_PATHS_BEGIN(tot) \
566 /* macro to finish defining paths for a builtin KeyingSet */
567 #define BI_KS_PATHS_END \
570 /* macro for defining a builtin KeyingSet's path */
571 #define BI_KSP_DEFINE(id_type, templates, prop_path, array_index, flag, groupflag) \
572 {NULL, NULL, NULL, "", id_type, templates, prop_path, array_index, flag, groupflag}
574 /* macro for defining a builtin KeyingSet with no paths (use in place of BI_KS_PAHTS_BEGIN/END block) */
575 #define BI_KS_PATHS_NONE \
580 /* Struct type for finding all the arrays of builtin KeyingSets */
581 typedef struct bBuiltinKSContext {
582 bBuiltinKeyingSet *bks; /* array of KeyingSet definitions */
583 int tot; /* number of KeyingSets in this array */
586 /* macro for defining builtin KeyingSet sets
587 * NOTE: all the arrays of sets must follow this naming convention!
589 #define BKSC_TEMPLATE(ctx_name) {&def_builtin_keyingsets_##ctx_name[0], sizeof(def_builtin_keyingsets_##ctx_name)/sizeof(bBuiltinKeyingSet)}
592 /* 3D-View Builtin KeyingSets ------------------------ */
594 static bBuiltinKeyingSet def_builtin_keyingsets_v3d[] =
596 /* Simple Keying Sets ************************************* */
597 /* Keying Set - "Location" ---------- */
598 BI_KS_DEFINE_BEGIN("Location", 0)
600 BI_KSP_DEFINE(ID_OB, KSP_TEMPLATE_OBJECT|KSP_TEMPLATE_PCHAN, "location", 0, KSP_FLAG_WHOLE_ARRAY, KSP_GROUP_TEMPLATE_ITEM)
604 /* Keying Set - "Rotation" ---------- */
605 BI_KS_DEFINE_BEGIN("Rotation", 0)
607 BI_KSP_DEFINE(ID_OB, KSP_TEMPLATE_OBJECT|KSP_TEMPLATE_PCHAN|KSP_TEMPLATE_PCHAN_ROT, "rotation", 0, KSP_FLAG_WHOLE_ARRAY, KSP_GROUP_TEMPLATE_ITEM)
611 /* Keying Set - "Scaling" ---------- */
612 BI_KS_DEFINE_BEGIN("Scaling", 0)
614 BI_KSP_DEFINE(ID_OB, KSP_TEMPLATE_OBJECT|KSP_TEMPLATE_PCHAN, "scale", 0, KSP_FLAG_WHOLE_ARRAY, KSP_GROUP_TEMPLATE_ITEM)
618 /* Compound Keying Sets *********************************** */
619 /* Keying Set - "LocRot" ---------- */
620 BI_KS_DEFINE_BEGIN("LocRot", 0)
622 BI_KSP_DEFINE(ID_OB, KSP_TEMPLATE_OBJECT|KSP_TEMPLATE_PCHAN, "location", 0, KSP_FLAG_WHOLE_ARRAY, KSP_GROUP_TEMPLATE_ITEM),
623 BI_KSP_DEFINE(ID_OB, KSP_TEMPLATE_OBJECT|KSP_TEMPLATE_PCHAN|KSP_TEMPLATE_PCHAN_ROT, "rotation", 0, KSP_FLAG_WHOLE_ARRAY, KSP_GROUP_TEMPLATE_ITEM)
627 /* Keying Set - "LocRotScale" ---------- */
628 BI_KS_DEFINE_BEGIN("LocRotScale", 0)
630 BI_KSP_DEFINE(ID_OB, KSP_TEMPLATE_OBJECT|KSP_TEMPLATE_PCHAN, "location", 0, KSP_FLAG_WHOLE_ARRAY, KSP_GROUP_TEMPLATE_ITEM),
631 BI_KSP_DEFINE(ID_OB, KSP_TEMPLATE_OBJECT|KSP_TEMPLATE_PCHAN|KSP_TEMPLATE_PCHAN_ROT, "rotation", 0, KSP_FLAG_WHOLE_ARRAY, KSP_GROUP_TEMPLATE_ITEM),
632 BI_KSP_DEFINE(ID_OB, KSP_TEMPLATE_OBJECT|KSP_TEMPLATE_PCHAN, "scale", 0, KSP_FLAG_WHOLE_ARRAY, KSP_GROUP_TEMPLATE_ITEM)
636 /* Keying Sets with Keying Flags ************************* */
637 /* Keying Set - "VisualLoc" ---------- */
638 BI_KS_DEFINE_BEGIN("VisualLoc", 0)
640 BI_KSP_DEFINE(ID_OB, KSP_TEMPLATE_OBJECT|KSP_TEMPLATE_PCHAN, "location", INSERTKEY_MATRIX, KSP_FLAG_WHOLE_ARRAY, KSP_GROUP_TEMPLATE_ITEM)
644 /* Keying Set - "Rotation" ---------- */
645 BI_KS_DEFINE_BEGIN("VisualRot", 0)
647 BI_KSP_DEFINE(ID_OB, KSP_TEMPLATE_OBJECT|KSP_TEMPLATE_PCHAN|KSP_TEMPLATE_PCHAN_ROT, "rotation", INSERTKEY_MATRIX, KSP_FLAG_WHOLE_ARRAY, KSP_GROUP_TEMPLATE_ITEM)
651 /* Keying Set - "VisualLocRot" ---------- */
652 BI_KS_DEFINE_BEGIN("VisualLocRot", 0)
654 BI_KSP_DEFINE(ID_OB, KSP_TEMPLATE_OBJECT|KSP_TEMPLATE_PCHAN, "location", INSERTKEY_MATRIX, KSP_FLAG_WHOLE_ARRAY, KSP_GROUP_TEMPLATE_ITEM),
655 BI_KSP_DEFINE(ID_OB, KSP_TEMPLATE_OBJECT|KSP_TEMPLATE_PCHAN|KSP_TEMPLATE_PCHAN_ROT, "rotation", INSERTKEY_MATRIX, KSP_FLAG_WHOLE_ARRAY, KSP_GROUP_TEMPLATE_ITEM)
660 /* All Builtin KeyingSets ------------------------ */
662 /* total number of builtin KeyingSet contexts */
663 #define MAX_BKSC_TYPES 1
665 /* array containing all the available builtin KeyingSets definition sets
666 * - size of this is MAX_BKSC_TYPES+1 so that we don't smash the stack
668 static bBuiltinKSContext def_builtin_keyingsets[MAX_BKSC_TYPES+1] =
671 /* add more contexts above this line... */
675 /* ListBase of these KeyingSets chained up ready for usage
676 * NOTE: this is exported to keyframing.c for use...
678 ListBase builtin_keyingsets = {NULL, NULL};
680 /* Utility API ------------------------ */
682 /* Link up all of the builtin Keying Sets when starting up Blender
683 * This is called from WM_init() in wm_init_exit.c
685 void init_builtin_keyingsets (void)
687 bBuiltinKSContext *bksc;
688 bBuiltinKeyingSet *bks;
691 /* loop over all the sets of KeyingSets, setting them up, and chaining them to the builtins list */
692 for (bksc_i= 0, bksc= &def_builtin_keyingsets[0]; bksc_i < MAX_BKSC_TYPES; bksc_i++, bksc++)
694 /* for each set definitions for a builtin KeyingSet, chain the paths to that KeyingSet and add */
695 for (bks_i= 0, bks= bksc->bks; bks_i < bksc->tot; bks_i++, bks++)
697 KeyingSet *ks= &bks->ks;
701 /* loop over paths, linking them to the KeyingSet and each other */
702 for (pIndex= 0, ksp= &bks->paths[0]; pIndex < bks->tot; pIndex++, ksp++)
703 BLI_addtail(&ks->paths, ksp);
705 /* add KeyingSet to builtin sets list */
706 BLI_addtail(&builtin_keyingsets, ks);
712 /* Get the first builtin KeyingSet with the given name, which occurs after the given one (or start of list if none given) */
713 KeyingSet *ANIM_builtin_keyingset_get_named (KeyingSet *prevKS, char name[])
715 KeyingSet *ks, *first=NULL;
717 /* sanity checks - any name to check? */
721 /* get first KeyingSet to use */
722 if (prevKS && prevKS->next)
725 first= builtin_keyingsets.first;
727 /* loop over KeyingSets checking names */
728 for (ks= first; ks; ks= ks->next) {
729 if (strcmp(name, ks->name) == 0)
733 /* no matches found */
737 /* ******************************************* */
738 /* KEYFRAME MODIFICATION */
740 /* KeyingSet Menu Helpers ------------ */
742 /* Extract the maximum set of requirements from the KeyingSet */
743 static int keyingset_relative_get_templates (KeyingSet *ks)
748 /* loop over the paths (could be slow to do for a number of KeyingSets)? */
749 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
750 /* add the destination's templates to the set of templates required for the set */
751 templates |= ksp->templates;
757 /* Check if context data is suitable for the given absolute Keying Set */
758 short keyingset_context_ok_poll (bContext *C, KeyingSet *ks)
760 ScrArea *sa= CTX_wm_area(C);
762 /* data retrieved from context depends on active editor */
763 if (sa == NULL) return 0;
765 switch (sa->spacetype) {
768 Object *obact= CTX_data_active_object(C);
770 /* if in posemode, check if 'pose-channels' requested for in KeyingSet */
771 if ((obact && obact->pose) && (obact->mode & OB_MODE_POSE)) {
772 /* check for posechannels */
776 /* check for selected object */
787 /* KeyingSet Context Operations ------------ */
789 /* Get list of data-sources from context (in 3D-View) for inserting keyframes using the given relative Keying Set */
790 static short modifykey_get_context_v3d_data (bContext *C, ListBase *dsources, KeyingSet *ks)
793 Object *obact= CTX_data_active_object(C);
797 /* get the templates in use in this KeyingSet which we should supply data for */
798 templates = keyingset_relative_get_templates(ks);
800 /* check if the active object is in PoseMode (i.e. only deal with bones) */
801 // TODO: check with the templates to see what we really need to store
802 if ((obact && obact->pose) && (obact->mode & OB_MODE_POSE)) {
803 /* Pose Mode: Selected bones */
805 //set_pose_keys(ob); /* sets pchan->flag to POSE_KEY if bone selected, and clears if not */
807 /* loop through posechannels */
808 //for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next) {
809 // if (pchan->flag & POSE_KEY) {
814 CTX_DATA_BEGIN(C, bPoseChannel*, pchan, selected_pchans)
816 /* add a new keying-source */
817 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
818 BLI_addtail(dsources, cks);
820 /* set necessary info */
824 if (templates & KSP_TEMPLATE_CONSTRAINT)
825 cks->con= constraints_get_active(&pchan->constraints);
832 /* Object Mode: Selected objects */
833 CTX_DATA_BEGIN(C, Base*, base, selected_bases)
835 Object *ob= base->object;
837 /* add a new keying-source */
838 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
839 BLI_addtail(dsources, cks);
841 /* set necessary info */
844 if (templates & KSP_TEMPLATE_CONSTRAINT)
845 cks->con= constraints_get_active(&ob->constraints);
852 /* return whether any data was extracted */
856 /* Get list of data-sources from context for inserting keyframes using the given relative Keying Set */
857 short modifykey_get_context_data (bContext *C, ListBase *dsources, KeyingSet *ks)
859 ScrArea *sa= CTX_wm_area(C);
861 /* for now, the active area is used to determine what set of contexts apply */
865 switch (sa->spacetype) {
866 case SPACE_VIEW3D: /* 3D-View: Selected Objects or Bones */
867 return modifykey_get_context_v3d_data(C, dsources, ks);
870 /* nothing happened */
874 /* KeyingSet Operations (Insert/Delete Keyframes) ------------ */
876 /* Given a KeyingSet and context info (if required), modify keyframes for the channels specified
877 * by the KeyingSet. This takes into account many of the different combinations of using KeyingSets.
878 * Returns the number of channels that keyframes were added to
880 int modify_keyframes (bContext *C, ListBase *dsources, bAction *act, KeyingSet *ks, short mode, float cfra)
883 int kflag=0, success= 0;
884 char *groupname= NULL;
886 /* get flags to use */
887 if (mode == MODIFYKEY_MODE_INSERT) {
888 /* use KeyingSet's flags as base */
889 kflag= ks->keyingflag;
891 /* suppliment with info from the context */
892 if (IS_AUTOKEY_FLAG(AUTOMATKEY)) kflag |= INSERTKEY_MATRIX;
893 if (IS_AUTOKEY_FLAG(INSERTNEEDED)) kflag |= INSERTKEY_NEEDED;
894 // if (IS_AUTOKEY_MODE(EDITKEYS)) flag |= INSERTKEY_REPLACE;
896 else if (mode == MODIFYKEY_MODE_DELETE)
899 /* check if the KeyingSet is absolute or not (i.e. does it requires sources info) */
900 if (ks->flag & KEYINGSET_ABSOLUTE) {
901 /* Absolute KeyingSets are simpler to use, as all the destination info has already been
902 * provided by the user, and is stored, ready to use, in the KeyingSet paths.
904 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
907 /* get pointer to name of group to add channels to */
908 if (ksp->groupmode == KSP_GROUP_NONE)
910 else if (ksp->groupmode == KSP_GROUP_KSNAME)
913 groupname= ksp->group;
915 /* init arraylen and i - arraylen should be greater than i so that
916 * normal non-array entries get keyframed correctly
921 /* get length of array if whole array option is enabled */
922 if (ksp->flag & KSP_FLAG_WHOLE_ARRAY) {
923 PointerRNA id_ptr, ptr;
926 RNA_id_pointer_create(ksp->id, &id_ptr);
927 if (RNA_path_resolve(&id_ptr, ksp->rna_path, &ptr, &prop) && prop)
928 arraylen= RNA_property_array_length(prop);
931 /* for each possible index, perform operation
932 * - assume that arraylen is greater than index
934 for (; i < arraylen; i++) {
935 /* action to take depends on mode */
936 if (mode == MODIFYKEY_MODE_INSERT)
937 success+= insert_keyframe(ksp->id, act, groupname, ksp->rna_path, i, cfra, kflag);
938 else if (mode == MODIFYKEY_MODE_DELETE)
939 success+= delete_keyframe(ksp->id, act, groupname, ksp->rna_path, i, cfra, kflag);
942 /* send notifiers and set recalc-flags */
943 // TODO: hopefully this doesn't result in execessive flooding of the notifier stack
945 switch (GS(ksp->id->name)) {
946 case ID_OB: /* Object (or Object-Related) Keyframes */
948 Object *ob= (Object *)ksp->id;
950 ob->recalc |= OB_RECALC;
951 WM_event_add_notifier(C, NC_OBJECT|ND_KEYS, ksp->id);
954 case ID_MA: /* Material Keyframes */
955 WM_event_add_notifier(C, NC_MATERIAL|ND_KEYS, ksp->id);
957 default: /* Any keyframes */
958 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME_EDIT, NULL);
964 else if (dsources && dsources->first) {
965 /* for each one of the 'sources', resolve the template markers and expand arrays, then insert keyframes */
968 /* for each 'source' for keyframe data, resolve each of the paths from the KeyingSet */
969 for (cks= dsources->first; cks; cks= cks->next) {
970 /* for each path in KeyingSet, construct a path using the templates */
971 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
972 DynStr *pathds= BLI_dynstr_new();
976 /* set initial group name */
977 groupname= (cks->id) ? cks->id->name+2 : NULL;
979 /* construct the path */
980 // FIXME: this currently only works with a few hardcoded cases
981 if ((ksp->templates & KSP_TEMPLATE_PCHAN) && (cks->pchan)) {
982 /* add basic pose-channel path access */
983 BLI_dynstr_append(pathds, "pose.pose_channels[\"");
984 BLI_dynstr_append(pathds, cks->pchan->name);
985 BLI_dynstr_append(pathds, "\"]");
987 /* override default group name */
988 groupname= cks->pchan->name;
990 if ((ksp->templates & KSP_TEMPLATE_CONSTRAINT) && (cks->con)) {
991 /* add basic constraint path access */
992 BLI_dynstr_append(pathds, "constraints[\"");
993 BLI_dynstr_append(pathds, cks->con->name);
994 BLI_dynstr_append(pathds, "\"]");
996 /* override default group name */
997 groupname= cks->con->name;
1000 /* add property stored in KeyingSet Path */
1001 if (BLI_dynstr_get_len(pathds))
1002 BLI_dynstr_append(pathds, ".");
1004 /* apply some further templates? */
1005 if ((ksp->templates & KSP_TEMPLATE_PCHAN_ROT) && (cks->pchan)) {
1006 /* if this path is exactly "rotation", and the rotation mode is set to eulers,
1007 * use "euler_rotation" instead so that rotations will be keyed correctly
1009 if (strcmp(ksp->rna_path, "rotation")==0 && (cks->pchan->rotmode))
1010 BLI_dynstr_append(pathds, "euler_rotation");
1012 BLI_dynstr_append(pathds, ksp->rna_path);
1015 /* just directly use the path */
1016 BLI_dynstr_append(pathds, ksp->rna_path);
1019 /* convert to C-string */
1020 path= BLI_dynstr_get_cstring(pathds);
1021 BLI_dynstr_free(pathds);
1024 /* get pointer to name of group to add channels to
1025 * - KSP_GROUP_TEMPLATE_ITEM is handled above while constructing the paths
1027 if (ksp->groupmode == KSP_GROUP_NONE)
1029 else if (ksp->groupmode == KSP_GROUP_KSNAME)
1030 groupname= ks->name;
1031 else if (ksp->groupmode == KSP_GROUP_NAMED)
1032 groupname= ksp->group;
1034 /* init arraylen and i - arraylen should be greater than i so that
1035 * normal non-array entries get keyframed correctly
1037 i= ksp->array_index;
1040 /* get length of array if whole array option is enabled */
1041 if (ksp->flag & KSP_FLAG_WHOLE_ARRAY) {
1042 PointerRNA id_ptr, ptr;
1045 RNA_id_pointer_create(cks->id, &id_ptr);
1046 if (RNA_path_resolve(&id_ptr, path, &ptr, &prop) && prop)
1047 arraylen= RNA_property_array_length(prop);
1050 /* for each possible index, perform operation
1051 * - assume that arraylen is greater than index
1053 for (; i < arraylen; i++) {
1054 /* action to take depends on mode */
1055 if (mode == MODIFYKEY_MODE_INSERT)
1056 success+= insert_keyframe(cks->id, act, groupname, path, i, cfra, kflag);
1057 else if (mode == MODIFYKEY_MODE_DELETE)
1058 success+= delete_keyframe(cks->id, act, groupname, path, i, cfra, kflag);
1065 /* send notifiers and set recalc-flags */
1066 // TODO: hopefully this doesn't result in execessive flooding of the notifier stack
1068 switch (GS(cks->id->name)) {
1069 case ID_OB: /* Object (or Object-Related) Keyframes */
1071 Object *ob= (Object *)cks->id;
1073 ob->recalc |= OB_RECALC;
1074 WM_event_add_notifier(C, NC_OBJECT|ND_KEYS, cks->id);
1077 case ID_MA: /* Material Keyframes */
1078 WM_event_add_notifier(C, NC_MATERIAL|ND_KEYS, cks->id);
1080 default: /* Any keyframes */
1081 WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME_EDIT, NULL);
1088 /* return the number of channels successfully affected */
1092 /* ************************************************** */