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