544157c40305e3d1f38712143c6c765bd49d396c
[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                                 ale->key_data= &nlt->strips;
738                                 ale->datatype= ALE_NLASTRIP;
739                         }
740                                 break;
741                         case ANIMTYPE_NLAACTION:
742                         {
743                                 /* nothing to include for now... nothing editable from NLA-perspective here */
744                                 ale->key_data= NULL;
745                                 ale->datatype= ALE_NONE;
746                         }
747                                 break;
748                 }
749         }
750         
751         /* return created datatype */
752         return ale;
753 }
754  
755 /* ----------------------------------------- */
756
757 /* find the next F-Curve that is usable for inclusion */
758 static FCurve *animdata_filter_fcurve_next (bDopeSheet *ads, FCurve *first, bActionGroup *grp, int filter_mode, ID *owner_id)
759 {
760         FCurve *fcu = NULL;
761         
762         /* loop over F-Curves - assume that the caller of this has already checked that these should be included 
763          * NOTE: we need to check if the F-Curves belong to the same group, as this gets called for groups too...
764          */
765         for (fcu= first; ((fcu) && (fcu->grp==grp)); fcu= fcu->next) {
766                 /* special exception for Pose-Channel Based F-Curves:
767                  *      - the 'Only Selected' data filter should be applied to Pose-Channel data too, but those are
768                  *        represented as F-Curves. The way the filter for objects worked was to be the first check
769                  *        after 'normal' visibility, so this is done first here too...
770                  *      - we currently use an 'approximate' method for getting these F-Curves that doesn't require
771                  *        carefully checking the entire path
772                  *      - this will also affect things like Drivers, and also works for Bone Constraints
773                  */
774                 if ( ((ads) && (ads->filterflag & ADS_FILTER_ONLYSEL)) && 
775                          ((owner_id) && (GS(owner_id->name) == ID_OB)) ) 
776                 {
777                         Object *ob= (Object *)owner_id;
778                         
779                         /* only consider if F-Curve involves pose.bones */
780                         if ((fcu->rna_path) && strstr(fcu->rna_path, "bones")) {
781                                 bPoseChannel *pchan;
782                                 char *bone_name;
783                                 
784                                 /* get bone-name, and check if this bone is selected */
785                                 bone_name= BLI_getQuotedStr(fcu->rna_path, "bones[");
786                                 pchan= get_pose_channel(ob->pose, bone_name);
787                                 if (bone_name) MEM_freeN(bone_name);
788                                 
789                                 /* can only add this F-Curve if it is selected */
790                                 if ((pchan) && (pchan->bone) && (pchan->bone->flag & BONE_SELECTED)==0)
791                                         continue;
792                         }
793                 }
794                 
795                 /* only include if visible (Graph Editor check, not channels check) */
796                 if (!(filter_mode & ANIMFILTER_CURVEVISIBLE) || (fcu->flag & FCURVE_VISIBLE)) {
797                         /* only work with this channel and its subchannels if it is editable */
798                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_FCU(fcu)) {
799                                 /* only include this curve if selected in a way consistent with the filtering requirements */
800                                 if ( ANIMCHANNEL_SELOK(SEL_FCU(fcu)) && ANIMCHANNEL_SELEDITOK(SEL_FCU(fcu)) ) {
801                                         /* only include if this curve is active */
802                                         if (!(filter_mode & ANIMFILTER_ACTIVE) || (fcu->flag & FCURVE_ACTIVE)) {
803                                                 /* this F-Curve can be used, so return it */
804                                                 return fcu;
805                                         }
806                                 }
807                         }
808                 }
809         }
810         
811         /* no (more) F-Curves from the list are suitable... */
812         return NULL;
813 }
814
815 static int animdata_filter_fcurves (ListBase *anim_data, bDopeSheet *ads, FCurve *first, bActionGroup *grp, void *owner, short ownertype, int filter_mode, ID *owner_id)
816 {
817         FCurve *fcu;
818         int items = 0;
819         
820         /* loop over every F-Curve able to be included 
821          *      - this for-loop works like this: 
822          *              1) the starting F-Curve is assigned to the fcu pointer so that we have a starting point to search from
823          *              2) the first valid F-Curve to start from (which may include the one given as 'first') in the remaining 
824          *                 list of F-Curves is found, and verified to be non-null
825          *              3) the F-Curve referenced by fcu pointer is added to the list
826          *              4) the fcu pointer is set to the F-Curve after the one we just added, so that we can keep going through 
827          *                 the rest of the F-Curve list without an eternal loop. Back to step 2 :)
828          */
829         for (fcu=first; ( (fcu = animdata_filter_fcurve_next(ads, fcu, grp, filter_mode, owner_id)) ); fcu=fcu->next)
830         {
831                 bAnimListElem *ale = make_new_animlistelem(fcu, ANIMTYPE_FCURVE, owner, ownertype, owner_id);
832                 
833                 if (ale) {
834                         BLI_addtail(anim_data, ale);
835                         items++;
836                 }
837         }
838         
839         /* return the number of items added to the list */
840         return items;
841 }
842
843 static int animdata_filter_action (ListBase *anim_data, bDopeSheet *ads, bAction *act, int filter_mode, void *owner, short ownertype, ID *owner_id)
844 {
845         bAnimListElem *ale=NULL;
846         bActionGroup *agrp;
847         FCurve *lastchan=NULL;
848         int items = 0;
849         
850         /* loop over groups */
851         // TODO: in future, should we expect to need nested groups?
852         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
853                 FCurve *first_fcu;
854                 
855                 /* store reference to last channel of group */
856                 if (agrp->channels.last) 
857                         lastchan= agrp->channels.last;
858                 
859                 /* get the first F-Curve in this group we can start to use, 
860                  * and if there isn't any F-Curve to start from, then don't 
861                  * this group at all...
862                  *
863                  * exceptions for when we might not care whether there's anything inside this group or not
864                  *      - if we're interested in channels and their selections, in which case group channel should get considered too
865                  *        even if all its sub channels are hidden...
866                  */
867                 first_fcu = animdata_filter_fcurve_next(ads, agrp->channels.first, agrp, filter_mode, owner_id);
868                 
869                 if ( (filter_mode & (ANIMFILTER_SEL|ANIMFILTER_UNSEL)) ||
870                          (first_fcu) ) 
871                 {
872                         /* add this group as a channel first */
873                         if ((filter_mode & ANIMFILTER_CHANNELS) || !(filter_mode & ANIMFILTER_CURVESONLY)) {
874                                 /* check if filtering by selection */
875                                 if ( ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) ) {
876                                         ale= make_new_animlistelem(agrp, ANIMTYPE_GROUP, NULL, ANIMTYPE_NONE, owner_id);
877                                         if (ale) {
878                                                 BLI_addtail(anim_data, ale);
879                                                 items++;
880                                         }
881                                 }
882                         }
883                         
884                         /* there are some situations, where only the channels of the action group should get considered */
885                         if (!(filter_mode & ANIMFILTER_ACTGROUPED) || (agrp->flag & AGRP_ACTIVE)) {
886                                 /* filters here are a bit convoulted...
887                                  *      - groups show a "summary" of keyframes beside their name which must accessable for tools which handle keyframes
888                                  *      - groups can be collapsed (and those tools which are only interested in channels rely on knowing that group is closed)
889                                  *
890                                  * cases when we should include F-Curves inside group:
891                                  *      - we don't care about visibility
892                                  *      - group is expanded
893                                  *      - we just need the F-Curves present
894                                  */
895                                 if ( (!(filter_mode & ANIMFILTER_VISIBLE) || EXPANDED_AGRP(agrp)) || (filter_mode & ANIMFILTER_CURVESONLY) ) 
896                                 {
897                                         /* for the Graph Editor, curves may be set to not be visible in the view to lessen clutter,
898                                          * but to do this, we need to check that the group doesn't have it's not-visible flag set preventing 
899                                          * all its sub-curves to be shown
900                                          */
901                                         if ( !(filter_mode & ANIMFILTER_CURVEVISIBLE) || !(agrp->flag & AGRP_NOTVISIBLE) )
902                                         {
903                                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_AGRP(agrp)) {
904                                                         items += animdata_filter_fcurves(anim_data, ads, first_fcu, agrp, owner, ownertype, filter_mode, owner_id);
905                                                 }
906                                         }
907                                 }
908                         }
909                 }
910         }
911         
912         /* loop over un-grouped F-Curves (only if we're not only considering those channels in the animive group) */
913         if (!(filter_mode & ANIMFILTER_ACTGROUPED))  {
914                 // XXX the 'owner' info here needs review...
915                 items += animdata_filter_fcurves(anim_data, ads, (lastchan)?(lastchan->next):(act->curves.first), NULL, owner, ownertype, filter_mode, owner_id);
916         }
917         
918         /* return the number of items added to the list */
919         return items;
920 }
921
922 /* Include NLA-Data for NLA-Editor:
923  *      - when ANIMFILTER_CHANNELS is used, that means we should be filtering the list for display
924  *        Although the evaluation order is from the first track to the last and then apply the Action on top,
925  *        we present this in the UI as the Active Action followed by the last track to the first so that we 
926  *        get the evaluation order presented as per a stack.
927  *      - for normal filtering (i.e. for editing), we only need the NLA-tracks but they can be in 'normal' evaluation
928  *        order, i.e. first to last. Otherwise, some tools may get screwed up.
929  */
930 static int animdata_filter_nla (ListBase *anim_data, bDopeSheet *ads, AnimData *adt, int filter_mode, void *owner, short ownertype, ID *owner_id)
931 {
932         bAnimListElem *ale;
933         NlaTrack *nlt;
934         NlaTrack *first=NULL, *next=NULL;
935         int items = 0;
936         
937         /* if showing channels, include active action */
938         if (filter_mode & ANIMFILTER_CHANNELS) {
939                 /* there isn't really anything editable here, so skip if need editable */
940                 // TODO: currently, selection isn't checked since it doesn't matter
941                 if ((filter_mode & ANIMFILTER_FOREDIT) == 0) { 
942                         /* just add the action track now (this MUST appear for drawing)
943                          *      - as AnimData may not have an action, we pass a dummy pointer just to get the list elem created, then
944                          *        overwrite this with the real value - REVIEW THIS...
945                          */
946                         ale= make_new_animlistelem((void *)(&adt->action), ANIMTYPE_NLAACTION, owner, ownertype, owner_id);
947                         ale->data= (adt->action) ? adt->action : NULL;
948                                 
949                         if (ale) {
950                                 BLI_addtail(anim_data, ale);
951                                 items++;
952                         }
953                 }
954                 
955                 /* first track to include will be the last one if we're filtering by channels */
956                 first= adt->nla_tracks.last;
957         }
958         else {
959                 /* first track to include will the the first one (as per normal) */
960                 first= adt->nla_tracks.first;
961         }
962         
963         /* loop over NLA Tracks - assume that the caller of this has already checked that these should be included */
964         for (nlt= first; nlt; nlt= next) {
965                 /* 'next' NLA-Track to use depends on whether we're filtering for drawing or not */
966                 if (filter_mode & ANIMFILTER_CHANNELS) 
967                         next= nlt->prev;
968                 else
969                         next= nlt->next;
970                 
971                 /* if we're in NLA-tweakmode, don't show this track if it was disabled (due to tweaking) for now 
972                  *      - active track should still get shown though (even though it has disabled flag set)
973                  */
974                 // FIXME: the channels after should still get drawn, just 'differently', and after an active-action channel
975                 if ((adt->flag & ADT_NLA_EDIT_ON) && (nlt->flag & NLATRACK_DISABLED) && !(nlt->flag & NLATRACK_ACTIVE))
976                         continue;
977                 
978                 /* only work with this channel and its subchannels if it is editable */
979                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_NLT(nlt)) {
980                         /* only include this track if selected in a way consistent with the filtering requirements */
981                         if ( ANIMCHANNEL_SELOK(SEL_NLT(nlt)) ) {
982                                 /* only include if this track is active */
983                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (nlt->flag & NLATRACK_ACTIVE)) {
984                                         ale= make_new_animlistelem(nlt, ANIMTYPE_NLATRACK, owner, ownertype, owner_id);
985                                                 
986                                         if (ale) {
987                                                 BLI_addtail(anim_data, ale);
988                                                 items++;
989                                         }
990                                 }
991                         }
992                 }
993         }
994         
995         /* return the number of items added to the list */
996         return items;
997 }
998
999 /* Include ShapeKey Data for ShapeKey Editor */
1000 static int animdata_filter_shapekey (ListBase *anim_data, Key *key, int filter_mode)
1001 {
1002         bAnimListElem *ale;
1003         int items = 0;
1004         
1005         /* check if channels or only F-Curves */
1006         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {
1007                 KeyBlock *kb;
1008                 
1009                 /* loop through the channels adding ShapeKeys as appropriate */
1010                 for (kb= key->block.first; kb; kb= kb->next) {
1011                         /* skip the first one, since that's the non-animateable basis */
1012                         // XXX maybe in future this may become handy?
1013                         if (kb == key->block.first) continue;
1014                         
1015                         /* only work with this channel and its subchannels if it is editable */
1016                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_SHAPEKEY(kb)) {
1017                                 /* only include this track if selected in a way consistent with the filtering requirements */
1018                                 if ( ANIMCHANNEL_SELOK(SEL_SHAPEKEY(kb)) ) {
1019                                         // TODO: consider 'active' too?
1020                                         
1021                                         /* owner-id here must be key so that the F-Curve can be resolved... */
1022                                         ale= make_new_animlistelem(kb, ANIMTYPE_SHAPEKEY, NULL, ANIMTYPE_NONE, (ID *)key);
1023                                         
1024                                         if (ale) {
1025                                                 BLI_addtail(anim_data, ale);
1026                                                 items++;
1027                                         }
1028                                 }
1029                         }
1030                 }
1031         }
1032         else {
1033                 /* just use the action associated with the shapekey */
1034                 // FIXME: is owner-id and having no owner/dopesheet really fine?
1035                 if (key->adt) {
1036                         if (filter_mode & ANIMFILTER_ANIMDATA)
1037                                 ANIMDATA_ADD_ANIMDATA(key)
1038                         else if (key->adt->action)
1039                                 items= animdata_filter_action(anim_data, NULL, key->adt->action, filter_mode, NULL, ANIMTYPE_NONE, (ID *)key);
1040                 }
1041         }
1042         
1043         /* return the number of items added to the list */
1044         return items;
1045 }
1046
1047 #if 0
1048 // FIXME: switch this to use the bDopeSheet...
1049 static int animdata_filter_gpencil (ListBase *anim_data, bScreen *sc, int filter_mode)
1050 {
1051         bAnimListElem *ale;
1052         ScrArea *sa, *curarea;
1053         bGPdata *gpd;
1054         bGPDlayer *gpl;
1055         int items = 0;
1056         
1057         /* check if filtering types are appropriate */
1058         {
1059                 /* special hack for fullscreen area (which must be this one then):
1060                  *      - we use the curarea->full as screen to get spaces from, since the
1061                  *        old (pre-fullscreen) screen was stored there...
1062                  *      - this is needed as all data would otherwise disappear
1063                  */
1064                 // XXX need to get new alternative for curarea
1065                 if ((curarea->full) && (curarea->spacetype==SPACE_ACTION))
1066                         sc= curarea->full;
1067                 
1068                 /* loop over spaces in current screen, finding gpd blocks (could be slow!) */
1069                 for (sa= sc->areabase.first; sa; sa= sa->next) {
1070                         /* try to get gp data */
1071                         // XXX need to put back grease pencil api...
1072                         gpd= gpencil_data_get_active(sa);
1073                         if (gpd == NULL) continue;
1074                         
1075                         /* add gpd as channel too (if for drawing, and it has layers) */
1076                         if ((filter_mode & ANIMFILTER_CHANNELS) && (gpd->layers.first)) {
1077                                 /* add to list */
1078                                 ale= make_new_animlistelem(gpd, ANIMTYPE_GPDATABLOCK, sa, ANIMTYPE_SPECIALDATA);
1079                                 if (ale) {
1080                                         BLI_addtail(anim_data, ale);
1081                                         items++;
1082                                 }
1083                         }
1084                         
1085                         /* only add layers if they will be visible (if drawing channels) */
1086                         if ( !(filter_mode & ANIMFILTER_VISIBLE) || (EXPANDED_GPD(gpd)) ) {
1087                                 /* loop over layers as the conditions are acceptable */
1088                                 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
1089                                         /* only if selected */
1090                                         if ( ANIMCHANNEL_SELOK(SEL_GPL(gpl)) ) {
1091                                                 /* only if editable */
1092                                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
1093                                                         /* add to list */
1094                                                         ale= make_new_animlistelem(gpl, ANIMTYPE_GPLAYER, gpd, ANIMTYPE_GPDATABLOCK);
1095                                                         if (ale) {
1096                                                                 BLI_addtail(anim_data, ale);
1097                                                                 items++;
1098                                                         }
1099                                                 }
1100                                         }
1101                                 }
1102                         }
1103                 }
1104         }
1105         
1106         /* return the number of items added to the list */
1107         return items;
1108 }
1109 #endif 
1110
1111
1112 static int animdata_filter_dopesheet_mats (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1113 {
1114         ListBase mats = {NULL, NULL};
1115         LinkData *ld;
1116         
1117         bAnimListElem *ale=NULL;
1118         Object *ob= base->object;
1119         int items=0, a=0;
1120         
1121         /* firstly check that we actuallly have some materials, by gathering all materials in a temp list */
1122         for (a=0; a < ob->totcol; a++) {
1123                 Material *ma= give_current_material(ob, a);
1124                 short ok = 0;
1125                 
1126                 /* for now, if no material returned, skip (this shouldn't confuse the user I hope) */
1127                 if (ELEM(NULL, ma, ma->adt)) 
1128                         continue;
1129                 
1130                 /* check if ok */
1131                 ANIMDATA_FILTER_CASES(ma, 
1132                         { /* AnimData blocks - do nothing... */ },
1133                         ok=1;, 
1134                         ok=1;, 
1135                         ok=1;)
1136                 if (ok == 0) continue;
1137                 
1138                 /* make a temp list elem for this */
1139                 ld= MEM_callocN(sizeof(LinkData), "DopeSheet-MaterialCache");
1140                 ld->data= ma;
1141                 BLI_addtail(&mats, ld);
1142         }
1143         
1144         /* if there were no channels found, no need to carry on */
1145         if (mats.first == NULL)
1146                 return 0;
1147         
1148         /* include materials-expand widget? */
1149         if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1150                 ale= make_new_animlistelem(ob, ANIMTYPE_FILLMATD, base, ANIMTYPE_OBJECT, (ID *)ob);
1151                 if (ale) {
1152                         BLI_addtail(anim_data, ale);
1153                         items++;
1154                 }
1155         }
1156         
1157         /* add materials? */
1158         if (FILTER_MAT_OBJC(ob) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1159                 /* for each material in cache, add channels  */
1160                 for (ld= mats.first; ld; ld= ld->next) {
1161                         Material *ma= (Material *)ld->data;
1162                         
1163                         /* include material-expand widget? */
1164                         // hmm... do we need to store the index of this material in the array anywhere?
1165                         if (filter_mode & ANIMFILTER_CHANNELS) {
1166                                 /* check if filtering by active status */
1167                                 if ANIMCHANNEL_ACTIVEOK(ma) {
1168                                         ale= make_new_animlistelem(ma, ANIMTYPE_DSMAT, base, ANIMTYPE_OBJECT, (ID *)ma);
1169                                         if (ale) {
1170                                                 BLI_addtail(anim_data, ale);
1171                                                 items++;
1172                                         }
1173                                 }
1174                         }
1175                         
1176                         /* add material's animation data */
1177                         if (!(filter_mode & ANIMFILTER_VISIBLE) || FILTER_MAT_OBJD(ma) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1178                                 ANIMDATA_FILTER_CASES(ma, 
1179                                         { /* AnimData blocks - do nothing... */ },
1180                                         items += animdata_filter_nla(anim_data, ads, ma->adt, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);, 
1181                                         items += animdata_filter_fcurves(anim_data, ads, ma->adt->drivers.first, NULL, ma, ANIMTYPE_DSMAT, filter_mode, (ID *)ma);, 
1182                                         items += animdata_filter_action(anim_data, ads, ma->adt->action, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);)
1183                         }
1184                 }
1185         }
1186         
1187         /* free cache */
1188         BLI_freelistN(&mats);
1189         
1190         /* return the number of items added to the list */
1191         return items;
1192 }
1193
1194 static int animdata_filter_dopesheet_particles (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1195 {
1196         bAnimListElem *ale=NULL;
1197         Object *ob= base->object;
1198         ParticleSystem *psys = ob->particlesystem.first;
1199         int items= 0, first = 1;
1200
1201         for(; psys; psys=psys->next) {
1202                 short ok = 0;
1203
1204                 if(ELEM(NULL, psys->part, psys->part->adt))
1205                         continue;
1206
1207                 ANIMDATA_FILTER_CASES(psys->part,
1208                         { /* AnimData blocks - do nothing... */ },
1209                         ok=1;, 
1210                         ok=1;, 
1211                         ok=1;)
1212                 if (ok == 0) continue;
1213
1214                 /* include particles-expand widget? */
1215                 if (first && (filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1216                         ale= make_new_animlistelem(ob, ANIMTYPE_FILLPARTD, base, ANIMTYPE_OBJECT, (ID *)ob);
1217                         if (ale) {
1218                                 BLI_addtail(anim_data, ale);
1219                                 items++;
1220                         }
1221                         first = 0;
1222                 }
1223                 
1224                 /* add particle settings? */
1225                 if (FILTER_PART_OBJC(ob) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1226                         if ((filter_mode & ANIMFILTER_CHANNELS)) {
1227                                 /* check if filtering by active status */
1228                                 if ANIMCHANNEL_ACTIVEOK(psys->part) {
1229                                         ale = make_new_animlistelem(psys->part, ANIMTYPE_DSPART, base, ANIMTYPE_OBJECT, (ID *)psys->part);
1230                                         if (ale) {
1231                                                 BLI_addtail(anim_data, ale);
1232                                                 items++;
1233                                         }
1234                                 }
1235                         }
1236                         
1237                         if (!(filter_mode & ANIMFILTER_VISIBLE) || FILTER_PART_OBJD(psys->part) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1238                                 ANIMDATA_FILTER_CASES(psys->part,
1239                                         { /* AnimData blocks - do nothing... */ },
1240                                         items += animdata_filter_nla(anim_data, ads, psys->part->adt, filter_mode, psys->part, ANIMTYPE_DSPART, (ID *)psys->part);, 
1241                                         items += animdata_filter_fcurves(anim_data, ads, psys->part->adt->drivers.first, NULL, psys->part, ANIMTYPE_DSPART, filter_mode, (ID *)psys->part);, 
1242                                         items += animdata_filter_action(anim_data, ads, psys->part->adt->action, filter_mode, psys->part, ANIMTYPE_DSPART, (ID *)psys->part);)
1243                         }
1244                 }
1245         }
1246         
1247         /* return the number of items added to the list */
1248         return items;
1249 }
1250
1251 static int animdata_filter_dopesheet_obdata (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1252 {
1253         bAnimListElem *ale=NULL;
1254         Object *ob= base->object;
1255         IdAdtTemplate *iat= ob->data;
1256         AnimData *adt= iat->adt;
1257         short type=0, expanded=0;
1258         int items= 0;
1259         
1260         /* get settings based on data type */
1261         switch (ob->type) {
1262                 case OB_CAMERA: /* ------- Camera ------------ */
1263                 {
1264                         Camera *ca= (Camera *)ob->data;
1265                         
1266                         type= ANIMTYPE_DSCAM;
1267                         expanded= FILTER_CAM_OBJD(ca);
1268                 }
1269                         break;
1270                 case OB_LAMP: /* ---------- Lamp ----------- */
1271                 {
1272                         Lamp *la= (Lamp *)ob->data;
1273                         
1274                         type= ANIMTYPE_DSLAM;
1275                         expanded= FILTER_LAM_OBJD(la);
1276                 }
1277                         break;
1278                 case OB_CURVE: /* ------- Curve ---------- */
1279                 {
1280                         Curve *cu= (Curve *)ob->data;
1281                         
1282                         type= ANIMTYPE_DSCUR;
1283                         expanded= FILTER_CUR_OBJD(cu);
1284                 }
1285                         break;
1286                 case OB_MBALL: /* ------- MetaBall ---------- */
1287                 {
1288                         MetaBall *mb= (MetaBall *)ob->data;
1289                         
1290                         type= ANIMTYPE_DSMBALL;
1291                         expanded= FILTER_MBALL_OBJD(mb);
1292                 }
1293                         break;
1294                 case OB_ARMATURE: /* ------- Armature ---------- */
1295                 {
1296                         bArmature *arm= (bArmature *)ob->data;
1297                         
1298                         type= ANIMTYPE_DSARM;
1299                         expanded= FILTER_ARM_OBJD(arm);
1300                 }
1301                         break;
1302         }
1303         
1304         /* special exception for drivers instead of action */
1305         if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
1306                 expanded= EXPANDED_DRVD(adt);
1307         
1308         /* include data-expand widget? */
1309         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {       
1310                 /* check if filtering by active status */
1311                 if ANIMCHANNEL_ACTIVEOK(iat) {
1312                         ale= make_new_animlistelem(iat, type, base, ANIMTYPE_OBJECT, (ID *)iat);
1313                         if (ale) BLI_addtail(anim_data, ale);
1314                 }
1315         }
1316         
1317         /* add object-data animation channels? */
1318         if (!(filter_mode & ANIMFILTER_VISIBLE) || (expanded) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1319                 /* filtering for channels - nla, drivers, keyframes */
1320                 ANIMDATA_FILTER_CASES(iat, 
1321                         { /* AnimData blocks - do nothing... */ },
1322                         items+= animdata_filter_nla(anim_data, ads, iat->adt, filter_mode, iat, type, (ID *)iat);,
1323                         items+= animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, iat, type, filter_mode, (ID *)iat);, 
1324                         items += animdata_filter_action(anim_data, ads, iat->adt->action, filter_mode, iat, type, (ID *)iat);)
1325         }
1326         
1327         /* return the number of items added to the list */
1328         return items;
1329 }
1330
1331 static int animdata_filter_dopesheet_ob (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1332 {
1333         bAnimListElem *ale=NULL;
1334         AnimData *adt = NULL;
1335         Object *ob= base->object;
1336         Key *key= ob_get_key(ob);
1337         short obdata_ok = 0;
1338         int items = 0;
1339         
1340         /* add this object as a channel first */
1341         if ((filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) == 0) {
1342                 /* check if filtering by selection */
1343                 if ANIMCHANNEL_SELOK((base->flag & SELECT)) {
1344                         /* check if filtering by active status */
1345                         if ANIMCHANNEL_ACTIVEOK(ob) {
1346                                 ale= make_new_animlistelem(base, ANIMTYPE_OBJECT, NULL, ANIMTYPE_NONE, (ID *)ob);
1347                                 if (ale) {
1348                                         BLI_addtail(anim_data, ale);
1349                                         items++;
1350                                 }
1351                         }
1352                 }
1353         }
1354         
1355         /* if collapsed, don't go any further (unless adding keyframes only) */
1356         if ( ((filter_mode & ANIMFILTER_VISIBLE) && EXPANDED_OBJC(ob) == 0) &&
1357                  !(filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) )
1358                 return items;
1359         
1360         /* Action, Drivers, or NLA */
1361         if (ob->adt) {
1362                 adt= ob->adt;
1363                 ANIMDATA_FILTER_CASES(ob,
1364                         { /* AnimData blocks - do nothing... */ },
1365                         { /* nla */
1366                                 /* add NLA tracks */
1367                                 items += animdata_filter_nla(anim_data, ads, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1368                         },
1369                         { /* drivers */
1370                                 /* include drivers-expand widget? */
1371                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1372                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, base, ANIMTYPE_OBJECT, (ID *)ob);
1373                                         if (ale) {
1374                                                 BLI_addtail(anim_data, ale);
1375                                                 items++;
1376                                         }
1377                                 }
1378                                 
1379                                 /* add F-Curve channels (drivers are F-Curves) */
1380                                 if (!(filter_mode & ANIMFILTER_VISIBLE) || EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1381                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1382                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, ob, ANIMTYPE_OBJECT, filter_mode, (ID *)ob);
1383                                 }
1384                         },
1385                         { /* action (keyframes) */
1386                                 /* include action-expand widget? */
1387                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1388                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, base, ANIMTYPE_OBJECT, (ID *)ob);
1389                                         if (ale) {
1390                                                 BLI_addtail(anim_data, ale);
1391                                                 items++;
1392                                         }
1393                                 }
1394                                 
1395                                 /* add F-Curve channels? */
1396                                 if (!(filter_mode & ANIMFILTER_VISIBLE) || EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1397                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1398                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob); 
1399                                 }
1400                         }
1401                 );
1402         }
1403         
1404         
1405         /* ShapeKeys? */
1406         if ((key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
1407                 adt= key->adt;
1408                 ANIMDATA_FILTER_CASES(key,
1409                         { /* AnimData blocks - do nothing... */ },
1410                         { /* nla */
1411                                 /* include shapekey-expand widget? */
1412                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1413                                         /* check if filtering by active status */
1414                                         if ANIMCHANNEL_ACTIVEOK(key) {
1415                                                 ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1416                                                 if (ale) {
1417                                                         BLI_addtail(anim_data, ale);
1418                                                         items++;
1419                                                 }
1420                                         }
1421                                 }
1422                                 
1423                                 /* add NLA tracks - only if expanded or so */
1424                                 if (!(filter_mode & ANIMFILTER_VISIBLE) || FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY))
1425                                         items += animdata_filter_nla(anim_data, ads, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1426                         },
1427                         { /* drivers */
1428                                 /* include shapekey-expand widget? */
1429                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1430                                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1431                                         if (ale) {
1432                                                 BLI_addtail(anim_data, ale);
1433                                                 items++;
1434                                         }
1435                                 }
1436                                 
1437                                 /* add channels */
1438                                 if (!(filter_mode & ANIMFILTER_VISIBLE) || FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1439                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, key, ANIMTYPE_DSSKEY, filter_mode, (ID *)key);
1440                                 }
1441                         },
1442                         { /* action (keyframes) */
1443                                 /* include shapekey-expand widget? */
1444                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1445                                         /* check if filtering by active status */
1446                                         if ANIMCHANNEL_ACTIVEOK(key) {
1447                                                 ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1448                                                 if (ale) {
1449                                                         BLI_addtail(anim_data, ale);
1450                                                         items++;
1451                                                 }
1452                                         }
1453                                 }
1454                                 
1455                                 /* add channels */
1456                                 if (!(filter_mode & ANIMFILTER_VISIBLE) || FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1457                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, key, ANIMTYPE_DSSKEY, (ID *)key); 
1458                                 }
1459                         }
1460                 );
1461         }
1462
1463         /* Materials? */
1464         if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT))
1465                 items += animdata_filter_dopesheet_mats(anim_data, ads, base, filter_mode);
1466         
1467         /* Object Data */
1468         switch (ob->type) {
1469                 case OB_CAMERA: /* ------- Camera ------------ */
1470                 {
1471                         Camera *ca= (Camera *)ob->data;
1472                         
1473                         if ((ads->filterflag & ADS_FILTER_NOCAM) == 0) {
1474                                 ANIMDATA_FILTER_CASES(ca,
1475                                         { /* AnimData blocks - do nothing... */ },
1476                                         obdata_ok= 1;,
1477                                         obdata_ok= 1;,
1478                                         obdata_ok= 1;)
1479                         }
1480                 }
1481                         break;
1482                 case OB_LAMP: /* ---------- Lamp ----------- */
1483                 {
1484                         Lamp *la= (Lamp *)ob->data;
1485                         
1486                         if ((ads->filterflag & ADS_FILTER_NOLAM) == 0) {
1487                                 ANIMDATA_FILTER_CASES(la,
1488                                         { /* AnimData blocks - do nothing... */ },
1489                                         obdata_ok= 1;,
1490                                         obdata_ok= 1;,
1491                                         obdata_ok= 1;)
1492                         }
1493                 }
1494                         break;
1495                 case OB_CURVE: /* ------- Curve ---------- */
1496                 {
1497                         Curve *cu= (Curve *)ob->data;
1498                         
1499                         if ((ads->filterflag & ADS_FILTER_NOCUR) == 0) {
1500                                 ANIMDATA_FILTER_CASES(cu,
1501                                         { /* AnimData blocks - do nothing... */ },
1502                                         obdata_ok= 1;,
1503                                         obdata_ok= 1;,
1504                                         obdata_ok= 1;)
1505                         }
1506                 }
1507                         break;
1508                 case OB_MBALL: /* ------- MetaBall ---------- */
1509                 {
1510                         MetaBall *mb= (MetaBall *)ob->data;
1511                         
1512                         if ((ads->filterflag & ADS_FILTER_NOMBA) == 0) {
1513                                 ANIMDATA_FILTER_CASES(mb,
1514                                         { /* AnimData blocks - do nothing... */ },
1515                                         obdata_ok= 1;,
1516                                         obdata_ok= 1;,
1517                                         obdata_ok= 1;)
1518                         }
1519                 }
1520                         break;
1521                 case OB_ARMATURE: /* ------- Armature ---------- */
1522                 {
1523                         bArmature *arm= (bArmature *)ob->data;
1524                         
1525                         if ((ads->filterflag & ADS_FILTER_NOARM) == 0) {
1526                                 ANIMDATA_FILTER_CASES(arm,
1527                                         { /* AnimData blocks - do nothing... */ },
1528                                         obdata_ok= 1;,
1529                                         obdata_ok= 1;,
1530                                         obdata_ok= 1;)
1531                         }
1532                 }
1533                         break;
1534         }
1535         if (obdata_ok) 
1536                 items += animdata_filter_dopesheet_obdata(anim_data, ads, base, filter_mode);
1537
1538         /* particles */
1539         if (ob->particlesystem.first && !(ads->filterflag & ADS_FILTER_NOPART))
1540                 items += animdata_filter_dopesheet_particles(anim_data, ads, base, filter_mode);
1541         
1542         /* return the number of items added to the list */
1543         return items;
1544 }       
1545
1546 static int animdata_filter_dopesheet_scene (ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
1547 {
1548         World *wo= sce->world;
1549         bNodeTree *ntree= sce->nodetree;
1550         AnimData *adt= NULL;
1551         bAnimListElem *ale;
1552         int items = 0;
1553         
1554         /* add scene as a channel first (even if we aren't showing scenes we still need to show the scene's sub-data */
1555         if ((filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) == 0) {
1556                 /* check if filtering by selection */
1557                 if (ANIMCHANNEL_SELOK( (sce->flag & SCE_DS_SELECTED) )) {
1558                         ale= make_new_animlistelem(sce, ANIMTYPE_SCENE, NULL, ANIMTYPE_NONE, NULL);
1559                         if (ale) {
1560                                 BLI_addtail(anim_data, ale);
1561                                 items++;
1562                         }
1563                 }
1564         }
1565         
1566         /* if collapsed, don't go any further (unless adding keyframes only) */
1567         if ( (EXPANDED_SCEC(sce) == 0) && !(filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) )
1568                 return items;
1569                 
1570         /* Action, Drivers, or NLA for Scene */
1571         if ((ads->filterflag & ADS_FILTER_NOSCE) == 0) {
1572                 adt= sce->adt;
1573                 ANIMDATA_FILTER_CASES(sce,
1574                         { /* AnimData blocks - do nothing... */ },
1575                         { /* nla */
1576                                 /* add NLA tracks */
1577                                 items += animdata_filter_nla(anim_data, ads, adt, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce);
1578                         },
1579                         { /* drivers */
1580                                 /* include drivers-expand widget? */
1581                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1582                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, sce, ANIMTYPE_SCENE, (ID *)sce);
1583                                         if (ale) {
1584                                                 BLI_addtail(anim_data, ale);
1585                                                 items++;
1586                                         }
1587                                 }
1588                                 
1589                                 /* add F-Curve channels (drivers are F-Curves) */
1590                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1591                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, sce, ANIMTYPE_SCENE, filter_mode, (ID *)sce);
1592                                 }
1593                         },
1594                         { /* action */
1595                                 /* include action-expand widget? */
1596                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1597                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, sce, ANIMTYPE_SCENE, (ID *)sce);
1598                                         if (ale) {
1599                                                 BLI_addtail(anim_data, ale);
1600                                                 items++;
1601                                         }
1602                                 }
1603                                 
1604                                 /* add F-Curve channels? */
1605                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1606                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce); 
1607                                 }
1608                         }
1609                 )
1610         }
1611         
1612         /* world */
1613         if ((wo && wo->adt) && !(ads->filterflag & ADS_FILTER_NOWOR)) {
1614                 /* Action, Drivers, or NLA for World */
1615                 adt= wo->adt;
1616                 ANIMDATA_FILTER_CASES(wo,
1617                         { /* AnimData blocks - do nothing... */ },
1618                         { /* nla */
1619                                 /* add NLA tracks */
1620                                 items += animdata_filter_nla(anim_data, ads, adt, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo);
1621                         },
1622                         { /* drivers */
1623                                 /* include world-expand widget? */
1624                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1625                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)wo);
1626                                         if (ale) {
1627                                                 BLI_addtail(anim_data, ale);
1628                                                 items++;
1629                                         }
1630                                 }
1631                                 
1632                                 /* add F-Curve channels (drivers are F-Curves) */
1633                                 if (FILTER_WOR_SCED(wo)/*EXPANDED_DRVD(adt)*/ || !(filter_mode & ANIMFILTER_CHANNELS)) {
1634                                         // XXX owner info is messed up now...
1635                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, wo, ANIMTYPE_DSWOR, filter_mode, (ID *)wo);
1636                                 }
1637                         },
1638                         { /* action */
1639                                 /* include world-expand widget? */
1640                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1641                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)sce);
1642                                         if (ale) {
1643                                                 BLI_addtail(anim_data, ale);
1644                                                 items++;
1645                                         }
1646                                 }
1647                                 
1648                                 /* add channels */
1649                                 if (FILTER_WOR_SCED(wo) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1650                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo); 
1651                                 }
1652                         }
1653                 )
1654         }
1655         /* nodetree */
1656         if ((ntree && ntree->adt) && !(ads->filterflag & ADS_FILTER_NONTREE)) {
1657                 /* Action, Drivers, or NLA for Nodetree */
1658                 adt= ntree->adt;
1659                 ANIMDATA_FILTER_CASES(ntree,
1660                         { /* AnimData blocks - do nothing... */ },
1661                         { /* nla */
1662                                 /* add NLA tracks */
1663                                 items += animdata_filter_nla(anim_data, ads, adt, filter_mode, ntree, ANIMTYPE_DSNTREE, (ID *)ntree);
1664                         },
1665                         { /* drivers */
1666                                 /* include nodetree-expand widget? */
1667                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1668                                         ale= make_new_animlistelem(ntree, ANIMTYPE_DSNTREE, sce, ANIMTYPE_SCENE, (ID *)ntree);
1669                                         if (ale) {
1670                                                 BLI_addtail(anim_data, ale);
1671                                                 items++;
1672                                         }
1673                                 }
1674                                 
1675                                 /* add F-Curve channels (drivers are F-Curves) */
1676                                 if (FILTER_NTREE_SCED(ntree)/*EXPANDED_DRVD(adt)*/ || !(filter_mode & ANIMFILTER_CHANNELS)) {
1677                                         // XXX owner info is messed up now...
1678                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, ntree, ANIMTYPE_DSNTREE, filter_mode, (ID *)ntree);
1679                                 }
1680                         },
1681                         { /* action */
1682                                 /* include nodetree-expand widget? */
1683                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1684                                         ale= make_new_animlistelem(ntree, ANIMTYPE_DSNTREE, sce, ANIMTYPE_SCENE, (ID *)sce);
1685                                         if (ale) {
1686                                                 BLI_addtail(anim_data, ale);
1687                                                 items++;
1688                                         }
1689                                 }
1690                                 
1691                                 /* add channels */
1692                                 if (FILTER_NTREE_SCED(ntree) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1693                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, ntree, ANIMTYPE_DSNTREE, (ID *)ntree); 
1694                                 }
1695                         }
1696                 )
1697         }
1698
1699         
1700         // TODO: scene compositing nodes (these aren't standard node-trees)
1701         
1702         /* return the number of items added to the list */
1703         return items;
1704 }
1705
1706 // 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)
1707 static int animdata_filter_dopesheet (ListBase *anim_data, bAnimContext *ac, bDopeSheet *ads, int filter_mode)
1708 {
1709         Scene *sce= (Scene *)ads->source;
1710         Base *base;
1711         bAnimListElem *ale;
1712         int items = 0;
1713         
1714         /* check that we do indeed have a scene */
1715         if ((ads->source == NULL) || (GS(ads->source->name)!=ID_SCE)) {
1716                 printf("DopeSheet Error: Not scene!\n");
1717                 if (G.f & G_DEBUG)
1718                         printf("\tPointer = %p, Name = '%s' \n", ads->source, (ads->source)?ads->source->name:NULL);
1719                 return 0;
1720         }
1721         
1722         /* scene-linked animation */
1723         // TODO: sequencer, composite nodes - are we to include those here too?
1724         {
1725                 short sceOk= 0, worOk= 0, nodeOk=0;
1726                 
1727                 /* check filtering-flags if ok */
1728                 ANIMDATA_FILTER_CASES(sce, 
1729                         {
1730                                 /* for the special AnimData blocks only case, we only need to add
1731                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1732                                  */
1733                                 ANIMDATA_ADD_ANIMDATA(sce);
1734                                 sceOk=0;
1735                         },
1736                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);, 
1737                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);, 
1738                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);)
1739                 if (sce->world) {
1740                         ANIMDATA_FILTER_CASES(sce->world, 
1741                                 {
1742                                         /* for the special AnimData blocks only case, we only need to add
1743                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1744                                          */
1745                                         ANIMDATA_ADD_ANIMDATA(sce->world);
1746                                         worOk=0;
1747                                 },
1748                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);, 
1749                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);, 
1750                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);)
1751                 }
1752                 if (sce->nodetree) {
1753                         ANIMDATA_FILTER_CASES(sce->nodetree, 
1754                                 {
1755                                         /* for the special AnimData blocks only case, we only need to add
1756                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1757                                          */
1758                                         ANIMDATA_ADD_ANIMDATA(sce->nodetree);
1759                                         nodeOk=0;
1760                                 },
1761                                 nodeOk= !(ads->filterflag & ADS_FILTER_NONTREE);, 
1762                                 nodeOk= !(ads->filterflag & ADS_FILTER_NONTREE);, 
1763                                 nodeOk= !(ads->filterflag & ADS_FILTER_NONTREE);)
1764                 }
1765                 
1766                 /* if only F-Curves with visible flags set can be shown, check that 
1767                  * datablocks haven't been set to invisible 
1768                  */
1769                 if (filter_mode & ANIMFILTER_CURVEVISIBLE) {
1770                         if ((sce->adt) && (sce->adt->flag & ADT_CURVES_NOT_VISIBLE))
1771                                 sceOk= worOk= nodeOk= 0;
1772                 }
1773                 
1774                 /* check if not all bad (i.e. so there is something to show) */
1775                 if ( !(!sceOk && !worOk && !nodeOk) ) {
1776                         /* add scene data to the list of filtered channels */
1777                         items += animdata_filter_dopesheet_scene(anim_data, ads, sce, filter_mode);
1778                 }
1779         }
1780         
1781         
1782         /* loop over all bases in the scene */
1783         for (base= sce->base.first; base; base= base->next) {
1784                 /* check if there's an object (all the relevant checks are done in the ob-function) */
1785                 if (base->object) {
1786                         Object *ob= base->object;
1787                         Key *key= ob_get_key(ob);
1788                         short actOk=1, keyOk=1, dataOk=1, matOk=1, partOk=1;
1789                         
1790                         /* firstly, check if object can be included, by the following factors:
1791                          *      - if only visible, must check for layer and also viewport visibility
1792                          *      - if only selected, must check if object is selected 
1793                          *      - there must be animation data to edit
1794                          */
1795                         // TODO: if cache is implemented, just check name here, and then 
1796                         if (filter_mode & ANIMFILTER_VISIBLE) {
1797                                 /* layer visibility - we check both object and base, since these may not be in sync yet */
1798                                 if ((sce->lay & (ob->lay|base->lay))==0) continue;
1799                                 
1800                                 /* outliner restrict-flag */
1801                                 if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
1802                         }
1803                         
1804                         /* if only F-Curves with visible flags set can be shown, check that 
1805                          * datablock hasn't been set to invisible 
1806                          */
1807                         if (filter_mode & ANIMFILTER_CURVEVISIBLE) {
1808                                 if ((ob->adt) && (ob->adt->flag & ADT_CURVES_NOT_VISIBLE))
1809                                         continue;
1810                         }
1811                         
1812                         /* additionally, dopesheet filtering also affects what objects to consider */
1813                         if (ads->filterflag) {
1814                                 /* check selection and object type filters */
1815                                 if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) /*|| (base == sce->basact)*/) )  {
1816                                         /* only selected should be shown */
1817                                         continue;
1818                                 }
1819                                 
1820                                 /* check filters for datatypes */
1821                                         /* object */
1822                                 actOk= 0;
1823                                 keyOk= 0;
1824                                 ANIMDATA_FILTER_CASES(ob, 
1825                                         {
1826                                                 /* for the special AnimData blocks only case, we only need to add
1827                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1828                                                  */
1829                                                 ANIMDATA_ADD_ANIMDATA(ob);
1830                                                 actOk=0;
1831                                         },
1832                                         actOk= 1;, 
1833                                         actOk= 1;, 
1834                                         actOk= 1;)
1835                                 if (key) {
1836                                         /* shapekeys */
1837                                         ANIMDATA_FILTER_CASES(key, 
1838                                                 {
1839                                                         /* for the special AnimData blocks only case, we only need to add
1840                                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1841                                                          */
1842                                                         ANIMDATA_ADD_ANIMDATA(key);
1843                                                         keyOk=0;
1844                                                 },
1845                                                 keyOk= 1;, 
1846                                                 keyOk= 1;, 
1847                                                 keyOk= 1;)
1848                                 }
1849                                 
1850                                 /* materials - only for geometric types */
1851                                 matOk= 0; /* by default, not ok... */
1852                                 if ( !(ads->filterflag & ADS_FILTER_NOMAT) && (ob->totcol) && 
1853                                          ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) ) 
1854                                 {
1855                                         int a;
1856                                         
1857                                         /* firstly check that we actuallly have some materials */
1858                                         for (a=0; a < ob->totcol; a++) {
1859                                                 Material *ma= give_current_material(ob, a);
1860                                                 
1861                                                 if (ma) {
1862                                                         /* if material has relevant animation data, break */
1863                                                         ANIMDATA_FILTER_CASES(ma, 
1864                                                                 {
1865                                                                         /* for the special AnimData blocks only case, we only need to add
1866                                                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1867                                                                          */
1868                                                                         ANIMDATA_ADD_ANIMDATA(ma);
1869                                                                         matOk=0;
1870                                                                 },
1871                                                                 matOk= 1;, 
1872                                                                 matOk= 1;, 
1873                                                                 matOk= 1;)
1874                                                 }
1875                                                         
1876                                                 if (matOk) 
1877                                                         break;
1878                                         }
1879                                 }
1880                                 
1881                                 /* data */
1882                                 switch (ob->type) {
1883                                         case OB_CAMERA: /* ------- Camera ------------ */
1884                                         {
1885                                                 Camera *ca= (Camera *)ob->data;
1886                                                 dataOk= 0;
1887                                                 ANIMDATA_FILTER_CASES(ca, 
1888                                                         if ((ads->filterflag & ADS_FILTER_NOCAM)==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(ca);
1893                                                                 dataOk=0;
1894                                                         },
1895                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);, 
1896                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);, 
1897                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);)
1898                                         }
1899                                                 break;
1900                                         case OB_LAMP: /* ---------- Lamp ----------- */
1901                                         {
1902                                                 Lamp *la= (Lamp *)ob->data;
1903                                                 dataOk= 0;
1904                                                 ANIMDATA_FILTER_CASES(la, 
1905                                                         if ((ads->filterflag & ADS_FILTER_NOLAM)==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(la);
1910                                                                 dataOk=0;
1911                                                         },
1912                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);, 
1913                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);, 
1914                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);)
1915                                         }
1916                                                 break;
1917                                         case OB_CURVE: /* ------- Curve ---------- */
1918                                         {
1919                                                 Curve *cu= (Curve *)ob->data;
1920                                                 dataOk= 0;
1921                                                 ANIMDATA_FILTER_CASES(cu, 
1922                                                         if ((ads->filterflag & ADS_FILTER_NOCUR)==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(cu);
1927                                                                 dataOk=0;
1928                                                         },
1929                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);, 
1930                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);, 
1931                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);)
1932                                         }
1933                                                 break;
1934                                         case OB_MBALL: /* ------- MetaBall ---------- */
1935                                         {
1936                                                 MetaBall *mb= (MetaBall *)ob->data;
1937                                                 dataOk= 0;
1938                                                 ANIMDATA_FILTER_CASES(mb, 
1939                                                         if ((ads->filterflag & ADS_FILTER_NOMBA)==0) {
1940                                                                 /* for the special AnimData blocks only case, we only need to add
1941                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1942                                                                  */
1943                                                                 ANIMDATA_ADD_ANIMDATA(mb);
1944                                                                 dataOk=0;
1945                                                         },
1946                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOMBA);, 
1947                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOMBA);, 
1948                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOMBA);)
1949                                         }
1950                                                 break;
1951                                         case OB_ARMATURE: /* ------- Armature ---------- */
1952                                         {
1953                                                 bArmature *arm= (bArmature *)ob->data;
1954                                                 dataOk= 0;
1955                                                 ANIMDATA_FILTER_CASES(arm, 
1956                                                         if ((ads->filterflag & ADS_FILTER_NOARM)==0) {
1957                                                                 /* for the special AnimData blocks only case, we only need to add
1958                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1959                                                                  */
1960                                                                 ANIMDATA_ADD_ANIMDATA(arm);
1961                                                                 dataOk=0;
1962                                                         },
1963                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOARM);, 
1964                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOARM);, 
1965                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOARM);)
1966                                         }
1967                                                 break;
1968                                         default: /* --- other --- */
1969                                                 dataOk= 0;
1970                                                 break;
1971                                 }
1972                                 
1973                                 /* particles */
1974                                 partOk = 0;
1975                                 if (!(ads->filterflag & ADS_FILTER_NOPART) && ob->particlesystem.first) {
1976                                         ParticleSystem *psys = ob->particlesystem.first;
1977                                         for(; psys; psys=psys->next) {
1978                                                 if (psys->part) {
1979                                                         /* if particlesettings has relevant animation data, break */
1980                                                         ANIMDATA_FILTER_CASES(psys->part, 
1981                                                                 {
1982                                                                         /* for the special AnimData blocks only case, we only need to add
1983                                                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1984                                                                          */
1985                                                                         ANIMDATA_ADD_ANIMDATA(psys->part);
1986                                                                         partOk=0;
1987                                                                 },
1988                                                                 partOk= 1;, 
1989                                                                 partOk= 1;, 
1990                                                                 partOk= 1;)
1991                                                 }
1992                                                         
1993                                                 if (partOk) 
1994                                                         break;
1995                                         }
1996                                 }
1997                                 
1998                                 /* check if all bad (i.e. nothing to show) */
1999                                 if (!actOk && !keyOk && !dataOk && !matOk && !partOk)
2000                                         continue;
2001                         }
2002                         else {
2003                                 /* check data-types */
2004                                 actOk= ANIMDATA_HAS_KEYS(ob);
2005                                 keyOk= (key != NULL);
2006                                 
2007                                 /* materials - only for geometric types */
2008                                 matOk= 0; /* by default, not ok... */
2009                                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) && (ob->totcol)) 
2010                                 {
2011                                         int a;
2012                                         
2013                                         /* firstly check that we actuallly have some materials */
2014                                         for (a=0; a < ob->totcol; a++) {
2015                                                 Material *ma= give_current_material(ob, a);
2016                                                 
2017                                                 if ((ma) && ANIMDATA_HAS_KEYS(ma)) {
2018                                                         matOk= 1;
2019                                                         break;
2020                                                 }
2021                                         }
2022                                 }
2023                                 
2024                                 /* data */
2025                                 switch (ob->type) {
2026                                         case OB_CAMERA: /* ------- Camera ------------ */
2027                                         {
2028                                                 Camera *ca= (Camera *)ob->data;
2029                                                 dataOk= ANIMDATA_HAS_KEYS(ca);                                          
2030                                         }
2031                                                 break;
2032                                         case OB_LAMP: /* ---------- Lamp ----------- */
2033                                         {
2034                                                 Lamp *la= (Lamp *)ob->data;
2035                                                 dataOk= ANIMDATA_HAS_KEYS(la);  
2036                                         }
2037                                                 break;
2038                                         case OB_CURVE: /* -------- Curve ---------- */
2039                                         {
2040                                                 Curve *cu= (Curve *)ob->data;
2041                                                 dataOk= ANIMDATA_HAS_KEYS(cu);  
2042                                         }
2043                                                 break;
2044                                         case OB_MBALL: /* -------- Metas ---------- */
2045                                         {
2046                                                 MetaBall *mb= (MetaBall *)ob->data;
2047                                                 dataOk= ANIMDATA_HAS_KEYS(mb);  
2048                                         }
2049                                                 break;
2050                                         case OB_ARMATURE: /* -------- Armature ---------- */
2051                                         {
2052                                                 bArmature *arm= (bArmature *)ob->data;
2053                                                 dataOk= ANIMDATA_HAS_KEYS(arm); 
2054                                         }
2055                                                 break;
2056                                         default: /* --- other --- */
2057                                                 dataOk= 0;
2058                                                 break;
2059                                 }
2060                                 
2061                                 /* particles */
2062                                 partOk = 0;
2063                                 if (ob->particlesystem.first) {
2064                                         ParticleSystem *psys = ob->particlesystem.first;
2065                                         for(; psys; psys=psys->next) {
2066                                                 if(psys->part && ANIMDATA_HAS_KEYS(psys->part)) {
2067                                                         partOk = 1;
2068                                                         break;
2069                                                 }
2070                                         }
2071                                 }
2072                                 
2073                                 /* check if all bad (i.e. nothing to show) */
2074                                 if (!actOk && !keyOk && !dataOk && !matOk && !partOk)
2075                                         continue;
2076                         }
2077                         
2078                         /* since we're still here, this object should be usable */
2079                         items += animdata_filter_dopesheet_ob(anim_data, ads, base, filter_mode);
2080                 }
2081         }
2082         
2083         /* return the number of items in the list */
2084         return items;
2085 }
2086
2087 /* Summary track for DopeSheet/Action Editor 
2088  *      - return code is whether the summary lets the other channels get drawn
2089  */
2090 static short animdata_filter_dopesheet_summary (bAnimContext *ac, ListBase *anim_data, int filter_mode, int *items)
2091 {
2092         bDopeSheet *ads = NULL;
2093         
2094         /* get the DopeSheet information to use 
2095          *      - we should only need to deal with the DopeSheet/Action Editor, 
2096          *        since all the other Animation Editors won't have this concept
2097          *        being applicable.
2098          */
2099         if ((ac && ac->sa) && (ac->sa->spacetype == SPACE_ACTION)) {
2100                 SpaceAction *saction= (SpaceAction *)ac->sa->spacedata.first;
2101                 ads= &saction->ads;
2102         }
2103         else {
2104                 /* invalid space type - skip this summary channels */
2105                 return 1;
2106         }
2107         
2108         /* dopesheet summary 
2109          *      - only for drawing and/or selecting keyframes in channels, but not for real editing 
2110          *      - only useful for DopeSheet Editor, where the summary is useful
2111          */
2112         // TODO: we should really check if some other prohibited filters are also active, but that can be for later
2113         if ((filter_mode & ANIMFILTER_CHANNELS) && (ads->filterflag & ADS_FILTER_SUMMARY)) {
2114                 bAnimListElem *ale= make_new_animlistelem(ac, ANIMTYPE_SUMMARY, NULL, ANIMTYPE_NONE, NULL);
2115                 if (ale) {
2116                         BLI_addtail(anim_data, ale);
2117                         (*items)++;
2118                 }
2119                 
2120                 /* if summary is collapsed, don't show other channels beneath this 
2121                  *      - this check is put inside the summary check so that it doesn't interfere with normal operation
2122                  */ 
2123                 if (ads->flag & ADS_FLAG_SUMMARY_COLLAPSED)
2124                         return 0;
2125         }
2126         
2127         /* the other channels beneath this can be shown */
2128         return 1;
2129 }  
2130
2131 /* ----------- Public API --------------- */
2132
2133 /* This function filters the active data source to leave only animation channels suitable for
2134  * usage by the caller. It will return the length of the list 
2135  * 
2136  *      *anim_data: is a pointer to a ListBase, to which the filtered animation channels
2137  *              will be placed for use.
2138  *      filter_mode: how should the data be filtered - bitmapping accessed flags
2139  */
2140 int ANIM_animdata_filter (bAnimContext *ac, ListBase *anim_data, int filter_mode, void *data, short datatype)
2141 {
2142         int items = 0;
2143         
2144         /* only filter data if there's somewhere to put it */
2145         if (data && anim_data) {
2146                 bAnimListElem *ale, *next;
2147                 Object *obact= (ac) ? ac->obact : NULL;
2148                 
2149                 /* firstly filter the data */
2150                 switch (datatype) {
2151                         case ANIMCONT_ACTION:   /* 'Action Editor' */
2152                         {
2153                                 /* the check for the DopeSheet summary is included here since the summary works here too */
2154                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2155                                         items += animdata_filter_action(anim_data, NULL, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
2156                         }
2157                                 break;
2158                                 
2159                         case ANIMCONT_SHAPEKEY: /* 'ShapeKey Editor' */
2160                         {
2161                                 /* the check for the DopeSheet summary is included here since the summary works here too */
2162                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2163                                         items= animdata_filter_shapekey(anim_data, data, filter_mode);
2164                         }
2165                                 break;
2166                                 
2167                         case ANIMCONT_GPENCIL:
2168                         {
2169                                 //items= animdata_filter_gpencil(anim_data, data, filter_mode);
2170                         }
2171                                 break;
2172                                 
2173                         case ANIMCONT_DOPESHEET: /* 'DopeSheet Editor' */
2174                         {
2175                                 /* the DopeSheet editor is the primary place where the DopeSheet summaries are useful */
2176                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2177                                         items += animdata_filter_dopesheet(anim_data, ac, data, filter_mode);
2178                         }
2179                                 break;
2180                                 
2181                         case ANIMCONT_FCURVES: /* Graph Editor -> FCurves/Animation Editing */
2182                         case ANIMCONT_DRIVERS: /* Graph Editor -> Drivers Editing */
2183                         case ANIMCONT_NLA: /* NLA Editor */
2184                         {
2185                                 /* all of these editors use the basic DopeSheet data for filtering options, but don't have all the same features */
2186                                 items = animdata_filter_dopesheet(anim_data, ac, data, filter_mode);
2187                         }
2188                                 break;
2189                 }
2190                         
2191                 /* remove any weedy entries */
2192                 // XXX this is weedy code!
2193                 for (ale= anim_data->first; ale; ale= next) {
2194                         next= ale->next;
2195                         
2196                         if (ale->type == ANIMTYPE_NONE) {
2197                                 items--;
2198                                 BLI_freelinkN(anim_data, ale);
2199                         }
2200                 }
2201         }
2202         
2203         /* return the number of items in the list */
2204         return items;
2205 }
2206
2207 /* ************************************************************ */