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