441ba0eab5252a2a43e4a11013ee309858258fde
[blender.git] / source / blender / src / poselib.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2007, Blender Foundation
21  * This is a new part of Blender
22  *
23  * Contributor(s): Joshua Leung
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27  
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <math.h>
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_arithb.h"
40 #include "BLI_blenlib.h"
41 #include "BLI_dynstr.h"
42
43 #include "DNA_listBase.h"
44 #include "DNA_action_types.h"
45 #include "DNA_armature_types.h"
46 #include "DNA_curve_types.h"
47 #include "DNA_ipo_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_object_force.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_userdef_types.h"
52
53 #include "BKE_action.h"
54 #include "BKE_armature.h"
55 #include "BKE_depsgraph.h"
56 #include "BKE_ipo.h"
57 #include "BKE_modifier.h"
58 #include "BKE_object.h"
59
60 #include "BKE_global.h"
61 #include "BKE_utildefines.h"
62
63 //#include "BIF_keyframing.h"
64 #include "BSE_editipo.h"
65
66 #include "BDR_drawaction.h"
67 #include "BSE_time.h"
68
69 #include "BIF_poselib.h"
70 #include "BIF_interface.h"
71 #include "BIF_editaction.h"
72 #include "BIF_space.h"
73 #include "BIF_screen.h"
74 #include "BIF_toets.h"
75 #include "BIF_toolbox.h"
76
77
78 #include "blendef.h"
79
80 #include "PIL_time.h"                   /* sleep                                */
81 #include "mydevice.h"
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 /* gets list of poses in poselib as a string usable for pupmenu() */
103 char *poselib_build_poses_menu (bAction *act, char title[])
104 {
105         DynStr *pupds= BLI_dynstr_new();
106         TimeMarker *marker;
107         char *str;
108         char buf[64];
109         int i;
110         
111         /* add title first */
112         sprintf(buf, "%s%%t|", title);
113         BLI_dynstr_append(pupds, buf);
114         
115         /* loop through markers, adding them */
116         for (marker=act->markers.first, i=1; marker; marker=marker->next, i++) {
117                 BLI_dynstr_append(pupds, marker->name);
118                 
119                 sprintf(buf, "%%x%d", i);
120                 BLI_dynstr_append(pupds, buf);
121                 
122                 if (marker->next)
123                         BLI_dynstr_append(pupds, "|");
124         }
125         
126         /* convert to normal MEM_malloc'd string */
127         str= BLI_dynstr_get_cstring(pupds);
128         BLI_dynstr_free(pupds);
129         
130         return str;
131 }
132
133 /* gets the first available frame in poselib to store a pose on 
134  *      - frames start from 1, and a pose should occur on every frame... 0 is error!
135  */
136 int poselib_get_free_index (bAction *act)
137 {
138         TimeMarker *marker;
139         int low=0, high=0;
140         
141         /* sanity checks */
142         if (ELEM(NULL, act, act->markers.first)) return 1;
143         
144         /* loop over poses finding various values (poses are not stored in chronological order) */
145         for (marker= act->markers.first; marker; marker= marker->next) {
146                 /* only increase low if value is 1 greater than low, to find "gaps" where
147                  * poses were removed from the poselib
148                  */
149                 if (marker->frame == (low + 1)) 
150                         low++;
151                 
152                 /* value replaces high if it is the highest value encountered yet */
153                 if (marker->frame > high) 
154                         high= marker->frame;
155         }
156         
157         /* - if low is not equal to high, then low+1 is a gap 
158          * - if low is equal to high, then high+1 is the next index (add at end) 
159          */
160         if (low < high) 
161                 return (low + 1);
162         else 
163                 return (high + 1);
164 }
165
166 /* returns the active pose for a poselib */
167 TimeMarker *poselib_get_active_pose (bAction *act)
168 {       
169         if ((act) && (act->active_marker))
170                 return BLI_findlink(&act->markers, act->active_marker-1);
171         else
172                 return NULL;
173 }
174
175 /* ************************************************************* */
176
177 /* Initialise a new poselib (whether it is needed or not) */
178 bAction *poselib_init_new (Object *ob)
179 {
180         /* sanity checks - only for armatures */
181         if (ELEM(NULL, ob, ob->pose))
182                 return NULL;
183         
184         /* init object's poselib action (unlink old one if there) */
185         if (ob->poselib)
186                 ob->poselib->id.us--;
187         ob->poselib= add_empty_action("PoseLib");
188         
189         return ob->poselib;
190 }
191
192 /* Initialise a new poselib (checks if that needs to happen) */
193 bAction *poselib_validate (Object *ob)
194 {
195         if (ELEM(NULL, ob, ob->pose))
196                 return NULL;
197         else if (ob->poselib == NULL)
198                 return poselib_init_new(ob);
199         else
200                 return ob->poselib;
201 }
202
203
204 /* This tool automagically generates/validates poselib data so that it corresponds to the data 
205  * in the action. This is for use in making existing actions usable as poselibs.
206  */
207 void poselib_validate_act (bAction *act)
208 {
209         ListBase keys = {NULL, NULL};
210         ActKeyColumn *ak;
211         TimeMarker *marker, *markern;
212         
213         /* validate action and poselib */
214         if (act == NULL)  {
215                 error("No Action to validate");
216                 return;
217         }
218         
219         /* determine which frames have keys */
220         action_to_keylist(act, &keys, NULL);
221         
222         /* for each key, make sure there is a correspnding pose */
223         for (ak= keys.first; ak; ak= ak->next) {
224                 /* check if any pose matches this */
225                 for (marker= act->markers.first; marker; marker= marker->next) {
226                         if (IS_EQ(marker->frame, ak->cfra)) {
227                                 marker->flag = -1;
228                                 break;
229                         }
230                 }
231                 
232                 /* add new if none found */
233                 if (marker == NULL) {
234                         char name[64];
235                         
236                         /* add pose to poselib */
237                         marker= MEM_callocN(sizeof(TimeMarker), "ActionMarker");
238                         
239                         strcpy(name, "Pose");
240                         BLI_strncpy(marker->name, name, sizeof(marker->name));
241                         
242                         marker->frame= (int)ak->cfra;
243                         marker->flag= -1;
244                         
245                         BLI_addtail(&act->markers, marker);
246                 }
247         }
248         
249         /* remove all untagged poses (unused), and remove all tags */
250         for (marker= act->markers.first; marker; marker= markern) {
251                 markern= marker->next;
252                 
253                 if (marker->flag != -1)
254                         BLI_freelinkN(&act->markers, marker);
255                 else
256                         marker->flag = 0;
257         }
258         
259         /* free temp memory */
260         BLI_freelistN(&keys);
261         
262         BIF_undo_push("PoseLib Validate Action");
263 }
264
265 /* ************************************************************* */
266
267 /* This function adds an ipo-curve of the right type where it's needed */
268 static IpoCurve *poselib_verify_icu (Ipo *ipo, int adrcode)
269 {
270         IpoCurve *icu;
271         
272         for (icu= ipo->curve.first; icu; icu= icu->next) {
273                 if (icu->adrcode==adrcode) break;
274         }
275         if (icu==NULL) {
276                 icu= MEM_callocN(sizeof(IpoCurve), "ipocurve");
277                 
278                 icu->flag |= IPO_VISIBLE|IPO_AUTO_HORIZ;
279                 if (ipo->curve.first==NULL) icu->flag |= IPO_ACTIVE;    /* first one added active */
280                 
281                 icu->blocktype= ID_PO;
282                 icu->adrcode= adrcode;
283                 
284                 set_icu_vars(icu);
285                 
286                 BLI_addtail(&ipo->curve, icu);
287         }
288         
289         return icu;
290 }
291
292 /* This tool adds the current pose to the poselib 
293  *      Note: Standard insertkey cannot be used for this due to its limitations
294  */
295 void poselib_add_current_pose (Object *ob, int val)
296 {
297         bArmature *arm= (ob) ? ob->data : NULL;
298         bPose *pose= (ob) ? ob->pose : NULL;
299         bPoseChannel *pchan;
300         TimeMarker *marker;
301         bAction *act;
302         bActionChannel *achan;
303         IpoCurve *icu;
304         int frame;
305         char name[64];
306         
307         /* sanity check */
308         if (ELEM3(NULL, ob, arm, pose)) 
309                 return;
310         
311         /* mode - add new or replace existing */
312         if (val == 0) {
313                 if ((ob->poselib) && (ob->poselib->markers.first)) {
314                         val= pupmenu("PoseLib Add Current Pose%t|Add New%x1|Replace Existing%x2");
315                         if (val <= 0) return;
316                 }
317                 else 
318                         val= 1;
319         }
320         
321         if ((ob->poselib) && (val == 2)) {
322                 char *menustr;
323                 
324                 /* get poselib */
325                 act= ob->poselib;
326                 
327                 /* get the pose to replace */
328                 menustr= poselib_build_poses_menu(act, "Replace PoseLib Pose");
329                 val= pupmenu(menustr);
330                 if (menustr) MEM_freeN(menustr);
331                 
332                 if (val <= 0) return;
333                 marker= BLI_findlink(&act->markers, val-1);
334                 if (marker == NULL) return;
335                 
336                 /* get the frame from the poselib */
337                 frame= marker->frame;
338         }
339         else {
340                 /* get name of pose */
341                 sprintf(name, "Pose");
342                 if (sbutton(name, 0, sizeof(name)-1, "Name: ") == 0)
343                         return;
344                         
345                 /* get/initialise poselib */
346                 act= poselib_validate(ob);
347                 
348                 /* validate name and get frame */
349                 frame= poselib_get_free_index(act);
350                 
351                 /* add pose to poselib - replaces any existing pose there */
352                 for (marker= act->markers.first; marker; marker= marker->next) {
353                         if (marker->frame == frame) {
354                                 BLI_strncpy(marker->name, name, sizeof(marker->name));
355                                 break;
356                         }
357                 }
358                 if (marker == NULL) {
359                         marker= MEM_callocN(sizeof(TimeMarker), "ActionMarker");
360                         
361                         BLI_strncpy(marker->name, name, sizeof(marker->name));
362                         marker->frame= frame;
363                         
364                         BLI_addtail(&act->markers, marker);
365                 }
366         }       
367         
368         /* loop through selected posechannels, keying their pose to the action */
369         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
370                 /* check if available */
371                 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
372                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) {
373                                 /* make action-channel if needed */
374                                 achan= verify_action_channel(act, pchan->name);
375                                 
376                                 /* make ipo if needed... */
377                                 if (achan->ipo == NULL)
378                                         achan->ipo= add_ipo(achan->name, ID_PO);
379                                         
380                                 /* add missing ipo-curves and insert keys */
381                                 #define INSERT_KEY_ICU(adrcode, data) {\
382                                                 icu= poselib_verify_icu(achan->ipo, adrcode); \
383                                                 insert_vert_icu(icu, frame, data, 1); \
384                                         }
385                                         
386                                 INSERT_KEY_ICU(AC_LOC_X, pchan->loc[0])
387                                 INSERT_KEY_ICU(AC_LOC_Y, pchan->loc[1])
388                                 INSERT_KEY_ICU(AC_LOC_Z, pchan->loc[2])
389                                 INSERT_KEY_ICU(AC_SIZE_X, pchan->size[0])
390                                 INSERT_KEY_ICU(AC_SIZE_Y, pchan->size[1])
391                                 INSERT_KEY_ICU(AC_SIZE_Z, pchan->size[2])
392                                 INSERT_KEY_ICU(AC_QUAT_W, pchan->quat[0])
393                                 INSERT_KEY_ICU(AC_QUAT_X, pchan->quat[1])
394                                 INSERT_KEY_ICU(AC_QUAT_Y, pchan->quat[2])
395                                 INSERT_KEY_ICU(AC_QUAT_Z, pchan->quat[3])
396                         }
397                 }
398         }
399         
400         /* store new 'active' pose number */
401         act->active_marker= BLI_countlist(&act->markers);
402         
403         BIF_undo_push("PoseLib Add Pose");
404         allqueue(REDRAWBUTSEDIT, 0);
405 }
406
407
408 /* This tool removes the pose that the user selected from the poselib (or the provided pose) */
409 void poselib_remove_pose (Object *ob, TimeMarker *marker)
410 {
411         bPose *pose= (ob) ? ob->pose : NULL;
412         bAction *act= (ob) ? ob->poselib : NULL;
413         bActionChannel *achan;
414         char *menustr;
415         int val;
416         
417         /* check if valid poselib */
418         if (ELEM(NULL, ob, pose)) {
419                 error("PoseLib is only for Armatures in PoseMode");
420                 return;
421         }
422         if (act == NULL) {
423                 error("Object doesn't have PoseLib data");
424                 return;
425         }
426         
427         /* get index (and pointer) of pose to remove */
428         if (marker == NULL) {
429                 menustr= poselib_build_poses_menu(act, "Remove PoseLib Pose");
430                 val= pupmenu(menustr);
431                 if (menustr) MEM_freeN(menustr);
432                 
433                 if (val <= 0) return;
434                 marker= BLI_findlink(&act->markers, val-1);
435                 if (marker == NULL) return;
436         }
437         else {
438                 /* only continue if pose belongs to poselib */
439                 if (BLI_findindex(&act->markers, marker) == -1) 
440                         return;
441         }
442         
443         /* remove relevant keyframes */
444         for (achan= act->chanbase.first; achan; achan= achan->next) {
445                 Ipo *ipo= achan->ipo;
446                 IpoCurve *icu;
447                 BezTriple *bezt;
448                 int i;
449                 
450                 for (icu= ipo->curve.first; icu; icu= icu->next) {
451                         for (i=0, bezt=icu->bezt; i < icu->totvert; i++, bezt++) {
452                                 /* check if remove... */
453                                 if (IS_EQ(bezt->vec[1][0], marker->frame)) {
454                                         delete_icu_key(icu, i);
455                                         break;
456                                 }
457                         }       
458                 }
459         }
460         
461         /* remove poselib from list */
462         BLI_freelinkN(&act->markers, marker);
463         
464         /* fix active pose number */
465         act->active_marker= 0;
466         
467         /* undo + redraw */
468         BIF_undo_push("PoseLib Remove Pose");
469         allqueue(REDRAWBUTSEDIT, 0);
470         allqueue(REDRAWACTION, 0);
471 }
472
473
474 /* This tool renames the pose that the user selected from the poselib */
475 void poselib_rename_pose (Object *ob)
476 {
477         bPose *pose= (ob) ? ob->pose : NULL;
478         bAction *act= (ob) ? ob->poselib : NULL;
479         TimeMarker *marker;
480         char *menustr, name[64];
481         int val;
482         
483         /* check if valid poselib */
484         if (ELEM(NULL, ob, pose)) {
485                 error("PoseLib is only for Armatures in PoseMode");
486                 return;
487         }
488         if (act == NULL) {
489                 error("Object doesn't have a valid PoseLib");
490                 return;
491         }
492         
493         /* get index of pose to remove */
494         menustr= poselib_build_poses_menu(act, "Rename PoseLib Pose");
495         val= pupmenu(menustr);
496         if (menustr) MEM_freeN(menustr);
497         
498         if (val <= 0) return;
499         marker= BLI_findlink(&act->markers, val-1);
500         if (marker == NULL) return;
501         
502         /* get name of pose */
503         sprintf(name, marker->name);
504         if (sbutton(name, 0, sizeof(name)-1, "Name: ") == 0)
505                 return;
506         
507         /* copy name */
508         BLI_strncpy(marker->name, name, sizeof(marker->name));
509         
510         /* undo and update */
511         BIF_undo_push("PoseLib Rename Pose");
512         allqueue(REDRAWBUTSEDIT, 0);
513         allqueue(REDRAWACTION, 0);
514 }
515
516
517 /* ************************************************************* */
518
519 /* Simple struct for storing settings/data for use during PoseLib preview */
520 typedef struct tPoseLib_PreviewData {
521         ListBase backups;               /* tPoseLib_Backup structs for restoring poses */
522         ListBase searchp;               /* LinkData structs storing list of poses which match the current search-string */
523         
524         Object *ob;                             /* object to work on */
525         bArmature *arm;                 /* object's armature data */
526         bPose *pose;                    /* object's pose */
527         bAction *act;                   /* poselib to use */
528         TimeMarker *marker;             /* 'active' pose */
529         
530         short state;                    /* state of main loop */
531         short redraw;                   /* redraw/update settings during main loop */
532         short firsttime;                /* first loop... (no restore) */
533         
534         int selcount;                   /* number of selected elements to work on */
535         int totcount;                   /* total number of elements to work on */
536         
537         char headerstr[200];    /* Info-text to print in header */
538         
539         char searchstr[64];             /* (Part of) Name to search for to filter poses that get shown */
540         char searchold[64];             /* Previously set searchstr (from last loop run), so that we can detected when to rebuild searchp */
541         short search_cursor;    /* position of cursor in searchstr (cursor occurs before the item at the nominated index) */
542 } tPoseLib_PreviewData;
543
544 /* defines for tPoseLib_PreviewData->state values */
545 enum {
546         PL_PREVIEW_ERROR = -1,
547         PL_PREVIEW_RUNNING,
548         PL_PREVIEW_CONFIRM,
549         PL_PREVIEW_CANCEL,
550         PL_PREVIEW_RUNONCE 
551 };
552
553 /* defines for tPoseLib_PreviewData->redraw values */
554 enum {
555         PL_PREVIEW_NOREDRAW = 0,
556         PL_PREVIEW_REDRAWALL,
557         PL_PREVIEW_REDRAWHEADER,
558 };
559
560 /* ---------------------------- */
561
562 /* simple struct for storing backup info */
563 typedef struct tPoseLib_Backup {
564         struct tPoseLib_Backup *next, *prev;
565         
566         bPoseChannel *pchan;
567         bPoseChannel olddata;
568 } tPoseLib_Backup;
569
570 /* Makes a copy of the current pose for restoration purposes - doesn't do constraints currently */
571 static void poselib_backup_posecopy (tPoseLib_PreviewData *pld)
572 {
573         bActionChannel *achan;
574         bPoseChannel *pchan;
575         
576         /* for each posechannel that has an actionchannel in */
577         for (achan= pld->act->chanbase.first; achan; achan= achan->next) {
578                 /* try to find posechannel */
579                 pchan= get_pose_channel(pld->pose, achan->name);
580                 
581                 /* backup data if available */
582                 if (pchan) {
583                         tPoseLib_Backup *plb;
584                         
585                         /* store backup */
586                         plb= MEM_callocN(sizeof(tPoseLib_Backup), "tPoseLib_Backup");
587                         
588                         plb->pchan= pchan;
589                         memcpy(&plb->olddata, plb->pchan, sizeof(bPoseChannel));
590                         
591                         BLI_addtail(&pld->backups, plb);
592                         
593                         /* mark as being affected */
594                         if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED))
595                                 pld->selcount++;
596                         pld->totcount++;
597                 }
598         }
599 }
600
601 /* Restores original pose - doesn't do constraints currently */
602 static void poselib_backup_restore (tPoseLib_PreviewData *pld)
603 {
604         tPoseLib_Backup *plb;
605         
606         for (plb= pld->backups.first; plb; plb= plb->next) {
607                 memcpy(plb->pchan, &plb->olddata, sizeof(bPoseChannel));
608         }
609 }
610
611 /* ---------------------------- */
612
613 /* Applies the appropriate stored pose from the pose-library to the current pose
614  *      - assumes that a valid object, with a poselib has been supplied
615  *      - gets the string to print in the header
616  *      - this code is based on the code for extract_pose_from_action in blenkernel/action.c
617  */
618 static void poselib_apply_pose (tPoseLib_PreviewData *pld)
619 {
620         bPose *pose= pld->pose;
621         bPoseChannel *pchan;
622         bAction *act= pld->act;
623         bActionChannel *achan;
624         IpoCurve *icu;
625         int frame;
626         
627         if (pld->marker)
628                 frame= pld->marker->frame;
629         else
630                 return; 
631         
632         /* start applying - only those channels which have a key at this point in time! */
633         for (achan= act->chanbase.first; achan; achan= achan->next) {
634                 short found= 0;
635                 
636                 /* apply this achan? */
637                 if (achan->ipo) {
638                         /* find a keyframe at this frame - users may not have defined the pose on every channel, so this is necessary */
639                         for (icu= achan->ipo->curve.first; icu; icu= icu->next) {
640                                 BezTriple *bezt;
641                                 int i;
642                                 
643                                 for (i=0, bezt=icu->bezt; i < icu->totvert; i++, bezt++) {
644                                         if (IN_RANGE(bezt->vec[1][0], (frame-0.5f), (frame+0.5f))) {
645                                                 found= 1;
646                                                 break;
647                                         }
648                                 }
649                                 
650                                 if (found) break;
651                         }
652                         
653                         /* apply pose - only if posechannel selected? */
654                         if (found) {
655                                 pchan= get_pose_channel(pose, achan->name);
656                                 
657                                 if (pchan) {    
658                                         short ok;
659                                         
660                                         ok= (pchan->bone) ? (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) : 0;
661                                         ok= (ok || pld->selcount) ? 1 : 0; 
662                                         
663                                         if (ok) {
664                                                 /* Evaluates and sets the internal ipo values   */
665                                                 calc_ipo(achan->ipo, frame);
666                                                 /* This call also sets the pchan flags */
667                                                 execute_action_ipo(achan, pchan);
668                                         }
669                                 }
670                         }
671                 }
672                 
673                 /* tag achan as having been used or not... */
674                 if (found)
675                         achan->flag |= ACHAN_SELECTED;
676                 else
677                         achan->flag &= ~ACHAN_SELECTED;
678         }
679 }
680
681 /* Auto-keys/tags bones affected by the pose used from the poselib */
682 static void poselib_keytag_pose (tPoseLib_PreviewData *pld)
683 {
684         bPose *pose= pld->pose;
685         bPoseChannel *pchan;
686         bAction *act= pld->act;
687         bActionChannel *achan;
688         
689         /* start tagging/keying */
690         for (achan= act->chanbase.first; achan; achan= achan->next) {
691                 /* only for selected action channels */
692                 if (achan->flag & ACHAN_SELECTED) {
693                         pchan= get_pose_channel(pose, achan->name);
694                         
695                         if (pchan) {
696                                 // TODO: use a standard autokeying function in future (to allow autokeying-editkeys to work)
697                                 if (IS_AUTOKEY_MODE(NORMAL)) {
698                                         ID *id= &pld->ob->id;
699                                         
700                                         /* Set keys on pose */
701                                         if (pchan->flag & POSE_ROT) {
702                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X, 0);
703                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y, 0);
704                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z, 0);
705                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W, 0);
706                                         }
707                                         if (pchan->flag & POSE_SIZE) {
708                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X, 0);
709                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y, 0);
710                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z, 0);
711                                         }
712                                         if (pchan->flag & POSE_LOC) {
713                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X, 0);
714                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y, 0);
715                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z, 0);
716                                         }
717                                         
718                                         /* clear any unkeyed tags */
719                                         if (pchan->bone)
720                                                 pchan->bone->flag &= ~BONE_UNKEYED;
721                                 }
722                                 else {
723                                         /* add unkeyed tags */
724                                         if (pchan->bone)
725                                                 pchan->bone->flag |= BONE_UNKEYED;
726                                 }
727                         }
728                 }
729         }
730 }
731
732 /* ---------------------------- */
733
734 /* This helper function is called during poselib_preview_poses to find the 
735  * pose to preview next (after a change event)
736  */
737 static void poselib_preview_get_next (tPoseLib_PreviewData *pld, int step)
738 {
739         if ((pld->marker) && (step)) {
740                 /* search-string dictates a special approach */
741                 if (pld->searchstr[0]) {
742                         TimeMarker *marker;
743                         LinkData *ld, *ldn, *ldc;
744                         
745                         /* free and rebuild if needed (i.e. if search-str changed) */
746                         if (strcmp(pld->searchstr, pld->searchold)) {
747                                 /* free list of temporary search matches */
748                                 BLI_freelistN(&pld->searchp);
749                                 
750                                 /* generate a new list of search matches */
751                                 for (marker= pld->act->markers.first; marker; marker= marker->next) {
752                                         /* does the name partially match? 
753                                          *      - don't worry about case, to make it easier for users to quickly input a name (or 
754                                          *        part of one), which is the whole point of this feature
755                                          */
756                                         if (BLI_strcasestr(marker->name, pld->searchstr)) {
757                                                 /* make link-data to store reference to it */
758                                                 ld= MEM_callocN(sizeof(LinkData), "PoseMatch");
759                                                 ld->data= marker;
760                                                 BLI_addtail(&pld->searchp, ld);
761                                         }
762                                 }
763                                 
764                                 /* set current marker to NULL (so that we start from first) */
765                                 pld->marker= NULL;
766                         }
767                         
768                         /* check if any matches */
769                         if (pld->searchp.first == NULL) { 
770                                 pld->marker= NULL;
771                                 return;
772                         }
773                         
774                         /* find first match */
775                         for (ldc= pld->searchp.first; ldc; ldc= ldc->next) {
776                                 if (ldc->data == pld->marker)
777                                         break;
778                         }
779                         if (ldc == NULL)
780                                 ldc= pld->searchp.first;
781                                 
782                         /* Loop through the matches in a cyclic fashion, incrementing/decrementing step as appropriate 
783                          * until step == 0. At this point, marker should be the correct marker.
784                          */
785                         if (step > 0) {
786                                 for (ld=ldc; ld && step; ld=ldn, step--)
787                                         ldn= (ld->next) ? ld->next : pld->searchp.first;
788                         }
789                         else {
790                                 for (ld=ldc; ld && step; ld=ldn, step++)
791                                         ldn= (ld->prev) ? ld->prev : pld->searchp.last;
792                         }
793                         
794                         /* set marker */
795                         if (ld)
796                                 pld->marker= ld->data;
797                 }
798                 else {
799                         TimeMarker *marker, *next;
800                         
801                         /* Loop through the markers in a cyclic fashion, incrementing/decrementing step as appropriate 
802                          * until step == 0. At this point, marker should be the correct marker.
803                          */
804                         if (step > 0) {
805                                 for (marker=pld->marker; marker && step; marker=next, step--)
806                                         next= (marker->next) ? marker->next : pld->act->markers.first;
807                         }
808                         else {
809                                 for (marker=pld->marker; marker && step; marker=next, step++)
810                                         next= (marker->prev) ? marker->prev : pld->act->markers.last;
811                         }
812                         
813                         /* it should be fairly impossible for marker to be NULL */
814                         if (marker)
815                                 pld->marker= marker;
816                 }
817         }
818 }
819
820 /* specially handle events for searching */
821 static void poselib_preview_handle_search (tPoseLib_PreviewData *pld, unsigned short event, char ascii)
822 {
823         if (ascii) {
824                 /* character to add to the string */
825                 short index= pld->search_cursor;
826                 short len= (pld->searchstr[0]) ? strlen(pld->searchstr) : 0;
827                 short i;
828                 
829                 if (len) {
830                         for (i = len; i > index; i--)  
831                                 pld->searchstr[i]= pld->searchstr[i-1];
832                 }
833                 else
834                         pld->searchstr[1]= 0;
835                         
836                 pld->searchstr[index]= ascii;
837                 pld->search_cursor++;
838                 
839                 poselib_preview_get_next(pld, 1);
840                 pld->redraw = PL_PREVIEW_REDRAWALL;
841         }
842         else {
843                 /* some form of string manipulation */
844                 switch (event) {
845                         case BACKSPACEKEY:
846                                 if (pld->searchstr[0] && pld->search_cursor) {
847                                         short len= strlen(pld->searchstr);
848                                         short index= pld->search_cursor;
849                                         short i;
850                                         
851                                         for (i = index; i <= len; i++) 
852                                                 pld->searchstr[i-1] = pld->searchstr[i];
853                                         
854                                         pld->search_cursor--;
855                                         
856                                         poselib_preview_get_next(pld, 1);
857                                         pld->redraw = PL_PREVIEW_REDRAWALL;
858                                 }       
859                                 break;
860                                 
861                         case DELKEY:
862                                 if (pld->searchstr[0] && pld->searchstr[1]) {
863                                         short len= strlen(pld->searchstr);
864                                         short index= pld->search_cursor;
865                                         int i;
866                                         
867                                         if (index < len) {
868                                                 for (i = index; i < len; i++) 
869                                                         pld->searchstr[i] = pld->searchstr[i+1];
870                                                         
871                                                 poselib_preview_get_next(pld, 1);
872                                                 pld->redraw = PL_PREVIEW_REDRAWALL;
873                                         }
874                                 }
875                                 break;
876                 }
877         }
878 }
879
880 /* handle events for poselib_preview_poses */
881 static void poselib_preview_handle_event (tPoseLib_PreviewData *pld, unsigned short event, char ascii)
882 {
883         /* backup stuff that needs to occur before every operation
884          *      - make a copy of searchstr, so that we know if cache needs to be rebuilt
885          */
886         strcpy(pld->searchold, pld->searchstr);
887         
888         /* searching takes priority over normal activity */
889         switch (event) {
890                 /* exit - cancel */
891                 case ESCKEY:
892                 case RIGHTMOUSE:
893                         pld->state= PL_PREVIEW_CANCEL;
894                         break;
895                         
896                 /* exit - confirm */
897                 case LEFTMOUSE:
898                 case RETKEY:
899                 case PADENTER:
900                 case SPACEKEY:
901                         pld->state= PL_PREVIEW_CONFIRM;
902                         break;
903                 
904                 /* change to previous pose (cyclic) */
905                 case PAGEUPKEY:
906                 case WHEELUPMOUSE:
907                         poselib_preview_get_next(pld, -1);
908                         pld->redraw= PL_PREVIEW_REDRAWALL;
909                         break;
910                 
911                 /* change to next pose (cyclic) */
912                 case PAGEDOWNKEY:
913                 case WHEELDOWNMOUSE:
914                         poselib_preview_get_next(pld, 1);
915                         pld->redraw= PL_PREVIEW_REDRAWALL;
916                         break;
917                 
918                 /* jump 5 poses (cyclic, back) */
919                 case DOWNARROWKEY:
920                         poselib_preview_get_next(pld, -5);
921                         pld->redraw= PL_PREVIEW_REDRAWALL;
922                         break;
923                 
924                 /* jump 5 poses (cyclic, forward) */
925                 case UPARROWKEY:
926                         poselib_preview_get_next(pld, 5);
927                         pld->redraw= PL_PREVIEW_REDRAWALL;
928                         break;
929                 
930                 /* change to previous pose or searching cursor control */
931                 case RIGHTARROWKEY:
932                         if (pld->searchstr[0]) {
933                                 /* move text-cursor to the right */
934                                 if (pld->search_cursor < strlen(pld->searchstr))
935                                         pld->search_cursor++;
936                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
937                         }
938                         else {
939                                 /* change to previous pose (cyclic) */
940                                 poselib_preview_get_next(pld, -1);
941                                 pld->redraw= PL_PREVIEW_REDRAWALL;
942                         }
943                         break;
944                         
945                 /* change to next pose or searching cursor control */
946                 case LEFTARROWKEY:
947                         if (pld->searchstr[0]) {
948                                 /* move text-cursor to the left */
949                                 if (pld->search_cursor)
950                                         pld->search_cursor--;
951                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
952                         }
953                         else {
954                                 /* change to next pose (cyclic) */
955                                 poselib_preview_get_next(pld, 1);
956                                 pld->redraw= PL_PREVIEW_REDRAWALL;
957                         }
958                         break;
959                         
960                 /* change to first pose or start of searching string */
961                 case HOMEKEY:
962                         if (pld->searchstr[0]) {
963                                 pld->search_cursor= 0;
964                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
965                         }
966                         else {
967                                 /* change to first pose */
968                                 pld->marker= pld->act->markers.first;
969                                 pld->act->active_marker= 1;
970                                 
971                                 pld->redraw= PL_PREVIEW_REDRAWALL;
972                         }
973                         break;
974                         
975                 /* change to last pose or start of searching string */
976                 case ENDKEY:
977                         if (pld->searchstr[0]) {
978                                 pld->search_cursor= strlen(pld->searchstr);
979                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
980                         }
981                         else {
982                                 /* change to last pose */
983                                 pld->marker= pld->act->markers.last;
984                                 pld->act->active_marker= BLI_countlist(&pld->act->markers);
985                                 
986                                 pld->redraw= PL_PREVIEW_REDRAWALL;
987                         }
988                         break;
989                         
990                 /* view manipulation */
991                 case MIDDLEMOUSE:
992                         // there's a little bug here that causes the normal header to get drawn while view is manipulated 
993                         handle_view_middlemouse();
994                         pld->redraw= PL_PREVIEW_REDRAWHEADER;
995                         break;
996                         
997                 /* view manipulation, or searching */
998                 case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
999                 case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1000                 case PADPLUSKEY: case PADMINUS:
1001                         if (pld->searchstr[0]) {
1002                                 poselib_preview_handle_search(pld, event, ascii);
1003                         }
1004                         else {
1005                                 persptoetsen(event);
1006                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
1007                         }
1008                         break;
1009                         
1010                 /* otherwise, assume that searching might be able to handle it */
1011                 default:
1012                         poselib_preview_handle_search(pld, event, ascii);
1013                         break;
1014         }
1015 }
1016
1017 /* ---------------------------- */
1018
1019 /* Init PoseLib Previewing data */
1020 static void poselib_preview_init_data (tPoseLib_PreviewData *pld, Object *ob, short apply_active)
1021 {
1022         /* clear pld first as it resides on the stack */
1023         memset(pld, 0, sizeof(tPoseLib_PreviewData));
1024         
1025         /* get basic data */
1026         pld->ob= ob;
1027         pld->arm= (ob) ? (ob->data) : NULL;
1028         pld->pose= (ob) ? (ob->pose) : NULL;
1029         pld->act= (ob) ? (ob->poselib) : NULL;
1030         pld->marker= poselib_get_active_pose(pld->act);
1031         
1032         /* check if valid poselib */
1033         if (ELEM3(NULL, pld->ob, pld->pose, pld->arm)) {
1034                 error("PoseLib is only for Armatures in PoseMode");
1035                 pld->state= PL_PREVIEW_ERROR;
1036                 return;
1037         }
1038         if (pld->act == NULL) {
1039                 error("Object doesn't have a valid PoseLib");
1040                 pld->state= PL_PREVIEW_ERROR;
1041                 return;
1042         }
1043         if (pld->marker == NULL) {
1044                 if ((apply_active==0) || (pld->act->markers.first)) {
1045                         /* just use first one then... */
1046                         pld->marker= pld->act->markers.first;
1047                         printf("PoseLib had no active pose\n");
1048                 }
1049                 else {
1050                         error("PoseLib has no poses to preview/apply");
1051                         pld->state= PL_PREVIEW_ERROR;
1052                         return;
1053                 }
1054         }
1055         
1056         /* make backups for restoring pose */
1057         poselib_backup_posecopy(pld);
1058         
1059         /* set flags for running */
1060         pld->state= (apply_active) ? PL_PREVIEW_RUNONCE : PL_PREVIEW_RUNNING;
1061         pld->redraw= PL_PREVIEW_REDRAWALL;
1062         pld->firsttime= 1;
1063         
1064         /* set depsgraph flags */
1065                 /* make sure the lock is set OK, unlock can be accidentally saved? */
1066         pld->pose->flag |= POSE_LOCKED;
1067         pld->pose->flag &= ~POSE_DO_UNLOCK;
1068         
1069         /* clear strings + search */
1070         strcpy(pld->headerstr, "");
1071         strcpy(pld->searchstr, "");
1072         strcpy(pld->searchold, "");
1073         pld->search_cursor= 0;
1074 }
1075
1076 /* After previewing poses */
1077 static void poselib_preview_cleanup (tPoseLib_PreviewData *pld)
1078 {
1079         Base *base;
1080         Object *ob= pld->ob;
1081         bPose *pose= pld->pose;
1082         bArmature *arm= pld->arm;
1083         bAction *act= pld->act;
1084         TimeMarker *marker= pld->marker;
1085         
1086         /* this signal does one recalc on pose, then unlocks, so ESC or edit will work */
1087         pose->flag |= POSE_DO_UNLOCK;
1088         
1089         /* clear pose if cancelled */
1090         if (pld->state == PL_PREVIEW_CANCEL) {
1091                 poselib_backup_restore(pld);
1092                 
1093                 /* old optimize trick... this enforces to bypass the depgraph 
1094                  *      - note: code copied from transform_generics.c -> recalcData()
1095                  */
1096                 if ((arm->flag & ARM_DELAYDEFORM)==0) {
1097                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);  /* sets recalc flags */
1098                         
1099                         /* bah, softbody exception... recalcdata doesnt reset */
1100                         for (base= FIRSTBASE; base; base= base->next) {
1101                                 if (base->object->recalc & OB_RECALC_DATA)
1102                                         if (modifiers_isSoftbodyEnabled(base->object)) {
1103                                                 base->object->softflag |= OB_SB_REDO;
1104                                 }
1105                         }
1106                 }
1107                 else
1108                         where_is_pose(ob);
1109                 
1110                 allqueue(REDRAWVIEW3D, 0);
1111                 allqueue(REDRAWBUTSEDIT, 0);
1112         }
1113         else if (pld->state == PL_PREVIEW_CONFIRM) {
1114                 /* tag poses as appropriate */
1115                 poselib_keytag_pose(pld);
1116                 
1117                 /* change active pose setting */
1118                 act->active_marker= BLI_findindex(&act->markers, marker) + 1;
1119                 action_set_activemarker(act, marker, 0);
1120                 
1121                 /* Update event for pose and deformation children */
1122                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1123                 
1124                 /* updates */
1125                 if (IS_AUTOKEY_MODE(NORMAL)) {
1126                         remake_action_ipos(ob->action);
1127                         
1128                         allqueue(REDRAWIPO, 0);
1129                         allqueue(REDRAWVIEW3D, 0);
1130                         allqueue(REDRAWBUTSEDIT, 0);
1131                         allqueue(REDRAWACTION, 0);              
1132                         allqueue(REDRAWNLA, 0);
1133                 }
1134                 else {
1135                         /* need to trick depgraph, action is not allowed to execute on pose */
1136                         where_is_pose(ob);
1137                         ob->recalc= 0;
1138                         
1139                         allqueue(REDRAWVIEW3D, 0);
1140                         allqueue(REDRAWBUTSEDIT, 0);
1141                 }
1142         }
1143         
1144         /* free memory used for backups */
1145         BLI_freelistN(&pld->backups);
1146         BLI_freelistN(&pld->searchp);
1147 }
1148
1149
1150
1151 /* This tool allows users to preview the pose from the pose-lib using the mouse-scrollwheel/pageupdown
1152  * It is also used to apply the active poselib pose only
1153  */
1154 void poselib_preview_poses (Object *ob, short apply_active)
1155 {
1156         tPoseLib_PreviewData pld;
1157         Base *base;
1158         
1159         unsigned short event;
1160         short val=0;
1161         char ascii;
1162         
1163         /* check if valid poselib */
1164         poselib_preview_init_data(&pld, ob, apply_active);
1165         if (pld.state == PL_PREVIEW_ERROR)
1166                 return;
1167                 
1168         /* start preview loop */
1169         while (ELEM(pld.state, PL_PREVIEW_RUNNING, PL_PREVIEW_RUNONCE)) {
1170                 /* preview a pose */
1171                 if (pld.redraw) {
1172                         /* only recalc pose (and its dependencies) if pose has changed */
1173                         if (pld.redraw == PL_PREVIEW_REDRAWALL) {
1174                                 /* don't clear pose if firsttime */
1175                                 if (pld.firsttime == 0)
1176                                         poselib_backup_restore(&pld);
1177                                 else
1178                                         pld.firsttime = 0;
1179                                         
1180                                 /* pose should be the right one to draw */
1181                                 poselib_apply_pose(&pld);
1182                                 
1183                                 /* old optimize trick... this enforces to bypass the depgraph 
1184                                  *      - note: code copied from transform_generics.c -> recalcData()
1185                                  */
1186                                 if ((pld.arm->flag & ARM_DELAYDEFORM)==0) {
1187                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);  /* sets recalc flags */
1188                                         
1189                                         /* bah, softbody exception... recalcdata doesnt reset */
1190                                         for (base= FIRSTBASE; base; base= base->next) {
1191                                                 if (base->object->recalc & OB_RECALC_DATA)
1192                                                         if (modifiers_isSoftbodyEnabled(base->object)) {
1193                                                                 base->object->softflag |= OB_SB_REDO;
1194                                                 }
1195                                         }
1196                                 }
1197                                 else
1198                                         where_is_pose(ob);
1199                         }
1200                         
1201                         /* do header print - if interactively previewing */
1202                         if (pld.state == PL_PREVIEW_RUNNING) {
1203                                 if (pld.searchstr[0]) {
1204                                         char tempstr[65];
1205                                         char markern[64];
1206                                         short index;
1207                                         
1208                                         /* get search-string */
1209                                         index= pld.search_cursor;
1210                                         
1211                                         memcpy(&tempstr[0], &pld.searchstr[0], index);
1212                                         tempstr[index]= '|';
1213                                         memcpy(&tempstr[index+1], &pld.searchstr[index], 64-index);
1214                                         
1215                                         /* get marker name */
1216                                         if (pld.marker)
1217                                                 strcpy(markern, pld.marker->name);
1218                                         else
1219                                                 strcpy(markern, "No Matches");
1220                                         
1221                                         sprintf(pld.headerstr, "PoseLib Previewing Pose: Filter - [%s] | Current Pose - \"%s\"  | Use ScrollWheel or PageUp/Down to change", tempstr, markern);
1222                                         headerprint(pld.headerstr);
1223                                 }
1224                                 else {
1225                                         sprintf(pld.headerstr, "PoseLib Previewing Pose: \"%s\"  | Use ScrollWheel or PageUp/Down to change", pld.marker->name);
1226                                         headerprint(pld.headerstr);
1227                                 }
1228                         }
1229                         
1230                         /* force drawing of view + clear redraw flag */
1231                         force_draw(0);
1232                         pld.redraw= PL_PREVIEW_NOREDRAW;
1233                 }
1234                 
1235                 /* stop now if only running once */
1236                 if (pld.state == PL_PREVIEW_RUNONCE) {
1237                         pld.state = PL_PREVIEW_CONFIRM;
1238                         break;
1239                 }
1240                 
1241                 /* essential for idling subloop */
1242                 if (qtest() == 0) 
1243                         PIL_sleep_ms(2);
1244                 
1245                 /* emptying queue and reading events */
1246                 while ( qtest() ) {
1247                         event= extern_qread_ext(&val, &ascii);
1248                         
1249                         /* event processing */
1250                         if (val) {
1251                                 poselib_preview_handle_event(&pld, event, ascii);
1252                         }
1253                 }
1254         }
1255         
1256         /* finish up */
1257         poselib_preview_cleanup(&pld);
1258         
1259         BIF_undo_push("PoseLib Apply Pose");
1260 }