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