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