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