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_constraint.h"
31 #include "BKE_fcurve.h"
32 #include "BKE_utildefines.h"
33 #include "BKE_context.h"
34 #include "BKE_report.h"
36 #include "BKE_material.h"
38 #include "ED_anim_api.h"
39 #include "ED_keyframing.h"
40 #include "ED_keyframes_edit.h"
41 #include "ED_screen.h"
44 #include "UI_interface.h"
49 #include "RNA_access.h"
50 #include "RNA_define.h"
51 #include "RNA_types.h"
53 /* ************************************************** */
54 /* LOCAL TYPES AND DEFINES */
56 /* ----------- Common KeyData Sources ------------ */
58 /* temporary struct to gather data combos to keyframe */
59 typedef struct bCommonKeySrc {
60 struct bCommonKeySrc *next, *prev;
62 /* general data/destination-source settings */
63 ID *id; /* id-block this comes from */
64 char *rna_path; /* base path to use */ // xxx.... maybe we don't need this?
67 bPoseChannel *pchan; /* only needed when doing recalcs... */
70 /* ******************************************* */
71 /* Animation Data Validation */
73 /* Get (or add relevant data to be able to do so) F-Curve from the Active Action,
74 * for the given Animation Data block. This assumes that all the destinations are valid.
76 FCurve *verify_fcurve (ID *id, const char group[], const char rna_path[], const int array_index, short add)
84 if ELEM(NULL, id, rna_path)
87 /* init animdata if none available yet */
88 adt= BKE_animdata_from_id(id);
89 if ((adt == NULL) && (add))
90 adt= BKE_id_add_animdata(id);
92 /* if still none (as not allowed to add, or ID doesn't have animdata for some reason) */
96 /* init action if none available yet */
97 // TODO: need some wizardry to handle NLA stuff correct
98 if ((adt->action == NULL) && (add))
99 adt->action= add_empty_action("Action");
102 /* try to find f-curve matching for this setting
103 * - add if not found and allowed to add one
104 * TODO: add auto-grouping support? how this works will need to be resolved
107 fcu= list_find_fcurve(&act->curves, rna_path, array_index);
111 if ((fcu == NULL) && (add)) {
112 /* use default settings to make a F-Curve */
113 fcu= MEM_callocN(sizeof(FCurve), "FCurve");
115 fcu->flag = (FCURVE_VISIBLE|FCURVE_AUTO_HANDLES|FCURVE_SELECTED);
116 if (act->curves.first==NULL)
117 fcu->flag |= FCURVE_ACTIVE; /* first one added active */
119 /* store path - make copy, and store that */
120 fcu->rna_path= BLI_strdupn(rna_path, strlen(rna_path));
121 fcu->array_index= array_index;
123 /* if a group name has been provided, try to add or find a group, then add F-Curve to it */
125 /* try to find group */
126 grp= action_groups_find_named(act, group);
128 /* no matching groups, so add one */
130 /* Add a new group, and make it active */
131 grp= MEM_callocN(sizeof(bActionGroup), "bActionGroup");
133 grp->flag = AGRP_SELECTED;
134 BLI_snprintf(grp->name, 64, group);
136 BLI_addtail(&act->groups, grp);
137 BLI_uniquename(&act->groups, grp, "Group", offsetof(bActionGroup, name), 64);
140 /* add F-Curve to group */
141 action_groups_add_channel(act, grp, fcu);
144 /* just add F-Curve to end of Action's list */
145 BLI_addtail(&act->curves, fcu);
149 /* return the F-Curve */
153 /* ************************************************** */
154 /* KEYFRAME INSERTION */
156 /* -------------- BezTriple Insertion -------------------- */
158 /* threshold for inserting keyframes - threshold here should be good enough for now, but should become userpref */
159 #define BEZT_INSERT_THRESH 0.00001f
161 /* Binary search algorithm for finding where to insert BezTriple. (for use by insert_bezt_icu)
162 * Returns the index to insert at (data already at that index will be offset if replace is 0)
164 static int binarysearch_bezt_index (BezTriple array[], float frame, int arraylen, short *replace)
166 int start=0, end=arraylen;
167 int loopbreaker= 0, maxloop= arraylen * 2;
169 /* initialise replace-flag first */
172 /* sneaky optimisations (don't go through searching process if...):
173 * - keyframe to be added is to be added out of current bounds
174 * - keyframe to be added would replace one of the existing ones on bounds
176 if ((arraylen <= 0) || (array == NULL)) {
177 printf("Warning: binarysearch_bezt_index() encountered invalid array \n");
181 /* check whether to add before/after/on */
184 /* 'First' Keyframe (when only one keyframe, this case is used) */
185 framenum= array[0].vec[1][0];
186 if (IS_EQT(frame, framenum, BEZT_INSERT_THRESH)) {
190 else if (frame < framenum)
193 /* 'Last' Keyframe */
194 framenum= array[(arraylen-1)].vec[1][0];
195 if (IS_EQT(frame, framenum, BEZT_INSERT_THRESH)) {
197 return (arraylen - 1);
199 else if (frame > framenum)
204 /* most of the time, this loop is just to find where to put it
205 * 'loopbreaker' is just here to prevent infinite loops
207 for (loopbreaker=0; (start <= end) && (loopbreaker < maxloop); loopbreaker++) {
208 /* compute and get midpoint */
209 int mid = start + ((end - start) / 2); /* we calculate the midpoint this way to avoid int overflows... */
210 float midfra= array[mid].vec[1][0];
212 /* check if exactly equal to midpoint */
213 if (IS_EQT(frame, midfra, BEZT_INSERT_THRESH)) {
218 /* repeat in upper/lower half */
221 else if (frame < midfra)
225 /* print error if loop-limit exceeded */
226 if (loopbreaker == (maxloop-1)) {
227 printf("Error: binarysearch_bezt_index() was taking too long \n");
229 // include debug info
230 printf("\tround = %d: start = %d, end = %d, arraylen = %d \n", loopbreaker, start, end, arraylen);
233 /* not found, so return where to place it */
237 /* This function adds a given BezTriple to an F-Curve. It will allocate
238 * memory for the array if needed, and will insert the BezTriple into a
239 * suitable place in chronological order.
241 * NOTE: any recalculate of the F-Curve that needs to be done will need to
242 * be done by the caller.
244 int insert_bezt_fcurve (FCurve *fcu, BezTriple *bezt)
251 i = binarysearch_bezt_index(fcu->bezt, bezt->vec[1][0], fcu->totvert, &replace);
254 /* sanity check: 'i' may in rare cases exceed arraylen */
255 // FIXME: do not overwrite handletype if just replacing...?
256 if ((i >= 0) && (i < fcu->totvert))
257 *(fcu->bezt + i) = *bezt;
261 newb= MEM_callocN((fcu->totvert+1)*sizeof(BezTriple), "beztriple");
263 /* add the beztriples that should occur before the beztriple to be pasted (originally in ei->icu) */
265 memcpy(newb, fcu->bezt, i*sizeof(BezTriple));
267 /* add beztriple to paste at index i */
270 /* add the beztriples that occur after the beztriple to be pasted (originally in icu) */
271 if (i < fcu->totvert)
272 memcpy(newb+i+1, fcu->bezt+i, (fcu->totvert-i)*sizeof(BezTriple));
274 /* replace (+ free) old with new */
275 MEM_freeN(fcu->bezt);
282 // TODO: need to check for old sample-data now...
283 fcu->bezt= MEM_callocN(sizeof(BezTriple), "beztriple");
289 /* we need to return the index, so that some tools which do post-processing can
290 * detect where we added the BezTriple in the array
295 /* This function is a wrapper for insert_bezt_icu, and should be used when
296 * adding a new keyframe to a curve, when the keyframe doesn't exist anywhere
299 * 'fast' - is only for the python API where importing BVH's would take an extreamly long time.
301 void insert_vert_fcurve (FCurve *fcu, float x, float y, short fast)
306 /* set all three points, for nicer start position */
307 memset(&beztr, 0, sizeof(BezTriple));
314 beztr.ipo= U.ipo_new; /* use default interpolation mode here... */
315 beztr.f1= beztr.f2= beztr.f3= SELECT;
316 beztr.h1= beztr.h2= HD_AUTO; // XXX what about when we replace an old one?
318 /* add temp beztriple to keyframes */
319 a= insert_bezt_fcurve(fcu, &beztr);
321 /* what if 'a' is a negative index?
322 * for now, just exit to prevent any segfaults
326 /* don't recalculate handles if fast is set
327 * - this is a hack to make importers faster
328 * - we may calculate twice (see editipo_changed(), due to autohandle needing two calculations)
330 if (!fast) calchandles_fcurve(fcu);
332 /* set handletype and interpolation */
333 if (fcu->totvert > 2) {
334 BezTriple *bezt= (fcu->bezt + a);
337 /* set handles (autohandles by default) */
340 if (a > 0) h1= (bezt-1)->h2;
341 if (a < fcu->totvert-1) h2= (bezt+1)->h1;
346 /* set interpolation from previous (if available) */
347 if (a > 0) bezt->ipo= (bezt-1)->ipo;
348 else if (a < fcu->totvert-1) bezt->ipo= (bezt+1)->ipo;
350 /* don't recalculate handles if fast is set
351 * - this is a hack to make importers faster
352 * - we may calculate twice (see editipo_changed(), due to autohandle needing two calculations)
354 if (!fast) calchandles_fcurve(fcu);
358 /* -------------- 'Smarter' Keyframing Functions -------------------- */
359 /* return codes for new_key_needed */
361 KEYNEEDED_DONTADD = 0,
367 /* This helper function determines whether a new keyframe is needed */
368 /* Cases where keyframes should not be added:
369 * 1. Keyframe to be added bewteen two keyframes with similar values
370 * 2. Keyframe to be added on frame where two keyframes are already situated
371 * 3. Keyframe lies at point that intersects the linear line between two keyframes
373 static short new_key_needed (FCurve *fcu, float cFrame, float nValue)
375 BezTriple *bezt=NULL, *prev=NULL;
377 float valA = 0.0f, valB = 0.0f;
379 /* safety checking */
380 if (fcu == NULL) return KEYNEEDED_JUSTADD;
381 totCount= fcu->totvert;
382 if (totCount == 0) return KEYNEEDED_JUSTADD;
384 /* loop through checking if any are the same */
386 for (i=0; i<totCount; i++) {
387 float prevPosi=0.0f, prevVal=0.0f;
388 float beztPosi=0.0f, beztVal=0.0f;
390 /* get current time+value */
391 beztPosi= bezt->vec[1][0];
392 beztVal= bezt->vec[1][1];
395 /* there is a keyframe before the one currently being examined */
397 /* get previous time+value */
398 prevPosi= prev->vec[1][0];
399 prevVal= prev->vec[1][1];
401 /* keyframe to be added at point where there are already two similar points? */
402 if (IS_EQ(prevPosi, cFrame) && IS_EQ(beztPosi, cFrame) && IS_EQ(beztPosi, prevPosi)) {
403 return KEYNEEDED_DONTADD;
406 /* keyframe between prev+current points ? */
407 if ((prevPosi <= cFrame) && (cFrame <= beztPosi)) {
408 /* is the value of keyframe to be added the same as keyframes on either side ? */
409 if (IS_EQ(prevVal, nValue) && IS_EQ(beztVal, nValue) && IS_EQ(prevVal, beztVal)) {
410 return KEYNEEDED_DONTADD;
415 /* get real value of curve at that point */
416 realVal= evaluate_fcurve(fcu, cFrame);
418 /* compare whether it's the same as proposed */
419 if (IS_EQ(realVal, nValue))
420 return KEYNEEDED_DONTADD;
422 return KEYNEEDED_JUSTADD;
426 /* new keyframe before prev beztriple? */
427 if (cFrame < prevPosi) {
428 /* A new keyframe will be added. However, whether the previous beztriple
429 * stays around or not depends on whether the values of previous/current
430 * beztriples and new keyframe are the same.
432 if (IS_EQ(prevVal, nValue) && IS_EQ(beztVal, nValue) && IS_EQ(prevVal, beztVal))
433 return KEYNEEDED_DELNEXT;
435 return KEYNEEDED_JUSTADD;
439 /* just add a keyframe if there's only one keyframe
440 * and the new one occurs before the exisiting one does.
442 if ((cFrame < beztPosi) && (totCount==1))
443 return KEYNEEDED_JUSTADD;
446 /* continue. frame to do not yet passed (or other conditions not met) */
447 if (i < (totCount-1)) {
455 /* Frame in which to add a new-keyframe occurs after all other keys
456 * -> If there are at least two existing keyframes, then if the values of the
457 * last two keyframes and the new-keyframe match, the last existing keyframe
458 * gets deleted as it is no longer required.
459 * -> Otherwise, a keyframe is just added. 1.0 is added so that fake-2nd-to-last
460 * keyframe is not equal to last keyframe.
462 bezt= (fcu->bezt + (fcu->totvert - 1));
463 valA= bezt->vec[1][1];
466 valB= prev->vec[1][1];
468 valB= bezt->vec[1][1] + 1.0f;
470 if (IS_EQ(valA, nValue) && IS_EQ(valA, valB))
471 return KEYNEEDED_DELPREV;
473 return KEYNEEDED_JUSTADD;
476 /* ------------------ RNA Data-Access Functions ------------------ */
478 /* Try to read value using RNA-properties obtained already */
479 static float setting_get_rna_value (PointerRNA *ptr, PropertyRNA *prop, int index)
483 switch (RNA_property_type(ptr, prop)) {
485 if (RNA_property_array_length(ptr, prop))
486 value= (float)RNA_property_boolean_get_index(ptr, prop, index);
488 value= (float)RNA_property_boolean_get(ptr, prop);
491 if (RNA_property_array_length(ptr, prop))
492 value= (float)RNA_property_int_get_index(ptr, prop, index);
494 value= (float)RNA_property_int_get(ptr, prop);
497 if (RNA_property_array_length(ptr, prop))
498 value= RNA_property_float_get_index(ptr, prop, index);
500 value= RNA_property_float_get(ptr, prop);
503 value= (float)RNA_property_enum_get(ptr, prop);
512 /* ------------------ 'Visual' Keyframing Functions ------------------ */
514 /* internal status codes for visualkey_can_use */
521 /* This helper function determines if visual-keyframing should be used when
522 * inserting keyframes for the given channel. As visual-keyframing only works
523 * on Object and Pose-Channel blocks, this should only get called for those
524 * blocktypes, when using "standard" keying but 'Visual Keying' option in Auto-Keying
527 static short visualkey_can_use (PointerRNA *ptr, PropertyRNA *prop)
529 bConstraint *con= NULL;
530 short searchtype= VISUALKEY_NONE;
531 char *identifier= NULL;
534 // TODO: this check is probably not needed, but it won't hurt
535 if (ELEM3(NULL, ptr, ptr->data, prop))
538 /* get first constraint and determine type of keyframe constraints to check for
539 * - constraints can be on either Objects or PoseChannels, so we only check if the
540 * ptr->type is RNA_Object or RNA_PoseChannel, which are the RNA wrapping-info for
541 * those structs, allowing us to identify the owner of the data
543 if (ptr->type == &RNA_Object) {
545 Object *ob= (Object *)ptr->data;
547 con= ob->constraints.first;
548 identifier= (char *)RNA_property_identifier(ptr, prop);
550 else if (ptr->type == &RNA_PoseChannel) {
552 bPoseChannel *pchan= (bPoseChannel *)ptr->data;
554 con= pchan->constraints.first;
555 identifier= (char *)RNA_property_identifier(ptr, prop);
558 /* check if any data to search using */
559 if (ELEM(NULL, con, identifier))
562 /* location or rotation identifiers only... */
563 if (strstr(identifier, "location"))
564 searchtype= VISUALKEY_LOC;
565 else if (strstr(identifier, "rotation"))
566 searchtype= VISUALKEY_ROT;
568 printf("visualkey_can_use() failed: identifier - '%s' \n", identifier);
573 /* only search if a searchtype and initial constraint are available */
574 if (searchtype && con) {
575 for (; con; con= con->next) {
576 /* only consider constraint if it is not disabled, and has influence */
577 if (con->flag & CONSTRAINT_DISABLE) continue;
578 if (con->enforce == 0.0f) continue;
580 /* some constraints may alter these transforms */
582 /* multi-transform constraints */
583 case CONSTRAINT_TYPE_CHILDOF:
585 case CONSTRAINT_TYPE_TRANSFORM:
587 case CONSTRAINT_TYPE_FOLLOWPATH:
589 case CONSTRAINT_TYPE_KINEMATIC:
592 /* single-transform constraits */
593 case CONSTRAINT_TYPE_TRACKTO:
594 if (searchtype==VISUALKEY_ROT) return 1;
596 case CONSTRAINT_TYPE_ROTLIMIT:
597 if (searchtype==VISUALKEY_ROT) return 1;
599 case CONSTRAINT_TYPE_LOCLIMIT:
600 if (searchtype==VISUALKEY_LOC) return 1;
602 case CONSTRAINT_TYPE_ROTLIKE:
603 if (searchtype==VISUALKEY_ROT) return 1;
605 case CONSTRAINT_TYPE_DISTLIMIT:
606 if (searchtype==VISUALKEY_LOC) return 1;
608 case CONSTRAINT_TYPE_LOCLIKE:
609 if (searchtype==VISUALKEY_LOC) return 1;
611 case CONSTRAINT_TYPE_LOCKTRACK:
612 if (searchtype==VISUALKEY_ROT) return 1;
614 case CONSTRAINT_TYPE_MINMAX:
615 if (searchtype==VISUALKEY_LOC) return 1;
624 /* when some condition is met, this function returns, so here it can be 0 */
628 /* This helper function extracts the value to use for visual-keyframing
629 * In the event that it is not possible to perform visual keying, try to fall-back
630 * to using the default method. Assumes that all data it has been passed is valid.
632 static float visualkey_get_value (PointerRNA *ptr, PropertyRNA *prop, int array_index)
634 char *identifier= (char *)RNA_property_identifier(ptr, prop);
636 /* handle for Objects or PoseChannels only
637 * - constraints can be on either Objects or PoseChannels, so we only check if the
638 * ptr->type is RNA_Object or RNA_PoseChannel, which are the RNA wrapping-info for
639 * those structs, allowing us to identify the owner of the data
640 * - assume that array_index will be sane
642 if (ptr->type == &RNA_Object) {
643 Object *ob= (Object *)ptr->data;
645 /* parented objects are not supported, as the effects of the parent
646 * are included in the matrix, which kindof beats the point
648 if (ob->parent == NULL) {
649 /* only Location or Rotation keyframes are supported now */
650 if (strstr(identifier, "location")) {
651 return ob->obmat[3][array_index];
653 else if (strstr(identifier, "rotation")) {
656 Mat4ToEul(ob->obmat, eul);
657 return eul[array_index];
661 else if (ptr->type == &RNA_PoseChannel) {
662 Object *ob= (Object *)ptr->id.data; /* we assume that this is always set, and is an object */
663 bPoseChannel *pchan= (bPoseChannel *)ptr->data;
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 (strstr(identifier, "location")) {
676 /* only use for non-connected bones */
677 if ((pchan->bone->parent) && !(pchan->bone->flag & BONE_CONNECTED))
678 return tmat[3][array_index];
679 else if (pchan->bone->parent == NULL)
680 return tmat[3][array_index];
682 else if (strstr(identifier, "euler_rotation")) {
685 /* euler-rotation test before standard rotation, as standard rotation does quats */
686 Mat4ToEul(tmat, eul);
687 return eul[array_index];
689 else if (strstr(identifier, "rotation")) {
690 float trimat[3][3], quat[4];
692 Mat3CpyMat4(trimat, tmat);
693 Mat3ToQuat_is_ok(trimat, quat);
695 return quat[array_index];
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 || prop == NULL) {
722 printf("Insert Key: Could not insert keyframe, as RNA Path is invalid for the given ID (%s)\n", rna_path);
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 if (fcu->totvert == 0) {
856 BLI_remlink(&act->curves, fcu);
869 /* ******************************************* */
872 /* Operators ------------------------------------------- */
874 /* These operators are only provided for scripting/macro usage, not for direct
875 * calling from the UI since they wrap some of the data-access API code for these
876 * (defined in blenkernel) which have quite a few properties.
881 static int keyingset_add_destination_exec (bContext *C, wmOperator *op)
886 char rna_path[256], group_name[64]; // xxx
887 short groupmode=0, flag=0;
890 /* get settings from operator properties */
891 ptr = RNA_pointer_get(op->ptr, "keyingset");
893 ks= (KeyingSet *)ptr.data;
895 ptr = RNA_pointer_get(op->ptr, "id");
899 groupmode= RNA_enum_get(op->ptr, "grouping_method");
900 RNA_string_get(op->ptr, "group_name", group_name);
902 RNA_string_get(op->ptr, "rna_path", rna_path);
903 array_index= RNA_int_get(op->ptr, "array_index");
905 if (RNA_boolean_get(op->ptr, "entire_array"))
906 flag |= KSP_FLAG_WHOLE_ARRAY;
908 /* if enough args are provided, call API method */
910 BKE_keyingset_add_destination(ks, id, group_name, rna_path, array_index, flag, groupmode);
911 return OPERATOR_FINISHED;
914 BKE_report(op->reports, RPT_ERROR, "Keying Set could not be added.");
915 return OPERATOR_CANCELLED;
919 void ANIM_OT_keyingset_add_destination (wmOperatorType *ot)
921 // XXX: this is also defined in rna_animation.c
922 static EnumPropertyItem prop_mode_grouping_items[] = {
923 {KSP_GROUP_NAMED, "NAMED", "Named Group", ""},
924 {KSP_GROUP_NONE, "NONE", "None", ""},
925 {KSP_GROUP_KSNAME, "KEYINGSET", "Keying Set Name", ""},
926 {0, NULL, NULL, NULL}};
929 ot->name= "Add Keying Set Destination";
930 ot->idname= "ANIM_OT_keyingset_add_destination";
933 ot->exec= keyingset_add_destination_exec;
934 ot->poll= ED_operator_scene_editable;
937 /* pointers */ // xxx - do we want to directly expose these?
938 RNA_def_pointer_runtime(ot->srna, "keyingset", &RNA_KeyingSet, "Keying Set", "Keying Set to add destination to.");
939 RNA_def_pointer_runtime(ot->srna, "id", &RNA_ID, "ID", "ID-block for the destination.");
941 RNA_def_enum(ot->srna, "grouping_method", prop_mode_grouping_items, KSP_GROUP_NAMED, "Grouping Method", "Method used to define which Group-name to use.");
942 RNA_def_string(ot->srna, "group_name", "", 64, "Group Name", "Name of Action Group to assign destination to (only if grouping mode is to use this name).");
944 RNA_def_string(ot->srna, "rna_path", "", 256, "RNA-Path", "RNA-Path to destination property."); // xxx hopefully this is long enough
945 RNA_def_int(ot->srna, "array_index", 0, 0, INT_MAX, "Array Index", "If applicable, the index ", 0, INT_MAX);
947 RNA_def_boolean(ot->srna, "entire_array", 1, "Entire Array", "hen an 'array/vector' type is chosen (Location, Rotation, Color, etc.), entire array is to be used.");
953 static int keyingset_add_new_exec (bContext *C, wmOperator *op)
955 Scene *sce= CTX_data_scene(C);
957 short flag=0, keyingflag=0;
960 /* get settings from operator properties */
961 RNA_string_get(op->ptr, "name", name);
963 if (RNA_boolean_get(op->ptr, "absolute"))
964 flag |= KEYINGSET_ABSOLUTE;
965 if (RNA_boolean_get(op->ptr, "insertkey_needed"))
966 keyingflag |= INSERTKEY_NEEDED;
967 if (RNA_boolean_get(op->ptr, "insertkey_visual"))
968 keyingflag |= INSERTKEY_MATRIX;
970 /* call the API func, and set the active keyingset index */
971 ks= BKE_keyingset_add(&sce->keyingsets, name, flag, keyingflag);
974 sce->active_keyingset= BLI_countlist(&sce->keyingsets);
975 return OPERATOR_FINISHED;
978 BKE_report(op->reports, RPT_ERROR, "Keying Set could not be added.");
979 return OPERATOR_CANCELLED;
983 void ANIM_OT_keyingset_add_new (wmOperatorType *ot)
986 ot->name= "Add Keying Set";
987 ot->idname= "ANIM_OT_keyingset_add_new";
990 ot->exec= keyingset_add_new_exec;
991 ot->poll= ED_operator_scene_editable;
995 RNA_def_string(ot->srna, "name", "KeyingSet", 64, "Name", "Name of Keying Set");
997 RNA_def_boolean(ot->srna, "absolute", 1, "Absolute", "Keying Set defines specific paths/settings to be keyframed (i.e. is not reliant on context info)");
999 RNA_def_boolean(ot->srna, "insertkey_needed", 0, "Insert Keyframes - Only Needed", "Only insert keyframes where they're needed in the relevant F-Curves.");
1000 RNA_def_boolean(ot->srna, "insertkey_visual", 0, "Insert Keyframes - Visual", "Insert keyframes based on 'visual transforms'.");
1003 /* UI API --------------------------------------------- */
1005 /* Build menu-string of available keying-sets (allocates memory for string)
1006 * NOTE: mode must not be longer than 64 chars
1008 char *ANIM_build_keyingsets_menu (ListBase *list, short for_edit)
1010 DynStr *pupds= BLI_dynstr_new();
1016 /* add title first */
1017 BLI_dynstr_append(pupds, "Keying Sets%t|");
1019 /* add dummy entries for none-active */
1021 BLI_dynstr_append(pupds, "Add New%x-1|");
1022 BLI_dynstr_append(pupds, " %x0|");
1025 BLI_dynstr_append(pupds, "<No Keying Set Active>%x0|");
1027 /* loop through keyingsets, adding them */
1028 for (ks=list->first, i=1; ks; ks=ks->next, i++) {
1030 BLI_dynstr_append(pupds, "KS: ");
1032 BLI_dynstr_append(pupds, ks->name);
1033 BLI_snprintf( buf, 64, "%%x%d%s", i, ((ks->next)?"|":"") );
1034 BLI_dynstr_append(pupds, buf);
1037 /* convert to normal MEM_malloc'd string */
1038 str= BLI_dynstr_get_cstring(pupds);
1039 BLI_dynstr_free(pupds);
1046 /* ******************************************* */
1047 /* KEYFRAME MODIFICATION */
1049 /* mode for commonkey_modifykey */
1051 COMMONKEY_MODE_INSERT = 0,
1052 COMMONKEY_MODE_DELETE,
1053 } eCommonModifyKey_Modes;
1055 #if 0 // XXX old keyingsets code based on adrcodes... to be restored in due course
1057 /* --------- KeyingSet Adrcode Getters ------------ */
1059 /* initialise a channel-getter storage */
1060 static void ks_adrcodegetter_init (bKS_AdrcodeGetter *kag, bKeyingSet *ks, bCommonKeySrc *cks)
1062 /* error checking */
1066 if (ELEM(NULL, ks, cks)) {
1067 /* set invalid settings that won't cause harm */
1074 /* store settings */
1078 /* - index is -1, as that allows iterators to return first element
1079 * - tot is chan_num by default, but may get overriden if -1 is encountered (for extension-type getters)
1082 kag->tot= ks->chan_num;
1086 /* 'default' channel-getter that will be used when iterating through keyingset's channels
1087 * - iteration will stop when adrcode <= 0 is encountered, so we use that as escape
1089 static short ks_getnextadrcode_default (bKS_AdrcodeGetter *kag)
1091 bKeyingSet *ks= (kag)? kag->ks : NULL;
1093 /* error checking */
1094 if (ELEM(NULL, kag, ks)) return 0;
1095 if (kag->tot <= 0) return 0;
1098 if ((kag->index < 0) || (kag->index >= kag->tot)) return 0;
1100 /* return the adrcode stored at index then */
1101 return ks->adrcodes[kag->index];
1104 /* add map flag (for MTex channels, as certain ones need special offset) */
1105 static short ks_getnextadrcode_addmap (bKS_AdrcodeGetter *kag)
1107 short adrcode= ks_getnextadrcode_default(kag);
1109 /* if there was an adrcode returned, assume that kag stuff is set ok */
1111 bCommonKeySrc *cks= kag->cks;
1112 bKeyingSet *ks= kag->ks;
1114 if (ELEM3(ks->blocktype, ID_MA, ID_LA, ID_WO)) {
1116 case MAP_OFS_X: case MAP_OFS_Y: case MAP_OFS_Z:
1117 case MAP_SIZE_X: case MAP_SIZE_Y: case MAP_SIZE_Z:
1118 case MAP_R: case MAP_G: case MAP_B: case MAP_DVAR:
1119 case MAP_COLF: case MAP_NORF: case MAP_VARF: case MAP_DISP:
1120 adrcode += cks->map;
1126 /* adrcode must be returned! */
1130 /* extend posechannel keyingsets with rotation info (when KAG_CHAN_EXTEND is encountered)
1131 * - iteration will stop when adrcode <= 0 is encountered, so we use that as escape
1132 * - when we encounter KAG_CHAN_EXTEND as adrcode, start returning our own
1134 static short ks_getnextadrcode_pchanrot (bKS_AdrcodeGetter *kag)
1136 /* hardcoded adrcode channels used here only
1137 * - length is keyed-channels + 1 (last item must be 0 to escape)
1139 static short quat_adrcodes[5] = {AC_QUAT_W, AC_QUAT_X, AC_QUAT_Y, AC_QUAT_Z, 0};
1140 static short eul_adrcodes[4] = {AC_EUL_X, AC_EUL_Y, AC_EUL_Z, 0};
1142 /* useful variables */
1143 bKeyingSet *ks= (kag)? kag->ks : NULL;
1144 bCommonKeySrc *cks= (kag) ? kag->cks : NULL;
1145 short index, adrcode;
1147 /* error checking */
1148 if (ELEM3(NULL, kag, ks, cks)) return 0;
1149 if (ks->chan_num <= 0) return 0;
1152 * - if past the last item (kag->tot), return stuff from our static arrays
1153 * - otherwise, just keep returning stuff from the keyingset (but check out for -1!)
1159 /* normal (static stuff) */
1160 if (kag->index < kag->tot) {
1161 /* get adrcode, and return if not KAG_CHAN_EXTEND (i.e. point for start of iteration) */
1162 adrcode= ks->adrcodes[kag->index];
1164 if (adrcode != KAG_CHAN_EXTEND)
1167 kag->tot= kag->index;
1170 /* based on current rotation-mode
1171 * - index can be at most 5, if we are to prevent segfaults
1173 index= kag->index - kag->tot;
1174 if ((index < 0) || (index > 5))
1177 if (cks->pchan && cks->pchan->rotmode)
1178 return eul_adrcodes[index];
1180 return quat_adrcodes[index];
1183 /* ------------- KeyingSet Defines ------------ */
1184 /* Note: these must all be named with the defks_* prefix, otherwise the template macro will not work! */
1186 /* macro for defining keyingset contexts */
1187 #define KSC_TEMPLATE(ctx_name) {&defks_##ctx_name[0], NULL, sizeof(defks_##ctx_name)/sizeof(bKeyingSet)}
1191 /* check if option not available for deleting keys */
1192 static short incl_non_del_keys (bKeyingSet *ks, const char mode[])
1194 /* as optimisation, assume that it is sufficient to check only first letter
1195 * of mode (int comparison should be faster than string!)
1197 //if (strcmp(mode, "Delete")==0)
1198 if (mode && mode[0]=='D')
1204 /* Object KeyingSets ------ */
1206 /* check if include shapekey entry */
1207 static short incl_v3d_ob_shapekey (bKeyingSet *ks, const char mode[])
1209 //Object *ob= (G.obedit)? (G.obedit) : (OBACT); // XXX
1211 char *newname= NULL;
1216 /* not available for delete mode */
1217 if (strcmp(mode, "Delete")==0)
1220 /* check if is geom object that can get shapekeys */
1223 case OB_MESH: newname= "Mesh"; break;
1224 case OB_CURVE: newname= "Curve"; break;
1225 case OB_SURF: newname= "Surface"; break;
1226 case OB_LATTICE: newname= "Lattice"; break;
1233 /* if ks is shapekey entry (this could be callled for separator before too!) */
1235 BLI_strncpy(ks->name, newname, sizeof(ks->name));
1237 /* if it gets here, it's ok */
1241 /* array for object keyingset defines */
1242 bKeyingSet defks_v3d_object[] =
1244 /* include_cb, adrcode-getter, name, blocktype, flag, chan_num, adrcodes */
1245 {NULL, "Loc", ID_OB, 0, 3, {OB_LOC_X,OB_LOC_Y,OB_LOC_Z}},
1246 {NULL, "Rot", ID_OB, 0, 3, {OB_ROT_X,OB_ROT_Y,OB_ROT_Z}},
1247 {NULL, "Scale", ID_OB, 0, 3, {OB_SIZE_X,OB_SIZE_Y,OB_SIZE_Z}},
1249 {NULL, "%l", 0, -1, 0, {0}}, // separator
1251 {NULL, "LocRot", ID_OB, 0, 6,
1252 {OB_LOC_X,OB_LOC_Y,OB_LOC_Z,
1253 OB_ROT_X,OB_ROT_Y,OB_ROT_Z}},
1255 {NULL, "LocScale", ID_OB, 0, 6,
1256 {OB_LOC_X,OB_LOC_Y,OB_LOC_Z,
1257 OB_SIZE_X,OB_SIZE_Y,OB_SIZE_Z}},
1259 {NULL, "LocRotScale", ID_OB, 0, 9,
1260 {OB_LOC_X,OB_LOC_Y,OB_LOC_Z,
1261 OB_ROT_X,OB_ROT_Y,OB_ROT_Z,
1262 OB_SIZE_X,OB_SIZE_Y,OB_SIZE_Z}},
1264 {NULL, "RotScale", ID_OB, 0, 6,
1265 {OB_ROT_X,OB_ROT_Y,OB_ROT_Z,
1266 OB_SIZE_X,OB_SIZE_Y,OB_SIZE_Z}},
1268 {incl_non_del_keys, "%l", 0, -1, 0, {0}}, // separator
1270 {incl_non_del_keys, "VisualLoc", ID_OB, INSERTKEY_MATRIX, 3, {OB_LOC_X,OB_LOC_Y,OB_LOC_Z}},
1271 {incl_non_del_keys, "VisualRot", ID_OB, INSERTKEY_MATRIX, 3, {OB_ROT_X,OB_ROT_Y,OB_ROT_Z}},
1273 {incl_non_del_keys, "VisualLocRot", ID_OB, INSERTKEY_MATRIX, 6,
1274 {OB_LOC_X,OB_LOC_Y,OB_LOC_Z,
1275 OB_ROT_X,OB_ROT_Y,OB_ROT_Z}},
1277 {NULL, "%l", 0, -1, 0, {0}}, // separator
1279 {NULL, "Layer", ID_OB, 0, 1, {OB_LAY}}, // icky option...
1280 {NULL, "Available", ID_OB, -2, 0, {0}},
1282 {incl_v3d_ob_shapekey, "%l%l", 0, -1, 0, {0}}, // separator (linked to shapekey entry)
1283 {incl_v3d_ob_shapekey, "<ShapeKey>", ID_OB, -3, 0, {0}}
1286 /* PoseChannel KeyingSets ------ */
1288 /* array for posechannel keyingset defines */
1289 bKeyingSet defks_v3d_pchan[] =
1291 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
1292 {NULL, "Loc", ID_PO, 0, 3, {AC_LOC_X,AC_LOC_Y,AC_LOC_Z}},
1293 {NULL, "Rot", ID_PO, COMMONKEY_PCHANROT, 1, {KAG_CHAN_EXTEND}},
1294 {NULL, "Scale", ID_PO, 0, 3, {AC_SIZE_X,AC_SIZE_Y,AC_SIZE_Z}},
1296 {NULL, "%l", 0, -1, 0, {0}}, // separator
1298 {NULL, "LocRot", ID_PO, COMMONKEY_PCHANROT, 4,
1299 {AC_LOC_X,AC_LOC_Y,AC_LOC_Z,
1302 {NULL, "LocScale", ID_PO, 0, 6,
1303 {AC_LOC_X,AC_LOC_Y,AC_LOC_Z,
1304 AC_SIZE_X,AC_SIZE_Y,AC_SIZE_Z}},
1306 {NULL, "LocRotScale", ID_PO, COMMONKEY_PCHANROT, 7,
1307 {AC_LOC_X,AC_LOC_Y,AC_LOC_Z,AC_SIZE_X,AC_SIZE_Y,AC_SIZE_Z,
1310 {NULL, "RotScale", ID_PO, 0, 4,
1311 {AC_SIZE_X,AC_SIZE_Y,AC_SIZE_Z,
1314 {incl_non_del_keys, "%l", 0, -1, 0, {0}}, // separator
1316 {incl_non_del_keys, "VisualLoc", ID_PO, INSERTKEY_MATRIX, 3, {AC_LOC_X,AC_LOC_Y,AC_LOC_Z}},
1317 {incl_non_del_keys, "VisualRot", ID_PO, INSERTKEY_MATRIX|COMMONKEY_PCHANROT, 1, {KAG_CHAN_EXTEND}},
1319 {incl_non_del_keys, "VisualLocRot", ID_PO, INSERTKEY_MATRIX|COMMONKEY_PCHANROT, 4,
1320 {AC_LOC_X,AC_LOC_Y,AC_LOC_Z, KAG_CHAN_EXTEND}},
1322 {NULL, "%l", 0, -1, 0, {0}}, // separator
1324 {NULL, "Available", ID_PO, -2, 0, {0}}
1327 /* Material KeyingSets ------ */
1329 /* array for material keyingset defines */
1330 bKeyingSet defks_buts_shading_mat[] =
1332 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
1333 {NULL, "RGB", ID_MA, 0, 3, {MA_COL_R,MA_COL_G,MA_COL_B}},
1334 {NULL, "Alpha", ID_MA, 0, 1, {MA_ALPHA}},
1335 {NULL, "Halo Size", ID_MA, 0, 1, {MA_HASIZE}},
1336 {NULL, "Mode", ID_MA, 0, 1, {MA_MODE}}, // evil bitflags
1338 {NULL, "%l", 0, -1, 0, {0}}, // separator
1340 {NULL, "All Color", ID_MA, 0, 18,
1341 {MA_COL_R,MA_COL_G,MA_COL_B,
1342 MA_ALPHA,MA_HASIZE, MA_MODE,
1343 MA_SPEC_R,MA_SPEC_G,MA_SPEC_B,
1344 MA_REF,MA_EMIT,MA_AMB,MA_SPEC,MA_HARD,
1345 MA_MODE,MA_TRANSLU,MA_ADD}},
1347 {NULL, "All Mirror", ID_MA, 0, 5,
1348 {MA_RAYM,MA_FRESMIR,MA_FRESMIRI,
1349 MA_FRESTRA,MA_FRESTRAI}},
1351 {NULL, "%l", 0, -1, 0, {0}}, // separator
1353 {NULL, "Ofs", ID_MA, COMMONKEY_ADDMAP, 3, {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z}},
1354 {NULL, "Size", ID_MA, COMMONKEY_ADDMAP, 3, {MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z}},
1356 {NULL, "All Mapping", ID_MA, COMMONKEY_ADDMAP, 14,
1357 {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z,
1358 MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z,
1359 MAP_R,MAP_G,MAP_B,MAP_DVAR,
1360 MAP_COLF,MAP_NORF,MAP_VARF,MAP_DISP}},
1362 {NULL, "%l", 0, -1, 0, {0}}, // separator
1364 {NULL, "Available", ID_MA, -2, 0, {0}}
1367 /* World KeyingSets ------ */
1369 /* array for world keyingset defines */
1370 bKeyingSet defks_buts_shading_wo[] =
1372 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
1373 {NULL, "Zenith RGB", ID_WO, 0, 3, {WO_ZEN_R,WO_ZEN_G,WO_ZEN_B}},
1374 {NULL, "Horizon RGB", ID_WO, 0, 3, {WO_HOR_R,WO_HOR_G,WO_HOR_B}},
1376 {NULL, "%l", 0, -1, 0, {0}}, // separator
1378 {NULL, "Mist", ID_WO, 0, 4, {WO_MISI,WO_MISTDI,WO_MISTSTA,WO_MISTHI}},
1379 {NULL, "Stars", ID_WO, 0, 5, {WO_STAR_R,WO_STAR_G,WO_STAR_B,WO_STARDIST,WO_STARSIZE}},
1382 {NULL, "%l", 0, -1, 0, {0}}, // separator
1384 {NULL, "Ofs", ID_WO, COMMONKEY_ADDMAP, 3, {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z}},
1385 {NULL, "Size", ID_WO, COMMONKEY_ADDMAP, 3, {MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z}},
1387 {NULL, "All Mapping", ID_WO, COMMONKEY_ADDMAP, 14,
1388 {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z,
1389 MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z,
1390 MAP_R,MAP_G,MAP_B,MAP_DVAR,
1391 MAP_COLF,MAP_NORF,MAP_VARF,MAP_DISP}},
1393 {NULL, "%l", 0, -1, 0, {0}}, // separator
1395 {NULL, "Available", ID_WO, -2, 0, {0}}
1398 /* Lamp KeyingSets ------ */
1400 /* array for lamp keyingset defines */
1401 bKeyingSet defks_buts_shading_la[] =
1403 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
1404 {NULL, "RGB", ID_LA, 0, 3, {LA_COL_R,LA_COL_G,LA_COL_B}},
1405 {NULL, "Energy", ID_LA, 0, 1, {LA_ENERGY}},
1406 {NULL, "Spot Size", ID_LA, 0, 1, {LA_SPOTSI}},
1408 {NULL, "%l", 0, -1, 0, {0}}, // separator
1410 {NULL, "Ofs", ID_LA, COMMONKEY_ADDMAP, 3, {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z}},
1411 {NULL, "Size", ID_LA, COMMONKEY_ADDMAP, 3, {MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z}},
1413 {NULL, "All Mapping", ID_LA, COMMONKEY_ADDMAP, 14,
1414 {MAP_OFS_X,MAP_OFS_Y,MAP_OFS_Z,
1415 MAP_SIZE_X,MAP_SIZE_Y,MAP_SIZE_Z,
1416 MAP_R,MAP_G,MAP_B,MAP_DVAR,
1417 MAP_COLF,MAP_NORF,MAP_VARF,MAP_DISP}},
1419 {NULL, "%l", 0, -1, 0, {0}}, // separator
1421 {NULL, "Available", ID_LA, -2, 0, {0}}
1424 /* Texture KeyingSets ------ */
1426 /* array for texture keyingset defines */
1427 bKeyingSet defks_buts_shading_tex[] =
1429 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
1430 {NULL, "Clouds", ID_TE, 0, 5,
1431 {TE_NSIZE,TE_NDEPTH,TE_NTYPE,
1432 TE_MG_TYP,TE_N_BAS1}},
1434 {NULL, "Marble", ID_TE, 0, 7,
1435 {TE_NSIZE,TE_NDEPTH,TE_NTYPE,
1436 TE_TURB,TE_MG_TYP,TE_N_BAS1,TE_N_BAS2}},
1438 {NULL, "Stucci", ID_TE, 0, 5,
1439 {TE_NSIZE,TE_NTYPE,TE_TURB,
1440 TE_MG_TYP,TE_N_BAS1}},
1442 {NULL, "Wood", ID_TE, 0, 6,
1443 {TE_NSIZE,TE_NTYPE,TE_TURB,
1444 TE_MG_TYP,TE_N_BAS1,TE_N_BAS2}},
1446 {NULL, "Magic", ID_TE, 0, 2, {TE_NDEPTH,TE_TURB}},
1448 {NULL, "Blend", ID_TE, 0, 1, {TE_MG_TYP}},
1450 {NULL, "Musgrave", ID_TE, 0, 6,
1451 {TE_MG_TYP,TE_MGH,TE_MG_LAC,
1452 TE_MG_OCT,TE_MG_OFF,TE_MG_GAIN}},
1454 {NULL, "Voronoi", ID_TE, 0, 9,
1455 {TE_VNW1,TE_VNW2,TE_VNW3,TE_VNW4,
1456 TE_VNMEXP,TE_VN_DISTM,TE_VN_COLT,
1459 {NULL, "Distorted Noise", ID_TE, 0, 4,
1460 {TE_MG_OCT,TE_MG_OFF,TE_MG_GAIN,TE_DISTA}},
1462 {NULL, "Color Filter", ID_TE, 0, 5,
1463 {TE_COL_R,TE_COL_G,TE_COL_B,TE_BRIGHT,TE_CONTRA}},
1465 {NULL, "%l", 0, -1, 0, {0}}, // separator
1467 {NULL, "Available", ID_TE, -2, 0, {0}}
1470 /* Object Buttons KeyingSets ------ */
1472 /* check if include particles entry */
1473 static short incl_buts_ob (bKeyingSet *ks, const char mode[])
1475 //Object *ob= OBACT; // xxx
1477 /* only if object is mesh type */
1479 if(ob==NULL) return 0;
1480 return (ob->type == OB_MESH);
1483 /* array for texture keyingset defines */
1484 bKeyingSet defks_buts_object[] =
1486 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
1487 {incl_buts_ob, "Surface Damping", ID_OB, 0, 1, {OB_PD_SDAMP}},
1488 {incl_buts_ob, "Random Damping", ID_OB, 0, 1, {OB_PD_RDAMP}},
1489 {incl_buts_ob, "Permeability", ID_OB, 0, 1, {OB_PD_PERM}},
1491 {NULL, "%l", 0, -1, 0, {0}}, // separator
1493 {NULL, "Force Strength", ID_OB, 0, 1, {OB_PD_FSTR}},
1494 {NULL, "Force Falloff", ID_OB, 0, 1, {OB_PD_FFALL}},
1496 {NULL, "%l", 0, -1, 0, {0}}, // separator
1498 {NULL, "Available", ID_OB, -2, 0, {0}} // this will include ob-transforms too!
1501 /* Camera Buttons KeyingSets ------ */
1503 /* check if include internal-renderer entry */
1504 static short incl_buts_cam1 (bKeyingSet *ks, const char mode[])
1506 Scene *scene= NULL; // FIXME this will cause a crash, but we need an extra arg first!
1507 /* only if renderer is internal renderer */
1508 return (scene->r.renderer==R_INTERN);
1511 /* check if include external-renderer entry */
1512 static short incl_buts_cam2 (bKeyingSet *ks, const char mode[])
1514 Scene *scene= NULL; // FIXME this will cause a crash, but we need an extra arg first!
1515 /* only if renderer is internal renderer */
1516 return (scene->r.renderer!=R_INTERN);
1519 /* array for camera keyingset defines */
1520 bKeyingSet defks_buts_cam[] =
1522 /* include_cb, name, blocktype, flag, chan_num, adrcodes */
1523 {NULL, "Lens", ID_CA, 0, 1, {CAM_LENS}},
1524 {NULL, "Clipping", ID_CA, 0, 2, {CAM_STA,CAM_END}},
1525 {NULL, "Focal Distance", ID_CA, 0, 1, {CAM_YF_FDIST}},
1527 {NULL, "%l", 0, -1, 0, {0}}, // separator
1530 {incl_buts_cam2, "Aperture", ID_CA, 0, 1, {CAM_YF_APERT}},
1531 {incl_buts_cam1, "Viewplane Shift", ID_CA, 0, 2, {CAM_SHIFT_X,CAM_SHIFT_Y}},
1533 {NULL, "%l", 0, -1, 0, {0}}, // separator
1535 {NULL, "Available", ID_CA, -2, 0, {0}}
1540 /* Keying Context Defines - Must keep in sync with enumeration (eKS_Contexts) */
1541 bKeyingContext ks_contexts[] =
1543 KSC_TEMPLATE(v3d_object),
1544 KSC_TEMPLATE(v3d_pchan),
1546 KSC_TEMPLATE(buts_shading_mat),
1547 KSC_TEMPLATE(buts_shading_wo),
1548 KSC_TEMPLATE(buts_shading_la),
1549 KSC_TEMPLATE(buts_shading_tex),
1551 KSC_TEMPLATE(buts_object),
1552 KSC_TEMPLATE(buts_cam)
1555 /* Keying Context Enumeration - Must keep in sync with definitions*/
1556 typedef enum eKS_Contexts {
1568 /* make sure this last one remains untouched! */
1573 /* ---------------- KeyingSet Tools ------------------- */
1575 /* helper for commonkey_context_get() - get keyingsets for 3d-view */
1576 static void commonkey_context_getv3d (const bContext *C, ListBase *sources, bKeyingContext **ksc)
1578 Scene *scene= CTX_data_scene(C);
1582 if ((OBACT) && (OBACT->flag & OB_POSEMODE)) {
1583 bPoseChannel *pchan;
1587 *ksc= &ks_contexts[KSC_V3D_PCHAN];
1589 //set_pose_keys(ob); /* sets pchan->flag to POSE_KEY if bone selected, and clears if not */
1591 /* loop through posechannels */
1592 for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1593 if (pchan->flag & POSE_KEY) {
1596 /* add new keyframing destination */
1597 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1598 BLI_addtail(sources, cks);
1600 /* set id-block to key to, and action */
1602 cks->act= ob->action;
1606 cks->actname= pchan->name;
1612 *ksc= &ks_contexts[KSC_V3D_OBJECT];
1614 /* loop through bases */
1615 // XXX but we're only supposed to do this on editable ones, not just selected ones!
1616 CTX_DATA_BEGIN(C, Base*, base, selected_bases) {
1619 /* add new keyframing destination */
1620 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1621 BLI_addtail(sources, cks);
1623 /* set id-block to key to */
1631 /* helper for commonkey_context_get() - get keyingsets for buttons window */
1632 static void commonkey_context_getsbuts (const bContext *C, ListBase *sources, bKeyingContext **ksc)
1634 #if 0 // XXX dunno what's the future of this stuff...
1637 /* check on tab-type */
1638 switch (G.buts->mainb) {
1639 case CONTEXT_SHADING: /* ------------- Shading buttons ---------------- */
1640 /* subtabs include "Material", "Texture", "Lamp", "World"*/
1641 switch (G.buts->tab[CONTEXT_SHADING]) {
1642 case TAB_SHADING_MAT: /* >------------- Material Tab -------------< */
1644 Material *ma= editnode_get_active_material(G.buts->lockpoin);
1647 /* add new keyframing destination */
1648 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1649 BLI_addtail(sources, cks);
1654 cks->map= texchannel_to_adrcode(ma->texact);
1656 /* set keyingsets */
1657 *ksc= &ks_contexts[KSC_BUTS_MAT];
1662 case TAB_SHADING_WORLD: /* >------------- World Tab -------------< */
1664 World *wo= G.buts->lockpoin;
1667 /* add new keyframing destination */
1668 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1669 BLI_addtail(sources, cks);
1674 cks->map= texchannel_to_adrcode(wo->texact);
1676 /* set keyingsets */
1677 *ksc= &ks_contexts[KSC_BUTS_WO];
1682 case TAB_SHADING_LAMP: /* >------------- Lamp Tab -------------< */
1684 Lamp *la= G.buts->lockpoin;
1687 /* add new keyframing destination */
1688 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1689 BLI_addtail(sources, cks);
1694 cks->map= texchannel_to_adrcode(la->texact);
1696 /* set keyingsets */
1697 *ksc= &ks_contexts[KSC_BUTS_LA];
1702 case TAB_SHADING_TEX: /* >------------- Texture Tab -------------< */
1704 Tex *tex= G.buts->lockpoin;
1707 /* add new keyframing destination */
1708 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1709 BLI_addtail(sources, cks);
1715 /* set keyingsets */
1716 *ksc= &ks_contexts[KSC_BUTS_TEX];
1724 case CONTEXT_OBJECT: /* ------------- Object buttons ---------------- */
1729 /* add new keyframing destination */
1730 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1731 BLI_addtail(sources, cks);
1733 /* set id-block to key to */
1737 /* set keyingsets */
1738 *ksc= &ks_contexts[KSC_BUTS_OB];
1744 case CONTEXT_EDITING: /* ------------- Editing buttons ---------------- */
1748 if ((ob) && (ob->type==OB_CAMERA) && (G.buts->lockpoin)) { /* >---------------- camera buttons ---------------< */
1749 Camera *ca= G.buts->lockpoin;
1751 /* add new keyframing destination */
1752 cks= MEM_callocN(sizeof(bCommonKeySrc), "bCommonKeySrc");
1753 BLI_addtail(sources, cks);
1755 /* set id-block to key to */
1759 /* set keyingsets */
1760 *ksc= &ks_contexts[KSC_BUTS_CAM];
1766 #endif // XXX end of buttons stuff to port...
1768 /* if nothing happened... */
1772 #endif // XXX old keyingsets code based on adrcodes... to be restored in due course
1774 #if 0 // XXX new relative keyingsets code
1776 /* Check if context data is suitable for the given absolute Keying Set */
1777 static short keyingset_context_ok_poll (bContext *C, KeyingSet *ks)
1783 /* Get list of data-sources from context for inserting keyframes using the given relative Keying Set */
1784 static short commonkey_get_context_data (bContext *C, ListBase *dsources, KeyingSet *ks)
1789 #endif // XXX new relative keyingsets code
1791 /* Given a KeyingSet and context info (if required), modify keyframes for the channels specified
1792 * by the KeyingSet. This takes into account many of the different combinations of using KeyingSets.
1793 * Returns the number of channels that keyframes were added to
1795 static int commonkey_modifykey (ListBase *dsources, KeyingSet *ks, short mode, float cfra)
1798 int kflag=0, success= 0;
1799 char *groupname= NULL;
1801 /* get flags to use */
1802 if (mode == COMMONKEY_MODE_INSERT) {
1803 /* use KeyingSet's flags as base */
1804 kflag= ks->keyingflag;
1806 /* suppliment with info from the context */
1807 if (IS_AUTOKEY_FLAG(AUTOMATKEY)) kflag |= INSERTKEY_MATRIX;
1808 if (IS_AUTOKEY_FLAG(INSERTNEEDED)) kflag |= INSERTKEY_NEEDED;
1809 // if (IS_AUTOKEY_MODE(EDITKEYS)) flag |= INSERTKEY_REPLACE;
1811 else if (mode == COMMONKEY_MODE_DELETE)
1814 /* check if the KeyingSet is absolute or not (i.e. does it requires sources info) */
1815 if (ks->flag & KEYINGSET_ABSOLUTE) {
1816 /* Absolute KeyingSets are simpler to use, as all the destination info has already been
1817 * provided by the user, and is stored, ready to use, in the KeyingSet paths.
1819 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
1822 /* get pointer to name of group to add channels to */
1823 if (ksp->groupmode == KSP_GROUP_NONE)
1825 else if (ksp->groupmode == KSP_GROUP_KSNAME)
1826 groupname= ks->name;
1828 groupname= ksp->group;
1830 /* init arraylen and i - arraylen should be greater than i so that
1831 * normal non-array entries get keyframed correctly
1833 i= ksp->array_index;
1836 /* get length of array if whole array option is enabled */
1837 if (ksp->flag & KSP_FLAG_WHOLE_ARRAY) {
1838 PointerRNA id_ptr, ptr;
1841 RNA_id_pointer_create(ksp->id, &id_ptr);
1842 if (RNA_path_resolve(&id_ptr, ksp->rna_path, &ptr, &prop) && prop)
1843 arraylen= RNA_property_array_length(&ptr, prop);
1846 /* for each possible index, perform operation
1847 * - assume that arraylen is greater than index
1849 for (; i < arraylen; i++) {
1850 /* action to take depends on mode */
1851 if (mode == COMMONKEY_MODE_INSERT)
1852 success+= insertkey(ksp->id, groupname, ksp->rna_path, i, cfra, kflag);
1853 else if (mode == COMMONKEY_MODE_DELETE)
1854 success+= deletekey(ksp->id, groupname, ksp->rna_path, i, cfra, kflag);
1857 // TODO: set recalc tags on the ID?
1860 #if 0 // XXX still need to figure out how to get such keyingsets working
1861 else if (dsources) {
1862 /* for each one of the 'sources', resolve the template markers and expand arrays, then insert keyframes */
1867 /* for each 'source' for keyframe data, resolve each of the paths from the KeyingSet */
1868 for (cks= dsources->first; cks; cks= cks->next) {
1872 #endif // XXX still need to figure out how to get such
1878 /* Polling callback for use with ANIM_*_keyframe() operators
1879 * This is based on the standard ED_operator_areaactive callback,
1880 * except that it does special checks for a few spacetypes too...
1882 static int modify_key_op_poll(bContext *C)
1884 ScrArea *sa= CTX_wm_area(C);
1885 Scene *scene= CTX_data_scene(C);
1887 /* if no area or active scene */
1888 if (ELEM(NULL, sa, scene))
1891 /* if Outliner, only allow in DataBlocks view */
1892 if (sa->spacetype == SPACE_OUTLINER) {
1893 SpaceOops *so= (SpaceOops *)CTX_wm_space_data(C);
1895 if ((so->outlinevis != SO_DATABLOCKS))
1899 /* TODO: checks for other space types can be added here */
1901 /* should be fine */
1905 /* Insert Key Operator ------------------------ */
1908 * This is one of the 'simpler new-style' Insert Keyframe operators which relies on Keying Sets.
1909 * For now, these are absolute Keying Sets only, so there is very little context info involved.
1911 * -- Joshua Leung, Feb 2009
1914 static int insert_key_exec (bContext *C, wmOperator *op)
1916 ListBase dsources = {NULL, NULL};
1917 Scene *scene= CTX_data_scene(C);
1918 KeyingSet *ks= NULL;
1919 float cfra= (float)CFRA; // XXX for now, don't bother about all the yucky offset crap
1922 /* try to get KeyingSet */
1923 if (scene->active_keyingset > 0)
1924 ks= BLI_findlink(&scene->keyingsets, scene->active_keyingset-1);
1925 /* report failure */
1927 BKE_report(op->reports, RPT_ERROR, "No active Keying Set");
1928 return OPERATOR_CANCELLED;
1931 /* try to insert keyframes for the channels specified by KeyingSet */
1932 success= commonkey_modifykey(&dsources, ks, COMMONKEY_MODE_INSERT, cfra);
1933 printf("KeyingSet '%s' - Successfully added %d Keyframes \n", ks->name, success);
1935 /* report failure? */
1937 BKE_report(op->reports, RPT_WARNING, "Keying Set failed to insert any keyframes");
1940 ED_anim_dag_flush_update(C);
1942 /* for now, only send ND_KEYS for KeyingSets */
1943 WM_event_add_notifier(C, ND_KEYS, NULL);
1945 return OPERATOR_FINISHED;
1948 void ANIM_OT_insert_keyframe (wmOperatorType *ot)
1951 ot->name= "Insert Keyframe";
1952 ot->idname= "ANIM_OT_insert_keyframe";
1955 ot->exec= insert_key_exec;
1956 ot->poll= modify_key_op_poll;
1959 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1962 /* Delete Key Operator ------------------------ */
1965 * This is one of the 'simpler new-style' Insert Keyframe operators which relies on Keying Sets.
1966 * For now, these are absolute Keying Sets only, so there is very little context info involved.
1968 * -- Joshua Leung, Feb 2009
1971 static int delete_key_exec (bContext *C, wmOperator *op)
1973 ListBase dsources = {NULL, NULL};
1974 Scene *scene= CTX_data_scene(C);
1975 KeyingSet *ks= NULL;
1976 float cfra= (float)CFRA; // XXX for now, don't bother about all the yucky offset crap
1979 /* try to get KeyingSet */
1980 if (scene->active_keyingset > 0)
1981 ks= BLI_findlink(&scene->keyingsets, scene->active_keyingset-1);
1982 /* report failure */
1984 BKE_report(op->reports, RPT_ERROR, "No active Keying Set");
1985 return OPERATOR_CANCELLED;
1988 /* try to insert keyframes for the channels specified by KeyingSet */
1989 success= commonkey_modifykey(&dsources, ks, COMMONKEY_MODE_DELETE, cfra);
1990 printf("KeyingSet '%s' - Successfully removed %d Keyframes \n", ks->name, success);
1992 /* report failure? */
1994 BKE_report(op->reports, RPT_WARNING, "Keying Set failed to remove any keyframes");
1997 ED_anim_dag_flush_update(C);
1999 /* for now, only send ND_KEYS for KeyingSets */
2000 WM_event_add_notifier(C, ND_KEYS, NULL);
2002 return OPERATOR_FINISHED;
2005 void ANIM_OT_delete_keyframe (wmOperatorType *ot)
2008 ot->name= "Delete Keyframe";
2009 ot->idname= "ANIM_OT_delete_keyframe";
2012 ot->exec= delete_key_exec;
2013 ot->poll= modify_key_op_poll;
2016 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2019 /* Insert Key Operator ------------------------ */
2022 * This is currently just a basic operator, which work in 3d-view context on objects/bones only
2023 * and will insert keyframes for a few settings only. This is until it becomes clear how
2024 * to separate (or not) the process for RNA-path creation between context + keyingsets.
2026 * -- Joshua Leung, Jan 2009
2029 /* defines for basic insert-key testing operator */
2030 // XXX this will definitely be replaced
2031 EnumPropertyItem prop_insertkey_types[] = {
2032 {0, "KEYINGSET", "Active KeyingSet", ""},
2033 {1, "OBLOC", "Object Location", ""},
2034 {2, "OBROT", "Object Rotation", ""},
2035 {3, "OBSCALE", "Object Scale", ""},
2036 {4, "MAT_COL", "Active Material - Color", ""},
2037 {5, "PCHANLOC", "Pose-Channel Location", ""},
2038 {6, "PCHANROT", "Pose-Channel Rotation", ""},
2039 {7, "PCHANSCALE", "Pose-Channel Scale", ""},
2040 {0, NULL, NULL, NULL}
2043 static int insert_key_old_invoke (bContext *C, wmOperator *op, wmEvent *event)
2045 Object *ob= CTX_data_active_object(C);
2049 return OPERATOR_CANCELLED;
2051 head= uiPupMenuBegin("Insert Keyframe", 0);
2053 /* active keyingset */
2054 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe_old", "type", 0);
2056 /* selective inclusion */
2057 if ((ob->pose) && (ob->flag & OB_POSEMODE)) {
2059 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe_old", "type", 5);
2060 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe_old", "type", 6);
2061 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe_old", "type", 7);
2065 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe_old", "type", 1);
2066 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe_old", "type", 2);
2067 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe_old", "type", 3);
2068 uiMenuItemEnumO(head, "", 0, "ANIM_OT_insert_keyframe_old", "type", 4);
2071 uiPupMenuEnd(C, head);
2073 return OPERATOR_CANCELLED;
2076 static int insert_key_old_exec (bContext *C, wmOperator *op)
2078 Scene *scene= CTX_data_scene(C);
2079 short mode= RNA_enum_get(op->ptr, "type");
2080 float cfra= (float)CFRA; // XXX for now, don't bother about all the yucky offset crap
2082 /* for now, handle 'active keyingset' one separately */
2084 ListBase dsources = {NULL, NULL};
2085 KeyingSet *ks= NULL;
2088 /* try to get KeyingSet */
2089 if (scene->active_keyingset > 0)
2090 ks= BLI_findlink(&scene->keyingsets, scene->active_keyingset-1);
2091 /* report failure */
2093 BKE_report(op->reports, RPT_ERROR, "No active Keying Set");
2094 return OPERATOR_CANCELLED;
2097 /* try to insert keyframes for the channels specified by KeyingSet */
2098 success= commonkey_modifykey(&dsources, ks, COMMONKEY_MODE_INSERT, cfra);
2099 printf("KeyingSet '%s' - Successfully added %d Keyframes \n", ks->name, success);
2101 /* report failure? */
2103 BKE_report(op->reports, RPT_WARNING, "Keying Set failed to insert any keyframes");
2106 // more comprehensive tests will be needed
2107 CTX_DATA_BEGIN(C, Base*, base, selected_bases)
2109 Object *ob= base->object;
2113 /* check which keyframing mode chosen for this object */
2115 /* object-based keyframes */
2117 case 4: /* color of active material (only for geometry...) */
2118 // NOTE: this is just a demo... but ideally we'd go through materials instead of active one only so reference stays same
2119 // XXX no group for now
2120 success+= insertkey(id, NULL, "active_material.diffuse_color", 0, cfra, 0);
2121 success+= insertkey(id, NULL, "active_material.diffuse_color", 1, cfra, 0);
2122 success+= insertkey(id, NULL, "active_material.diffuse_color", 2, cfra, 0);
2124 case 3: /* object scale */
2125 success+= insertkey(id, "Object Transforms", "scale", 0, cfra, 0);
2126 success+= insertkey(id, "Object Transforms", "scale", 1, cfra, 0);
2127 success+= insertkey(id, "Object Transforms", "scale", 2, cfra, 0);
2129 case 2: /* object rotation */
2130 success+= insertkey(id, "Object Transforms", "rotation", 0, cfra, 0);
2131 success+= insertkey(id, "Object Transforms", "rotation", 1, cfra, 0);
2132 success+= insertkey(id, "Object Transforms", "rotation", 2, cfra, 0);
2134 default: /* object location */
2135 success+= insertkey(id, "Object Transforms", "location", 0, cfra, 0);
2136 success+= insertkey(id, "Object Transforms", "location", 1, cfra, 0);
2137 success+= insertkey(id, "Object Transforms", "location", 2, cfra, 0);
2141 ob->recalc |= OB_RECALC_OB;
2143 else if ((ob->pose) && (ob->flag & OB_POSEMODE)) {
2144 /* PoseChannel based keyframes */
2145 bPoseChannel *pchan;
2147 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2148 /* only if selected */
2149 if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED)) {
2153 case 7: /* pchan scale */
2154 sprintf(buf, "pose.pose_channels[\"%s\"].scale", pchan->name);
2155 success+= insertkey(id, pchan->name, buf, 0, cfra, 0);
2156 success+= insertkey(id, pchan->name, buf, 1, cfra, 0);
2157 success+= insertkey(id, pchan->name, buf, 2, cfra, 0);
2159 case 6: /* pchan rotation */
2160 if (pchan->rotmode == PCHAN_ROT_QUAT) {
2161 sprintf(buf, "pose.pose_channels[\"%s\"].rotation", pchan->name);
2162 success+= insertkey(id, pchan->name, buf, 0, cfra, 0);
2163 success+= insertkey(id, pchan->name, buf, 1, cfra, 0);
2164 success+= insertkey(id, pchan->name, buf, 2, cfra, 0);
2165 success+= insertkey(id, pchan->name, buf, 3, cfra, 0);
2168 sprintf(buf, "pose.pose_channels[\"%s\"].euler_rotation", pchan->name);
2169 success+= insertkey(id, pchan->name, buf, 0, cfra, 0);
2170 success+= insertkey(id, pchan->name, buf, 1, cfra, 0);
2171 success+= insertkey(id, pchan->name, buf, 2, cfra, 0);
2174 default: /* pchan location */
2175 sprintf(buf, "pose.pose_channels[\"%s\"].location", pchan->name);
2176 success+= insertkey(id, pchan->name, buf, 0, cfra, 0);
2177 success+= insertkey(id, pchan->name, buf, 1, cfra, 0);
2178 success+= insertkey(id, pchan->name, buf, 2, cfra, 0);
2184 ob->recalc |= OB_RECALC_OB;
2187 printf("Ob '%s' - Successfully added %d Keyframes \n", id->name+2, success);
2193 ED_anim_dag_flush_update(C);
2195 if (mode == 0) /* for now, only send ND_KEYS for KeyingSets */
2196 WM_event_add_notifier(C, ND_KEYS, NULL);
2197 else if (mode == 4) /* material color requires different notifiers */
2198 WM_event_add_notifier(C, NC_MATERIAL|ND_KEYS, NULL);
2200 WM_event_add_notifier(C, NC_OBJECT|ND_KEYS, NULL);
2202 return OPERATOR_FINISHED;
2205 void ANIM_OT_insert_keyframe_old (wmOperatorType *ot)
2210 ot->name= "Insert Keyframe";
2211 ot->idname= "ANIM_OT_insert_keyframe_old";
2214 ot->invoke= insert_key_old_invoke;
2215 ot->exec= insert_key_old_exec;
2216 ot->poll= ED_operator_areaactive;
2219 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2222 // XXX update this for the latest RNA stuff styles...
2223 prop= RNA_def_property(ot->srna, "type", PROP_ENUM, PROP_NONE);
2224 RNA_def_property_enum_items(prop, prop_insertkey_types);
2227 /* Delete Key Operator ------------------------ */
2230 * This is currently just a basic operator, which work in 3d-view context on objects only.
2231 * -- Joshua Leung, Jan 2009
2234 static int delete_key_old_exec (bContext *C, wmOperator *op)
2236 Scene *scene= CTX_data_scene(C);
2237 float cfra= (float)CFRA; // XXX for now, don't bother about all the yucky offset crap
2239 // XXX more comprehensive tests will be needed
2240 CTX_DATA_BEGIN(C, Base*, base, selected_bases)
2242 Object *ob= base->object;
2247 /* loop through all curves in animdata and delete keys on this frame */
2249 AnimData *adt= ob->adt;
2250 bAction *act= adt->action;
2252 for (fcu= act->curves.first; fcu; fcu= fcn) {
2254 success+= deletekey(id, NULL, fcu->rna_path, fcu->array_index, cfra, 0);
2258 printf("Ob '%s' - Successfully removed %d keyframes \n", id->name+2, success);
2260 ob->recalc |= OB_RECALC_OB;
2265 ED_anim_dag_flush_update(C);
2267 // XXX what if it was a material keyframe?
2268 WM_event_add_notifier(C, NC_OBJECT|ND_KEYS, NULL);
2270 return OPERATOR_FINISHED;
2273 void ANIM_OT_delete_keyframe_old (wmOperatorType *ot)
2276 ot->name= "Delete Keyframe";
2277 ot->idname= "ANIM_OT_delete_keyframe_old";
2280 ot->invoke= WM_operator_confirm; // XXX we will need our own one eventually, to cope with the dynamic menus...
2281 ot->exec= delete_key_old_exec;
2283 ot->poll= ED_operator_areaactive;
2286 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2289 /* ******************************************* */
2290 /* KEYFRAME DETECTION */
2292 /* --------------- API/Per-Datablock Handling ------------------- */
2294 /* Checks whether an IPO-block has a keyframe for a given frame
2295 * Since we're only concerned whether a keyframe exists, we can simply loop until a match is found...
2297 short action_frame_has_keyframe (bAction *act, float frame, short filter)
2301 /* can only find if there is data */
2305 /* if only check non-muted, check if muted */
2306 if ((filter & ANIMFILTER_KEYS_MUTED) || (act->flag & ACT_MUTED))
2309 /* loop over F-Curves, using binary-search to try to find matches
2310 * - this assumes that keyframes are only beztriples
2312 for (fcu= act->curves.first; fcu; fcu= fcu->next) {
2313 /* only check if there are keyframes (currently only of type BezTriple) */
2314 if (fcu->bezt && fcu->totvert) {
2315 /* we either include all regardless of muting, or only non-muted */
2316 if ((filter & ANIMFILTER_KEYS_MUTED) || (fcu->flag & FCURVE_MUTED)==0) {
2318 int i = binarysearch_bezt_index(fcu->bezt, frame, fcu->totvert, &replace);
2320 /* binarysearch_bezt_index will set replace to be 0 or 1
2321 * - obviously, 1 represents a match
2324 /* sanity check: 'i' may in rare cases exceed arraylen */
2325 if ((i >= 0) && (i < fcu->totvert))
2336 /* Checks whether an Object has a keyframe for a given frame */
2337 short object_frame_has_keyframe (Object *ob, float frame, short filter)
2339 /* error checking */
2343 /* check own animation data - specifically, the action it contains */
2344 if ((ob->adt) && (ob->adt->action)) {
2345 if (action_frame_has_keyframe(ob->adt->action, frame, filter))
2349 /* try shapekey keyframes (if available, and allowed by filter) */
2350 if ( !(filter & ANIMFILTER_KEYS_LOCAL) && !(filter & ANIMFILTER_KEYS_NOSKEY) ) {
2351 Key *key= ob_get_key(ob);
2353 /* shapekeys can have keyframes ('Relative Shape Keys')
2354 * or depend on time (old 'Absolute Shape Keys')
2357 /* 1. test for relative (with keyframes) */
2358 if (id_frame_has_keyframe((ID *)key, frame, filter))
2361 /* 2. test for time */
2362 // TODO... yet to be implemented (this feature may evolve before then anyway)
2366 if ( !(filter & ANIMFILTER_KEYS_LOCAL) && !(filter & ANIMFILTER_KEYS_NOMAT) ) {
2367 /* if only active, then we can skip a lot of looping */
2368 if (filter & ANIMFILTER_KEYS_ACTIVE) {
2369 Material *ma= give_current_material(ob, (ob->actcol + 1));
2371 /* we only retrieve the active material... */
2372 if (id_frame_has_keyframe((ID *)ma, frame, filter))
2378 /* loop over materials */
2379 for (a=0; a<ob->totcol; a++) {
2380 Material *ma= give_current_material(ob, a+1);
2382 if (id_frame_has_keyframe((ID *)ma, frame, filter))
2392 /* --------------- API ------------------- */
2394 /* Checks whether a keyframe exists for the given ID-block one the given frame */
2395 short id_frame_has_keyframe (ID *id, float frame, short filter)
2401 /* perform special checks for 'macro' types */
2402 switch (GS(id->name)) {
2403 case ID_OB: /* object */
2404 return object_frame_has_keyframe((Object *)id, frame, filter);
2407 case ID_SCE: /* scene */
2408 // XXX TODO... for now, just use 'normal' behaviour
2411 default: /* 'normal type' */
2413 AnimData *adt= BKE_animdata_from_id(id);
2415 /* only check keyframes in active action */
2417 return action_frame_has_keyframe(adt->action, frame, filter);
2423 /* no keyframe found */
2427 /* ************************************************** */