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