Merge from trunk
[blender.git] / source / blender / src / editnla.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL 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) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL *****
28  *
29  * This file is a horrible mess: An attmept to cram some
30  * final functionality into blender before it is too late.
31  *
32  * Hopefully it can be tidied up at a later date...
33 */
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <math.h>
38
39 #include "PIL_time.h"
40
41 #include "MEM_guardedalloc.h"
42
43 #include "BLI_blenlib.h"
44
45 #include "DNA_action_types.h"
46 #include "DNA_constraint_types.h"
47 #include "DNA_curve_types.h"
48 #include "DNA_ipo_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_nla_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_space_types.h"
53 #include "DNA_scene_types.h"
54 #include "DNA_userdef_types.h"
55
56 #include "BKE_action.h"
57 #include "BKE_blender.h"
58 #include "BKE_depsgraph.h"
59 #include "BKE_group.h"
60 #include "BKE_global.h"
61 #include "BKE_ipo.h"
62 #include "BKE_library.h"
63 #include "BKE_main.h"
64 #include "BKE_nla.h"
65 #include "BKE_utildefines.h"
66
67 #include "BIF_screen.h"
68 #include "BIF_interface.h"
69 #include "BIF_butspace.h"
70 #include "BIF_space.h"
71 #include "BIF_mywindow.h"
72 #include "BIF_editview.h"
73 #include "BIF_toolbox.h"
74 #include "BIF_editnla.h"
75 #include "BIF_editaction.h"
76 #include "BIF_transform.h"
77
78 #include "BSE_editipo.h"
79 #include "BSE_editnla_types.h"
80 #include "BSE_headerbuttons.h"
81 #include "BSE_drawipo.h"
82 #include "BSE_editaction_types.h"
83 #include "BSE_trans_types.h"
84 #include "BSE_edit.h"
85 #include "BSE_filesel.h"
86 #include "BDR_editobject.h"
87 #include "BSE_drawnla.h"
88 #include "BSE_time.h"
89
90 #include "blendef.h"
91 #include "mydevice.h"
92
93 #ifdef HAVE_CONFIG_H
94 #include <config.h>
95 #endif
96
97 /* Note: A lot of these pretty much duplicate the behaviour of the
98 action windows.  The functions should be shared, not copy-pasted */
99
100 static void mouse_nla(int selectmode);
101 static Base *get_nearest_nlachannel_ob_key (float *index, short *sel);
102 static bAction *get_nearest_nlachannel_ac_key (float *index, short *sel);
103 static Base *get_nearest_nlastrip (bActionStrip **rstrip, short *sel);
104 static void mouse_nlachannels(short mval[2]);
105
106 /* ******************** SPACE: NLA ********************** */
107
108 void shift_nlastrips_up(void) {
109         
110         Base *base;
111         bActionStrip *strip, *prevstrip;
112
113         for (base=G.scene->base.first; base; base=base->next) {
114                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
115                         continue;
116                 
117                 for (strip = base->object->nlastrips.first; 
118                          strip; strip=strip->next){
119                         if (strip->flag & ACTSTRIP_SELECT) {
120                                 if ( (prevstrip = strip->prev) ) {
121                                         if (prevstrip->prev)
122                                                 prevstrip->prev->next = strip;
123                                         if (strip->next)
124                                                 strip->next->prev = prevstrip;
125                                         strip->prev = prevstrip->prev;
126                                         prevstrip->next = strip->next;
127                                         strip->next = prevstrip;
128                                         prevstrip->prev = strip;
129
130                                         if (prevstrip == base->object->nlastrips.first)
131                                                 base->object->nlastrips.first = strip;
132                                         if (strip == base->object->nlastrips.last)
133                                                 base->object->nlastrips.last = prevstrip;
134
135                                         strip = prevstrip;
136                                 }
137                                 else {
138                                         break;
139                                 }
140                         }
141                 }
142         }
143         BIF_undo_push("Shift NLA strip");
144         allqueue (REDRAWNLA, 0);
145
146 }
147
148 void shift_nlastrips_down(void) {
149         
150         Base *base;
151         bActionStrip *strip, *nextstrip;
152
153         for (base=G.scene->base.first; base; base=base->next) {
154                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
155                         continue;
156                 
157                 for (strip = base->object->nlastrips.last; 
158                          strip; strip=strip->prev){
159                         if (strip->flag & ACTSTRIP_SELECT) {
160                                 if ( (nextstrip = strip->next) ) {
161                                         if (nextstrip->next)
162                                                 nextstrip->next->prev = strip;
163                                         if (strip->prev)
164                                                 strip->prev->next = nextstrip;
165                                         strip->next = nextstrip->next;
166                                         nextstrip->prev = strip->prev;
167                                         strip->prev = nextstrip;
168                                         nextstrip->next = strip;
169
170                                         if (nextstrip == base->object->nlastrips.last)
171                                                 base->object->nlastrips.last = strip;
172                                         if (strip == base->object->nlastrips.first)
173                                                 base->object->nlastrips.first = nextstrip;
174
175                                         strip = nextstrip;
176                                 }
177                                 else {
178                                         break;
179                                 }
180                         }
181                 }
182         }
183         
184         BIF_undo_push("Shift NLA strips");
185         allqueue (REDRAWNLA, 0);
186 }
187
188 void synchronize_action_strips(void)
189 {
190         Base *base;
191         Object *ob;
192         bActionStrip *strip;
193         
194         for (base=G.scene->base.first; base; base=base->next) {
195                 /* get object first */
196                 ob= base->object;
197                 
198                 /* step 1: adjust strip-lengths */
199                 //      FIXME: this seems very buggy
200                 for (strip = ob->nlastrips.last; strip; strip=strip->prev) {
201                         if (strip->flag & ACTSTRIP_LOCK_ACTION) {
202                                 float actstart, actend;
203                                 
204                                 calc_action_range(strip->act, &actstart, &actend, 1);
205                                 
206                                 if ((strip->actstart!=actstart) || (strip->actend!=actend)) {           
207                                         float offset = strip->scale * (actstart - strip->actstart);
208                                         float actlen = actend - actstart;
209                                         
210                                         strip->start += offset;
211                                         strip->end = (strip->scale * strip->repeat * actlen) + strip->start;
212                                         
213                                         strip->actstart= actstart;
214                                         strip->actend= actend;
215                                 }
216                         }
217                 }
218                 
219                 /* step 2: adjust blendin/out values for each strip if option is turned on */
220                 for (strip= ob->nlastrips.first; strip; strip=strip->next) {
221                         if (strip->flag & ACTSTRIP_AUTO_BLENDS) {
222                                 bActionStrip *prev= strip->prev;
223                                 bActionStrip *next= strip->next;
224                                 float pr[2], nr[2];
225                                 
226                                 strip->blendin = 0.0f;
227                                 strip->blendout = 0.0f;
228                                 
229                                 /* setup test ranges */
230                                 if (prev && next) {
231                                         /* init range for previous strip */
232                                         pr[0]= prev->start;
233                                         pr[1]= prev->end;
234                                         
235                                         /* init range for next strip */
236                                         nr[0]= next->start;
237                                         nr[1]= next->end;
238                                 }
239                                 else if (prev) {
240                                         /* next strip's range is same as previous strip's range  */
241                                         pr[0] = nr[0] = prev->start;
242                                         pr[1] = nr[1] = prev->end;
243                                 }
244                                 else if (next) {
245                                         /* previous strip's range is same as next strip's range */
246                                         pr[0] = nr[0] = next->start;
247                                         pr[1] = nr[1] = next->end;
248                                 }
249                                 else {
250                                         /* there shouldn't be any more strips to loop through for this operation */
251                                         break;
252                                 }
253                                 
254                                 /* test various cases */
255                                 if ( IN_RANGE(pr[1], strip->start, strip->end) && 
256                                         (IN_RANGE(pr[0], strip->start, strip->end)==0) )
257                                 {
258                                         /* previous strip intersects start of current */
259                                         
260                                         if ( IN_RANGE(nr[1], strip->start, strip->end) && 
261                                                 (IN_RANGE(nr[0], strip->start, strip->end)==0) )
262                                         {
263                                                 /* next strip also intersects start of current */
264                                                 if (nr[1] < pr[1])
265                                                         strip->blendin= nr[1] - strip->start;
266                                                 else
267                                                         strip->blendin= pr[1] - strip->start;
268                                         }
269                                         else if (IN_RANGE(nr[0], strip->start, strip->end) && 
270                                                         (IN_RANGE(nr[1], strip->start, strip->end)==0))
271                                         {
272                                                 /* next strip intersects end of current */
273                                                 strip->blendout= strip->end - nr[0];
274                                                 strip->blendin= pr[1] - strip->start;
275                                         }
276                                         else {
277                                                 /* only previous strip intersects current */
278                                                 strip->blendin= pr[1] - strip->start;
279                                         }
280                                 }
281                                 else if (IN_RANGE(pr[0], strip->start, strip->end) && 
282                                                 (IN_RANGE(pr[1], strip->start, strip->end)==0) )
283                                 {
284                                         /* previous strip intersects end of current */
285                                         
286                                         if ( IN_RANGE(nr[0], strip->start, strip->end) && 
287                                                 (IN_RANGE(nr[1], strip->start, strip->end)==0) )
288                                         {
289                                                 /* next strip also intersects end of current */
290                                                 if (nr[1] > pr[1])
291                                                         strip->blendout= strip->end - nr[0];
292                                                 else
293                                                         strip->blendout= strip->end - pr[0];
294                                         }
295                                         else if (IN_RANGE(nr[1], strip->start, strip->end) && 
296                                                         (IN_RANGE(nr[0], strip->start, strip->end)==0))
297                                         {
298                                                 /* next strip intersects start of current */
299                                                 strip->blendin= nr[1] - strip->start;
300                                                 strip->blendout= strip->end - pr[0];
301                                         }
302                                         else {
303                                                 /* only previous strip intersects current */
304                                                 strip->blendout= strip->end - pr[0];
305                                         }
306                                 }
307                                 else if (IN_RANGE(nr[1], strip->start, strip->end) && 
308                                                 (IN_RANGE(nr[0], strip->start, strip->end)==0) )
309                                 {
310                                         /* next strip intersects start of current */
311                                         
312                                         if ( IN_RANGE(pr[1], strip->start, strip->end) && 
313                                                 (IN_RANGE(pr[0], strip->start, strip->end)==0) )
314                                         {
315                                                 /* previous strip also intersects start of current */
316                                                 if (pr[1] < nr[1])
317                                                         strip->blendin= pr[1] - strip->start;
318                                                 else
319                                                         strip->blendin= nr[1] - strip->start;
320                                         }
321                                         else if (IN_RANGE(pr[0], strip->start, strip->end) && 
322                                                         (IN_RANGE(pr[1], strip->start, strip->end)==0))
323                                         {
324                                                 /* previous strip intersects end of current */
325                                                 strip->blendout= strip->end - pr[0];
326                                                 strip->blendin= nr[1] - strip->start;
327                                         }
328                                         else {
329                                                 /* only next strip intersects current */
330                                                 strip->blendin= nr[1] - strip->start;
331                                         }
332                                 }
333                                 else if (IN_RANGE(nr[0], strip->start, strip->end) && 
334                                                 (IN_RANGE(nr[1], strip->start, strip->end)==0) )
335                                 {
336                                         /* next strip intersects end of current */
337                                         
338                                         if ( IN_RANGE(pr[0], strip->start, strip->end) && 
339                                                 (IN_RANGE(pr[1], strip->start, strip->end)==0) )
340                                         {
341                                                 /* previous strip also intersects end of current */
342                                                 if (pr[1] > nr[1])
343                                                         strip->blendout= strip->end - pr[0];
344                                                 else
345                                                         strip->blendout= strip->end - nr[0];
346                                         }
347                                         else if (IN_RANGE(pr[1], strip->start, strip->end) && 
348                                                         (IN_RANGE(pr[0], strip->start, strip->end)==0))
349                                         {
350                                                 /* previous strip intersects start of current */
351                                                 strip->blendin= pr[1] - strip->start;
352                                                 strip->blendout= strip->end - nr[0];
353                                         }
354                                         else {
355                                                 /* only next strip intersects current */
356                                                 strip->blendout= strip->end - nr[0];
357                                         }
358                                 }
359                                 
360                                 /* make sure blending stays in ranges */
361                                 CLAMP(strip->blendin, 0, (strip->end-strip->start));
362                                 CLAMP(strip->blendout, 0, (strip->end-strip->start));
363                         }
364                 }
365         }
366         
367 }
368
369 void reset_action_strips(int val)
370 {
371         Base *base;
372         bActionStrip *strip;
373         
374         for (base=G.scene->base.first; base; base=base->next) {
375                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
376                         continue;
377                 
378                 for (strip = base->object->nlastrips.last; strip; strip=strip->prev) {
379                         if (strip->flag & ACTSTRIP_SELECT) {
380                                 switch (val) {
381                                         case 1:
382                                         {
383                                                 /* clear scaling - reset to 1.0 without touching keys */
384                                                 float actlen= (strip->actend - strip->actstart);
385                                                 
386                                                 strip->scale= 1.0f;
387                                                 strip->end= (strip->repeat * actlen) + strip->start;
388                                         }
389                                                 break;
390                                         case 2:
391                                         {
392                                                 /* reset action-range */
393                                                 calc_action_range(strip->act, &strip->actstart, &strip->actend, 1);
394                                         }
395                                                 break;
396                                         case 3:
397                                         {
398                                                 /* apply scale to keys - scale is reset to 1.0f, but keys stay at the same times */
399                                                 bActionChannel *achan;
400                                                 
401                                                 if (strip->act) {
402                                                         for (achan= strip->act->chanbase.first; achan; achan= achan->next) {
403                                                                 actstrip_map_ipo_keys(base->object, achan->ipo, 0, 0);
404                                                         }
405                                                         
406                                                         /* now we can reset scale */
407                                                         calc_action_range(strip->act, &strip->actstart, &strip->actend, 1);
408                                                         strip->scale= 1.0f;
409                                                         strip->end = (strip->repeat * (strip->actend - strip->actstart)) + strip->start;
410                                                 }
411                                         }
412                                                 break;
413                                 }
414                                 base->object->ctime= -1234567.0f;       // evil! 
415                                 DAG_object_flush_update(G.scene, base->object, OB_RECALC_OB|OB_RECALC_DATA);
416                         }
417                 }
418         }
419         BIF_undo_push("Reset NLA strips");
420         allqueue (REDRAWVIEW3D, 0);
421         allqueue (REDRAWACTION, 0);
422         allqueue (REDRAWNLA, 0);
423 }
424
425 void snap_action_strips(int snap_mode)
426 {
427         Base *base;
428         bActionStrip *strip;
429         
430         for (base=G.scene->base.first; base; base=base->next) {
431                 /* object has ipo - these keyframes should be able to be snapped, even if strips are collapsed */
432                 if (base->object->ipo) {
433                         snap_ipo_keys(base->object->ipo, snap_mode);
434                 }
435                 
436                 /* object is collapsed - action and nla strips not shown/editable */
437                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
438                         continue;
439                 
440                 /* snap action strips */
441                 for (strip = base->object->nlastrips.last; strip; strip=strip->prev) {
442                         if (strip->flag & ACTSTRIP_SELECT) {
443                                 if (snap_mode==1) {
444                                         /* nearest frame */
445                                         strip->start= floor(strip->start+0.5);
446                                         strip->end= floor(strip->end+0.5);
447                                 }
448                                 else if (snap_mode==2) {
449                                         /* current frame */
450                                         float diff;
451                                         if (CFRA < strip->start) {
452                                                 diff = (strip->start - CFRA);
453                                                 strip->start -= diff;
454                                                 strip->end -= diff;
455                                         }
456                                         else {
457                                                 diff = (CFRA - strip->start);
458                                                 strip->start += diff;
459                                                 strip->end += diff;
460                                         }
461                                 }
462                                 else if (snap_mode==3) {
463                                         /* nearest second */
464                                         float secf = FPS;
465                                         strip->start= (float)(floor(strip->start/secf + 0.5f) * secf);
466                                         strip->end= (float)(floor(strip->end/secf + 0.5f) * secf);
467                                 }
468                         }
469                 }
470                 
471                 /* object has action */
472                 if (base->object->action) {
473                         ListBase act_data = {NULL, NULL};
474                         bActListElem *ale;
475                         int filter;
476                         
477                         /* filter action data */
478                         filter= (ACTFILTER_VISIBLE | ACTFILTER_FOREDIT | ACTFILTER_IPOKEYS);
479                         actdata_filter(&act_data, filter, base->object->action, ACTCONT_ACTION);
480                         
481                         /* snap to frame */
482                         for (ale= act_data.first; ale; ale= ale->next) {
483                                 actstrip_map_ipo_keys(base->object, ale->key_data, 0, 1); 
484                                 snap_ipo_keys(ale->key_data, snap_mode);
485                                 actstrip_map_ipo_keys(base->object, ale->key_data, 1, 1);
486                         }
487                         BLI_freelistN(&act_data);
488                         
489                         remake_action_ipos(base->object->action);
490                 }
491         }
492         BIF_undo_push("Snap NLA strips");
493         allqueue (REDRAWVIEW3D, 0);
494         allqueue (REMAKEIPO, 0);
495         allqueue (REDRAWIPO, 0);
496         allqueue (REDRAWACTION, 0);
497         allqueue (REDRAWNLA, 0);
498 }
499
500 static void set_active_strip(Object *ob, bActionStrip *act)
501 {
502         bActionStrip *strip;
503         
504         for (strip = ob->nlastrips.first; strip; strip=strip->next)
505                 strip->flag &= ~ACTSTRIP_ACTIVE;
506         
507         if(act) {
508                 act->flag |= ACTSTRIP_ACTIVE;
509         
510                 if(ob->action!=act->act) {
511                         if(ob->action) ob->action->id.us--;
512                         if(act->act->id.lib) {
513                                 ob->action= NULL;
514                         }
515                         else {
516                                 ob->action= act->act;
517                                 id_us_plus(&ob->action->id);
518                         }                       
519                         allqueue(REDRAWIPO, 0);
520                         allqueue(REDRAWVIEW3D, 0);
521                         allqueue(REDRAWACTION, 0);
522                         allqueue(REDRAWNLA, 0);
523                         ob->ctime= -1234567.0f; // eveil! 
524                         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB|OB_RECALC_DATA);
525                 }
526         }       
527 }
528
529 void convert_nla(void)
530 {
531         bActionStrip *strip;
532         Object *ob= OBACT;
533         char str[128];
534         short event;
535         
536         if ((ob==NULL)||(ob->action==NULL)) {
537                 error("Need active Object to convert Action to NLA Strip");
538                 return;
539         }
540         
541         sprintf(str, "Convert Action%%t|%s to NLA Strip%%x1", ob->action->id.name+2);
542         event = pupmenu(str);
543         
544         if (event==1) {
545                 if (ob->action) {
546                         deselect_nlachannel_keys(0);
547                         strip = convert_action_to_strip(ob); //creates a new NLA strip from the action in given object
548                         set_active_strip(ob, strip);
549                         BIF_undo_push("Convert NLA");
550                         allqueue (REDRAWNLA, 0);
551                 }
552         }
553 }
554
555 static void add_nla_block(short event)
556 {
557         Object *ob= OBACT;
558         bAction *act=NULL;
559         bActionStrip *strip;
560         int             cur;
561
562         if (event!=-1){
563                 for (cur = 1, act=G.main->action.first; act; act=act->id.next, cur++){
564                         if (cur==event){
565                                 break;
566                         }
567                 }
568         }
569         
570         /* Bail out if no action was chosen */
571         if (!act){
572                 return;
573         }
574         
575         /* Initialize the new action block */
576         strip = MEM_callocN(sizeof(bActionStrip), "bActionStrip");
577         
578         deselect_nlachannel_keys(0);
579         
580         /* Link the action to the strip */
581         strip->act = act;
582         id_us_plus(&act->id);
583         calc_action_range(strip->act, &strip->actstart, &strip->actend, 1);
584         strip->start = G.scene->r.cfra;         /* could be mval[0] another time... */
585         strip->end = strip->start + (strip->actend-strip->actstart);
586                 /* simple prevention of zero strips */
587         if(strip->start>strip->end-2) 
588                 strip->end= strip->start+100;
589         strip->repeat = strip->scale= 1.0f;
590         
591         strip->flag = ACTSTRIP_SELECT|ACTSTRIP_LOCK_ACTION;
592         
593         find_stridechannel(ob, strip);
594         set_active_strip(ob, strip);
595         strip->object= group_get_member_with_action(ob->dup_group, act);
596         if(strip->object)
597                 id_lib_extern(&strip->object->id);      /* checks lib data, sets correct flag for saving then */
598
599         if(ob->nlastrips.first == NULL)
600                 ob->nlaflag |= OB_NLA_OVERRIDE;
601         
602         BLI_addtail(&ob->nlastrips, strip);
603
604         BIF_undo_push("Add NLA strip");
605 }
606
607 static void add_nla_block_by_name(char name[32], Object *ob, short hold, short add, float repeat)
608 {
609         bAction *act=NULL;
610         bActionStrip *strip;
611         int             cur;
612
613         if (name){
614                 for (cur = 1, act=G.main->action.first; act; act=act->id.next, cur++){
615                         if (strcmp(name,act->id.name)==0) {
616                                 break;
617                         }
618                 }
619         }
620         
621         /* Bail out if no action was chosen */
622         if (!act){
623                 return;
624         }
625         
626         /* Initialize the new action block */
627         strip = MEM_callocN(sizeof(bActionStrip), "bActionStrip");
628         strip->scale= 1.0f;
629         
630         deselect_nlachannel_keys(0);
631         
632         /* Link the action to the strip */
633         strip->act = act;
634         calc_action_range(strip->act, &strip->actstart, &strip->actend, 1);
635         strip->start = G.scene->r.cfra;         /* could be mval[0] another time... */
636         strip->end = strip->start + (strip->actend-strip->actstart);
637                 /* simple prevention of zero strips */
638         if(strip->start>strip->end-2) 
639                 strip->end= strip->start+100;
640         
641         strip->flag = ACTSTRIP_SELECT|ACTSTRIP_LOCK_ACTION; //|ACTSTRIP_USEMATCH;
642         
643         if (hold==1)
644                 strip->flag = strip->flag|ACTSTRIP_HOLDLASTFRAME;
645                 
646         if (add==1)
647                 strip->mode = ACTSTRIPMODE_ADD;
648         
649         find_stridechannel(ob, strip);
650         
651         set_active_strip(ob, strip);
652         
653         strip->repeat = repeat;
654         
655         act->id.us++;
656         
657         if(ob->nlastrips.first == NULL)
658                 ob->nlaflag |= OB_NLA_OVERRIDE;
659                 
660         BLI_addtail(&ob->nlastrips, strip);
661 }
662
663 static void add_nla_databrowse_callback(unsigned short val)
664 {
665         /* val is not used, databrowse needs it to optional pass an event */
666         short event;
667         
668         if(OBACT==NULL) return;
669         
670         event= G.snla->menunr;  /* set by databrowse or pupmenu */
671         
672         add_nla_block(event);
673 }
674
675 /* Adds strip to to active Object */
676 void add_nlablock(void)
677 {
678         Object *ob= OBACT;
679         short event;
680         short nr=0;
681         char *str, title[64];
682         
683         if(ob==NULL) {
684                 error("Need active Object to add NLA strips");
685                 return;
686         }
687         
688         sprintf(title, "Add Action strip to: %s", ob->id.name+2);
689         
690         /* Popup action menu */
691         IDnames_to_pupstring(&str, title, NULL, &G.main->action, (ID *)G.scene, &nr);
692         
693         if(nr==-2) {
694                 MEM_freeN(str);
695
696                 activate_databrowse((ID *)NULL, ID_AC, 0, 0, &G.snla->menunr, 
697                                                         add_nla_databrowse_callback );
698                 
699                 return;                 
700         }
701         else {
702                 event = pupmenu_col(str, 20);
703                 MEM_freeN(str);
704                 add_nla_block(event);
705         }
706 }
707
708 /* Creates a new action, and makes a new actionstrip of that */
709 void add_empty_nlablock(void)
710 {
711         Object *ob= OBACT;
712         bAction *act= NULL;
713         bActionStrip *strip;
714         
715         /* check for active object first - will add strip to active object */
716         if (ob == NULL) 
717                 return;
718                 
719         /* make new action */
720         if ((ob->type == OB_ARMATURE) && (ob->flag & OB_POSEMODE))
721                 act= add_empty_action("ObAction");
722         else
723                 act= add_empty_action("Action");
724                 
725         /* make a new strip for it */
726         add_nla_block_by_name(act->id.name, ob, 0, 1, 1.0f);
727         strip= ob->nlastrips.last; 
728         
729         /* change some settings of the strip - try to avoid bad scaling */
730         if ((EFRA-CFRA) < 100) {
731                 strip->flag |= ACTSTRIP_AUTO_BLENDS;
732                 strip->flag &= ~ACTSTRIP_LOCK_ACTION;
733                 strip->actstart = CFRA;
734                 strip->actend = CFRA + 100;
735                 
736                 strip->start = CFRA;
737                 strip->end = CFRA + 100;
738         }
739         else {
740                 strip->flag |= ACTSTRIP_AUTO_BLENDS;
741                 strip->flag &= ~ACTSTRIP_LOCK_ACTION;
742                 strip->actstart = CFRA;
743                 strip->actend = EFRA;
744                 
745                 strip->start = CFRA;
746                 strip->end = EFRA;
747         }
748         
749         BIF_undo_push("Add NLA strip");
750 }
751
752 /* Adds strip to to active Object */
753 static void relink_active_strip(void)
754 {
755         Object *ob= OBACT;
756         bActionStrip *strip;
757         bAction *act;
758         short event;
759         short cur;
760         char *str;
761         
762         if(ob==NULL) return;
763         if(ob->nlaflag & OB_NLA_COLLAPSED) return;
764         
765         for (strip = ob->nlastrips.first; strip; strip=strip->next)
766                 if(strip->flag & ACTSTRIP_ACTIVE)
767                         break;
768         
769         if(strip==NULL) return;
770         
771         /* Popup action menu */
772         IDnames_to_pupstring(&str, "Relink Action strip", NULL, &G.main->action, (ID *)G.scene, NULL);
773         if(str) {
774                 event = pupmenu_col(str, 20);
775                 MEM_freeN(str);
776                 
777                 for (cur = 1, act=G.main->action.first; act; act=act->id.next, cur++){
778                         if (cur==event){
779                                 break;
780                         }
781                 }
782                 
783                 if(act) {
784                         if(strip->act) strip->act->id.us--;
785                         strip->act = act;
786                         id_us_plus(&act->id);
787                         
788                         allqueue(REDRAWVIEW3D, 0);
789                         allqueue(REDRAWACTION, 0);
790                         allqueue(REDRAWNLA, 0);
791                 }
792         }
793 }
794
795
796
797 /* Left hand side of channels display, selects objects */
798 static void mouse_nlachannels(short mval[2])
799 {
800         bActionStrip *strip= NULL;
801         Base    *base;
802         Object *ob=NULL;
803         float   x,y;
804         int             click, obclick=0, actclick=0;
805         int             wsize;
806         
807         wsize = (count_nla_levels ()*(NLACHANNELHEIGHT+NLACHANNELSKIP));
808         wsize+= NLACHANNELHEIGHT/2;
809
810         areamouseco_to_ipoco(G.v2d, mval, &x, &y);
811         click = (int)floor( ((float)wsize - y) / (NLACHANNELHEIGHT+NLACHANNELSKIP));
812         
813         if (click<0)
814                 return;
815
816         for (base = G.scene->base.first; base; base=base->next){
817                 if (nla_filter(base)) {
818                         ob= base->object;
819                         
820                         /* See if this is a base selected */
821                         if (click==0) {
822                                 obclick= 1;
823                                 break;
824                         }
825                         click--;
826                         
827                         /* see if any strips under object */
828                         if ((ob->nlaflag & OB_NLA_COLLAPSED)==0) {
829                                 /* See if this is an action */
830                                 if (ob->action){
831                                         if (click==0) {
832                                                 actclick= 1;
833                                                 break;
834                                         }
835                                         click--;
836                                 }
837
838                                 /* See if this is an nla strip */
839                                 if(ob->nlastrips.first) {
840                                         for (strip = ob->nlastrips.first; strip; strip=strip->next){
841                                                 if (click==0) break;
842                                                 click--;                                
843                                         }
844                                         if (strip && click==0) break;
845                                 }
846                         }
847                 }
848         }
849
850         if (!base)
851                 return;
852
853         /* Handle object strip selection */
854         if (G.qual & LR_SHIFTKEY) {
855                 if (base->flag & SELECT) base->flag &= ~SELECT;
856                 else base->flag |= SELECT;
857         }
858         else {
859                 deselect_nlachannels (0);       // Auto clear
860                 base->flag |= SELECT;
861         }
862         ob->flag= base->flag;
863         
864         if(base!=BASACT) set_active_base(base);
865         
866         if(actclick) /* de-activate all strips */
867                 set_active_strip(ob, NULL);
868         else if(strip) {
869                 if(mval[0] >= (NLAWIDTH-16)) /* toggle strip muting */
870                         strip->flag ^= ACTSTRIP_MUTE;
871                 else /* set action */
872                         set_active_strip(ob, strip);
873         }
874
875         /* icon toggles beside strip */
876         if (obclick && mval[0]<20) {
877                 /* collapse option for NLA object strip */
878                 ob->nlaflag ^= OB_NLA_COLLAPSED;
879         }
880         else if(obclick && mval[0]<36) {
881                 /* override option for NLA */
882                 ob->nlaflag ^= OB_NLA_OVERRIDE;
883         }
884         else if((obclick) && (ob->ipo) && (mval[0] >= (NLAWIDTH-16))) {
885                 /* mute Object IPO-block */
886                 ob->ipo->muteipo = (ob->ipo->muteipo)? 0: 1;
887         }
888         
889         ob->ctime= -1234567.0f; // eveil! 
890         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB|OB_RECALC_DATA);
891
892         allqueue(REDRAWIPO, 0);
893         allqueue(REDRAWVIEW3D, 0);
894         allqueue(REDRAWACTION, 0);
895         allqueue(REDRAWNLA, 0);
896         
897 }
898
899 void deselect_nlachannel_keys (int test)
900 {
901         Base                    *base;
902         int                             sel=1;
903         bActionChannel  *chan;
904         bActionStrip    *strip;
905         bConstraintChannel *conchan;
906         
907         /* Determine if this is selection or deselection */
908         if (test){
909                 for (base=G.scene->base.first; base && sel; base=base->next){           
910                         /* Test object ipos */
911                         if (is_ipo_key_selected(base->object->ipo)){
912                                 sel = 0;
913                                 break;
914                         }
915                         
916                         /* Test object constraint ipos */
917                         if (sel){
918                                 for (conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next){
919                                         if (is_ipo_key_selected(conchan->ipo)){
920                                                 sel=0;
921                                                 break;
922                                         }
923                                 }
924                         }
925                         
926                         /* check if collapsed */
927                         if (base->object->nlaflag & OB_NLA_COLLAPSED)
928                                 continue;
929                         
930                         /* Test action ipos */
931                         if (sel){
932                                 if (base->object->action){
933                                         for (chan=base->object->action->chanbase.first; chan; chan=chan->next){
934                                                 if (is_ipo_key_selected(chan->ipo)){
935                                                         sel=0;
936                                                         break;
937                                                 }
938
939                                                 /* Test action constraints */
940                                                 if (sel){
941                                                         for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
942                                                                 if (is_ipo_key_selected(conchan->ipo)){
943                                                                         sel=0;
944                                                                         break;
945                                                                 }
946                                                         }
947                                                 }
948                                         }
949                                 }
950                         }
951                         
952                         /* Test NLA strips */
953                         if (sel){
954                                 for (strip=base->object->nlastrips.first; strip; strip=strip->next){
955                                         if (strip->flag & ACTSTRIP_SELECT){
956                                                 sel = 0;
957                                                 break;
958                                         }
959                                 }
960                         }
961                 }
962         }
963         else
964                 sel=0;
965         
966         
967         /* Set the flags */
968         for (base=G.scene->base.first; base; base=base->next){          
969                 /* Set the object ipos */
970                 set_ipo_key_selection(base->object->ipo, sel);
971                 
972                 /* Set the object constraint ipos */
973                 for (conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next){
974                         set_ipo_key_selection(conchan->ipo, sel);                       
975                 }
976
977                 /* check if collapsed */
978                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
979                         continue;
980                 
981                 /* Set the action ipos */
982                 if (base->object->action){
983                         for (chan=base->object->action->chanbase.first; chan; chan=chan->next){
984                                 set_ipo_key_selection(chan->ipo, sel);
985                                 /* Set the action constraint ipos */
986                                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
987                                         set_ipo_key_selection(conchan->ipo, sel);
988                         }
989                 }
990                 
991                 /* Set the nlastrips */
992                 for (strip=base->object->nlastrips.first; strip; strip=strip->next){
993                         if (sel)
994                                 strip->flag |= ACTSTRIP_SELECT;
995                         else
996                                 strip->flag &= ~ACTSTRIP_SELECT;
997                 }
998         }
999 }
1000
1001 /* very bad call! */
1002 static void recalc_all_ipos(void)
1003 {
1004         Ipo *ipo;
1005         IpoCurve *icu;
1006         
1007         /* Go to each ipo */
1008         for (ipo=G.main->ipo.first; ipo; ipo=ipo->id.next){
1009                 for (icu = ipo->curve.first; icu; icu=icu->next){
1010                         sort_time_ipocurve(icu);
1011                         testhandles_ipocurve(icu);
1012                 }
1013         }
1014 }
1015
1016 void transform_nlachannel_keys(int mode, int dummy)
1017 {
1018         short context = (U.flag & USER_DRAGIMMEDIATE)?CTX_TWEAK:CTX_NONE;
1019
1020         switch (mode) {
1021                 case 'g':
1022                 {
1023                         initTransform(TFM_TIME_TRANSLATE, context);
1024                         Transform();
1025                 }
1026                         break;
1027                 case 's':
1028                 {
1029                         initTransform(TFM_TIME_SCALE, context);
1030                         Transform();
1031                 }
1032                         break;
1033                 case 'e':
1034                 {
1035                         initTransform(TFM_TIME_EXTEND, context);
1036                         Transform();
1037                 }
1038                         break;
1039         }
1040 }
1041
1042 void delete_nlachannel_keys(void)
1043 {
1044         Base *base;
1045         bActionChannel *chan;
1046         bConstraintChannel *conchan;
1047         bActionStrip *strip, *nextstrip;
1048                 
1049         for (base = G.scene->base.first; base; base=base->next){
1050                 /* Delete object ipos */
1051                 delete_ipo_keys(base->object->ipo);
1052                 
1053                 /* Delete object constraint keys */
1054                 for(conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next)
1055                         delete_ipo_keys(conchan->ipo);
1056
1057                 /* skip actions and nlastrips if object collapsed */
1058                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
1059                         continue;
1060                         
1061                 /* Delete NLA strips */
1062                 for (strip = base->object->nlastrips.first; strip; strip=nextstrip){
1063                         nextstrip=strip->next;
1064                         if (strip->flag & ACTSTRIP_SELECT){
1065                                 free_actionstrip(strip);
1066                                 BLI_remlink(&base->object->nlastrips, strip);
1067                                 MEM_freeN(strip);
1068                         }
1069                 }
1070                 
1071                 /* Delete action ipos */
1072                 if (base->object->action){
1073                         for (chan=base->object->action->chanbase.first; chan; chan=chan->next){
1074                                 if (EDITABLE_ACHAN(chan))
1075                                         delete_ipo_keys(chan->ipo);
1076                                         
1077                                 /* Delete action constraint keys */
1078                                 for(conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
1079                                         if (EDITABLE_CONCHAN(conchan))
1080                                                 delete_ipo_keys(conchan->ipo);
1081                                 }
1082                         }
1083                 }
1084         }
1085         
1086         recalc_all_ipos();      // bad
1087         synchronize_action_strips();
1088         
1089         BIF_undo_push("Delete NLA keys");
1090         allspace(REMAKEIPO,0);
1091         allqueue (REDRAWVIEW3D, 0);
1092         allqueue(REDRAWNLA, 0);
1093         allqueue(REDRAWIPO, 0);
1094 }
1095
1096 void duplicate_nlachannel_keys(void)
1097 {
1098         Base *base;
1099         bActionChannel *chan;
1100         bConstraintChannel *conchan;
1101         bActionStrip *strip, *laststrip;
1102         
1103         /* Find selected items */
1104         for (base = G.scene->base.first; base; base=base->next){        
1105                 /* Duplicate object keys */
1106                 duplicate_ipo_keys(base->object->ipo);
1107                 
1108                 /* Duplicate object constraint keys */
1109                 for(conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next)
1110                         duplicate_ipo_keys(conchan->ipo);
1111                 
1112                 /* skip actions and nlastrips if object collapsed */
1113                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
1114                         continue;
1115                         
1116                 /* Duplicate nla strips */
1117                 laststrip = base->object->nlastrips.last;
1118                 for (strip=base->object->nlastrips.first; strip; strip=strip->next){
1119                         if (strip->flag & ACTSTRIP_SELECT){
1120                                 bActionStrip *newstrip;
1121                                 
1122                                 copy_actionstrip(&newstrip, &strip);
1123                                 
1124                                 BLI_addtail(&base->object->nlastrips, newstrip);
1125                                 
1126                                 strip->flag &= ~ACTSTRIP_SELECT;
1127                                 newstrip->flag |= ACTSTRIP_SELECT;
1128                                 set_active_strip(base->object, newstrip);
1129
1130                         }
1131                         if (strip==laststrip)
1132                                 break;
1133                 }
1134                 
1135                 /* Duplicate actionchannel keys */
1136                 if (base->object->action){
1137                         for (chan=base->object->action->chanbase.first; chan; chan=chan->next){
1138                                 if (EDITABLE_ACHAN(chan))
1139                                         duplicate_ipo_keys(chan->ipo);
1140                                         
1141                                 /* Duplicate action constraint keys */
1142                                 for(conchan=chan->constraintChannels.first; conchan; conchan=conchan->next) {
1143                                         if (EDITABLE_CONCHAN(conchan))
1144                                                 duplicate_ipo_keys(conchan->ipo);
1145                                 }
1146                         }
1147                 }
1148         }
1149         
1150         BIF_undo_push("Duplicate NLA");
1151         transform_nlachannel_keys ('g', 0);
1152 }
1153
1154 void borderselect_nla(void)
1155
1156         Base *base;
1157         rcti rect;
1158         rctf rectf;
1159         int  val, selectmode;
1160         short   mval[2];
1161         float   ymin, ymax;
1162         bActionStrip *strip;
1163         bConstraintChannel *conchan;
1164         
1165         if ( (val = get_border (&rect, 3)) ){
1166             if (val == LEFTMOUSE)
1167                         selectmode = SELECT_ADD;
1168             else
1169                         selectmode = SELECT_SUBTRACT;
1170           
1171                 mval[0]= rect.xmin;
1172                 mval[1]= rect.ymin+2;
1173                 areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
1174                 mval[0]= rect.xmax;
1175                 mval[1]= rect.ymax-2;
1176                 areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
1177                 
1178                 ymax = count_nla_levels();
1179                 ymax*= (NLACHANNELHEIGHT+NLACHANNELSKIP);
1180                 ymax+= (NLACHANNELHEIGHT+NLACHANNELSKIP)/2;
1181         
1182                 for (base=G.scene->base.first; base; base=base->next){
1183                         if (nla_filter(base)) {
1184                                 
1185                                 ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1186                                 
1187                                 /* Check object ipos */
1188                                 if (base->object->ipo){
1189                                         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax)))
1190                                                 borderselect_ipo_key(base->object->ipo, rectf.xmin, rectf.xmax,
1191                                  selectmode);
1192                                 }
1193                                 /* Check object constraint ipos */
1194                                 for (conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next){
1195                                         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax)))
1196                                                 borderselect_ipo_key(conchan->ipo, rectf.xmin, rectf.xmax,
1197                                  selectmode);
1198                                 }
1199                                 
1200                                 ymax=ymin;
1201                                 
1202                                 /* skip actions and nlastrips if object collapsed */
1203                                 if (base->object->nlaflag & OB_NLA_COLLAPSED)
1204                                         continue;
1205                                 
1206                                 /* Check action ipos */
1207                                 if (base->object->action){
1208                                         bActionChannel *chan;
1209                                         float xmin, xmax;
1210                                         
1211                                         ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1212                                         
1213                                         /* if action is mapped in NLA, it returns a correction */
1214                                         xmin= get_action_frame(base->object, rectf.xmin);
1215                                         xmax= get_action_frame(base->object, rectf.xmax);
1216                                         
1217                                         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
1218                                                 for (chan=base->object->action->chanbase.first; chan; chan=chan->next){
1219                                                         borderselect_ipo_key(chan->ipo, xmin, xmax, selectmode);
1220                                                         /* Check action constraint ipos */
1221                                                         for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
1222                                                                 borderselect_ipo_key(conchan->ipo, xmin, xmax, selectmode);
1223                                                 }
1224                                         }
1225
1226                                         ymax=ymin;
1227                                 }       /* End of if action */
1228                                 
1229                                 /* Skip nlastrips */
1230                                 for (strip=base->object->nlastrips.first; strip; strip=strip->next){
1231                                         ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1232                                         //
1233                                         if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
1234                                                 if (!((rectf.xmax<strip->start) || (rectf.xmin>strip->end))){
1235                                                         if (val==1)
1236                                                                 strip->flag |= ACTSTRIP_SELECT;
1237                                                         else
1238                                                                 strip->flag &= ~ACTSTRIP_SELECT;
1239                                                 }
1240                                         }
1241                                         
1242                                         ymax=ymin;
1243                                 }
1244                         }
1245                 }       
1246                 BIF_undo_push("Border select NLA");
1247                 allqueue(REDRAWMARKER, 0);
1248         }
1249 }
1250
1251 /* right hand side of window, does ipokeys, actionkeys or strips */
1252 static void mouse_nla(int selectmode)
1253 {
1254         Base *base;
1255         bAction *act;
1256         bActionChannel *chan;
1257         bActionStrip *rstrip;
1258         bConstraintChannel *conchan;
1259         TimeMarker *marker;
1260         float   selx;
1261         short   mval[2];
1262         short sel, isdone=0;
1263         
1264         getmouseco_areawin (mval);
1265         
1266         /* Try object ipo or ob-constraint ipo selection */
1267         base= get_nearest_nlachannel_ob_key(&selx, &sel);
1268         marker=find_nearest_marker(SCE_MARKERS, 1);
1269         if (base) {
1270                 isdone= 1;
1271                 
1272                 if (selectmode == SELECT_REPLACE){
1273                         deselect_nlachannel_keys(0);
1274                         selectmode = SELECT_ADD;
1275                 }
1276                 
1277                 select_ipo_key(base->object->ipo, selx, selectmode);
1278                 
1279                 /* Try object constraint selection */
1280                 for (conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next)
1281                         select_ipo_key(conchan->ipo, selx, selectmode);
1282         }
1283         else if (marker) {
1284                 /* marker */            
1285                 if (selectmode == SELECT_REPLACE) {                     
1286                         deselect_markers(0, 0);
1287                         marker->flag |= SELECT;
1288                 }
1289                 else if (selectmode == SELECT_INVERT) {
1290                         if (marker->flag & SELECT)
1291                                 marker->flag &= ~SELECT;
1292                         else
1293                                 marker->flag |= SELECT;
1294                 }
1295                 else if (selectmode == SELECT_ADD) 
1296                         marker->flag |= SELECT;
1297                 else if (selectmode == SELECT_SUBTRACT)
1298                         marker->flag &= ~SELECT;
1299                 
1300                 std_rmouse_transform(transform_markers);
1301                 
1302                 allqueue(REDRAWMARKER, 0);
1303         }
1304         else {
1305                 /* Try action ipo selection */
1306                 act= get_nearest_nlachannel_ac_key(&selx, &sel);
1307                 if (act) {
1308                         isdone= 1;
1309                         
1310                         if (selectmode == SELECT_REPLACE){
1311                                 deselect_nlachannel_keys(0);
1312                                 selectmode = SELECT_ADD;
1313                         }
1314                         
1315                         for (chan=act->chanbase.first; chan; chan=chan->next) {
1316                                 select_ipo_key(chan->ipo, selx, selectmode);
1317                                 /* Try action constraint selection */
1318                                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next)
1319                                         select_ipo_key(conchan->ipo, selx, selectmode);
1320                         }
1321                 }
1322                 else {
1323         
1324                         /* Try nla strip selection */
1325                         base= get_nearest_nlastrip(&rstrip, &sel);
1326                         if (base){
1327                                 isdone= 1;
1328                                 
1329                                 if (!(G.qual & LR_SHIFTKEY)){
1330                                         deselect_nlachannel_keys(0);
1331                                         sel = 0;
1332                                 }
1333                                 
1334                                 if (sel)
1335                                         rstrip->flag &= ~ACTSTRIP_SELECT;
1336                                 else
1337                                         rstrip->flag |= ACTSTRIP_SELECT;
1338                                 
1339                                 set_active_strip(base->object, rstrip);
1340                                 
1341                                 if(base!=BASACT) set_active_base(base);
1342                         }
1343                 }
1344         }
1345         
1346         if(isdone) {
1347                 std_rmouse_transform(transform_nlachannel_keys);
1348                 
1349                 allqueue(REDRAWIPO, 0);
1350                 allqueue(REDRAWVIEW3D, 0);
1351                 allqueue(REDRAWNLA, 0);
1352         }
1353 }
1354
1355 /* This function is currently more complicated than it seems like it should be.
1356 * However, this will be needed once the nla strip timeline is more complex */
1357 static Base *get_nearest_nlastrip (bActionStrip **rstrip, short *sel)
1358 {
1359         Base *base, *firstbase=NULL;
1360         bActionStrip *strip, *firststrip=NULL, *foundstrip=NULL;
1361         rctf    rectf;
1362         float ymin, ymax;
1363         short mval[2];
1364         short foundsel = 0;
1365
1366         getmouseco_areawin (mval);
1367         
1368         mval[0]-=7;
1369         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
1370         
1371         mval[0]+=14;
1372         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
1373         
1374         ymax = count_nla_levels();
1375         ymax*=(NLACHANNELHEIGHT + NLACHANNELSKIP);
1376         ymax+= NLACHANNELHEIGHT/2;
1377         
1378         for (base = G.scene->base.first; base; base=base->next){
1379                 if (nla_filter(base)) {
1380                         
1381                         /* Skip object ipos */
1382                         ymax-=(NLACHANNELHEIGHT+NLACHANNELSKIP);
1383                         
1384                         /* check if skip strips if collapsed */
1385                         if (base->object->nlaflag & OB_NLA_COLLAPSED)
1386                                 continue;
1387
1388                         /* Skip action ipos */
1389                         if (base->object->action)
1390                                 ymax-=(NLACHANNELHEIGHT+NLACHANNELSKIP);
1391                         
1392                         /* the strips */
1393                         for (strip=base->object->nlastrips.first; strip; strip=strip->next){
1394                                 ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1395                                 /* Do Ytest */
1396                                 if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
1397                                         /* Do XTest */
1398                                         if (!((rectf.xmax<strip->start) || (rectf.xmin>strip->end))){
1399                                                 if (!firstbase){
1400                                                         firstbase=base;
1401                                                         firststrip=strip;
1402                                                         *sel = strip->flag & ACTSTRIP_SELECT;
1403                                                 }
1404                                                 
1405                                                 if (strip->flag & ACTSTRIP_SELECT){ 
1406                                                         if (!foundsel){
1407                                                                 foundsel=1;
1408                                                                 foundstrip = strip;
1409                                                         }
1410                                                 }
1411                                                 else if (foundsel && strip != foundstrip){
1412                                                         *rstrip=strip;
1413                                                         *sel = 0;
1414                                                         return base;
1415                                                 }
1416                                         }
1417                                 }
1418                                 ymax=ymin;
1419                         }
1420                 }
1421         }
1422         *rstrip=firststrip;
1423         return firstbase;
1424 }
1425
1426 static Base *get_nearest_nlachannel_ob_key (float *index, short *sel)
1427 {
1428         Base *base;
1429         IpoCurve *icu;
1430         Base *firstbase=NULL;
1431         bConstraintChannel *conchan;
1432         int     foundsel=0;
1433         float firstvertx=-1, foundx=-1;
1434         int i;
1435         short mval[2];
1436         float ymin, ymax;
1437         rctf    rectf;
1438         
1439         *index=0;
1440         
1441         getmouseco_areawin (mval);
1442         
1443         mval[0]-=7;
1444         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
1445         
1446         mval[0]+=14;
1447         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
1448         
1449         ymax = count_nla_levels();
1450         
1451         ymax*= (NLACHANNELHEIGHT + NLACHANNELSKIP);
1452         ymax+= NLACHANNELHEIGHT/2;
1453         
1454         *sel=0;
1455         
1456         for (base=G.scene->base.first; base; base=base->next){
1457                 if (nla_filter(base)) {
1458                         
1459                         ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1460                         
1461                         /* Handle object ipo selection */
1462                         if (base->object->ipo){
1463                                 if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
1464                                         for (icu=base->object->ipo->curve.first; icu; icu=icu->next){
1465                                                 for (i=0; i<icu->totvert; i++){
1466                                                         if (icu->bezt[i].vec[1][0] > rectf.xmin && icu->bezt[i].vec[1][0] <= rectf.xmax ){
1467                                                                 if (!firstbase){
1468                                                                         firstbase=base;
1469                                                                         firstvertx=icu->bezt[i].vec[1][0];
1470                                                                         *sel = icu->bezt[i].f2 & 1;     
1471                                                                 }
1472                                                                 
1473                                                                 if (icu->bezt[i].f2 & 1){ 
1474                                                                         if (!foundsel){
1475                                                                                 foundsel=1;
1476                                                                                 foundx = icu->bezt[i].vec[1][0];
1477                                                                         }
1478                                                                 }
1479                                                                 else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
1480                                                                         *index=icu->bezt[i].vec[1][0];
1481                                                                         *sel = 0;
1482                                                                         return base;
1483                                                                 }
1484                                                         }
1485                                                 }
1486                                         }
1487                                 }
1488                         }
1489                         /* Handle object constraint ipos */
1490                         for (conchan=base->object->constraintChannels.first; conchan; conchan=conchan->next){
1491                                 if ( !((ymax < rectf.ymin) || (ymin > rectf.ymax)) && conchan->ipo){
1492                                         for (icu=conchan->ipo->curve.first; icu; icu=icu->next){
1493                                                 for (i=0; i<icu->totvert; i++){
1494                                                         if (icu->bezt[i].vec[1][0] > rectf.xmin && icu->bezt[i].vec[1][0] <= rectf.xmax ){
1495                                                                 if (!firstbase){
1496                                                                         firstbase=base;
1497                                                                         firstvertx=icu->bezt[i].vec[1][0];
1498                                                                         *sel = icu->bezt[i].f2 & 1;     
1499                                                                 }
1500                                                                 
1501                                                                 if (icu->bezt[i].f2 & 1){ 
1502                                                                         if (!foundsel){
1503                                                                                 foundsel=1;
1504                                                                                 foundx = icu->bezt[i].vec[1][0];
1505                                                                         }
1506                                                                 }
1507                                                                 else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
1508                                                                         *index=icu->bezt[i].vec[1][0];
1509                                                                         *sel = 0;
1510                                                                         return base;
1511                                                                 }
1512                                                         }
1513                                                 }
1514                                         }
1515                                 }
1516                         }
1517
1518                         ymax=ymin;
1519                         
1520                         /* Skip actions and nlastrips if object is collapsed */
1521                         if (base->object->nlaflag & OB_NLA_COLLAPSED)
1522                                 continue;
1523                         
1524                         /* Skip action ipos */
1525                         if (base->object->action){
1526                                 ymax-=(NLACHANNELHEIGHT+NLACHANNELSKIP);
1527                         }
1528                         /* Skip nlastrips */
1529                         ymax-=(NLACHANNELHEIGHT+NLACHANNELSKIP)*BLI_countlist(&base->object->nlastrips);
1530                 }
1531         }       
1532         
1533         *index=firstvertx;
1534         return firstbase;
1535 }
1536
1537 static bAction *get_nearest_nlachannel_ac_key (float *index, short *sel)
1538 {
1539         Base *base;
1540         IpoCurve *icu;
1541         bAction *firstact=NULL;
1542         bActionChannel *chan;
1543         bConstraintChannel *conchan;
1544         rctf    rectf;
1545         float firstvert=-1, foundx=-1;
1546         float ymin, ymax, xmin, xmax;
1547         int i;
1548         int     foundsel=0;
1549         short mval[2];
1550         
1551         *index=0;
1552         
1553         getmouseco_areawin (mval);
1554         
1555         mval[0]-=7;
1556         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
1557         
1558         mval[0]+=14;
1559         areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
1560         
1561         ymax = count_nla_levels();
1562         
1563         ymax*= (NLACHANNELHEIGHT + NLACHANNELSKIP);
1564         ymax+= NLACHANNELHEIGHT/2;
1565         
1566         *sel=0;
1567         
1568         for (base=G.scene->base.first; base; base=base->next){
1569                 /* Handle object ipo selection */
1570                 if (nla_filter(base)) {
1571                         
1572                         /* Skip object ipo and ob-constraint ipo */
1573                         ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1574                         
1575                         /* skip this object if it is collapsed */
1576                         if (base->object->nlaflag & OB_NLA_COLLAPSED)
1577                                 continue;
1578                         
1579                         ymax=ymin;
1580                         
1581                         /* Handle action ipos */
1582                         if (base->object->action){
1583                                 bAction *act= base->object->action;
1584                                 
1585                                 /* if action is mapped in NLA, it returns a correction */
1586                                 xmin= get_action_frame(base->object, rectf.xmin);
1587                                 xmax= get_action_frame(base->object, rectf.xmax);
1588                                 
1589                                 ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1590                                 if (!((ymax < rectf.ymin) || (ymin > rectf.ymax))){
1591                                         for (chan=act->chanbase.first; chan; chan=chan->next){
1592                                                 if(chan->ipo) {
1593                                                         for (icu=chan->ipo->curve.first; icu; icu=icu->next){
1594                                                                 for (i=0; i<icu->totvert; i++){
1595                                                                         if (icu->bezt[i].vec[1][0] > xmin && icu->bezt[i].vec[1][0] <= xmax ){
1596                                                                                 if (!firstact){
1597                                                                                         firstact= act;
1598                                                                                         firstvert=icu->bezt[i].vec[1][0];
1599                                                                                         *sel = icu->bezt[i].f2 & 1;     
1600                                                                                 }
1601                                                                                 
1602                                                                                 if (icu->bezt[i].f2 & 1){ 
1603                                                                                         if (!foundsel){
1604                                                                                                 foundsel=1;
1605                                                                                                 foundx = icu->bezt[i].vec[1][0];
1606                                                                                         }
1607                                                                                 }
1608                                                                                 else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
1609                                                                                         *index=icu->bezt[i].vec[1][0];
1610                                                                                         *sel = 0;
1611                                                                                         return act;
1612                                                                                 }
1613                                                                         }
1614                                                                 }
1615                                                         }
1616                                                 }
1617                                                 
1618                                                 for (conchan=chan->constraintChannels.first; conchan; conchan=conchan->next){
1619                                                         ymin=ymax-(NLACHANNELHEIGHT+NLACHANNELSKIP);
1620                                                         if ( !((ymax < rectf.ymin) || (ymin > rectf.ymax)) && conchan->ipo){
1621                                                                 for (icu=conchan->ipo->curve.first; icu; icu=icu->next){
1622                                                                         for (i=0; i<icu->totvert; i++){
1623                                                                                 if (icu->bezt[i].vec[1][0] > xmin && icu->bezt[i].vec[1][0] <= xmax ){
1624                                                                                         if (!firstact){
1625                                                                                                 firstact=base->object->action;
1626                                                                                                 firstvert=icu->bezt[i].vec[1][0];
1627                                                                                                 *sel = icu->bezt[i].f2 & 1;     
1628                                                                                         }
1629                                                                                         
1630                                                                                         if (icu->bezt[i].f2 & 1){ 
1631                                                                                                 if (!foundsel){
1632                                                                                                         foundsel=1;
1633                                                                                                         foundx = icu->bezt[i].vec[1][0];
1634                                                                                                 }
1635                                                                                         }
1636                                                                                         else if (foundsel && icu->bezt[i].vec[1][0] != foundx){
1637                                                                                                 *index=icu->bezt[i].vec[1][0];
1638                                                                                                 *sel = 0;
1639                                                                                                 return base->object->action;
1640                                                                                         }
1641                                                                                 }
1642                                                                         }
1643                                                                 }
1644                                                         }
1645                                                         ymax=ymin;
1646                                                 }
1647                                         
1648                                         
1649                                         }
1650                                 }                       
1651                                 ymax=ymin;
1652                         }
1653                         
1654                         /* Skip nlastrips */
1655                         ymax-=(NLACHANNELHEIGHT+NLACHANNELSKIP)*BLI_countlist(&base->object->nlastrips);
1656                 }
1657         }       
1658         
1659         *index=firstvert;
1660         return firstact;
1661 }
1662
1663 void deselect_nlachannels(int test)
1664 {
1665         Base *base;
1666         int sel = 1;
1667
1668         if (test){
1669                 for (base=G.scene->base.first; base; base=base->next){
1670                         /* Check base flags for previous selection */
1671                         if (base->flag & SELECT){
1672                                 sel=0;
1673                                 break;
1674                         }
1675                 }
1676         }
1677         else
1678                 sel = 0;
1679
1680         /* Select objects */
1681         for (base=G.scene->base.first; base; base=base->next){
1682                 if (sel){
1683                         if (nla_filter(base))
1684                                 base->flag |= SELECT;
1685                 }
1686                 else
1687                         base->flag &= ~SELECT;
1688                 
1689                 base->object->flag= base->flag;
1690         }       
1691 }
1692
1693 static Object *get_object_from_active_strip(void) {
1694
1695         Base *base;
1696         bActionStrip *strip;
1697         
1698         for (base=G.scene->base.first; base; base=base->next) {
1699                 if ((base->object->nlaflag & OB_NLA_COLLAPSED)==0) {
1700                         for (strip = base->object->nlastrips.first; 
1701                                  strip; strip=strip->next){
1702                                 if (strip->flag & ACTSTRIP_SELECT) {
1703                                         return base->object;
1704                                 }
1705                         }
1706                 }
1707         }
1708         return NULL;
1709 }
1710
1711
1712 void winqreadnlaspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
1713 {
1714         unsigned short event= evt->event;
1715         short val= evt->val;
1716         SpaceNla *snla = curarea->spacedata.first;
1717         int doredraw= 0;
1718         short   mval[2];
1719         float dx,dy;
1720         int     cfra;
1721         short mousebut = L_MOUSE;
1722         Object          *ob; //in shift-B / bake
1723         
1724         if (curarea->win==0) return;
1725         if (!snla) return;
1726         
1727         if(val) {
1728                 if( uiDoBlocks(&curarea->uiblocks, event, 1)!=UI_NOTHING ) event= 0;
1729                 
1730                 /* swap mouse buttons based on user preference */
1731                 if (U.flag & USER_LMOUSESELECT) {
1732                         if (event == LEFTMOUSE) {
1733                                 event = RIGHTMOUSE;
1734                                 mousebut = L_MOUSE;
1735                         } else if (event == RIGHTMOUSE) {
1736                                 event = LEFTMOUSE;
1737                                 mousebut = R_MOUSE;
1738                         }
1739                 }
1740                 
1741                 getmouseco_areawin(mval);
1742                 
1743                 switch(event) {
1744                         case UI_BUT_EVENT:
1745                                 do_nlabuts(val); // in drawnla.c
1746                                 break;
1747                                 
1748                         case HOMEKEY:
1749                                 do_nla_buttons(B_NLAHOME);
1750                                 break;
1751                                 
1752                         case EQUALKEY:
1753                                 shift_nlastrips_up();
1754                                 break;
1755                         
1756                         case PAGEUPKEY:
1757                                 if (G.qual & LR_CTRLKEY)
1758                                         shift_nlastrips_up();
1759                                 else {
1760                                         nextprev_marker(1);
1761                                         allqueue(REDRAWMARKER, 0);
1762                                 }                               
1763                                 break;
1764                                 
1765                         case MINUSKEY:
1766                                 shift_nlastrips_down();
1767                                 break;
1768                                 
1769                         case PAGEDOWNKEY:
1770                                 if (G.qual & LR_CTRLKEY)
1771                                         shift_nlastrips_down();
1772                                 else {
1773                                         nextprev_marker(-1);
1774                                         allqueue(REDRAWMARKER, 0);
1775                                 }
1776                                 break;
1777                                 
1778                         case AKEY:
1779                                 if (G.qual & LR_SHIFTKEY){
1780                                         add_nlablock();
1781                                         allqueue (REDRAWNLA, 0);
1782                                         allqueue (REDRAWVIEW3D, 0);
1783                                 }
1784                                 else if (G.qual & LR_CTRLKEY) {
1785                                         deselect_markers(1, 0);
1786                                         allqueue(REDRAWMARKER, 0);
1787                                 }
1788                                 else{
1789                                         if (mval[0]>=NLAWIDTH)
1790                                                 deselect_nlachannel_keys(1);
1791                                         else{
1792                                                 deselect_nlachannels(1);
1793                                                 allqueue (REDRAWVIEW3D, 0);
1794                                         }
1795                                         allqueue (REDRAWNLA, 0);
1796                                         allqueue (REDRAWIPO, 0);
1797                                         BIF_undo_push("(De)select all NLA");
1798                                 }
1799                                 break;
1800                                 
1801                         case BKEY:
1802                                 if (G.qual & LR_SHIFTKEY){
1803                                         bake_all_to_action();
1804                                         allqueue (REDRAWNLA, 0);
1805                                     allqueue (REDRAWVIEW3D, 0);
1806                                     BIF_undo_push("Bake All To Action");
1807                                     ob = get_object_from_active_strip();
1808                                     //build_match_caches(ob);
1809                                 }
1810                                 else if (G.qual & LR_CTRLKEY) 
1811                                         borderselect_markers();
1812                                 else
1813                                         borderselect_nla();
1814                                 break;
1815                                 
1816                         case CKEY:
1817                                 if(G.qual==LR_CTRLKEY) {
1818                                         if(okee("Copy Modifiers"))
1819                                                 copy_action_modifiers();
1820                                 }
1821                                 else convert_nla();
1822                                 break;
1823                                 
1824                         case DKEY:
1825                                 if (G.qual == (LR_CTRLKEY|LR_SHIFTKEY) && mval[0]>=NLAWIDTH) {
1826                                         duplicate_marker();
1827                                 }
1828                                 else if (G.qual & LR_SHIFTKEY && mval[0]>=NLAWIDTH){
1829                                         duplicate_nlachannel_keys();
1830                                         update_for_newframe_muted();
1831                                 }
1832                                 
1833                                 break;
1834                                 
1835                         case EKEY:
1836                                 if (mval[0] >= NLAWIDTH) {
1837                                         transform_nlachannel_keys ('e', 0);
1838                                         update_for_newframe_muted();
1839                                 }
1840                                 break;
1841                                 
1842                         case GKEY:
1843                                 if (mval[0]>=NLAWIDTH) {
1844                                         if (G.qual & LR_CTRLKEY) {
1845                                                 transform_markers('g', 0);
1846                                         }
1847                                         else {
1848                                                 transform_nlachannel_keys ('g', 0);
1849                                                 update_for_newframe_muted();
1850                                         }
1851                                 }
1852                                 break;
1853                         
1854                         case MKEY:
1855                                 /* marker operations */
1856                                 if (G.qual == 0)
1857                                         add_marker(CFRA);
1858                                 else if (G.qual == LR_CTRLKEY)
1859                                         rename_marker();
1860                                 else 
1861                                         break;
1862                                 allqueue(REDRAWMARKER, 0);
1863                                 break;                          
1864                         
1865                         case NKEY:
1866                                 if(G.qual==0) {
1867                                         toggle_blockhandler(curarea, NLA_HANDLER_PROPERTIES, UI_PNL_TO_MOUSE);
1868                                         scrarea_queue_winredraw(curarea);
1869                                 }
1870                                 else if (G.qual & LR_SHIFTKEY) {
1871                                         add_empty_nlablock();
1872                                 }
1873                                 break;
1874                         case LKEY:
1875                                 relink_active_strip();
1876                                 break;
1877                                 
1878                         case PKEY:
1879                                 if (G.qual & LR_CTRLKEY) /* set preview range */
1880                                         anim_previewrange_set();
1881                                 else if (G.qual & LR_ALTKEY) /* clear preview range */
1882                                         anim_previewrange_clear();
1883                                 allqueue(REDRAWMARKER, 0);
1884                                 break;
1885                                 
1886                         case SKEY:
1887                                 if (G.qual==LR_ALTKEY) {
1888                                         val= pupmenu("Action Strip Scale%t|Reset Strip Scale%x1|Remap Action Start/End%x2|Apply Scale%x3");
1889                                         if (val > 0)
1890                                                 reset_action_strips(val);
1891                                 }
1892                                 else if (G.qual & LR_SHIFTKEY) {
1893                                         if (snla->flag & SNLA_DRAWTIME)
1894                                                 val= pupmenu("Snap To%t|Nearest Second%x3|Current Time%x2");
1895                                         else
1896                                                 val= pupmenu("Snap To%t|Nearest Frame%x1|Current Frame%x2");
1897                                         if (ELEM3(val, 1, 2, 3))
1898                                                 snap_action_strips(val);
1899                                 }
1900                                 else {
1901                                         if (mval[0]>=NLAWIDTH)
1902                                                 transform_nlachannel_keys ('s', 0);
1903                                         update_for_newframe_muted();
1904                                 }
1905                                 break;
1906                                 
1907                         case TKEY:
1908                                 if (G.qual & LR_CTRLKEY) {
1909                                         val= pupmenu("Time value%t|Frames %x1|Seconds%x2");
1910                                         
1911                                         if (val > 0) {
1912                                                 if (val == 2) snla->flag |= SNLA_DRAWTIME;
1913                                                 else snla->flag &= ~SNLA_DRAWTIME;
1914                                                 
1915                                                 doredraw= 1;
1916                                         }
1917                                 }                               
1918                                 break;
1919                                 
1920                         case DELKEY:
1921                         case XKEY:
1922                                 if (mval[0]>=NLAWIDTH) {
1923                                         if (okee("Erase selected?")) {
1924                                                 delete_nlachannel_keys();
1925                                                 update_for_newframe_muted();
1926                                                 
1927                                                 remove_marker();
1928                                                 
1929                                                 allqueue(REDRAWMARKER, 0);
1930                                         }
1931                                 }
1932                                 break;
1933                                 
1934                                 /* LEFTMOUSE and RIGHTMOUSE event codes can be swapped above,
1935                                 * based on user preference USER_LMOUSESELECT
1936                                 */
1937                         case LEFTMOUSE:
1938                                 if(view2dmove(LEFTMOUSE))
1939                                         break; // only checks for sliders
1940                                 else if (mval[0]>=snla->v2d.mask.xmin) {
1941                                         do {
1942                                                 getmouseco_areawin(mval);
1943                                                 
1944                                                 areamouseco_to_ipoco(G.v2d, mval, &dx, &dy);
1945                                                 
1946                                                 cfra= (int)(dx+0.5f);
1947                                                 if(cfra< 1) cfra= 1;
1948                                                 
1949                                                 if( cfra!=CFRA ) {
1950                                                         CFRA= cfra;
1951                                                         update_for_newframe();
1952                                                         force_draw_all(0);
1953                                                 }
1954                                                 else PIL_sleep_ms(30);
1955                                                 
1956                                         } while(get_mbut() & mousebut);
1957                                         break;
1958                                 }
1959                                         /* else pass on! */
1960                                 case RIGHTMOUSE:
1961                                         if (mval[0]>=snla->v2d.mask.xmin) {
1962                                                 if(G.qual & LR_SHIFTKEY)
1963                                                         mouse_nla(SELECT_INVERT);
1964                                                 else
1965                                                         mouse_nla(SELECT_REPLACE);
1966                                         }
1967                                         else
1968                                                 mouse_nlachannels(mval);
1969                                         break;
1970                                         
1971                                 case PADPLUSKEY:
1972                                         view2d_zoom(G.v2d, 0.1154, sa->winx, sa->winy);
1973                                         test_view2d(G.v2d, sa->winx, sa->winy);
1974                                         view2d_do_locks(curarea, V2D_LOCK_COPY);
1975                                         doredraw= 1;
1976                                         break;
1977                                 case PADMINUS:
1978                                         view2d_zoom(G.v2d, -0.15, sa->winx, sa->winy);
1979                                         test_view2d(G.v2d, sa->winx, sa->winy);
1980                                         view2d_do_locks(curarea, V2D_LOCK_COPY);
1981                                         doredraw= 1;
1982                                         break;
1983                                 case MIDDLEMOUSE:
1984                                 case WHEELUPMOUSE:
1985                                 case WHEELDOWNMOUSE:
1986                                         view2dmove(event);      /* in drawipo.c */
1987                                         break;
1988                 }
1989         }
1990         
1991         if(doredraw) scrarea_queue_winredraw(curarea);
1992 }
1993
1994 void bake_all_to_action(void)
1995 {
1996         Object          *ob;
1997         bAction         *newAction;
1998         Ipo             *ipo;
1999         ID              *id;
2000         short           hold, add;
2001         float           repeat;
2002
2003         /* burn object-level motion into a new action */
2004         ob = get_object_from_active_strip();
2005         if (ob) {
2006                 if (ob->flag&OB_ARMATURE) {
2007                         //newAction = bake_obIPO_to_action(ob);
2008                         newAction = NULL;
2009                         if (newAction) {
2010                                 /* unlink the object's IPO */
2011                                 ipo=ob->ipo;
2012                                 if (ipo) {
2013                                         id = &ipo->id;
2014                                         if (id->us > 0)
2015                                                 id->us--;
2016                                         ob->ipo = NULL;
2017                                 }
2018                                 
2019                                 /* add the new Action to NLA as a strip */
2020                                 hold=1;
2021                                 add=1;
2022                                 repeat=1.0;
2023                                 printf("about to add nla block...\n");
2024                                 add_nla_block_by_name(newAction->id.name, ob, hold, add, repeat);
2025                                 BIF_undo_push("Add NLA strip");
2026                         }
2027                 }
2028         }
2029 }
2030
2031 void copy_action_modifiers(void)
2032 {
2033         bActionStrip *strip, *actstrip;
2034         Object *ob= OBACT;
2035         
2036         if(ob==NULL)
2037                 return;
2038         
2039         /* active strip */
2040         for (actstrip=ob->nlastrips.first; actstrip; actstrip=actstrip->next)
2041                 if(actstrip->flag & ACTSTRIP_ACTIVE)
2042                         break;
2043         if(actstrip==NULL)
2044                 return;
2045         
2046         /* copy to selected items */
2047         for (strip=ob->nlastrips.first; strip; strip=strip->next){
2048                 if (strip->flag & ACTSTRIP_SELECT) {
2049                         if(strip!=actstrip) {
2050                                 if (strip->modifiers.first)
2051                                         BLI_freelistN(&strip->modifiers);
2052                                 if (actstrip->modifiers.first)
2053                                         duplicatelist (&strip->modifiers, &actstrip->modifiers);
2054                         }
2055                 }
2056         }
2057         
2058         BIF_undo_push("Copy Action Modifiers");
2059         allqueue(REDRAWNLA, 0);
2060         DAG_scene_flush_update(G.scene, screen_view3d_layers(), 0);
2061 }
2062