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