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