ShapeKey Editor (sub-mode of DopeSheet Editor)
[blender.git] / source / blender / editors / animation / anim_filter.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation, Joshua Leung
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Joshua Leung (original author)
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /* This file contains a system used to provide a layer of abstraction between sources
30  * of animation data and tools in Animation Editors. The method used here involves 
31  * generating a list of edit structures which enable tools to naively perform the actions 
32  * they require without all the boiler-plate associated with loops within loops and checking 
33  * for cases to ignore. 
34  *
35  * While this is primarily used for the Action/Dopesheet Editor (and its accessory modes),
36  * the Graph Editor also uses this for its channel list and for determining which curves
37  * are being edited. Likewise, the NLA Editor also uses this for its channel list and in
38  * its operators.
39  *
40  * Note: much of the original system this was based on was built before the creation of the RNA
41  * system. In future, it would be interesting to replace some parts of this code with RNA queries,
42  * however, RNA does not eliminate some of the boiler-plate reduction benefits presented by this 
43  * system, so if any such work does occur, it should only be used for the internals used here...
44  *
45  * -- Joshua Leung, Dec 2008 (Last revision July 2009)
46  */
47
48 #include <string.h>
49 #include <stdio.h>
50
51 #include "DNA_listBase.h"
52 #include "DNA_ID.h"
53 #include "DNA_anim_types.h"
54 #include "DNA_action_types.h"
55 #include "DNA_armature_types.h"
56 #include "DNA_constraint_types.h"
57 #include "DNA_camera_types.h"
58 #include "DNA_curve_types.h"
59 #include "DNA_gpencil_types.h"
60 #include "DNA_ipo_types.h"
61 #include "DNA_lamp_types.h"
62 #include "DNA_lattice_types.h"
63 #include "DNA_key_types.h"
64 #include "DNA_material_types.h"
65 #include "DNA_mesh_types.h"
66 #include "DNA_meta_types.h"
67 #include "DNA_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         int items = 0;
930         
931         /* check if channels or only F-Curves */
932         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {
933                 bAnimListElem *ale;
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 && key->adt->action)
963                         items= animdata_filter_action(anim_data, NULL, key->adt->action, filter_mode, NULL, ANIMTYPE_NONE, (ID *)key);
964         }
965         
966         /* return the number of items added to the list */
967         return items;
968 }
969
970 #if 0
971 // FIXME: switch this to use the bDopeSheet...
972 static int animdata_filter_gpencil (ListBase *anim_data, bScreen *sc, int filter_mode)
973 {
974         bAnimListElem *ale;
975         ScrArea *sa, *curarea;
976         bGPdata *gpd;
977         bGPDlayer *gpl;
978         int items = 0;
979         
980         /* check if filtering types are appropriate */
981         {
982                 /* special hack for fullscreen area (which must be this one then):
983                  *      - we use the curarea->full as screen to get spaces from, since the
984                  *        old (pre-fullscreen) screen was stored there...
985                  *      - this is needed as all data would otherwise disappear
986                  */
987                 // XXX need to get new alternative for curarea
988                 if ((curarea->full) && (curarea->spacetype==SPACE_ACTION))
989                         sc= curarea->full;
990                 
991                 /* loop over spaces in current screen, finding gpd blocks (could be slow!) */
992                 for (sa= sc->areabase.first; sa; sa= sa->next) {
993                         /* try to get gp data */
994                         // XXX need to put back grease pencil api...
995                         gpd= gpencil_data_get_active(sa);
996                         if (gpd == NULL) continue;
997                         
998                         /* add gpd as channel too (if for drawing, and it has layers) */
999                         if ((filter_mode & ANIMFILTER_CHANNELS) && (gpd->layers.first)) {
1000                                 /* add to list */
1001                                 ale= make_new_animlistelem(gpd, ANIMTYPE_GPDATABLOCK, sa, ANIMTYPE_SPECIALDATA);
1002                                 if (ale) {
1003                                         BLI_addtail(anim_data, ale);
1004                                         items++;
1005                                 }
1006                         }
1007                         
1008                         /* only add layers if they will be visible (if drawing channels) */
1009                         if ( !(filter_mode & ANIMFILTER_VISIBLE) || (EXPANDED_GPD(gpd)) ) {
1010                                 /* loop over layers as the conditions are acceptable */
1011                                 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
1012                                         /* only if selected */
1013                                         if ( ANIMCHANNEL_SELOK(SEL_GPL(gpl)) ) {
1014                                                 /* only if editable */
1015                                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
1016                                                         /* add to list */
1017                                                         ale= make_new_animlistelem(gpl, ANIMTYPE_GPLAYER, gpd, ANIMTYPE_GPDATABLOCK);
1018                                                         if (ale) {
1019                                                                 BLI_addtail(anim_data, ale);
1020                                                                 items++;
1021                                                         }
1022                                                 }
1023                                         }
1024                                 }
1025                         }
1026                 }
1027         }
1028         
1029         /* return the number of items added to the list */
1030         return items;
1031 }
1032 #endif 
1033
1034
1035 static int animdata_filter_dopesheet_mats (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1036 {
1037         ListBase mats = {NULL, NULL};
1038         LinkData *ld;
1039         
1040         bAnimListElem *ale=NULL;
1041         Object *ob= base->object;
1042         int items=0, a=0;
1043         
1044         /* firstly check that we actuallly have some materials, by gathering all materials in a temp list */
1045         for (a=0; a < ob->totcol; a++) {
1046                 Material *ma= give_current_material(ob, a);
1047                 short ok = 0;
1048                 
1049                 /* for now, if no material returned, skip (this shouldn't confuse the user I hope) */
1050                 if (ELEM(NULL, ma, ma->adt)) 
1051                         continue;
1052                 
1053                 /* check if ok */
1054                 ANIMDATA_FILTER_CASES(ma, 
1055                         { /* AnimData blocks - do nothing... */ },
1056                         ok=1;, 
1057                         ok=1;, 
1058                         ok=1;)
1059                 if (ok == 0) continue;
1060                 
1061                 /* make a temp list elem for this */
1062                 ld= MEM_callocN(sizeof(LinkData), "DopeSheet-MaterialCache");
1063                 ld->data= ma;
1064                 BLI_addtail(&mats, ld);
1065         }
1066         
1067         /* if there were no channels found, no need to carry on */
1068         if (mats.first == NULL)
1069                 return 0;
1070         
1071         /* include materials-expand widget? */
1072         if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1073                 ale= make_new_animlistelem(ob, ANIMTYPE_FILLMATD, base, ANIMTYPE_OBJECT, (ID *)ob);
1074                 if (ale) {
1075                         BLI_addtail(anim_data, ale);
1076                         items++;
1077                 }
1078         }
1079         
1080         /* add materials? */
1081         if (FILTER_MAT_OBJC(ob) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1082                 /* for each material in cache, add channels  */
1083                 for (ld= mats.first; ld; ld= ld->next) {
1084                         Material *ma= (Material *)ld->data;
1085                         
1086                         /* include material-expand widget? */
1087                         // hmm... do we need to store the index of this material in the array anywhere?
1088                         if (filter_mode & ANIMFILTER_CHANNELS) {
1089                                 /* check if filtering by active status */
1090                                 if ANIMCHANNEL_ACTIVEOK(ma) {
1091                                         ale= make_new_animlistelem(ma, ANIMTYPE_DSMAT, base, ANIMTYPE_OBJECT, (ID *)ma);
1092                                         if (ale) {
1093                                                 BLI_addtail(anim_data, ale);
1094                                                 items++;
1095                                         }
1096                                 }
1097                         }
1098                         
1099                         /* add material's animation data */
1100                         if (FILTER_MAT_OBJD(ma) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1101                                 ANIMDATA_FILTER_CASES(ma, 
1102                                         { /* AnimData blocks - do nothing... */ },
1103                                         items += animdata_filter_nla(anim_data, ads, ma->adt, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);, 
1104                                         items += animdata_filter_fcurves(anim_data, ads, ma->adt->drivers.first, NULL, ma, ANIMTYPE_DSMAT, filter_mode, (ID *)ma);, 
1105                                         items += animdata_filter_action(anim_data, ads, ma->adt->action, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);)
1106                         }
1107                 }
1108         }
1109         
1110         /* free cache */
1111         BLI_freelistN(&mats);
1112         
1113         /* return the number of items added to the list */
1114         return items;
1115 }
1116
1117 static int animdata_filter_dopesheet_particles (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1118 {
1119         bAnimListElem *ale=NULL;
1120         Object *ob= base->object;
1121         ParticleSystem *psys = ob->particlesystem.first;
1122         int items= 0, first = 1;
1123
1124         for(; psys; psys=psys->next) {
1125                 short ok = 0;
1126
1127                 if(ELEM(NULL, psys->part, psys->part->adt))
1128                         continue;
1129
1130                 ANIMDATA_FILTER_CASES(psys->part,
1131                         { /* AnimData blocks - do nothing... */ },
1132                         ok=1;, 
1133                         ok=1;, 
1134                         ok=1;)
1135                 if (ok == 0) continue;
1136
1137                 /* include particles-expand widget? */
1138                 if (first && (filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1139                         ale= make_new_animlistelem(ob, ANIMTYPE_FILLPARTD, base, ANIMTYPE_OBJECT, (ID *)ob);
1140                         if (ale) {
1141                                 BLI_addtail(anim_data, ale);
1142                                 items++;
1143                         }
1144                         first = 0;
1145                 }
1146                 
1147                 /* add particle settings? */
1148                 if (FILTER_PART_OBJC(ob) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1149                         if ((filter_mode & ANIMFILTER_CHANNELS)) {
1150                                 /* check if filtering by active status */
1151                                 if ANIMCHANNEL_ACTIVEOK(psys->part) {
1152                                         ale = make_new_animlistelem(psys->part, ANIMTYPE_DSPART, base, ANIMTYPE_OBJECT, (ID *)psys->part);
1153                                         if (ale) {
1154                                                 BLI_addtail(anim_data, ale);
1155                                                 items++;
1156                                         }
1157                                 }
1158                         }
1159                         
1160                         if (FILTER_PART_OBJD(psys->part) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1161                                 ANIMDATA_FILTER_CASES(psys->part,
1162                                         { /* AnimData blocks - do nothing... */ },
1163                                         items += animdata_filter_nla(anim_data, ads, psys->part->adt, filter_mode, psys->part, ANIMTYPE_DSPART, (ID *)psys->part);, 
1164                                         items += animdata_filter_fcurves(anim_data, ads, psys->part->adt->drivers.first, NULL, psys->part, ANIMTYPE_DSPART, filter_mode, (ID *)psys->part);, 
1165                                         items += animdata_filter_action(anim_data, ads, psys->part->adt->action, filter_mode, psys->part, ANIMTYPE_DSPART, (ID *)psys->part);)
1166                         }
1167                 }
1168         }
1169         
1170         /* return the number of items added to the list */
1171         return items;
1172 }
1173
1174 static int animdata_filter_dopesheet_obdata (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1175 {
1176         bAnimListElem *ale=NULL;
1177         Object *ob= base->object;
1178         IdAdtTemplate *iat= ob->data;
1179         AnimData *adt= iat->adt;
1180         short type=0, expanded=0;
1181         int items= 0;
1182         
1183         /* get settings based on data type */
1184         switch (ob->type) {
1185                 case OB_CAMERA: /* ------- Camera ------------ */
1186                 {
1187                         Camera *ca= (Camera *)ob->data;
1188                         
1189                         type= ANIMTYPE_DSCAM;
1190                         expanded= FILTER_CAM_OBJD(ca);
1191                 }
1192                         break;
1193                 case OB_LAMP: /* ---------- Lamp ----------- */
1194                 {
1195                         Lamp *la= (Lamp *)ob->data;
1196                         
1197                         type= ANIMTYPE_DSLAM;
1198                         expanded= FILTER_LAM_OBJD(la);
1199                 }
1200                         break;
1201                 case OB_CURVE: /* ------- Curve ---------- */
1202                 {
1203                         Curve *cu= (Curve *)ob->data;
1204                         
1205                         type= ANIMTYPE_DSCUR;
1206                         expanded= FILTER_CUR_OBJD(cu);
1207                 }
1208                         break;
1209                 case OB_MBALL: /* ------- MetaBall ---------- */
1210                 {
1211                         MetaBall *mb= (MetaBall *)ob->data;
1212                         
1213                         type= ANIMTYPE_DSMBALL;
1214                         expanded= FILTER_MBALL_OBJD(mb);
1215                 }
1216                         break;
1217                 case OB_ARMATURE: /* ------- Armature ---------- */
1218                 {
1219                         bArmature *arm= (bArmature *)ob->data;
1220                         
1221                         type= ANIMTYPE_DSARM;
1222                         expanded= FILTER_ARM_OBJD(arm);
1223                 }
1224                         break;
1225         }
1226         
1227         /* special exception for drivers instead of action */
1228         if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
1229                 expanded= EXPANDED_DRVD(adt);
1230         
1231         /* include data-expand widget? */
1232         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {       
1233                 /* check if filtering by active status */
1234                 if ANIMCHANNEL_ACTIVEOK(iat) {
1235                         ale= make_new_animlistelem(iat, type, base, ANIMTYPE_OBJECT, (ID *)iat);
1236                         if (ale) BLI_addtail(anim_data, ale);
1237                 }
1238         }
1239         
1240         /* add object-data animation channels? */
1241         if ((expanded) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1242                 /* filtering for channels - nla, drivers, keyframes */
1243                 ANIMDATA_FILTER_CASES(iat, 
1244                         { /* AnimData blocks - do nothing... */ },
1245                         items+= animdata_filter_nla(anim_data, ads, iat->adt, filter_mode, iat, type, (ID *)iat);,
1246                         items+= animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, iat, type, filter_mode, (ID *)iat);, 
1247                         items += animdata_filter_action(anim_data, ads, iat->adt->action, filter_mode, iat, type, (ID *)iat);)
1248         }
1249         
1250         /* return the number of items added to the list */
1251         return items;
1252 }
1253
1254 static int animdata_filter_dopesheet_ob (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1255 {
1256         bAnimListElem *ale=NULL;
1257         AnimData *adt = NULL;
1258         Object *ob= base->object;
1259         Key *key= ob_get_key(ob);
1260         short obdata_ok = 0;
1261         int items = 0;
1262         
1263         /* add this object as a channel first */
1264         if ((filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) == 0) {
1265                 /* check if filtering by selection */
1266                 if ANIMCHANNEL_SELOK((base->flag & SELECT)) {
1267                         /* check if filtering by active status */
1268                         if ANIMCHANNEL_ACTIVEOK(ob) {
1269                                 ale= make_new_animlistelem(base, ANIMTYPE_OBJECT, NULL, ANIMTYPE_NONE, (ID *)ob);
1270                                 if (ale) {
1271                                         BLI_addtail(anim_data, ale);
1272                                         items++;
1273                                 }
1274                         }
1275                 }
1276         }
1277         
1278         /* if collapsed, don't go any further (unless adding keyframes only) */
1279         if ( (EXPANDED_OBJC(ob) == 0) && !(filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) )
1280                 return items;
1281         
1282         /* Action, Drivers, or NLA */
1283         if (ob->adt) {
1284                 adt= ob->adt;
1285                 ANIMDATA_FILTER_CASES(ob,
1286                         { /* AnimData blocks - do nothing... */ },
1287                         { /* nla */
1288                                 /* add NLA tracks */
1289                                 items += animdata_filter_nla(anim_data, ads, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1290                         },
1291                         { /* drivers */
1292                                 /* include drivers-expand widget? */
1293                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1294                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, base, ANIMTYPE_OBJECT, (ID *)ob);
1295                                         if (ale) {
1296                                                 BLI_addtail(anim_data, ale);
1297                                                 items++;
1298                                         }
1299                                 }
1300                                 
1301                                 /* add F-Curve channels (drivers are F-Curves) */
1302                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1303                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1304                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, ob, ANIMTYPE_OBJECT, filter_mode, (ID *)ob);
1305                                 }
1306                         },
1307                         { /* action (keyframes) */
1308                                 /* include action-expand widget? */
1309                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1310                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, base, ANIMTYPE_OBJECT, (ID *)ob);
1311                                         if (ale) {
1312                                                 BLI_addtail(anim_data, ale);
1313                                                 items++;
1314                                         }
1315                                 }
1316                                 
1317                                 /* add F-Curve channels? */
1318                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1319                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1320                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob); 
1321                                 }
1322                         }
1323                 );
1324         }
1325         
1326         
1327         /* ShapeKeys? */
1328         if ((key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
1329                 adt= key->adt;
1330                 ANIMDATA_FILTER_CASES(key,
1331                         { /* AnimData blocks - do nothing... */ },
1332                         { /* nla */
1333                                 /* include shapekey-expand widget? */
1334                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1335                                         /* check if filtering by active status */
1336                                         if ANIMCHANNEL_ACTIVEOK(key) {
1337                                                 ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1338                                                 if (ale) {
1339                                                         BLI_addtail(anim_data, ale);
1340                                                         items++;
1341                                                 }
1342                                         }
1343                                 }
1344                                 
1345                                 /* add NLA tracks - only if expanded or so */
1346                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY))
1347                                         items += animdata_filter_nla(anim_data, ads, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1348                         },
1349                         { /* drivers */
1350                                 /* include shapekey-expand widget? */
1351                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1352                                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1353                                         if (ale) {
1354                                                 BLI_addtail(anim_data, ale);
1355                                                 items++;
1356                                         }
1357                                 }
1358                                 
1359                                 /* add channels */
1360                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1361                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, key, ANIMTYPE_DSSKEY, filter_mode, (ID *)key);
1362                                 }
1363                         },
1364                         { /* action (keyframes) */
1365                                 /* include shapekey-expand widget? */
1366                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1367                                         /* check if filtering by active status */
1368                                         if ANIMCHANNEL_ACTIVEOK(key) {
1369                                                 ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1370                                                 if (ale) {
1371                                                         BLI_addtail(anim_data, ale);
1372                                                         items++;
1373                                                 }
1374                                         }
1375                                 }
1376                                 
1377                                 /* add channels */
1378                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1379                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, key, ANIMTYPE_DSSKEY, (ID *)key); 
1380                                 }
1381                         }
1382                 );
1383         }
1384
1385         /* Materials? */
1386         if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT))
1387                 items += animdata_filter_dopesheet_mats(anim_data, ads, base, filter_mode);
1388         
1389         /* Object Data */
1390         switch (ob->type) {
1391                 case OB_CAMERA: /* ------- Camera ------------ */
1392                 {
1393                         Camera *ca= (Camera *)ob->data;
1394                         
1395                         if ((ads->filterflag & ADS_FILTER_NOCAM) == 0) {
1396                                 ANIMDATA_FILTER_CASES(ca,
1397                                         { /* AnimData blocks - do nothing... */ },
1398                                         obdata_ok= 1;,
1399                                         obdata_ok= 1;,
1400                                         obdata_ok= 1;)
1401                         }
1402                 }
1403                         break;
1404                 case OB_LAMP: /* ---------- Lamp ----------- */
1405                 {
1406                         Lamp *la= (Lamp *)ob->data;
1407                         
1408                         if ((ads->filterflag & ADS_FILTER_NOLAM) == 0) {
1409                                 ANIMDATA_FILTER_CASES(la,
1410                                         { /* AnimData blocks - do nothing... */ },
1411                                         obdata_ok= 1;,
1412                                         obdata_ok= 1;,
1413                                         obdata_ok= 1;)
1414                         }
1415                 }
1416                         break;
1417                 case OB_CURVE: /* ------- Curve ---------- */
1418                 {
1419                         Curve *cu= (Curve *)ob->data;
1420                         
1421                         if ((ads->filterflag & ADS_FILTER_NOCUR) == 0) {
1422                                 ANIMDATA_FILTER_CASES(cu,
1423                                         { /* AnimData blocks - do nothing... */ },
1424                                         obdata_ok= 1;,
1425                                         obdata_ok= 1;,
1426                                         obdata_ok= 1;)
1427                         }
1428                 }
1429                         break;
1430                 case OB_MBALL: /* ------- MetaBall ---------- */
1431                 {
1432                         MetaBall *mb= (MetaBall *)ob->data;
1433                         
1434                         if ((ads->filterflag & ADS_FILTER_NOMBA) == 0) {
1435                                 ANIMDATA_FILTER_CASES(mb,
1436                                         { /* AnimData blocks - do nothing... */ },
1437                                         obdata_ok= 1;,
1438                                         obdata_ok= 1;,
1439                                         obdata_ok= 1;)
1440                         }
1441                 }
1442                         break;
1443                 case OB_ARMATURE: /* ------- Armature ---------- */
1444                 {
1445                         bArmature *arm= (bArmature *)ob->data;
1446                         
1447                         if ((ads->filterflag & ADS_FILTER_NOARM) == 0) {
1448                                 ANIMDATA_FILTER_CASES(arm,
1449                                         { /* AnimData blocks - do nothing... */ },
1450                                         obdata_ok= 1;,
1451                                         obdata_ok= 1;,
1452                                         obdata_ok= 1;)
1453                         }
1454                 }
1455                         break;
1456         }
1457         if (obdata_ok) 
1458                 items += animdata_filter_dopesheet_obdata(anim_data, ads, base, filter_mode);
1459
1460         /* particles */
1461         if (ob->particlesystem.first && !(ads->filterflag & ADS_FILTER_NOPART))
1462                 items += animdata_filter_dopesheet_particles(anim_data, ads, base, filter_mode);
1463         
1464         /* return the number of items added to the list */
1465         return items;
1466 }       
1467
1468 static int animdata_filter_dopesheet_scene (ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
1469 {
1470         World *wo= sce->world;
1471         AnimData *adt= NULL;
1472         bAnimListElem *ale;
1473         int items = 0;
1474         
1475         /* add scene as a channel first (even if we aren't showing scenes we still need to show the scene's sub-data */
1476         if ((filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) == 0) {
1477                 /* check if filtering by selection */
1478                 if (ANIMCHANNEL_SELOK( (sce->flag & SCE_DS_SELECTED) )) {
1479                         ale= make_new_animlistelem(sce, ANIMTYPE_SCENE, NULL, ANIMTYPE_NONE, NULL);
1480                         if (ale) {
1481                                 BLI_addtail(anim_data, ale);
1482                                 items++;
1483                         }
1484                 }
1485         }
1486         
1487         /* if collapsed, don't go any further (unless adding keyframes only) */
1488         if ( (EXPANDED_SCEC(sce) == 0) && !(filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) )
1489                 return items;
1490                 
1491         /* Action, Drivers, or NLA for Scene */
1492         if ((ads->filterflag & ADS_FILTER_NOSCE) == 0) {
1493                 adt= sce->adt;
1494                 ANIMDATA_FILTER_CASES(sce,
1495                         { /* AnimData blocks - do nothing... */ },
1496                         { /* nla */
1497                                 /* add NLA tracks */
1498                                 items += animdata_filter_nla(anim_data, ads, adt, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce);
1499                         },
1500                         { /* drivers */
1501                                 /* include drivers-expand widget? */
1502                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1503                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, sce, ANIMTYPE_SCENE, (ID *)sce);
1504                                         if (ale) {
1505                                                 BLI_addtail(anim_data, ale);
1506                                                 items++;
1507                                         }
1508                                 }
1509                                 
1510                                 /* add F-Curve channels (drivers are F-Curves) */
1511                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1512                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, sce, ANIMTYPE_SCENE, filter_mode, (ID *)sce);
1513                                 }
1514                         },
1515                         { /* action */
1516                                 /* include action-expand widget? */
1517                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1518                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, sce, ANIMTYPE_SCENE, (ID *)sce);
1519                                         if (ale) {
1520                                                 BLI_addtail(anim_data, ale);
1521                                                 items++;
1522                                         }
1523                                 }
1524                                 
1525                                 /* add F-Curve channels? */
1526                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1527                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce); 
1528                                 }
1529                         }
1530                 )
1531         }
1532         
1533         /* world */
1534         if ((wo && wo->adt) && !(ads->filterflag & ADS_FILTER_NOWOR)) {
1535                 /* Action, Drivers, or NLA for World */
1536                 adt= wo->adt;
1537                 ANIMDATA_FILTER_CASES(wo,
1538                         { /* AnimData blocks - do nothing... */ },
1539                         { /* nla */
1540                                 /* add NLA tracks */
1541                                 items += animdata_filter_nla(anim_data, ads, adt, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo);
1542                         },
1543                         { /* drivers */
1544                                 /* include world-expand widget? */
1545                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1546                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)wo);
1547                                         if (ale) {
1548                                                 BLI_addtail(anim_data, ale);
1549                                                 items++;
1550                                         }
1551                                 }
1552                                 
1553                                 /* add F-Curve channels (drivers are F-Curves) */
1554                                 if (FILTER_WOR_SCED(wo)/*EXPANDED_DRVD(adt)*/ || !(filter_mode & ANIMFILTER_CHANNELS)) {
1555                                         // XXX owner info is messed up now...
1556                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, wo, ANIMTYPE_DSWOR, filter_mode, (ID *)wo);
1557                                 }
1558                         },
1559                         { /* action */
1560                                 /* include world-expand widget? */
1561                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1562                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)sce);
1563                                         if (ale) {
1564                                                 BLI_addtail(anim_data, ale);
1565                                                 items++;
1566                                         }
1567                                 }
1568                                 
1569                                 /* add channels */
1570                                 if (FILTER_WOR_SCED(wo) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1571                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo); 
1572                                 }
1573                         }
1574                 )
1575         }
1576         
1577         // TODO: scene compositing nodes (these aren't standard node-trees)
1578         
1579         /* return the number of items added to the list */
1580         return items;
1581 }
1582
1583 // 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)
1584 static int animdata_filter_dopesheet (ListBase *anim_data, bAnimContext *ac, bDopeSheet *ads, int filter_mode)
1585 {
1586         Scene *sce= (Scene *)ads->source;
1587         Base *base;
1588         bAnimListElem *ale;
1589         int items = 0;
1590         
1591         /* check that we do indeed have a scene */
1592         if ((ads->source == NULL) || (GS(ads->source->name)!=ID_SCE)) {
1593                 printf("DopeSheet Error: Not scene! \n");
1594                 return 0;
1595         }
1596         
1597         /* scene-linked animation */
1598         // TODO: sequencer, composite nodes - are we to include those here too?
1599         {
1600                 short sceOk= 0, worOk= 0;
1601                 
1602                 /* check filtering-flags if ok */
1603                 ANIMDATA_FILTER_CASES(sce, 
1604                         {
1605                                 /* for the special AnimData blocks only case, we only need to add
1606                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1607                                  */
1608                                 ANIMDATA_ADD_ANIMDATA(sce);
1609                                 sceOk=0;
1610                         },
1611                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);, 
1612                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);, 
1613                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);)
1614                 if (sce->world) {
1615                         ANIMDATA_FILTER_CASES(sce->world, 
1616                                 {
1617                                         /* for the special AnimData blocks only case, we only need to add
1618                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1619                                          */
1620                                         ANIMDATA_ADD_ANIMDATA(sce->world);
1621                                         worOk=0;
1622                                 },
1623                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);, 
1624                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);, 
1625                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);)
1626                 }
1627                 
1628                 /* if only F-Curves with visible flags set can be shown, check that 
1629                  * datablocks haven't been set to invisible 
1630                  */
1631                 if (filter_mode & ANIMFILTER_CURVEVISIBLE) {
1632                         if ((sce->adt) && (sce->adt->flag & ADT_CURVES_NOT_VISIBLE))
1633                                 sceOk= worOk= 0;
1634                 }
1635                 
1636                 /* check if not all bad (i.e. so there is something to show) */
1637                 if ( !(!sceOk && !worOk) ) {
1638                         /* add scene data to the list of filtered channels */
1639                         items += animdata_filter_dopesheet_scene(anim_data, ads, sce, filter_mode);
1640                 }
1641         }
1642         
1643         
1644         /* loop over all bases in the scene */
1645         for (base= sce->base.first; base; base= base->next) {
1646                 /* check if there's an object (all the relevant checks are done in the ob-function) */
1647                 if (base->object) {
1648                         Object *ob= base->object;
1649                         Key *key= ob_get_key(ob);
1650                         short actOk=1, keyOk=1, dataOk=1, matOk=1, partOk=1;
1651                         
1652                         /* firstly, check if object can be included, by the following factors:
1653                          *      - if only visible, must check for layer and also viewport visibility
1654                          *      - if only selected, must check if object is selected 
1655                          *      - there must be animation data to edit
1656                          */
1657                         // TODO: if cache is implemented, just check name here, and then 
1658                         if (filter_mode & ANIMFILTER_VISIBLE) {
1659                                 /* layer visibility - we check both object and base, since these may not be in sync yet */
1660                                 if ((sce->lay & (ob->lay|base->lay))==0) continue;
1661                                 
1662                                 /* outliner restrict-flag */
1663                                 if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
1664                         }
1665                         
1666                         /* if only F-Curves with visible flags set can be shown, check that 
1667                          * datablock hasn't been set to invisible 
1668                          */
1669                         if (filter_mode & ANIMFILTER_CURVEVISIBLE) {
1670                                 if ((ob->adt) && (ob->adt->flag & ADT_CURVES_NOT_VISIBLE))
1671                                         continue;
1672                         }
1673                         
1674                         /* additionally, dopesheet filtering also affects what objects to consider */
1675                         if (ads->filterflag) {
1676                                 /* check selection and object type filters */
1677                                 if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) || (base == sce->basact)) )  {
1678                                         /* only selected should be shown */
1679                                         continue;
1680                                 }
1681                                 
1682                                 /* check filters for datatypes */
1683                                         /* object */
1684                                 actOk= 0;
1685                                 keyOk= 0;
1686                                 ANIMDATA_FILTER_CASES(ob, 
1687                                         {
1688                                                 /* for the special AnimData blocks only case, we only need to add
1689                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1690                                                  */
1691                                                 ANIMDATA_ADD_ANIMDATA(ob);
1692                                                 actOk=0;
1693                                         },
1694                                         actOk= 1;, 
1695                                         actOk= 1;, 
1696                                         actOk= 1;)
1697                                 if (key) {
1698                                         /* shapekeys */
1699                                         ANIMDATA_FILTER_CASES(key, 
1700                                                 {
1701                                                         /* for the special AnimData blocks only case, we only need to add
1702                                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1703                                                          */
1704                                                         ANIMDATA_ADD_ANIMDATA(key);
1705                                                         keyOk=0;
1706                                                 },
1707                                                 keyOk= 1;, 
1708                                                 keyOk= 1;, 
1709                                                 keyOk= 1;)
1710                                 }
1711                                 
1712                                 /* materials - only for geometric types */
1713                                 matOk= 0; /* by default, not ok... */
1714                                 if ( !(ads->filterflag & ADS_FILTER_NOMAT) && (ob->totcol) && 
1715                                          ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) ) 
1716                                 {
1717                                         int a;
1718                                         
1719                                         /* firstly check that we actuallly have some materials */
1720                                         for (a=0; a < ob->totcol; a++) {
1721                                                 Material *ma= give_current_material(ob, a);
1722                                                 
1723                                                 if (ma) {
1724                                                         /* if material has relevant animation data, break */
1725                                                         ANIMDATA_FILTER_CASES(ma, 
1726                                                                 {
1727                                                                         /* for the special AnimData blocks only case, we only need to add
1728                                                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1729                                                                          */
1730                                                                         ANIMDATA_ADD_ANIMDATA(ma);
1731                                                                         matOk=0;
1732                                                                 },
1733                                                                 matOk= 1;, 
1734                                                                 matOk= 1;, 
1735                                                                 matOk= 1;)
1736                                                 }
1737                                                         
1738                                                 if (matOk) 
1739                                                         break;
1740                                         }
1741                                 }
1742                                 
1743                                 /* data */
1744                                 switch (ob->type) {
1745                                         case OB_CAMERA: /* ------- Camera ------------ */
1746                                         {
1747                                                 Camera *ca= (Camera *)ob->data;
1748                                                 dataOk= 0;
1749                                                 ANIMDATA_FILTER_CASES(ca, 
1750                                                         if ((ads->filterflag & ADS_FILTER_NOCAM)==0) {
1751                                                                 /* for the special AnimData blocks only case, we only need to add
1752                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1753                                                                  */
1754                                                                 ANIMDATA_ADD_ANIMDATA(ca);
1755                                                                 dataOk=0;
1756                                                         },
1757                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);, 
1758                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);, 
1759                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);)
1760                                         }
1761                                                 break;
1762                                         case OB_LAMP: /* ---------- Lamp ----------- */
1763                                         {
1764                                                 Lamp *la= (Lamp *)ob->data;
1765                                                 dataOk= 0;
1766                                                 ANIMDATA_FILTER_CASES(la, 
1767                                                         if ((ads->filterflag & ADS_FILTER_NOLAM)==0) {
1768                                                                 /* for the special AnimData blocks only case, we only need to add
1769                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1770                                                                  */
1771                                                                 ANIMDATA_ADD_ANIMDATA(la);
1772                                                                 dataOk=0;
1773                                                         },
1774                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);, 
1775                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);, 
1776                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);)
1777                                         }
1778                                                 break;
1779                                         case OB_CURVE: /* ------- Curve ---------- */
1780                                         {
1781                                                 Curve *cu= (Curve *)ob->data;
1782                                                 dataOk= 0;
1783                                                 ANIMDATA_FILTER_CASES(cu, 
1784                                                         if ((ads->filterflag & ADS_FILTER_NOCUR)==0) {
1785                                                                 /* for the special AnimData blocks only case, we only need to add
1786                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1787                                                                  */
1788                                                                 ANIMDATA_ADD_ANIMDATA(cu);
1789                                                                 dataOk=0;
1790                                                         },
1791                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);, 
1792                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);, 
1793                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);)
1794                                         }
1795                                                 break;
1796                                         case OB_MBALL: /* ------- MetaBall ---------- */
1797                                         {
1798                                                 MetaBall *mb= (MetaBall *)ob->data;
1799                                                 dataOk= 0;
1800                                                 ANIMDATA_FILTER_CASES(mb, 
1801                                                         if ((ads->filterflag & ADS_FILTER_NOMBA)==0) {
1802                                                                 /* for the special AnimData blocks only case, we only need to add
1803                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1804                                                                  */
1805                                                                 ANIMDATA_ADD_ANIMDATA(mb);
1806                                                                 dataOk=0;
1807                                                         },
1808                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOMBA);, 
1809                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOMBA);, 
1810                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOMBA);)
1811                                         }
1812                                                 break;
1813                                         case OB_ARMATURE: /* ------- Armature ---------- */
1814                                         {
1815                                                 bArmature *arm= (bArmature *)ob->data;
1816                                                 dataOk= 0;
1817                                                 ANIMDATA_FILTER_CASES(arm, 
1818                                                         if ((ads->filterflag & ADS_FILTER_NOARM)==0) {
1819                                                                 /* for the special AnimData blocks only case, we only need to add
1820                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1821                                                                  */
1822                                                                 ANIMDATA_ADD_ANIMDATA(arm);
1823                                                                 dataOk=0;
1824                                                         },
1825                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOARM);, 
1826                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOARM);, 
1827                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOARM);)
1828                                         }
1829                                                 break;
1830                                         default: /* --- other --- */
1831                                                 dataOk= 0;
1832                                                 break;
1833                                 }
1834                                 
1835                                 /* particles */
1836                                 partOk = 0;
1837                                 if (!(ads->filterflag & ADS_FILTER_NOPART) && ob->particlesystem.first) {
1838                                         ParticleSystem *psys = ob->particlesystem.first;
1839                                         for(; psys; psys=psys->next) {
1840                                                 if (psys->part) {
1841                                                         /* if particlesettings has relevant animation data, break */
1842                                                         ANIMDATA_FILTER_CASES(psys->part, 
1843                                                                 {
1844                                                                         /* for the special AnimData blocks only case, we only need to add
1845                                                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1846                                                                          */
1847                                                                         ANIMDATA_ADD_ANIMDATA(psys->part);
1848                                                                         partOk=0;
1849                                                                 },
1850                                                                 partOk= 1;, 
1851                                                                 partOk= 1;, 
1852                                                                 partOk= 1;)
1853                                                 }
1854                                                         
1855                                                 if (partOk) 
1856                                                         break;
1857                                         }
1858                                 }
1859                                 
1860                                 /* check if all bad (i.e. nothing to show) */
1861                                 if (!actOk && !keyOk && !dataOk && !matOk && !partOk)
1862                                         continue;
1863                         }
1864                         else {
1865                                 /* check data-types */
1866                                 actOk= ANIMDATA_HAS_KEYS(ob);
1867                                 keyOk= (key != NULL);
1868                                 
1869                                 /* materials - only for geometric types */
1870                                 matOk= 0; /* by default, not ok... */
1871                                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) && (ob->totcol)) 
1872                                 {
1873                                         int a;
1874                                         
1875                                         /* firstly check that we actuallly have some materials */
1876                                         for (a=0; a < ob->totcol; a++) {
1877                                                 Material *ma= give_current_material(ob, a);
1878                                                 
1879                                                 if ((ma) && ANIMDATA_HAS_KEYS(ma)) {
1880                                                         matOk= 1;
1881                                                         break;
1882                                                 }
1883                                         }
1884                                 }
1885                                 
1886                                 /* data */
1887                                 switch (ob->type) {
1888                                         case OB_CAMERA: /* ------- Camera ------------ */
1889                                         {
1890                                                 Camera *ca= (Camera *)ob->data;
1891                                                 dataOk= ANIMDATA_HAS_KEYS(ca);                                          
1892                                         }
1893                                                 break;
1894                                         case OB_LAMP: /* ---------- Lamp ----------- */
1895                                         {
1896                                                 Lamp *la= (Lamp *)ob->data;
1897                                                 dataOk= ANIMDATA_HAS_KEYS(la);  
1898                                         }
1899                                                 break;
1900                                         case OB_CURVE: /* -------- Curve ---------- */
1901                                         {
1902                                                 Curve *cu= (Curve *)ob->data;
1903                                                 dataOk= ANIMDATA_HAS_KEYS(cu);  
1904                                         }
1905                                                 break;
1906                                         case OB_MBALL: /* -------- Metas ---------- */
1907                                         {
1908                                                 MetaBall *mb= (MetaBall *)ob->data;
1909                                                 dataOk= ANIMDATA_HAS_KEYS(mb);  
1910                                         }
1911                                                 break;
1912                                         case OB_ARMATURE: /* -------- Armature ---------- */
1913                                         {
1914                                                 bArmature *arm= (bArmature *)ob->data;
1915                                                 dataOk= ANIMDATA_HAS_KEYS(arm); 
1916                                         }
1917                                                 break;
1918                                         default: /* --- other --- */
1919                                                 dataOk= 0;
1920                                                 break;
1921                                 }
1922                                 
1923                                 /* particles */
1924                                 partOk = 0;
1925                                 if (ob->particlesystem.first) {
1926                                         ParticleSystem *psys = ob->particlesystem.first;
1927                                         for(; psys; psys=psys->next) {
1928                                                 if(psys->part && ANIMDATA_HAS_KEYS(psys->part)) {
1929                                                         partOk = 1;
1930                                                         break;
1931                                                 }
1932                                         }
1933                                 }
1934                                 
1935                                 /* check if all bad (i.e. nothing to show) */
1936                                 if (!actOk && !keyOk && !dataOk && !matOk && !partOk)
1937                                         continue;
1938                         }
1939                         
1940                         /* since we're still here, this object should be usable */
1941                         items += animdata_filter_dopesheet_ob(anim_data, ads, base, filter_mode);
1942                 }
1943         }
1944         
1945         /* return the number of items in the list */
1946         return items;
1947 }
1948
1949 /* Summary track for DopeSheet/Action Editor 
1950  *      - return code is whether the summary lets the other channels get drawn
1951  */
1952 static short animdata_filter_dopesheet_summary (bAnimContext *ac, ListBase *anim_data, int filter_mode, int *items)
1953 {
1954         bDopeSheet *ads = NULL;
1955         
1956         /* get the DopeSheet information to use 
1957          *      - we should only need to deal with the DopeSheet/Action Editor, 
1958          *        since all the other Animation Editors won't have this concept
1959          *        being applicable.
1960          */
1961         if ((ac && ac->sa) && (ac->sa->spacetype == SPACE_ACTION)) {
1962                 SpaceAction *saction= (SpaceAction *)ac->sa->spacedata.first;
1963                 ads= &saction->ads;
1964         }
1965         else {
1966                 /* invalid space type - skip this summary channels */
1967                 return 1;
1968         }
1969         
1970         /* dopesheet summary 
1971          *      - only for drawing and/or selecting keyframes in channels, but not for real editing 
1972          *      - only useful for DopeSheet Editor, where the summary is useful
1973          */
1974         // TODO: we should really check if some other prohibited filters are also active, but that can be for later
1975         if ((filter_mode & ANIMFILTER_CHANNELS) && (ads->filterflag & ADS_FILTER_SUMMARY)) {
1976                 bAnimListElem *ale= make_new_animlistelem(ac, ANIMTYPE_SUMMARY, NULL, ANIMTYPE_NONE, NULL);
1977                 if (ale) {
1978                         BLI_addtail(anim_data, ale);
1979                         (*items)++;
1980                 }
1981                 
1982                 /* if summary is collapsed, don't show other channels beneath this 
1983                  *      - this check is put inside the summary check so that it doesn't interfere with normal operation
1984                  */ 
1985                 if (ads->flag & ADS_FLAG_SUMMARY_COLLAPSED)
1986                         return 0;
1987         }
1988         
1989         /* the other channels beneath this can be shown */
1990         return 1;
1991 }  
1992
1993 /* ----------- Public API --------------- */
1994
1995 /* This function filters the active data source to leave only animation channels suitable for
1996  * usage by the caller. It will return the length of the list 
1997  * 
1998  *      *anim_data: is a pointer to a ListBase, to which the filtered animation channels
1999  *              will be placed for use.
2000  *      filter_mode: how should the data be filtered - bitmapping accessed flags
2001  */
2002 int ANIM_animdata_filter (bAnimContext *ac, ListBase *anim_data, int filter_mode, void *data, short datatype)
2003 {
2004         int items = 0;
2005         
2006         /* only filter data if there's somewhere to put it */
2007         if (data && anim_data) {
2008                 bAnimListElem *ale, *next;
2009                 Object *obact= (ac) ? ac->obact : NULL;
2010                 
2011                 /* firstly filter the data */
2012                 switch (datatype) {
2013                         case ANIMCONT_ACTION:   /* 'Action Editor' */
2014                         {
2015                                 /* the check for the DopeSheet summary is included here since the summary works here too */
2016                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2017                                         items += animdata_filter_action(anim_data, NULL, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
2018                         }
2019                                 break;
2020                                 
2021                         case ANIMCONT_SHAPEKEY: /* 'ShapeKey Editor' */
2022                         {
2023                                 /* the check for the DopeSheet summary is included here since the summary works here too */
2024                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2025                                         items= animdata_filter_shapekey(anim_data, data, filter_mode);
2026                         }
2027                                 break;
2028                                 
2029                         case ANIMCONT_GPENCIL:
2030                         {
2031                                 //items= animdata_filter_gpencil(anim_data, data, filter_mode);
2032                         }
2033                                 break;
2034                                 
2035                         case ANIMCONT_DOPESHEET: /* 'DopeSheet Editor' */
2036                         {
2037                                 /* the DopeSheet editor is the primary place where the DopeSheet summaries are useful */
2038                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2039                                         items += animdata_filter_dopesheet(anim_data, ac, data, filter_mode);
2040                         }
2041                                 break;
2042                                 
2043                         case ANIMCONT_FCURVES: /* Graph Editor -> FCurves/Animation Editing */
2044                         case ANIMCONT_DRIVERS: /* Graph Editor -> Drivers Editing */
2045                         case ANIMCONT_NLA: /* NLA Editor */
2046                         {
2047                                 /* all of these editors use the basic DopeSheet data for filtering options, but don't have all the same features */
2048                                 items = animdata_filter_dopesheet(anim_data, ac, data, filter_mode);
2049                         }
2050                                 break;
2051                 }
2052                         
2053                 /* remove any weedy entries */
2054                 // XXX this is weedy code!
2055                 for (ale= anim_data->first; ale; ale= next) {
2056                         next= ale->next;
2057                         
2058                         if (ale->type == ANIMTYPE_NONE) {
2059                                 items--;
2060                                 BLI_freelinkN(anim_data, ale);
2061                         }
2062                 }
2063         }
2064         
2065         /* return the number of items in the list */
2066         return items;
2067 }
2068
2069 /* ************************************************************ */