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