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