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