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