code cleanup: move file string defines into BLI_path_utils.h, BKE_utildefines is...
[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 (ob_get_key) */
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 = ob_get_key(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: /* Grease Pencil */ /* XXX review how this mode is handled... */
178                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
179 {
180                         /* TODO, other methods to get the mask */
181                         // Sequence *seq = BKE_sequencer_active_get(ac->scene);
182                         //MovieClip *clip = ac->scene->clip;
183 //                      struct Mask *mask = seq ? seq->mask : NULL;
184
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 = key_get_curValue_rnaPath(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         // TODO: need a way of tagging other channels that may also be affected...
1142         if ((filter_mode & ANIMFILTER_FOREDIT) && (act->id.lib))
1143                 return 0;
1144                 
1145         /* do groups */
1146         // TODO: do nested groups?
1147         for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1148                 /* store reference to last channel of group */
1149                 if (agrp->channels.last) 
1150                         lastchan = agrp->channels.last;
1151                         
1152                 /* action group's channels */
1153                 items += animfilter_act_group(ac, anim_data, ads, act, agrp, filter_mode, owner_id);
1154         }
1155         
1156         /* un-grouped F-Curves (only if we're not only considering those channels in the active group) */
1157         if (!(filter_mode & ANIMFILTER_ACTGROUPED)) {
1158                 FCurve *firstfcu = (lastchan) ? (lastchan->next) : (act->curves.first);
1159                 items += animfilter_fcurves(anim_data, ads, firstfcu, NULL, filter_mode, owner_id);
1160         }
1161         
1162         /* return the number of items added to the list */
1163         return items;
1164 }
1165
1166 /* Include NLA-Data for NLA-Editor:
1167  *      - when ANIMFILTER_LIST_CHANNELS is used, that means we should be filtering the list for display
1168  *        Although the evaluation order is from the first track to the last and then apply the Action on top,
1169  *        we present this in the UI as the Active Action followed by the last track to the first so that we 
1170  *        get the evaluation order presented as per a stack.
1171  *      - for normal filtering (i.e. for editing), we only need the NLA-tracks but they can be in 'normal' evaluation
1172  *        order, i.e. first to last. Otherwise, some tools may get screwed up.
1173  */
1174 static size_t animfilter_nla(bAnimContext *UNUSED(ac), ListBase *anim_data, bDopeSheet *ads, AnimData *adt, int filter_mode, ID *owner_id)
1175 {
1176         NlaTrack *nlt;
1177         NlaTrack *first = NULL, *next = NULL;
1178         size_t items = 0;
1179         
1180         /* if showing channels, include active action */
1181         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1182                 /* if NLA action-line filtering is off, don't show unless there are keyframes, 
1183                  * in order to keep things more compact for doing transforms
1184                  */
1185                 if (!(ads->filterflag & ADS_FILTER_NLA_NOACT) || (adt->action)) {
1186                         /* there isn't really anything editable here, so skip if need editable */
1187                         if ((filter_mode & ANIMFILTER_FOREDIT) == 0) { 
1188                                 /* just add the action track now (this MUST appear for drawing)
1189                                  *      - as AnimData may not have an action, we pass a dummy pointer just to get the list elem created, then
1190                                  *        overwrite this with the real value - REVIEW THIS...
1191                                  */
1192                                 ANIMCHANNEL_NEW_CHANNEL_FULL((void *)(&adt->action), ANIMTYPE_NLAACTION, owner_id, 
1193                                         {
1194                                                 ale->data = adt->action ? adt->action : NULL; 
1195                                         });
1196                         }
1197                 }
1198                 
1199                 /* first track to include will be the last one if we're filtering by channels */
1200                 first = adt->nla_tracks.last;
1201         }
1202         else {
1203                 /* first track to include will the the first one (as per normal) */
1204                 first = adt->nla_tracks.first;
1205         }
1206         
1207         /* loop over NLA Tracks - assume that the caller of this has already checked that these should be included */
1208         for (nlt = first; nlt; nlt = next) {
1209                 /* 'next' NLA-Track to use depends on whether we're filtering for drawing or not */
1210                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) 
1211                         next = nlt->prev;
1212                 else
1213                         next = nlt->next;
1214                 
1215                 /* if we're in NLA-tweakmode, don't show this track if it was disabled (due to tweaking) for now 
1216                  *      - active track should still get shown though (even though it has disabled flag set)
1217                  */
1218                 // FIXME: the channels after should still get drawn, just 'differently', and after an active-action channel
1219                 if ((adt->flag & ADT_NLA_EDIT_ON) && (nlt->flag & NLATRACK_DISABLED) && !(nlt->flag & NLATRACK_ACTIVE))
1220                         continue;
1221                 
1222                 /* only work with this channel and its subchannels if it is editable */
1223                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_NLT(nlt)) {
1224                         /* only include this track if selected in a way consistent with the filtering requirements */
1225                         if (ANIMCHANNEL_SELOK(SEL_NLT(nlt))) {
1226                                 /* only include if this track is active */
1227                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (nlt->flag & NLATRACK_ACTIVE)) {
1228                                         ANIMCHANNEL_NEW_CHANNEL(nlt, ANIMTYPE_NLATRACK, owner_id);
1229                                 }
1230                         }
1231                 }
1232         }
1233         
1234         /* return the number of items added to the list */
1235         return items;
1236 }
1237
1238 /* determine what animation data from AnimData block should get displayed */
1239 static size_t animfilter_block_data(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *id, int filter_mode)
1240 {
1241         AnimData *adt = BKE_animdata_from_id(id);
1242         size_t items = 0;
1243
1244         /* image object datablocks have no anim-data so check for NULL */
1245         if (adt) {
1246                 IdAdtTemplate *iat = (IdAdtTemplate *)id;
1247
1248                 /* NOTE: this macro is used instead of inlining the logic here, since this sort of filtering is still needed
1249                  * in a few places in he rest of the code still - notably for the few cases where special mode-based
1250                  * different types of data expanders are required.
1251                  */
1252                 ANIMDATA_FILTER_CASES(iat,
1253                         { /* AnimData */
1254                                 /* specifically filter animdata block */
1255                                 ANIMCHANNEL_NEW_CHANNEL(adt, ANIMTYPE_ANIMDATA, id);
1256                         },
1257                         { /* NLA */
1258                                 items += animfilter_nla(ac, anim_data, ads, adt, filter_mode, id);
1259                         },
1260                         { /* Drivers */
1261                                 items += animfilter_fcurves(anim_data, ads, adt->drivers.first, NULL, filter_mode, id);
1262                         },
1263                         { /* Keyframes */
1264                                 items += animfilter_action(ac, anim_data, ads, adt->action, filter_mode, id);
1265                         }
1266                 );
1267         }
1268
1269         return items;
1270 }
1271
1272
1273
1274 /* Include ShapeKey Data for ShapeKey Editor */
1275 static size_t animdata_filter_shapekey(bAnimContext *ac, ListBase *anim_data, Key *key, int filter_mode)
1276 {
1277         size_t items = 0;
1278         
1279         /* check if channels or only F-Curves */
1280         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1281                 KeyBlock *kb;
1282                 
1283                 /* loop through the channels adding ShapeKeys as appropriate */
1284                 for (kb = key->block.first; kb; kb = kb->next) {
1285                         /* skip the first one, since that's the non-animatable basis */
1286                         // XXX maybe in future this may become handy?
1287                         if (kb == key->block.first) continue;
1288                         
1289                         /* only work with this channel and its subchannels if it is editable */
1290                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_SHAPEKEY(kb)) {
1291                                 /* only include this track if selected in a way consistent with the filtering requirements */
1292                                 if (ANIMCHANNEL_SELOK(SEL_SHAPEKEY(kb)) ) {
1293                                         // TODO: consider 'active' too?
1294                                         
1295                                         /* owner-id here must be key so that the F-Curve can be resolved... */
1296                                         ANIMCHANNEL_NEW_CHANNEL(kb, ANIMTYPE_SHAPEKEY, key);
1297                                 }
1298                         }
1299                 }
1300         }
1301         else {
1302                 /* just use the action associated with the shapekey */
1303                 // TODO: somehow manage to pass dopesheet info down here too?
1304                 if (key->adt) {
1305                         if (filter_mode & ANIMFILTER_ANIMDATA) {
1306                                 ANIMCHANNEL_NEW_CHANNEL(key->adt, ANIMTYPE_ANIMDATA, key);
1307                         }
1308                         else if (key->adt->action) {
1309                                 items = animfilter_action(ac, anim_data, NULL, key->adt->action, filter_mode, (ID *)key);
1310                         }
1311                 }
1312         }
1313         
1314         /* return the number of items added to the list */
1315         return items;
1316 }
1317
1318 /* Helper for Grease Pencil - layers within a datablock */
1319 static size_t animdata_filter_gpencil_data(ListBase *anim_data, bGPdata *gpd, int filter_mode)
1320 {
1321         bGPDlayer *gpl;
1322         size_t items = 0;
1323         
1324         /* loop over layers as the conditions are acceptable */
1325         for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1326                 /* only if selected */
1327                 if (ANIMCHANNEL_SELOK(SEL_GPL(gpl)) ) {
1328                         /* only if editable */
1329                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
1330                                 /* active... */
1331                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (gpl->flag & GP_LAYER_ACTIVE)) {
1332                                         /* add to list */
1333                                         ANIMCHANNEL_NEW_CHANNEL(gpl, ANIMTYPE_GPLAYER, gpd);
1334                                 }
1335                         }
1336                 }
1337         }
1338         
1339         return items;
1340 }
1341
1342 /* Grab all Grease Pencil datablocks in file */
1343 // TODO: should this be amalgamated with the dopesheet filtering code?
1344 static size_t animdata_filter_gpencil(ListBase *anim_data, void *UNUSED(data), int filter_mode)
1345 {
1346         bGPdata *gpd;
1347         size_t items = 0;
1348         
1349         /* for now, grab grease pencil datablocks directly from main */
1350         // XXX: this is not good...
1351         for (gpd = G.main->gpencil.first; gpd; gpd = gpd->id.next) {
1352                 ListBase tmp_data = {NULL, NULL};
1353                 size_t tmp_items = 0;
1354                 
1355                 /* only show if gpd is used by something... */
1356                 if (ID_REAL_USERS(gpd) < 1)
1357                         continue;
1358                         
1359                 /* add gpencil animation channels */
1360                 BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_GPD(gpd))
1361                 {
1362                         tmp_items += animdata_filter_gpencil_data(&tmp_data, gpd, filter_mode);
1363                 }
1364                 END_ANIMFILTER_SUBCHANNELS;
1365                 
1366                 /* did we find anything? */
1367                 if (tmp_items) {
1368                         /* include data-expand widget first */
1369                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1370                                 /* add gpd as channel too (if for drawing, and it has layers) */
1371                                 ANIMCHANNEL_NEW_CHANNEL(gpd, ANIMTYPE_GPDATABLOCK, NULL);
1372                         }
1373                         
1374                         /* now add the list of collected channels */
1375                         BLI_movelisttolist(anim_data, &tmp_data);
1376                         BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1377                         items += tmp_items;
1378                 }
1379         }
1380         
1381         /* return the number of items added to the list */
1382         return items;
1383 }
1384
1385 /* Helper for Mask Editing - mask layers */
1386 static size_t animdata_filter_mask_data(ListBase *anim_data, Mask *mask, const int filter_mode)
1387 {
1388         MaskLayer *masklay_act = BKE_mask_layer_active(mask);
1389         MaskLayer *masklay;
1390         size_t items = 0;
1391
1392         /* loop over layers as the conditions are acceptable */
1393         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
1394                 /* only if selected */
1395                 if (ANIMCHANNEL_SELOK(SEL_MASKLAY(masklay)) ) {
1396                         /* only if editable */
1397 //                      if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
1398                                 /* active... */
1399                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (masklay_act == masklay)) {
1400                                         /* add to list */
1401                                         ANIMCHANNEL_NEW_CHANNEL(masklay, ANIMTYPE_MASKLAYER, mask);
1402                                 }
1403 //                      }
1404                 }
1405         }
1406
1407         return items;
1408 }
1409
1410 /* Grab all mask data */
1411 static size_t animdata_filter_mask(ListBase *anim_data, void *UNUSED(data), int filter_mode)
1412 {
1413         Mask *mask;
1414         size_t items = 0;
1415         
1416         /* for now, grab mask datablocks directly from main */
1417         // XXX: this is not good...
1418         for (mask = G.main->mask.first; mask; mask = mask->id.next) {
1419                 ListBase tmp_data = {NULL, NULL};
1420                 size_t tmp_items = 0;
1421                 
1422                 /* only show if mask is used by something... */
1423                 if (ID_REAL_USERS(mask) < 1)
1424                         continue;
1425                 
1426                 /* add mask animation channels */
1427                 BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_MASK(mask))
1428                 {
1429                         tmp_items += animdata_filter_mask_data(&tmp_data, mask, filter_mode);
1430                 }
1431                 END_ANIMFILTER_SUBCHANNELS;
1432                 
1433                 /* did we find anything? */
1434                 if (tmp_items) {
1435                         /* include data-expand widget first */
1436                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1437                                 /* add gpd as channel too (if for drawing, and it has layers) */
1438                                 ANIMCHANNEL_NEW_CHANNEL(mask, ANIMTYPE_MASKDATABLOCK, NULL);
1439                         }
1440                         
1441                         /* now add the list of collected channels */
1442                         BLI_movelisttolist(anim_data, &tmp_data);
1443                         BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1444                         items += tmp_items;
1445                 }
1446         }
1447         
1448         /* return the number of items added to the list */
1449         return items;
1450 }
1451
1452 /* NOTE: owner_id is scene, material, or texture block, which is the direct owner of the node tree in question */
1453 // TODO: how to handle group nodes is still unclear...
1454 static size_t animdata_filter_ds_nodetree(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *owner_id, bNodeTree *ntree, int filter_mode)
1455 {
1456         ListBase tmp_data = {NULL, NULL};
1457         size_t tmp_items = 0;
1458         size_t items = 0;
1459         
1460         /* add nodetree animation channels */
1461         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_NTREE_DATA(ntree))
1462         {
1463                 /* animation data filtering */
1464                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)ntree, filter_mode);
1465         }
1466         END_ANIMFILTER_SUBCHANNELS;
1467         
1468         /* did we find anything? */
1469         if (tmp_items) {
1470                 /* include data-expand widget first */
1471                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1472                         /* check if filtering by active status */
1473                         if (ANIMCHANNEL_ACTIVEOK(ntree)) {
1474                                 ANIMCHANNEL_NEW_CHANNEL(ntree, ANIMTYPE_DSNTREE, owner_id);
1475                         }
1476                 }
1477                 
1478                 /* now add the list of collected channels */
1479                 BLI_movelisttolist(anim_data, &tmp_data);
1480                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1481                 items += tmp_items;
1482         }
1483         
1484         /* return the number of items added to the list */
1485         return items;
1486 }
1487
1488 /* NOTE: owner_id is either material, lamp, or world block, which is the direct owner of the texture stack in question */
1489 static size_t animdata_filter_ds_textures(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *owner_id, int filter_mode)
1490 {
1491         MTex **mtex = NULL;
1492         size_t items = 0;
1493         int a = 0;
1494         
1495         /* get datatype specific data first */
1496         if (owner_id == NULL)
1497                 return 0;
1498         
1499         switch (GS(owner_id->name)) {
1500                 case ID_MA:
1501                 {
1502                         Material *ma = (Material *)owner_id;
1503                         mtex = (MTex **)(&ma->mtex);
1504                 }
1505                 break;
1506                 case ID_LA:
1507                 {
1508                         Lamp *la = (Lamp *)owner_id;
1509                         mtex = (MTex **)(&la->mtex);
1510                 }
1511                 break;
1512                 case ID_WO:
1513                 {
1514                         World *wo = (World *)owner_id;
1515                         mtex = (MTex **)(&wo->mtex);
1516                 }
1517                 break;
1518                 default: 
1519                 {
1520                         /* invalid/unsupported option */
1521                         if (G.debug & G_DEBUG)
1522                                 printf("ERROR: unsupported owner_id (i.e. texture stack) for filter textures - %s\n", owner_id->name);
1523                         return 0;
1524                 }
1525         }
1526         
1527         /* firstly check that we actuallly have some textures, by gathering all textures in a temp list */
1528         for (a = 0; a < MAX_MTEX; a++) {
1529                 Tex *tex = (mtex[a]) ? mtex[a]->tex : NULL;
1530                 ListBase tmp_data = {NULL, NULL};
1531                 size_t tmp_items = 0;
1532                 
1533                 /* for now, if no texture returned, skip (this shouldn't confuse the user I hope) */
1534                 if (tex == NULL) 
1535                         continue;
1536                 
1537                 /* add texture's animation data to temp collection */
1538                 BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_TEX_DATA(tex)) 
1539                 {
1540                         /* texture animdata */
1541                         tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)tex, filter_mode);
1542                         
1543                         /* nodes */
1544                         if ((tex->nodetree) && !(ads->filterflag & ADS_FILTER_NONTREE)) {
1545                                 /* owner_id as id instead of texture, since it'll otherwise be impossible to track the depth */
1546                                 // FIXME: perhaps as a result, textures should NOT be included under materials, but under their own section instead
1547                                 // so that free-floating textures can also be animated
1548                                 tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)tex, tex->nodetree, filter_mode);
1549                         }
1550                 }
1551                 END_ANIMFILTER_SUBCHANNELS;
1552                 
1553                 /* did we find anything? */
1554                 if (tmp_items) {
1555                         /* include texture-expand widget? */
1556                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1557                                 /* check if filtering by active status */
1558                                 if (ANIMCHANNEL_ACTIVEOK(tex)) {
1559                                         ANIMCHANNEL_NEW_CHANNEL(tex, ANIMTYPE_DSTEX, owner_id);
1560                                 }
1561                         }
1562                         
1563                         /* now add the list of collected channels */
1564                         BLI_movelisttolist(anim_data, &tmp_data);
1565                         BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1566                         items += tmp_items;
1567                 }
1568         }
1569         
1570         /* return the number of items added to the list */
1571         return items;
1572 }
1573
1574
1575 static size_t animdata_filter_ds_material(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Material *ma, int filter_mode)
1576 {
1577         ListBase tmp_data = {NULL, NULL};
1578         size_t tmp_items = 0;
1579         size_t items = 0;
1580         
1581         /* add material's animation data to temp collection */
1582         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_MAT_OBJD(ma))
1583         {
1584                 /* material's animation data */
1585                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)ma, filter_mode);
1586                         
1587                 /* textures */
1588                 if (!(ads->filterflag & ADS_FILTER_NOTEX))
1589                         tmp_items += animdata_filter_ds_textures(ac, &tmp_data, ads, (ID *)ma, filter_mode);
1590                         
1591                 /* nodes */
1592                 if ((ma->nodetree) && !(ads->filterflag & ADS_FILTER_NONTREE)) 
1593                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)ma, ma->nodetree, filter_mode);
1594         }
1595         END_ANIMFILTER_SUBCHANNELS;
1596         
1597         /* did we find anything? */
1598         if (tmp_items) {
1599                 /* include material-expand widget first */
1600                 // hmm... do we need to store the index of this material in the array anywhere?
1601                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1602                         /* check if filtering by active status */
1603                         if (ANIMCHANNEL_ACTIVEOK(ma)) {
1604                                 ANIMCHANNEL_NEW_CHANNEL(ma, ANIMTYPE_DSMAT, ma);
1605                         }
1606                 }
1607                 
1608                 /* now add the list of collected channels */
1609                 BLI_movelisttolist(anim_data, &tmp_data);
1610                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1611                 items += tmp_items;
1612         }
1613         
1614         return items;
1615 }
1616
1617 static size_t animdata_filter_ds_materials(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
1618 {
1619         short has_nested = 0;
1620         size_t items = 0;
1621         int a = 0;
1622         
1623         /* first pass: take the materials referenced via the Material slots of the object */
1624         for (a = 1; a <= ob->totcol; a++) {
1625                 Material *ma = give_current_material(ob, a);
1626                 
1627                 /* if material is valid, try to add relevant contents from here */
1628                 if (ma) {
1629                         /* add channels */
1630                         items += animdata_filter_ds_material(ac, anim_data, ads, ma, filter_mode);
1631                         
1632                         /* for optimising second pass - check if there's a nested material here to come back for */
1633                         if (has_nested == 0)
1634                                 has_nested = give_node_material(ma) != NULL;
1635                 }
1636         }
1637         
1638         /* second pass: go through a second time looking for "nested" materials (material.material references)
1639          *
1640          * NOTE: here we ignore the expanded status of the parent, as it could be too confusing as to why these are
1641          *       disappearing/not available, since the relationships between these is not that clear
1642          */
1643         if (has_nested) {
1644                 for (a = 1; a <= ob->totcol; a++) {
1645                         Material *base = give_current_material(ob, a);
1646                         Material *ma   = give_node_material(base);
1647                         
1648                         /* add channels from the nested material if it exists */
1649                         if (ma)
1650                                 items += animdata_filter_ds_material(ac, anim_data, ads, ma, filter_mode);
1651                 }
1652         }
1653         
1654         /* return the number of items added to the list */
1655         return items;
1656 }
1657
1658 static size_t animdata_filter_ds_particles(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
1659 {
1660         ParticleSystem *psys;
1661         size_t items = 0;
1662
1663         for (psys = ob->particlesystem.first; psys; psys = psys->next) {
1664                 ListBase tmp_data = {NULL, NULL};
1665                 size_t tmp_items = 0;
1666                 
1667                 /* if no material returned, skip - so that we don't get weird blank entries... */
1668                 if (ELEM(NULL, psys->part, psys->part->adt))
1669                         continue;
1670                 
1671                 /* add particle-system's animation data to temp collection */
1672                 BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_PART_OBJD(psys->part))
1673                 {
1674                         /* material's animation data */
1675                         tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)psys->part, filter_mode);
1676                 }
1677                 END_ANIMFILTER_SUBCHANNELS;
1678                 
1679                 /* did we find anything? */
1680                 if (tmp_items) {
1681                         /* include particle-expand widget first */
1682                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1683                                 /* check if filtering by active status */
1684                                 if (ANIMCHANNEL_ACTIVEOK(psys->part)) {
1685                                         ANIMCHANNEL_NEW_CHANNEL(psys->part, ANIMTYPE_DSPART, psys->part);
1686                                 }
1687                         }
1688                         
1689                         /* now add the list of collected channels */
1690                         BLI_movelisttolist(anim_data, &tmp_data);
1691                         BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1692                         items += tmp_items;
1693                 }
1694         }
1695         
1696         /* return the number of items added to the list */
1697         return items;
1698 }
1699
1700 static size_t animdata_filter_ds_obdata(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
1701 {
1702         ListBase tmp_data = {NULL, NULL};
1703         size_t tmp_items = 0;
1704         size_t items = 0;
1705         
1706         IdAdtTemplate *iat = ob->data;
1707         short type = 0, expanded = 0;
1708         
1709         /* get settings based on data type */
1710         switch (ob->type) {
1711                 case OB_CAMERA: /* ------- Camera ------------ */
1712                 {
1713                         Camera *ca = (Camera *)ob->data;
1714                         
1715                         if (ads->filterflag & ADS_FILTER_NOCAM)
1716                                 return 0;
1717                         
1718                         type = ANIMTYPE_DSCAM;
1719                         expanded = FILTER_CAM_OBJD(ca);
1720                 }
1721                 break;
1722                 case OB_LAMP: /* ---------- Lamp ----------- */
1723                 {
1724                         Lamp *la = (Lamp *)ob->data;
1725                         
1726                         if (ads->filterflag & ADS_FILTER_NOLAM)
1727                                 return 0;
1728                         
1729                         type = ANIMTYPE_DSLAM;
1730                         expanded = FILTER_LAM_OBJD(la);
1731                 }
1732                 break;
1733                 case OB_CURVE: /* ------- Curve ---------- */
1734                 case OB_SURF: /* ------- Nurbs Surface ---------- */
1735                 case OB_FONT: /* ------- Text Curve ---------- */
1736                 {
1737                         Curve *cu = (Curve *)ob->data;
1738                         
1739                         if (ads->filterflag & ADS_FILTER_NOCUR)
1740                                 return 0;
1741                         
1742                         type = ANIMTYPE_DSCUR;
1743                         expanded = FILTER_CUR_OBJD(cu);
1744                 }
1745                 break;
1746                 case OB_MBALL: /* ------- MetaBall ---------- */
1747                 {
1748                         MetaBall *mb = (MetaBall *)ob->data;
1749                         
1750                         if (ads->filterflag & ADS_FILTER_NOMBA)
1751                                 return 0;
1752                         
1753                         type = ANIMTYPE_DSMBALL;
1754                         expanded = FILTER_MBALL_OBJD(mb);
1755                 }
1756                 break;
1757                 case OB_ARMATURE: /* ------- Armature ---------- */
1758                 {
1759                         bArmature *arm = (bArmature *)ob->data;
1760                         
1761                         if (ads->filterflag & ADS_FILTER_NOARM)
1762                                 return 0;
1763                         
1764                         type = ANIMTYPE_DSARM;
1765                         expanded = FILTER_ARM_OBJD(arm);
1766                 }
1767                 break;
1768                 case OB_MESH: /* ------- Mesh ---------- */
1769                 {
1770                         Mesh *me = (Mesh *)ob->data;
1771                         
1772                         if (ads->filterflag & ADS_FILTER_NOMESH)
1773                                 return 0;
1774                         
1775                         type = ANIMTYPE_DSMESH;
1776                         expanded = FILTER_MESH_OBJD(me);
1777                 }
1778                 break;
1779                 case OB_LATTICE: /* ---- Lattice ---- */
1780                 {
1781                         Lattice *lt = (Lattice *)ob->data;
1782                         
1783                         if (ads->filterflag & ADS_FILTER_NOLAT)
1784                                 return 0;
1785                         
1786                         type = ANIMTYPE_DSLAT;
1787                         expanded = FILTER_LATTICE_OBJD(lt);
1788                 }
1789                 break;
1790                 case OB_SPEAKER: /* ---------- Speaker ----------- */
1791                 {
1792                         Speaker *spk = (Speaker *)ob->data;
1793                         
1794                         type = ANIMTYPE_DSSPK;
1795                         expanded = FILTER_SPK_OBJD(spk);
1796                 }
1797                 break;
1798         }
1799         
1800         /* add object data animation channels */
1801         BEGIN_ANIMFILTER_SUBCHANNELS(expanded)
1802         {
1803                 /* animation data filtering */
1804                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)iat, filter_mode);
1805                 
1806                 /* sub-data filtering... */
1807                 switch (ob->type) {
1808                         case OB_LAMP:  /* lamp - textures + nodetree */
1809                         {
1810                                 Lamp *la = ob->data;
1811                                 bNodeTree *ntree = la->nodetree;
1812
1813                                 /* nodetree */
1814                                 if ((ntree) && !(ads->filterflag & ADS_FILTER_NONTREE))
1815                                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, &la->id, ntree, filter_mode);
1816
1817                                 /* textures */
1818                                 if (!(ads->filterflag & ADS_FILTER_NOTEX))
1819                                         tmp_items += animdata_filter_ds_textures(ac, &tmp_data, ads, &la->id, filter_mode);
1820                         }
1821                         break;
1822                 }
1823         }
1824         END_ANIMFILTER_SUBCHANNELS;
1825         
1826         /* did we find anything? */
1827         if (tmp_items) {
1828                 /* include data-expand widget first */
1829                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1830                         /* check if filtering by active status */
1831                         if (ANIMCHANNEL_ACTIVEOK(iat)) {
1832                                 ANIMCHANNEL_NEW_CHANNEL(iat, type, iat);
1833                         }
1834                 }
1835                 
1836                 /* now add the list of collected channels */
1837                 BLI_movelisttolist(anim_data, &tmp_data);
1838                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1839                 items += tmp_items;
1840         }
1841         
1842         /* return the number of items added to the list */
1843         return items;
1844 }
1845
1846 /* shapekey-level animation */
1847 static size_t animdata_filter_ds_keyanim(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, Key *key, int filter_mode)
1848 {
1849         ListBase tmp_data = {NULL, NULL};
1850         size_t tmp_items = 0;
1851         size_t items = 0;
1852         
1853         /* add shapekey-level animation channels */
1854         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_SKE_OBJD(key))
1855         {
1856                 /* animation data filtering */
1857                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)key, filter_mode);
1858         }
1859         END_ANIMFILTER_SUBCHANNELS;
1860         
1861         /* did we find anything? */
1862         if (tmp_items) {
1863                 /* include key-expand widget first */
1864                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1865                         if (ANIMCHANNEL_ACTIVEOK(key)) {
1866                                 ANIMCHANNEL_NEW_CHANNEL(key, ANIMTYPE_DSSKEY, ob);
1867                         }
1868                 }
1869                 
1870                 /* now add the list of collected channels */
1871                 BLI_movelisttolist(anim_data, &tmp_data);
1872                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1873                 items += tmp_items;
1874         }
1875         
1876         /* return the number of items added to the list */
1877         return items;
1878 }
1879
1880 /* object-level animation */
1881 static size_t animdata_filter_ds_obanim(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
1882 {
1883         ListBase tmp_data = {NULL, NULL};
1884         size_t tmp_items = 0;
1885         size_t items = 0;
1886         
1887         AnimData *adt = ob->adt;
1888         short type = 0, expanded = 1;
1889         void *cdata = NULL;
1890
1891         /* determine the type of expander channels to use */
1892         /* this is the best way to do this for now... */
1893         ANIMDATA_FILTER_CASES(ob,
1894                 { /* AnimData - no channel, but consider data */ },
1895                 { /* NLA - no channel, but consider data */ },
1896                 { /* Drivers */
1897                         type = ANIMTYPE_FILLDRIVERS;
1898                         cdata = adt;
1899                         expanded = EXPANDED_DRVD(adt);
1900                 },
1901                 { /* Keyframes */
1902                         type = ANIMTYPE_FILLACTD;
1903                         cdata = adt->action;
1904                         expanded = EXPANDED_ACTC(adt->action);
1905                 });
1906                 
1907         /* add object-level animation channels */
1908         BEGIN_ANIMFILTER_SUBCHANNELS(expanded)
1909         {
1910                 /* animation data filtering */
1911                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)ob, filter_mode);
1912         }
1913         END_ANIMFILTER_SUBCHANNELS;
1914         
1915         /* did we find anything? */
1916         if (tmp_items) {
1917                 /* include anim-expand widget first */
1918                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1919                         if (type != ANIMTYPE_NONE) {
1920                                 /* NOTE: active-status (and the associated checks) don't apply here... */
1921                                 ANIMCHANNEL_NEW_CHANNEL(cdata, type, ob);
1922                         }
1923                 }
1924                 
1925                 /* now add the list of collected channels */
1926                 BLI_movelisttolist(anim_data, &tmp_data);
1927                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1928                 items += tmp_items;
1929         }
1930         
1931         /* return the number of items added to the list */
1932         return items;
1933 }
1934
1935 /* get animation channels from object2 */
1936 static size_t animdata_filter_dopesheet_ob(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1937 {
1938         ListBase tmp_data = {NULL, NULL};
1939         Object *ob = base->object;
1940         size_t tmp_items = 0;
1941         size_t items = 0;
1942         
1943         /* filter data contained under object first */
1944         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_OBJC(ob))
1945         {
1946                 Key *key = ob_get_key(ob);
1947                 
1948                 /* object-level animation */
1949                 if ((ob->adt) && !(ads->filterflag & ADS_FILTER_NOOBJ)) {
1950                         tmp_items += animdata_filter_ds_obanim(ac, &tmp_data, ads, ob, filter_mode);
1951                 }
1952                 
1953                 /* shape-key */
1954                 if ((key && key->adt) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
1955                         tmp_items += animdata_filter_ds_keyanim(ac, &tmp_data, ads, ob, key, filter_mode);
1956                 }
1957                 
1958                 /* materials */
1959                 if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT)) {
1960                         tmp_items += animdata_filter_ds_materials(ac, &tmp_data, ads, ob, filter_mode);
1961                 }
1962                 
1963                 /* object data */
1964                 if (ob->data) {
1965                         tmp_items += animdata_filter_ds_obdata(ac, &tmp_data, ads, ob, filter_mode);
1966                 }
1967                 
1968                 /* particles */
1969                 if ((ob->particlesystem.first) && !(ads->filterflag & ADS_FILTER_NOPART)) {
1970                         tmp_items += animdata_filter_ds_particles(ac, &tmp_data, ads, ob, filter_mode);
1971                 }
1972         }
1973         END_ANIMFILTER_SUBCHANNELS;
1974         
1975         
1976         /* if we collected some channels, add these to the new list... */
1977         if (tmp_items) {
1978                 /* firstly add object expander if required */
1979                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1980                         /* check if filtering by selection */
1981                         // XXX: double-check on this - most of the time, a lot of tools need to filter out these channels!
1982                         if (ANIMCHANNEL_SELOK((base->flag & SELECT))) {
1983                                 /* check if filtering by active status */
1984                                 if (ANIMCHANNEL_ACTIVEOK(ob)) {
1985                                         ANIMCHANNEL_NEW_CHANNEL(base, ANIMTYPE_OBJECT, ob);
1986                                 }
1987                         }
1988                 }
1989                 
1990                 /* now add the list of collected channels */
1991                 BLI_movelisttolist(anim_data, &tmp_data);
1992                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1993                 items += tmp_items;
1994         }
1995         
1996         /* return the number of items added */
1997         return items;
1998 }
1999
2000 static size_t animdata_filter_ds_world(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, World *wo, int filter_mode)
2001 {
2002         ListBase tmp_data = {NULL, NULL};
2003         size_t tmp_items = 0;
2004         size_t items = 0;
2005         
2006         /* add world animation channels */
2007         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_WOR_SCED(wo))
2008         {
2009                 /* animation data filtering */
2010                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)wo, filter_mode);
2011                 
2012                 /* textures for world */
2013                 if (!(ads->filterflag & ADS_FILTER_NOTEX))
2014                         items += animdata_filter_ds_textures(ac, &tmp_data, ads, (ID *)wo, filter_mode);
2015                         
2016                 /* nodes */
2017                 if ((wo->nodetree) && !(ads->filterflag & ADS_FILTER_NONTREE)) 
2018                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)wo, wo->nodetree, filter_mode);
2019         }
2020         END_ANIMFILTER_SUBCHANNELS;
2021         
2022         /* did we find anything? */
2023         if (tmp_items) {
2024                 /* include data-expand widget first */
2025                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2026                         /* check if filtering by active status */
2027                         if (ANIMCHANNEL_ACTIVEOK(wo)) {
2028                                 ANIMCHANNEL_NEW_CHANNEL(wo, ANIMTYPE_DSWOR, sce);
2029                         }
2030                 }
2031                 
2032                 /* now add the list of collected channels */
2033                 BLI_movelisttolist(anim_data, &tmp_data);
2034                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
2035                 items += tmp_items;
2036         }
2037         
2038         /* return the number of items added to the list */
2039         return items;
2040 }
2041
2042 static size_t animdata_filter_ds_scene(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
2043 {
2044         ListBase tmp_data = {NULL, NULL};
2045         size_t tmp_items = 0;
2046         size_t items = 0;
2047         
2048         AnimData *adt = sce->adt;
2049         short type = 0, expanded = 1;
2050         void *cdata = NULL;
2051         
2052         /* determine the type of expander channels to use */
2053         // this is the best way to do this for now...
2054         ANIMDATA_FILTER_CASES(sce,
2055                 { /* AnimData - no channel, but consider data */},
2056                 { /* NLA - no channel, but consider data */},
2057                 { /* Drivers */
2058                         type = ANIMTYPE_FILLDRIVERS;
2059                         cdata = adt;
2060                         expanded = EXPANDED_DRVD(adt);
2061                 },
2062                 { /* Keyframes */
2063                         type = ANIMTYPE_FILLACTD;
2064                         cdata = adt->action;
2065                         expanded = EXPANDED_ACTC(adt->action);
2066                 });
2067                 
2068         /* add scene-level animation channels */
2069         BEGIN_ANIMFILTER_SUBCHANNELS(expanded)
2070         {
2071                 /* animation data filtering */
2072                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)sce, filter_mode);
2073         }
2074         END_ANIMFILTER_SUBCHANNELS;
2075         
2076         /* did we find anything? */
2077         if (tmp_items) {
2078                 /* include anim-expand widget first */
2079                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2080                         if (type != ANIMTYPE_NONE) {
2081                                 /* NOTE: active-status (and the associated checks) don't apply here... */
2082                                 ANIMCHANNEL_NEW_CHANNEL(cdata, type, sce);
2083                         }
2084                 }
2085                 
2086                 /* now add the list of collected channels */
2087                 BLI_movelisttolist(anim_data, &tmp_data);
2088                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
2089                 items += tmp_items;
2090         }
2091         
2092         /* return the number of items added to the list */
2093         return items;
2094 }
2095
2096 static size_t animdata_filter_dopesheet_scene(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
2097 {
2098         ListBase tmp_data = {NULL, NULL};
2099         size_t tmp_items = 0;
2100         size_t items = 0;
2101         
2102         /* filter data contained under object first */
2103         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_SCEC(sce))
2104         {
2105                 bNodeTree *ntree = sce->nodetree;
2106                 World *wo = sce->world;
2107                 
2108                 /* Action, Drivers, or NLA for Scene */
2109                 if ((ads->filterflag & ADS_FILTER_NOSCE) == 0) {
2110                         tmp_items += animdata_filter_ds_scene(ac, &tmp_data, ads, sce, filter_mode);
2111                 }
2112                 
2113                 /* world */
2114                 if ((wo) && !(ads->filterflag & ADS_FILTER_NOWOR)) {
2115                         tmp_items += animdata_filter_ds_world(ac, &tmp_data, ads, sce, wo, filter_mode);
2116                 }
2117                 
2118                 /* nodetree */
2119                 if ((ntree) && !(ads->filterflag & ADS_FILTER_NONTREE)) {
2120                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)sce, ntree, filter_mode);
2121                 }
2122
2123                 /* TODO: one day, when sequencer becomes its own datatype, perhaps it should be included here */
2124         }
2125         END_ANIMFILTER_SUBCHANNELS;
2126
2127         /* if we collected some channels, add these to the new list... */
2128         if (tmp_items) {
2129                 /* firstly add object expander if required */
2130                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
2131                         /* check if filtering by selection */
2132                         if (ANIMCHANNEL_SELOK((sce->flag & SCE_DS_SELECTED))) {
2133                                 /* NOTE: active-status doesn't matter for this! */
2134                                 ANIMCHANNEL_NEW_CHANNEL(sce, ANIMTYPE_SCENE, sce);
2135                         }
2136                 }
2137                 
2138                 /* now add the list of collected channels */
2139                 BLI_movelisttolist(anim_data, &tmp_data);
2140                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
2141                 items += tmp_items;
2142         }
2143         
2144         /* return the number of items added */
2145         return items;
2146 }
2147
2148 // 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)
2149 static size_t animdata_filter_dopesheet(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, int filter_mode)
2150 {
2151         Scene *sce = (Scene *)ads->source;
2152         Base *base;
2153         size_t items = 0;
2154         
2155         /* check that we do indeed have a scene */
2156         if ((ads->source == NULL) || (GS(ads->source->name) != ID_SCE)) {
2157                 printf("DopeSheet Error: Not scene!\n");
2158                 if (G.debug & G_DEBUG)
2159                         printf("\tPointer = %p, Name = '%s'\n", (void *)ads->source, (ads->source) ? ads->source->name : NULL);
2160                 return 0;
2161         }
2162         
2163         /* augment the filter-flags with settings based on the dopesheet filterflags 
2164          * so that some temp settings can get added automagically...
2165          */
2166         if (ads->filterflag & ADS_FILTER_SELEDIT) {
2167                 /* only selected F-Curves should get their keyframes considered for editability */
2168                 filter_mode |= ANIMFILTER_SELEDIT;
2169         }
2170         
2171         /* scene-linked animation - e.g. world, compositing nodes, scene anim (including sequencer currently) */
2172         items += animdata_filter_dopesheet_scene(ac, anim_data, ads, sce, filter_mode);
2173         
2174         /* loop over all bases (i.e.objects) in the scene */
2175         for (base = sce->base.first; base; base = base->next) {
2176                 /* check if there's an object (all the relevant checks are done in the ob-function) */
2177                 if (base->object) {
2178                         Object *ob = base->object;
2179                         
2180                         /* firstly, check if object can be included, by the following factors:
2181                          *      - if only visible, must check for layer and also viewport visibility
2182                          *              --> while tools may demand only visible, user setting takes priority
2183                          *                      as user option controls whether sets of channels get included while
2184                          *                      tool-flag takes into account collapsed/open channels too
2185                          *      - if only selected, must check if object is selected 
2186                          *      - there must be animation data to edit (this is done recursively as we 
2187                          *        try to add the channels)
2188                          */
2189                         if ((filter_mode & ANIMFILTER_DATA_VISIBLE) && !(ads->filterflag & ADS_FILTER_INCL_HIDDEN)) {
2190                                 /* layer visibility - we check both object and base, since these may not be in sync yet */
2191                                 if ((sce->lay & (ob->lay | base->lay)) == 0) continue;
2192                                 
2193                                 /* outliner restrict-flag */
2194                                 if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
2195                         }
2196                         
2197                         /* if only F-Curves with visible flags set can be shown, check that 
2198                          * datablock hasn't been set to invisible 
2199                          */
2200                         if (filter_mode & ANIMFILTER_CURVE_VISIBLE) {
2201                                 if ((ob->adt) && (ob->adt->flag & ADT_CURVES_NOT_VISIBLE))
2202                                         continue;
2203                         }
2204                         
2205                         /* check selection and object type filters */
2206                         if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) /*|| (base == sce->basact)*/) ) {
2207                                 /* only selected should be shown */
2208                                 continue;
2209                         }
2210                         
2211                         /* check if object belongs to the filtering group if option to filter 
2212                          * objects by the grouped status is on
2213                          *      - used to ease the process of doing multiple-character choreographies
2214                          */
2215                         if (ads->filterflag & ADS_FILTER_ONLYOBGROUP) {
2216                                 if (object_in_group(ob, ads->filter_grp) == 0)
2217                                         continue;
2218                         }
2219                                 
2220                         /* since we're still here, this object should be usable */
2221                         items += animdata_filter_dopesheet_ob(ac, anim_data, ads, base, filter_mode);
2222                 }
2223         }
2224         
2225         /* return the number of items in the list */
2226         return items;
2227 }
2228
2229 /* Summary track for DopeSheet/Action Editor 
2230  *  - return code is whether the summary lets the other channels get drawn
2231  */
2232 static short animdata_filter_dopesheet_summary(bAnimContext *ac, ListBase *anim_data, int filter_mode, size_t *items)
2233 {
2234         bDopeSheet *ads = NULL;
2235         
2236         /* get the DopeSheet information to use 
2237          *      - we should only need to deal with the DopeSheet/Action Editor, 
2238          *        since all the other Animation Editors won't have this concept
2239          *        being applicable.
2240          */
2241         if ((ac && ac->sl) && (ac->spacetype == SPACE_ACTION)) {
2242                 SpaceAction *saction = (SpaceAction *)ac->sl;
2243                 ads = &saction->ads;
2244         }
2245         else {
2246                 /* invalid space type - skip this summary channels */
2247                 return 1;
2248         }
2249         
2250         /* dopesheet summary 
2251          *      - only for drawing and/or selecting keyframes in channels, but not for real editing 
2252          *      - only useful for DopeSheet/Action/etc. editors where it is actually useful
2253          */
2254         if ((filter_mode & ANIMFILTER_LIST_CHANNELS) && (ads->filterflag & ADS_FILTER_SUMMARY)) {
2255                 bAnimListElem *ale = make_new_animlistelem(ac, ANIMTYPE_SUMMARY, NULL);
2256                 if (ale) {
2257                         BLI_addtail(anim_data, ale);
2258                         (*items)++;
2259                 }
2260                 
2261                 /* if summary is collapsed, don't show other channels beneath this 
2262                  *      - this check is put inside the summary check so that it doesn't interfere with normal operation
2263                  */ 
2264                 if (ads->flag & ADS_FLAG_SUMMARY_COLLAPSED)
2265                         return 0;
2266         }
2267         
2268         /* the other channels beneath this can be shown */
2269         return 1;
2270 }  
2271
2272 /* ......................... */
2273
2274 /* filter data associated with a channel - usually for handling summary-channels in DopeSheet */
2275 static size_t animdata_filter_animchan(bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, bAnimListElem *channel, int filter_mode)
2276 {
2277         size_t items = 0;
2278         
2279         /* data to filter depends on channel type */
2280         // XXX: only common channel-types have been handled for now
2281         switch (channel->type) {
2282                 case ANIMTYPE_SUMMARY:
2283                         items += animdata_filter_dopesheet(ac, anim_data, ads, filter_mode);
2284                         break;
2285                         
2286                 case ANIMTYPE_SCENE:
2287                         items += animdata_filter_dopesheet_scene(ac, anim_data, ads, channel->data, filter_mode);
2288                         break;
2289                 
2290                 case ANIMTYPE_OBJECT:
2291                         items += animdata_filter_dopesheet_ob(ac, anim_data, ads, channel->data, filter_mode);
2292                         break;
2293         }
2294         
2295         return items;
2296 }
2297
2298 /* ----------- Cleanup API --------------- */
2299
2300 /* Remove entries with invalid types in animation channel list */
2301 static size_t animdata_filter_remove_invalid(ListBase *anim_data)
2302 {
2303         bAnimListElem *ale, *next;
2304         size_t items = 0;
2305         
2306         /* only keep entries with valid types */
2307         for (ale = anim_data->first; ale; ale = next) {
2308                 next = ale->next;
2309                 
2310                 if (ale->type == ANIMTYPE_NONE)
2311                         BLI_freelinkN(anim_data, ale);
2312                 else
2313                         items++;
2314         }
2315         
2316         return items;
2317 }
2318
2319 /* Remove duplicate entries in animation channel list */
2320 static size_t animdata_filter_remove_duplis(ListBase *anim_data)
2321 {
2322         bAnimListElem *ale, *next;
2323         GHash *gh;
2324         size_t items = 0;
2325         
2326         /* build new hashtable to efficiently store and retrieve which entries have been 
2327          * encountered already while searching
2328          */
2329         gh = BLI_ghash_ptr_new("animdata_filter_duplis_remove gh");
2330         
2331         /* loop through items, removing them from the list if a similar item occurs already */
2332         for (ale = anim_data->first; ale; ale = next) {
2333                 next = ale->next;
2334                 
2335                 /* check if hash has any record of an entry like this 
2336                  *      - just use ale->data for now, though it would be nicer to involve 
2337                  *        ale->type in combination too to capture corner cases (where same data performs differently)
2338                  */
2339                 if (BLI_ghash_haskey(gh, ale->data) == 0) {
2340                         /* this entry is 'unique' and can be kept */
2341                         BLI_ghash_insert(gh, ale->data, NULL);
2342                         items++;
2343                 }
2344                 else {
2345                         /* this entry isn't needed anymore */
2346                         BLI_freelinkN(anim_data, ale);
2347                 }
2348         }
2349         
2350         /* free the hash... */
2351         BLI_ghash_free(gh, NULL, NULL);
2352         
2353         /* return the number of items still in the list */
2354         return items;
2355 }
2356
2357 /* ----------- Public API --------------- */
2358
2359 /* This function filters the active data source to leave only animation channels suitable for
2360  * usage by the caller. It will return the length of the list 
2361  * 
2362  *  *anim_data: is a pointer to a ListBase, to which the filtered animation channels
2363  *              will be placed for use.
2364  *      filter_mode: how should the data be filtered - bitmapping accessed flags
2365  */
2366 size_t ANIM_animdata_filter(bAnimContext *ac, ListBase *anim_data, int filter_mode, void *data, short datatype)
2367 {
2368         size_t items = 0;
2369         
2370         /* only filter data if there's somewhere to put it */
2371         if (data && anim_data) {
2372                 
2373                 /* firstly filter the data */
2374                 switch (datatype) {
2375                         case ANIMCONT_ACTION:   /* 'Action Editor' */
2376                         {
2377                                 Object *obact = ac->obact;
2378                                 SpaceAction *saction = (SpaceAction *)ac->sl;
2379                                 bDopeSheet *ads = (saction) ? &saction->ads : NULL;
2380                                 
2381                                 /* the check for the DopeSheet summary is included here since the summary works here too */
2382                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2383                                         items += animfilter_action(ac, anim_data, ads, data, filter_mode, (ID *)obact);
2384                         }
2385                         break;
2386                         
2387                         case ANIMCONT_SHAPEKEY: /* 'ShapeKey Editor' */
2388                         {
2389                                 /* the check for the DopeSheet summary is included here since the summary works here too */
2390                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2391                                         items = animdata_filter_shapekey(ac, anim_data, data, filter_mode);
2392                         }
2393                         break;
2394                                 
2395                         case ANIMCONT_GPENCIL:
2396                         {
2397                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2398                                         items = animdata_filter_gpencil(anim_data, data, filter_mode);
2399                         }
2400                         break;
2401                         
2402                         case ANIMCONT_MASK:
2403                         {
2404                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2405                                         items = animdata_filter_mask(anim_data, data, filter_mode);
2406                         }
2407                         break;
2408                         
2409                         case ANIMCONT_DOPESHEET: /* 'DopeSheet Editor' */
2410                         {
2411                                 /* the DopeSheet editor is the primary place where the DopeSheet summaries are useful */
2412                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2413                                         items += animdata_filter_dopesheet(ac, anim_data, data, filter_mode);
2414                         }
2415                         break;
2416                                 
2417                         case ANIMCONT_FCURVES: /* Graph Editor -> FCurves/Animation Editing */
2418                         case ANIMCONT_DRIVERS: /* Graph Editor -> Drivers Editing */
2419                         case ANIMCONT_NLA: /* NLA Editor */
2420                         {
2421                                 /* all of these editors use the basic DopeSheet data for filtering options, but don't have all the same features */
2422                                 items = animdata_filter_dopesheet(ac, anim_data, data, filter_mode);
2423                         }
2424                         break;
2425                         
2426                         case ANIMCONT_CHANNEL: /* animation channel */
2427                         {
2428                                 bDopeSheet *ads = ac->ads;
2429                                 
2430                                 /* based on the channel type, filter relevant data for this */
2431                                 items = animdata_filter_animchan(ac, anim_data, ads, data, filter_mode);
2432                         }
2433                         break;
2434                 }
2435                         
2436                 /* remove any 'weedy' entries */
2437                 items = animdata_filter_remove_invalid(anim_data);
2438                 
2439                 /* remove duplicates (if required) */
2440                 if (filter_mode & ANIMFILTER_NODUPLIS)
2441                         items = animdata_filter_remove_duplis(anim_data);
2442         }
2443         
2444         /* return the number of items in the list */
2445         return items;
2446 }
2447
2448 /* ************************************************************ */