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