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