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