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