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