small merge needed for testing new animsys in 2.5 BGE, mixing own changes with merge...
[blender.git] / source / blender / editors / armature / poselib.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2007, Blender Foundation
21  * This is a new part of Blender
22  *
23  * Contributor(s): Joshua Leung
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27  
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stddef.h>
31 #include <string.h>
32 #include <math.h>
33 #include <float.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_arithb.h"
38 #include "BLI_blenlib.h"
39 #include "BLI_dynstr.h"
40
41 #include "DNA_listBase.h"
42 #include "DNA_anim_types.h"
43 #include "DNA_action_types.h"
44 #include "DNA_armature_types.h"
45 #include "DNA_curve_types.h"
46 #include "DNA_ipo_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_object_force.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_userdef_types.h"
51
52 #include "BKE_animsys.h"
53 #include "BKE_action.h"
54 #include "BKE_armature.h"
55 #include "BKE_depsgraph.h"
56 #include "BKE_modifier.h"
57 #include "BKE_object.h"
58
59 #include "BKE_global.h"
60 #include "BKE_context.h"
61 #include "BKE_report.h"
62 #include "BKE_utildefines.h"
63
64 #include "PIL_time.h"                   /* sleep                                */
65
66 #include "RNA_access.h"
67 #include "RNA_define.h"
68 #include "RNA_types.h"
69
70 #include "WM_api.h"
71 #include "WM_types.h"
72
73 #include "UI_interface.h"
74 #include "UI_resources.h"
75
76 #include "ED_anim_api.h"
77 #include "ED_armature.h"
78 #include "ED_keyframes_draw.h"
79 #include "ED_keyframing.h"
80 #include "ED_keyframes_edit.h"
81 #include "ED_screen.h"
82
83 #include "armature_intern.h"
84
85 /* ******* XXX ********** */
86
87 static void BIF_undo_push() {}
88 static void error() {}
89
90 static void action_set_activemarker() {}
91
92 /* ************************************************************* */
93 /* == POSE-LIBRARY TOOL FOR BLENDER == 
94  *      
95  * Overview: 
96  *      This tool allows animators to store a set of frequently used poses to dump into
97  *      the active action to help in "budget" productions to quickly block out new actions.
98  *      It acts as a kind of "glorified clipboard for poses", allowing for naming of poses.
99  *
100  * Features:
101  *      - PoseLibs are simply normal Actions
102  *      - Each "pose" is simply a set of keyframes that occur on a particular frame
103  *              -> a set of TimeMarkers that belong to each Action, help 'label' where a 'pose' can be
104  *                 found in the Action
105  *      - The Scrollwheel or PageUp/Down buttons when used in a special mode or after pressing/holding
106  *        [a modifier] key, cycles through the poses available for the active pose's poselib, allowing the
107  *        animator to preview what action best suits that pose
108  */
109 /* ************************************************************* */
110
111 /* gets list of poses in poselib as a string usable for pupmenu() */
112 char *poselib_build_poses_menu (bAction *act, char title[])
113 {
114         DynStr *pupds= BLI_dynstr_new();
115         TimeMarker *marker;
116         char *str;
117         char buf[64];
118         int i;
119         
120         /* add title first */
121         sprintf(buf, "%s%%t|", title);
122         BLI_dynstr_append(pupds, buf);
123         
124         /* loop through markers, adding them */
125         for (marker=act->markers.first, i=1; marker; marker=marker->next, i++) {
126                 BLI_dynstr_append(pupds, marker->name);
127                 
128                 sprintf(buf, "%%x%d", i);
129                 BLI_dynstr_append(pupds, buf);
130                 
131                 if (marker->next)
132                         BLI_dynstr_append(pupds, "|");
133         }
134         
135         /* convert to normal MEM_malloc'd string */
136         str= BLI_dynstr_get_cstring(pupds);
137         BLI_dynstr_free(pupds);
138         
139         return str;
140 }
141
142 /* gets the first available frame in poselib to store a pose on 
143  *      - frames start from 1, and a pose should occur on every frame... 0 is error!
144  */
145 int poselib_get_free_index (bAction *act)
146 {
147         TimeMarker *marker;
148         int low=0, high=0;
149         
150         /* sanity checks */
151         if (ELEM(NULL, act, act->markers.first)) return 1;
152         
153         /* loop over poses finding various values (poses are not stored in chronological order) */
154         for (marker= act->markers.first; marker; marker= marker->next) {
155                 /* only increase low if value is 1 greater than low, to find "gaps" where
156                  * poses were removed from the poselib
157                  */
158                 if (marker->frame == (low + 1)) 
159                         low++;
160                 
161                 /* value replaces high if it is the highest value encountered yet */
162                 if (marker->frame > high) 
163                         high= marker->frame;
164         }
165         
166         /* - if low is not equal to high, then low+1 is a gap 
167          * - if low is equal to high, then high+1 is the next index (add at end) 
168          */
169         if (low < high) 
170                 return (low + 1);
171         else 
172                 return (high + 1);
173 }
174
175 /* returns the active pose for a poselib */
176 TimeMarker *poselib_get_active_pose (bAction *act)
177 {       
178         if ((act) && (act->active_marker))
179                 return BLI_findlink(&act->markers, act->active_marker-1);
180         else
181                 return NULL;
182 }
183
184 /* ************************************************************* */
185
186 /* Initialise a new poselib (whether it is needed or not) */
187 bAction *poselib_init_new (Object *ob)
188 {
189         /* sanity checks - only for armatures */
190         if (ELEM(NULL, ob, ob->pose))
191                 return NULL;
192         
193         /* init object's poselib action (unlink old one if there) */
194         if (ob->poselib)
195                 ob->poselib->id.us--;
196         ob->poselib= add_empty_action("PoseLib");
197         
198         return ob->poselib;
199 }
200
201 /* Initialise a new poselib (checks if that needs to happen) */
202 bAction *poselib_validate (Object *ob)
203 {
204         if (ELEM(NULL, ob, ob->pose))
205                 return NULL;
206         else if (ob->poselib == NULL)
207                 return poselib_init_new(ob);
208         else
209                 return ob->poselib;
210 }
211
212
213 /* This tool automagically generates/validates poselib data so that it corresponds to the data 
214  * in the action. This is for use in making existing actions usable as poselibs.
215  */
216 // TODO: operatorfy me!
217 void poselib_validate_act (bAction *act)
218 {
219         ListBase keys = {NULL, NULL};
220         ActKeyColumn *ak;
221         TimeMarker *marker, *markern;
222         
223         /* validate action and poselib */
224         if (act == NULL)  {
225                 error("No Action to validate");
226                 return;
227         }
228         
229         /* determine which frames have keys */
230         action_to_keylist(act, &keys, NULL, NULL);
231         
232         /* for each key, make sure there is a correspnding pose */
233         for (ak= keys.first; ak; ak= ak->next) {
234                 /* check if any pose matches this */
235                 for (marker= act->markers.first; marker; marker= marker->next) {
236                         if (IS_EQ(marker->frame, ak->cfra)) {
237                                 marker->flag = -1;
238                                 break;
239                         }
240                 }
241                 
242                 /* add new if none found */
243                 if (marker == NULL) {
244                         char name[64];
245                         
246                         /* add pose to poselib */
247                         marker= MEM_callocN(sizeof(TimeMarker), "ActionMarker");
248                         
249                         strcpy(name, "Pose");
250                         BLI_strncpy(marker->name, name, sizeof(marker->name));
251                         
252                         marker->frame= (int)ak->cfra;
253                         marker->flag= -1;
254                         
255                         BLI_addtail(&act->markers, marker);
256                 }
257         }
258         
259         /* remove all untagged poses (unused), and remove all tags */
260         for (marker= act->markers.first; marker; marker= markern) {
261                 markern= marker->next;
262                 
263                 if (marker->flag != -1)
264                         BLI_freelinkN(&act->markers, marker);
265                 else
266                         marker->flag = 0;
267         }
268         
269         /* free temp memory */
270         BLI_freelistN(&keys);
271         
272         BIF_undo_push("PoseLib Validate Action");
273 }
274
275 /* ************************************************************* */
276
277 /* Pointers to the builtin KeyingSets that we want to use */
278 static KeyingSet *poselib_ks_locrotscale = NULL;                /* quaternion rotations */
279 static KeyingSet *poselib_ks_locrotscale2 = NULL;               /* euler rotations */           // XXX FIXME...
280 static short poselib_ks_need_init= 1;                                   /* have the above been obtained yet? */
281
282 /* Make sure the builtin KeyingSets are initialised properly 
283  * (only gets called on first run of  poselib_add_current_pose).
284  */
285 static void poselib_get_builtin_keyingsets (void)
286 {
287         /* only if we haven't got these yet */
288         // FIXME: this assumes that we will always get the builtin sets... 
289         if (poselib_ks_need_init) {
290                 /* LocRotScale (quaternions) */
291                 poselib_ks_locrotscale= ANIM_builtin_keyingset_get_named(NULL, "LocRotScale");
292                 
293                 /* LocRotScale (euler) */
294                 //ks_locrotscale2= ANIM_builtin_keyingset_get_named(ks_locrotscale, "LocRotScale");
295                 poselib_ks_locrotscale2= poselib_ks_locrotscale; // FIXME: for now, just use the same one...
296                 
297                 /* clear flag requesting init */
298                 poselib_ks_need_init= 0;
299         }
300 }
301
302 /* ----- */
303
304 static void poselib_add_menu_invoke__replacemenu (bContext *C, uiLayout *layout, void *arg)
305 {
306         Object *ob= CTX_data_active_object(C);
307         bAction *act= ob->poselib;
308         TimeMarker *marker;
309         
310         /* add each marker to this menu */
311         for (marker= act->markers.first; marker; marker= marker->next)
312                 uiItemIntO(layout, marker->name, ICON_ARMATURE_DATA, "POSELIB_OT_pose_add", "frame", marker->frame);
313 }
314
315 static int poselib_add_menu_invoke (bContext *C, wmOperator *op, wmEvent *evt)
316 {
317         Scene *scene= CTX_data_scene(C);
318         Object *ob= CTX_data_active_object(C);
319         bArmature *arm= (ob) ? ob->data : NULL;
320         bPose *pose= (ob) ? ob->pose : NULL;
321         uiPopupMenu *pup;
322         uiLayout *layout;
323         
324         /* sanity check */
325         if (ELEM3(NULL, ob, arm, pose)) 
326                 return OPERATOR_CANCELLED;
327         
328         /* start building */
329         pup= uiPupMenuBegin(C, op->type->name, 0);
330         layout= uiPupMenuLayout(pup);
331         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
332         
333         /* add new (adds to the first unoccupied frame) */
334         uiItemIntO(layout, "Add New", 0, "POSELIB_OT_pose_add", "frame", poselib_get_free_index(ob->poselib));
335         
336         /* check if we have any choices to add a new pose in any other way */
337         if ((ob->poselib) && (ob->poselib->markers.first)) {
338                 /* add new (on current frame) */
339                 uiItemIntO(layout, "Add New (Current Frame)", 0, "POSELIB_OT_pose_add", "frame", CFRA);
340                 
341                 /* replace existing - submenu */
342                 uiItemMenuF(layout, "Replace Existing...", 0, poselib_add_menu_invoke__replacemenu);
343         }
344         
345         uiPupMenuEnd(C, pup);
346         
347         /* this operator is only for a menu, not used further */
348         return OPERATOR_CANCELLED;
349 }
350
351
352 static int poselib_add_exec (bContext *C, wmOperator *op)
353 {
354         Object *ob= CTX_data_active_object(C);
355         bAction *act = poselib_validate(ob);
356         bArmature *arm= (ob) ? ob->data : NULL;
357         bPose *pose= (ob) ? ob->pose : NULL;
358         bPoseChannel *pchan;
359         TimeMarker *marker;
360         int frame= RNA_int_get(op->ptr, "frame");
361         char name[64];
362         
363         bCommonKeySrc cks;
364         ListBase dsources = {&cks, &cks};
365         
366         /* sanity check (invoke should have checked this anyway) */
367         if (ELEM3(NULL, ob, arm, pose)) 
368                 return OPERATOR_CANCELLED;
369         
370         /* get name to give to pose */
371         RNA_string_get(op->ptr, "name", name);
372         
373         /* add pose to poselib - replaces any existing pose there
374          *      - for the 'replace' option, this should end up finding the appropriate marker,
375          *        so no new one will be added
376          */
377         for (marker= act->markers.first; marker; marker= marker->next) {
378                 if (marker->frame == frame) {
379                         BLI_strncpy(marker->name, name, sizeof(marker->name));
380                         break;
381                 }
382         }
383         if (marker == NULL) {
384                 marker= MEM_callocN(sizeof(TimeMarker), "ActionMarker");
385                 
386                 BLI_strncpy(marker->name, name, sizeof(marker->name));
387                 marker->frame= frame;
388                 
389                 BLI_addtail(&act->markers, marker);
390         }
391         
392         /* validate name */
393         BLI_uniquename(&act->markers, marker, "Pose", '.', offsetof(TimeMarker, name), 64);
394         
395         /* make sure we've got KeyingSets to use */
396         poselib_get_builtin_keyingsets();
397         
398         /* init common-key-source for use by KeyingSets */
399         memset(&cks, 0, sizeof(bCommonKeySrc));
400         cks.id= &ob->id;
401         
402         /* loop through selected posechannels, keying their pose to the action */
403         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
404                 /* check if available */
405                 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
406                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) {
407                                 /* init cks for this PoseChannel, then use the relative KeyingSets to keyframe it */
408                                 cks.pchan= pchan;
409                                 
410                                 /* KeyingSet to use depends on rotation mode  */
411                                 if (pchan->rotmode)
412                                         modify_keyframes(C, &dsources, act, poselib_ks_locrotscale2, MODIFYKEY_MODE_INSERT, (float)frame);
413                                 else
414                                         modify_keyframes(C, &dsources, act, poselib_ks_locrotscale, MODIFYKEY_MODE_INSERT, (float)frame);
415                         }
416                 }
417         }
418         
419         /* store new 'active' pose number */
420         act->active_marker= BLI_countlist(&act->markers);
421         
422         /* done */
423         return OPERATOR_FINISHED;
424 }
425
426
427 void POSELIB_OT_pose_add (wmOperatorType *ot)
428 {
429         /* identifiers */
430         ot->name= "PoseLib Add Pose";
431         ot->idname= "POSELIB_OT_pose_add";
432         ot->description= "Add the current Pose to the active Pose Library";
433         
434         /* api callbacks */
435         ot->invoke= poselib_add_menu_invoke;
436         ot->exec= poselib_add_exec;
437         ot->poll= ED_operator_posemode;
438         
439         /* flags */
440         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
441         
442         /* properties */
443         RNA_def_int(ot->srna, "frame", 1, 0, INT_MAX, "Frame", "Frame to store pose on", 0, INT_MAX);
444         RNA_def_string(ot->srna, "name", "Pose", 64, "Pose Name", "Name of newly added Pose");
445 }
446
447 /* ----- */
448
449 static int poselib_stored_pose_menu_invoke (bContext *C, wmOperator *op, wmEvent *evt)
450 {
451         Object *ob= CTX_data_active_object(C);
452         bAction *act= (ob) ? ob->poselib : NULL;
453         TimeMarker *marker;
454         uiPopupMenu *pup;
455         uiLayout *layout;
456         int i;
457         
458         /* sanity check */
459         if (ELEM(NULL, ob, act)) 
460                 return OPERATOR_CANCELLED;
461         
462         /* start building */
463         pup= uiPupMenuBegin(C, op->type->name, 0);
464         layout= uiPupMenuLayout(pup);
465         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
466         
467         /* add each marker to this menu */
468         for (marker=act->markers.first, i=0; marker; marker= marker->next, i++)
469                 uiItemIntO(layout, marker->name, ICON_ARMATURE_DATA, op->idname, "index", i);
470         
471         uiPupMenuEnd(C, pup);
472         
473         /* this operator is only for a menu, not used further */
474         return OPERATOR_CANCELLED;
475 }
476
477
478
479 static int poselib_remove_exec (bContext *C, wmOperator *op)
480 {
481         Object *ob= CTX_data_active_object(C);
482         bAction *act= (ob) ? ob->poselib : NULL;
483         TimeMarker *marker;
484         FCurve *fcu;
485         
486         /* check if valid poselib */
487         if (act == NULL) {
488                 BKE_report(op->reports, RPT_ERROR, "Object doesn't have PoseLib data");
489                 return OPERATOR_CANCELLED;
490         }
491         
492         /* get index (and pointer) of pose to remove */
493         marker= BLI_findlink(&act->markers, RNA_int_get(op->ptr, "index"));
494         if (marker == NULL) {
495                 BKE_report(op->reports, RPT_ERROR, "Invalid index for Pose");
496         }
497         
498         /* remove relevant keyframes */
499         for (fcu= act->curves.first; fcu; fcu= fcu->next) {
500                 BezTriple *bezt;
501                 int i;
502                 
503                 if (fcu->bezt) {
504                         for (i=0, bezt=fcu->bezt; i < fcu->totvert; i++, bezt++) {
505                                 /* check if remove */
506                                 if (IS_EQ(bezt->vec[1][0], marker->frame)) {
507                                         delete_fcurve_key(fcu, i, 1);
508                                         break;
509                                 }
510                         }
511                 }
512         }
513         
514         /* remove poselib from list */
515         BLI_freelinkN(&act->markers, marker);
516         
517         /* fix active pose number */
518         act->active_marker= 0;
519         
520         /* done */
521         return OPERATOR_FINISHED;
522 }
523
524 void POSELIB_OT_pose_remove (wmOperatorType *ot)
525 {
526         /* identifiers */
527         ot->name= "PoseLib Remove Pose";
528         ot->idname= "POSELIB_OT_pose_remove";
529         ot->description= "Remove nth pose from the active Pose Library";
530         
531         /* api callbacks */
532         ot->invoke= poselib_stored_pose_menu_invoke;
533         ot->exec= poselib_remove_exec;
534         ot->poll= ED_operator_posemode;
535         
536         /* flags */
537         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
538         
539         /* properties */
540         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "The index of the pose to remove", 0, INT_MAX);
541 }
542
543
544
545 static int poselib_rename_exec (bContext *C, wmOperator *op)
546 {
547         Object *ob= CTX_data_active_object(C);
548         bAction *act= (ob) ? ob->poselib : NULL;
549         TimeMarker *marker;
550         char newname[64];
551         
552         /* check if valid poselib */
553         if (act == NULL) {
554                 BKE_report(op->reports, RPT_ERROR, "Object doesn't have PoseLib data");
555                 return OPERATOR_CANCELLED;
556         }
557         
558         /* get index (and pointer) of pose to remove */
559         marker= BLI_findlink(&act->markers, RNA_int_get(op->ptr, "index"));
560         if (marker == NULL) {
561                 BKE_report(op->reports, RPT_ERROR, "Invalid index for Pose");
562         }
563         
564         /* get new name */
565         RNA_string_get(op->ptr, "name", newname);
566         
567         /* copy name and validate it */
568         BLI_strncpy(marker->name, newname, sizeof(marker->name));
569         BLI_uniquename(&act->markers, marker, "Pose", '.', offsetof(TimeMarker, name), 64);
570         
571         /* done */
572         return OPERATOR_FINISHED;
573 }
574
575 void POSELIB_OT_pose_rename (wmOperatorType *ot)
576 {
577         /* identifiers */
578         ot->name= "PoseLib Rename Pose";
579         ot->idname= "POSELIB_OT_pose_rename";
580         ot->description= "Rename nth pose from the active Pose Library";
581         
582         /* api callbacks */
583         ot->invoke= poselib_stored_pose_menu_invoke;
584         ot->exec= poselib_rename_exec;
585         ot->poll= ED_operator_posemode;
586         
587         /* flags */
588         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
589         
590         /* properties */
591         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "The index of the pose to remove", 0, INT_MAX);
592         RNA_def_string(ot->srna, "name", "RenamedPose", 64, "New Pose Name", "New name for pose");
593 }
594
595 /* ************************************************************* */
596
597 /* Simple struct for storing settings/data for use during PoseLib preview */
598 typedef struct tPoseLib_PreviewData {
599         ListBase backups;               /* tPoseLib_Backup structs for restoring poses */
600         ListBase searchp;               /* LinkData structs storing list of poses which match the current search-string */
601         
602         Scene *scene;                   /* active scene */
603         ScrArea *sa;                    /* active area */
604         
605         PointerRNA rna_ptr;             /* RNA-Pointer to Object 'ob' */
606         Object *ob;                             /* object to work on */
607         bArmature *arm;                 /* object's armature data */
608         bPose *pose;                    /* object's pose */
609         bAction *act;                   /* poselib to use */
610         TimeMarker *marker;             /* 'active' pose */
611         
612         short state;                    /* state of main loop */
613         short redraw;                   /* redraw/update settings during main loop */
614         short flag;                             /* flags for various settings */
615         
616         int selcount;                   /* number of selected elements to work on */
617         int totcount;                   /* total number of elements to work on */
618         
619         char headerstr[200];    /* Info-text to print in header */
620         
621         char searchstr[64];             /* (Part of) Name to search for to filter poses that get shown */
622         char searchold[64];             /* Previously set searchstr (from last loop run), so that we can detected when to rebuild searchp */
623         short search_cursor;    /* position of cursor in searchstr (cursor occurs before the item at the nominated index) */
624 } tPoseLib_PreviewData;
625
626 /* defines for tPoseLib_PreviewData->state values */
627 enum {
628         PL_PREVIEW_ERROR = -1,
629         PL_PREVIEW_RUNNING,
630         PL_PREVIEW_CONFIRM,
631         PL_PREVIEW_CANCEL,
632         PL_PREVIEW_RUNONCE 
633 };
634
635 /* defines for tPoseLib_PreviewData->redraw values */
636 enum {
637         PL_PREVIEW_NOREDRAW = 0,
638         PL_PREVIEW_REDRAWALL,
639         PL_PREVIEW_REDRAWHEADER,
640 };
641
642 /* defines for tPoseLib_PreviewData->flag values */
643 enum {
644         PL_PREVIEW_FIRSTTIME    = (1<<0),
645         PL_PREVIEW_SHOWORIGINAL = (1<<1)
646 };
647
648 /* ---------------------------- */
649
650 /* simple struct for storing backup info */
651 typedef struct tPoseLib_Backup {
652         struct tPoseLib_Backup *next, *prev;
653         
654         bPoseChannel *pchan;
655         bPoseChannel olddata;
656 } tPoseLib_Backup;
657
658 /* Makes a copy of the current pose for restoration purposes - doesn't do constraints currently */
659 static void poselib_backup_posecopy (tPoseLib_PreviewData *pld)
660 {
661         bActionGroup *agrp;
662         bPoseChannel *pchan;
663         
664         /* for each posechannel that has an actionchannel in */
665         for (agrp= pld->act->groups.first; agrp; agrp= agrp->next) {
666                 /* try to find posechannel */
667                 pchan= get_pose_channel(pld->pose, agrp->name);
668                 
669                 /* backup data if available */
670                 if (pchan) {
671                         tPoseLib_Backup *plb;
672                         
673                         /* store backup */
674                         plb= MEM_callocN(sizeof(tPoseLib_Backup), "tPoseLib_Backup");
675                         
676                         plb->pchan= pchan;
677                         memcpy(&plb->olddata, plb->pchan, sizeof(bPoseChannel));
678                         
679                         BLI_addtail(&pld->backups, plb);
680                         
681                         /* mark as being affected */
682                         if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED))
683                                 pld->selcount++;
684                         pld->totcount++;
685                 }
686         }
687 }
688
689 /* Restores original pose - doesn't do constraints currently */
690 static void poselib_backup_restore (tPoseLib_PreviewData *pld)
691 {
692         tPoseLib_Backup *plb;
693         
694         for (plb= pld->backups.first; plb; plb= plb->next) {
695                 memcpy(plb->pchan, &plb->olddata, sizeof(bPoseChannel));
696         }
697 }
698
699 /* ---------------------------- */
700
701 /* Applies the appropriate stored pose from the pose-library to the current pose
702  *      - assumes that a valid object, with a poselib has been supplied
703  *      - gets the string to print in the header
704  *      - this code is based on the code for extract_pose_from_action in blenkernel/action.c
705  */
706 static void poselib_apply_pose (tPoseLib_PreviewData *pld)
707 {
708         PointerRNA *ptr= &pld->rna_ptr;
709         bArmature *arm= pld->arm;
710         bPose *pose= pld->pose;
711         bPoseChannel *pchan;
712         bAction *act= pld->act;
713         bActionGroup *agrp;
714         
715         BeztEditData bed;
716         BeztEditFunc group_ok_cb;
717         int frame= 1;
718         
719         /* get the frame */
720         if (pld->marker)
721                 frame= pld->marker->frame;
722         else
723                 return; 
724         
725         
726         /* init settings for testing groups for keyframes */
727         group_ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAMERANGE);
728         memset(&bed, 0, sizeof(BeztEditData)); 
729         bed.f1= ((float)frame) - 0.5f;
730         bed.f2= ((float)frame) + 0.5f;
731         
732         
733         /* start applying - only those channels which have a key at this point in time! */
734         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
735                 /* check if group has any keyframes */
736                 if (ANIM_animchanneldata_keys_bezier_loop(&bed, agrp, ALE_GROUP, NULL, group_ok_cb, NULL, 0)) {
737                         /* has keyframe on this frame, so try to get a PoseChannel with this name */
738                         pchan= get_pose_channel(pose, agrp->name);
739                         
740                         if (pchan) {    
741                                 short ok= 0;
742                                 
743                                 /* check if this bone should get any animation applied */
744                                 if (pld->selcount == 0) {
745                                         /* if no bones are selected, then any bone is ok */
746                                         ok= 1;
747                                 }
748                                 else if (pchan->bone) {
749                                         /* only ok if bone is visible and selected */
750                                         if ( (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) &&
751                                                  (pchan->bone->flag & BONE_HIDDEN_P)==0 &&
752                                                  (pchan->bone->layer & arm->layer) )
753                                                 ok = 1;
754                                 }
755                                 
756                                 if (ok) 
757                                         animsys_evaluate_action_group(ptr, act, agrp, NULL, (float)frame);
758                         }
759                 }
760         }
761 }
762
763 /* Auto-keys/tags bones affected by the pose used from the poselib */
764 static void poselib_keytag_pose (Scene *scene, tPoseLib_PreviewData *pld)
765 {
766         bPose *pose= pld->pose;
767         bPoseChannel *pchan;
768         bAction *act= pld->act;
769         bActionGroup *agrp;
770         
771         /* start tagging/keying */
772         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
773                 /* only for selected action channels */
774                 if (agrp->flag & AGRP_SELECTED) {
775                         pchan= get_pose_channel(pose, agrp->name);
776                         
777                         if (pchan) {
778 #if 0 // XXX old animation system       
779                                 // TODO: use a standard autokeying function in future (to allow autokeying-editkeys to work)
780                                 if (IS_AUTOKEY_MODE(NORMAL)) {
781                                         ID *id= &pld->ob->id;
782                                         
783                                         /* Set keys on pose */
784                                         if (pchan->flag & POSE_ROT) {
785                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X, 0);
786                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y, 0);
787                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z, 0);
788                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W, 0);
789                                         }
790                                         if (pchan->flag & POSE_SIZE) {
791                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X, 0);
792                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y, 0);
793                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z, 0);
794                                         }
795                                         if (pchan->flag & POSE_LOC) {
796                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X, 0);
797                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y, 0);
798                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z, 0);
799                                         }
800                                         
801                                         /* clear any unkeyed tags */
802                                         if (pchan->bone)
803                                                 pchan->bone->flag &= ~BONE_UNKEYED;
804                                 }
805                                 else {
806                                         /* add unkeyed tags */
807                                         if (pchan->bone)
808                                                 pchan->bone->flag |= BONE_UNKEYED;
809                                 }
810 #endif // XXX old animation system      
811                 
812                         }
813                 }
814         }
815 }
816
817 /* Apply the relevant changes to the pose */
818 static void poselib_preview_apply (bContext *C, wmOperator *op)
819 {
820         tPoseLib_PreviewData *pld= (tPoseLib_PreviewData *)op->customdata;
821         
822         /* only recalc pose (and its dependencies) if pose has changed */
823         if (pld->redraw == PL_PREVIEW_REDRAWALL) {
824                 /* don't clear pose if firsttime */
825                 if ((pld->flag & PL_PREVIEW_FIRSTTIME)==0)
826                         poselib_backup_restore(pld);
827                 else
828                         pld->flag &= ~PL_PREVIEW_FIRSTTIME;
829                         
830                 /* pose should be the right one to draw (unless we're temporarily not showing it) */
831                 if ((pld->flag & PL_PREVIEW_SHOWORIGINAL)==0) {
832                         RNA_int_set(op->ptr, "pose_index", BLI_findindex(&pld->act->markers, pld->marker));
833                         poselib_apply_pose(pld);
834                 }
835                 else
836                         RNA_int_set(op->ptr, "pose_index", -2); /* -2 means don't apply any pose */
837                 
838                 /* old optimize trick... this enforces to bypass the depgraph 
839                  *      - note: code copied from transform_generics.c -> recalcData()
840                  */
841                 // FIXME: shouldn't this use the builtin stuff?
842                 if ((pld->arm->flag & ARM_DELAYDEFORM)==0)
843                         DAG_object_flush_update(pld->scene, pld->ob, OB_RECALC_DATA);  /* sets recalc flags */
844                 else
845                         where_is_pose(pld->scene, pld->ob);
846         }
847         
848         /* do header print - if interactively previewing */
849         if (pld->state == PL_PREVIEW_RUNNING) {
850                 if (pld->flag & PL_PREVIEW_SHOWORIGINAL) {
851                         sprintf(pld->headerstr, "PoseLib Previewing Pose: [Showing Original Pose] | Use Tab to start previewing poses again");
852                         ED_area_headerprint(pld->sa, pld->headerstr);
853                 }
854                 else if (pld->searchstr[0]) {
855                         char tempstr[65];
856                         char markern[64];
857                         short index;
858                         
859                         /* get search-string */
860                         index= pld->search_cursor;
861                         
862                         if (IN_RANGE(index, 0, 64)) {
863                                 memcpy(&tempstr[0], &pld->searchstr[0], index);
864                                 tempstr[index]= '|';
865                                 memcpy(&tempstr[index+1], &pld->searchstr[index], 64-index);
866                         }
867                         else {
868                                 strncpy(tempstr, pld->searchstr, 64);
869                         }
870                         
871                         /* get marker name */
872                         if (pld->marker)
873                                 strcpy(markern, pld->marker->name);
874                         else
875                                 strcpy(markern, "No Matches");
876                         
877                         sprintf(pld->headerstr, "PoseLib Previewing Pose: Filter - [%s] | Current Pose - \"%s\"  | Use ScrollWheel or PageUp/Down to change", tempstr, markern);
878                         ED_area_headerprint(pld->sa, pld->headerstr);
879                 }
880                 else {
881                         sprintf(pld->headerstr, "PoseLib Previewing Pose: \"%s\"  | Use ScrollWheel or PageUp/Down to change", pld->marker->name);
882                         ED_area_headerprint(pld->sa, pld->headerstr);
883                 }
884         }
885         
886         /* request drawing of view + clear redraw flag */
887         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM|ND_POSE, pld->ob);
888         pld->redraw= PL_PREVIEW_NOREDRAW;
889 }
890
891 /* ---------------------------- */
892
893 /* This helper function is called during poselib_preview_poses to find the 
894  * pose to preview next (after a change event)
895  */
896 static void poselib_preview_get_next (tPoseLib_PreviewData *pld, int step)
897 {
898         /* check if we no longer have search-string, but don't have any marker */
899         if (pld->marker == NULL) {
900                 if ((step) && (pld->searchstr[0] == 0))
901                         pld->marker= pld->act->markers.first;
902         }       
903         
904         /* the following operations assume that there is a starting point and direction */
905         if ((pld->marker) && (step)) {
906                 /* search-string dictates a special approach */
907                 if (pld->searchstr[0]) {
908                         TimeMarker *marker;
909                         LinkData *ld, *ldn, *ldc;
910                         
911                         /* free and rebuild if needed (i.e. if search-str changed) */
912                         if (strcmp(pld->searchstr, pld->searchold)) {
913                                 /* free list of temporary search matches */
914                                 BLI_freelistN(&pld->searchp);
915                                 
916                                 /* generate a new list of search matches */
917                                 for (marker= pld->act->markers.first; marker; marker= marker->next) {
918                                         /* does the name partially match? 
919                                          *      - don't worry about case, to make it easier for users to quickly input a name (or 
920                                          *        part of one), which is the whole point of this feature
921                                          */
922                                         if (BLI_strcasestr(marker->name, pld->searchstr)) {
923                                                 /* make link-data to store reference to it */
924                                                 ld= MEM_callocN(sizeof(LinkData), "PoseMatch");
925                                                 ld->data= marker;
926                                                 BLI_addtail(&pld->searchp, ld);
927                                         }
928                                 }
929                                 
930                                 /* set current marker to NULL (so that we start from first) */
931                                 pld->marker= NULL;
932                         }
933                         
934                         /* check if any matches */
935                         if (pld->searchp.first == NULL) { 
936                                 pld->marker= NULL;
937                                 return;
938                         }
939                         
940                         /* find first match */
941                         for (ldc= pld->searchp.first; ldc; ldc= ldc->next) {
942                                 if (ldc->data == pld->marker)
943                                         break;
944                         }
945                         if (ldc == NULL)
946                                 ldc= pld->searchp.first;
947                                 
948                         /* Loop through the matches in a cyclic fashion, incrementing/decrementing step as appropriate 
949                          * until step == 0. At this point, marker should be the correct marker.
950                          */
951                         if (step > 0) {
952                                 for (ld=ldc; ld && step; ld=ldn, step--)
953                                         ldn= (ld->next) ? ld->next : pld->searchp.first;
954                         }
955                         else {
956                                 for (ld=ldc; ld && step; ld=ldn, step++)
957                                         ldn= (ld->prev) ? ld->prev : pld->searchp.last;
958                         }
959                         
960                         /* set marker */
961                         if (ld)
962                                 pld->marker= ld->data;
963                 }
964                 else {
965                         TimeMarker *marker, *next;
966                         
967                         /* Loop through the markers in a cyclic fashion, incrementing/decrementing step as appropriate 
968                          * until step == 0. At this point, marker should be the correct marker.
969                          */
970                         if (step > 0) {
971                                 for (marker=pld->marker; marker && step; marker=next, step--)
972                                         next= (marker->next) ? marker->next : pld->act->markers.first;
973                         }
974                         else {
975                                 for (marker=pld->marker; marker && step; marker=next, step++)
976                                         next= (marker->prev) ? marker->prev : pld->act->markers.last;
977                         }
978                         
979                         /* it should be fairly impossible for marker to be NULL */
980                         if (marker)
981                                 pld->marker= marker;
982                 }
983         }
984 }
985
986 /* specially handle events for searching */
987 static void poselib_preview_handle_search (tPoseLib_PreviewData *pld, unsigned short event, char ascii)
988 {
989         /* try doing some form of string manipulation first */
990         switch (event) {
991                 case BACKSPACEKEY:
992                         if (pld->searchstr[0] && pld->search_cursor) {
993                                 short len= strlen(pld->searchstr);
994                                 short index= pld->search_cursor;
995                                 short i;
996                                 
997                                 for (i = index; i <= len; i++) 
998                                         pld->searchstr[i-1] = pld->searchstr[i];
999                                 
1000                                 pld->search_cursor--;
1001                                 
1002                                 poselib_preview_get_next(pld, 1);
1003                                 pld->redraw = PL_PREVIEW_REDRAWALL;
1004                                 return;
1005                         }       
1006                         break;
1007                         
1008                 case DELKEY:
1009                         if (pld->searchstr[0] && pld->searchstr[1]) {
1010                                 short len= strlen(pld->searchstr);
1011                                 short index= pld->search_cursor;
1012                                 int i;
1013                                 
1014                                 if (index < len) {
1015                                         for (i = index; i < len; i++) 
1016                                                 pld->searchstr[i] = pld->searchstr[i+1];
1017                                                 
1018                                         poselib_preview_get_next(pld, 1);
1019                                         pld->redraw = PL_PREVIEW_REDRAWALL;
1020                                         return;
1021                                 }
1022                         }
1023                         break;
1024         }
1025         
1026         if (ascii) {
1027                 /* character to add to the string */
1028                 short index= pld->search_cursor;
1029                 short len= (pld->searchstr[0]) ? strlen(pld->searchstr) : 0;
1030                 short i;
1031                 
1032                 if (len) {
1033                         for (i = len; i > index; i--)  
1034                                 pld->searchstr[i]= pld->searchstr[i-1];
1035                 }
1036                 else
1037                         pld->searchstr[1]= 0;
1038                         
1039                 pld->searchstr[index]= ascii;
1040                 pld->search_cursor++;
1041                 
1042                 poselib_preview_get_next(pld, 1);
1043                 pld->redraw = PL_PREVIEW_REDRAWALL;
1044         }
1045 }
1046
1047 /* handle events for poselib_preview_poses */
1048 static int poselib_preview_handle_event (bContext *C, wmOperator *op, wmEvent *event)
1049 {
1050         tPoseLib_PreviewData *pld= op->customdata; 
1051         int ret = OPERATOR_RUNNING_MODAL;
1052         
1053         /* backup stuff that needs to occur before every operation
1054          *      - make a copy of searchstr, so that we know if cache needs to be rebuilt
1055          */
1056         strcpy(pld->searchold, pld->searchstr);
1057         
1058         /* if we're currently showing the original pose, only certain events are handled */
1059         if (pld->flag & PL_PREVIEW_SHOWORIGINAL) {
1060                 switch (event->type) {
1061                         /* exit - cancel */
1062                         case ESCKEY:
1063                         case RIGHTMOUSE:
1064                                 pld->state= PL_PREVIEW_CANCEL;
1065                                 break;
1066                                 
1067                         /* exit - confirm */
1068                         case LEFTMOUSE:
1069                         case RETKEY:
1070                         case PADENTER:
1071                         case SPACEKEY:
1072                                 pld->state= PL_PREVIEW_CONFIRM;
1073                                 break;
1074                         
1075                         /* view manipulation */
1076                         /* we add pass through here, so that the operators responsible for these can still run, 
1077                          * even though we still maintain control (as RUNNING_MODAL flag is still set too)
1078                          */
1079                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1080                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1081                         case PADPLUSKEY: case PADMINUS: case MIDDLEMOUSE:
1082                                 //pld->redraw= PL_PREVIEW_REDRAWHEADER;
1083                                 ret |= OPERATOR_PASS_THROUGH;
1084                                 break;
1085                                 
1086                         /* quicky compare to original */
1087                         case TABKEY:
1088                                 /* only respond to one event */
1089                                 if (event->val == 0) {
1090                                         pld->flag &= ~PL_PREVIEW_SHOWORIGINAL;
1091                                         pld->redraw= PL_PREVIEW_REDRAWALL;
1092                                 }
1093                                 break;
1094                 }
1095                 
1096                 /* EXITS HERE... */
1097                 return ret;
1098         }
1099         
1100         /* NORMAL EVENT HANDLING... */
1101         /* searching takes priority over normal activity */
1102         switch (event->type) {
1103                 /* exit - cancel */
1104                 case ESCKEY:
1105                 case RIGHTMOUSE:
1106                         pld->state= PL_PREVIEW_CANCEL;
1107                         break;
1108                         
1109                 /* exit - confirm */
1110                 case LEFTMOUSE:
1111                 case RETKEY:
1112                 case PADENTER:
1113                 case SPACEKEY:
1114                         pld->state= PL_PREVIEW_CONFIRM;
1115                         break;
1116                         
1117                 /* toggle between original pose and poselib pose*/
1118                 case TABKEY:
1119                         /* only respond to one event */
1120                         if (event->val == 0) {
1121                                 pld->flag |= PL_PREVIEW_SHOWORIGINAL;
1122                                 pld->redraw= PL_PREVIEW_REDRAWALL;
1123                         }
1124                         break;
1125                 
1126                 /* change to previous pose (cyclic) */
1127                 case PAGEUPKEY:
1128                 case WHEELUPMOUSE:
1129                         poselib_preview_get_next(pld, -1);
1130                         pld->redraw= PL_PREVIEW_REDRAWALL;
1131                         break;
1132                 
1133                 /* change to next pose (cyclic) */
1134                 case PAGEDOWNKEY:
1135                 case WHEELDOWNMOUSE:
1136                         poselib_preview_get_next(pld, 1);
1137                         pld->redraw= PL_PREVIEW_REDRAWALL;
1138                         break;
1139                 
1140                 /* jump 5 poses (cyclic, back) */
1141                 case DOWNARROWKEY:
1142                         poselib_preview_get_next(pld, -5);
1143                         pld->redraw= PL_PREVIEW_REDRAWALL;
1144                         break;
1145                 
1146                 /* jump 5 poses (cyclic, forward) */
1147                 case UPARROWKEY:
1148                         poselib_preview_get_next(pld, 5);
1149                         pld->redraw= PL_PREVIEW_REDRAWALL;
1150                         break;
1151                 
1152                 /* change to next pose or searching cursor control */
1153                 case RIGHTARROWKEY:
1154                         if (pld->searchstr[0]) {
1155                                 /* move text-cursor to the right */
1156                                 if (pld->search_cursor < strlen(pld->searchstr))
1157                                         pld->search_cursor++;
1158                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
1159                         }
1160                         else {
1161                                 /* change to next pose (cyclic) */
1162                                 poselib_preview_get_next(pld, 1);
1163                                 pld->redraw= PL_PREVIEW_REDRAWALL;
1164                         }
1165                         break;
1166                         
1167                 /* change to next pose or searching cursor control */
1168                 case LEFTARROWKEY:
1169                         if (pld->searchstr[0]) {
1170                                 /* move text-cursor to the left */
1171                                 if (pld->search_cursor)
1172                                         pld->search_cursor--;
1173                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
1174                         }
1175                         else {
1176                                 /* change to previous pose (cyclic) */
1177                                 poselib_preview_get_next(pld, -1);
1178                                 pld->redraw= PL_PREVIEW_REDRAWALL;
1179                         }
1180                         break;
1181                         
1182                 /* change to first pose or start of searching string */
1183                 case HOMEKEY:
1184                         if (pld->searchstr[0]) {
1185                                 pld->search_cursor= 0;
1186                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
1187                         }
1188                         else {
1189                                 /* change to first pose */
1190                                 pld->marker= pld->act->markers.first;
1191                                 pld->act->active_marker= 1;
1192                                 
1193                                 pld->redraw= PL_PREVIEW_REDRAWALL;
1194                         }
1195                         break;
1196                         
1197                 /* change to last pose or start of searching string */
1198                 case ENDKEY:
1199                         if (pld->searchstr[0]) {
1200                                 pld->search_cursor= strlen(pld->searchstr);
1201                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
1202                         }
1203                         else {
1204                                 /* change to last pose */
1205                                 pld->marker= pld->act->markers.last;
1206                                 pld->act->active_marker= BLI_countlist(&pld->act->markers);
1207                                 
1208                                 pld->redraw= PL_PREVIEW_REDRAWALL;
1209                         }
1210                         break;
1211                 
1212                 /* view manipulation */
1213                 /* we add pass through here, so that the operators responsible for these can still run, 
1214                  * even though we still maintain control (as RUNNING_MODAL flag is still set too)
1215                  */
1216                 case MIDDLEMOUSE:
1217                         //pld->redraw= PL_PREVIEW_REDRAWHEADER;
1218                         ret |= OPERATOR_PASS_THROUGH;
1219                         break;
1220                         
1221                 /* view manipulation, or searching */
1222                 case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1223                 case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1224                 case PADPLUSKEY: case PADMINUS:
1225                         if (pld->searchstr[0]) {
1226                                 /* searching... */
1227                                 poselib_preview_handle_search(pld, event->type, event->ascii);
1228                         }
1229                         else {
1230                                 /* view manipulation (see above) */
1231                                 //pld->redraw= PL_PREVIEW_REDRAWHEADER;
1232                                 ret |= OPERATOR_PASS_THROUGH;
1233                         }
1234                         break;
1235                         
1236                 /* otherwise, assume that searching might be able to handle it */
1237                 default:
1238                         poselib_preview_handle_search(pld, event->type, event->ascii);
1239                         break;
1240         }
1241         
1242         return ret;
1243 }
1244
1245 /* ---------------------------- */
1246
1247 /* Init PoseLib Previewing data */
1248 static void poselib_preview_init_data (bContext *C, wmOperator *op)
1249 {
1250         tPoseLib_PreviewData *pld;
1251         Object *ob= CTX_data_active_object(C);
1252         int pose_index = RNA_int_get(op->ptr, "pose_index");
1253         
1254         /* set up preview state info */
1255         op->customdata= pld= MEM_callocN(sizeof(tPoseLib_PreviewData), "PoseLib Preview Data");
1256         
1257         /* get basic data */
1258         pld->ob= ob;
1259         pld->arm= (ob) ? (ob->data) : NULL;
1260         pld->pose= (ob) ? (ob->pose) : NULL;
1261         pld->act= (ob) ? (ob->poselib) : NULL;
1262         
1263         pld->scene= CTX_data_scene(C);
1264         pld->sa= CTX_wm_area(C);
1265         
1266         /* get starting pose based on RNA-props for this operator */
1267         if (pose_index == -1)
1268                 pld->marker= poselib_get_active_pose(pld->act);
1269         else if (pose_index == -2)
1270                 pld->flag |= PL_PREVIEW_SHOWORIGINAL;
1271         else
1272                 pld->marker= (pld->act) ? BLI_findlink(&pld->act->markers, pose_index) : NULL;
1273         
1274         /* check if valid poselib */
1275         if (ELEM3(NULL, pld->ob, pld->pose, pld->arm)) {
1276                 BKE_report(op->reports, RPT_ERROR, "PoseLib is only for Armatures in PoseMode");
1277                 pld->state= PL_PREVIEW_ERROR;
1278                 return;
1279         }
1280         if (pld->act == NULL) {
1281                 BKE_report(op->reports, RPT_ERROR, "Object doesn't have a valid PoseLib");
1282                 pld->state= PL_PREVIEW_ERROR;
1283                 return;
1284         }
1285         if (pld->marker == NULL) {
1286                 if (pld->act->markers.first) {
1287                         /* just use first one then... */
1288                         pld->marker= pld->act->markers.first;
1289                         if (pose_index > -2) printf("PoseLib had no active pose\n");
1290                 }
1291                 else {
1292                         BKE_report(op->reports, RPT_ERROR, "PoseLib has no poses to preview/apply");
1293                         pld->state= PL_PREVIEW_ERROR;
1294                         return;
1295                 }
1296         }
1297         
1298         /* get ID pointer for applying poses */
1299         RNA_id_pointer_create(&ob->id, &pld->rna_ptr);
1300         
1301         /* make backups for restoring pose */
1302         poselib_backup_posecopy(pld);
1303         
1304         /* set flags for running */
1305         pld->state= PL_PREVIEW_RUNNING;
1306         pld->redraw= PL_PREVIEW_REDRAWALL;
1307         pld->flag |= PL_PREVIEW_FIRSTTIME;
1308         
1309         /* set depsgraph flags */
1310                 /* make sure the lock is set OK, unlock can be accidentally saved? */
1311         pld->pose->flag |= POSE_LOCKED;
1312         pld->pose->flag &= ~POSE_DO_UNLOCK;
1313         
1314         /* clear strings + search */
1315         strcpy(pld->headerstr, "");
1316         strcpy(pld->searchstr, "");
1317         strcpy(pld->searchold, "");
1318         pld->search_cursor= 0;
1319 }
1320
1321 /* After previewing poses */
1322 static void poselib_preview_cleanup (bContext *C, wmOperator *op)
1323 {
1324         tPoseLib_PreviewData *pld= (tPoseLib_PreviewData *)op->customdata;
1325         Scene *scene= pld->scene;
1326         Object *ob= pld->ob;
1327         bPose *pose= pld->pose;
1328         bArmature *arm= pld->arm;
1329         bAction *act= pld->act;
1330         TimeMarker *marker= pld->marker;
1331         
1332         /* redraw the header so that it doesn't show any of our stuff anymore */
1333         ED_area_headerprint(pld->sa, NULL);
1334         
1335         /* this signal does one recalc on pose, then unlocks, so ESC or edit will work */
1336         pose->flag |= POSE_DO_UNLOCK;
1337         
1338         /* clear pose if cancelled */
1339         if (pld->state == PL_PREVIEW_CANCEL) {
1340                 poselib_backup_restore(pld);
1341                 
1342                 /* old optimize trick... this enforces to bypass the depgraph 
1343                  *      - note: code copied from transform_generics.c -> recalcData()
1344                  */
1345                 if ((arm->flag & ARM_DELAYDEFORM)==0)
1346                         DAG_object_flush_update(scene, ob, OB_RECALC_DATA);  /* sets recalc flags */
1347                 else
1348                         where_is_pose(scene, ob);
1349                 
1350         }
1351         else if (pld->state == PL_PREVIEW_CONFIRM) {
1352                 /* tag poses as appropriate */
1353                 poselib_keytag_pose(scene, pld);
1354                 
1355                 /* change active pose setting */
1356                 act->active_marker= BLI_findindex(&act->markers, marker) + 1;
1357                 action_set_activemarker(act, marker, 0);
1358                 
1359                 /* Update event for pose and deformation children */
1360                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1361                 
1362                 /* updates */
1363                 if (IS_AUTOKEY_MODE(scene, NORMAL)) {
1364                         //remake_action_ipos(ob->action);
1365                 }
1366                 else {
1367                         /* need to trick depgraph, action is not allowed to execute on pose */
1368                         where_is_pose(scene, ob);
1369                         ob->recalc= 0;
1370                 }
1371         }
1372         
1373         /* free memory used for backups */
1374         BLI_freelistN(&pld->backups);
1375         BLI_freelistN(&pld->searchp);
1376         
1377         /* free temp data for operator */
1378         MEM_freeN(pld);
1379         op->customdata= NULL;
1380 }
1381
1382 /* End previewing operation */
1383 static int poselib_preview_exit (bContext *C, wmOperator *op)
1384 {
1385         tPoseLib_PreviewData *pld= op->customdata;
1386         
1387         /* finish up */
1388         poselib_preview_cleanup(C, op);
1389         
1390         if (ELEM(pld->state, PL_PREVIEW_CANCEL, PL_PREVIEW_ERROR))
1391                 return OPERATOR_CANCELLED;
1392         else
1393                 return OPERATOR_FINISHED;
1394 }
1395
1396 /* Cancel previewing operation (called when exiting Blender) */
1397 static int poselib_preview_cancel (bContext *C, wmOperator *op)
1398 {
1399         poselib_preview_exit(C, op);
1400         return OPERATOR_CANCELLED;
1401 }
1402
1403 /* main modal status check */
1404 static int poselib_preview_modal (bContext *C, wmOperator *op, wmEvent *event)
1405 {
1406         tPoseLib_PreviewData *pld= op->customdata;
1407         int ret;
1408         
1409         /* 1) check state to see if we're still running */
1410         if (pld->state != PL_PREVIEW_RUNNING)
1411                 return poselib_preview_exit(C, op);
1412         
1413         /* 2) handle events */
1414         ret= poselib_preview_handle_event(C, op, event);
1415         
1416         /* 3) apply changes and redraw, otherwise, confirming goes wrong */
1417         if (pld->redraw)
1418                 poselib_preview_apply(C, op);
1419         
1420         return ret;
1421 }
1422
1423 /* Modal Operator init */
1424 static int poselib_preview_invoke(bContext *C, wmOperator *op, wmEvent *event)
1425 {
1426         tPoseLib_PreviewData *pld;
1427         
1428         /* check if everything is ok, and init settings for modal operator */
1429         poselib_preview_init_data(C, op);
1430         pld= (tPoseLib_PreviewData *)op->customdata;
1431         
1432         if (pld->state == PL_PREVIEW_ERROR) {
1433                 /* an error occurred, so free temp mem used */
1434                 poselib_preview_cleanup(C, op);
1435                 return OPERATOR_CANCELLED;
1436         }
1437         
1438         /* do initial apply to have something to look at */
1439         poselib_preview_apply(C, op);
1440         
1441         /* add temp handler if we're running as a modal operator */
1442         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1443
1444         return OPERATOR_RUNNING_MODAL;
1445 }
1446
1447 /* Repeat operator */
1448 static int poselib_preview_exec (bContext *C, wmOperator *op)
1449 {
1450         tPoseLib_PreviewData *pld;
1451         
1452         /* check if everything is ok, and init settings for modal operator */
1453         poselib_preview_init_data(C, op);
1454         pld= (tPoseLib_PreviewData *)op->customdata;
1455         
1456         if (pld->state == PL_PREVIEW_ERROR) {
1457                 /* an error occurred, so free temp mem used */
1458                 poselib_preview_cleanup(C, op);
1459                 return OPERATOR_CANCELLED;
1460         }
1461         
1462         /* the exec() callback is effectively a 'run-once' scenario, so set the state to that
1463          * so that everything draws correctly
1464          */
1465         pld->state = PL_PREVIEW_RUNONCE;
1466         
1467         /* apply the active pose */
1468         poselib_preview_apply(C, op);
1469         
1470         /* now, set the status to exit */
1471         pld->state = PL_PREVIEW_CONFIRM;
1472         
1473         /* cleanup */
1474         return poselib_preview_exit(C, op);
1475 }
1476
1477 void POSELIB_OT_browse_interactive (wmOperatorType *ot)
1478 {
1479         /* identifiers */
1480         ot->name= "PoseLib Browse Poses";
1481         ot->idname= "POSELIB_OT_browse_interactive";
1482         ot->description= "Interactively browse poses in 3D-View";
1483         
1484         /* api callbacks */
1485         ot->invoke= poselib_preview_invoke;
1486         ot->modal= poselib_preview_modal;
1487         ot->cancel= poselib_preview_cancel;
1488         ot->exec= poselib_preview_exec;
1489         ot->poll= ED_operator_posemode;
1490         
1491         /* flags */
1492         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1493         
1494         /* properties */        
1495         RNA_def_int(ot->srna, "pose_index", -1, -2, INT_MAX, "Pose", "Index of the pose to apply (-2 for no change to pose, -1 for poselib active pose)", 0, INT_MAX);
1496 }