1 /* Testing code for 2.5 animation system
2 * Copyright 2009, Joshua Leung
11 #include "MEM_guardedalloc.h"
13 #include "BLI_blenlib.h"
14 #include "BLI_arithb.h"
15 #include "BLI_dynstr.h"
17 #include "DNA_anim_types.h"
18 #include "DNA_action_types.h"
19 #include "DNA_armature_types.h"
20 #include "DNA_constraint_types.h"
21 #include "DNA_key_types.h"
22 #include "DNA_object_types.h"
23 #include "DNA_material_types.h"
24 #include "DNA_scene_types.h"
25 #include "DNA_userdef_types.h"
26 #include "DNA_windowmanager_types.h"
28 #include "BKE_animsys.h"
29 #include "BKE_action.h"
30 #include "BKE_fcurve.h"
31 #include "BKE_utildefines.h"
32 #include "BKE_context.h"
33 #include "BKE_report.h"
35 #include "BKE_material.h"
37 #include "ED_anim_api.h"
38 #include "ED_keyframing.h"
39 #include "ED_keyframes_edit.h"
40 #include "ED_screen.h"
43 #include "UI_interface.h"
48 #include "RNA_access.h"
49 #include "RNA_define.h"
50 #include "RNA_types.h"
52 /* ************************************************** */
53 /* LOCAL TYPES AND DEFINES */
55 /* ----------- Common KeyData Sources ------------ */
57 /* temporary struct to gather data combos to keyframe */
58 typedef struct bCommonKeySrc {
59 struct bCommonKeySrc *next, *prev;
61 /* general data/destination-source settings */
62 ID *id; /* id-block this comes from */
63 char *rna_path; /* base path to use */ // xxx.... maybe we don't need this?
66 bPoseChannel *pchan; /* only needed when doing recalcs... */
69 /* ******************************************* */
70 /* Animation Data Validation */
72 /* Get (or add relevant data to be able to do so) F-Curve from the Active Action,
73 * for the given Animation Data block. This assumes that all the destinations are valid.
75 FCurve *verify_fcurve (ID *id, const char group[], const char rna_path[], const int array_index, short add)
83 if ELEM(NULL, id, rna_path)
86 /* init animdata if none available yet */
87 adt= BKE_animdata_from_id(id);
88 if ((adt == NULL) && (add))
89 adt= BKE_id_add_animdata(id);
91 /* if still none (as not allowed to add, or ID doesn't have animdata for some reason) */
95 /* init action if none available yet */
96 // TODO: need some wizardry to handle NLA stuff correct
97 if ((adt->action == NULL) && (add))
98 adt->action= add_empty_action("Action");
101 /* try to find f-curve matching for this setting
102 * - add if not found and allowed to add one
103 * TODO: add auto-grouping support? how this works will need to be resolved
106 fcu= list_find_fcurve(&act->curves, rna_path, array_index);
110 if ((fcu == NULL) && (add)) {
111 /* use default settings to make a F-Curve */
112 fcu= MEM_callocN(sizeof(FCurve), "FCurve");
114 fcu->flag = (FCURVE_VISIBLE|FCURVE_AUTO_HANDLES|FCURVE_SELECTED);
115 if (act->curves.first==NULL)
116 fcu->flag |= FCURVE_ACTIVE; /* first one added active */
118 /* store path - make copy, and store that */
119 fcu->rna_path= BLI_strdupn(rna_path, strlen(rna_path));
120 fcu->array_index= array_index;
122 /* if a group name has been provided, try to add or find a group, then add F-Curve to it */
124 /* try to find group */
125 grp= action_groups_find_named(act, group);
127 /* no matching groups, so add one */
129 /* Add a new group, and make it active */
130 grp= MEM_callocN(sizeof(bActionGroup), "bActionGroup");
132 grp->flag |= (AGRP_ACTIVE|AGRP_SELECTED|AGRP_EXPANDED);
133 BLI_snprintf(grp->name, 64, group);
135 BLI_addtail(&act->groups, grp);
136 BLI_uniquename(&act->groups, grp, "Group", offsetof(bActionGroup, name), 64);
138 set_active_action_group(act, grp, 1);
141 /* add F-Curve to group */
142 action_groups_add_channel(act, grp, fcu);
145 /* just add F-Curve to end of Action's list */
146 BLI_addtail(&act->curves, fcu);
150 /* return the F-Curve */
154 /* ************************************************** */
155 /* KEYFRAME INSERTION */
157 /* -------------- BezTriple Insertion -------------------- */
159 /* threshold for inserting keyframes - threshold here should be good enough for now, but should become userpref */
160 #define BEZT_INSERT_THRESH 0.00001f
162 /* Binary search algorithm for finding where to insert BezTriple. (for use by insert_bezt_icu)
163 * Returns the index to insert at (data already at that index will be offset if replace is 0)
165 static int binarysearch_bezt_index (BezTriple array[], float frame, int arraylen, short *replace)
167 int start=0, end=arraylen;
168 int loopbreaker= 0, maxloop= arraylen * 2;
170 /* initialise replace-flag first */
173 /* sneaky optimisations (don't go through searching process if...):
174 * - keyframe to be added is to be added out of current bounds
175 * - keyframe to be added would replace one of the existing ones on bounds
177 if ((arraylen <= 0) || (array == NULL)) {
178 printf("Warning: binarysearch_bezt_index() encountered invalid array \n");
182 /* check whether to add before/after/on */
185 /* 'First' Keyframe (when only one keyframe, this case is used) */
186 framenum= array[0].vec[1][0];
187 if (IS_EQT(frame, framenum, BEZT_INSERT_THRESH)) {
191 else if (frame < framenum)
194 /* 'Last' Keyframe */
195 framenum= array[(arraylen-1)].vec[1][0];
196 if (IS_EQT(frame, framenum, BEZT_INSERT_THRESH)) {
198 return (arraylen - 1);
200 else if (frame > framenum)
205 /* most of the time, this loop is just to find where to put it
206 * 'loopbreaker' is just here to prevent infinite loops
208 for (loopbreaker=0; (start <= end) && (loopbreaker < maxloop); loopbreaker++) {
209 /* compute and get midpoint */
210 int mid = (start + end) / 2;
211 float midfra= array[mid].vec[1][0];
213 /* check if exactly equal to midpoint */
214 if (IS_EQT(frame, midfra, BEZT_INSERT_THRESH)) {
219 /* repeat in upper/lower half */
222 else if (frame < midfra)
226 /* print error if loop-limit exceeded */
227 if (loopbreaker == (maxloop-1)) {
228 printf("Error: binarysearch_bezt_index() was taking too long \n");
230 // include debug info
231 printf("\tround = %d: start = %d, end = %d, arraylen = %d \n", loopbreaker, start, end, arraylen);
234 /* not found, so return where to place it */
238 /* This function adds a given BezTriple to an F-Curve. It will allocate
239 * memory for the array if needed, and will insert the BezTriple into a
240 * suitable place in chronological order.
242 * NOTE: any recalculate of the F-Curve that needs to be done will need to
243 * be done by the caller.
245 int insert_bezt_fcurve (FCurve *fcu, BezTriple *bezt)
252 i = binarysearch_bezt_index(fcu->bezt, bezt->vec[1][0], fcu->totvert, &replace);
255 /* sanity check: 'i' may in rare cases exceed arraylen */
256 // FIXME: do not overwrite handletype if just replacing...?
257 if ((i >= 0) && (i < fcu->totvert))
258 *(fcu->bezt + i) = *bezt;
262 newb= MEM_callocN((fcu->totvert+1)*sizeof(BezTriple), "beztriple");
264 /* add the beztriples that should occur before the beztriple to be pasted (originally in ei->icu) */
266 memcpy(newb, fcu->bezt, i*sizeof(BezTriple));
268 /* add beztriple to paste at index i */
271 /* add the beztriples that occur after the beztriple to be pasted (originally in icu) */
272 if (i < fcu->totvert)
273 memcpy(newb+i+1, fcu->bezt+i, (fcu->totvert-i)*sizeof(BezTriple));
275 /* replace (+ free) old with new */
276 MEM_freeN(fcu->bezt);
283 // TODO: need to check for old sample-data now...
284 fcu->bezt= MEM_callocN(sizeof(BezTriple), "beztriple");
290 /* we need to return the index, so that some tools which do post-processing can
291 * detect where we added the BezTriple in the array
296 /* This function is a wrapper for insert_bezt_icu, and should be used when
297 * adding a new keyframe to a curve, when the keyframe doesn't exist anywhere
300 * 'fast' - is only for the python API where importing BVH's would take an extreamly long time.
302 void insert_vert_fcurve (FCurve *fcu, float x, float y, short fast)
307 /* set all three points, for nicer start position */
308 memset(&beztr, 0, sizeof(BezTriple));
315 beztr.ipo= U.ipo_new; /* use default interpolation mode here... */
316 beztr.f1= beztr.f2= beztr.f3= SELECT;
317 beztr.h1= beztr.h2= HD_AUTO; // XXX what about when we replace an old one?
319 /* add temp beztriple to keyframes */
320 a= insert_bezt_fcurve(fcu, &beztr);
322 /* what if 'a' is a negative index?
323 * for now, just exit to prevent any segfaults
327 /* don't recalculate handles if fast is set
328 * - this is a hack to make importers faster
329 * - we may calculate twice (see editipo_changed(), due to autohandle needing two calculations)
331 if (!fast) calchandles_fcurve(fcu);
333 /* set handletype and interpolation */
334 if (fcu->totvert > 2) {
335 BezTriple *bezt= (fcu->bezt + a);
338 /* set handles (autohandles by default) */
341 if (a > 0) h1= (bezt-1)->h2;
342 if (a < fcu->totvert-1) h2= (bezt+1)->h1;
347 /* set interpolation from previous (if available) */
348 if (a > 0) bezt->ipo= (bezt-1)->ipo;
349 else if (a < fcu->totvert-1) bezt->ipo= (bezt+1)->ipo;
351 /* don't recalculate handles if fast is set
352 * - this is a hack to make importers faster
353 * - we may calculate twice (see editipo_changed(), due to autohandle needing two calculations)
355 if (!fast) calchandles_fcurve(fcu);
359 /* -------------- 'Smarter' Keyframing Functions -------------------- */
360 /* return codes for new_key_needed */
362 KEYNEEDED_DONTADD = 0,
368 /* This helper function determines whether a new keyframe is needed */
369 /* Cases where keyframes should not be added:
370 * 1. Keyframe to be added bewteen two keyframes with similar values
371 * 2. Keyframe to be added on frame where two keyframes are already situated
372 * 3. Keyframe lies at point that intersects the linear line between two keyframes
374 static short new_key_needed (FCurve *fcu, float cFrame, float nValue)
376 BezTriple *bezt=NULL, *prev=NULL;
378 float valA = 0.0f, valB = 0.0f;
380 /* safety checking */
381 if (fcu == NULL) return KEYNEEDED_JUSTADD;
382 totCount= fcu->totvert;
383 if (totCount == 0) return KEYNEEDED_JUSTADD;
385 /* loop through checking if any are the same */
387 for (i=0; i<totCount; i++) {
388 float prevPosi=0.0f, prevVal=0.0f;
389 float beztPosi=0.0f, beztVal=0.0f;
391 /* get current time+value */
392 beztPosi= bezt->vec[1][0];
393 beztVal= bezt->vec[1][1];
396 /* there is a keyframe before the one currently being examined */
398 /* get previous time+value */
399 prevPosi= prev->vec[1][0];
400 prevVal= prev->vec[1][1];
402 /* keyframe to be added at point where there are already two similar points? */
403 if (IS_EQ(prevPosi, cFrame) && IS_EQ(beztPosi, cFrame) && IS_EQ(beztPosi, prevPosi)) {
404 return KEYNEEDED_DONTADD;
407 /* keyframe between prev+current points ? */
408 if ((prevPosi <= cFrame) && (cFrame <= beztPosi)) {
409 /* is the value of keyframe to be added the same as keyframes on either side ? */
410 if (IS_EQ(prevVal, nValue) && IS_EQ(beztVal, nValue) && IS_EQ(prevVal, beztVal)) {
411 return KEYNEEDED_DONTADD;
416 /* get real value of curve at that point */
417 realVal= evaluate_fcurve(fcu, cFrame);
419 /* compare whether it's the same as proposed */
420 if (IS_EQ(realVal, nValue))
421 return KEYNEEDED_DONTADD;
423 return KEYNEEDED_JUSTADD;
427 /* new keyframe before prev beztriple? */
428 if (cFrame < prevPosi) {
429 /* A new keyframe will be added. However, whether the previous beztriple
430 * stays around or not depends on whether the values of previous/current
431 * beztriples and new keyframe are the same.
433 if (IS_EQ(prevVal, nValue) && IS_EQ(beztVal, nValue) && IS_EQ(prevVal, beztVal))
434 return KEYNEEDED_DELNEXT;
436 return KEYNEEDED_JUSTADD;
440 /* just add a keyframe if there's only one keyframe
441 * and the new one occurs before the exisiting one does.
443 if ((cFrame < beztPosi) && (totCount==1))
444 return KEYNEEDED_JUSTADD;
447 /* continue. frame to do not yet passed (or other conditions not met) */
448 if (i < (totCount-1)) {
456 /* Frame in which to add a new-keyframe occurs after all other keys
457 * -> If there are at least two existing keyframes, then if the values of the
458 * last two keyframes and the new-keyframe match, the last existing keyframe
459 * gets deleted as it is no longer required.
460 * -> Otherwise, a keyframe is just added. 1.0 is added so that fake-2nd-to-last
461 * keyframe is not equal to last keyframe.
463 bezt= (fcu->bezt + (fcu->totvert - 1));
464 valA= bezt->vec[1][1];
467 valB= prev->vec[1][1];
469 valB= bezt->vec[1][1] + 1.0f;
471 if (IS_EQ(valA, nValue) && IS_EQ(valA, valB))
472 return KEYNEEDED_DELPREV;
474 return KEYNEEDED_JUSTADD;
477 /* ------------------ RNA Data-Access Functions ------------------ */
479 /* Try to read value using RNA-properties obtained already */
480 static float setting_get_rna_value (PointerRNA *ptr, PropertyRNA *prop, int index)
484 switch (RNA_property_type(ptr, prop)) {
486 if (RNA_property_array_length(ptr, prop))
487 value= (float)RNA_property_boolean_get_index(ptr, prop, index);
489 value= (float)RNA_property_boolean_get(ptr, prop);
492 if (RNA_property_array_length(ptr, prop))
493 value= (float)RNA_property_int_get_index(ptr, prop, index);
495 value= (float)RNA_property_int_get(ptr, prop);
498 if (RNA_property_array_length(ptr, prop))
499 value= RNA_property_float_get_index(ptr, prop, index);
501 value= RNA_property_float_get(ptr, prop);
504 value= (float)RNA_property_enum_get(ptr, prop);
513 /* ------------------ 'Visual' Keyframing Functions ------------------ */
515 /* internal status codes for visualkey_can_use */
522 /* This helper function determines if visual-keyframing should be used when
523 * inserting keyframes for the given channel. As visual-keyframing only works
524 * on Object and Pose-Channel blocks, this should only get called for those
525 * blocktypes, when using "standard" keying but 'Visual Keying' option in Auto-Keying
528 static short visualkey_can_use (PointerRNA *ptr, PropertyRNA *prop)
531 bConstraint *con= NULL;
532 short searchtype= VISUALKEY_NONE;
534 #if 0 // XXX old animation system
536 if ((id == NULL) || (GS(id->name)!=ID_OB) || !(ELEM(blocktype, ID_OB, ID_PO)))
539 /* get first constraint and determine type of keyframe constraints to check for*/
542 if (blocktype == ID_OB) {
543 con= ob->constraints.first;
545 if (ELEM3(adrcode, OB_LOC_X, OB_LOC_Y, OB_LOC_Z))
546 searchtype= VISUALKEY_LOC;
547 else if (ELEM3(adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z))
548 searchtype= VISUALKEY_ROT;
550 else if (blocktype == ID_PO) {
551 bPoseChannel *pchan= get_pose_channel(ob->pose, actname);
552 con= pchan->constraints.first;
554 if (ELEM3(adrcode, AC_LOC_X, AC_LOC_Y, AC_LOC_Z))
555 searchtype= VISUALKEY_LOC;
556 else if (ELEM4(adrcode, AC_QUAT_W, AC_QUAT_X, AC_QUAT_Y, AC_QUAT_Z))
557 searchtype= VISUALKEY_ROT;
561 /* only search if a searchtype and initial constraint are available */
562 if (searchtype && con) {
563 for (; con; con= con->next) {
564 /* only consider constraint if it is not disabled, and has influence */
565 if (con->flag & CONSTRAINT_DISABLE) continue;
566 if (con->enforce == 0.0f) continue;
568 /* some constraints may alter these transforms */
570 /* multi-transform constraints */
571 case CONSTRAINT_TYPE_CHILDOF:
573 case CONSTRAINT_TYPE_TRANSFORM:
575 case CONSTRAINT_TYPE_FOLLOWPATH:
577 case CONSTRAINT_TYPE_KINEMATIC:
580 /* single-transform constraits */
581 case CONSTRAINT_TYPE_TRACKTO:
582 if (searchtype==VISUALKEY_ROT) return 1;
584 case CONSTRAINT_TYPE_ROTLIMIT:
585 if (searchtype==VISUALKEY_ROT) return 1;
587 case CONSTRAINT_TYPE_LOCLIMIT:
588 if (searchtype==VISUALKEY_LOC) return 1;
590 case CONSTRAINT_TYPE_ROTLIKE:
591 if (searchtype==VISUALKEY_ROT) return 1;
593 case CONSTRAINT_TYPE_DISTLIMIT:
594 if (searchtype==VISUALKEY_LOC) return 1;
596 case CONSTRAINT_TYPE_LOCLIKE:
597 if (searchtype==VISUALKEY_LOC) return 1;
599 case CONSTRAINT_TYPE_LOCKTRACK:
600 if (searchtype==VISUALKEY_ROT) return 1;
602 case CONSTRAINT_TYPE_MINMAX:
603 if (searchtype==VISUALKEY_LOC) return 1;
612 /* when some condition is met, this function returns, so here it can be 0 */
616 /* This helper function extracts the value to use for visual-keyframing
617 * In the event that it is not possible to perform visual keying, try to fall-back
618 * to using the default method. Assumes that all data it has been passed is valid.
620 // xxx... ptr here should be struct that data is in.... prop is the channel that's being used
621 static float visualkey_get_value (PointerRNA *ptr, PropertyRNA *prop, int array_index)
623 #if 0 // XXX old animation system
628 /* validate situtation */
629 if ((id==NULL) || (GS(id->name)!=ID_OB) || (ELEM(blocktype, ID_OB, ID_PO)==0))
635 /* only valid for objects or posechannels */
636 if (blocktype == ID_OB) {
637 /* parented objects are not supported, as the effects of the parent
638 * are included in the matrix, which kindof beats the point
640 if (ob->parent == NULL) {
641 /* only Location or Rotation keyframes are supported now */
642 if (ELEM3(adrcode, OB_LOC_X, OB_LOC_Y, OB_LOC_Z)) {
643 /* assumes that OB_LOC_Z > OB_LOC_Y > OB_LOC_X */
644 index= adrcode - OB_LOC_X;
646 return ob->obmat[3][index];
648 else if (ELEM3(adrcode, OB_ROT_X, OB_ROT_Y, OB_ROT_Z)) {
651 /* assumes that OB_ROT_Z > OB_ROT_Y > OB_ROT_X */
652 index= adrcode - OB_ROT_X;
654 Mat4ToEul(ob->obmat, eul);
655 return eul[index]*(5.72958f);
659 else if (blocktype == ID_PO) {
663 /* get data to use */
664 pchan= get_pose_channel(ob->pose, actname);
666 /* Although it is not strictly required for this particular space conversion,
667 * arg1 must not be null, as there is a null check for the other conversions to
668 * be safe. Therefore, the active object is passed here, and in many cases, this
669 * will be what owns the pose-channel that is getting this anyway.
671 Mat4CpyMat4(tmat, pchan->pose_mat);
672 constraint_mat_convertspace(ob, pchan, tmat, CONSTRAINT_SPACE_POSE, CONSTRAINT_SPACE_LOCAL);
674 /* Loc, Rot/Quat keyframes are supported... */
675 if (ELEM3(adrcode, AC_LOC_X, AC_LOC_Y, AC_LOC_Z)) {
676 /* assumes that AC_LOC_Z > AC_LOC_Y > AC_LOC_X */
677 index= adrcode - AC_LOC_X;
679 /* only use for non-connected bones */
680 if ((pchan->bone->parent) && !(pchan->bone->flag & BONE_CONNECTED))
681 return tmat[3][index];
682 else if (pchan->bone->parent == NULL)
683 return tmat[3][index];
685 else if (ELEM4(adrcode, AC_QUAT_W, AC_QUAT_X, AC_QUAT_Y, AC_QUAT_Z)) {
686 float trimat[3][3], quat[4];
688 /* assumes that AC_QUAT_Z > AC_QUAT_Y > AC_QUAT_X > AC_QUAT_W */
689 index= adrcode - AC_QUAT_W;
691 Mat3CpyMat4(trimat, tmat);
692 Mat3ToQuat_is_ok(trimat, quat);
697 #endif // XXX old animation system
699 /* as the function hasn't returned yet, read value from system in the default way */
700 return setting_get_rna_value(ptr, prop, array_index);
703 /* ------------------------- Insert Key API ------------------------- */
705 /* Main Keyframing API call:
706 * Use this when validation of necessary animation data isn't necessary as it
707 * already exists. It will insert a keyframe using the current value being keyframed.
709 * The flag argument is used for special settings that alter the behaviour of
710 * the keyframe insertion. These include the 'visual' keyframing modes, quick refresh,
711 * and extra keyframe filtering.
713 short insertkey (ID *id, const char group[], const char rna_path[], int array_index, float cfra, short flag)
715 PointerRNA id_ptr, ptr;
719 /* validate pointer first - exit if failure*/
720 RNA_id_pointer_create(id, &id_ptr);
721 if (RNA_path_resolve(&id_ptr, rna_path, &ptr, &prop) == 0) {
722 printf("Insert Key: Could not insert keyframe, as RNA Path is invalid for the given ID \n");
727 fcu= verify_fcurve(id, group, rna_path, array_index, 1);
729 /* only continue if we have an F-Curve to add keyframe to */
733 /* set additional flags for the F-Curve (i.e. only integer values) */
734 if (RNA_property_type(&ptr, prop) != PROP_FLOAT)
735 fcu->flag |= FCURVE_INT_VALUES;
737 /* apply special time tweaking */
738 // XXX check on this stuff...
739 if (GS(id->name) == ID_OB) {
740 //Object *ob= (Object *)id;
742 /* apply NLA-scaling (if applicable) */
743 //cfra= get_action_frame(ob, cfra);
745 /* ancient time-offset cruft */
746 //if ( (ob->ipoflag & OB_OFFS_OB) && (give_timeoffset(ob)) ) {
747 // /* actually frametofloat calc again! */
748 // cfra-= give_timeoffset(ob)*scene->r.framelen;
752 /* obtain value to give keyframe */
753 if ( (flag & INSERTKEY_MATRIX) &&
754 (visualkey_can_use(&ptr, prop)) )
756 /* visual-keying is only available for object and pchan datablocks, as
757 * it works by keyframing using a value extracted from the final matrix
758 * instead of using the kt system to extract a value.
760 curval= visualkey_get_value(&ptr, prop, array_index);
763 /* read value from system */
764 curval= setting_get_rna_value(&ptr, prop, array_index);
767 /* only insert keyframes where they are needed */
768 if (flag & INSERTKEY_NEEDED) {
771 /* check whether this curve really needs a new keyframe */
772 insert_mode= new_key_needed(fcu, cfra, curval);
774 /* insert new keyframe at current frame */
776 insert_vert_fcurve(fcu, cfra, curval, (flag & INSERTKEY_FAST));
778 /* delete keyframe immediately before/after newly added */
779 switch (insert_mode) {
780 case KEYNEEDED_DELPREV:
781 delete_fcurve_key(fcu, fcu->totvert-2, 1);
783 case KEYNEEDED_DELNEXT:
784 delete_fcurve_key(fcu, 1, 1);
788 /* only return success if keyframe added */
793 /* just insert keyframe */
794 insert_vert_fcurve(fcu, cfra, curval, (flag & INSERTKEY_FAST));
805 /* ************************************************** */
806 /* KEYFRAME DELETION */
808 /* Main Keyframing API call:
809 * Use this when validation of necessary animation data isn't necessary as it
810 * already exists. It will delete a keyframe at the current frame.
812 * The flag argument is used for special settings that alter the behaviour of
813 * the keyframe deletion. These include the quick refresh options.
815 short deletekey (ID *id, const char group[], const char rna_path[], int array_index, float cfra, short flag)
821 * Note: here is one of the places where we don't want new Action + F-Curve added!
822 * so 'add' var must be 0
824 /* we don't check the validity of the path here yet, but it should be ok... */
825 fcu= verify_fcurve(id, group, rna_path, array_index, 0);
826 adt= BKE_animdata_from_id(id);
828 /* only continue if we have an ipo-curve to remove keyframes from */
829 if (adt && adt->action && fcu) {
830 bAction *act= adt->action;
834 /* apply special time tweaking */
835 if (GS(id->name) == ID_OB) {
836 //Object *ob= (Object *)id;
838 /* apply NLA-scaling (if applicable) */
839 // cfra= get_action_frame(ob, cfra);
841 /* ancient time-offset cruft */
842 //if ( (ob->ipoflag & OB_OFFS_OB) && (give_timeoffset(ob)) ) {
843 // /* actually frametofloat calc again! */
844 // cfra-= give_timeoffset(ob)*scene->r.framelen;
848 /* try to find index of beztriple to get rid of */
849 i = binarysearch_bezt_index(fcu->bezt, cfra, fcu->totvert, &found);
851 /* delete the key at the index (will sanity check + do recalc afterwards) */
852 delete_fcurve_key(fcu, i, 1);
854 /* Only delete curve too if there are no points (we don't need to check for drivers, as they're kept separate) */
855 // XXX how do we handle drivers then?
856 if (fcu->totvert == 0) {
857 BLI_remlink(&act->curves, fcu);
870 /* ******************************************* */
871 /* KEYFRAME MODIFICATION */
873 /* mode for common_modifykey */
875 COMMONKEY_MODE_INSERT = 0,
876 COMMONKEY_MODE_DELETE,
877 } eCommonModifyKey_Modes;
880 /* Build menu-string of available keying-sets (allocates memory for string)
881 * NOTE: mode must not be longer than 64 chars
883 char *ANIM_build_keyingsets_menu (ListBase *list, short for_edit)
885 DynStr *pupds= BLI_dynstr_new();
891 /* add title first */
892 BLI_dynstr_append(pupds, "Keying Sets%t|");
894 /* add dummy entries for none-active */
896 BLI_dynstr_append(pupds, "Add New%x-1|");
897 BLI_dynstr_append(pupds, " %x0|");
900 BLI_dynstr_append(pupds, "<No Keying Set Active>%x0|");
902 /* loop through keyingsets, adding them */
903 for (ks=list->first, i=1; ks; ks=ks->next, i++) {
905 BLI_dynstr_append(pupds, "KS: ");
907 BLI_dynstr_append(pupds, ks->name);
908 BLI_snprintf( buf, 64, "%%x%d%s", i, ((ks->next)?"|":"") );
909 BLI_dynstr_append(pupds, buf);
912 /* convert to normal MEM_malloc'd string */
913 str= BLI_dynstr_get_cstring(pupds);
914 BLI_dynstr_free(pupds);
919 #if 0 // XXX old keyingsets code based on adrcodes... to be restored in due course
921 /* --------- KeyingSet Adrcode Getters ------------ */
923 /* initialise a channel-getter storage */
924 static void ks_adrcodegetter_init (bKS_AdrcodeGetter *kag, bKeyingSet *ks, bCommonKeySrc *cks)
930 if (ELEM(NULL, ks, cks)) {
931 /* set invalid settings that won't cause harm */
942 /* - index is -1, as that allows iterators to return first element
943 * - tot is chan_num by default, but may get overriden if -1 is encountered (for extension-type getters)
946 kag->tot= ks->chan_num;
950 /* 'default' channel-getter that will be used when iterating through keyingset's channels
951 * - iteration will stop when adrcode <= 0 is encountered, so we use that as escape
953 static short ks_getnextadrcode_default (bKS_AdrcodeGetter *kag)
955 bKeyingSet *ks= (kag)? kag->ks : NULL;
958 if (ELEM(NULL, kag, ks)) return 0;
959 if (kag->tot <= 0) return 0;
962 if ((kag->index < 0) || (kag->index >= kag->tot)) return 0;
964 /* return the adrcode stored at index then */
965 return ks->adrcodes[kag->index];
968 /* add map flag (for MTex channels, as certain ones need special offset) */
969 static short ks_getnextadrcode_addmap (bKS_AdrcodeGetter *kag)
971 short adrcode= ks_getnextadrcode_default(kag);
973 /* if there was an adrcode returned, assume that kag stuff is set ok */
975 bCommonKeySrc *cks= kag->cks;
976 bKeyingSet *ks= kag->ks;
978 if (ELEM3(ks->blocktype, ID_MA, ID_LA, ID_WO)) {
980 case MAP_OFS_X: case MAP_OFS_Y: case MAP_OFS_Z:
981 case MAP_SIZE_X: case MAP_SIZE_Y: case MAP_SIZE_Z:
982 case MAP_R: case MAP_G: case MAP_B: case MAP_DVAR:
983 case MAP_COLF: case MAP_NORF: case MAP_VARF: case MAP_DISP:
990 /* adrcode must be returned! */
994 /* extend posechannel keyingsets with rotation info (when KAG_CHAN_EXTEND is encountered)
995 * - iteration will stop when adrcode <= 0 is encountered, so we use that as escape
996 * - when we encounter KAG_CHAN_EXTEND as adrcode, start returning our own
998 static short ks_getnextadrcode_pchanrot (bKS_AdrcodeGetter *kag)
1000 /* hardcoded adrcode channels used here only
1001 * - length is keyed-channels + 1 (last item must be 0 to escape)
1003 static short quat_adrcodes[5] = {AC_QUAT_W, AC_QUAT_X, AC_QUAT_Y, AC_QUAT_Z, 0};
1004 static short eul_adrcodes[4] = {AC_EUL_X, AC_EUL_Y, AC_EUL_Z, 0};
1006 /* useful variables */
1007 bKeyingSet *ks= (kag)? kag->ks : NULL;
1008 bCommonKeySrc *cks= (kag) ? kag->cks : NULL;
1009 short index, adrcode;
1011 /* error checking */
1012 if (ELEM3(NULL, kag, ks, cks)) return 0;
1013 if (ks->chan_num <= 0) return 0;
1016 * - if past the last item (kag->tot), return stuff from our static arrays
1017 * - otherwise, just keep returning stuff from the keyingset (but check out for -1!)
1023 /* normal (static stuff) */
1024 if (kag->index < kag->tot) {
1025 /* get adrcode, and return if not KAG_CHAN_EXTEND (i.e. point for start of iteration) */
1026 adrcode= ks->adrcodes[kag->index];
1028 if (adrcode != KAG_CHAN_EXTEND)
1031 kag->tot= kag->index;
1034 /* based on current rotation-mode
1035 * - index can be at most 5, if we are to prevent segfaults
1037 index= kag->index - kag->tot;
1038 if ((index < 0) || (index > 5))
1041 if (cks->pchan && cks->pchan->rotmode)
1042 return eul_adrcodes[index];
1044 return quat_adrcodes[index];
1047 /* ------------- KeyingSet Defines ------------ */
1048 /* Note: these must all be named with the defks_* prefix, otherwise the template macro will not work! */
1050 /* macro for defining keyingset contexts */
1051 #define KSC_TEMPLATE(ctx_name) {&defks_##ctx_name[0], NULL, sizeof(defks_##ctx_name)/sizeof(bKeyingSet)}
1055 /* check if option not available for deleting keys */
1056 static short incl_non_del_keys (bKeyingSet *ks, const char mode[])
1058 /* as optimisation, assume that it is sufficient to check only first letter
1059 * of mode (int comparison should be faster than string!)
1061 //if (strcmp(mode, "Delete")==0)
1062 if (mode && mode[0]=='D')
1068 /* Object KeyingSets ------ */
1070 /* check if include shapekey entry */
1071 static short incl_v3d_ob_shapekey (bKeyingSet *ks, const char mode[])
1073 //Object *ob= (G.obedit)? (G.obedit) : (OBACT); // XXX
1075 char *newname= NULL;
1080 /* not available for delete mode */
1081 if (strcmp(mode, "Delete")==0)
1084 /* check if is geom object that can get shapekeys */
1087 case OB_MESH: newname= "Mesh"; break;
1088 case OB_CURVE: newname= "Curve"; break;
1089 case OB_SURF: newname= "Surface"; break;
1090 case OB_LATTICE: newname= "Lattice"; break;
1097 /* if ks is shapekey entry (this could be callled for separator before too!) */
1099 BLI_strncpy(ks->name, newname, sizeof(ks->name));
1101 /* if it gets here, it's ok */
1105 /* array for object keyingset defines */
1106 bKeyingSet defks_v3d_object[] =
1108 /* include_cb, adrcode-getter, name, blocktype, flag, chan_num, adrcodes */
1109 {NULL, "Loc", ID_OB, 0, 3, {OB_LOC_X,OB_LOC_Y,OB_LOC_Z}},
1110 {NULL, "Rot", ID_OB, 0, 3, {OB_ROT_X,OB_ROT_Y,OB_ROT_Z}},
1111 {NULL, "Scale", ID_OB, 0, 3, {OB_SIZE_X,OB_SIZE_Y,OB_SIZE_Z}},
1113 {NULL, "%l", 0, -1, 0, {0}}, // separator
1115 {NULL, "LocRot", ID_OB, 0, 6,
1116 {OB_LOC_X,OB_LOC_Y,OB_LOC_Z,
1117 OB_ROT_X,OB_ROT_Y,OB_ROT_Z}},
1119 {NULL, "LocScale", ID_OB, 0, 6,
1120 {OB_LOC_X,OB_LOC_Y,OB_LOC_Z,
1121 OB_SIZE_X,OB_SIZE_Y,OB_SIZE_Z}},
1123 {NULL, "LocRotScale", ID_OB, 0, 9,
1124 {OB_LOC_X,OB_LOC_Y,OB_LOC_Z,
1125 OB_ROT_X,OB_ROT_Y,OB_ROT_Z,
1126 OB_SIZE_X,OB_SIZE_Y,OB_SIZE_Z}},
1128 {NULL, "RotScale", ID_OB, 0, 6,
1129 {OB_ROT_X,OB_ROT_Y,OB_ROT_Z,
1130 OB_SIZE_X,OB_SIZE_Y,OB_SIZE_Z}},
1132 {incl_non_del_keys, "%l", 0, -1, 0, {0}}, // separator
1134 {incl_non_del_keys, "VisualLoc", ID_OB, INSERTKEY_MATRIX, 3, {OB_LOC_X,OB_LOC_Y,OB_LOC_Z}},
1135 {incl_non_del_keys, "VisualRot", ID_OB, INSERTKEY_MATRIX, 3, {OB_ROT_X,OB_ROT_Y,OB_ROT_Z}},
1137 {incl_non_del_keys, "VisualLocRot", ID_OB, INSERTKEY_MATRIX, 6,
1138 {OB_LOC_X,OB_LOC_Y,OB_LOC_Z,
1139 OB_ROT_X,OB_ROT_Y,OB_ROT_Z}},
1141 {NULL, "%l", 0, -1, 0, {0}}, // separator
1143 {NULL, "Layer", ID_OB, 0, 1, {OB_LAY}}, // icky option...
1144 {NULL, "Available", ID_OB, -2, 0, {0}},
1146 {incl_v3d_ob_shapekey, "%l%l", 0, -1, 0, {0}}, // separator (linked to shapekey entry)
1147 {incl_v3d_ob_shapekey, "<ShapeKey>", ID_OB, -3, 0, {0}}
1150 /* PoseChannel KeyingSets ------ */
1152 /* array for posechannel keyingset defines */
1153 bKeyingSet defks_v3d_pchan[] =
1155 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
1156 {NULL, "Loc", ID_PO, 0, 3, {AC_LOC_X,AC_LOC_Y,AC_LOC_Z}},
1157 {NULL, "Rot", ID_PO, COMMONKEY_PCHANROT, 1, {KAG_CHAN_EXTEND}},
1158 {NULL, "Scale", ID_PO, 0, 3, {AC_SIZE_X,AC_SIZE_Y,AC_SIZE_Z}},
1160 {NULL, "%l", 0, -1, 0, {0}}, // separator
1162 {NULL, "LocRot", ID_PO, COMMONKEY_PCHANROT, 4,
1163 {AC_LOC_X,AC_LOC_Y,AC_LOC_Z,
1166 {NULL, "LocScale", ID_PO, 0, 6,
1167 {AC_LOC_X,AC_LOC_Y,AC_LOC_Z,
1168 AC_SIZE_X,AC_SIZE_Y,AC_SIZE_Z}},
1170 {NULL, "LocRotScale", ID_PO, COMMONKEY_PCHANROT, 7,
1171 {AC_LOC_X,AC_LOC_Y,AC_LOC_Z,AC_SIZE_X,AC_SIZE_Y,AC_SIZE_Z,
1174 {NULL, "RotScale", ID_PO, 0, 4,
1175 {AC_SIZE_X,AC_SIZE_Y,AC_SIZE_Z,
1178 {incl_non_del_keys, "%l", 0, -1, 0, {0}}, // separator
1180 {incl_non_del_keys, "VisualLoc", ID_PO, INSERTKEY_MATRIX, 3, {AC_LOC_X,AC_LOC_Y,AC_LOC_Z}},
1181 {incl_non_del_keys, "VisualRot", ID_PO, INSERTKEY_MATRIX|COMMONKEY_PCHANROT, 1, {KAG_CHAN_EXTEND}},
1183 {incl_non_del_keys, "VisualLocRot", ID_PO, INSERTKEY_MATRIX|COMMONKEY_PCHANROT, 4,
1184 {AC_LOC_X,AC_LOC_Y,AC_LOC_Z, KAG_CHAN_EXTEND}},
1186 {NULL, "%l", 0, -1, 0, {0}}, // separator
1188 {NULL, "Available", ID_PO, -2, 0, {0}}
1191 /* Material KeyingSets ------ */
1193 /* array for material keyingset defines */
1194 bKeyingSet defks_buts_shading_mat[] =
1196 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
1197 {NULL, "RGB", ID_MA, 0, 3, {MA_COL_R,MA_COL_G,MA_COL_B}},
1198 {NULL, "Alpha", ID_MA, 0, 1, {MA_ALPHA}},
1199 {NULL, "Halo Size", ID_MA, 0, 1, {MA_HASIZE}},
1200 {NULL, "Mode", ID_MA, 0, 1, {MA_MODE}}, // evil bitflags
1202 {NULL, "%l", 0, -1, 0, {0}}, // separator
1204 {NULL, "All Color", ID_MA, 0, 18,
1205 {MA_COL_R,MA_COL_G,MA_COL_B,
1206 MA_ALPHA,MA_HASIZE, MA_MODE,
1207 MA_SPEC_R,MA_SPEC_G,MA_SPEC_B,
1208 MA_REF,MA_EMIT,MA_AMB,MA_SPEC,MA_HARD,
1209 MA_MODE,MA_TRANSLU,MA_ADD}},
1211 {NULL, "All Mirror", ID_MA, 0, 5,
1212 {MA_RAYM,MA_FRESMIR,MA_FRESMIRI,
1213 MA_FRESTRA,MA_FRESTRAI}},
1215 {NULL, "%l", 0, -1, 0, {0}}, // separator
1217 {NULL, "Ofs", ID_MA, COMMONKEY_ADDMAP, 3, {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z}},
1218 {NULL, "Size", ID_MA, COMMONKEY_ADDMAP, 3, {MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z}},
1220 {NULL, "All Mapping", ID_MA, COMMONKEY_ADDMAP, 14,
1221 {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z,
1222 MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z,
1223 MAP_R,MAP_G,MAP_B,MAP_DVAR,
1224 MAP_COLF,MAP_NORF,MAP_VARF,MAP_DISP}},
1226 {NULL, "%l", 0, -1, 0, {0}}, // separator
1228 {NULL, "Available", ID_MA, -2, 0, {0}}
1231 /* World KeyingSets ------ */
1233 /* array for world keyingset defines */
1234 bKeyingSet defks_buts_shading_wo[] =
1236 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
1237 {NULL, "Zenith RGB", ID_WO, 0, 3, {WO_ZEN_R,WO_ZEN_G,WO_ZEN_B}},
1238 {NULL, "Horizon RGB", ID_WO, 0, 3, {WO_HOR_R,WO_HOR_G,WO_HOR_B}},
1240 {NULL, "%l", 0, -1, 0, {0}}, // separator
1242 {NULL, "Mist", ID_WO, 0, 4, {WO_MISI,WO_MISTDI,WO_MISTSTA,WO_MISTHI}},
1243 {NULL, "Stars", ID_WO, 0, 5, {WO_STAR_R,WO_STAR_G,WO_STAR_B,WO_STARDIST,WO_STARSIZE}},
1246 {NULL, "%l", 0, -1, 0, {0}}, // separator
1248 {NULL, "Ofs", ID_WO, COMMONKEY_ADDMAP, 3, {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z}},
1249 {NULL, "Size", ID_WO, COMMONKEY_ADDMAP, 3, {MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z}},
1251 {NULL, "All Mapping", ID_WO, COMMONKEY_ADDMAP, 14,
1252 {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z,
1253 MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z,
1254 MAP_R,MAP_G,MAP_B,MAP_DVAR,
1255 MAP_COLF,MAP_NORF,MAP_VARF,MAP_DISP}},
1257 {NULL, "%l", 0, -1, 0, {0}}, // separator
1259 {NULL, "Available", ID_WO, -2, 0, {0}}
1262 /* Lamp KeyingSets ------ */
1264 /* array for lamp keyingset defines */
1265 bKeyingSet defks_buts_shading_la[] =
1267 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
1268 {NULL, "RGB", ID_LA, 0, 3, {LA_COL_R,LA_COL_G,LA_COL_B}},
1269 {NULL, "Energy", ID_LA, 0, 1, {LA_ENERGY}},
1270 {NULL, "Spot Size", ID_LA, 0, 1, {LA_SPOTSI}},
1272 {NULL, "%l", 0, -1, 0, {0}}, // separator
1274 {NULL, "Ofs", ID_LA, COMMONKEY_ADDMAP, 3, {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z}},
1275 {NULL, "Size", ID_LA, COMMONKEY_ADDMAP, 3, {MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z}},
1277 {NULL, "All Mapping", ID_LA, COMMONKEY_ADDMAP, 14,
1278 {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z,
1279 MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z,
1280 MAP_R,MAP_G,MAP_B,MAP_DVAR,
1281 MAP_COLF,MAP_NORF,MAP_VARF,MAP_DISP}},
1283 {NULL, "%l", 0, -1, 0, {0}}, // separator
1285 {NULL, "Available", ID_LA, -2, 0, {0}}
1288 /* Texture KeyingSets ------ */
1290 /* array for texture keyingset defines */
1291 bKeyingSet defks_buts_shading_tex[] =
1293 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
1294 {NULL, "Clouds", ID_TE, 0, 5,
1295 {TE_NSIZE,TE_NDEPTH,TE_NTYPE,
1296 TE_MG_TYP,TE_N_BAS1}},
1298 {NULL, "Marble", ID_TE, 0, 7,
1299 {TE_NSIZE,TE_NDEPTH,TE_NTYPE,
1300 TE_TURB,TE_MG_TYP,TE_N_BAS1,TE_N_BAS2}},
1302 {NULL, "Stucci", ID_TE, 0, 5,
1303 {TE_NSIZE,TE_NTYPE,TE_TURB,
1304 TE_MG_TYP,TE_N_BAS1}},
1306 {NULL, "Wood", ID_TE, 0, 6,
1307 {TE_NSIZE,TE_NTYPE,TE_TURB,
1308 TE_MG_TYP,TE_N_BAS1,TE_N_BAS2}},
1310 {NULL, "Magic", ID_TE, 0, 2, {TE_NDEPTH,TE_TURB}},
1312 {NULL, "Blend", ID_TE, 0, 1, {TE_MG_TYP}},
1314 {NULL, "Musgrave", ID_TE, 0, 6,
1315 {TE_MG_TYP,TE_MGH,TE_MG_LAC,
1316 TE_MG_OCT,TE_MG_OFF,TE_MG_GAIN}},
1318 {NULL, "Voronoi", ID_TE, 0, 9,
1319 {TE_VNW1,TE_VNW2,TE_VNW3,TE_VNW4,
1320 TE_VNMEXP,TE_VN_DISTM,TE_VN_COLT,
1323 {NULL, "Distorted Noise", ID_TE, 0, 4,
1324 {TE_MG_OCT,TE_MG_OFF,TE_MG_GAIN,TE_DISTA}},
1326 {NULL, "Color Filter", ID_TE, 0, 5,
1327 {TE_COL_R,TE_COL_G,TE_COL_B,TE_BRIGHT,TE_CONTRA}},
1329 {NULL, "%l", 0, -1, 0, {0}}, // separator
1331 {NULL, "Available", ID_TE, -2, 0, {0}}
1334 /* Object Buttons KeyingSets ------ */
1336 /* check if include particles entry */
1337 static short incl_buts_ob (bKeyingSet *ks, const char mode[])
1339 //Object *ob= OBACT; // xxx
1341 /* only if object is mesh type */
1343 if(ob==NULL) return 0;
1344 return (ob->type == OB_MESH);
1347 /* array for texture keyingset defines */
1348 bKeyingSet defks_buts_object[] =
1350 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
1351 {incl_buts_ob, "Surface Damping", ID_OB, 0, 1, {OB_PD_SDAMP}},
1352 {incl_buts_ob, "Random Damping", ID_OB, 0, 1, {OB_PD_RDAMP}},
1353 {incl_buts_ob, "Permeability", ID_OB, 0, 1, {OB_PD_PERM}},
1355 {NULL, "%l", 0, -1, 0, {0}}, // separator
1357 {NULL, "Force Strength", ID_OB, 0, 1, {OB_PD_FSTR}},
1358 {NULL, "Force Falloff", ID_OB, 0, 1, {OB_PD_FFALL}},
1360 {NULL, "%l", 0, -1, 0, {0}}, // separator
1362 {NULL, "Available", ID_OB, -2, 0, {0}} // this will include ob-transforms too!
1365 /* Camera Buttons KeyingSets ------ */
1367 /* check if include internal-renderer entry */
1368 static short incl_buts_cam1 (bKeyingSet *ks, const char mode[])
1370 Scene *scene= NULL; // FIXME this will cause a crash, but we need an extra arg first!
1371 /* only if renderer is internal renderer */
1372 return (scene->r.renderer==R_INTERN);
1375 /* check if include external-renderer entry */
1376 static short incl_buts_cam2 (bKeyingSet *ks, const char mode[])
1378 Scene *scene= NULL; // FIXME this will cause a crash, but we need an extra arg first!
1379 /* only if renderer is internal renderer */
1380 return (scene->r.renderer!=R_INTERN);
1383 /* array for camera keyingset defines */
1384 bKeyingSet defks_buts_cam[] =
1386 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
1387 {NULL, "Lens", ID_CA, 0, 1, {CAM_LENS}},
1388 {NULL, "Clipping", ID_CA, 0, 2, {CAM_STA,CAM_END}},
1389 {NULL, "Focal Distance", ID_CA, 0, 1, {CAM_YF_FDIST}},
1391 {NULL, "%l", 0, -1, 0, {0}}, // separator
1394 {incl_buts_cam2, "Aperture", ID_CA, 0, 1, {CAM_YF_APERT}},
1395 {incl_buts_cam1, "Viewplane Shift", ID_CA, 0, 2, {CAM_SHIFT_X,CAM_SHIFT_Y}},
1397 {NULL, "%l", 0, -1, 0, {0}}, // separator
1399 {NULL, "Available", ID_CA, -2, 0, {0}}
1404 /* Keying Context Defines - Must keep in sync with enumeration (eKS_Contexts) */
1405 bKeyingContext ks_contexts[] =
1407 KSC_TEMPLATE(v3d_object),
1408 KSC_TEMPLATE(v3d_pchan),
1410 KSC_TEMPLATE(buts_shading_mat),
1411 KSC_TEMPLATE(buts_shading_wo),
1412 KSC_TEMPLATE(buts_shading_la),
1413 KSC_TEMPLATE(buts_shading_tex),
1415 KSC_TEMPLATE(buts_object),
1416 KSC_TEMPLATE(buts_cam)
1419 /* Keying Context Enumeration - Must keep in sync with definitions*/
1420 typedef enum eKS_Contexts {
1432 /* make sure this last one remains untouched! */
1437 /* ---------------- KeyingSet Tools ------------------- */
1439 /* helper for commonkey_context_get() - get keyingsets for 3d-view */
1440 static void commonkey_context_getv3d (const bContext *C, ListBase *sources, bKeyingContext **ksc)
1442 Scene *scene= CTX_data_scene(C);
1446 if ((OBACT) && (OBACT->flag & OB_POSEMODE)) {
1447 bPoseChannel *pchan;
1451 *ksc= &ks_contexts[KSC_V3D_PCHAN];
1453 //set_pose_keys(ob); /* sets pchan->flag to POSE_KEY if bone selected, and clears if not */
1455 /* loop through posechannels */
1456 for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1457 if (pchan->flag & POSE_KEY) {
1460 /* add new keyframing destination */
1461 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1462 BLI_addtail(sources, cks);
1464 /* set id-block to key to, and action */
1466 cks->act= ob->action;
1470 cks->actname= pchan->name;
1476 *ksc= &ks_contexts[KSC_V3D_OBJECT];
1478 /* loop through bases */
1479 // XXX but we're only supposed to do this on editable ones, not just selected ones!
1480 CTX_DATA_BEGIN(C, Base*, base, selected_bases) {
1483 /* add new keyframing destination */
1484 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1485 BLI_addtail(sources, cks);
1487 /* set id-block to key to */
1491 /* when ob's keyframes are in an action, default to using 'Object' as achan name */
1492 if (ob->ipoflag & OB_ACTION_OB)
1493 cks->actname= "Object";
1496 // TODO: add checks for lib-linked data
1497 if ((ob->ipo) || (ob->action)) {
1502 bActionChannel *achan;
1504 cks->act= ob->action;
1505 achan= get_action_channel(ob->action, cks->actname);
1507 if (achan && achan->ipo)
1508 cks->ipo= achan->ipo;
1510 /* cks->ipo can be NULL while editing */
1512 /* deselect all ipo-curves */
1513 for (icu= cks->ipo->curve.first; icu; icu= icu->next) {
1514 icu->flag &= ~IPO_SELECT;
1523 /* helper for commonkey_context_get() - get keyingsets for buttons window */
1524 static void commonkey_context_getsbuts (const bContext *C, ListBase *sources, bKeyingContext **ksc)
1526 #if 0 // XXX dunno what's the future of this stuff...
1529 /* check on tab-type */
1530 switch (G.buts->mainb) {
1531 case CONTEXT_SHADING: /* ------------- Shading buttons ---------------- */
1532 /* subtabs include "Material", "Texture", "Lamp", "World"*/
1533 switch (G.buts->tab[CONTEXT_SHADING]) {
1534 case TAB_SHADING_MAT: /* >------------- Material Tab -------------< */
1536 Material *ma= editnode_get_active_material(G.buts->lockpoin);
1539 /* add new keyframing destination */
1540 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1541 BLI_addtail(sources, cks);
1546 cks->map= texchannel_to_adrcode(ma->texact);
1548 /* set keyingsets */
1549 *ksc= &ks_contexts[KSC_BUTS_MAT];
1554 case TAB_SHADING_WORLD: /* >------------- World Tab -------------< */
1556 World *wo= G.buts->lockpoin;
1559 /* add new keyframing destination */
1560 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1561 BLI_addtail(sources, cks);
1566 cks->map= texchannel_to_adrcode(wo->texact);
1568 /* set keyingsets */
1569 *ksc= &ks_contexts[KSC_BUTS_WO];
1574 case TAB_SHADING_LAMP: /* >------------- Lamp Tab -------------< */
1576 Lamp *la= G.buts->lockpoin;
1579 /* add new keyframing destination */
1580 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1581 BLI_addtail(sources, cks);
1586 cks->map= texchannel_to_adrcode(la->texact);
1588 /* set keyingsets */
1589 *ksc= &ks_contexts[KSC_BUTS_LA];
1594 case TAB_SHADING_TEX: /* >------------- Texture Tab -------------< */
1596 Tex *tex= G.buts->lockpoin;
1599 /* add new keyframing destination */
1600 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1601 BLI_addtail(sources, cks);
1607 /* set keyingsets */
1608 *ksc= &ks_contexts[KSC_BUTS_TEX];
1616 case CONTEXT_OBJECT: /* ------------- Object buttons ---------------- */
1621 /* add new keyframing destination */
1622 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1623 BLI_addtail(sources, cks);
1625 /* set id-block to key to */
1629 /* set keyingsets */
1630 *ksc= &ks_contexts[KSC_BUTS_OB];
1636 case CONTEXT_EDITING: /* ------------- Editing buttons ---------------- */
1640 if ((ob) && (ob->type==OB_CAMERA) && (G.buts->lockpoin)) { /* >---------------- camera buttons ---------------< */
1641 Camera *ca= G.buts->lockpoin;
1643 /* add new keyframing destination */
1644 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1645 BLI_addtail(sources, cks);
1647 /* set id-block to key to */
1651 /* set keyingsets */
1652 *ksc= &ks_contexts[KSC_BUTS_CAM];
1658 #endif // XXX end of buttons stuff to port...
1660 /* if nothing happened... */
1665 /* get keyingsets for appropriate context */
1666 static void commonkey_context_get (const bContext *C, ScrArea *sa, short mode, ListBase *sources, bKeyingContext **ksc)
1668 /* get current view if no view is defined */
1672 /* check view type */
1673 switch (sa->spacetype) {
1674 /* 3d view - first one tested as most often used */
1677 commonkey_context_getv3d(C, sources, ksc);
1684 commonkey_context_getsbuts(C, sources, ksc);
1688 /* spaces with their own methods */
1690 //if (mode == COMMONKEY_MODE_INSERT)
1691 // insertkey_editipo(); // XXX old calls...
1694 //if (mode == COMMONKEY_MODE_INSERT)
1695 // insertkey_action(); // XXX old calls...
1698 /* timeline view - keyframe buttons */
1701 bScreen *sc= CTX_wm_screen(C);
1705 /* try to find largest 3d-view available
1706 * (mostly of the time, this is what when user will want this,
1707 * as it's a standard feature in all other apps)
1709 //sab= find_biggest_area_of_type(SPACE_VIEW3D);
1710 sab= NULL; // XXX for now...
1712 commonkey_context_getv3d(C, sources, ksc);
1716 /* if not found, sab is now NULL, so perform own biggest area test */
1717 for (sa= sc->areabase.first; sa; sa= sa->next) { // XXX this has changed!
1718 int area= sa->winx * sa->winy;
1720 if (sa->spacetype != SPACE_TIME) {
1721 if ( (!sab) || (area > bigarea) ) {
1728 /* use whichever largest area was found (it shouldn't be a time window) */
1730 commonkey_context_get(C, sab, mode, sources, ksc);
1736 /* flush updates after all operations */
1737 static void commonkey_context_finish (const bContext *C, ListBase *sources)
1739 ScrArea *curarea= CTX_wm_area(C);
1740 Scene *scene= CTX_data_scene(C);
1742 /* check view type */
1743 switch (curarea->spacetype) {
1744 /* 3d view - first one tested as most often used */
1747 /* either pose or object level */
1748 if (OBACT && (OBACT->pose)) {
1749 //Object *ob= OBACT;
1751 /* recalculate ipo handles, etc. */
1752 // XXX this method has been removed!
1754 // remake_action_ipos(ob->action);
1756 /* recalculate bone-paths on adding new keyframe? */
1757 // XXX missing function
1758 // TODO: currently, there is no setting to turn this on/off globally
1759 //if (ob->pose->flag & POSE_RECALCPATHS)
1760 // pose_recalculate_paths(ob);
1765 /* loop over bases (as seen in sources) */
1766 for (cks= sources->first; cks; cks= cks->next) {
1767 Object *ob= (Object *)cks->id;
1769 /* simply set recalc flag */
1770 ob->recalc |= OB_RECALC_OB;
1778 /* flush refreshes after undo */
1779 static void commonkey_context_refresh (bContext *C)
1781 ScrArea *curarea= CTX_wm_area(C);
1783 /* check view type */
1784 switch (curarea->spacetype) {
1785 /* 3d view - first one tested as most often used */
1789 ED_anim_dag_flush_update(C);
1793 /* buttons window */
1796 //allspace(REMAKEIPO, 0);
1797 //allqueue(REDRAWVIEW3D, 0);
1798 //allqueue(REDRAWMARKER, 0);
1806 /* Get the keying set that was chosen by the user from the menu */
1807 static bKeyingSet *get_keyingset_fromcontext (bKeyingContext *ksc, short index)
1809 /* check if index is valid */
1810 if (ELEM(NULL, ksc, ksc->keyingsets))
1812 if ((index < 1) || (index > ksc->tot))
1815 /* index starts from 1, and should directly correspond to keyingset in array */
1816 return (bKeyingSet *)(ksc->keyingsets + (index - 1));
1819 /* ---------------- Keyframe Management API -------------------- */
1821 /* Display a menu for handling the insertion of keyframes based on the active view */
1822 void common_modifykey (bContext *C, short mode)
1824 ListBase dsources = {NULL, NULL};
1825 bKeyingContext *ksc= NULL;
1827 bKeyingSet *ks = NULL;
1828 char *menustr, buf[64];
1831 /* check if mode is valid */
1832 if (ELEM(mode, COMMONKEY_MODE_INSERT, COMMONKEY_MODE_DELETE)==0)
1835 /* delegate to other functions or get keyingsets to use
1836 * - if the current area doesn't have its own handling, there will be data returned...
1838 commonkey_context_get(C, NULL, mode, &dsources, &ksc);
1840 /* check that there is data to operate on */
1841 if (ELEM(NULL, dsources.first, ksc)) {
1842 BLI_freelistN(&dsources);
1846 /* get menu and process it */
1847 if (mode == COMMONKEY_MODE_DELETE)
1848 menustr= build_keyingsets_menu(ksc, "Delete");
1850 menustr= build_keyingsets_menu(ksc, "Insert");
1851 // XXX: this goes to the invoke!
1852 //menu_nr= pupmenu(menustr);
1853 //if (menustr) MEM_freeN(menustr);
1854 menu_nr = -1; // XXX for now
1856 /* no item selected or shapekey entry? */
1858 /* free temp sources */
1859 BLI_freelistN(&dsources);
1861 /* check if insert new shapekey */
1862 // XXX missing function!
1863 //if ((menu_nr == 0) && (mode == COMMONKEY_MODE_INSERT))
1864 // insert_shapekey(OBACT);
1866 ksc->lastused= NULL;
1871 /* try to get keyingset */
1872 ks= get_keyingset_fromcontext(ksc, menu_nr);
1875 BLI_freelistN(&dsources);
1880 /* loop over each destination, applying the keying set */
1881 for (cks= dsources.first; cks; cks= cks->next) {
1884 /* special hacks for 'available' option */
1885 if (ks->flag == -2) {
1886 IpoCurve *icu= NULL, *icn= NULL;
1888 /* get first IPO-curve */
1889 if (cks->act && cks->actname) {
1890 bActionChannel *achan= get_action_channel(cks->act, cks->actname);
1892 // FIXME: what about constraint channels?
1893 if (achan && achan->ipo)
1894 icu= achan->ipo->curve.first;
1897 icu= cks->ipo->curve.first;
1899 /* we get adrcodes directly from IPO curves (see method below...) */
1900 for (; icu; icu= icn) {
1903 /* get next ipo-curve in case current is deleted */
1906 /* insert mode or delete mode */
1907 if (mode == COMMONKEY_MODE_DELETE) {
1908 /* local flags only add on to global flags */
1911 /* delete keyframe */
1912 success += deletekey(cks->id, ks->blocktype, cks->actname, cks->constname, icu->adrcode, flag);
1915 /* local flags only add on to global flags */
1917 if (IS_AUTOKEY_FLAG(AUTOMATKEY)) flag |= INSERTKEY_MATRIX;
1918 if (IS_AUTOKEY_FLAG(INSERTNEEDED)) flag |= INSERTKEY_NEEDED;
1919 // if (IS_AUTOKEY_MODE(EDITKEYS)) flag |= INSERTKEY_REPLACE;
1921 /* insert keyframe */
1922 success += insertkey(cks->id, ks->blocktype, cks->actname, cks->constname, icu->adrcode, flag);
1927 bKS_AdrcodeGetter kag;
1928 short (*get_next_adrcode)(bKS_AdrcodeGetter *);
1931 /* initialise keyingset channel iterator */
1932 ks_adrcodegetter_init(&kag, ks, cks);
1934 /* get iterator - only one can be in use at a time... the flags should be mutually exclusive in this regard */
1935 if (ks->flag & COMMONKEY_PCHANROT)
1936 get_next_adrcode= ks_getnextadrcode_pchanrot;
1937 else if (ks->flag & COMMONKEY_ADDMAP)
1938 get_next_adrcode= ks_getnextadrcode_addmap;
1940 get_next_adrcode= ks_getnextadrcode_default;
1942 /* loop over channels available in keyingset */
1943 for (adrcode= get_next_adrcode(&kag); adrcode > 0; adrcode= get_next_adrcode(&kag)) {
1946 /* insert mode or delete mode */
1947 if (mode == COMMONKEY_MODE_DELETE) {
1948 /* local flags only add on to global flags */
1950 //flag &= ~COMMONKEY_MODES;
1952 /* delete keyframe */
1953 success += deletekey(cks->id, ks->blocktype, cks->actname, cks->constname, adrcode, flag);
1956 /* local flags only add on to global flags */
1958 if (IS_AUTOKEY_FLAG(AUTOMATKEY)) flag |= INSERTKEY_MATRIX;
1959 if (IS_AUTOKEY_FLAG(INSERTNEEDED)) flag |= INSERTKEY_NEEDED;
1960 // if (IS_AUTOKEY_MODE(EDITKEYS)) flag |= INSERTKEY_REPLACE;
1961 flag &= ~COMMONKEY_MODES;
1963 /* insert keyframe */
1964 success += insertkey(cks->id, ks->blocktype, cks->actname, cks->constname, adrcode, flag);
1969 /* special handling for some key-sources */
1971 /* set pose recalc-paths flag */
1973 Object *ob= (Object *)cks->id;
1974 bPoseChannel *pchan= cks->pchan;
1976 /* set flag to trigger path recalc */
1978 ob->pose->flag |= POSE_RECALCPATHS;
1980 /* clear unkeyed flag (it doesn't matter if it's set or not) */
1981 // XXX old animation system
1983 // pchan->bone->flag &= ~BONE_UNKEYED;
1986 // XXX for new system, need to remove overrides
1990 /* apply post-keying flushes for this data sources */
1991 commonkey_context_finish(C, &dsources);
1993 /* free temp data */
1994 BLI_freelistN(&dsources);
1996 /* queue updates for contexts */
1997 commonkey_context_refresh(C);
2000 #endif // XXX old keyingsets code based on adrcodes... to be restored in due course
2002 /* Given a KeyingSet and context info (if required), modify keyframes for the channels specified
2003 * by the KeyingSet. This takes into account many of the different combinations of using KeyingSets.
2004 * Returns the number of channels that keyframes were added to
2006 static int commonkey_modifykey (ListBase *dsources, KeyingSet *ks, short mode, float cfra)
2009 int kflag=0, success= 0;
2010 char *groupname= NULL;
2012 /* get flags to use */
2013 if (mode == COMMONKEY_MODE_INSERT) {
2014 /* use KeyingSet's flags as base */
2015 kflag= ks->keyingflag;
2017 /* suppliment with info from the context */
2018 if (IS_AUTOKEY_FLAG(AUTOMATKEY)) kflag |= INSERTKEY_MATRIX;
2019 if (IS_AUTOKEY_FLAG(INSERTNEEDED)) kflag |= INSERTKEY_NEEDED;
2020 // if (IS_AUTOKEY_MODE(EDITKEYS)) flag |= INSERTKEY_REPLACE;
2022 else if (mode == COMMONKEY_MODE_DELETE)
2025 /* check if the KeyingSet is absolute or not (i.e. does it requires sources info) */
2026 if (ks->flag & KEYINGSET_ABSOLUTE) {
2027 /* Absolute KeyingSets are simpler to use, as all the destination info has already been
2028 * provided by the user, and is stored, ready to use, in the KeyingSet paths.
2030 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
2033 /* get pointer to name of group to add channels to */
2034 if (ksp->groupmode == KSP_GROUP_NONE)
2036 else if (ksp->groupmode == KSP_GROUP_KSNAME)
2037 groupname= ks->name;
2039 groupname= ksp->group;
2041 /* init arraylen and i - arraylen should be greater than i so that
2042 * normal non-array entries get keyframed correctly
2044 i= ksp->array_index;
2047 /* get length of array if whole array option is enabled */
2048 if (ksp->flag & KSP_FLAG_WHOLE_ARRAY) {
2049 PointerRNA id_ptr, ptr;
2052 RNA_id_pointer_create(ksp->id, &id_ptr);
2053 if (RNA_path_resolve(&id_ptr, ksp->rna_path, &ptr, &prop))
2054 arraylen= RNA_property_array_length(&ptr, prop);
2057 /* for each possible index, perform operation
2058 * - assume that arraylen is greater than index
2060 for (; i < arraylen; i++) {
2061 /* action to take depends on mode */
2062 if (mode == COMMONKEY_MODE_INSERT)
2063 success+= insertkey(ksp->id, groupname, ksp->rna_path, i, cfra, kflag);
2064 else if (mode == COMMONKEY_MODE_DELETE)
2065 success+= deletekey(ksp->id, groupname, ksp->rna_path, i, cfra, kflag);
2068 // TODO: set recalc tags on the ID?
2071 #if 0 // XXX still need to figure out how to get such keyingsets working
2072 else if (dsources) {
2073 /* for each one of the 'sources', resolve the template markers and expand arrays, then insert keyframes */
2078 /* for each 'source' for keyframe data, resolve each of the paths from the KeyingSet */
2079 for (cks= dsources->first; cks; cks= cks->next) {
2083 #endif // XXX still need to figure out how to get such
2088 /* Insert Key Operator ------------------------ */
2091 * This is one of the 'simpler new-style' Insert Keyframe operators which relies on Keying Sets.
2092 * For now, these are absolute Keying Sets only, so there is very little context info involved.
2094 * -- Joshua Leung, Feb 2009
2097 static int insert_key_exec (bContext *C, wmOperator *op)
2099 ListBase dsources = {NULL, NULL};
2100 Scene *scene= CTX_data_scene(C);
2101 KeyingSet *ks= NULL;
2102 float cfra= (float)CFRA; // XXX for now, don't bother about all the yucky offset crap
2105 /* try to get KeyingSet */
2106 if (scene->active_keyingset > 0)
2107 ks= BLI_findlink(&scene->keyingsets, scene->active_keyingset-1);
2108 /* report failure */
2110 BKE_report(op->reports, RPT_ERROR, "No active Keying Set");
2111 return OPERATOR_CANCELLED;
2114 /* try to insert keyframes for the channels specified by KeyingSet */
2115 success= commonkey_modifykey(&dsources, ks, COMMONKEY_MODE_INSERT, cfra);
2116 printf("KeyingSet '%s' - Successfully added %d Keyframes \n", ks->name, success);
2118 /* report failure? */
2120 BKE_report(op->reports, RPT_WARNING, "Keying Set failed to insert any keyframes");
2123 ED_anim_dag_flush_update(C);
2125 /* for now, only send ND_KEYS for KeyingSets */
2126 WM_event_add_notifier(C, ND_KEYS, NULL);
2128 return OPERATOR_FINISHED;
2131 void ANIM_OT_insert_keyframe (wmOperatorType *ot)
2134 ot->name= "Insert Keyframe";
2135 ot->idname= "ANIM_OT_insert_keyframe";
2138 ot->exec= insert_key_exec;
2139 ot->poll= ED_operator_areaactive;
2142 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2145 /* Delete Key Operator ------------------------ */
2148 * This is one of the 'simpler new-style' Insert Keyframe operators which relies on Keying Sets.
2149 * For now, these are absolute Keying Sets only, so there is very little context info involved.
2151 * -- Joshua Leung, Feb 2009
2154 static int delete_key_exec (bContext *C, wmOperator *op)
2156 ListBase dsources = {NULL, NULL};
2157 Scene *scene= CTX_data_scene(C);
2158 KeyingSet *ks= NULL;
2159 float cfra= (float)CFRA; // XXX for now, don't bother about all the yucky offset crap
2162 /* try to get KeyingSet */
2163 if (scene->active_keyingset > 0)
2164 ks= BLI_findlink(&scene->keyingsets, scene->active_keyingset-1);
2165 /* report failure */
2167 BKE_report(op->reports, RPT_ERROR, "No active Keying Set");
2168 return OPERATOR_CANCELLED;
2171 /* try to insert keyframes for the channels specified by KeyingSet */
2172 success= commonkey_modifykey(&dsources, ks, COMMONKEY_MODE_DELETE, cfra);
2173 printf("KeyingSet '%s' - Successfully removed %d Keyframes \n", ks->name, success);
2175 /* report failure? */
2177 BKE_report(op->reports, RPT_WARNING, "Keying Set failed to remove any keyframes");
2180 ED_anim_dag_flush_update(C);
2182 /* for now, only send ND_KEYS for KeyingSets */
2183 WM_event_add_notifier(C, ND_KEYS, NULL);
2185 return OPERATOR_FINISHED;
2188 void ANIM_OT_delete_keyframe (wmOperatorType *ot)
2191 ot->name= "Delete Keyframe";
2192 ot->idname= "ANIM_OT_delete_keyframe";
2195 ot->exec= delete_key_exec;
2197 ot->poll= ED_operator_areaactive;
2200 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2203 /* Insert Key Operator ------------------------ */
2206 * This is currently just a basic operator, which work in 3d-view context on objects/bones only
2207 * and will insert keyframes for a few settings only. This is until it becomes clear how
2208 * to separate (or not) the process for RNA-path creation between context + keyingsets.
2210 * -- Joshua Leung, Jan 2009
2213 /* defines for basic insert-key testing operator */
2214 // XXX this will definitely be replaced
2215 EnumPropertyItem prop_insertkey_types[] = {
2216 {0, "KEYINGSET", "Active KeyingSet", ""},
2217 {1, "OBLOC", "Object Location", ""},
2218 {2, "OBROT", "Object Rotation", ""},
2219 {3, "OBSCALE", "Object Scale", ""},
2220 {4, "MAT_COL", "Active Material - Color", ""},
2221 {5, "PCHANLOC", "Pose-Channel Location", ""},
2222 {6, "PCHANROT", "Pose-Channel Rotation", ""},
2223 {7, "PCHANSCALE", "Pose-Channel Scale", ""},
2224 {0, NULL, NULL, NULL}
2227 static int insert_key_old_invoke (bContext *C, wmOperator *op, wmEvent *event)
2229 Object *ob= CTX_data_active_object(C);
2233 return OPERATOR_CANCELLED;
2235 head= uiPupMenuBegin("Insert Keyframe", 0);
2237 /* active keyingset */
2238 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe", "type", 0);
2240 /* selective inclusion */
2241 if ((ob->pose) && (ob->flag & OB_POSEMODE)) {
2243 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe", "type", 5);
2244 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe", "type", 6);
2245 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe", "type", 7);
2249 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe", "type", 1);
2250 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe", "type", 2);
2251 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe", "type", 3);
2252 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe", "type", 4);
2255 uiPupMenuEnd(C, head);
2257 return OPERATOR_CANCELLED;
2260 static int insert_key_old_exec (bContext *C, wmOperator *op)
2262 Scene *scene= CTX_data_scene(C);
2263 short mode= RNA_enum_get(op->ptr, "type");
2264 float cfra= (float)CFRA; // XXX for now, don't bother about all the yucky offset crap
2266 /* for now, handle 'active keyingset' one separately */
2268 ListBase dsources = {NULL, NULL};
2269 KeyingSet *ks= NULL;
2272 /* try to get KeyingSet */
2273 if (scene->active_keyingset > 0)
2274 ks= BLI_findlink(&scene->keyingsets, scene->active_keyingset-1);
2275 /* report failure */
2277 BKE_report(op->reports, RPT_ERROR, "No active Keying Set");
2278 return OPERATOR_CANCELLED;
2281 /* try to insert keyframes for the channels specified by KeyingSet */
2282 success= commonkey_modifykey(&dsources, ks, COMMONKEY_MODE_INSERT, cfra);
2283 printf("KeyingSet '%s' - Successfully added %d Keyframes \n", ks->name, success);
2285 /* report failure? */
2287 BKE_report(op->reports, RPT_WARNING, "Keying Set failed to insert any keyframes");
2290 // more comprehensive tests will be needed
2291 CTX_DATA_BEGIN(C, Base*, base, selected_bases)
2293 Object *ob= base->object;
2297 /* check which keyframing mode chosen for this object */
2299 /* object-based keyframes */
2301 case 4: /* color of active material (only for geometry...) */
2302 // NOTE: this is just a demo... but ideally we'd go through materials instead of active one only so reference stays same
2303 // XXX no group for now
2304 success+= insertkey(id, NULL, "active_material.diffuse_color", 0, cfra, 0);
2305 success+= insertkey(id, NULL, "active_material.diffuse_color", 1, cfra, 0);
2306 success+= insertkey(id, NULL, "active_material.diffuse_color", 2, cfra, 0);
2308 case 3: /* object scale */
2309 success+= insertkey(id, "Object Transforms", "scale", 0, cfra, 0);
2310 success+= insertkey(id, "Object Transforms", "scale", 1, cfra, 0);
2311 success+= insertkey(id, "Object Transforms", "scale", 2, cfra, 0);
2313 case 2: /* object rotation */
2314 success+= insertkey(id, "Object Transforms", "rotation", 0, cfra, 0);
2315 success+= insertkey(id, "Object Transforms", "rotation", 1, cfra, 0);
2316 success+= insertkey(id, "Object Transforms", "rotation", 2, cfra, 0);
2318 case 1: /* object location */
2319 success+= insertkey(id, "Object Transforms", "location", 0, cfra, 0);
2320 success+= insertkey(id, "Object Transforms", "location", 1, cfra, 0);
2321 success+= insertkey(id, "Object Transforms", "location", 2, cfra, 0);
2325 ob->recalc |= OB_RECALC_OB;
2327 else if ((ob->pose) && (ob->flag & OB_POSEMODE)) {
2328 /* PoseChannel based keyframes */
2329 bPoseChannel *pchan;
2331 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2332 /* only if selected */
2333 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
2337 case 6: /* pchan scale */
2338 sprintf(buf, "pose.pose_channels[\"%s\"].scale", pchan->name);
2339 success+= insertkey(id, pchan->name, buf, 0, cfra, 0);
2340 success+= insertkey(id, pchan->name, buf, 1, cfra, 0);
2341 success+= insertkey(id, pchan->name, buf, 2, cfra, 0);
2343 case 5: /* pchan rotation */
2344 if (pchan->rotmode == PCHAN_ROT_QUAT) {
2345 sprintf(buf, "pose.pose_channels[\"%s\"].rotation", pchan->name);
2346 success+= insertkey(id, pchan->name, buf, 0, cfra, 0);
2347 success+= insertkey(id, pchan->name, buf, 1, cfra, 0);
2348 success+= insertkey(id, pchan->name, buf, 2, cfra, 0);
2349 success+= insertkey(id, pchan->name, buf, 3, cfra, 0);
2352 sprintf(buf, "pose.pose_channels[\"%s\"].euler_rotation", pchan->name);
2353 success+= insertkey(id, pchan->name, buf, 0, cfra, 0);
2354 success+= insertkey(id, pchan->name, buf, 1, cfra, 0);
2355 success+= insertkey(id, pchan->name, buf, 2, cfra, 0);
2358 default: /* pchan location */
2359 sprintf(buf, "pose.pose_channels[\"%s\"].location", pchan->name);
2360 success+= insertkey(id, pchan->name, buf, 0, cfra, 0);
2361 success+= insertkey(id, pchan->name, buf, 1, cfra, 0);
2362 success+= insertkey(id, pchan->name, buf, 2, cfra, 0);
2368 ob->recalc |= OB_RECALC_OB;
2371 printf("Ob '%s' - Successfully added %d Keyframes \n", id->name+2, success);
2377 ED_anim_dag_flush_update(C);
2379 if (mode == 0) /* for now, only send ND_KEYS for KeyingSets */
2380 WM_event_add_notifier(C, ND_KEYS, NULL);
2381 else if (mode == 4) /* material color requires different notifiers */
2382 WM_event_add_notifier(C, NC_MATERIAL|ND_KEYS, NULL);
2384 WM_event_add_notifier(C, NC_OBJECT|ND_KEYS, NULL);
2386 return OPERATOR_FINISHED;
2389 void ANIM_OT_insert_keyframe_old (wmOperatorType *ot)
2394 ot->name= "Insert Keyframe";
2395 ot->idname= "ANIM_OT_insert_keyframe_old";
2398 ot->invoke= insert_key_old_invoke;
2399 ot->exec= insert_key_old_exec;
2400 ot->poll= ED_operator_areaactive;
2403 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2406 // XXX update this for the latest RNA stuff styles...
2407 prop= RNA_def_property(ot->srna, "type", PROP_ENUM, PROP_NONE);
2408 RNA_def_property_enum_items(prop, prop_insertkey_types);
2411 /* Delete Key Operator ------------------------ */
2414 * This is currently just a basic operator, which work in 3d-view context on objects only.
2415 * -- Joshua Leung, Jan 2009
2418 static int delete_key_old_exec (bContext *C, wmOperator *op)
2420 Scene *scene= CTX_data_scene(C);
2421 float cfra= (float)CFRA; // XXX for now, don't bother about all the yucky offset crap
2423 // XXX more comprehensive tests will be needed
2424 CTX_DATA_BEGIN(C, Base*, base, selected_bases)
2426 Object *ob= base->object;
2431 /* loop through all curves in animdata and delete keys on this frame */
2433 AnimData *adt= ob->adt;
2434 bAction *act= adt->action;
2436 for (fcu= act->curves.first; fcu; fcu= fcn) {
2438 success+= deletekey(id, NULL, fcu->rna_path, fcu->array_index, cfra, 0);
2442 printf("Ob '%s' - Successfully removed %d keyframes \n", id->name+2, success);
2444 ob->recalc |= OB_RECALC_OB;
2449 ED_anim_dag_flush_update(C);
2451 // XXX what if it was a material keyframe?
2452 WM_event_add_notifier(C, NC_OBJECT|ND_KEYS, NULL);
2454 return OPERATOR_FINISHED;
2457 void ANIM_OT_delete_keyframe_old (wmOperatorType *ot)
2460 ot->name= "Delete Keyframe";
2461 ot->idname= "ANIM_OT_delete_keyframe_old";
2464 ot->invoke= WM_operator_confirm; // XXX we will need our own one eventually, to cope with the dynamic menus...
2465 ot->exec= delete_key_old_exec;
2467 ot->poll= ED_operator_areaactive;
2470 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2473 /* ******************************************* */
2474 /* KEYFRAME DETECTION */
2476 /* --------------- API/Per-Datablock Handling ------------------- */
2478 /* Checks whether an IPO-block has a keyframe for a given frame
2479 * Since we're only concerned whether a keyframe exists, we can simply loop until a match is found...
2481 short action_frame_has_keyframe (bAction *act, float frame, short filter)
2485 /* can only find if there is data */
2489 /* if only check non-muted, check if muted */
2490 if ((filter & ANIMFILTER_KEYS_MUTED) || (act->flag & ACT_MUTED))
2493 /* loop over F-Curves, using binary-search to try to find matches
2494 * - this assumes that keyframes are only beztriples
2496 for (fcu= act->curves.first; fcu; fcu= fcu->next) {
2497 /* only check if there are keyframes (currently only of type BezTriple) */
2498 if (fcu->bezt && fcu->totvert) {
2499 /* we either include all regardless of muting, or only non-muted */
2500 if ((filter & ANIMFILTER_KEYS_MUTED) || (fcu->flag & FCURVE_MUTED)==0) {
2502 int i = binarysearch_bezt_index(fcu->bezt, frame, fcu->totvert, &replace);
2504 /* binarysearch_bezt_index will set replace to be 0 or 1
2505 * - obviously, 1 represents a match
2508 /* sanity check: 'i' may in rare cases exceed arraylen */
2509 if ((i >= 0) && (i < fcu->totvert))
2520 /* Checks whether an Object has a keyframe for a given frame */
2521 short object_frame_has_keyframe (Object *ob, float frame, short filter)
2523 /* error checking */
2527 /* check own animation data - specifically, the action it contains */
2528 if ((ob->adt) && (ob->adt->action)) {
2529 if (action_frame_has_keyframe(ob->adt->action, frame, filter))
2533 /* try shapekey keyframes (if available, and allowed by filter) */
2534 if ( !(filter & ANIMFILTER_KEYS_LOCAL) && !(filter & ANIMFILTER_KEYS_NOSKEY) ) {
2535 Key *key= ob_get_key(ob);
2537 /* shapekeys can have keyframes ('Relative Shape Keys')
2538 * or depend on time (old 'Absolute Shape Keys')
2541 /* 1. test for relative (with keyframes) */
2542 if (id_frame_has_keyframe((ID *)key, frame, filter))
2545 /* 2. test for time */
2546 // TODO... yet to be implemented (this feature may evolve before then anyway)
2550 if ( !(filter & ANIMFILTER_KEYS_LOCAL) && !(filter & ANIMFILTER_KEYS_NOMAT) ) {
2551 /* if only active, then we can skip a lot of looping */
2552 if (filter & ANIMFILTER_KEYS_ACTIVE) {
2553 Material *ma= give_current_material(ob, (ob->actcol + 1));
2555 /* we only retrieve the active material... */
2556 if (id_frame_has_keyframe((ID *)ma, frame, filter))
2562 /* loop over materials */
2563 for (a=0; a<ob->totcol; a++) {
2564 Material *ma= give_current_material(ob, a+1);
2566 if (id_frame_has_keyframe((ID *)ma, frame, filter))
2576 /* --------------- API ------------------- */
2578 /* Checks whether a keyframe exists for the given ID-block one the given frame */
2579 short id_frame_has_keyframe (ID *id, float frame, short filter)
2585 /* perform special checks for 'macro' types */
2586 switch (GS(id->name)) {
2587 case ID_OB: /* object */
2588 return object_frame_has_keyframe((Object *)id, frame, filter);
2591 case ID_SCE: /* scene */
2592 // XXX TODO... for now, just use 'normal' behaviour
2595 default: /* 'normal type' */
2597 AnimData *adt= BKE_animdata_from_id(id);
2599 /* only check keyframes in active action */
2601 return action_frame_has_keyframe(adt->action, frame, filter);
2607 /* no keyframe found */
2611 /* ************************************************** */