Cleanup: remove moar G.main usages.
[blender.git] / source / blender / editors / animation / anim_filter.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) 2008 Blender Foundation, Joshua Leung
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Joshua Leung (original author)
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/animation/anim_filter.c
28  *  \ingroup edanimation
29  */
30
31
32 /* This file contains a system used to provide a layer of abstraction between sources
33  * of animation data and tools in Animation Editors. The method used here involves
34  * generating a list of edit structures which enable tools to naively perform the actions
35  * they require without all the boiler-plate associated with loops within loops and checking
36  * for cases to ignore.
37  *
38  * While this is primarily used for the Action/Dopesheet Editor (and its accessory modes),
39  * the Graph Editor also uses this for its channel list and for determining which curves
40  * are being edited. Likewise, the NLA Editor also uses this for its channel list and in
41  * its operators.
42  *
43  * Note: much of the original system this was based on was built before the creation of the RNA
44  * system. In future, it would be interesting to replace some parts of this code with RNA queries,
45  * however, RNA does not eliminate some of the boiler-plate reduction benefits presented by this
46  * system, so if any such work does occur, it should only be used for the internals used here...
47  *
48  * -- Joshua Leung, Dec 2008 (Last revision July 2009)
49  */
50
51 #include <string.h>
52
53 #include "DNA_anim_types.h"
54 #include "DNA_armature_types.h"
55 #include "DNA_camera_types.h"
56 #include "DNA_cachefile_types.h"
57 #include "DNA_lamp_types.h"
58 #include "DNA_lattice_types.h"
59 #include "DNA_linestyle_types.h"
60 #include "DNA_key_types.h"
61 #include "DNA_mask_types.h"
62 #include "DNA_material_types.h"
63 #include "DNA_mesh_types.h"
64 #include "DNA_meta_types.h"
65 #include "DNA_movieclip_types.h"
66 #include "DNA_node_types.h"
67 #include "DNA_particle_types.h"
68 #include "DNA_space_types.h"
69 #include "DNA_sequence_types.h"
70 #include "DNA_scene_types.h"
71 #include "DNA_screen_types.h"
72 #include "DNA_speaker_types.h"
73 #include "DNA_world_types.h"
74 #include "DNA_gpencil_types.h"
75 #include "DNA_object_types.h"
76 #include "DNA_userdef_types.h"
77
78 #include "MEM_guardedalloc.h"
79
80 #include "BLI_blenlib.h"
81 #include "BLI_utildefines.h"
82 #include "BLI_alloca.h"
83 #include "BLI_ghash.h"
84 #include "BLI_string.h"
85
86 #include "BKE_animsys.h"
87 #include "BKE_action.h"
88 #include "BKE_fcurve.h"
89 #include "BKE_context.h"
90 #include "BKE_global.h"
91 #include "BKE_group.h"
92 #include "BKE_key.h"
93 #include "BKE_main.h"
94 #include "BKE_material.h"
95 #include "BKE_modifier.h"
96 #include "BKE_node.h"
97 #include "BKE_mask.h"
98 #include "BKE_sequencer.h"
99
100 #include "ED_anim_api.h"
101 #include "ED_markers.h"
102
103 #include "UI_resources.h"  /* for TH_KEYFRAME_SCALE lookup */
104
105 /* ************************************************************ */
106 /* Blender Context <-> Animation Context mapping */
107
108 /* ----------- Private Stuff - General -------------------- */
109
110 /* Get vertical scaling factor (i.e. typically used for keyframe size) */
111 static void animedit_get_yscale_factor(bAnimContext *ac)
112 {
113         bTheme *btheme = UI_GetTheme();
114
115         /* grab scale factor directly from action editor setting
116          * NOTE: This theme setting doesn't have an ID, as it cannot be accessed normally
117          *       since it is a float, and the theme settings methods can only handle chars.
118          */
119         ac->yscale_fac = btheme->tact.keyframe_scale_fac;
120
121         /* clamp to avoid problems with uninitialised values... */
122         if (ac->yscale_fac < 0.1f)
123                 ac->yscale_fac = 1.0f;
124         //printf("yscale_fac = %f\n", ac->yscale_fac);
125 }
126
127 /* ----------- Private Stuff - Action Editor ------------- */
128
129 /* Get shapekey data being edited (for Action Editor -> ShapeKey mode) */
130 /* Note: there's a similar function in key.c (BKE_key_from_object) */
131 static Key *actedit_get_shapekeys(bAnimContext *ac)
132 {
133         Scene *scene = ac->scene;
134         Object *ob;
135         Key *key;
136
137         ob = OBACT;
138         if (ob == NULL)
139                 return NULL;
140
141         /* XXX pinning is not available in 'ShapeKey' mode... */
142         //if (saction->pin) return NULL;
143
144         /* shapekey data is stored with geometry data */
145         key = BKE_key_from_object(ob);
146
147         if (key) {
148                 if (key->type == KEY_RELATIVE)
149                         return key;
150         }
151
152         return NULL;
153 }
154
155 /* Get data being edited in Action Editor (depending on current 'mode') */
156 static bool actedit_get_context(bAnimContext *ac, SpaceAction *saction)
157 {
158         /* get dopesheet */
159         ac->ads = &saction->ads;
160
161         /* sync settings with current view status, then return appropriate data */
162         switch (saction->mode) {
163                 case SACTCONT_ACTION: /* 'Action Editor' */
164                         /* if not pinned, sync with active object */
165                         if (/*saction->pin == 0*/ true) {
166                                 if (ac->obact && ac->obact->adt)
167                                         saction->action = ac->obact->adt->action;
168                                 else
169                                         saction->action = NULL;
170                         }
171
172                         ac->datatype = ANIMCONT_ACTION;
173                         ac->data = saction->action;
174
175                         ac->mode = saction->mode;
176                         return true;
177
178                 case SACTCONT_SHAPEKEY: /* 'ShapeKey Editor' */
179                         ac->datatype = ANIMCONT_SHAPEKEY;
180                         ac->data = actedit_get_shapekeys(ac);
181
182                         /* if not pinned, sync with active object */
183                         if (/*saction->pin == 0*/ true) {
184                                 Key *key = (Key *)ac->data;
185
186                                 if (key && key->adt)
187                                         saction->action = key->adt->action;
188                                 else
189                                         saction->action = NULL;
190                         }
191
192                         ac->mode = saction->mode;
193                         return true;
194
195                 case SACTCONT_GPENCIL: /* Grease Pencil */ /* XXX review how this mode is handled... */
196                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
197                         saction->ads.source = (ID *)ac->scene;
198
199                         ac->datatype = ANIMCONT_GPENCIL;
200                         ac->data = &saction->ads;
201
202                         ac->mode = saction->mode;
203                         return true;
204
205                 case SACTCONT_CACHEFILE: /* Cache File */ /* XXX review how this mode is handled... */
206                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
207                         saction->ads.source = (ID *)ac->scene;
208
209                         ac->datatype = ANIMCONT_CHANNEL;
210                         ac->data = &saction->ads;
211
212                         ac->mode = saction->mode;
213                         return true;
214
215                 case SACTCONT_MASK: /* Mask */ /* XXX review how this mode is handled... */
216                 {
217                         /* TODO, other methods to get the mask */
218                         // Sequence *seq = BKE_sequencer_active_get(ac->scene);
219                         //MovieClip *clip = ac->scene->clip;
220 //                      struct Mask *mask = seq ? seq->mask : NULL;
221
222                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
223                         saction->ads.source = (ID *)ac->scene;
224
225                         ac->datatype = ANIMCONT_MASK;
226                         ac->data = &saction->ads;
227
228                         ac->mode = saction->mode;
229                         return true;
230                 }
231                 case SACTCONT_DOPESHEET: /* DopeSheet */
232                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
233                         saction->ads.source = (ID *)ac->scene;
234
235                         ac->datatype = ANIMCONT_DOPESHEET;
236                         ac->data = &saction->ads;
237
238                         ac->mode = saction->mode;
239                         return true;
240
241                 default: /* unhandled yet */
242                         ac->datatype = ANIMCONT_NONE;
243                         ac->data = NULL;
244
245                         ac->mode = -1;
246                         return false;
247         }
248 }
249
250 /* ----------- Private Stuff - Graph Editor ------------- */
251
252 /* Get data being edited in Graph Editor (depending on current 'mode') */
253 static bool graphedit_get_context(bAnimContext *ac, SpaceIpo *sipo)
254 {
255         /* init dopesheet data if non-existent (i.e. for old files) */
256         if (sipo->ads == NULL) {
257                 sipo->ads = MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
258                 sipo->ads->source = (ID *)ac->scene;
259         }
260         ac->ads = sipo->ads;
261
262         /* set settings for Graph Editor - "Selected = Editable" */
263         if (sipo->flag & SIPO_SELCUVERTSONLY)
264                 sipo->ads->filterflag |= ADS_FILTER_SELEDIT;
265         else
266                 sipo->ads->filterflag &= ~ADS_FILTER_SELEDIT;
267
268         /* sync settings with current view status, then return appropriate data */
269         switch (sipo->mode) {
270                 case SIPO_MODE_ANIMATION:  /* Animation F-Curve Editor */
271                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
272                         sipo->ads->source = (ID *)ac->scene;
273                         sipo->ads->filterflag &= ~ADS_FILTER_ONLYDRIVERS;
274
275                         ac->datatype = ANIMCONT_FCURVES;
276                         ac->data = sipo->ads;
277
278                         ac->mode = sipo->mode;
279                         return true;
280
281                 case SIPO_MODE_DRIVERS:  /* Driver F-Curve Editor */
282                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
283                         sipo->ads->source = (ID *)ac->scene;
284                         sipo->ads->filterflag |= ADS_FILTER_ONLYDRIVERS;
285
286                         ac->datatype = ANIMCONT_DRIVERS;
287                         ac->data = sipo->ads;
288
289                         ac->mode = sipo->mode;
290                         return true;
291
292                 default: /* unhandled yet */
293                         ac->datatype = ANIMCONT_NONE;
294                         ac->data = NULL;
295
296                         ac->mode = -1;
297                         return false;
298         }
299 }
300
301 /* ----------- Private Stuff - NLA Editor ------------- */
302
303 /* Get data being edited in Graph Editor (depending on current 'mode') */
304 static bool nlaedit_get_context(bAnimContext *ac, SpaceNla *snla)
305 {
306         /* init dopesheet data if non-existent (i.e. for old files) */
307         if (snla->ads == NULL)
308                 snla->ads = MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
309         ac->ads = snla->ads;
310
311         /* sync settings with current view status, then return appropriate data */
312         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
313         snla->ads->source = (ID *)ac->scene;
314         snla->ads->filterflag |= ADS_FILTER_ONLYNLA;
315
316         ac->datatype = ANIMCONT_NLA;
317         ac->data = snla->ads;
318
319         return true;
320 }
321
322 /* ----------- Public API --------------- */
323
324 /* Obtain current anim-data context, given that context info from Blender context has already been set
325  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
326  *        allocation/freeing costs (which are not that avoidable with channels).
327  */
328 bool ANIM_animdata_context_getdata(bAnimContext *ac)
329 {
330         SpaceLink *sl = ac->sl;
331         bool ok = false;
332
333         /* context depends on editor we are currently in */
334         if (sl) {
335                 switch (ac->spacetype) {
336                         case SPACE_ACTION:
337                         {
338                                 SpaceAction *saction = (SpaceAction *)sl;
339                                 ok = actedit_get_context(ac, saction);
340                                 break;
341                         }
342                         case SPACE_IPO:
343                         {
344                                 SpaceIpo *sipo = (SpaceIpo *)sl;
345                                 ok = graphedit_get_context(ac, sipo);
346                                 break;
347                         }
348                         case SPACE_NLA:
349                         {
350                                 SpaceNla *snla = (SpaceNla *)sl;
351                                 ok = nlaedit_get_context(ac, snla);
352                                 break;
353                         }
354                 }
355         }
356
357         /* check if there's any valid data */
358         return (ok && ac->data);
359 }
360
361 /* Obtain current anim-data context from Blender Context info
362  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
363  *        allocation/freeing costs (which are not that avoidable with channels).
364  *      - Clears data and sets the information from Blender Context which is useful
365  */
366 bool ANIM_animdata_get_context(const bContext *C, bAnimContext *ac)
367 {
368         Main *bmain = CTX_data_main(C);
369         ScrArea *sa = CTX_wm_area(C);
370         ARegion *ar = CTX_wm_region(C);
371         SpaceLink *sl = CTX_wm_space_data(C);
372         Scene *scene = CTX_data_scene(C);
373
374         /* clear old context info */
375         if (ac == NULL) return false;
376         memset(ac, 0, sizeof(bAnimContext));
377
378         /* get useful default context settings from context */
379         ac->bmain = bmain;
380         ac->scene = scene;
381         if (scene) {
382                 ac->markers = ED_context_get_markers(C);
383                 ac->obact = (scene->basact) ?  scene->basact->object : NULL;
384         }
385         ac->sa = sa;
386         ac->ar = ar;
387         ac->sl = sl;
388         ac->spacetype = (sa) ? sa->spacetype : 0;
389         ac->regiontype = (ar) ? ar->regiontype : 0;
390
391         /* initialise default y-scale factor */
392         animedit_get_yscale_factor(ac);
393
394         /* get data context info */
395         // XXX: if the below fails, try to grab this info from context instead... (to allow for scripting)
396         return ANIM_animdata_context_getdata(ac);
397 }
398
399 /* ************************************************************ */
400 /* Blender Data <-- Filter --> Channels to be operated on */
401
402 /* macros to use before/after getting the sub-channels of some channel,
403  * to abstract away some of the tricky logic involved
404  *
405  * cases:
406  *      1) Graph Edit main area (just data) OR channels visible in Channel List
407  *      2) If not showing channels, we're only interested in the data (Action Editor's editing)
408  *      3) We don't care what data, we just care there is some (so that a collapsed
409  *         channel can be kept around). No need to clear channels-flag in order to
410  *         keep expander channels with no sub-data out, as those cases should get
411  *         dealt with by the recursive detection idiom in place.
412  *
413  * Implementation Note:
414  *  YES the _doSubChannels variable is NOT read anywhere. BUT, this is NOT an excuse
415  *  to go steamrolling the logic into a single-line expression as from experience,
416  *  those are notoriously difficult to read + debug when extending later on. The code
417  *  below is purposefully laid out so that each case noted above corresponds clearly to
418  *  one case below.
419  */
420 #define BEGIN_ANIMFILTER_SUBCHANNELS(expanded_check) \
421         { \
422                 int _filter = filter_mode; \
423                 short _doSubChannels = 0; \
424                 if (!(filter_mode & ANIMFILTER_LIST_VISIBLE) || (expanded_check)) \
425                         _doSubChannels = 1; \
426                 else if (!(filter_mode & ANIMFILTER_LIST_CHANNELS)) \
427                         _doSubChannels = 2; \
428                 else { \
429                         filter_mode |= ANIMFILTER_TMP_PEEK; \
430                 } \
431                  \
432                 { \
433                         (void) _doSubChannels; \
434                 }
435 /* ... standard sub-channel filtering can go on here now ... */
436 #define END_ANIMFILTER_SUBCHANNELS \
437                 filter_mode = _filter; \
438         } (void)0
439
440 /* ............................... */
441
442 /* quick macro to test if AnimData is usable */
443 #define ANIMDATA_HAS_KEYS(id) ((id)->adt && (id)->adt->action)
444
445 /* quick macro to test if AnimData is usable for drivers */
446 #define ANIMDATA_HAS_DRIVERS(id) ((id)->adt && (id)->adt->drivers.first)
447
448 /* quick macro to test if AnimData is usable for NLA */
449 #define ANIMDATA_HAS_NLA(id) ((id)->adt && (id)->adt->nla_tracks.first)
450
451 /* Quick macro to test for all three above usability tests, performing the appropriate provided
452  * action for each when the AnimData context is appropriate.
453  *
454  * Priority order for this goes (most important, to least): AnimData blocks, NLA, Drivers, Keyframes.
455  *
456  * For this to work correctly, a standard set of data needs to be available within the scope that this
457  * gets called in:
458  *  - ListBase anim_data;
459  *  - bDopeSheet *ads;
460  *  - bAnimListElem *ale;
461  *  - size_t items;
462  *
463  *  - id: ID block which should have an AnimData pointer following it immediately, to use
464  *  - adtOk: line or block of code to execute for AnimData-blocks case (usually ANIMDATA_ADD_ANIMDATA)
465  *  - nlaOk: line or block of code to execute for NLA tracks+strips case
466  *  - driversOk: line or block of code to execute for Drivers case
467  *  - nlaKeysOk: line or block of code for NLA Strip Keyframes case
468  *  - keysOk: line or block of code for Keyframes case
469  *
470  * The checks for the various cases are as follows:
471  *      0) top level: checks for animdata and also that all the F-Curves for the block will be visible
472  *      1) animdata check: for filtering animdata blocks only
473  *      2A) nla tracks: include animdata block's data as there are NLA tracks+strips there
474  *      2B) actions to convert to nla: include animdata block's data as there is an action that can be
475  *              converted to a new NLA strip, and the filtering options allow this
476  *      2C) allow non-animated datablocks to be included so that datablocks can be added
477  *      3) drivers: include drivers from animdata block (for Drivers mode in Graph Editor)
478  *  4A) nla strip keyframes: these are the per-strip controls for time and influence
479  *      4B) normal keyframes: only when there is an active action
480  */
481 #define ANIMDATA_FILTER_CASES(id, adtOk, nlaOk, driversOk, nlaKeysOk, keysOk) \
482         { \
483                 if ((id)->adt) { \
484                         if (!(filter_mode & ANIMFILTER_CURVE_VISIBLE) || !((id)->adt->flag & ADT_CURVES_NOT_VISIBLE)) { \
485                                 if (filter_mode & ANIMFILTER_ANIMDATA) { \
486                                         adtOk \
487                                 } \
488                                 else if (ads->filterflag & ADS_FILTER_ONLYNLA) { \
489                                         if (ANIMDATA_HAS_NLA(id)) { \
490                                                 nlaOk \
491                                         } \
492                                         else if (!(ads->filterflag & ADS_FILTER_NLA_NOACT) || ANIMDATA_HAS_KEYS(id)) { \
493                                                 nlaOk \
494                                         } \
495                                 } \
496                                 else if (ads->filterflag & ADS_FILTER_ONLYDRIVERS) { \
497                                         if (ANIMDATA_HAS_DRIVERS(id)) { \
498                                                 driversOk \
499                                         } \
500                                 } \
501                                 else { \
502                                         if (ANIMDATA_HAS_NLA(id)) { \
503                                                 nlaKeysOk \
504                                         } \
505                                         if (ANIMDATA_HAS_KEYS(id)) { \
506                                                 keysOk \
507                                         } \
508                                 } \
509                         } \
510                 } \
511         } (void)0
512
513 /* ............................... */
514
515 /* Add a new animation channel, taking into account the "peek" flag, which is used to just check
516  * whether any channels will be added (but without needing them to actually get created).
517  *
518  * ! This causes the calling function to return early if we're only "peeking" for channels
519  */
520 // XXX: ale_statement stuff is really a hack for one special case. It shouldn't really be needed...
521 #define ANIMCHANNEL_NEW_CHANNEL_FULL(channel_data, channel_type, owner_id, ale_statement) \
522         if (filter_mode & ANIMFILTER_TMP_PEEK) \
523                 return 1; \
524         else { \
525                 bAnimListElem *ale = make_new_animlistelem(channel_data, channel_type, (ID *)owner_id); \
526                 if (ale) { \
527                         BLI_addtail(anim_data, ale); \
528                         items ++; \
529                         ale_statement \
530                 } \
531         } (void)0
532
533 #define ANIMCHANNEL_NEW_CHANNEL(channel_data, channel_type, owner_id) \
534         ANIMCHANNEL_NEW_CHANNEL_FULL(channel_data, channel_type, owner_id, {})
535
536 /* ............................... */
537
538 /* quick macro to test if an anim-channel representing an AnimData block is suitably active */
539 #define ANIMCHANNEL_ACTIVEOK(ale) \
540         (!(filter_mode & ANIMFILTER_ACTIVE) || !(ale->adt) || (ale->adt->flag & ADT_UI_ACTIVE) )
541
542 /* quick macro to test if an anim-channel (F-Curve, Group, etc.) is selected in an acceptable way */
543 #define ANIMCHANNEL_SELOK(test_func) \
544         (!(filter_mode & (ANIMFILTER_SEL | ANIMFILTER_UNSEL)) || \
545          ((filter_mode & ANIMFILTER_SEL) && test_func) || \
546          ((filter_mode & ANIMFILTER_UNSEL) && test_func == 0) )
547
548 /* quick macro to test if an anim-channel (F-Curve) is selected ok for editing purposes
549  *      - _SELEDIT means that only selected curves will have visible+editable keyframes
550  *
551  * checks here work as follows:
552  *      1) seledit off - don't need to consider the implications of this option
553  *      2) foredit off - we're not considering editing, so channel is ok still
554  *      3) test_func (i.e. selection test) - only if selected, this test will pass
555  */
556 #define ANIMCHANNEL_SELEDITOK(test_func) \
557         (!(filter_mode & ANIMFILTER_SELEDIT) || \
558          !(filter_mode & ANIMFILTER_FOREDIT) || \
559          (test_func) )
560
561 /* ----------- 'Private' Stuff --------------- */
562
563 /* this function allocates memory for a new bAnimListElem struct for the
564  * provided animation channel-data.
565  */
566 static bAnimListElem *make_new_animlistelem(void *data, short datatype, ID *owner_id)
567 {
568         bAnimListElem *ale = NULL;
569
570         /* only allocate memory if there is data to convert */
571         if (data) {
572                 /* allocate and set generic data */
573                 ale = MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
574
575                 ale->data = data;
576                 ale->type = datatype;
577
578                 ale->id = owner_id;
579                 ale->adt = BKE_animdata_from_id(owner_id);
580
581                 /* do specifics */
582                 switch (datatype) {
583                         case ANIMTYPE_SUMMARY:
584                         {
585                                 /* nothing to include for now... this is just a dummy wrappy around all the other channels
586                                  * in the DopeSheet, and gets included at the start of the list
587                                  */
588                                 ale->key_data = NULL;
589                                 ale->datatype = ALE_ALL;
590                                 break;
591                         }
592                         case ANIMTYPE_SCENE:
593                         {
594                                 Scene *sce = (Scene *)data;
595
596                                 ale->flag = sce->flag;
597
598                                 ale->key_data = sce;
599                                 ale->datatype = ALE_SCE;
600
601                                 ale->adt = BKE_animdata_from_id(data);
602                                 break;
603                         }
604                         case ANIMTYPE_OBJECT:
605                         {
606                                 Base *base = (Base *)data;
607                                 Object *ob = base->object;
608
609                                 ale->flag = ob->flag;
610
611                                 ale->key_data = ob;
612                                 ale->datatype = ALE_OB;
613
614                                 ale->adt = BKE_animdata_from_id(&ob->id);
615                                 break;
616                         }
617                         case ANIMTYPE_FILLACTD:
618                         {
619                                 bAction *act = (bAction *)data;
620
621                                 ale->flag = act->flag;
622
623                                 ale->key_data = act;
624                                 ale->datatype = ALE_ACT;
625                                 break;
626                         }
627                         case ANIMTYPE_FILLDRIVERS:
628                         {
629                                 AnimData *adt = (AnimData *)data;
630
631                                 ale->flag = adt->flag;
632
633                                 // XXX... drivers don't show summary for now
634                                 ale->key_data = NULL;
635                                 ale->datatype = ALE_NONE;
636                                 break;
637                         }
638                         case ANIMTYPE_DSMAT:
639                         {
640                                 Material *ma = (Material *)data;
641                                 AnimData *adt = ma->adt;
642
643                                 ale->flag = FILTER_MAT_OBJD(ma);
644
645                                 ale->key_data = (adt) ? adt->action : NULL;
646                                 ale->datatype = ALE_ACT;
647
648                                 ale->adt = BKE_animdata_from_id(data);
649                                 break;
650                         }
651                         case ANIMTYPE_DSLAM:
652                         {
653                                 Lamp *la = (Lamp *)data;
654                                 AnimData *adt = la->adt;
655
656                                 ale->flag = FILTER_LAM_OBJD(la);
657
658                                 ale->key_data = (adt) ? adt->action : NULL;
659                                 ale->datatype = ALE_ACT;
660
661                                 ale->adt = BKE_animdata_from_id(data);
662                                 break;
663                         }
664                         case ANIMTYPE_DSCAM:
665                         {
666                                 Camera *ca = (Camera *)data;
667                                 AnimData *adt = ca->adt;
668
669                                 ale->flag = FILTER_CAM_OBJD(ca);
670
671                                 ale->key_data = (adt) ? adt->action : NULL;
672                                 ale->datatype = ALE_ACT;
673
674                                 ale->adt = BKE_animdata_from_id(data);
675                                 break;
676                         }
677                         case ANIMTYPE_DSCACHEFILE:
678                         {
679                                 CacheFile *cache_file = (CacheFile *)data;
680                                 AnimData *adt = cache_file->adt;
681
682                                 ale->flag = FILTER_CACHEFILE_OBJD(cache_file);
683
684                                 ale->key_data = (adt) ? adt->action : NULL;
685                                 ale->datatype = ALE_ACT;
686
687                                 ale->adt = BKE_animdata_from_id(data);
688                                 break;
689                         }
690                         case ANIMTYPE_DSCUR:
691                         {
692                                 Curve *cu = (Curve *)data;
693                                 AnimData *adt = cu->adt;
694
695                                 ale->flag = FILTER_CUR_OBJD(cu);
696
697                                 ale->key_data = (adt) ? adt->action : NULL;
698                                 ale->datatype = ALE_ACT;
699
700                                 ale->adt = BKE_animdata_from_id(data);
701                                 break;
702                         }
703                         case ANIMTYPE_DSARM:
704                         {
705                                 bArmature *arm = (bArmature *)data;
706                                 AnimData *adt = arm->adt;
707
708                                 ale->flag = FILTER_ARM_OBJD(arm);
709
710                                 ale->key_data = (adt) ? adt->action : NULL;
711                                 ale->datatype = ALE_ACT;
712
713                                 ale->adt = BKE_animdata_from_id(data);
714                                 break;
715                         }
716                         case ANIMTYPE_DSMESH:
717                         {
718                                 Mesh *me = (Mesh *)data;
719                                 AnimData *adt = me->adt;
720
721                                 ale->flag = FILTER_MESH_OBJD(me);
722
723                                 ale->key_data = (adt) ? adt->action : NULL;
724                                 ale->datatype = ALE_ACT;
725
726                                 ale->adt = BKE_animdata_from_id(data);
727                                 break;
728                         }
729                         case ANIMTYPE_DSLAT:
730                         {
731                                 Lattice *lt = (Lattice *)data;
732                                 AnimData *adt = lt->adt;
733
734                                 ale->flag = FILTER_LATTICE_OBJD(lt);
735
736                                 ale->key_data = (adt) ? adt->action : NULL;
737                                 ale->datatype = ALE_ACT;
738
739                                 ale->adt = BKE_animdata_from_id(data);
740                                 break;
741                         }
742                         case ANIMTYPE_DSSPK:
743                         {
744                                 Speaker *spk = (Speaker *)data;
745                                 AnimData *adt = spk->adt;
746
747                                 ale->flag = FILTER_SPK_OBJD(spk);
748
749                                 ale->key_data = (adt) ? adt->action : NULL;
750                                 ale->datatype = ALE_ACT;
751
752                                 ale->adt = BKE_animdata_from_id(data);
753                                 break;
754                         }
755                         case ANIMTYPE_DSSKEY:
756                         {
757                                 Key *key = (Key *)data;
758                                 AnimData *adt = key->adt;
759
760                                 ale->flag = FILTER_SKE_OBJD(key);
761
762                                 ale->key_data = (adt) ? adt->action : NULL;
763                                 ale->datatype = ALE_ACT;
764
765                                 ale->adt = BKE_animdata_from_id(data);
766                                 break;
767                         }
768                         case ANIMTYPE_DSWOR:
769                         {
770                                 World *wo = (World *)data;
771                                 AnimData *adt = wo->adt;
772
773                                 ale->flag = FILTER_WOR_SCED(wo);
774
775                                 ale->key_data = (adt) ? adt->action : NULL;
776                                 ale->datatype = ALE_ACT;
777
778                                 ale->adt = BKE_animdata_from_id(data);
779                                 break;
780                         }
781                         case ANIMTYPE_DSNTREE:
782                         {
783                                 bNodeTree *ntree = (bNodeTree *)data;
784                                 AnimData *adt = ntree->adt;
785
786                                 ale->flag = FILTER_NTREE_DATA(ntree);
787
788                                 ale->key_data = (adt) ? adt->action : NULL;
789                                 ale->datatype = ALE_ACT;
790
791                                 ale->adt = BKE_animdata_from_id(data);
792                                 break;
793                         }
794                         case ANIMTYPE_DSLINESTYLE:
795                         {
796                                 FreestyleLineStyle *linestyle = (FreestyleLineStyle *)data;
797                                 AnimData *adt = linestyle->adt;
798
799                                 ale->flag = FILTER_LS_SCED(linestyle);
800
801                                 ale->key_data = (adt) ? adt->action : NULL;
802                                 ale->datatype = ALE_ACT;
803
804                                 ale->adt = BKE_animdata_from_id(data);
805                                 break;
806                         }
807                         case ANIMTYPE_DSPART:
808                         {
809                                 ParticleSettings *part = (ParticleSettings *)ale->data;
810                                 AnimData *adt = part->adt;
811
812                                 ale->flag = FILTER_PART_OBJD(part);
813
814                                 ale->key_data = (adt) ? adt->action : NULL;
815                                 ale->datatype = ALE_ACT;
816
817                                 ale->adt = BKE_animdata_from_id(data);
818                                 break;
819                         }
820                         case ANIMTYPE_DSTEX:
821                         {
822                                 Tex *tex = (Tex *)data;
823                                 AnimData *adt = tex->adt;
824
825                                 ale->flag = FILTER_TEX_DATA(tex);
826
827                                 ale->key_data = (adt) ? adt->action : NULL;
828                                 ale->datatype = ALE_ACT;
829
830                                 ale->adt = BKE_animdata_from_id(data);
831                                 break;
832                         }
833                         case ANIMTYPE_DSGPENCIL:
834                         {
835                                 bGPdata *gpd = (bGPdata *)data;
836                                 AnimData *adt = gpd->adt;
837
838                                 /* NOTE: we just reuse the same expand filter for this case */
839                                 ale->flag = EXPANDED_GPD(gpd);
840
841                                 // XXX: currently, this is only used for access to its animation data
842                                 ale->key_data = (adt) ? adt->action : NULL;
843                                 ale->datatype = ALE_ACT;
844
845                                 ale->adt = BKE_animdata_from_id(data);
846                                 break;
847                         }
848                         case ANIMTYPE_DSMCLIP:
849                         {
850                                 MovieClip *clip = (MovieClip *)data;
851                                 AnimData *adt = clip->adt;
852
853                                 ale->flag = EXPANDED_MCLIP(clip);
854
855                                 ale->key_data = (adt) ? adt->action : NULL;
856                                 ale->datatype = ALE_ACT;
857
858                                 ale->adt = BKE_animdata_from_id(data);
859                                 break;
860                         }
861                         case ANIMTYPE_NLACONTROLS:
862                         {
863                                 AnimData *adt = (AnimData *)data;
864
865                                 ale->flag = adt->flag;
866
867                                 ale->key_data = NULL;
868                                 ale->datatype = ALE_NONE;
869                                 break;
870                         }
871                         case ANIMTYPE_GROUP:
872                         {
873                                 bActionGroup *agrp = (bActionGroup *)data;
874
875                                 ale->flag = agrp->flag;
876
877                                 ale->key_data = NULL;
878                                 ale->datatype = ALE_GROUP;
879                                 break;
880                         }
881                         case ANIMTYPE_FCURVE:
882                         case ANIMTYPE_NLACURVE: /* practically the same as ANIMTYPE_FCURVE. Differences are applied post-creation */
883                         {
884                                 FCurve *fcu = (FCurve *)data;
885
886                                 ale->flag = fcu->flag;
887
888                                 ale->key_data = fcu;
889                                 ale->datatype = ALE_FCURVE;
890                                 break;
891                         }
892                         case ANIMTYPE_SHAPEKEY:
893                         {
894                                 KeyBlock *kb = (KeyBlock *)data;
895                                 Key *key = (Key *)ale->id;
896
897                                 ale->flag = kb->flag;
898
899                                 /* whether we have keyframes depends on whether there is a Key block to find it from */
900                                 if (key) {
901                                         /* index of shapekey is defined by place in key's list */
902                                         ale->index = BLI_findindex(&key->block, kb);
903
904                                         /* the corresponding keyframes are from the animdata */
905                                         if (ale->adt && ale->adt->action) {
906                                                 bAction *act = ale->adt->action;
907                                                 char *rna_path = BKE_keyblock_curval_rnapath_get(key, kb);
908
909                                                 /* try to find the F-Curve which corresponds to this exactly,
910                                                  * then free the MEM_alloc'd string
911                                                  */
912                                                 if (rna_path) {
913                                                         ale->key_data = (void *)list_find_fcurve(&act->curves, rna_path, 0);
914                                                         MEM_freeN(rna_path);
915                                                 }
916                                         }
917                                         ale->datatype = (ale->key_data) ? ALE_FCURVE : ALE_NONE;
918                                 }
919                                 break;
920                         }
921                         case ANIMTYPE_GPLAYER:
922                         {
923                                 bGPDlayer *gpl = (bGPDlayer *)data;
924
925                                 ale->flag = gpl->flag;
926
927                                 ale->key_data = NULL;
928                                 ale->datatype = ALE_GPFRAME;
929                                 break;
930                         }
931                         case ANIMTYPE_MASKLAYER:
932                         {
933                                 MaskLayer *masklay = (MaskLayer *)data;
934
935                                 ale->flag = masklay->flag;
936
937                                 ale->key_data = NULL;
938                                 ale->datatype = ALE_MASKLAY;
939                                 break;
940                         }
941                         case ANIMTYPE_NLATRACK:
942                         {
943                                 NlaTrack *nlt = (NlaTrack *)data;
944
945                                 ale->flag = nlt->flag;
946
947                                 ale->key_data = &nlt->strips;
948                                 ale->datatype = ALE_NLASTRIP;
949                                 break;
950                         }
951                         case ANIMTYPE_NLAACTION:
952                         {
953                                 /* nothing to include for now... nothing editable from NLA-perspective here */
954                                 ale->key_data = NULL;
955                                 ale->datatype = ALE_NONE;
956                                 break;
957                         }
958                 }
959         }
960
961         /* return created datatype */
962         return ale;
963 }
964
965 /* ----------------------------------------- */
966
967 /* 'Only Selected' selected data and/or 'Include Hidden' filtering
968  * NOTE: when this function returns true, the F-Curve is to be skipped
969  */
970 static bool skip_fcurve_selected_data(bDopeSheet *ads, FCurve *fcu, ID *owner_id, int filter_mode)
971 {
972         if (fcu->grp != NULL && fcu->grp->flag & ADT_CURVES_ALWAYS_VISIBLE) {
973                 return false;
974         }
975         /* hidden items should be skipped if we only care about visible data, but we aren't interested in hidden stuff */
976         const bool skip_hidden = (filter_mode & ANIMFILTER_DATA_VISIBLE) && !(ads->filterflag & ADS_FILTER_INCL_HIDDEN);
977
978         if (GS(owner_id->name) == ID_OB) {
979                 Object *ob = (Object *)owner_id;
980
981                 /* only consider if F-Curve involves pose.bones */
982                 if ((fcu->rna_path) && strstr(fcu->rna_path, "pose.bones")) {
983                         bPoseChannel *pchan;
984                         char *bone_name;
985
986                         /* get bone-name, and check if this bone is selected */
987                         bone_name = BLI_str_quoted_substrN(fcu->rna_path, "pose.bones[");
988                         pchan = BKE_pose_channel_find_name(ob->pose, bone_name);
989                         if (bone_name) MEM_freeN(bone_name);
990
991                         /* check whether to continue or skip */
992                         if ((pchan) && (pchan->bone)) {
993                                 /* if only visible channels, skip if bone not visible unless user wants channels from hidden data too */
994                                 if (skip_hidden) {
995                                         bArmature *arm = (bArmature *)ob->data;
996
997                                         /* skipping - not visible on currently visible layers */
998                                         if ((arm->layer & pchan->bone->layer) == 0)
999                                                 return true;
1000                                         /* skipping - is currently hidden */
1001                                         if (pchan->bone->flag & BONE_HIDDEN_P)
1002                                                 return true;
1003                                 }
1004
1005                                 /* can only add this F-Curve if it is selected */
1006                                 if (ads->filterflag & ADS_FILTER_ONLYSEL) {
1007                                         if ((pchan->bone->flag & BONE_SELECTED) == 0)
1008                                                 return true;
1009                                 }
1010                         }
1011                 }
1012         }
1013         else if (GS(owner_id->name) == ID_SCE) {
1014                 Scene *scene = (Scene *)owner_id;
1015
1016                 /* only consider if F-Curve involves sequence_editor.sequences */
1017                 if ((fcu->rna_path) && strstr(fcu->rna_path, "sequences_all")) {
1018                         Editing *ed = BKE_sequencer_editing_get(scene, false);
1019                         Sequence *seq = NULL;
1020                         char *seq_name;
1021
1022                         if (ed) {
1023                                 /* get strip name, and check if this strip is selected */
1024                                 seq_name = BLI_str_quoted_substrN(fcu->rna_path, "sequences_all[");
1025                                 seq = BKE_sequence_get_by_name(ed->seqbasep, seq_name, false);
1026                                 if (seq_name) MEM_freeN(seq_name);
1027                         }
1028
1029                         /* can only add this F-Curve if it is selected */
1030                         if (ads->filterflag & ADS_FILTER_ONLYSEL) {
1031                                 if ((seq == NULL) || (seq->flag & SELECT) == 0)
1032                                         return true;
1033                         }
1034                 }
1035         }
1036         else if (GS(owner_id->name) == ID_NT) {
1037                 bNodeTree *ntree = (bNodeTree *)owner_id;
1038
1039                 /* check for selected nodes */
1040                 if ((fcu->rna_path) && strstr(fcu->rna_path, "nodes")) {
1041                         bNode *node;
1042                         char *node_name;
1043
1044                         /* get strip name, and check if this strip is selected */
1045                         node_name = BLI_str_quoted_substrN(fcu->rna_path, "nodes[");
1046                         node = nodeFindNodebyName(ntree, node_name);
1047                         if (node_name) MEM_freeN(node_name);
1048
1049                         /* can only add this F-Curve if it is selected */
1050                         if (ads->filterflag & ADS_FILTER_ONLYSEL) {
1051                                 if ((node) && (node->flag & NODE_SELECT) == 0)
1052                                         return true;
1053                         }
1054                 }
1055         }
1056
1057         return false;
1058 }
1059
1060 /* Helper for name-based filtering - Perform "partial/fuzzy matches" (as in 80a7efd) */
1061 static bool name_matches_dopesheet_filter(bDopeSheet *ads, char *name)
1062 {
1063         if (ads->flag & ADS_FLAG_FUZZY_NAMES) {
1064                 /* full fuzzy, multi-word, case insensitive matches */
1065                 const size_t str_len = strlen(ads->searchstr);
1066                 const int words_max = (str_len / 2) + 1;
1067
1068                 int (*words)[2] = BLI_array_alloca(words, words_max);
1069                 const int words_len = BLI_string_find_split_words(ads->searchstr, str_len, ' ', words, words_max);
1070                 bool found = false;
1071
1072                 /* match name against all search words */
1073                 for (int index = 0; index < words_len; index++) {
1074                         if (BLI_strncasestr(name, ads->searchstr + words[index][0], words[index][1])) {
1075                                 found = true;
1076                                 break;
1077                         }
1078                 }
1079
1080                 /* if we have a match somewhere, this returns true */
1081                 return found;
1082         }
1083         else {
1084                 /* fallback/default - just case insensitive, but starts from start of word */
1085                 return BLI_strcasestr(name, ads->searchstr) != NULL;
1086         }
1087 }
1088
1089 /* (Display-)Name-based F-Curve filtering
1090  * NOTE: when this function returns true, the F-Curve is to be skipped
1091  */
1092 static bool skip_fcurve_with_name(bDopeSheet *ads, FCurve *fcu, eAnim_ChannelType channel_type, void *owner, ID *owner_id)
1093 {
1094         bAnimListElem ale_dummy = {NULL};
1095         const bAnimChannelType *acf;
1096
1097         /* create a dummy wrapper for the F-Curve, so we can get typeinfo for it */
1098         ale_dummy.type = channel_type;
1099         ale_dummy.owner = owner;
1100         ale_dummy.id = owner_id;
1101         ale_dummy.data = fcu;
1102
1103         /* get type info for channel */
1104         acf = ANIM_channel_get_typeinfo(&ale_dummy);
1105         if (acf && acf->name) {
1106                 char name[256]; /* hopefully this will be enough! */
1107
1108                 /* get name */
1109                 acf->name(&ale_dummy, name);
1110
1111                 /* check for partial match with the match string, assuming case insensitive filtering
1112                  * if match, this channel shouldn't be ignored!
1113                  */
1114                 return !name_matches_dopesheet_filter(ads, name);
1115         }
1116
1117         /* just let this go... */
1118         return true;
1119 }
1120
1121 /**
1122  * Check if F-Curve has errors and/or is disabled
1123  *
1124  * \return true if F-Curve has errors/is disabled
1125  */
1126 static bool fcurve_has_errors(FCurve *fcu)
1127 {
1128         /* F-Curve disabled - path eval error */
1129         if (fcu->flag & FCURVE_DISABLED) {
1130                 return true;
1131         }
1132
1133         /* driver? */
1134         if (fcu->driver) {
1135                 ChannelDriver *driver = fcu->driver;
1136                 DriverVar *dvar;
1137
1138                 /* error flag on driver usually means that there is an error
1139                  * BUT this may not hold with PyDrivers as this flag gets cleared
1140                  *     if no critical errors prevent the driver from working...
1141                  */
1142                 if (driver->flag & DRIVER_FLAG_INVALID)
1143                         return true;
1144
1145                 /* check variables for other things that need linting... */
1146                 // TODO: maybe it would be more efficient just to have a quick flag for this?
1147                 for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
1148                         DRIVER_TARGETS_USED_LOOPER(dvar)
1149                         {
1150                                 if (dtar->flag & DTAR_FLAG_INVALID)
1151                                         return true;
1152                         }
1153                         DRIVER_TARGETS_LOOPER_END
1154                 }
1155         }
1156
1157         /* no errors found */
1158         return false;
1159 }
1160
1161 /* find the next F-Curve that is usable for inclusion */
1162 static FCurve *animfilter_fcurve_next(bDopeSheet *ads, FCurve *first, eAnim_ChannelType channel_type, int filter_mode, void *owner, ID *owner_id)
1163 {
1164         bActionGroup *grp = (channel_type == ANIMTYPE_FCURVE) ? owner : NULL;
1165         FCurve *fcu = NULL;
1166
1167         /* loop over F-Curves - assume that the caller of this has already checked that these should be included
1168          * NOTE: we need to check if the F-Curves belong to the same group, as this gets called for groups too...
1169          */
1170         for (fcu = first; ((fcu) && (fcu->grp == grp)); fcu = fcu->next) {
1171                 /* special exception for Pose-Channel/Sequence-Strip/Node Based F-Curves:
1172                  *      - the 'Only Selected' and 'Include Hidden' data filters should be applied to sub-ID data which
1173                  *        can be independently selected/hidden, such as Pose-Channels, Sequence Strips, and Nodes.
1174                  *        Since these checks were traditionally done as first check for objects, we do the same here
1175                  *      - we currently use an 'approximate' method for getting these F-Curves that doesn't require
1176                  *        carefully checking the entire path
1177                  *      - this will also affect things like Drivers, and also works for Bone Constraints
1178                  */
1179                 if (ads && owner_id) {
1180                         if ((filter_mode & ANIMFILTER_TMP_IGNORE_ONLYSEL) == 0) {
1181                                 if ((ads->filterflag & ADS_FILTER_ONLYSEL) || (ads->filterflag & ADS_FILTER_INCL_HIDDEN) == 0) {
1182                                         if (skip_fcurve_selected_data(ads, fcu, owner_id, filter_mode))
1183                                                 continue;
1184                                 }
1185                         }
1186                 }
1187
1188                 /* only include if visible (Graph Editor check, not channels check) */
1189                 if (!(filter_mode & ANIMFILTER_CURVE_VISIBLE) || (fcu->flag & FCURVE_VISIBLE)) {
1190                         /* only work with this channel and its subchannels if it is editable */
1191                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_FCU(fcu)) {
1192                                 /* only include this curve if selected in a way consistent with the filtering requirements */
1193                                 if (ANIMCHANNEL_SELOK(SEL_FCU(fcu)) && ANIMCHANNEL_SELEDITOK(SEL_FCU(fcu))) {
1194                                         /* only include if this curve is active */
1195                                         if (!(filter_mode & ANIMFILTER_ACTIVE) || (fcu->flag & FCURVE_ACTIVE)) {
1196                                                 /* name based filtering... */
1197                                                 if ( ((ads) && (ads->filterflag & ADS_FILTER_BY_FCU_NAME)) && (owner_id) ) {
1198                                                         if (skip_fcurve_with_name(ads, fcu, channel_type, owner, owner_id))
1199                                                                 continue;
1200                                                 }
1201
1202                                                 /* error-based filtering... */
1203                                                 if ((ads) && (ads->filterflag & ADS_FILTER_ONLY_ERRORS)) {
1204                                                         /* skip if no errors... */
1205                                                         if (fcurve_has_errors(fcu) == false)
1206                                                                 continue;
1207                                                 }
1208
1209                                                 /* this F-Curve can be used, so return it */
1210                                                 return fcu;
1211                                         }
1212                                 }
1213                         }
1214                 }
1215         }
1216
1217         /* no (more) F-Curves from the list are suitable... */
1218         return NULL;
1219 }
1220
1221 static size_t animfilter_fcurves(ListBase *anim_data, bDopeSheet *ads,
1222                                  FCurve *first, eAnim_ChannelType fcurve_type,
1223                                  int filter_mode,
1224                                  void *owner, ID *owner_id)
1225 {
1226         FCurve *fcu;
1227         size_t items = 0;
1228
1229         /* loop over every F-Curve able to be included
1230          *      - this for-loop works like this:
1231          *              1) the starting F-Curve is assigned to the fcu pointer so that we have a starting point to search from
1232          *              2) the first valid F-Curve to start from (which may include the one given as 'first') in the remaining
1233          *                 list of F-Curves is found, and verified to be non-null
1234          *              3) the F-Curve referenced by fcu pointer is added to the list
1235          *              4) the fcu pointer is set to the F-Curve after the one we just added, so that we can keep going through
1236          *                 the rest of the F-Curve list without an eternal loop. Back to step 2 :)
1237          */
1238         for (fcu = first; ( (fcu = animfilter_fcurve_next(ads, fcu, fcurve_type, filter_mode, owner, owner_id)) ); fcu = fcu->next) {
1239                 if (UNLIKELY(fcurve_type == ANIMTYPE_NLACURVE)) {
1240                         /* NLA Control Curve - Basically the same as normal F-Curves, except we need to set some stuff differently */
1241                         ANIMCHANNEL_NEW_CHANNEL_FULL(fcu, ANIMTYPE_NLACURVE, owner_id, {
1242                                 ale->owner = owner; /* strip */
1243                                 ale->adt = NULL;    /* to prevent time mapping from causing problems */
1244                         });
1245                 }
1246                 else {
1247                         /* Normal FCurve */
1248                         ANIMCHANNEL_NEW_CHANNEL(fcu, ANIMTYPE_FCURVE, owner_id);
1249                 }
1250         }
1251
1252         /* return the number of items added to the list */
1253         return items;
1254 }
1255
1256 static size_t animfilter_act_group(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, bAction *UNUSED(act), bActionGroup *agrp, int filter_mode, ID *owner_id)
1257 {
1258         ListBase tmp_data = {NULL, NULL};
1259         size_t tmp_items = 0;
1260         size_t items = 0;
1261         //int ofilter = filter_mode;
1262
1263         /* if we care about the selection status of the channels,
1264          * but the group isn't expanded (1)...
1265          *  (1) this only matters if we actually care about the hierarchy though.
1266          *              - Hierarchy matters: this hack should be applied
1267          *              - Hierarchy ignored: cases like [#21276] won't work properly, unless we skip this hack
1268          */
1269         if ( ((filter_mode & ANIMFILTER_LIST_VISIBLE) && EXPANDED_AGRP(ac, agrp) == 0) &&     /* care about hierarchy but group isn't expanded */
1270              (filter_mode & (ANIMFILTER_SEL | ANIMFILTER_UNSEL)) )                          /* care about selection status */
1271         {
1272                 /* if the group itself isn't selected appropriately, we shouldn't consider it's children either */
1273                 if (ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) == 0)
1274                         return 0;
1275
1276                 /* if we're still here, then the selection status of the curves within this group should not matter,
1277                  * since this creates too much overhead for animators (i.e. making a slow workflow)
1278                  *
1279                  * Tools affected by this at time of coding (2010 Feb 09):
1280                  *      - inserting keyframes on selected channels only
1281                  *      - pasting keyframes
1282                  *      - creating ghost curves in Graph Editor
1283                  */
1284                 filter_mode &= ~(ANIMFILTER_SEL | ANIMFILTER_UNSEL | ANIMFILTER_LIST_VISIBLE);
1285         }
1286
1287         /* add grouped F-Curves */
1288         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_AGRP(ac, agrp))
1289         {
1290                 /* special filter so that we can get just the F-Curves within the active group */
1291                 if (!(filter_mode & ANIMFILTER_ACTGROUPED) || (agrp->flag & AGRP_ACTIVE)) {
1292                         /* for the Graph Editor, curves may be set to not be visible in the view to lessen clutter,
1293                          * but to do this, we need to check that the group doesn't have it's not-visible flag set preventing
1294                          * all its sub-curves to be shown
1295                          */
1296                         if (!(filter_mode & ANIMFILTER_CURVE_VISIBLE) || !(agrp->flag & AGRP_NOTVISIBLE)) {
1297                                 /* group must be editable for its children to be editable (if we care about this) */
1298                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_AGRP(agrp)) {
1299                                         /* get first F-Curve which can be used here */
1300                                         FCurve *first_fcu = animfilter_fcurve_next(ads, agrp->channels.first, ANIMTYPE_FCURVE, filter_mode, agrp, owner_id);
1301
1302                                         /* filter list, starting from this F-Curve */
1303                                         tmp_items += animfilter_fcurves(&tmp_data, ads, first_fcu, ANIMTYPE_FCURVE, filter_mode, agrp, owner_id);
1304                                 }
1305                         }
1306                 }
1307         }
1308         END_ANIMFILTER_SUBCHANNELS;
1309
1310         /* did we find anything? */
1311         if (tmp_items) {
1312                 /* add this group as a channel first */
1313                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1314                         /* restore original filter mode so that this next step works ok... */
1315                         //filter_mode = ofilter;
1316
1317                         /* filter selection of channel specially here again, since may be open and not subject to previous test */
1318                         if (ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) ) {
1319                                 ANIMCHANNEL_NEW_CHANNEL(agrp, ANIMTYPE_GROUP, owner_id);
1320                         }
1321                 }
1322
1323                 /* now add the list of collected channels */
1324                 BLI_movelisttolist(anim_data, &tmp_data);
1325                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
1326                 items += tmp_items;
1327         }
1328
1329         /* return the number of items added to the list */
1330         return items;
1331 }
1332
1333 static size_t animfilter_action(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, bAction *act, int filter_mode, ID *owner_id)
1334 {
1335         bActionGroup *agrp;
1336         FCurve *lastchan = NULL;
1337         size_t items = 0;
1338
1339         /* don't include anything from this action if it is linked in from another file,
1340          * and we're getting stuff for editing...
1341          */
1342         if ((filter_mode & ANIMFILTER_FOREDIT) && ID_IS_LINKED(act))
1343                 return 0;
1344
1345         /* do groups */
1346         // TODO: do nested groups?
1347         for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1348                 /* store reference to last channel of group */
1349                 if (agrp->channels.last)
1350                         lastchan = agrp->channels.last;
1351
1352                 /* action group's channels */
1353                 items += animfilter_act_group(ac, anim_data, ads, act, agrp, filter_mode, owner_id);
1354         }
1355
1356         /* un-grouped F-Curves (only if we're not only considering those channels in the active group) */
1357         if (!(filter_mode & ANIMFILTER_ACTGROUPED)) {
1358                 FCurve *firstfcu = (lastchan) ? (lastchan->next) : (act->curves.first);
1359                 items += animfilter_fcurves(anim_data, ads, firstfcu, ANIMTYPE_FCURVE, filter_mode, NULL, owner_id);
1360         }
1361
1362         /* return the number of items added to the list */
1363         return items;
1364 }
1365
1366 /* Include NLA-Data for NLA-Editor:
1367  *      - when ANIMFILTER_LIST_CHANNELS is used, that means we should be filtering the list for display
1368  *        Although the evaluation order is from the first track to the last and then apply the Action on top,
1369  *        we present this in the UI as the Active Action followed by the last track to the first so that we
1370  *        get the evaluation order presented as per a stack.
1371  *      - for normal filtering (i.e. for editing), we only need the NLA-tracks but they can be in 'normal' evaluation
1372  *        order, i.e. first to last. Otherwise, some tools may get screwed up.
1373  */
1374 static size_t animfilter_nla(bAnimContext *UNUSED(ac), ListBase *anim_data, bDopeSheet *ads, AnimData *adt, int filter_mode, ID *owner_id)
1375 {
1376         NlaTrack *nlt;
1377         NlaTrack *first = NULL, *next = NULL;
1378         size_t items = 0;
1379
1380         /* if showing channels, include active action */
1381         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1382                 /* if NLA action-line filtering is off, don't show unless there are keyframes,
1383                  * in order to keep things more compact for doing transforms
1384                  */
1385                 if (!(ads->filterflag & ADS_FILTER_NLA_NOACT) || (adt->action)) {
1386                         /* there isn't really anything editable here, so skip if need editable */
1387                         if ((filter_mode & ANIMFILTER_FOREDIT) == 0) {
1388                                 /* just add the action track now (this MUST appear for drawing)
1389                                  *      - as AnimData may not have an action, we pass a dummy pointer just to get the list elem created, then
1390                                  *        overwrite this with the real value - REVIEW THIS...
1391                                  */
1392                                 ANIMCHANNEL_NEW_CHANNEL_FULL((void *)(&adt->action), ANIMTYPE_NLAACTION, owner_id,
1393                                         {
1394                                                 ale->data = adt->action ? adt->action : NULL;
1395                                         });
1396                         }
1397                 }
1398
1399                 /* first track to include will be the last one if we're filtering by channels */
1400                 first = adt->nla_tracks.last;
1401         }
1402         else {
1403                 /* first track to include will the first one (as per normal) */
1404                 first = adt->nla_tracks.first;
1405         }
1406
1407         /* loop over NLA Tracks - assume that the caller of this has already checked that these should be included */
1408         for (nlt = first; nlt; nlt = next) {
1409                 /* 'next' NLA-Track to use depends on whether we're filtering for drawing or not */
1410                 if (filter_mode & ANIMFILTER_LIST_CHANNELS)
1411                         next = nlt->prev;
1412                 else
1413                         next = nlt->next;
1414
1415                 /* if we're in NLA-tweakmode, don't show this track if it was disabled (due to tweaking) for now
1416                  *      - active track should still get shown though (even though it has disabled flag set)
1417                  */
1418                 // FIXME: the channels after should still get drawn, just 'differently', and after an active-action channel
1419                 if ((adt->flag & ADT_NLA_EDIT_ON) && (nlt->flag & NLATRACK_DISABLED) && (adt->act_track != nlt))
1420                         continue;
1421
1422                 /* only work with this channel and its subchannels if it is editable */
1423                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_NLT(nlt)) {
1424                         /* only include this track if selected in a way consistent with the filtering requirements */
1425                         if (ANIMCHANNEL_SELOK(SEL_NLT(nlt))) {
1426                                 /* only include if this track is active */
1427                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (nlt->flag & NLATRACK_ACTIVE)) {
1428                                         /* name based filtering... */
1429                                         if (((ads) && (ads->filterflag & ADS_FILTER_BY_FCU_NAME)) && (owner_id)) {
1430                                                 bool track_ok = false, strip_ok = false;
1431
1432                                                 /* check if the name of the track, or the strips it has are ok... */
1433                                                 track_ok = name_matches_dopesheet_filter(ads, nlt->name);
1434
1435                                                 if (track_ok == false) {
1436                                                         NlaStrip *strip;
1437                                                         for (strip = nlt->strips.first; strip; strip = strip->next) {
1438                                                                 if (name_matches_dopesheet_filter(ads, strip->name)) {
1439                                                                         strip_ok = true;
1440                                                                         break;
1441                                                                 }
1442                                                         }
1443                                                 }
1444
1445                                                 /* skip if both fail this test... */
1446                                                 if (!track_ok && !strip_ok) {
1447                                                         continue;
1448                                                 }
1449                                         }
1450
1451                                         /* add the track now that it has passed all our tests */
1452                                         ANIMCHANNEL_NEW_CHANNEL(nlt, ANIMTYPE_NLATRACK, owner_id);
1453                                 }
1454                         }
1455                 }
1456         }
1457
1458         /* return the number of items added to the list */
1459         return items;
1460 }
1461
1462 /* Include the control FCurves per NLA Strip in the channel list
1463  * NOTE: This is includes the expander too...
1464  */
1465 static size_t animfilter_nla_controls(ListBase *anim_data, bDopeSheet *ads, AnimData *adt, int filter_mode, ID *owner_id)
1466 {
1467         ListBase tmp_data = {NULL, NULL};
1468         size_t tmp_items = 0;
1469         size_t items = 0;
1470
1471         /* add control curves from each NLA strip... */
1472         /* NOTE: ANIMTYPE_FCURVES are created here, to avoid duplicating the code needed */
1473         BEGIN_ANIMFILTER_SUBCHANNELS(((adt->flag & ADT_NLA_SKEYS_COLLAPSED) == 0))
1474         {
1475                 NlaTrack *nlt;
1476                 NlaStrip *strip;
1477
1478                 /* for now, we only go one level deep - so controls on grouped FCurves are not handled */
1479                 for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
1480                         for (strip = nlt->strips.first; strip; strip = strip->next) {
1481                                 /* pass strip as the "owner", so that the name lookups (used while filtering) will resolve */
1482                                 tmp_items += animfilter_fcurves(&tmp_data, ads, strip->fcurves.first, ANIMTYPE_NLACURVE, filter_mode, strip, owner_id);
1483                         }
1484                 }
1485         }
1486         END_ANIMFILTER_SUBCHANNELS;
1487
1488         /* did we find anything? */
1489         if (tmp_items) {
1490                 /* add the expander as a channel first */
1491                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1492                         /* currently these channels cannot be selected, so they should be skipped */
1493                         if ((filter_mode & (ANIMFILTER_SEL | ANIMFILTER_UNSEL)) == 0) {
1494                                 ANIMCHANNEL_NEW_CHANNEL(adt, ANIMTYPE_NLACONTROLS, owner_id);
1495                         }
1496                 }
1497
1498                 /* now add the list of collected channels */
1499                 BLI_movelisttolist(anim_data, &tmp_data);
1500                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
1501                 items += tmp_items;
1502         }
1503
1504         /* return the numebr of items added to the list */
1505         return items;
1506 }
1507
1508 /* determine what animation data from AnimData block should get displayed */
1509 static size_t animfilter_block_data(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *id, int filter_mode)
1510 {
1511         AnimData *adt = BKE_animdata_from_id(id);
1512         size_t items = 0;
1513
1514         /* image object datablocks have no anim-data so check for NULL */
1515         if (adt) {
1516                 IdAdtTemplate *iat = (IdAdtTemplate *)id;
1517
1518                 /* NOTE: this macro is used instead of inlining the logic here, since this sort of filtering is still needed
1519                  * in a few places in the rest of the code still - notably for the few cases where special mode-based
1520                  * different types of data expanders are required.
1521                  */
1522                 ANIMDATA_FILTER_CASES(iat,
1523                         { /* AnimData */
1524                                 /* specifically filter animdata block */
1525                                 if (ANIMCHANNEL_SELOK(SEL_ANIMDATA(adt)) ) {
1526                                         ANIMCHANNEL_NEW_CHANNEL(adt, ANIMTYPE_ANIMDATA, id);
1527                                 }
1528                         },
1529                         { /* NLA */
1530                                 items += animfilter_nla(ac, anim_data, ads, adt, filter_mode, id);
1531                         },
1532                         { /* Drivers */
1533                                 items += animfilter_fcurves(anim_data, ads, adt->drivers.first, ANIMTYPE_FCURVE, filter_mode, NULL, id);
1534                         },
1535                         { /* NLA Control Keyframes */
1536                                 items += animfilter_nla_controls(anim_data, ads, adt, filter_mode, id);
1537                         },
1538                         { /* Keyframes */
1539                                 items += animfilter_action(ac, anim_data, ads, adt->action, filter_mode, id);
1540                         }
1541                 );
1542         }
1543
1544         return items;
1545 }
1546
1547
1548
1549 /* Include ShapeKey Data for ShapeKey Editor */
1550 static size_t animdata_filter_shapekey(bAnimContext *ac, ListBase *anim_data, Key *key, int filter_mode)
1551 {
1552         size_t items = 0;
1553
1554         /* check if channels or only F-Curves */
1555         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1556                 KeyBlock *kb;
1557
1558                 /* loop through the channels adding ShapeKeys as appropriate */
1559                 for (kb = key->block.first; kb; kb = kb->next) {
1560                         /* skip the first one, since that's the non-animatable basis */
1561                         if (kb == key->block.first) continue;
1562
1563                         /* only work with this channel and its subchannels if it is editable */
1564                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_SHAPEKEY(kb)) {
1565                                 /* only include this track if selected in a way consistent with the filtering requirements */
1566                                 if (ANIMCHANNEL_SELOK(SEL_SHAPEKEY(kb)) ) {
1567                                         // TODO: consider 'active' too?
1568
1569                                         /* owner-id here must be key so that the F-Curve can be resolved... */
1570                                         ANIMCHANNEL_NEW_CHANNEL(kb, ANIMTYPE_SHAPEKEY, key);
1571                                 }
1572                         }
1573                 }
1574         }
1575         else {
1576                 /* just use the action associated with the shapekey */
1577                 // TODO: somehow manage to pass dopesheet info down here too?
1578                 if (key->adt) {
1579                         if (filter_mode & ANIMFILTER_ANIMDATA) {
1580                                 if (ANIMCHANNEL_SELOK(SEL_ANIMDATA(key->adt)) ) {
1581                                         ANIMCHANNEL_NEW_CHANNEL(key->adt, ANIMTYPE_ANIMDATA, key);
1582                                 }
1583                         }
1584                         else if (key->adt->action) {
1585                                 items = animfilter_action(ac, anim_data, NULL, key->adt->action, filter_mode, (ID *)key);
1586                         }
1587                 }
1588         }
1589
1590         /* return the number of items added to the list */
1591         return items;
1592 }
1593
1594 /* Helper for Grease Pencil - layers within a datablock */
1595 static size_t animdata_filter_gpencil_layers_data(ListBase *anim_data, bDopeSheet *ads, bGPdata *gpd, int filter_mode)
1596 {
1597         bGPDlayer *gpl;
1598         size_t items = 0;
1599
1600         /* loop over layers as the conditions are acceptable */
1601         for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1602                 /* only if selected */
1603                 if (ANIMCHANNEL_SELOK(SEL_GPL(gpl)) ) {
1604                         /* only if editable */
1605                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
1606                                 /* active... */
1607                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (gpl->flag & GP_LAYER_ACTIVE)) {
1608                                         /* skip layer if the name doesn't match the filter string */
1609                                         if ((ads) && (ads->filterflag & ADS_FILTER_BY_FCU_NAME)) {
1610                                                 if (name_matches_dopesheet_filter(ads, gpl->info) == false)
1611                                                         continue;
1612                                         }
1613
1614
1615                                         /* add to list */
1616                                         ANIMCHANNEL_NEW_CHANNEL(gpl, ANIMTYPE_GPLAYER, gpd);
1617                                 }
1618                         }
1619                 }
1620         }
1621
1622         return items;
1623 }
1624
1625 /* Helper for Grease Pencil - Grease Pencil datablock - GP Frames */
1626 static size_t animdata_filter_gpencil_data(ListBase *anim_data, bDopeSheet *ads, bGPdata *gpd, int filter_mode)
1627 {
1628         size_t items = 0;
1629
1630         /* When asked from "AnimData" blocks (i.e. the top-level containers for normal animation),
1631          * for convenience, this will return GP Datablocks instead. This may cause issues down
1632          * the track, but for now, this will do...
1633          */
1634         if (filter_mode & ANIMFILTER_ANIMDATA) {
1635                 /* just add GPD as a channel - this will add everything needed */
1636                 ANIMCHANNEL_NEW_CHANNEL(gpd, ANIMTYPE_GPDATABLOCK, NULL);
1637         }
1638         else {
1639                 ListBase tmp_data = {NULL, NULL};
1640                 size_t tmp_items = 0;
1641
1642                 /* add gpencil animation channels */
1643                 BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_GPD(gpd))
1644                 {
1645                         tmp_items += animdata_filter_gpencil_layers_data(&tmp_data, ads, gpd, filter_mode);
1646                 }
1647                 END_ANIMFILTER_SUBCHANNELS;
1648
1649                 /* did we find anything? */
1650                 if (tmp_items) {
1651                         /* include data-expand widget first */
1652                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1653                                 /* add gpd as channel too (if for drawing, and it has layers) */
1654                                 ANIMCHANNEL_NEW_CHANNEL(gpd, ANIMTYPE_GPDATABLOCK, NULL);
1655                         }
1656
1657                         /* now add the list of collected channels */
1658                         BLI_movelisttolist(anim_data, &tmp_data);
1659                         BLI_assert(BLI_listbase_is_empty(&tmp_data));
1660                         items += tmp_items;
1661                 }
1662         }
1663
1664         return items;
1665 }
1666
1667 /* Grab all Grease Pencil datablocks in file */
1668 // TODO: should this be amalgamated with the dopesheet filtering code?
1669 static size_t animdata_filter_gpencil(bAnimContext *ac, ListBase *anim_data, void *UNUSED(data), int filter_mode)
1670 {
1671         bDopeSheet *ads = ac->ads;
1672         size_t items = 0;
1673
1674         if (ads->filterflag & ADS_FILTER_GP_3DONLY) {
1675                 Scene *scene = (Scene *)ads->source;
1676                 Base *base;
1677
1678                 /* Active scene's GPencil block first - No parent item needed... */
1679                 if (scene->gpd) {
1680                         items += animdata_filter_gpencil_data(anim_data, ads, scene->gpd, filter_mode);
1681                 }
1682
1683                 /* Objects in the scene */
1684                 for (base = scene->base.first; base; base = base->next) {
1685                         /* Only consider this object if it has got some GP data (saving on all the other tests) */
1686                         if (base->object && base->object->gpd) {
1687                                 Object *ob = base->object;
1688
1689                                 /* firstly, check if object can be included, by the following factors:
1690                                  *      - if only visible, must check for layer and also viewport visibility
1691                                  *              --> while tools may demand only visible, user setting takes priority
1692                                  *                      as user option controls whether sets of channels get included while
1693                                  *                      tool-flag takes into account collapsed/open channels too
1694                                  *      - if only selected, must check if object is selected
1695                                  *      - there must be animation data to edit (this is done recursively as we
1696                                  *        try to add the channels)
1697                                  */
1698                                 if ((filter_mode & ANIMFILTER_DATA_VISIBLE) && !(ads->filterflag & ADS_FILTER_INCL_HIDDEN)) {
1699                                         /* layer visibility - we check both object and base, since these may not be in sync yet */
1700                                         if ((scene->lay & (ob->lay | base->lay)) == 0) continue;
1701
1702                                         /* outliner restrict-flag */
1703                                         if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
1704                                 }
1705
1706                                 /* check selection and object type filters */
1707                                 if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) /*|| (base == scene->basact)*/) ) {
1708                                         /* only selected should be shown */
1709                                         continue;
1710                                 }
1711
1712                                 /* check if object belongs to the filtering group if option to filter
1713                                  * objects by the grouped status is on
1714                                  *      - used to ease the process of doing multiple-character choreographies
1715                                  */
1716                                 if (ads->filterflag & ADS_FILTER_ONLYOBGROUP) {
1717                                         if (BKE_group_object_exists(ads->filter_grp, ob) == 0)
1718                                                 continue;
1719                                 }
1720
1721                                 /* finally, include this object's grease pencil datablock */
1722                                 /* XXX: Should we store these under expanders per item? */
1723                                 items += animdata_filter_gpencil_data(anim_data, ads, ob->gpd, filter_mode);
1724                         }
1725                 }
1726         }
1727         else {
1728                 bGPdata *gpd;
1729
1730                 /* Grab all Grease Pencil datablocks directly from main, but only those that seem to be useful somewhere */
1731                 for (gpd = ac->bmain->gpencil.first; gpd; gpd = gpd->id.next) {
1732                         /* only show if gpd is used by something... */
1733                         if (ID_REAL_USERS(gpd) < 1)
1734                                 continue;
1735
1736                         /* add GP frames from this datablock */
1737                         items += animdata_filter_gpencil_data(anim_data, ads, gpd, filter_mode);
1738                 }
1739         }
1740
1741         /* return the number of items added to the list */
1742         return items;
1743 }
1744
1745 /* Helper for Grease Pencil data integrated with main DopeSheet */
1746 static size_t animdata_filter_ds_gpencil(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, bGPdata *gpd, int filter_mode)
1747 {
1748         ListBase tmp_data = {NULL, NULL};
1749         size_t tmp_items = 0;
1750         size_t items = 0;
1751
1752         /* add relevant animation channels for Grease Pencil */
1753         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_GPD(gpd))
1754         {
1755                 /* add animation channels */
1756                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, &gpd->id, filter_mode);
1757
1758                 /* add Grease Pencil layers */
1759                 // TODO: do these need a separate expander?
1760                 // XXX:  what order should these go in?
1761         }
1762         END_ANIMFILTER_SUBCHANNELS;
1763
1764         /* did we find anything? */
1765         if (tmp_items) {
1766                 /* include data-expand widget first */
1767                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1768                         /* check if filtering by active status */
1769                         // XXX: active check here needs checking
1770                         if (ANIMCHANNEL_ACTIVEOK(gpd)) {
1771                                 ANIMCHANNEL_NEW_CHANNEL(gpd, ANIMTYPE_DSGPENCIL, gpd);
1772                         }
1773                 }
1774
1775                 /* now add the list of collected channels */
1776                 BLI_movelisttolist(anim_data, &tmp_data);
1777                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
1778                 items += tmp_items;
1779         }
1780
1781         /* return the number of items added to the list */
1782         return items;
1783 }
1784
1785 /* Helper for Cache File data integrated with main DopeSheet */
1786 static size_t animdata_filter_ds_cachefile(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, CacheFile *cache_file, int filter_mode)
1787 {
1788         ListBase tmp_data = {NULL, NULL};
1789         size_t tmp_items = 0;
1790         size_t items = 0;
1791
1792         /* add relevant animation channels for Cache File */
1793         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_CACHEFILE_OBJD(cache_file))
1794         {
1795                 /* add animation channels */
1796                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, &cache_file->id, filter_mode);
1797         }
1798         END_ANIMFILTER_SUBCHANNELS;
1799
1800         /* did we find anything? */
1801         if (tmp_items) {
1802                 /* include data-expand widget first */
1803                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1804                         /* check if filtering by active status */
1805                         // XXX: active check here needs checking
1806                         if (ANIMCHANNEL_ACTIVEOK(cache_file)) {
1807                                 ANIMCHANNEL_NEW_CHANNEL(cache_file, ANIMTYPE_DSCACHEFILE, cache_file);
1808                         }
1809                 }
1810
1811                 /* now add the list of collected channels */
1812                 BLI_movelisttolist(anim_data, &tmp_data);
1813                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
1814                 items += tmp_items;
1815         }
1816
1817         /* return the number of items added to the list */
1818         return items;
1819 }
1820
1821 /* Helper for Mask Editing - mask layers */
1822 static size_t animdata_filter_mask_data(ListBase *anim_data, Mask *mask, const int filter_mode)
1823 {
1824         MaskLayer *masklay_act = BKE_mask_layer_active(mask);
1825         MaskLayer *masklay;
1826         size_t items = 0;
1827
1828         /* loop over layers as the conditions are acceptable */
1829         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
1830                 /* only if selected */
1831                 if (ANIMCHANNEL_SELOK(SEL_MASKLAY(masklay)) ) {
1832                         /* only if editable */
1833                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_MASK(masklay)) {
1834                                 /* active... */
1835                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (masklay_act == masklay)) {
1836                                         /* add to list */
1837                                         ANIMCHANNEL_NEW_CHANNEL(masklay, ANIMTYPE_MASKLAYER, mask);
1838                                 }
1839                         }
1840                 }
1841         }
1842
1843         return items;
1844 }
1845
1846 /* Grab all mask data */
1847 static size_t animdata_filter_mask(Main *bmain, ListBase *anim_data, void *UNUSED(data), int filter_mode)
1848 {
1849         Mask *mask;
1850         size_t items = 0;
1851
1852         /* for now, grab mask datablocks directly from main */
1853         // XXX: this is not good...
1854         for (mask = bmain->mask.first; mask; mask = mask->id.next) {
1855                 ListBase tmp_data = {NULL, NULL};
1856                 size_t tmp_items = 0;
1857
1858                 /* only show if mask is used by something... */
1859                 if (ID_REAL_USERS(mask) < 1)
1860                         continue;
1861
1862                 /* add mask animation channels */
1863                 BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_MASK(mask))
1864                 {
1865                         tmp_items += animdata_filter_mask_data(&tmp_data, mask, filter_mode);
1866                 }
1867                 END_ANIMFILTER_SUBCHANNELS;
1868
1869                 /* did we find anything? */
1870                 if (tmp_items) {
1871                         /* include data-expand widget first */
1872                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1873                                 /* add gpd as channel too (if for drawing, and it has layers) */
1874                                 ANIMCHANNEL_NEW_CHANNEL(mask, ANIMTYPE_MASKDATABLOCK, NULL);
1875                         }
1876
1877                         /* now add the list of collected channels */
1878                         BLI_movelisttolist(anim_data, &tmp_data);
1879                         BLI_assert(BLI_listbase_is_empty(&tmp_data));
1880                         items += tmp_items;
1881                 }
1882         }
1883
1884         /* return the number of items added to the list */
1885         return items;
1886 }
1887
1888 /* NOTE: owner_id is scene, material, or texture block, which is the direct owner of the node tree in question */
1889 static size_t animdata_filter_ds_nodetree_group(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *owner_id, bNodeTree *ntree, int filter_mode)
1890 {
1891         ListBase tmp_data = {NULL, NULL};
1892         size_t tmp_items = 0;
1893         size_t items = 0;
1894
1895         /* add nodetree animation channels */
1896         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_NTREE_DATA(ntree))
1897         {
1898                 /* animation data filtering */
1899                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)ntree, filter_mode);
1900         }
1901         END_ANIMFILTER_SUBCHANNELS;
1902
1903         /* did we find anything? */
1904         if (tmp_items) {
1905                 /* include data-expand widget first */
1906                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1907                         /* check if filtering by active status */
1908                         if (ANIMCHANNEL_ACTIVEOK(ntree)) {
1909                                 ANIMCHANNEL_NEW_CHANNEL(ntree, ANIMTYPE_DSNTREE, owner_id);
1910                         }
1911                 }
1912
1913                 /* now add the list of collected channels */
1914                 BLI_movelisttolist(anim_data, &tmp_data);
1915                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
1916                 items += tmp_items;
1917         }
1918
1919         /* return the number of items added to the list */
1920         return items;
1921 }
1922
1923 static size_t animdata_filter_ds_nodetree(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *owner_id, bNodeTree *ntree, int filter_mode)
1924 {
1925         bNode *node;
1926         size_t items = 0;
1927
1928         items += animdata_filter_ds_nodetree_group(ac, anim_data, ads, owner_id, ntree, filter_mode);
1929
1930         for (node = ntree->nodes.first; node; node = node->next) {
1931                 if (node->type == NODE_GROUP) {
1932                         if (node->id) {
1933                                 if ((ads->filterflag & ADS_FILTER_ONLYSEL) && (node->flag & NODE_SELECT) == 0) {
1934                                         continue;
1935                                 }
1936                                 items += animdata_filter_ds_nodetree_group(ac, anim_data, ads, owner_id, (bNodeTree *) node->id,
1937                                                                            filter_mode | ANIMFILTER_TMP_IGNORE_ONLYSEL);
1938                         }
1939                 }
1940         }
1941
1942         return items;
1943 }
1944
1945 static size_t animdata_filter_ds_linestyle(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
1946 {
1947         SceneRenderLayer *srl;
1948         FreestyleLineSet *lineset;
1949         size_t items = 0;
1950
1951         for (srl = sce->r.layers.first; srl; srl = srl->next) {
1952                 for (lineset = srl->freestyleConfig.linesets.first; lineset; lineset = lineset->next) {
1953                         if (lineset->linestyle) {
1954                                 lineset->linestyle->id.tag |= LIB_TAG_DOIT;
1955                         }
1956                 }
1957         }
1958
1959         for (srl = sce->r.layers.first; srl; srl = srl->next) {
1960                 /* skip render layers without Freestyle enabled */
1961                 if (!(srl->layflag & SCE_LAY_FRS))
1962                         continue;
1963
1964                 /* loop over linesets defined in the render layer */
1965                 for (lineset = srl->freestyleConfig.linesets.first; lineset; lineset = lineset->next) {
1966                         FreestyleLineStyle *linestyle = lineset->linestyle;
1967                         ListBase tmp_data = {NULL, NULL};
1968                         size_t tmp_items = 0;
1969
1970                         if ((linestyle == NULL) ||
1971                             !(linestyle->id.tag & LIB_TAG_DOIT))
1972                         {
1973                                 continue;
1974                         }
1975                         linestyle->id.tag &= ~LIB_TAG_DOIT;
1976
1977                         /* add scene-level animation channels */
1978                         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_LS_SCED(linestyle))
1979                         {
1980                                 /* animation data filtering */
1981                                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)linestyle, filter_mode);
1982                         }
1983                         END_ANIMFILTER_SUBCHANNELS;
1984
1985                         /* did we find anything? */
1986                         if (tmp_items) {
1987                                 /* include anim-expand widget first */
1988                                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1989                                         /* check if filtering by active status */
1990                                         if (ANIMCHANNEL_ACTIVEOK(linestyle)) {
1991                                                 ANIMCHANNEL_NEW_CHANNEL(linestyle, ANIMTYPE_DSLINESTYLE, sce);
1992                                         }
1993                                 }
1994
1995                                 /* now add the list of collected channels */
1996                                 BLI_movelisttolist(anim_data, &tmp_data);
1997                                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
1998                                 items += tmp_items;
1999                         }
2000                 }
2001         }
2002
2003         /* return the number of items added to the list */
2004         return items;
2005 }
2006
2007 static size_t animdata_filter_ds_texture(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads,
2008                                          Tex *tex, ID *owner_id, int filter_mode)
2009 {
2010         ListBase tmp_data = {NULL, NULL};
2011         size_t tmp_items = 0;
2012         size_t items = 0;
2013
2014         /* add texture's animation data to temp collection */
2015         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_TEX_DATA(tex))
2016         {
2017                 /* texture animdata */
2018                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)tex, filter_mode);
2019
2020                 /* nodes */
2021                 if ((tex->nodetree) && !(ads->filterflag & ADS_FILTER_NONTREE)) {
2022                         /* owner_id as id instead of texture, since it'll otherwise be impossible to track the depth */
2023                         // FIXME: perhaps as a result, textures should NOT be included under materials, but under their own section instead
2024                         // so that free-floating textures can also be animated
2025                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)tex, tex->nodetree, filter_mode);
2026                 }
2027         }
2028         END_ANIMFILTER_SUBCHANNELS;
2029
2030         /* did we find anything? */
2031         if (tmp_items) {
2032                 /* include texture-expand widget? */
2033                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2034                         /* check if filtering by active status */
2035                         if (ANIMCHANNEL_ACTIVEOK(tex)) {
2036                                 ANIMCHANNEL_NEW_CHANNEL(tex, ANIMTYPE_DSTEX, owner_id);
2037                         }
2038                 }
2039
2040                 /* now add the list of collected channels */
2041                 BLI_movelisttolist(anim_data, &tmp_data);
2042                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2043                 items += tmp_items;
2044         }
2045
2046         /* return the number of items added to the list */
2047         return items;
2048 }
2049
2050 /* NOTE: owner_id is either material, lamp, or world block, which is the direct owner of the texture stack in question */
2051 static size_t animdata_filter_ds_textures(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *owner_id, int filter_mode)
2052 {
2053         MTex **mtex = NULL;
2054         size_t items = 0;
2055         int a = 0;
2056
2057         /* get datatype specific data first */
2058         if (owner_id == NULL)
2059                 return 0;
2060
2061         switch (GS(owner_id->name)) {
2062                 case ID_MA:
2063                 {
2064                         Material *ma = (Material *)owner_id;
2065                         mtex = (MTex **)(&ma->mtex);
2066                         break;
2067                 }
2068                 case ID_LA:
2069                 {
2070                         Lamp *la = (Lamp *)owner_id;
2071                         mtex = (MTex **)(&la->mtex);
2072                         break;
2073                 }
2074                 case ID_WO:
2075                 {
2076                         World *wo = (World *)owner_id;
2077                         mtex = (MTex **)(&wo->mtex);
2078                         break;
2079                 }
2080                 case ID_PA:
2081                 {
2082                         ParticleSettings *part = (ParticleSettings *)owner_id;
2083                         mtex = (MTex **)(&part->mtex);
2084                         break;
2085                 }
2086                 default:
2087                 {
2088                         /* invalid/unsupported option */
2089                         if (G.debug & G_DEBUG)
2090                                 printf("ERROR: Unsupported owner_id (i.e. texture stack) for filter textures - %s\n", owner_id->name);
2091                         return 0;
2092                 }
2093         }
2094
2095         /* firstly check that we actuallly have some textures, by gathering all textures in a temp list */
2096         for (a = 0; a < MAX_MTEX; a++) {
2097                 Tex *tex = (mtex[a]) ? mtex[a]->tex : NULL;
2098
2099                 /* for now, if no texture returned, skip (this shouldn't confuse the user I hope) */
2100                 if (tex == NULL)
2101                         continue;
2102
2103                 /* add texture's anim channels */
2104                 items += animdata_filter_ds_texture(ac, anim_data, ads, tex, owner_id, filter_mode);
2105         }
2106
2107         /* return the number of items added to the list */
2108         return items;
2109 }
2110
2111
2112 static size_t animdata_filter_ds_material(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Material *ma, int filter_mode)
2113 {
2114         ListBase tmp_data = {NULL, NULL};
2115         size_t tmp_items = 0;
2116         size_t items = 0;
2117
2118         /* add material's animation data to temp collection */
2119         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_MAT_OBJD(ma))
2120         {
2121                 /* material's animation data */
2122                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)ma, filter_mode);
2123
2124                 /* textures */
2125                 if (!(ads->filterflag & ADS_FILTER_NOTEX))
2126                         tmp_items += animdata_filter_ds_textures(ac, &tmp_data, ads, (ID *)ma, filter_mode);
2127
2128                 /* nodes */
2129                 if ((ma->nodetree) && !(ads->filterflag & ADS_FILTER_NONTREE))
2130                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)ma, ma->nodetree, filter_mode);
2131         }
2132         END_ANIMFILTER_SUBCHANNELS;
2133
2134         /* did we find anything? */
2135         if (tmp_items) {
2136                 /* include material-expand widget first */
2137                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2138                         /* check if filtering by active status */
2139                         if (ANIMCHANNEL_ACTIVEOK(ma)) {
2140                                 ANIMCHANNEL_NEW_CHANNEL(ma, ANIMTYPE_DSMAT, ma);
2141                         }
2142                 }
2143
2144                 /* now add the list of collected channels */
2145                 BLI_movelisttolist(anim_data, &tmp_data);
2146                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2147                 items += tmp_items;
2148         }
2149
2150         return items;
2151 }
2152
2153 static size_t animdata_filter_ds_materials(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
2154 {
2155         bool has_nested = false;
2156         size_t items = 0;
2157         int a = 0;
2158
2159         /* first pass: take the materials referenced via the Material slots of the object */
2160         for (a = 1; a <= ob->totcol; a++) {
2161                 Material *ma = give_current_material(ob, a);
2162
2163                 /* if material is valid, try to add relevant contents from here */
2164                 if (ma) {
2165                         /* add channels */
2166                         items += animdata_filter_ds_material(ac, anim_data, ads, ma, filter_mode);
2167
2168                         /* for optimising second pass - check if there's a nested material here to come back for */
2169                         if (has_nested == false) {
2170                                 has_nested = (give_node_material(ma) != NULL);
2171                         }
2172                 }
2173         }
2174
2175         /* second pass: go through a second time looking for "nested" materials (material.material references)
2176          *
2177          * NOTE: here we ignore the expanded status of the parent, as it could be too confusing as to why these are
2178          *       disappearing/not available, since the relationships between these is not that clear
2179          */
2180         if (has_nested) {
2181                 for (a = 1; a <= ob->totcol; a++) {
2182                         Material *base = give_current_material(ob, a);
2183                         Material *ma   = give_node_material(base);
2184
2185                         /* add channels from the nested material if it exists
2186                          *   - skip if the same material is referenced in its node tree
2187                          *     (which is common for BI materials) as that results in
2188                          *     confusing duplicates
2189                          */
2190                         if ((ma) && (ma != base)) {
2191                                 items += animdata_filter_ds_material(ac, anim_data, ads, ma, filter_mode);
2192                         }
2193                 }
2194         }
2195
2196         /* return the number of items added to the list */
2197         return items;
2198 }
2199
2200
2201 /* ............ */
2202
2203 /* Temporary context for modifier linked-data channel extraction */
2204 typedef struct tAnimFilterModifiersContext {
2205         bAnimContext *ac;       /* anim editor context */
2206         bDopeSheet *ads;    /* dopesheet filtering settings */
2207
2208         ListBase tmp_data;  /* list of channels created (but not yet added to the main list) */
2209         size_t items;       /* number of channels created */
2210
2211         int filter_mode;    /* flags for stuff we want to filter */
2212 } tAnimFilterModifiersContext;
2213
2214
2215 /* dependency walker callback for modifier dependencies */
2216 static void animfilter_modifier_idpoin_cb(void *afm_ptr, Object *ob, ID **idpoin, int UNUSED(cb_flag))
2217 {
2218         tAnimFilterModifiersContext *afm = (tAnimFilterModifiersContext *)afm_ptr;
2219         ID *owner_id = &ob->id;
2220         ID *id = *idpoin;
2221
2222         /* NOTE: the walker only guarantees to give us all the ID-ptr *slots*,
2223          * not just the ones which are actually used, so be careful!
2224          */
2225         if (id == NULL)
2226                 return;
2227
2228         /* check if this is something we're interested in... */
2229         switch (GS(id->name)) {
2230                 case ID_TE: /* Textures */
2231                 {
2232                         Tex *tex = (Tex *)id;
2233                         if (!(afm->ads->filterflag & ADS_FILTER_NOTEX)) {
2234                                 afm->items += animdata_filter_ds_texture(afm->ac, &afm->tmp_data, afm->ads, tex, owner_id, afm->filter_mode);
2235                         }
2236                         break;
2237                 }
2238
2239                 /* TODO: images? */
2240                 default:
2241                         break;
2242         }
2243 }
2244
2245 /* animation linked to data used by modifiers
2246  * NOTE: strictly speaking, modifier animation is already included under Object level
2247  *       but for some modifiers (e.g. Displace), there can be linked data that has settings
2248  *       which would be nice to animate (i.e. texture parameters) but which are not actually
2249  *       attached to any other objects/materials/etc. in the scene
2250  */
2251 // TODO: do we want an expander for this?
2252 static size_t animdata_filter_ds_modifiers(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
2253 {
2254         tAnimFilterModifiersContext afm = {NULL};
2255         size_t items = 0;
2256
2257         /* 1) create a temporary "context" containing all the info we have here to pass to the callback
2258          *    use to walk through the dependencies of the modifiers
2259          *
2260          * ! Assumes that all other unspecified values (i.e. accumulation buffers) are zero'd out properly
2261          */
2262         afm.ac          = ac;
2263         afm.ads         = ads;
2264         afm.filter_mode = filter_mode;
2265
2266         /* 2) walk over dependencies */
2267         modifiers_foreachIDLink(ob, animfilter_modifier_idpoin_cb, &afm);
2268
2269         /* 3) extract data from the context, merging it back into the standard list */
2270         if (afm.items) {
2271                 /* now add the list of collected channels */
2272                 BLI_movelisttolist(anim_data, &afm.tmp_data);
2273                 BLI_assert(BLI_listbase_is_empty(&afm.tmp_data));
2274                 items += afm.items;
2275         }
2276
2277         return items;
2278 }
2279
2280 /* ............ */
2281
2282
2283 static size_t animdata_filter_ds_particles(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
2284 {
2285         ParticleSystem *psys;
2286         size_t items = 0;
2287
2288         for (psys = ob->particlesystem.first; psys; psys = psys->next) {
2289                 ListBase tmp_data = {NULL, NULL};
2290                 size_t tmp_items = 0;
2291
2292                 /* if no material returned, skip - so that we don't get weird blank entries... */
2293                 if (ELEM(NULL, psys->part, psys->part->adt))
2294                         continue;
2295
2296                 /* add particle-system's animation data to temp collection */
2297                 BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_PART_OBJD(psys->part))
2298                 {
2299                         /* particle system's animation data */
2300                         tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)psys->part, filter_mode);
2301
2302                         /* textures */
2303                         if (!(ads->filterflag & ADS_FILTER_NOTEX))
2304                                 tmp_items += animdata_filter_ds_textures(ac, &tmp_data, ads, (ID *)psys->part, filter_mode);
2305                 }
2306                 END_ANIMFILTER_SUBCHANNELS;
2307
2308                 /* did we find anything? */
2309                 if (tmp_items) {
2310                         /* include particle-expand widget first */
2311                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2312                                 /* check if filtering by active status */
2313                                 if (ANIMCHANNEL_ACTIVEOK(psys->part)) {
2314                                         ANIMCHANNEL_NEW_CHANNEL(psys->part, ANIMTYPE_DSPART, psys->part);
2315                                 }
2316                         }
2317
2318                         /* now add the list of collected channels */
2319                         BLI_movelisttolist(anim_data, &tmp_data);
2320                         BLI_assert(BLI_listbase_is_empty(&tmp_data));
2321                         items += tmp_items;
2322                 }
2323         }
2324
2325         /* return the number of items added to the list */
2326         return items;
2327 }
2328
2329
2330 static size_t animdata_filter_ds_obdata(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
2331 {
2332         ListBase tmp_data = {NULL, NULL};
2333         size_t tmp_items = 0;
2334         size_t items = 0;
2335
2336         IdAdtTemplate *iat = ob->data;
2337         short type = 0, expanded = 0;
2338
2339         /* get settings based on data type */
2340         switch (ob->type) {
2341                 case OB_CAMERA: /* ------- Camera ------------ */
2342                 {
2343                         Camera *ca = (Camera *)ob->data;
2344
2345                         if (ads->filterflag & ADS_FILTER_NOCAM)
2346                                 return 0;
2347
2348                         type = ANIMTYPE_DSCAM;
2349                         expanded = FILTER_CAM_OBJD(ca);
2350                         break;
2351                 }
2352                 case OB_LAMP: /* ---------- Lamp ----------- */
2353                 {
2354                         Lamp *la = (Lamp *)ob->data;
2355
2356                         if (ads->filterflag & ADS_FILTER_NOLAM)
2357                                 return 0;
2358
2359                         type = ANIMTYPE_DSLAM;
2360                         expanded = FILTER_LAM_OBJD(la);
2361                         break;
2362                 }
2363                 case OB_CURVE: /* ------- Curve ---------- */
2364                 case OB_SURF: /* ------- Nurbs Surface ---------- */
2365                 case OB_FONT: /* ------- Text Curve ---------- */
2366                 {
2367                         Curve *cu = (Curve *)ob->data;
2368
2369                         if (ads->filterflag & ADS_FILTER_NOCUR)
2370                                 return 0;
2371
2372                         type = ANIMTYPE_DSCUR;
2373                         expanded = FILTER_CUR_OBJD(cu);
2374                         break;
2375                 }
2376                 case OB_MBALL: /* ------- MetaBall ---------- */
2377                 {
2378                         MetaBall *mb = (MetaBall *)ob->data;
2379
2380                         if (ads->filterflag & ADS_FILTER_NOMBA)
2381                                 return 0;
2382
2383                         type = ANIMTYPE_DSMBALL;
2384                         expanded = FILTER_MBALL_OBJD(mb);
2385                         break;
2386                 }
2387                 case OB_ARMATURE: /* ------- Armature ---------- */
2388                 {
2389                         bArmature *arm = (bArmature *)ob->data;
2390
2391                         if (ads->filterflag & ADS_FILTER_NOARM)
2392                                 return 0;
2393
2394                         type = ANIMTYPE_DSARM;
2395                         expanded = FILTER_ARM_OBJD(arm);
2396                         break;
2397                 }
2398                 case OB_MESH: /* ------- Mesh ---------- */
2399                 {
2400                         Mesh *me = (Mesh *)ob->data;
2401
2402                         if (ads->filterflag & ADS_FILTER_NOMESH)
2403                                 return 0;
2404
2405                         type = ANIMTYPE_DSMESH;
2406                         expanded = FILTER_MESH_OBJD(me);
2407                         break;
2408                 }
2409                 case OB_LATTICE: /* ---- Lattice ---- */
2410                 {
2411                         Lattice *lt = (Lattice *)ob->data;
2412
2413                         if (ads->filterflag & ADS_FILTER_NOLAT)
2414                                 return 0;
2415
2416                         type = ANIMTYPE_DSLAT;
2417                         expanded = FILTER_LATTICE_OBJD(lt);
2418                         break;
2419                 }
2420                 case OB_SPEAKER: /* ---------- Speaker ----------- */
2421                 {
2422                         Speaker *spk = (Speaker *)ob->data;
2423
2424                         type = ANIMTYPE_DSSPK;
2425                         expanded = FILTER_SPK_OBJD(spk);
2426                         break;
2427                 }
2428         }
2429
2430         /* add object data animation channels */
2431         BEGIN_ANIMFILTER_SUBCHANNELS(expanded)
2432         {
2433                 /* animation data filtering */
2434                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)iat, filter_mode);
2435
2436                 /* sub-data filtering... */
2437                 switch (ob->type) {
2438                         case OB_LAMP:  /* lamp - textures + nodetree */
2439                         {
2440                                 Lamp *la = ob->data;
2441                                 bNodeTree *ntree = la->nodetree;
2442
2443                                 /* nodetree */
2444                                 if ((ntree) && !(ads->filterflag & ADS_FILTER_NONTREE))
2445                                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, &la->id, ntree, filter_mode);
2446
2447                                 /* textures */
2448                                 if (!(ads->filterflag & ADS_FILTER_NOTEX))
2449                                         tmp_items += animdata_filter_ds_textures(ac, &tmp_data, ads, &la->id, filter_mode);
2450                                 break;
2451                         }
2452                 }
2453         }
2454         END_ANIMFILTER_SUBCHANNELS;
2455
2456         /* did we find anything? */
2457         if (tmp_items) {
2458                 /* include data-expand widget first */
2459                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2460                         /* check if filtering by active status */
2461                         if (ANIMCHANNEL_ACTIVEOK(iat)) {
2462                                 ANIMCHANNEL_NEW_CHANNEL(iat, type, iat);
2463                         }
2464                 }
2465
2466                 /* now add the list of collected channels */
2467                 BLI_movelisttolist(anim_data, &tmp_data);
2468                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2469                 items += tmp_items;
2470         }
2471
2472         /* return the number of items added to the list */
2473         return items;
2474 }
2475
2476 /* shapekey-level animation */
2477 static size_t animdata_filter_ds_keyanim(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, Key *key, int filter_mode)
2478 {
2479         ListBase tmp_data = {NULL, NULL};
2480         size_t tmp_items = 0;
2481         size_t items = 0;
2482
2483         /* add shapekey-level animation channels */
2484         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_SKE_OBJD(key))
2485         {
2486                 /* animation data filtering */
2487                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)key, filter_mode);
2488         }
2489         END_ANIMFILTER_SUBCHANNELS;
2490
2491         /* did we find anything? */
2492         if (tmp_items) {
2493                 /* include key-expand widget first */
2494                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2495                         if (ANIMCHANNEL_ACTIVEOK(key)) {
2496                                 ANIMCHANNEL_NEW_CHANNEL(key, ANIMTYPE_DSSKEY, ob);
2497                         }
2498                 }
2499
2500                 /* now add the list of collected channels */
2501                 BLI_movelisttolist(anim_data, &tmp_data);
2502                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2503                 items += tmp_items;
2504         }
2505
2506         /* return the number of items added to the list */
2507         return items;
2508 }
2509
2510
2511 /* object-level animation */
2512 static size_t animdata_filter_ds_obanim(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
2513 {
2514         ListBase tmp_data = {NULL, NULL};
2515         size_t tmp_items = 0;
2516         size_t items = 0;
2517
2518         AnimData *adt = ob->adt;
2519         short type = 0, expanded = 1;
2520         void *cdata = NULL;
2521
2522         /* determine the type of expander channels to use */
2523         /* this is the best way to do this for now... */
2524         ANIMDATA_FILTER_CASES(ob,
2525                 { /* AnimData - no channel, but consider data */ },
2526                 { /* NLA - no channel, but consider data */ },
2527                 { /* Drivers */
2528                         type = ANIMTYPE_FILLDRIVERS;
2529                         cdata = adt;
2530                         expanded = EXPANDED_DRVD(adt);
2531                 },
2532                 { /* NLA Strip Controls - no dedicated channel for now (XXX) */ },
2533                 { /* Keyframes */
2534                         type = ANIMTYPE_FILLACTD;
2535                         cdata = adt->action;
2536                         expanded = EXPANDED_ACTC(adt->action);
2537                 });
2538
2539         /* add object-level animation channels */
2540         BEGIN_ANIMFILTER_SUBCHANNELS(expanded)
2541         {
2542                 /* animation data filtering */
2543                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)ob, filter_mode);
2544         }
2545         END_ANIMFILTER_SUBCHANNELS;
2546
2547         /* did we find anything? */
2548         if (tmp_items) {
2549                 /* include anim-expand widget first */
2550                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2551                         if (type != ANIMTYPE_NONE) {
2552                                 /* NOTE: active-status (and the associated checks) don't apply here... */
2553                                 ANIMCHANNEL_NEW_CHANNEL(cdata, type, ob);
2554                         }
2555                 }
2556
2557                 /* now add the list of collected channels */
2558                 BLI_movelisttolist(anim_data, &tmp_data);
2559                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2560                 items += tmp_items;
2561         }
2562
2563         /* return the number of items added to the list */
2564         return items;
2565 }
2566
2567 /* get animation channels from object2 */
2568 static size_t animdata_filter_dopesheet_ob(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
2569 {
2570         ListBase tmp_data = {NULL, NULL};
2571         Object *ob = base->object;
2572         size_t tmp_items = 0;
2573         size_t items = 0;
2574
2575         /* filter data contained under object first */
2576         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_OBJC(ob))
2577         {
2578                 Key *key = BKE_key_from_object(ob);
2579
2580                 /* object-level animation */
2581                 if ((ob->adt) && !(ads->filterflag & ADS_FILTER_NOOBJ)) {
2582                         tmp_items += animdata_filter_ds_obanim(ac, &tmp_data, ads, ob, filter_mode);
2583                 }
2584
2585                 /* shape-key */
2586                 if ((key && key->adt) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
2587                         tmp_items += animdata_filter_ds_keyanim(ac, &tmp_data, ads, ob, key, filter_mode);
2588                 }
2589
2590                 /* modifiers */
2591                 if ((ob->modifiers.first) && !(ads->filterflag & ADS_FILTER_NOMODIFIERS)) {
2592                         tmp_items += animdata_filter_ds_modifiers(ac, &tmp_data, ads, ob, filter_mode);
2593                 }
2594
2595                 /* materials */
2596                 if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT)) {
2597                         tmp_items += animdata_filter_ds_materials(ac, &tmp_data, ads, ob, filter_mode);
2598                 }
2599
2600                 /* object data */
2601                 if (ob->data) {
2602                         tmp_items += animdata_filter_ds_obdata(ac, &tmp_data, ads, ob, filter_mode);
2603                 }
2604
2605                 /* particles */
2606                 if ((ob->particlesystem.first) && !(ads->filterflag & ADS_FILTER_NOPART)) {
2607                         tmp_items += animdata_filter_ds_particles(ac, &tmp_data, ads, ob, filter_mode);
2608                 }
2609
2610                 /* grease pencil */
2611                 if ((ob->gpd) && !(ads->filterflag & ADS_FILTER_NOGPENCIL)) {
2612                         tmp_items += animdata_filter_ds_gpencil(ac, &tmp_data, ads, ob->gpd, filter_mode);
2613                 }
2614         }
2615         END_ANIMFILTER_SUBCHANNELS;
2616
2617
2618         /* if we collected some channels, add these to the new list... */
2619         if (tmp_items) {
2620                 /* firstly add object expander if required */
2621                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2622                         /* check if filtering by selection */
2623                         // XXX: double-check on this - most of the time, a lot of tools need to filter out these channels!
2624                         if (ANIMCHANNEL_SELOK((base->flag & SELECT))) {
2625                                 /* check if filtering by active status */
2626                                 if (ANIMCHANNEL_ACTIVEOK(ob)) {
2627                                         ANIMCHANNEL_NEW_CHANNEL(base, ANIMTYPE_OBJECT, ob);
2628                                 }
2629                         }
2630                 }
2631
2632                 /* now add the list of collected channels */
2633                 BLI_movelisttolist(anim_data, &tmp_data);
2634                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2635                 items += tmp_items;
2636         }
2637
2638         /* return the number of items added */
2639         return items;
2640 }
2641
2642 static size_t animdata_filter_ds_world(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, World *wo, int filter_mode)
2643 {
2644         ListBase tmp_data = {NULL, NULL};
2645         size_t tmp_items = 0;
2646         size_t items = 0;
2647
2648         /* add world animation channels */
2649         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_WOR_SCED(wo))
2650         {
2651                 /* animation data filtering */
2652                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)wo, filter_mode);
2653
2654                 /* textures for world */
2655                 if (!(ads->filterflag & ADS_FILTER_NOTEX))
2656                         tmp_items += animdata_filter_ds_textures(ac, &tmp_data, ads, (ID *)wo, filter_mode);
2657
2658                 /* nodes */
2659                 if ((wo->nodetree) && !(ads->filterflag & ADS_FILTER_NONTREE))
2660                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)wo, wo->nodetree, filter_mode);
2661         }
2662         END_ANIMFILTER_SUBCHANNELS;
2663
2664         /* did we find anything? */
2665         if (tmp_items) {
2666                 /* include data-expand widget first */
2667                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2668                         /* check if filtering by active status */
2669                         if (ANIMCHANNEL_ACTIVEOK(wo)) {
2670                                 ANIMCHANNEL_NEW_CHANNEL(wo, ANIMTYPE_DSWOR, sce);
2671                         }
2672                 }
2673
2674                 /* now add the list of collected channels */
2675                 BLI_movelisttolist(anim_data, &tmp_data);
2676                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2677                 items += tmp_items;
2678         }
2679
2680         /* return the number of items added to the list */
2681         return items;
2682 }
2683
2684 static size_t animdata_filter_ds_scene(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
2685 {
2686         ListBase tmp_data = {NULL, NULL};
2687         size_t tmp_items = 0;
2688         size_t items = 0;
2689
2690         AnimData *adt = sce->adt;
2691         short type = 0, expanded = 1;
2692         void *cdata = NULL;
2693
2694         /* determine the type of expander channels to use */
2695         // this is the best way to do this for now...
2696         ANIMDATA_FILTER_CASES(sce,
2697                 { /* AnimData - no channel, but consider data */},
2698                 { /* NLA - no channel, but consider data */},
2699                 { /* Drivers */
2700                         type = ANIMTYPE_FILLDRIVERS;
2701                         cdata = adt;
2702                         expanded = EXPANDED_DRVD(adt);
2703                 },
2704                 { /* NLA Strip Controls - no dedicated channel for now (XXX) */ },
2705                 { /* Keyframes */
2706                         type = ANIMTYPE_FILLACTD;
2707                         cdata = adt->action;
2708                         expanded = EXPANDED_ACTC(adt->action);
2709                 });
2710
2711         /* add scene-level animation channels */
2712         BEGIN_ANIMFILTER_SUBCHANNELS(expanded)
2713         {
2714                 /* animation data filtering */
2715                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)sce, filter_mode);
2716         }
2717         END_ANIMFILTER_SUBCHANNELS;
2718
2719         /* did we find anything? */
2720         if (tmp_items) {
2721                 /* include anim-expand widget first */
2722                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2723                         if (type != ANIMTYPE_NONE) {
2724                                 /* NOTE: active-status (and the associated checks) don't apply here... */
2725                                 ANIMCHANNEL_NEW_CHANNEL(cdata, type, sce);
2726                         }
2727                 }
2728
2729                 /* now add the list of collected channels */
2730                 BLI_movelisttolist(anim_data, &tmp_data);
2731                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2732                 items += tmp_items;
2733         }
2734
2735         /* return the number of items added to the list */
2736         return items;
2737 }
2738
2739 static size_t animdata_filter_dopesheet_scene(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
2740 {
2741         ListBase tmp_data = {NULL, NULL};
2742         size_t tmp_items = 0;
2743         size_t items = 0;
2744
2745         /* filter data contained under object first */
2746         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_SCEC(sce))
2747         {
2748                 bNodeTree *ntree = sce->nodetree;
2749                 bGPdata *gpd = sce->gpd;
2750                 World *wo = sce->world;
2751
2752                 /* Action, Drivers, or NLA for Scene */
2753                 if ((ads->filterflag & ADS_FILTER_NOSCE) == 0) {
2754                         tmp_items += animdata_filter_ds_scene(ac, &tmp_data, ads, sce, filter_mode);
2755                 }
2756
2757                 /* world */
2758                 if ((wo) && !(ads->filterflag & ADS_FILTER_NOWOR)) {
2759                         tmp_items += animdata_filter_ds_world(ac, &tmp_data, ads, sce, wo, filter_mode);
2760                 }
2761
2762                 /* nodetree */
2763                 if ((ntree) && !(ads->filterflag & ADS_FILTER_NONTREE)) {
2764                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)sce, ntree, filter_mode);
2765                 }
2766
2767                 /* line styles */
2768                 if ((ads->filterflag & ADS_FILTER_NOLINESTYLE) == 0) {
2769                         tmp_items += animdata_filter_ds_linestyle(ac, &tmp_data, ads, sce, filter_mode);
2770                 }
2771
2772                 /* grease pencil */
2773                 if ((gpd) && !(ads->filterflag & ADS_FILTER_NOGPENCIL)) {
2774                         tmp_items += animdata_filter_ds_gpencil(ac, &tmp_data, ads, gpd, filter_mode);
2775                 }
2776
2777                 /* TODO: one day, when sequencer becomes its own datatype, perhaps it should be included here */
2778         }
2779         END_ANIMFILTER_SUBCHANNELS;
2780
2781         /* if we collected some channels, add these to the new list... */
2782         if (tmp_items) {
2783                 /* firstly add object expander if required */
2784                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2785                         /* check if filtering by selection */
2786                         if (ANIMCHANNEL_SELOK((sce->flag & SCE_DS_SELECTED))) {
2787                                 /* NOTE: active-status doesn't matter for this! */
2788                                 ANIMCHANNEL_NEW_CHANNEL(sce, ANIMTYPE_SCENE, sce);
2789                         }
2790                 }
2791
2792                 /* now add the list of collected channels */
2793                 BLI_movelisttolist(anim_data, &tmp_data);
2794                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2795                 items += tmp_items;
2796         }
2797
2798         /* return the number of items added */
2799         return items;
2800 }
2801
2802 static size_t animdata_filter_ds_movieclip(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, MovieClip *clip, int filter_mode)
2803 {
2804         ListBase tmp_data = {NULL, NULL};
2805         size_t tmp_items = 0;
2806         size_t items = 0;
2807         /* add world animation channels */
2808         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_MCLIP(clip))
2809         {
2810                 /* animation data filtering */
2811                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)clip, filter_mode);
2812         }
2813         END_ANIMFILTER_SUBCHANNELS;
2814         /* did we find anything? */
2815         if (tmp_items) {
2816                 /* include data-expand widget first */
2817                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2818                         /* check if filtering by active status */
2819                         if (ANIMCHANNEL_ACTIVEOK(clip)) {
2820                                 ANIMCHANNEL_NEW_CHANNEL(clip, ANIMTYPE_DSMCLIP, clip);
2821                         }
2822                 }
2823                 /* now add the list of collected channels */
2824                 BLI_movelisttolist(anim_data, &tmp_data);
2825                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2826                 items += tmp_items;
2827         }
2828         /* return the number of items added to the list */
2829         return items;
2830 }
2831
2832 static size_t animdata_filter_dopesheet_movieclips(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, int filter_mode)
2833 {
2834         size_t items = 0;
2835         MovieClip *clip;
2836         for (clip = ac->bmain->movieclip.first; clip != NULL; clip = clip->id.next) {
2837                 /* only show if gpd is used by something... */
2838                 if (ID_REAL_USERS(clip) < 1) {
2839                         continue;
2840                 }
2841                 items += animdata_filter_ds_movieclip(ac, anim_data, ads, clip, filter_mode);
2842         }
2843         /* return the number of items added to the list */
2844         return items;
2845 }
2846
2847 /* Helper for animdata_filter_dopesheet() - For checking if an object should be included or not */
2848 static bool animdata_filter_base_is_ok(bDopeSheet *ads, Scene *scene, Base *base, int filter_mode)
2849 {
2850         Object *ob = base->object;
2851
2852         if (base->object == NULL)
2853                 return false;
2854
2855         /* firstly, check if object can be included, by the following factors:
2856          *      - if only visible, must check for layer and also viewport visibility
2857          *              --> while tools may demand only visible, user setting takes priority
2858          *                      as user option controls whether sets of channels get included while
2859          *                      tool-flag takes into account collapsed/open channels too
2860          *      - if only selected, must check if object is selected
2861          *      - there must be animation data to edit (this is done recursively as we
2862          *        try to add the channels)
2863          */
2864         if ((filter_mode & ANIMFILTER_DATA_VISIBLE) && !(ads->filterflag & ADS_FILTER_INCL_HIDDEN)) {
2865                 /* layer visibility - we check both object and base, since these may not be in sync yet */
2866                 if ((scene->lay & (ob->lay | base->lay)) == 0)
2867                         return false;
2868
2869                 /* outliner restrict-flag */
2870                 if (ob->restrictflag & OB_RESTRICT_VIEW)
2871                         return false;
2872         }
2873
2874         /* if only F-Curves with visible flags set can be shown, check that
2875          * datablock hasn't been set to invisible
2876          */
2877         if (filter_mode & ANIMFILTER_CURVE_VISIBLE) {
2878                 if ((ob->adt) && (ob->adt->flag & ADT_CURVES_NOT_VISIBLE))
2879                         return false;
2880         }
2881
2882         /* Pinned curves are visible regardless of selection flags. */
2883         if ((ob->adt) && (ob->adt->flag & ADT_CURVES_ALWAYS_VISIBLE)) {
2884                 return true;
2885         }
2886
2887         /* Special case.
2888          * We don't do recursive checks for pin, but we need to deal with tricky
2889          * setup like animated camera lens without animated camera location.
2890          * Without such special handle here we wouldn't be able to bin such
2891          * camera data only animation to the editor.
2892          */
2893         if (ob->adt == NULL && ob->data != NULL) {
2894                 AnimData *data_adt = BKE_animdata_from_id(ob->data);
2895                 if (data_adt != NULL && (data_adt->flag & ADT_CURVES_ALWAYS_VISIBLE)) {
2896                         return true;
2897                 }
2898         }
2899
2900         /* check selection and object type filters */
2901         if ((ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) /*|| (base == sce->basact)*/)) {
2902                 /* only selected should be shown */
2903                 return false;
2904         }
2905
2906         /* check if object belongs to the filtering group if option to filter
2907          * objects by the grouped status is on
2908          *      - used to ease the process of doing multiple-character choreographies
2909          */
2910         if (ads->filterflag & ADS_FILTER_ONLYOBGROUP) {
2911                 if (BKE_group_object_exists(ads->filter_grp, ob) == 0)
2912                         return false;
2913         }
2914
2915         /* no reason to exclude this object... */
2916         return true;
2917 }
2918
2919 /* Helper for animdata_filter_ds_sorted_bases() - Comparison callback for two Base pointers... */
2920 static int ds_base_sorting_cmp(const void *base1_ptr, const void *base2_ptr)
2921 {
2922         const Base *b1 = *((const Base **)base1_ptr);
2923         const Base *b2 = *((const Base **)base2_ptr);
2924
2925         return strcmp(b1->object->id.name + 2, b2->object->id.name + 2);
2926 }
2927
2928 /* Get a sorted list of all the bases - for inclusion in dopesheet (when drawing channels) */
2929 static Base **animdata_filter_ds_sorted_bases(bDopeSheet *ads, Scene *scene, int filter_mode, size_t *r_usable_bases)
2930 {
2931         /* Create an array with space for all the bases, but only containing the usable ones */
2932         size_t tot_bases = BLI_listbase_count(&scene->base);