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