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