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