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