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