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