Rename any instance of scene layer or render layer in code with view layer
[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 theem 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 *sl = ac->view_layer;
136         Object *ob;
137         Key *key;
138         
139         ob = OBACT(sl);
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                         {
884                                 FCurve *fcu = (FCurve *)data;
885                                 
886                                 ale->flag = fcu->flag;
887                                 
888                                 ale->key_data = fcu;
889                                 ale->datatype = ALE_FCURVE;
890                                 break;
891                         }
892                         case ANIMTYPE_SHAPEKEY:
893                         {
894                                 KeyBlock *kb = (KeyBlock *)data;
895                                 Key *key = (Key *)ale->id;
896                                 
897                                 ale->flag = kb->flag;
898                                 
899                                 /* whether we have keyframes depends on whether there is a Key block to find it from */
900                                 if (key) {
901                                         /* index of shapekey is defined by place in key's list */
902                                         ale->index = BLI_findindex(&key->block, kb);
903                                         
904                                         /* the corresponding keyframes are from the animdata */
905                                         if (ale->adt && ale->adt->action) {
906                                                 bAction *act = ale->adt->action;
907                                                 char *rna_path = BKE_keyblock_curval_rnapath_get(key, kb);
908                                                 
909                                                 /* try to find the F-Curve which corresponds to this exactly,
910                                                  * then free the MEM_alloc'd string
911                                                  */
912                                                 if (rna_path) {
913                                                         ale->key_data = (void *)list_find_fcurve(&act->curves, rna_path, 0);
914                                                         MEM_freeN(rna_path);
915                                                 }
916                                         }
917                                         ale->datatype = (ale->key_data) ? ALE_FCURVE : ALE_NONE;
918                                 }
919                                 break;
920                         }
921                         case ANIMTYPE_GPLAYER:
922                         {
923                                 bGPDlayer *gpl = (bGPDlayer *)data;
924                                 
925                                 ale->flag = gpl->flag;
926                                 
927                                 ale->key_data = NULL;
928                                 ale->datatype = ALE_GPFRAME;
929                                 break;
930                         }
931                         case ANIMTYPE_MASKLAYER:
932                         {
933                                 MaskLayer *masklay = (MaskLayer *)data;
934                                 
935                                 ale->flag = masklay->flag;
936                                 
937                                 ale->key_data = NULL;
938                                 ale->datatype = ALE_MASKLAY;
939                                 break;
940                         }
941                         case ANIMTYPE_NLATRACK:
942                         {
943                                 NlaTrack *nlt = (NlaTrack *)data;
944                                 
945                                 ale->flag = nlt->flag;
946                                 
947                                 ale->key_data = &nlt->strips;
948                                 ale->datatype = ALE_NLASTRIP;
949                                 break;
950                         }
951                         case ANIMTYPE_NLAACTION:
952                         {
953                                 /* nothing to include for now... nothing editable from NLA-perspective here */
954                                 ale->key_data = NULL;
955                                 ale->datatype = ALE_NONE;
956                                 break;
957                         }
958                 }
959         }
960         
961         /* return created datatype */
962         return ale;
963 }
964  
965 /* ----------------------------------------- */
966
967 /* 'Only Selected' selected data and/or 'Include Hidden' filtering
968  * NOTE: when this function returns true, the F-Curve is to be skipped 
969  */
970 static bool skip_fcurve_selected_data(bDopeSheet *ads, FCurve *fcu, ID *owner_id, int filter_mode)
971 {
972         if (fcu->grp != NULL && fcu->grp->flag & ADT_CURVES_ALWAYS_VISIBLE) {
973                 return false;
974         }
975         /* hidden items should be skipped if we only care about visible data, but we aren't interested in hidden stuff */
976         const bool skip_hidden = (filter_mode & ANIMFILTER_DATA_VISIBLE) && !(ads->filterflag & ADS_FILTER_INCL_HIDDEN);
977         
978         if (GS(owner_id->name) == ID_OB) {
979                 Object *ob = (Object *)owner_id;
980                 
981                 /* only consider if F-Curve involves pose.bones */
982                 if ((fcu->rna_path) && strstr(fcu->rna_path, "pose.bones")) {
983                         bPoseChannel *pchan;
984                         char *bone_name;
985                         
986                         /* get bone-name, and check if this bone is selected */
987                         bone_name = BLI_str_quoted_substrN(fcu->rna_path, "pose.bones[");
988                         pchan = BKE_pose_channel_find_name(ob->pose, bone_name);
989                         if (bone_name) MEM_freeN(bone_name);
990                         
991                         /* check whether to continue or skip */
992                         if ((pchan) && (pchan->bone)) {
993                                 /* if only visible channels, skip if bone not visible unless user wants channels from hidden data too */
994                                 if (skip_hidden) {
995                                         bArmature *arm = (bArmature *)ob->data;
996                                         
997                                         /* skipping - not visible on currently visible layers */
998                                         if ((arm->layer & pchan->bone->layer) == 0)
999                                                 return true;
1000                                         /* skipping - is currently hidden */
1001                                         if (pchan->bone->flag & BONE_HIDDEN_P)
1002                                                 return true;
1003                                 }
1004                                 
1005                                 /* can only add this F-Curve if it is selected */
1006                                 if (ads->filterflag & ADS_FILTER_ONLYSEL) {
1007                                         if ((pchan->bone->flag & BONE_SELECTED) == 0)
1008                                                 return true;
1009                                 }
1010                         }
1011                 }
1012         }
1013         else if (GS(owner_id->name) == ID_SCE) {
1014                 Scene *scene = (Scene *)owner_id;
1015                 
1016                 /* only consider if F-Curve involves sequence_editor.sequences */
1017                 if ((fcu->rna_path) && strstr(fcu->rna_path, "sequences_all")) {
1018                         Editing *ed = BKE_sequencer_editing_get(scene, false);
1019                         Sequence *seq = NULL;
1020                         char *seq_name;
1021                         
1022                         if (ed) {
1023                                 /* get strip name, and check if this strip is selected */
1024                                 seq_name = BLI_str_quoted_substrN(fcu->rna_path, "sequences_all[");
1025                                 seq = BKE_sequence_get_by_name(ed->seqbasep, seq_name, false);
1026                                 if (seq_name) MEM_freeN(seq_name);
1027                         }
1028                         
1029                         /* can only add this F-Curve if it is selected */
1030                         if (ads->filterflag & ADS_FILTER_ONLYSEL) {
1031                                 if ((seq == NULL) || (seq->flag & SELECT) == 0)
1032                                         return true;
1033                         }
1034                 }
1035         }
1036         else if (GS(owner_id->name) == ID_NT) {
1037                 bNodeTree *ntree = (bNodeTree *)owner_id;
1038                 
1039                 /* check for selected nodes */
1040                 if ((fcu->rna_path) && strstr(fcu->rna_path, "nodes")) {
1041                         bNode *node;
1042                         char *node_name;
1043                         
1044                         /* get strip name, and check if this strip is selected */
1045                         node_name = BLI_str_quoted_substrN(fcu->rna_path, "nodes[");
1046                         node = nodeFindNodebyName(ntree, node_name);
1047                         if (node_name) MEM_freeN(node_name);
1048                         
1049                         /* can only add this F-Curve if it is selected */
1050                         if (ads->filterflag & ADS_FILTER_ONLYSEL) {
1051                                 if ((node) && (node->flag & NODE_SELECT) == 0)
1052                                         return true;
1053                         }
1054                 }
1055         }
1056         
1057         return false;
1058 }
1059
1060 /* Helper for name-based filtering - Perform "partial/fuzzy matches" (as in 80a7efd) */
1061 static bool name_matches_dopesheet_filter(bDopeSheet *ads, char *name)
1062 {
1063         if (ads->flag & ADS_FLAG_FUZZY_NAMES) {
1064                 /* full fuzzy, multi-word, case insensitive matches */
1065                 const size_t str_len = strlen(ads->searchstr);
1066                 const int words_max = (str_len / 2) + 1;
1067                 
1068                 int (*words)[2] = BLI_array_alloca(words, words_max);
1069                 const int words_len = BLI_string_find_split_words(ads->searchstr, str_len, ' ', words, words_max);
1070                 bool found = false;
1071                 
1072                 /* match name against all search words */
1073                 for (int index = 0; index < words_len; index++) {
1074                         if (BLI_strncasestr(name, ads->searchstr + words[index][0], words[index][1])) {
1075                                 found = true;
1076                                 break;
1077                         }
1078                 }
1079                 
1080                 /* if we have a match somewhere, this returns true */
1081                 return found;
1082         }
1083         else {
1084                 /* fallback/default - just case insensitive, but starts from start of word */
1085                 return BLI_strcasestr(name, ads->searchstr) != NULL;
1086         }
1087 }
1088
1089 /* (Display-)Name-based F-Curve filtering
1090  * NOTE: when this function returns true, the F-Curve is to be skipped 
1091  */
1092 static bool skip_fcurve_with_name(bDopeSheet *ads, FCurve *fcu, ID *owner_id)
1093 {
1094         bAnimListElem ale_dummy = {NULL};
1095         const bAnimChannelType *acf;
1096         
1097         /* create a dummy wrapper for the F-Curve */
1098         ale_dummy.type = ANIMTYPE_FCURVE;
1099         ale_dummy.id = owner_id;
1100         ale_dummy.data = fcu;
1101         
1102         /* get type info for channel */
1103         acf = ANIM_channel_get_typeinfo(&ale_dummy);
1104         if (acf && acf->name) {
1105                 char name[256]; /* hopefully this will be enough! */
1106                 
1107                 /* get name */
1108                 acf->name(&ale_dummy, name);
1109                 
1110                 /* check for partial match with the match string, assuming case insensitive filtering 
1111                  * if match, this channel shouldn't be ignored!
1112                  */
1113                 return !name_matches_dopesheet_filter(ads, name);
1114         }
1115         
1116         /* just let this go... */
1117         return true;
1118 }
1119
1120 /**
1121  * Check if F-Curve has errors and/or is disabled
1122  *
1123  * \return true if F-Curve has errors/is disabled
1124  */
1125 static bool fcurve_has_errors(FCurve *fcu)
1126 {
1127         /* F-Curve disabled - path eval error */
1128         if (fcu->flag & FCURVE_DISABLED) {
1129                 return true;
1130         }
1131         
1132         /* driver? */
1133         if (fcu->driver) {
1134                 ChannelDriver *driver = fcu->driver;
1135                 DriverVar *dvar;
1136                 
1137                 /* error flag on driver usually means that there is an error
1138                  * BUT this may not hold with PyDrivers as this flag gets cleared
1139                  *     if no critical errors prevent the driver from working...
1140                  */
1141                 if (driver->flag & DRIVER_FLAG_INVALID)
1142                         return true;
1143                         
1144                 /* check variables for other things that need linting... */
1145                 // TODO: maybe it would be more efficient just to have a quick flag for this?
1146                 for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
1147                         DRIVER_TARGETS_USED_LOOPER(dvar)
1148                         {
1149                                 if (dtar->flag & DTAR_FLAG_INVALID)
1150                                         return true;
1151                         }
1152                         DRIVER_TARGETS_LOOPER_END
1153                 }
1154         }
1155         
1156         /* no errors found */
1157         return false;
1158 }
1159
1160 /* find the next F-Curve that is usable for inclusion */
1161 static FCurve *animfilter_fcurve_next(bDopeSheet *ads, FCurve *first, bActionGroup *grp, int filter_mode, ID *owner_id)
1162 {
1163         FCurve *fcu = NULL;
1164         
1165         /* loop over F-Curves - assume that the caller of this has already checked that these should be included 
1166          * NOTE: we need to check if the F-Curves belong to the same group, as this gets called for groups too...
1167          */
1168         for (fcu = first; ((fcu) && (fcu->grp == grp)); fcu = fcu->next) {
1169                 /* special exception for Pose-Channel/Sequence-Strip/Node Based F-Curves:
1170                  *      - the 'Only Selected' and 'Include Hidden' data filters should be applied to sub-ID data which
1171                  *        can be independently selected/hidden, such as Pose-Channels, Sequence Strips, and Nodes.
1172                  *        Since these checks were traditionally done as first check for objects, we do the same here
1173                  *      - we currently use an 'approximate' method for getting these F-Curves that doesn't require
1174                  *        carefully checking the entire path
1175                  *      - this will also affect things like Drivers, and also works for Bone Constraints
1176                  */
1177                 if (ads && owner_id) {
1178                         if ((filter_mode & ANIMFILTER_TMP_IGNORE_ONLYSEL) == 0) {
1179                                 if ((ads->filterflag & ADS_FILTER_ONLYSEL) || (ads->filterflag & ADS_FILTER_INCL_HIDDEN) == 0) {
1180                                         if (skip_fcurve_selected_data(ads, fcu, owner_id, filter_mode))
1181                                                 continue;
1182                                 }
1183                         }
1184                 }
1185                 
1186                 /* only include if visible (Graph Editor check, not channels check) */
1187                 if (!(filter_mode & ANIMFILTER_CURVE_VISIBLE) || (fcu->flag & FCURVE_VISIBLE)) {
1188                         /* only work with this channel and its subchannels if it is editable */
1189                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_FCU(fcu)) {
1190                                 /* only include this curve if selected in a way consistent with the filtering requirements */
1191                                 if (ANIMCHANNEL_SELOK(SEL_FCU(fcu)) && ANIMCHANNEL_SELEDITOK(SEL_FCU(fcu))) {
1192                                         /* only include if this curve is active */
1193                                         if (!(filter_mode & ANIMFILTER_ACTIVE) || (fcu->flag & FCURVE_ACTIVE)) {
1194                                                 /* name based filtering... */
1195                                                 if ( ((ads) && (ads->filterflag & ADS_FILTER_BY_FCU_NAME)) && (owner_id) ) {
1196                                                         if (skip_fcurve_with_name(ads, fcu, owner_id))
1197                                                                 continue;
1198                                                 }
1199                                                 
1200                                                 /* error-based filtering... */
1201                                                 if ((ads) && (ads->filterflag & ADS_FILTER_ONLY_ERRORS)) {
1202                                                         /* skip if no errors... */
1203                                                         if (fcurve_has_errors(fcu) == false)
1204                                                                 continue;
1205                                                 }
1206                                                 
1207                                                 /* this F-Curve can be used, so return it */
1208                                                 return fcu;
1209                                         }
1210                                 }
1211                         }
1212                 }
1213         }
1214         
1215         /* no (more) F-Curves from the list are suitable... */
1216         return NULL;
1217 }
1218
1219 static size_t animfilter_fcurves(ListBase *anim_data, bDopeSheet *ads, FCurve *first, bActionGroup *grp, int filter_mode, ID *owner_id)
1220 {
1221         FCurve *fcu;
1222         size_t items = 0;
1223         
1224         /* loop over every F-Curve able to be included 
1225          *      - this for-loop works like this: 
1226          *              1) the starting F-Curve is assigned to the fcu pointer so that we have a starting point to search from
1227          *              2) the first valid F-Curve to start from (which may include the one given as 'first') in the remaining 
1228          *                 list of F-Curves is found, and verified to be non-null
1229          *              3) the F-Curve referenced by fcu pointer is added to the list
1230          *              4) the fcu pointer is set to the F-Curve after the one we just added, so that we can keep going through 
1231          *                 the rest of the F-Curve list without an eternal loop. Back to step 2 :)
1232          */
1233         for (fcu = first; ( (fcu = animfilter_fcurve_next(ads, fcu, grp, filter_mode, owner_id)) ); fcu = fcu->next) {
1234                 ANIMCHANNEL_NEW_CHANNEL(fcu, ANIMTYPE_FCURVE, owner_id);
1235         }
1236         
1237         /* return the number of items added to the list */
1238         return items;
1239 }
1240
1241 static size_t animfilter_act_group(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, bAction *UNUSED(act), bActionGroup *agrp, int filter_mode, ID *owner_id)
1242 {
1243         ListBase tmp_data = {NULL, NULL};
1244         size_t tmp_items = 0;
1245         size_t items = 0;
1246         //int ofilter = filter_mode;
1247         
1248         /* if we care about the selection status of the channels, 
1249          * but the group isn't expanded (1)...
1250          *  (1) this only matters if we actually care about the hierarchy though.
1251          *              - Hierarchy matters: this hack should be applied
1252          *              - Hierarchy ignored: cases like [#21276] won't work properly, unless we skip this hack
1253          */
1254         if ( ((filter_mode & ANIMFILTER_LIST_VISIBLE) && EXPANDED_AGRP(ac, agrp) == 0) &&     /* care about hierarchy but group isn't expanded */
1255              (filter_mode & (ANIMFILTER_SEL | ANIMFILTER_UNSEL)) )                          /* care about selection status */
1256         {
1257                 /* if the group itself isn't selected appropriately, we shouldn't consider it's children either */
1258                 if (ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) == 0)
1259                         return 0;
1260                 
1261                 /* if we're still here, then the selection status of the curves within this group should not matter,
1262                  * since this creates too much overhead for animators (i.e. making a slow workflow)
1263                  *
1264                  * Tools affected by this at time of coding (2010 Feb 09):
1265                  *      - inserting keyframes on selected channels only
1266                  *      - pasting keyframes
1267                  *      - creating ghost curves in Graph Editor
1268                  */
1269                 filter_mode &= ~(ANIMFILTER_SEL | ANIMFILTER_UNSEL | ANIMFILTER_LIST_VISIBLE);
1270         }
1271         
1272         /* add grouped F-Curves */
1273         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_AGRP(ac, agrp))
1274         {
1275                 /* special filter so that we can get just the F-Curves within the active group */
1276                 if (!(filter_mode & ANIMFILTER_ACTGROUPED) || (agrp->flag & AGRP_ACTIVE)) {
1277                         /* for the Graph Editor, curves may be set to not be visible in the view to lessen clutter,
1278                          * but to do this, we need to check that the group doesn't have it's not-visible flag set preventing 
1279                          * all its sub-curves to be shown
1280                          */
1281                         if (!(filter_mode & ANIMFILTER_CURVE_VISIBLE) || !(agrp->flag & AGRP_NOTVISIBLE)) {
1282                                 /* group must be editable for its children to be editable (if we care about this) */
1283                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_AGRP(agrp)) {
1284                                         /* get first F-Curve which can be used here */
1285                                         FCurve *first_fcu = animfilter_fcurve_next(ads, agrp->channels.first, agrp, filter_mode, owner_id);
1286                                         
1287                                         /* filter list, starting from this F-Curve */
1288                                         tmp_items += animfilter_fcurves(&tmp_data, ads, first_fcu, agrp, filter_mode, owner_id);
1289                                 }
1290                         }
1291                 }
1292         }
1293         END_ANIMFILTER_SUBCHANNELS;
1294         
1295         /* did we find anything? */
1296         if (tmp_items) {
1297                 /* add this group as a channel first */
1298                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1299                         /* restore original filter mode so that this next step works ok... */
1300                         //filter_mode = ofilter;
1301                         
1302                         /* filter selection of channel specially here again, since may be open and not subject to previous test */
1303                         if (ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) ) {
1304                                 ANIMCHANNEL_NEW_CHANNEL(agrp, ANIMTYPE_GROUP, owner_id);
1305                         }
1306                 }
1307                 
1308                 /* now add the list of collected channels */
1309                 BLI_movelisttolist(anim_data, &tmp_data);
1310                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
1311                 items += tmp_items;
1312         }
1313         
1314         /* return the number of items added to the list */
1315         return items;
1316 }
1317
1318 static size_t animfilter_action(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, bAction *act, int filter_mode, ID *owner_id)
1319 {
1320         bActionGroup *agrp;
1321         FCurve *lastchan = NULL;
1322         size_t items = 0;
1323         
1324         /* don't include anything from this action if it is linked in from another file,
1325          * and we're getting stuff for editing...
1326          */
1327         if ((filter_mode & ANIMFILTER_FOREDIT) && ID_IS_LINKED(act))
1328                 return 0;
1329                 
1330         /* do groups */
1331         // TODO: do nested groups?
1332         for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1333                 /* store reference to last channel of group */
1334                 if (agrp->channels.last) 
1335                         lastchan = agrp->channels.last;
1336                         
1337                 /* action group's channels */
1338                 items += animfilter_act_group(ac, anim_data, ads, act, agrp, filter_mode, owner_id);
1339         }
1340         
1341         /* un-grouped F-Curves (only if we're not only considering those channels in the active group) */
1342         if (!(filter_mode & ANIMFILTER_ACTGROUPED)) {
1343                 FCurve *firstfcu = (lastchan) ? (lastchan->next) : (act->curves.first);
1344                 items += animfilter_fcurves(anim_data, ads, firstfcu, NULL, filter_mode, owner_id);
1345         }
1346         
1347         /* return the number of items added to the list */
1348         return items;
1349 }
1350
1351 /* Include NLA-Data for NLA-Editor:
1352  *      - when ANIMFILTER_LIST_CHANNELS is used, that means we should be filtering the list for display
1353  *        Although the evaluation order is from the first track to the last and then apply the Action on top,
1354  *        we present this in the UI as the Active Action followed by the last track to the first so that we 
1355  *        get the evaluation order presented as per a stack.
1356  *      - for normal filtering (i.e. for editing), we only need the NLA-tracks but they can be in 'normal' evaluation
1357  *        order, i.e. first to last. Otherwise, some tools may get screwed up.
1358  */
1359 static size_t animfilter_nla(bAnimContext *UNUSED(ac), ListBase *anim_data, bDopeSheet *ads, AnimData *adt, int filter_mode, ID *owner_id)
1360 {
1361         NlaTrack *nlt;
1362         NlaTrack *first = NULL, *next = NULL;
1363         size_t items = 0;
1364         
1365         /* if showing channels, include active action */
1366         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1367                 /* if NLA action-line filtering is off, don't show unless there are keyframes, 
1368                  * in order to keep things more compact for doing transforms
1369                  */
1370                 if (!(ads->filterflag & ADS_FILTER_NLA_NOACT) || (adt->action)) {
1371                         /* there isn't really anything editable here, so skip if need editable */
1372                         if ((filter_mode & ANIMFILTER_FOREDIT) == 0) {
1373                                 /* just add the action track now (this MUST appear for drawing)
1374                                  *      - as AnimData may not have an action, we pass a dummy pointer just to get the list elem created, then
1375                                  *        overwrite this with the real value - REVIEW THIS...
1376                                  */
1377                                 ANIMCHANNEL_NEW_CHANNEL_FULL((void *)(&adt->action), ANIMTYPE_NLAACTION, owner_id, 
1378                                         {
1379                                                 ale->data = adt->action ? adt->action : NULL; 
1380                                         });
1381                         }
1382                 }
1383                 
1384                 /* first track to include will be the last one if we're filtering by channels */
1385                 first = adt->nla_tracks.last;
1386         }
1387         else {
1388                 /* first track to include will the first one (as per normal) */
1389                 first = adt->nla_tracks.first;
1390         }
1391         
1392         /* loop over NLA Tracks - assume that the caller of this has already checked that these should be included */
1393         for (nlt = first; nlt; nlt = next) {
1394                 /* 'next' NLA-Track to use depends on whether we're filtering for drawing or not */
1395                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) 
1396                         next = nlt->prev;
1397                 else
1398                         next = nlt->next;
1399                 
1400                 /* if we're in NLA-tweakmode, don't show this track if it was disabled (due to tweaking) for now 
1401                  *      - active track should still get shown though (even though it has disabled flag set)
1402                  */
1403                 // FIXME: the channels after should still get drawn, just 'differently', and after an active-action channel
1404                 if ((adt->flag & ADT_NLA_EDIT_ON) && (nlt->flag & NLATRACK_DISABLED) && (adt->act_track != nlt))
1405                         continue;
1406                 
1407                 /* only work with this channel and its subchannels if it is editable */
1408                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_NLT(nlt)) {
1409                         /* only include this track if selected in a way consistent with the filtering requirements */
1410                         if (ANIMCHANNEL_SELOK(SEL_NLT(nlt))) {
1411                                 /* only include if this track is active */
1412                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (nlt->flag & NLATRACK_ACTIVE)) {
1413                                         /* name based filtering... */
1414                                         if (((ads) && (ads->filterflag & ADS_FILTER_BY_FCU_NAME)) && (owner_id)) {
1415                                                 bool track_ok = false, strip_ok = false;
1416                                                 
1417                                                 /* check if the name of the track, or the strips it has are ok... */
1418                                                 track_ok = name_matches_dopesheet_filter(ads, nlt->name);
1419                                                 
1420                                                 if (track_ok == false) {
1421                                                         NlaStrip *strip;
1422                                                         for (strip = nlt->strips.first; strip; strip = strip->next) {
1423                                                                 if (name_matches_dopesheet_filter(ads, strip->name)) {
1424                                                                         strip_ok = true;
1425                                                                         break;
1426                                                                 }
1427                                                         }
1428                                                 }
1429                                                 
1430                                                 /* skip if both fail this test... */
1431                                                 if (!track_ok && !strip_ok) {
1432                                                         continue;
1433                                                 }
1434                                         }
1435                                         
1436                                         /* add the track now that it has passed all our tests */
1437                                         ANIMCHANNEL_NEW_CHANNEL(nlt, ANIMTYPE_NLATRACK, owner_id);
1438                                 }
1439                         }
1440                 }
1441         }
1442         
1443         /* return the number of items added to the list */
1444         return items;
1445 }
1446
1447 /* Include the control FCurves per NLA Strip in the channel list
1448  * NOTE: This is includes the expander too...
1449  */
1450 static size_t animfilter_nla_controls(ListBase *anim_data, bDopeSheet *ads, AnimData *adt, int filter_mode, ID *owner_id)
1451 {
1452         ListBase tmp_data = {NULL, NULL};
1453         size_t tmp_items = 0;
1454         size_t items = 0;
1455         
1456         /* add control curves from each NLA strip... */
1457         /* NOTE: ANIMTYPE_FCURVES are created here, to avoid duplicating the code needed */
1458         BEGIN_ANIMFILTER_SUBCHANNELS(((adt->flag & ADT_NLA_SKEYS_COLLAPSED) == 0))
1459         {
1460                 NlaTrack *nlt;
1461                 NlaStrip *strip;
1462                 
1463                 /* for now, we only go one level deep - so controls on grouped FCurves are not handled */
1464                 for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
1465                         for (strip = nlt->strips.first; strip; strip = strip->next) {
1466                                 ListBase strip_curves = {NULL, NULL};
1467                                 size_t strip_items = 0;
1468                                 
1469                                 /* create the raw items */
1470                                 strip_items += animfilter_fcurves(&strip_curves, ads, strip->fcurves.first, NULL, filter_mode, owner_id);
1471                                 
1472                                 /* change their types and add extra data
1473                                  * - There is no point making a separate copy of animfilter_fcurves for this now/yet,
1474                                  *   unless we later get per-element control curves for other stuff too
1475                                  */
1476                                 if (strip_items) {
1477                                         bAnimListElem *ale, *ale_n = NULL;
1478                                         
1479                                         for (ale = strip_curves.first; ale; ale = ale_n) {
1480                                                 ale_n = ale->next;
1481                                                 
1482                                                 /* change the type to being a FCurve for editing NLA strip controls */
1483                                                 BLI_assert(ale->type == ANIMTYPE_FCURVE);
1484                                                 
1485                                                 ale->type = ANIMTYPE_NLACURVE;
1486                                                 ale->owner = strip;
1487                                                 
1488                                                 ale->adt  = NULL; /* XXX: This way, there are no problems with time mapping errors */
1489                                         }
1490                                 }
1491                                 
1492                                 /* add strip curves to the set of channels inside the group being collected */
1493                                 BLI_movelisttolist(&tmp_data, &strip_curves);
1494                                 BLI_assert(BLI_listbase_is_empty(&strip_curves));
1495                                 tmp_items += strip_items;
1496                         }
1497                 }
1498         }
1499         END_ANIMFILTER_SUBCHANNELS;
1500         
1501         /* did we find anything? */
1502         if (tmp_items) {
1503                 /* add the expander as a channel first */
1504                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1505                         /* currently these channels cannot be selected, so they should be skipped */
1506                         if ((filter_mode & (ANIMFILTER_SEL | ANIMFILTER_UNSEL)) == 0) {
1507                                 ANIMCHANNEL_NEW_CHANNEL(adt, ANIMTYPE_NLACONTROLS, owner_id);
1508                         }
1509                 }
1510                 
1511                 /* now add the list of collected channels */
1512                 BLI_movelisttolist(anim_data, &tmp_data);
1513                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
1514                 items += tmp_items;
1515         }
1516         
1517         /* return the numebr of items added to the list */
1518         return items;
1519 }
1520
1521 /* determine what animation data from AnimData block should get displayed */
1522 static size_t animfilter_block_data(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *id, int filter_mode)
1523 {
1524         AnimData *adt = BKE_animdata_from_id(id);
1525         size_t items = 0;
1526         
1527         /* image object datablocks have no anim-data so check for NULL */
1528         if (adt) {
1529                 IdAdtTemplate *iat = (IdAdtTemplate *)id;
1530                 
1531                 /* NOTE: this macro is used instead of inlining the logic here, since this sort of filtering is still needed
1532                  * in a few places in the rest of the code still - notably for the few cases where special mode-based
1533                  * different types of data expanders are required.
1534                  */
1535                 ANIMDATA_FILTER_CASES(iat,
1536                         { /* AnimData */
1537                                 /* specifically filter animdata block */
1538                                 if (ANIMCHANNEL_SELOK(SEL_ANIMDATA(adt)) ) {
1539                                         ANIMCHANNEL_NEW_CHANNEL(adt, ANIMTYPE_ANIMDATA, id);
1540                                 }
1541                         },
1542                         { /* NLA */
1543                                 items += animfilter_nla(ac, anim_data, ads, adt, filter_mode, id);
1544                         },
1545                         { /* Drivers */
1546                                 items += animfilter_fcurves(anim_data, ads, adt->drivers.first, NULL, filter_mode, id);
1547                         },
1548                         { /* NLA Control Keyframes */
1549                                 items += animfilter_nla_controls(anim_data, ads, adt, filter_mode, id);
1550                         },
1551                         { /* Keyframes */
1552                                 items += animfilter_action(ac, anim_data, ads, adt->action, filter_mode, id);
1553                         }
1554                 );
1555         }
1556         
1557         return items;
1558 }
1559
1560
1561
1562 /* Include ShapeKey Data for ShapeKey Editor */
1563 static size_t animdata_filter_shapekey(bAnimContext *ac, ListBase *anim_data, Key *key, int filter_mode)
1564 {
1565         size_t items = 0;
1566         
1567         /* check if channels or only F-Curves */
1568         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1569                 KeyBlock *kb;
1570                 
1571                 /* loop through the channels adding ShapeKeys as appropriate */
1572                 for (kb = key->block.first; kb; kb = kb->next) {
1573                         /* skip the first one, since that's the non-animatable basis */
1574                         if (kb == key->block.first) continue;
1575                         
1576                         /* only work with this channel and its subchannels if it is editable */
1577                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_SHAPEKEY(kb)) {
1578                                 /* only include this track if selected in a way consistent with the filtering requirements */
1579                                 if (ANIMCHANNEL_SELOK(SEL_SHAPEKEY(kb)) ) {
1580                                         // TODO: consider 'active' too?
1581                                         
1582                                         /* owner-id here must be key so that the F-Curve can be resolved... */
1583                                         ANIMCHANNEL_NEW_CHANNEL(kb, ANIMTYPE_SHAPEKEY, key);
1584                                 }
1585                         }
1586                 }
1587         }
1588         else {
1589                 /* just use the action associated with the shapekey */
1590                 // TODO: somehow manage to pass dopesheet info down here too?
1591                 if (key->adt) {
1592                         if (filter_mode & ANIMFILTER_ANIMDATA) {
1593                                 if (ANIMCHANNEL_SELOK(SEL_ANIMDATA(key->adt)) ) {
1594                                         ANIMCHANNEL_NEW_CHANNEL(key->adt, ANIMTYPE_ANIMDATA, key);
1595                                 }
1596                         }
1597                         else if (key->adt->action) {
1598                                 items = animfilter_action(ac, anim_data, NULL, key->adt->action, filter_mode, (ID *)key);
1599                         }
1600                 }
1601         }
1602         
1603         /* return the number of items added to the list */
1604         return items;
1605 }
1606
1607 /* Helper for Grease Pencil - layers within a datablock */
1608 static size_t animdata_filter_gpencil_layers_data(ListBase *anim_data, bDopeSheet *ads, bGPdata *gpd, int filter_mode)
1609 {
1610         bGPDlayer *gpl;
1611         size_t items = 0;
1612         
1613         /* loop over layers as the conditions are acceptable */
1614         for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1615                 /* only if selected */
1616                 if (ANIMCHANNEL_SELOK(SEL_GPL(gpl)) ) {
1617                         /* only if editable */
1618                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
1619                                 /* active... */
1620                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (gpl->flag & GP_LAYER_ACTIVE)) {
1621                                         /* skip layer if the name doesn't match the filter string */
1622                                         if ((ads) && (ads->filterflag & ADS_FILTER_BY_FCU_NAME)) {
1623                                                 if (name_matches_dopesheet_filter(ads, gpl->info) == false)
1624                                                         continue;
1625                                         }
1626                                         
1627                                         
1628                                         /* add to list */
1629                                         ANIMCHANNEL_NEW_CHANNEL(gpl, ANIMTYPE_GPLAYER, gpd);
1630                                 }
1631                         }
1632                 }
1633         }
1634         
1635         return items;
1636 }
1637
1638 /* Helper for Grease Pencil - Grease Pencil datablock - GP Frames */
1639 static size_t animdata_filter_gpencil_data(ListBase *anim_data, bDopeSheet *ads, bGPdata *gpd, int filter_mode)
1640 {
1641         size_t items = 0;
1642         
1643         /* When asked from "AnimData" blocks (i.e. the top-level containers for normal animation),
1644          * for convenience, this will return GP Datablocks instead. This may cause issues down
1645          * the track, but for now, this will do...
1646          */
1647         if (filter_mode & ANIMFILTER_ANIMDATA) {
1648                 /* just add GPD as a channel - this will add everything needed */
1649                 ANIMCHANNEL_NEW_CHANNEL(gpd, ANIMTYPE_GPDATABLOCK, NULL);
1650         }
1651         else {
1652                 ListBase tmp_data = {NULL, NULL};
1653                 size_t tmp_items = 0;
1654                 
1655                 /* add gpencil animation channels */
1656                 BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_GPD(gpd))
1657                 {
1658                         tmp_items += animdata_filter_gpencil_layers_data(&tmp_data, ads, gpd, filter_mode);
1659                 }
1660                 END_ANIMFILTER_SUBCHANNELS;
1661                 
1662                 /* did we find anything? */
1663                 if (tmp_items) {
1664                         /* include data-expand widget first */
1665                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1666                                 /* add gpd as channel too (if for drawing, and it has layers) */
1667                                 ANIMCHANNEL_NEW_CHANNEL(gpd, ANIMTYPE_GPDATABLOCK, NULL);
1668                         }
1669                         
1670                         /* now add the list of collected channels */
1671                         BLI_movelisttolist(anim_data, &tmp_data);
1672                         BLI_assert(BLI_listbase_is_empty(&tmp_data));
1673                         items += tmp_items;
1674                 }
1675         }
1676         
1677         return items;
1678 }
1679
1680 /* Grab all Grease Pencil datablocks in file */
1681 // TODO: should this be amalgamated with the dopesheet filtering code?
1682 static size_t animdata_filter_gpencil(bAnimContext *ac, ListBase *anim_data, void *UNUSED(data), int filter_mode)
1683 {
1684         bDopeSheet *ads = ac->ads;
1685         size_t items = 0;
1686         
1687         if (ads->filterflag & ADS_FILTER_GP_3DONLY) {
1688                 Scene *scene = (Scene *)ads->source;
1689                 ViewLayer *sl = (ViewLayer *)ac->view_layer;
1690                 Base *base;
1691
1692                 /* Active scene's GPencil block first - No parent item needed... */
1693                 if (scene->gpd) {
1694                         items += animdata_filter_gpencil_data(anim_data, ads, scene->gpd, filter_mode);
1695                 }
1696                 
1697                 /* Objects in the scene */
1698                 for (base = sl->object_bases.first; base; base = base->next) {
1699                         /* Only consider this object if it has got some GP data (saving on all the other tests) */
1700                         if (base->object && base->object->gpd) {
1701                                 Object *ob = base->object;
1702                                 
1703                                 /* firstly, check if object can be included, by the following factors:
1704                                  *      - if only visible, must check for layer and also viewport visibility
1705                                  *              --> while tools may demand only visible, user setting takes priority
1706                                  *                      as user option controls whether sets of channels get included while
1707                                  *                      tool-flag takes into account collapsed/open channels too
1708                                  *      - if only selected, must check if object is selected 
1709                                  *      - there must be animation data to edit (this is done recursively as we 
1710                                  *        try to add the channels)
1711                                  */
1712                                 if ((filter_mode & ANIMFILTER_DATA_VISIBLE) && !(ads->filterflag & ADS_FILTER_INCL_HIDDEN)) {
1713                                         /* layer visibility - we check both object and base, since these may not be in sync yet */
1714                                         if ((base->flag & BASE_VISIBLED) == 0) continue;
1715                                         
1716                                         /* outliner restrict-flag */
1717                                         if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
1718                                 }
1719                                 
1720                                 /* check selection and object type filters */
1721                                 if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & BASE_SELECTED) /*|| (base == scene->basact)*/) ) {
1722                                         /* only selected should be shown */
1723                                         continue;
1724                                 }
1725                                 
1726                                 /* check if object belongs to the filtering group if option to filter 
1727                                  * objects by the grouped status is on
1728                                  *      - used to ease the process of doing multiple-character choreographies
1729                                  */
1730                                 if (ads->filterflag & ADS_FILTER_ONLYOBGROUP) {
1731                                         if (BKE_group_object_exists(ads->filter_grp, ob) == 0)
1732                                                 continue;
1733                                 }
1734                                 
1735                                 /* finally, include this object's grease pencil datablock */
1736                                 /* XXX: Should we store these under expanders per item? */
1737                                 items += animdata_filter_gpencil_data(anim_data, ads, ob->gpd, filter_mode);
1738                         }
1739                 }
1740         }
1741         else {
1742                 bGPdata *gpd;
1743                 
1744                 /* Grab all Grease Pencil datablocks directly from main, but only those that seem to be useful somewhere */
1745                 for (gpd = G.main->gpencil.first; gpd; gpd = gpd->id.next) {
1746                         /* only show if gpd is used by something... */
1747                         if (ID_REAL_USERS(gpd) < 1)
1748                                 continue;
1749                         
1750                         /* add GP frames from this datablock */
1751                         items += animdata_filter_gpencil_data(anim_data, ads, gpd, filter_mode);
1752                 }
1753         }
1754         
1755         /* return the number of items added to the list */
1756         return items;
1757 }
1758
1759 /* Helper for Grease Pencil data integrated with main DopeSheet */
1760 static size_t animdata_filter_ds_gpencil(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, bGPdata *gpd, int filter_mode)
1761 {
1762         ListBase tmp_data = {NULL, NULL};
1763         size_t tmp_items = 0;
1764         size_t items = 0;
1765         
1766         /* add relevant animation channels for Grease Pencil */
1767         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_GPD(gpd))
1768         {
1769                 /* add animation channels */
1770                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, &gpd->id, filter_mode);
1771                 
1772                 /* add Grease Pencil layers */
1773                 // TODO: do these need a separate expander?
1774                 // XXX:  what order should these go in?
1775         }
1776         END_ANIMFILTER_SUBCHANNELS;
1777         
1778         /* did we find anything? */
1779         if (tmp_items) {
1780                 /* include data-expand widget first */
1781                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1782                         /* check if filtering by active status */
1783                         // XXX: active check here needs checking
1784                         if (ANIMCHANNEL_ACTIVEOK(gpd)) {
1785                                 ANIMCHANNEL_NEW_CHANNEL(gpd, ANIMTYPE_DSGPENCIL, gpd);
1786                         }
1787                 }
1788                 
1789                 /* now add the list of collected channels */
1790                 BLI_movelisttolist(anim_data, &tmp_data);
1791                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
1792                 items += tmp_items;
1793         }
1794         
1795         /* return the number of items added to the list */
1796         return items;
1797 }
1798
1799 /* Helper for Cache File data integrated with main DopeSheet */
1800 static size_t animdata_filter_ds_cachefile(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, CacheFile *cache_file, int filter_mode)
1801 {
1802         ListBase tmp_data = {NULL, NULL};
1803         size_t tmp_items = 0;
1804         size_t items = 0;
1805
1806         /* add relevant animation channels for Cache File */
1807         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_CACHEFILE_OBJD(cache_file))
1808         {
1809                 /* add animation channels */
1810                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, &cache_file->id, filter_mode);
1811         }
1812         END_ANIMFILTER_SUBCHANNELS;
1813
1814         /* did we find anything? */
1815         if (tmp_items) {
1816                 /* include data-expand widget first */
1817                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1818                         /* check if filtering by active status */
1819                         // XXX: active check here needs checking
1820                         if (ANIMCHANNEL_ACTIVEOK(cache_file)) {
1821                                 ANIMCHANNEL_NEW_CHANNEL(cache_file, ANIMTYPE_DSCACHEFILE, cache_file);
1822                         }
1823                 }
1824
1825                 /* now add the list of collected channels */
1826                 BLI_movelisttolist(anim_data, &tmp_data);
1827                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
1828                 items += tmp_items;
1829         }
1830
1831         /* return the number of items added to the list */
1832         return items;
1833 }
1834
1835 /* Helper for Mask Editing - mask layers */
1836 static size_t animdata_filter_mask_data(ListBase *anim_data, Mask *mask, const int filter_mode)
1837 {
1838         MaskLayer *masklay_act = BKE_mask_layer_active(mask);
1839         MaskLayer *masklay;
1840         size_t items = 0;
1841
1842         /* loop over layers as the conditions are acceptable */
1843         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
1844                 /* only if selected */
1845                 if (ANIMCHANNEL_SELOK(SEL_MASKLAY(masklay)) ) {
1846                         /* only if editable */
1847                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_MASK(masklay)) {
1848                                 /* active... */
1849                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (masklay_act == masklay)) {
1850                                         /* add to list */
1851                                         ANIMCHANNEL_NEW_CHANNEL(masklay, ANIMTYPE_MASKLAYER, mask);
1852                                 }
1853                         }
1854                 }
1855         }
1856
1857         return items;
1858 }
1859
1860 /* Grab all mask data */
1861 static size_t animdata_filter_mask(ListBase *anim_data, void *UNUSED(data), int filter_mode)
1862 {
1863         Mask *mask;
1864         size_t items = 0;
1865         
1866         /* for now, grab mask datablocks directly from main */
1867         // XXX: this is not good...
1868         for (mask = G.main->mask.first; mask; mask = mask->id.next) {
1869                 ListBase tmp_data = {NULL, NULL};
1870                 size_t tmp_items = 0;
1871                 
1872                 /* only show if mask is used by something... */
1873                 if (ID_REAL_USERS(mask) < 1)
1874                         continue;
1875                 
1876                 /* add mask animation channels */
1877                 BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_MASK(mask))
1878                 {
1879                         tmp_items += animdata_filter_mask_data(&tmp_data, mask, filter_mode);
1880                 }
1881                 END_ANIMFILTER_SUBCHANNELS;
1882                 
1883                 /* did we find anything? */
1884                 if (tmp_items) {
1885                         /* include data-expand widget first */
1886                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1887                                 /* add gpd as channel too (if for drawing, and it has layers) */
1888                                 ANIMCHANNEL_NEW_CHANNEL(mask, ANIMTYPE_MASKDATABLOCK, NULL);
1889                         }
1890                         
1891                         /* now add the list of collected channels */
1892                         BLI_movelisttolist(anim_data, &tmp_data);
1893                         BLI_assert(BLI_listbase_is_empty(&tmp_data));
1894                         items += tmp_items;
1895                 }
1896         }
1897         
1898         /* return the number of items added to the list */
1899         return items;
1900 }
1901
1902 /* NOTE: owner_id is scene, material, or texture block, which is the direct owner of the node tree in question */
1903 static size_t animdata_filter_ds_nodetree_group(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *owner_id, bNodeTree *ntree, int filter_mode)
1904 {
1905         ListBase tmp_data = {NULL, NULL};
1906         size_t tmp_items = 0;
1907         size_t items = 0;
1908         
1909         /* add nodetree animation channels */
1910         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_NTREE_DATA(ntree))
1911         {
1912                 /* animation data filtering */
1913                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)ntree, filter_mode);
1914         }
1915         END_ANIMFILTER_SUBCHANNELS;
1916         
1917         /* did we find anything? */
1918         if (tmp_items) {
1919                 /* include data-expand widget first */
1920                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1921                         /* check if filtering by active status */
1922                         if (ANIMCHANNEL_ACTIVEOK(ntree)) {
1923                                 ANIMCHANNEL_NEW_CHANNEL(ntree, ANIMTYPE_DSNTREE, owner_id);
1924                         }
1925                 }
1926                 
1927                 /* now add the list of collected channels */
1928                 BLI_movelisttolist(anim_data, &tmp_data);
1929                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
1930                 items += tmp_items;
1931         }
1932         
1933         /* return the number of items added to the list */
1934         return items;
1935 }
1936
1937 static size_t animdata_filter_ds_nodetree(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *owner_id, bNodeTree *ntree, int filter_mode)
1938 {
1939         bNode *node;
1940         size_t items = 0;
1941         
1942         items += animdata_filter_ds_nodetree_group(ac, anim_data, ads, owner_id, ntree, filter_mode);
1943         
1944         for (node = ntree->nodes.first; node; node = node->next) {
1945                 if (node->type == NODE_GROUP) {
1946                         if (node->id) {
1947                                 if ((ads->filterflag & ADS_FILTER_ONLYSEL) && (node->flag & NODE_SELECT) == 0) {
1948                                         continue;
1949                                 }
1950                                 items += animdata_filter_ds_nodetree_group(ac, anim_data, ads, owner_id, (bNodeTree *) node->id,
1951                                                                            filter_mode | ANIMFILTER_TMP_IGNORE_ONLYSEL);
1952                         }
1953                 }
1954         }
1955
1956         return items;
1957 }
1958
1959 static size_t animdata_filter_ds_linestyle(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
1960 {
1961         ViewLayer *view_layer;
1962         FreestyleLineSet *lineset;
1963         size_t items = 0;
1964         
1965         for (view_layer = sce->view_layers.first; view_layer; view_layer = view_layer->next) {
1966                 for (lineset = view_layer->freestyle_config.linesets.first; lineset; lineset = lineset->next) {
1967                         if (lineset->linestyle) {
1968                                 lineset->linestyle->id.tag |= LIB_TAG_DOIT;
1969                         }
1970                 }
1971         }
1972         
1973         for (view_layer = sce->view_layers.first; view_layer; view_layer = view_layer->next) {
1974                 /* skip render layers without Freestyle enabled */
1975                 if ((view_layer->flag & VIEW_LAYER_FREESTYLE) == 0) {
1976                         continue;
1977                 }
1978
1979                 /* loop over linesets defined in the render layer */
1980                 for (lineset = view_layer->freestyle_config.linesets.first; lineset; lineset = lineset->next) {
1981                         FreestyleLineStyle *linestyle = lineset->linestyle;
1982                         ListBase tmp_data = {NULL, NULL};
1983                         size_t tmp_items = 0;
1984
1985                         if ((linestyle == NULL) ||
1986                             !(linestyle->id.tag & LIB_TAG_DOIT))
1987                         {
1988                                 continue;
1989                         }
1990                         linestyle->id.tag &= ~LIB_TAG_DOIT;
1991                         
1992                         /* add scene-level animation channels */
1993                         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_LS_SCED(linestyle))
1994                         {
1995                                 /* animation data filtering */
1996                                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)linestyle, filter_mode);
1997                         }
1998                         END_ANIMFILTER_SUBCHANNELS;
1999                         
2000                         /* did we find anything? */
2001                         if (tmp_items) {
2002                                 /* include anim-expand widget first */
2003                                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2004                                         /* check if filtering by active status */
2005                                         if (ANIMCHANNEL_ACTIVEOK(linestyle)) {
2006                                                 ANIMCHANNEL_NEW_CHANNEL(linestyle, ANIMTYPE_DSLINESTYLE, sce);
2007                                         }
2008                                 }
2009                                 
2010                                 /* now add the list of collected channels */
2011                                 BLI_movelisttolist(anim_data, &tmp_data);
2012                                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2013                                 items += tmp_items;
2014                         }
2015                 }
2016         }
2017         
2018         /* return the number of items added to the list */
2019         return items;
2020 }
2021
2022 static size_t animdata_filter_ds_texture(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, 
2023                                          Tex *tex, ID *owner_id, int filter_mode)
2024 {
2025         ListBase tmp_data = {NULL, NULL};
2026         size_t tmp_items = 0;
2027         size_t items = 0;
2028         
2029         /* add texture's animation data to temp collection */
2030         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_TEX_DATA(tex)) 
2031         {
2032                 /* texture animdata */
2033                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)tex, filter_mode);
2034                 
2035                 /* nodes */
2036                 if ((tex->nodetree) && !(ads->filterflag & ADS_FILTER_NONTREE)) {
2037                         /* owner_id as id instead of texture, since it'll otherwise be impossible to track the depth */
2038                         // FIXME: perhaps as a result, textures should NOT be included under materials, but under their own section instead
2039                         // so that free-floating textures can also be animated
2040                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)tex, tex->nodetree, filter_mode);
2041                 }
2042         }
2043         END_ANIMFILTER_SUBCHANNELS;
2044         
2045         /* did we find anything? */
2046         if (tmp_items) {
2047                 /* include texture-expand widget? */
2048                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2049                         /* check if filtering by active status */
2050                         if (ANIMCHANNEL_ACTIVEOK(tex)) {
2051                                 ANIMCHANNEL_NEW_CHANNEL(tex, ANIMTYPE_DSTEX, owner_id);
2052                         }
2053                 }
2054                 
2055                 /* now add the list of collected channels */
2056                 BLI_movelisttolist(anim_data, &tmp_data);
2057                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2058                 items += tmp_items;
2059         }
2060         
2061         /* return the number of items added to the list */
2062         return items;
2063 }
2064
2065 /* NOTE: owner_id is either material, lamp, or world block, which is the direct owner of the texture stack in question */
2066 static size_t animdata_filter_ds_textures(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *owner_id, int filter_mode)
2067 {
2068         MTex **mtex = NULL;
2069         size_t items = 0;
2070         int a = 0;
2071         
2072         /* get datatype specific data first */
2073         if (owner_id == NULL)
2074                 return 0;
2075         
2076         switch (GS(owner_id->name)) {
2077                 case ID_MA:
2078                 {
2079                         Material *ma = (Material *)owner_id;
2080                         mtex = (MTex **)(&ma->mtex);
2081                         break;
2082                 }
2083                 case ID_LA:
2084                 {
2085                         Lamp *la = (Lamp *)owner_id;
2086                         mtex = (MTex **)(&la->mtex);
2087                         break;
2088                 }
2089                 case ID_WO:
2090                 {
2091                         World *wo = (World *)owner_id;
2092                         mtex = (MTex **)(&wo->mtex);
2093                         break;
2094                 }
2095                 case ID_PA:
2096                 {
2097                         ParticleSettings *part = (ParticleSettings *)owner_id;
2098                         mtex = (MTex **)(&part->mtex);
2099                         break;
2100                 }
2101                 default: 
2102                 {
2103                         /* invalid/unsupported option */
2104                         if (G.debug & G_DEBUG)
2105                                 printf("ERROR: Unsupported owner_id (i.e. texture stack) for filter textures - %s\n", owner_id->name);
2106                         return 0;
2107                 }
2108         }
2109         
2110         /* firstly check that we actuallly have some textures, by gathering all textures in a temp list */
2111         for (a = 0; a < MAX_MTEX; a++) {
2112                 Tex *tex = (mtex[a]) ? mtex[a]->tex : NULL;
2113                 
2114                 /* for now, if no texture returned, skip (this shouldn't confuse the user I hope) */
2115                 if (tex == NULL) 
2116                         continue;
2117                 
2118                 /* add texture's anim channels */
2119                 items += animdata_filter_ds_texture(ac, anim_data, ads, tex, owner_id, filter_mode);
2120         }
2121         
2122         /* return the number of items added to the list */
2123         return items;
2124 }
2125
2126
2127 static size_t animdata_filter_ds_material(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Material *ma, int filter_mode)
2128 {
2129         ListBase tmp_data = {NULL, NULL};
2130         size_t tmp_items = 0;
2131         size_t items = 0;
2132         
2133         /* add material's animation data to temp collection */
2134         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_MAT_OBJD(ma))
2135         {
2136                 /* material's animation data */
2137                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)ma, filter_mode);
2138                         
2139                 /* textures */
2140                 if (!(ads->filterflag & ADS_FILTER_NOTEX))
2141                         tmp_items += animdata_filter_ds_textures(ac, &tmp_data, ads, (ID *)ma, filter_mode);
2142                         
2143                 /* nodes */
2144                 if ((ma->nodetree) && !(ads->filterflag & ADS_FILTER_NONTREE)) 
2145                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)ma, ma->nodetree, filter_mode);
2146         }
2147         END_ANIMFILTER_SUBCHANNELS;
2148         
2149         /* did we find anything? */
2150         if (tmp_items) {
2151                 /* include material-expand widget first */
2152                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2153                         /* check if filtering by active status */
2154                         if (ANIMCHANNEL_ACTIVEOK(ma)) {
2155                                 ANIMCHANNEL_NEW_CHANNEL(ma, ANIMTYPE_DSMAT, ma);
2156                         }
2157                 }
2158                 
2159                 /* now add the list of collected channels */
2160                 BLI_movelisttolist(anim_data, &tmp_data);
2161                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2162                 items += tmp_items;
2163         }
2164         
2165         return items;
2166 }
2167
2168 static size_t animdata_filter_ds_materials(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
2169 {
2170         bool has_nested = false;
2171         size_t items = 0;
2172         int a = 0;
2173         
2174         /* first pass: take the materials referenced via the Material slots of the object */
2175         for (a = 1; a <= ob->totcol; a++) {
2176                 Material *ma = give_current_material(ob, a);
2177                 
2178                 /* if material is valid, try to add relevant contents from here */
2179                 if (ma) {
2180                         /* add channels */
2181                         items += animdata_filter_ds_material(ac, anim_data, ads, ma, filter_mode);
2182                         
2183                         /* for optimising second pass - check if there's a nested material here to come back for */
2184                         if (has_nested == false) {
2185                                 has_nested = (give_node_material(ma) != NULL);
2186                         }
2187                 }
2188         }
2189         
2190         /* second pass: go through a second time looking for "nested" materials (material.material references)
2191          *
2192          * NOTE: here we ignore the expanded status of the parent, as it could be too confusing as to why these are
2193          *       disappearing/not available, since the relationships between these is not that clear
2194          */
2195         if (has_nested) {
2196                 for (a = 1; a <= ob->totcol; a++) {
2197                         Material *base = give_current_material(ob, a);
2198                         Material *ma   = give_node_material(base);
2199                         
2200                         /* add channels from the nested material if it exists
2201                          *   - skip if the same material is referenced in its node tree
2202                          *     (which is common for BI materials) as that results in
2203                          *     confusing duplicates
2204                          */
2205                         if ((ma) && (ma != base)) {
2206                                 items += animdata_filter_ds_material(ac, anim_data, ads, ma, filter_mode);
2207                         }
2208                 }
2209         }
2210         
2211         /* return the number of items added to the list */
2212         return items;
2213 }
2214
2215
2216 /* ............ */
2217
2218 /* Temporary context for modifier linked-data channel extraction */
2219 typedef struct tAnimFilterModifiersContext {
2220         bAnimContext *ac;       /* anim editor context */
2221         bDopeSheet *ads;    /* dopesheet filtering settings */
2222         
2223         ListBase tmp_data;  /* list of channels created (but not yet added to the main list) */
2224         size_t items;       /* number of channels created */
2225         
2226         int filter_mode;    /* flags for stuff we want to filter */
2227 } tAnimFilterModifiersContext;
2228
2229
2230 /* dependency walker callback for modifier dependencies */
2231 static void animfilter_modifier_idpoin_cb(void *afm_ptr, Object *ob, ID **idpoin, int UNUSED(cb_flag))
2232 {
2233         tAnimFilterModifiersContext *afm = (tAnimFilterModifiersContext *)afm_ptr;
2234         ID *owner_id = &ob->id;
2235         ID *id = *idpoin;
2236         
2237         /* NOTE: the walker only guarantees to give us all the ID-ptr *slots*, 
2238          * not just the ones which are actually used, so be careful!
2239          */
2240         if (id == NULL)
2241                 return;
2242                 
2243         /* check if this is something we're interested in... */
2244         switch (GS(id->name)) {
2245                 case ID_TE: /* Textures */
2246                 {
2247                         Tex *tex = (Tex *)id;
2248                         if (!(afm->ads->filterflag & ADS_FILTER_NOTEX)) {       
2249                                 afm->items += animdata_filter_ds_texture(afm->ac, &afm->tmp_data, afm->ads, tex, owner_id, afm->filter_mode);
2250                         }
2251                         break;
2252                 }
2253                 
2254                 /* TODO: images? */
2255                 default:
2256                         break;
2257         }
2258 }
2259
2260 /* animation linked to data used by modifiers 
2261  * NOTE: strictly speaking, modifier animation is already included under Object level
2262  *       but for some modifiers (e.g. Displace), there can be linked data that has settings
2263  *       which would be nice to animate (i.e. texture parameters) but which are not actually
2264  *       attached to any other objects/materials/etc. in the scene
2265  */
2266 // TODO: do we want an expander for this?
2267 static size_t animdata_filter_ds_modifiers(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
2268 {
2269         tAnimFilterModifiersContext afm = {NULL};
2270         size_t items = 0;
2271         
2272         /* 1) create a temporary "context" containing all the info we have here to pass to the callback 
2273          *    use to walk through the dependencies of the modifiers
2274          *
2275          * ! Assumes that all other unspecified values (i.e. accumulation buffers) are zero'd out properly
2276          */
2277         afm.ac          = ac;
2278         afm.ads         = ads;
2279         afm.filter_mode = filter_mode;
2280         
2281         /* 2) walk over dependencies */
2282         modifiers_foreachIDLink(ob, animfilter_modifier_idpoin_cb, &afm);
2283         
2284         /* 3) extract data from the context, merging it back into the standard list */
2285         if (afm.items) {
2286                 /* now add the list of collected channels */
2287                 BLI_movelisttolist(anim_data, &afm.tmp_data);
2288                 BLI_assert(BLI_listbase_is_empty(&afm.tmp_data));
2289                 items += afm.items;
2290         }
2291         
2292         return items;
2293 }
2294
2295 /* ............ */
2296
2297
2298 static size_t animdata_filter_ds_particles(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
2299 {
2300         ParticleSystem *psys;
2301         size_t items = 0;
2302
2303         for (psys = ob->particlesystem.first; psys; psys = psys->next) {
2304                 ListBase tmp_data = {NULL, NULL};
2305                 size_t tmp_items = 0;
2306                 
2307                 /* if no material returned, skip - so that we don't get weird blank entries... */
2308                 if (ELEM(NULL, psys->part, psys->part->adt))
2309                         continue;
2310                 
2311                 /* add particle-system's animation data to temp collection */
2312                 BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_PART_OBJD(psys->part))
2313                 {
2314                         /* particle system's animation data */
2315                         tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)psys->part, filter_mode);
2316                         
2317                         /* textures */
2318                         if (!(ads->filterflag & ADS_FILTER_NOTEX))
2319                                 tmp_items += animdata_filter_ds_textures(ac, &tmp_data, ads, (ID *)psys->part, filter_mode);
2320                 }
2321                 END_ANIMFILTER_SUBCHANNELS;
2322                 
2323                 /* did we find anything? */
2324                 if (tmp_items) {
2325                         /* include particle-expand widget first */
2326                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2327                                 /* check if filtering by active status */
2328                                 if (ANIMCHANNEL_ACTIVEOK(psys->part)) {
2329                                         ANIMCHANNEL_NEW_CHANNEL(psys->part, ANIMTYPE_DSPART, psys->part);
2330                                 }
2331                         }
2332                         
2333                         /* now add the list of collected channels */
2334                         BLI_movelisttolist(anim_data, &tmp_data);
2335                         BLI_assert(BLI_listbase_is_empty(&tmp_data));
2336                         items += tmp_items;
2337                 }
2338         }
2339         
2340         /* return the number of items added to the list */
2341         return items;
2342 }
2343
2344
2345 static size_t animdata_filter_ds_obdata(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
2346 {
2347         ListBase tmp_data = {NULL, NULL};
2348         size_t tmp_items = 0;
2349         size_t items = 0;
2350         
2351         IdAdtTemplate *iat = ob->data;
2352         short type = 0, expanded = 0;
2353         
2354         /* get settings based on data type */
2355         switch (ob->type) {
2356                 case OB_CAMERA: /* ------- Camera ------------ */
2357                 {
2358                         Camera *ca = (Camera *)ob->data;
2359                         
2360                         if (ads->filterflag & ADS_FILTER_NOCAM)
2361                                 return 0;
2362                         
2363                         type = ANIMTYPE_DSCAM;
2364                         expanded = FILTER_CAM_OBJD(ca);
2365                         break;
2366                 }
2367                 case OB_LAMP: /* ---------- Lamp ----------- */
2368                 {
2369                         Lamp *la = (Lamp *)ob->data;
2370                         
2371                         if (ads->filterflag & ADS_FILTER_NOLAM)
2372                                 return 0;
2373                         
2374                         type = ANIMTYPE_DSLAM;
2375                         expanded = FILTER_LAM_OBJD(la);
2376                         break;
2377                 }
2378                 case OB_CURVE: /* ------- Curve ---------- */
2379                 case OB_SURF: /* ------- Nurbs Surface ---------- */
2380                 case OB_FONT: /* ------- Text Curve ---------- */
2381                 {
2382                         Curve *cu = (Curve *)ob->data;
2383                         
2384                         if (ads->filterflag & ADS_FILTER_NOCUR)
2385                                 return 0;
2386                         
2387                         type = ANIMTYPE_DSCUR;
2388                         expanded = FILTER_CUR_OBJD(cu);
2389                         break;
2390                 }
2391                 case OB_MBALL: /* ------- MetaBall ---------- */
2392                 {
2393                         MetaBall *mb = (MetaBall *)ob->data;
2394                         
2395                         if (ads->filterflag & ADS_FILTER_NOMBA)
2396                                 return 0;
2397                         
2398                         type = ANIMTYPE_DSMBALL;
2399                         expanded = FILTER_MBALL_OBJD(mb);
2400                         break;
2401                 }
2402                 case OB_ARMATURE: /* ------- Armature ---------- */
2403                 {
2404                         bArmature *arm = (bArmature *)ob->data;
2405                         
2406                         if (ads->filterflag & ADS_FILTER_NOARM)
2407                                 return 0;
2408                         
2409                         type = ANIMTYPE_DSARM;
2410                         expanded = FILTER_ARM_OBJD(arm);
2411                         break;
2412                 }
2413                 case OB_MESH: /* ------- Mesh ---------- */
2414                 {
2415                         Mesh *me = (Mesh *)ob->data;
2416                         
2417                         if (ads->filterflag & ADS_FILTER_NOMESH)
2418                                 return 0;
2419                         
2420                         type = ANIMTYPE_DSMESH;
2421                         expanded = FILTER_MESH_OBJD(me);
2422                         break;
2423                 }
2424                 case OB_LATTICE: /* ---- Lattice ---- */
2425                 {
2426                         Lattice *lt = (Lattice *)ob->data;
2427                         
2428                         if (ads->filterflag & ADS_FILTER_NOLAT)
2429                                 return 0;
2430                         
2431                         type = ANIMTYPE_DSLAT;
2432                         expanded = FILTER_LATTICE_OBJD(lt);
2433                         break;
2434                 }
2435                 case OB_SPEAKER: /* ---------- Speaker ----------- */
2436                 {
2437                         Speaker *spk = (Speaker *)ob->data;
2438                         
2439                         type = ANIMTYPE_DSSPK;
2440                         expanded = FILTER_SPK_OBJD(spk);
2441                         break;
2442                 }
2443         }
2444         
2445         /* add object data animation channels */
2446         BEGIN_ANIMFILTER_SUBCHANNELS(expanded)
2447         {
2448                 /* animation data filtering */
2449                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)iat, filter_mode);
2450                 
2451                 /* sub-data filtering... */
2452                 switch (ob->type) {
2453                         case OB_LAMP:  /* lamp - textures + nodetree */
2454                         {
2455                                 Lamp *la = ob->data;
2456                                 bNodeTree *ntree = la->nodetree;
2457                                 
2458                                 /* nodetree */
2459                                 if ((ntree) && !(ads->filterflag & ADS_FILTER_NONTREE))
2460                                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, &la->id, ntree, filter_mode);
2461                                 
2462                                 /* textures */
2463                                 if (!(ads->filterflag & ADS_FILTER_NOTEX))
2464                                         tmp_items += animdata_filter_ds_textures(ac, &tmp_data, ads, &la->id, filter_mode);
2465                                 break;
2466                         }
2467                 }
2468         }
2469         END_ANIMFILTER_SUBCHANNELS;
2470         
2471         /* did we find anything? */
2472         if (tmp_items) {
2473                 /* include data-expand widget first */
2474                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2475                         /* check if filtering by active status */
2476                         if (ANIMCHANNEL_ACTIVEOK(iat)) {
2477                                 ANIMCHANNEL_NEW_CHANNEL(iat, type, iat);
2478                         }
2479                 }
2480                 
2481                 /* now add the list of collected channels */
2482                 BLI_movelisttolist(anim_data, &tmp_data);
2483                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2484                 items += tmp_items;
2485         }
2486         
2487         /* return the number of items added to the list */
2488         return items;
2489 }
2490
2491 /* shapekey-level animation */
2492 static size_t animdata_filter_ds_keyanim(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, Key *key, int filter_mode)
2493 {
2494         ListBase tmp_data = {NULL, NULL};
2495         size_t tmp_items = 0;
2496         size_t items = 0;
2497         
2498         /* add shapekey-level animation channels */
2499         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_SKE_OBJD(key))
2500         {
2501                 /* animation data filtering */
2502                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)key, filter_mode);
2503         }
2504         END_ANIMFILTER_SUBCHANNELS;
2505         
2506         /* did we find anything? */
2507         if (tmp_items) {
2508                 /* include key-expand widget first */
2509                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2510                         if (ANIMCHANNEL_ACTIVEOK(key)) {
2511                                 ANIMCHANNEL_NEW_CHANNEL(key, ANIMTYPE_DSSKEY, ob);
2512                         }
2513                 }
2514                 
2515                 /* now add the list of collected channels */
2516                 BLI_movelisttolist(anim_data, &tmp_data);
2517                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2518                 items += tmp_items;
2519         }
2520         
2521         /* return the number of items added to the list */
2522         return items;
2523 }
2524
2525
2526 /* object-level animation */
2527 static size_t animdata_filter_ds_obanim(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
2528 {
2529         ListBase tmp_data = {NULL, NULL};
2530         size_t tmp_items = 0;
2531         size_t items = 0;
2532         
2533         AnimData *adt = ob->adt;
2534         short type = 0, expanded = 1;
2535         void *cdata = NULL;
2536
2537         /* determine the type of expander channels to use */
2538         /* this is the best way to do this for now... */
2539         ANIMDATA_FILTER_CASES(ob,
2540                 { /* AnimData - no channel, but consider data */ },
2541                 { /* NLA - no channel, but consider data */ },
2542                 { /* Drivers */
2543                         type = ANIMTYPE_FILLDRIVERS;
2544                         cdata = adt;
2545                         expanded = EXPANDED_DRVD(adt);
2546                 },
2547                 { /* NLA Strip Controls - no dedicated channel for now (XXX) */ },
2548                 { /* Keyframes */
2549                         type = ANIMTYPE_FILLACTD;
2550                         cdata = adt->action;
2551                         expanded = EXPANDED_ACTC(adt->action);
2552                 });
2553                 
2554         /* add object-level animation channels */
2555         BEGIN_ANIMFILTER_SUBCHANNELS(expanded)
2556         {
2557                 /* animation data filtering */
2558                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)ob, filter_mode);
2559         }
2560         END_ANIMFILTER_SUBCHANNELS;
2561         
2562         /* did we find anything? */
2563         if (tmp_items) {
2564                 /* include anim-expand widget first */
2565                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2566                         if (type != ANIMTYPE_NONE) {
2567                                 /* NOTE: active-status (and the associated checks) don't apply here... */
2568                                 ANIMCHANNEL_NEW_CHANNEL(cdata, type, ob);
2569                         }
2570                 }
2571                 
2572                 /* now add the list of collected channels */
2573                 BLI_movelisttolist(anim_data, &tmp_data);
2574                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2575                 items += tmp_items;
2576         }
2577         
2578         /* return the number of items added to the list */
2579         return items;
2580 }
2581
2582 /* get animation channels from object2 */
2583 static size_t animdata_filter_dopesheet_ob(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
2584 {
2585         ListBase tmp_data = {NULL, NULL};
2586         Object *ob = base->object;
2587         size_t tmp_items = 0;
2588         size_t items = 0;
2589         
2590         /* filter data contained under object first */
2591         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_OBJC(ob))
2592         {
2593                 Key *key = BKE_key_from_object(ob);
2594                 
2595                 /* object-level animation */
2596                 if ((ob->adt) && !(ads->filterflag & ADS_FILTER_NOOBJ)) {
2597                         tmp_items += animdata_filter_ds_obanim(ac, &tmp_data, ads, ob, filter_mode);
2598                 }
2599                 
2600                 /* shape-key */
2601                 if ((key && key->adt) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
2602                         tmp_items += animdata_filter_ds_keyanim(ac, &tmp_data, ads, ob, key, filter_mode);
2603                 }
2604                 
2605                 /* modifiers */
2606                 if ((ob->modifiers.first) && !(ads->filterflag & ADS_FILTER_NOMODIFIERS)) {
2607                         tmp_items += animdata_filter_ds_modifiers(ac, &tmp_data, ads, ob, filter_mode);
2608                 }
2609                 
2610                 /* materials */
2611                 if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT)) {
2612                         tmp_items += animdata_filter_ds_materials(ac, &tmp_data, ads, ob, filter_mode);
2613                 }
2614                 
2615                 /* object data */
2616                 if (ob->data) {
2617                         tmp_items += animdata_filter_ds_obdata(ac, &tmp_data, ads, ob, filter_mode);
2618                 }
2619                 
2620                 /* particles */
2621                 if ((ob->particlesystem.first) && !(ads->filterflag & ADS_FILTER_NOPART)) {
2622                         tmp_items += animdata_filter_ds_particles(ac, &tmp_data, ads, ob, filter_mode);
2623                 }
2624                 
2625                 /* grease pencil */
2626                 if ((ob->gpd) && !(ads->filterflag & ADS_FILTER_NOGPENCIL)) {
2627                         tmp_items += animdata_filter_ds_gpencil(ac, &tmp_data, ads, ob->gpd, filter_mode);
2628                 }
2629         }
2630         END_ANIMFILTER_SUBCHANNELS;
2631         
2632         
2633         /* if we collected some channels, add these to the new list... */
2634         if (tmp_items) {
2635                 /* firstly add object expander if required */
2636                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2637                         /* check if filtering by selection */
2638                         // XXX: double-check on this - most of the time, a lot of tools need to filter out these channels!
2639                         if (ANIMCHANNEL_SELOK((base->flag & BASE_SELECTED))) {
2640                                 /* check if filtering by active status */
2641                                 if (ANIMCHANNEL_ACTIVEOK(ob)) {
2642                                         ANIMCHANNEL_NEW_CHANNEL(base, ANIMTYPE_OBJECT, ob);
2643                                 }
2644                         }
2645                 }
2646                 
2647                 /* now add the list of collected channels */
2648                 BLI_movelisttolist(anim_data, &tmp_data);
2649                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2650                 items += tmp_items;
2651         }
2652         
2653         /* return the number of items added */
2654         return items;
2655 }
2656
2657 static size_t animdata_filter_ds_world(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, World *wo, int filter_mode)
2658 {
2659         ListBase tmp_data = {NULL, NULL};
2660         size_t tmp_items = 0;
2661         size_t items = 0;
2662         
2663         /* add world animation channels */
2664         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_WOR_SCED(wo))
2665         {
2666                 /* animation data filtering */
2667                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)wo, filter_mode);
2668                 
2669                 /* textures for world */
2670                 if (!(ads->filterflag & ADS_FILTER_NOTEX))
2671                         tmp_items += animdata_filter_ds_textures(ac, &tmp_data, ads, (ID *)wo, filter_mode);
2672                         
2673                 /* nodes */
2674                 if ((wo->nodetree) && !(ads->filterflag & ADS_FILTER_NONTREE)) 
2675                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)wo, wo->nodetree, filter_mode);
2676         }
2677         END_ANIMFILTER_SUBCHANNELS;
2678         
2679         /* did we find anything? */
2680         if (tmp_items) {
2681                 /* include data-expand widget first */
2682                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2683                         /* check if filtering by active status */
2684                         if (ANIMCHANNEL_ACTIVEOK(wo)) {
2685                                 ANIMCHANNEL_NEW_CHANNEL(wo, ANIMTYPE_DSWOR, sce);
2686                         }
2687                 }
2688                 
2689                 /* now add the list of collected channels */
2690                 BLI_movelisttolist(anim_data, &tmp_data);
2691                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2692                 items += tmp_items;
2693         }
2694         
2695         /* return the number of items added to the list */
2696         return items;
2697 }
2698
2699 static size_t animdata_filter_ds_scene(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
2700 {
2701         ListBase tmp_data = {NULL, NULL};
2702         size_t tmp_items = 0;
2703         size_t items = 0;
2704         
2705         AnimData *adt = sce->adt;
2706         short type = 0, expanded = 1;
2707         void *cdata = NULL;
2708         
2709         /* determine the type of expander channels to use */
2710         // this is the best way to do this for now...
2711         ANIMDATA_FILTER_CASES(sce,
2712                 { /* AnimData - no channel, but consider data */},
2713                 { /* NLA - no channel, but consider data */},
2714                 { /* Drivers */
2715                         type = ANIMTYPE_FILLDRIVERS;
2716                         cdata = adt;
2717                         expanded = EXPANDED_DRVD(adt);
2718                 },
2719                 { /* NLA Strip Controls - no dedicated channel for now (XXX) */ },
2720                 { /* Keyframes */
2721                         type = ANIMTYPE_FILLACTD;
2722                         cdata = adt->action;
2723                         expanded = EXPANDED_ACTC(adt->action);
2724                 });
2725                 
2726         /* add scene-level animation channels */
2727         BEGIN_ANIMFILTER_SUBCHANNELS(expanded)
2728         {
2729                 /* animation data filtering */
2730                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)sce, filter_mode);
2731         }
2732         END_ANIMFILTER_SUBCHANNELS;
2733         
2734         /* did we find anything? */
2735         if (tmp_items) {
2736                 /* include anim-expand widget first */
2737                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2738                         if (type != ANIMTYPE_NONE) {
2739                                 /* NOTE: active-status (and the associated checks) don't apply here... */
2740                                 ANIMCHANNEL_NEW_CHANNEL(cdata, type, sce);
2741                         }
2742                 }
2743                 
2744                 /* now add the list of collected channels */
2745                 BLI_movelisttolist(anim_data, &tmp_data);
2746                 BLI_assert(BLI_listbase_is_empty(&tmp_data));
2747                 items += tmp_items;
2748         }
2749         
2750         /* return the number of items added to the list */
2751         return items;
2752 }
2753
2754 static size_t animdata_filter_dopesheet_scene(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
2755 {
2756         ListBase tmp_data = {NULL, NULL};
2757         size_t tmp_items = 0;
2758         size_t items = 0;
2759         
2760         /* filter data contained under object first */
2761         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_SCEC(sce))
2762         {
2763                 bNodeTree *ntree = sce->nodetree;
2764                 bGPdata *gpd = sce->gpd;
2765                 World *wo = sce->world;
2766                 
2767                 /* Action, Drivers, or NLA for Scene */
2768                 if ((ads->filterflag & ADS_FILTER_NOSCE) == 0) {
2769                         tmp_items += animdata_filter_ds_scene(ac, &tmp_data, ads, sce, filter_mode);
2770                 }
2771                 
2772                 /* world */
2773                 if ((wo) && !(ads->filterflag & ADS_FILTER_NOWOR)) {
2774                         tmp_items += animdata_filter_ds_world(ac, &tmp_data, ads, sce, wo, filter_mode);
2775                 }
2776                 
2777                 /* nodetree */
2778                 if ((ntree) && !(ads->filterflag & ADS_FILTER_NONTREE)) {
2779                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)sce, ntree, filter_mode);
2780                 }
2781                 
2782                 /* line styles */
2783                 if ((ads->filterflag & ADS_FILTER_NOLINESTYLE) == 0) {
2784                         tmp_items += animdata_filter_ds_linestyle(ac, &tmp_data, ads, sce, filter_mode);
2785                 }
2786                 
2787                 /* grease pencil */
2788                 if ((gpd) && !(ads->filterflag & ADS_FILTER_NOGPENCIL)) {
2789                         tmp_items += animdata_filter_ds_gpencil(ac, &tmp_data, ads, gpd, filter_mode);
2790                 }
2791                 
2792                 /* TODO: one day, when sequencer becomes its own datatype, perhaps it should be included here */
2793         }
2794         END_ANIMFILTER_SUBCHANNELS;
2795
2796         /* if we collected some channels, add these to the new list... */
2797         if (tmp_items) {
2798                 /* firstly add object expander if required */
2799                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2800                         /* check if filtering by selection */
2801                         if (ANIMCHANNEL_SELOK((sce->flag & SCE_DS_SELECTED))) {
2802                                 /* NOTE: active-status doesn't matter for this! */
2803                                 ANIMCHANNEL_NEW_CHANNEL(sce, ANIMTYPE_SCENE, sce);
2804                         }
2805                 }
2806