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