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