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