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