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