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