Merge branch 'blender2.7'
[blender.git] / source / blender / editors / space_nla / nla_channels.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 Blender Foundation, Joshua Leung
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Joshua Leung (major recode)
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_nla/nla_channels.c
28  *  \ingroup spnla
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <math.h>
36
37 #include "DNA_anim_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_utildefines.h"
43
44 #include "BKE_animsys.h"
45 #include "BKE_nla.h"
46 #include "BKE_context.h"
47 #include "BKE_global.h"
48 #include "BKE_scene.h"
49 #include "BKE_screen.h"
50 #include "BKE_report.h"
51
52 #include "ED_anim_api.h"
53 #include "ED_keyframes_edit.h"
54 #include "ED_object.h"
55 #include "ED_screen.h"
56
57 #include "RNA_access.h"
58 #include "RNA_define.h"
59
60 #include "WM_api.h"
61 #include "WM_types.h"
62
63 #include "DEG_depsgraph.h"
64 #include "DEG_depsgraph_build.h"
65
66 #include "UI_view2d.h"
67
68 #include "nla_intern.h" // own include
69
70 /* *********************************************** */
71 /* Operators for NLA channels-list which need to be different
72  * from the standard Animation Editor ones */
73
74 /* ******************** Mouse-Click Operator *********************** */
75 /* Depending on the channel that was clicked on, the mouse click will activate whichever
76  * part of the channel is relevant.
77  *
78  * NOTE: eventually, this should probably be phased out when many of these things are replaced with buttons
79  * --> Most channels are now selection only...
80  */
81
82 static int mouse_nla_channels(bContext *C, bAnimContext *ac, float x, int channel_index, short selectmode)
83 {
84         ListBase anim_data = {NULL, NULL};
85         bAnimListElem *ale;
86         int filter;
87
88         View2D *v2d = &ac->ar->v2d;
89         int notifierFlags = 0;
90
91         /* get the channel that was clicked on */
92         /* filter channels */
93         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
94         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
95
96         /* get channel from index */
97         ale = BLI_findlink(&anim_data, channel_index);
98         if (ale == NULL) {
99                 /* channel not found */
100                 if (G.debug & G_DEBUG)
101                         printf("Error: animation channel (index = %d) not found in mouse_anim_channels()\n", channel_index);
102
103                 ANIM_animdata_freelist(&anim_data);
104                 return 0;
105         }
106
107         /* action to take depends on what channel we've got */
108         // WARNING: must keep this in sync with the equivalent function in anim_channels_edit.c
109         switch (ale->type) {
110                 case ANIMTYPE_SCENE:
111                 {
112                         Scene *sce = (Scene *)ale->data;
113                         AnimData *adt = sce->adt;
114
115                         /* set selection status */
116                         if (selectmode == SELECT_INVERT) {
117                                 /* swap select */
118                                 sce->flag ^= SCE_DS_SELECTED;
119                                 if (adt) adt->flag ^= ADT_UI_SELECTED;
120                         }
121                         else {
122                                 sce->flag |= SCE_DS_SELECTED;
123                                 if (adt) adt->flag |= ADT_UI_SELECTED;
124                         }
125
126                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
127                         break;
128                 }
129                 case ANIMTYPE_OBJECT:
130                 {
131                         ViewLayer *view_layer = ac->view_layer;
132                         Base *base = (Base *)ale->data;
133                         Object *ob = base->object;
134                         AnimData *adt = ob->adt;
135
136                         if (nlaedit_is_tweakmode_on(ac) == 0 && (base->flag & BASE_SELECTABLE)) {
137                                 /* set selection status */
138                                 if (selectmode == SELECT_INVERT) {
139                                         /* swap select */
140                                         ED_object_base_select(base, BA_INVERT);
141                                         BKE_scene_object_base_flag_sync_from_base(base);
142
143                                         if (adt) adt->flag ^= ADT_UI_SELECTED;
144                                 }
145                                 else {
146                                         /* deselect all */
147                                         /* TODO: should this deselect all other types of channels too? */
148                                         for (Base *b = view_layer->object_bases.first; b; b = b->next) {
149                                                 ED_object_base_select(b, BA_DESELECT);
150                                                 BKE_scene_object_base_flag_sync_from_base(b);
151                                                 if (b->object->adt) b->object->adt->flag &= ~(ADT_UI_SELECTED | ADT_UI_ACTIVE);
152                                         }
153
154                                         /* select object now */
155                                         ED_object_base_select(base, BA_SELECT);
156                                         BKE_scene_object_base_flag_sync_from_base(base);
157                                         if (adt) adt->flag |= ADT_UI_SELECTED;
158                                 }
159
160                                 /* change active object - regardless of whether it is now selected [T37883] */
161                                 ED_object_base_activate(C, base); /* adds notifier */
162
163                                 if ((adt) && (adt->flag & ADT_UI_SELECTED))
164                                         adt->flag |= ADT_UI_ACTIVE;
165
166                                 /* notifiers - channel was selected */
167                                 notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
168                         }
169                         break;
170                 }
171                 case ANIMTYPE_FILLACTD: /* Action Expander */
172                 case ANIMTYPE_DSMAT:    /* Datablock AnimData Expanders */
173                 case ANIMTYPE_DSLAM:
174                 case ANIMTYPE_DSCAM:
175                 case ANIMTYPE_DSCACHEFILE:
176                 case ANIMTYPE_DSCUR:
177                 case ANIMTYPE_DSSKEY:
178                 case ANIMTYPE_DSWOR:
179                 case ANIMTYPE_DSNTREE:
180                 case ANIMTYPE_DSPART:
181                 case ANIMTYPE_DSMBALL:
182                 case ANIMTYPE_DSARM:
183                 case ANIMTYPE_DSMESH:
184                 case ANIMTYPE_DSTEX:
185                 case ANIMTYPE_DSLAT:
186                 case ANIMTYPE_DSLINESTYLE:
187                 case ANIMTYPE_DSSPK:
188                 case ANIMTYPE_DSGPENCIL:
189                 case ANIMTYPE_PALETTE:
190                 {
191                         /* sanity checking... */
192                         if (ale->adt) {
193                                 /* select/deselect */
194                                 if (selectmode == SELECT_INVERT) {
195                                         /* inverse selection status of this AnimData block only */
196                                         ale->adt->flag ^= ADT_UI_SELECTED;
197                                 }
198                                 else {
199                                         /* select AnimData block by itself */
200                                         ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
201                                         ale->adt->flag |= ADT_UI_SELECTED;
202                                 }
203
204                                 /* set active? */
205                                 if ((ale->adt) && (ale->adt->flag & ADT_UI_SELECTED))
206                                         ale->adt->flag |= ADT_UI_ACTIVE;
207                         }
208
209                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
210                         break;
211                 }
212                 case ANIMTYPE_NLATRACK:
213                 {
214                         NlaTrack *nlt = (NlaTrack *)ale->data;
215                         AnimData *adt = ale->adt;
216                         short offset;
217
218                         /* offset for start of channel (on LHS of channel-list) */
219                         if (ale->id) {
220                                 /* special exception for materials and particles */
221                                 if (ELEM(GS(ale->id->name), ID_MA, ID_PA))
222                                         offset = 21 + NLACHANNEL_BUTTON_WIDTH;
223                                 else
224                                         offset = 14;
225                         }
226                         else
227                                 offset = 0;
228
229                         if (x >= (v2d->cur.xmax - NLACHANNEL_BUTTON_WIDTH)) {
230                                 /* toggle protection (only if there's a toggle there) */
231                                 nlt->flag ^= NLATRACK_PROTECTED;
232
233                                 /* notifier flags - channel was edited */
234                                 notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
235                         }
236                         else if (x >= (v2d->cur.xmax - 2 * NLACHANNEL_BUTTON_WIDTH)) {
237                                 /* toggle mute */
238                                 nlt->flag ^= NLATRACK_MUTED;
239
240                                 /* notifier flags - channel was edited */
241                                 notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
242                                 ale->update |= ANIM_UPDATE_DEPS;
243                         }
244                         else if (x <= ((NLACHANNEL_BUTTON_WIDTH * 2) + offset)) {
245                                 /* toggle 'solo' */
246                                 BKE_nlatrack_solo_toggle(adt, nlt);
247
248                                 /* notifier flags - channel was edited */
249                                 notifierFlags |= (ND_ANIMCHAN | NA_EDITED);
250                                 ale->update |= ANIM_UPDATE_DEPS;
251                         }
252                         else if (nlaedit_is_tweakmode_on(ac) == 0) {
253                                 /* set selection */
254                                 if (selectmode == SELECT_INVERT) {
255                                         /* inverse selection status of this F-Curve only */
256                                         nlt->flag ^= NLATRACK_SELECTED;
257                                 }
258                                 else {
259                                         /* select F-Curve by itself */
260                                         ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
261                                         nlt->flag |= NLATRACK_SELECTED;
262                                 }
263
264                                 /* if NLA-Track is selected now,
265                                  * make NLA-Track the 'active' one in the visible list */
266                                 if (nlt->flag & NLATRACK_SELECTED)
267                                         ANIM_set_active_channel(ac, ac->data, ac->datatype, filter, nlt, ANIMTYPE_NLATRACK);
268
269                                 /* notifier flags - channel was selected */
270                                 notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
271                         }
272                         break;
273                 }
274                 case ANIMTYPE_NLAACTION:
275                 {
276                         AnimData *adt = BKE_animdata_from_id(ale->id);
277
278                         /* button region... */
279                         if (x >= (v2d->cur.xmax - NLACHANNEL_BUTTON_WIDTH)) {
280                                 if (nlaedit_is_tweakmode_on(ac) == 0) {
281                                         /* 'push-down' action - only usable when not in TweakMode */
282                                         /* TODO: make this use the operator instead of calling the function directly
283                                          * however, calling the operator requires that we supply the args,
284                                          * and that works with proper buttons only */
285                                         BKE_nla_action_pushdown(adt);
286                                 }
287                                 else {
288                                         /* when in tweakmode, this button becomes the toggle for mapped editing */
289                                         adt->flag ^= ADT_NLA_EDIT_NOMAP;
290                                 }
291
292                                 /* changes to NLA-Action occurred */
293                                 notifierFlags |= ND_NLA_ACTCHANGE;
294                                 ale->update |= ANIM_UPDATE_DEPS;
295                         }
296                         /* OR rest of name... */
297                         else {
298                                 /* NOTE: rest of NLA-Action name doubles for operating on the AnimData block
299                                  * - this is useful when there's no clear divider, and makes more sense in
300                                  *   the case of users trying to use this to change actions
301                                  * - in tweakmode, clicking here gets us out of tweakmode, as changing selection
302                                  *   while in tweakmode is really evil!
303                                  * - we disable "solo" flags too, to make it easier to work with stashed actions
304                                  *   with less trouble
305                                  */
306                                 if (nlaedit_is_tweakmode_on(ac)) {
307                                         /* exit tweakmode immediately */
308                                         nlaedit_disable_tweakmode(ac, true);
309
310                                         /* changes to NLA-Action occurred */
311                                         notifierFlags |= ND_NLA_ACTCHANGE;
312                                         ale->update |= ANIM_UPDATE_DEPS;
313                                 }
314                                 else {
315                                         /* select/deselect */
316                                         if (selectmode == SELECT_INVERT) {
317                                                 /* inverse selection status of this AnimData block only */
318                                                 adt->flag ^= ADT_UI_SELECTED;
319                                         }
320                                         else {
321                                                 /* select AnimData block by itself */
322                                                 ANIM_deselect_anim_channels(ac, ac->data, ac->datatype, 0, ACHANNEL_SETFLAG_CLEAR);
323                                                 adt->flag |= ADT_UI_SELECTED;
324                                         }
325
326                                         /* set active? */
327                                         if (adt->flag & ADT_UI_SELECTED)
328                                                 adt->flag |= ADT_UI_ACTIVE;
329
330                                         notifierFlags |= (ND_ANIMCHAN | NA_SELECTED);
331                                 }
332                         }
333                         break;
334                 }
335                 default:
336                         if (G.debug & G_DEBUG)
337                                 printf("Error: Invalid channel type in mouse_nla_channels()\n");
338                         break;
339         }
340
341         /* free channels */
342         ANIM_animdata_update(ac, &anim_data);
343         ANIM_animdata_freelist(&anim_data);
344
345         /* return the notifier-flags set */
346         return notifierFlags;
347 }
348
349 /* ------------------- */
350
351 /* handle clicking */
352 static int nlachannels_mouseclick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
353 {
354         bAnimContext ac;
355         SpaceNla *snla;
356         ARegion *ar;
357         View2D *v2d;
358         int channel_index;
359         int notifierFlags = 0;
360         short selectmode;
361         float x, y;
362
363         /* get editor data */
364         if (ANIM_animdata_get_context(C, &ac) == 0)
365                 return OPERATOR_CANCELLED;
366
367         /* get useful pointers from animation context data */
368         snla = (SpaceNla *)ac.sl;
369         ar = ac.ar;
370         v2d = &ar->v2d;
371
372         /* select mode is either replace (deselect all, then add) or add/extend */
373         if (RNA_boolean_get(op->ptr, "extend"))
374                 selectmode = SELECT_INVERT;
375         else
376                 selectmode = SELECT_REPLACE;
377
378         /* figure out which channel user clicked in
379          * Note: although channels technically start at y= NLACHANNEL_FIRST, we need to adjust by half a channel's height
380          *      so that the tops of channels get caught ok. Since NLACHANNEL_FIRST is really NLACHANNEL_HEIGHT, we simply use
381          *      NLACHANNEL_HEIGHT_HALF.
382          */
383         UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &x, &y);
384         UI_view2d_listview_view_to_cell(v2d, NLACHANNEL_NAMEWIDTH, NLACHANNEL_STEP(snla), 0, (float)NLACHANNEL_HEIGHT_HALF(snla), x, y, NULL, &channel_index);
385
386         /* handle mouse-click in the relevant channel then */
387         notifierFlags = mouse_nla_channels(C, &ac, x, channel_index, selectmode);
388
389         /* set notifier that things have changed */
390         WM_event_add_notifier(C, NC_ANIMATION | notifierFlags, NULL);
391
392         return OPERATOR_FINISHED;
393 }
394
395 void NLA_OT_channels_click(wmOperatorType *ot)
396 {
397         PropertyRNA *prop;
398
399         /* identifiers */
400         ot->name = "Mouse Click on NLA Channels";
401         ot->idname = "NLA_OT_channels_click";
402         ot->description = "Handle clicks to select NLA channels";
403
404         /* api callbacks */
405         ot->invoke = nlachannels_mouseclick_invoke;
406         ot->poll = ED_operator_nla_active;
407
408         /* flags */
409         ot->flag = OPTYPE_UNDO;
410
411         /* props */
412         prop = RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", ""); // SHIFTKEY
413         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
414 }
415
416 /* *********************************************** */
417 /* Special Operators */
418
419 /* ******************** Action Push Down ******************************** */
420
421 static int nlachannels_pushdown_exec(bContext *C, wmOperator *op)
422 {
423         bAnimContext ac;
424         ID *id = NULL;
425         AnimData *adt = NULL;
426         int channel_index = RNA_int_get(op->ptr, "channel_index");
427
428         /* get editor data */
429         if (ANIM_animdata_get_context(C, &ac) == 0)
430                 return OPERATOR_CANCELLED;
431
432         /* get anim-channel to use (or more specifically, the animdata block behind it) */
433         if (channel_index == -1) {
434                 PointerRNA adt_ptr = {{NULL}};
435
436                 /* active animdata block */
437                 if (nla_panel_context(C, &adt_ptr, NULL, NULL) == 0 || (adt_ptr.data == NULL)) {
438                         BKE_report(op->reports, RPT_ERROR, "No active AnimData block to use "
439                                    "(select a data-block expander first or set the appropriate flags on an AnimData block)");
440                         return OPERATOR_CANCELLED;
441                 }
442                 else {
443                         id = adt_ptr.id.data;
444                         adt = adt_ptr.data;
445                 }
446         }
447         else {
448                 /* indexed channel */
449                 ListBase anim_data = {NULL, NULL};
450                 bAnimListElem *ale;
451                 int filter;
452
453                 /* filter channels */
454                 filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_LIST_CHANNELS);
455                 ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
456
457                 /* get channel from index */
458                 ale = BLI_findlink(&anim_data, channel_index);
459                 if (ale == NULL) {
460                         BKE_reportf(op->reports, RPT_ERROR, "No animation channel found at index %d", channel_index);
461                         ANIM_animdata_freelist(&anim_data);
462                         return OPERATOR_CANCELLED;
463                 }
464                 else if (ale->type != ANIMTYPE_NLAACTION) {
465                         BKE_reportf(op->reports, RPT_ERROR, "Animation channel at index %d is not a NLA 'Active Action' channel", channel_index);
466                         ANIM_animdata_freelist(&anim_data);
467                         return OPERATOR_CANCELLED;
468                 }
469
470                 /* grab AnimData from the channel */
471                 adt = ale->adt;
472                 id = ale->id;
473
474                 /* we don't need anything here anymore, so free it all */
475                 ANIM_animdata_freelist(&anim_data);
476         }
477
478         /* double-check that we are free to push down here... */
479         if (adt == NULL) {
480                 BKE_report(op->reports, RPT_WARNING, "Internal Error - AnimData block is not valid");
481                 return OPERATOR_CANCELLED;
482         }
483         else if (nlaedit_is_tweakmode_on(&ac)) {
484                 BKE_report(op->reports, RPT_WARNING,
485                            "Cannot push down actions while tweaking a strip's action, exit tweak mode first");
486                 return OPERATOR_CANCELLED;
487         }
488         else if (adt->action == NULL) {
489                 BKE_report(op->reports, RPT_WARNING, "No active action to push down");
490                 return OPERATOR_CANCELLED;
491         }
492         else {
493                 /* 'push-down' action - only usable when not in TweakMode */
494                 BKE_nla_action_pushdown(adt);
495
496                 DEG_id_tag_update_ex(CTX_data_main(C), id, ID_RECALC_ANIMATION | ID_RECALC_COPY_ON_WRITE);
497         }
498
499         /* set notifier that things have changed */
500         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA_ACTCHANGE, NULL);
501         return OPERATOR_FINISHED;
502 }
503
504 void NLA_OT_action_pushdown(wmOperatorType *ot)
505 {
506         /* identifiers */
507         ot->name = "Push Down Action";
508         ot->idname = "NLA_OT_action_pushdown";
509         ot->description = "Push action down onto the top of the NLA stack as a new strip";
510
511         /* callbacks */
512         ot->exec = nlachannels_pushdown_exec;
513         ot->poll = nlaop_poll_tweakmode_off;
514
515         /* flags */
516         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
517
518         /* properties */
519         ot->prop = RNA_def_int(ot->srna, "channel_index", -1, -1, INT_MAX, "Channel Index",
520                                "Index of NLA action channel to perform pushdown operation on",
521                                0, INT_MAX);
522         RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE);
523 }
524
525 /* ******************** Action Unlink ******************************** */
526
527 static bool nla_action_unlink_poll(bContext *C)
528 {
529         if (ED_operator_nla_active(C)) {
530                 return nla_panel_context(C, NULL, NULL, NULL);
531         }
532
533         /* something failed... */
534         return false;
535 }
536
537 static int nla_action_unlink_exec(bContext *C, wmOperator *op)
538 {
539         PointerRNA adt_ptr;
540         AnimData *adt;
541
542         /* check context and also validity of pointer */
543         if (!nla_panel_context(C, &adt_ptr, NULL, NULL))
544                 return OPERATOR_CANCELLED;
545
546         /* get animdata */
547         adt = adt_ptr.data;
548         if (adt == NULL)
549                 return OPERATOR_CANCELLED;
550
551         /* do unlinking */
552         if (adt && adt->action) {
553                 bool force_delete = RNA_boolean_get(op->ptr, "force_delete");
554                 ED_animedit_unlink_action(C, adt_ptr.id.data, adt, adt->action, op->reports, force_delete);
555         }
556
557         return OPERATOR_FINISHED;
558 }
559
560 static int nla_action_unlink_invoke(bContext *C, wmOperator *op, const wmEvent *evt)
561 {
562         /* NOTE: this is hardcoded to match the behavior for the unlink button
563          * (in interface_templates.c) */
564         RNA_boolean_set(op->ptr, "force_delete", evt->shift != 0);
565         return nla_action_unlink_exec(C, op);
566 }
567
568 void NLA_OT_action_unlink(wmOperatorType *ot)
569 {
570         PropertyRNA *prop;
571
572         /* identifiers */
573         ot->name = "Unlink Action";
574         ot->idname = "NLA_OT_action_unlink";
575         ot->description = "Unlink this action from the active action slot (and/or exit Tweak Mode)";
576
577         /* callbacks */
578         ot->invoke = nla_action_unlink_invoke;
579         ot->exec = nla_action_unlink_exec;
580         ot->poll = nla_action_unlink_poll;
581
582         /* properties */
583         prop = RNA_def_boolean(ot->srna, "force_delete", false, "Force Delete",
584                                "Clear Fake User and remove copy stashed in this datablock's NLA stack");
585         RNA_def_property_flag(prop, PROP_SKIP_SAVE);
586 }
587
588 /* ******************** Add Tracks Operator ***************************** */
589 /* Add NLA Tracks to the same AnimData block as a selected track, or above the selected tracks */
590
591 /* helper - add NLA Tracks alongside existing ones */
592 bool nlaedit_add_tracks_existing(bAnimContext *ac, bool above_sel)
593 {
594         ListBase anim_data = {NULL, NULL};
595         bAnimListElem *ale;
596         int filter;
597         AnimData *lastAdt = NULL;
598         bool added = false;
599
600         /* get a list of the (selected) NLA Tracks being shown in the NLA */
601         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_NODUPLIS);
602         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
603
604         /* add tracks... */
605         for (ale = anim_data.first; ale; ale = ale->next) {
606                 if (ale->type == ANIMTYPE_NLATRACK) {
607                         NlaTrack *nlt = (NlaTrack *)ale->data;
608                         AnimData *adt = ale->adt;
609
610                         /* check if just adding a new track above this one,
611                          * or whether we're adding a new one to the top of the stack that this one belongs to
612                          */
613                         if (above_sel) {
614                                 /* just add a new one above this one */
615                                 BKE_nlatrack_add(adt, nlt);
616                                 ale->update = ANIM_UPDATE_DEPS;
617                                 added = true;
618                         }
619                         else if ((lastAdt == NULL) || (adt != lastAdt)) {
620                                 /* add one track to the top of the owning AnimData's stack,
621                                  * then don't add anymore to this stack */
622                                 BKE_nlatrack_add(adt, NULL);
623                                 lastAdt = adt;
624                                 ale->update = ANIM_UPDATE_DEPS;
625                                 added = true;
626                         }
627                 }
628         }
629
630         /* free temp data */
631         ANIM_animdata_update(ac, &anim_data);
632         ANIM_animdata_freelist(&anim_data);
633
634         return added;
635 }
636
637 /* helper - add NLA Tracks to empty (and selected) AnimData blocks */
638 bool nlaedit_add_tracks_empty(bAnimContext *ac)
639 {
640         ListBase anim_data = {NULL, NULL};
641         bAnimListElem *ale;
642         int filter;
643         bool added = false;
644
645         /* get a list of the selected AnimData blocks in the NLA */
646         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_ANIMDATA | ANIMFILTER_SEL | ANIMFILTER_NODUPLIS);
647         ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);
648
649         /* check if selected AnimData blocks are empty, and add tracks if so... */
650         for (ale = anim_data.first; ale; ale = ale->next) {
651                 AnimData *adt = ale->adt;
652
653                 /* sanity check */
654                 BLI_assert(adt->flag & ADT_UI_SELECTED);
655
656                 /* ensure it is empty */
657                 if (BLI_listbase_is_empty(&adt->nla_tracks)) {
658                         /* add new track to this AnimData block then */
659                         BKE_nlatrack_add(adt, NULL);
660                         ale->update = ANIM_UPDATE_DEPS;
661                         added = true;
662                 }
663         }
664
665         /* cleanup */
666         ANIM_animdata_update(ac, &anim_data);
667         ANIM_animdata_freelist(&anim_data);
668
669         return added;
670 }
671
672 /* ----- */
673
674 static int nlaedit_add_tracks_exec(bContext *C, wmOperator *op)
675 {
676         bAnimContext ac;
677         bool above_sel = RNA_boolean_get(op->ptr, "above_selected");
678         bool op_done = false;
679
680         /* get editor data */
681         if (ANIM_animdata_get_context(C, &ac) == 0)
682                 return OPERATOR_CANCELLED;
683
684         /* perform adding in two passes - existing first so that we don't double up for empty */
685         op_done |= nlaedit_add_tracks_existing(&ac, above_sel);
686         op_done |= nlaedit_add_tracks_empty(&ac);
687
688         /* done? */
689         if (op_done) {
690                 DEG_relations_tag_update(CTX_data_main(C));
691
692                 /* set notifier that things have changed */
693                 WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
694
695                 /* done */
696                 return OPERATOR_FINISHED;
697         }
698         else {
699                 /* failed to add any tracks */
700                 BKE_report(op->reports, RPT_WARNING,
701                            "Select an existing NLA Track or an empty action line first");
702
703                 /* not done */
704                 return OPERATOR_CANCELLED;
705         }
706 }
707
708 void NLA_OT_tracks_add(wmOperatorType *ot)
709 {
710         /* identifiers */
711         ot->name = "Add Tracks";
712         ot->idname = "NLA_OT_tracks_add";
713         ot->description = "Add NLA-Tracks above/after the selected tracks";
714
715         /* api callbacks */
716         ot->exec = nlaedit_add_tracks_exec;
717         ot->poll = nlaop_poll_tweakmode_off;
718
719         /* flags */
720         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
721
722         /* properties */
723         RNA_def_boolean(ot->srna, "above_selected", 0, "Above Selected", "Add a new NLA Track above every existing selected one");
724 }
725
726 /* ******************** Delete Tracks Operator ***************************** */
727 /* Delete selected NLA Tracks */
728
729 static int nlaedit_delete_tracks_exec(bContext *C, wmOperator *UNUSED(op))
730 {
731         bAnimContext ac;
732
733         ListBase anim_data = {NULL, NULL};
734         bAnimListElem *ale;
735         int filter;
736
737         /* get editor data */
738         if (ANIM_animdata_get_context(C, &ac) == 0)
739                 return OPERATOR_CANCELLED;
740
741         /* get a list of the AnimData blocks being shown in the NLA */
742         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_NODUPLIS);
743         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
744
745         /* delete tracks */
746         for (ale = anim_data.first; ale; ale = ale->next) {
747                 if (ale->type == ANIMTYPE_NLATRACK) {
748                         NlaTrack *nlt = (NlaTrack *)ale->data;
749                         AnimData *adt = ale->adt;
750
751                         /* if track is currently 'solo', then AnimData should have its
752                          * 'has solo' flag disabled
753                          */
754                         if (nlt->flag & NLATRACK_SOLO)
755                                 adt->flag &= ~ADT_NLA_SOLO_TRACK;
756
757                         /* call delete on this track - deletes all strips too */
758                         BKE_nlatrack_free(&adt->nla_tracks, nlt, true);
759                         ale->update = ANIM_UPDATE_DEPS;
760                 }
761         }
762
763         /* free temp data */
764         ANIM_animdata_update(&ac, &anim_data);
765         ANIM_animdata_freelist(&anim_data);
766
767         DEG_relations_tag_update(ac.bmain);
768
769         /* set notifier that things have changed */
770         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
771
772         /* done */
773         return OPERATOR_FINISHED;
774 }
775
776 void NLA_OT_tracks_delete(wmOperatorType *ot)
777 {
778         /* identifiers */
779         ot->name = "Delete Tracks";
780         ot->idname = "NLA_OT_tracks_delete";
781         ot->description = "Delete selected NLA-Tracks and the strips they contain";
782
783         /* api callbacks */
784         ot->exec = nlaedit_delete_tracks_exec;
785         ot->poll = nlaop_poll_tweakmode_off;
786
787         /* flags */
788         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
789 }
790
791 /* *********************************************** */
792 /* AnimData Related Operators */
793
794 /* ******************** Include Objects Operator ***************************** */
795 /* Include selected objects in NLA Editor, by giving them AnimData blocks
796  * NOTE: This doesn't help for non-object AnimData, where we do not have any effective
797  *       selection mechanism in place. Unfortunately, this means that non-object AnimData
798  *       once again becomes a second-class citizen here. However, at least for the most
799  *       common use case, we now have a nice shortcut again.
800  */
801
802 static int nlaedit_objects_add_exec(bContext *C, wmOperator *UNUSED(op))
803 {
804         bAnimContext ac;
805         SpaceNla *snla;
806
807         /* get editor data */
808         if (ANIM_animdata_get_context(C, &ac) == 0)
809                 return OPERATOR_CANCELLED;
810
811         /* ensure that filters are set so that the effect will be immediately visible */
812         snla = (SpaceNla *)ac.sl;
813         if (snla && snla->ads) {
814                 snla->ads->filterflag &= ~ADS_FILTER_NLA_NOACT;
815         }
816
817         /* operate on selected objects... */
818         CTX_DATA_BEGIN (C, Object *, ob, selected_objects)
819         {
820                 /* ensure that object has AnimData... that's all */
821                 BKE_animdata_add_id(&ob->id);
822         }
823         CTX_DATA_END;
824
825         /* set notifier that things have changed */
826         WM_event_add_notifier(C, NC_ANIMATION | ND_NLA | NA_EDITED, NULL);
827
828         /* done */
829         return OPERATOR_FINISHED;
830 }
831
832 void NLA_OT_selected_objects_add(wmOperatorType *ot)
833 {
834         /* identifiers */
835         ot->name = "Include Selected Objects";
836         ot->idname = "NLA_OT_selected_objects_add";
837         ot->description = "Make selected objects appear in NLA Editor by adding Animation Data";
838
839         /* api callbacks */
840         ot->exec = nlaedit_objects_add_exec;
841         ot->poll = nlaop_poll_tweakmode_off;
842
843         /* flags */
844         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
845 }
846
847 /* *********************************************** */