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