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