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