2.5 - Quick fix for NLA drawing
[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.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Joshua Leung
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 IPO Editor also uses this for it's channel list and for determining which curves
37  * are being edited.
38  *
39  * Note: much of the original system this was based on was built before the creation of the RNA
40  * system. In future, it would be interesting to replace some parts of this code with RNA queries,
41  * however, RNA does not eliminate some of the boiler-plate reduction benefits presented by this 
42  * system, so if any such work does occur, it should only be used for the internals used here...
43  *
44  * -- Joshua Leung, Dec 2008
45  */
46
47 #include <string.h>
48 #include <stdio.h>
49
50 #include "DNA_listBase.h"
51 #include "DNA_ID.h"
52 #include "DNA_anim_types.h"
53 #include "DNA_action_types.h"
54 #include "DNA_constraint_types.h"
55 #include "DNA_camera_types.h"
56 #include "DNA_curve_types.h"
57 #include "DNA_gpencil_types.h"
58 #include "DNA_ipo_types.h"
59 #include "DNA_lamp_types.h"
60 #include "DNA_lattice_types.h"
61 #include "DNA_key_types.h"
62 #include "DNA_material_types.h"
63 #include "DNA_mesh_types.h"
64 #include "DNA_object_types.h"
65 #include "DNA_space_types.h"
66 #include "DNA_scene_types.h"
67 #include "DNA_screen_types.h"
68 #include "DNA_windowmanager_types.h"
69 #include "DNA_world_types.h"
70
71 #include "MEM_guardedalloc.h"
72
73 #include "BLI_blenlib.h"
74
75 #include "BKE_context.h"
76 #include "BKE_global.h"
77 #include "BKE_key.h"
78 #include "BKE_object.h"
79 #include "BKE_material.h"
80 #include "BKE_screen.h"
81 #include "BKE_utildefines.h"
82
83 #include "ED_anim_api.h"
84 #include "ED_types.h"
85 #include "ED_util.h"
86
87 #include "WM_api.h"
88 #include "WM_types.h"
89
90 #include "BIF_gl.h"
91 #include "BIF_glutil.h"
92
93 #include "UI_interface.h"
94 #include "UI_resources.h"
95 #include "UI_view2d.h"
96
97 /* ************************************************************ */
98 /* Blender Context <-> Animation Context mapping */
99
100 /* ----------- Private Stuff - Action Editor ------------- */
101
102 /* Get shapekey data being edited (for Action Editor -> ShapeKey mode) */
103 /* Note: there's a similar function in key.c (ob_get_key) */
104 Key *actedit_get_shapekeys (bAnimContext *ac, SpaceAction *saction) 
105 {
106     Scene *scene= ac->scene;
107     Object *ob;
108     Key *key;
109         
110     ob = OBACT;
111     if (ob == NULL) 
112                 return NULL;
113         
114         /* XXX pinning is not available in 'ShapeKey' mode... */
115         //if (saction->pin) return NULL;
116         
117         /* shapekey data is stored with geometry data */
118         switch (ob->type) {
119                 case OB_MESH:
120                         key= ((Mesh *)ob->data)->key;
121                         break;
122                         
123                 case OB_LATTICE:
124                         key= ((Lattice *)ob->data)->key;
125                         break;
126                         
127                 case OB_CURVE:
128                 case OB_SURF:
129                         key= ((Curve *)ob->data)->key;
130                         break;
131                         
132                 default:
133                         return NULL;
134         }
135         
136         if (key) {
137                 if (key->type == KEY_RELATIVE)
138                         return key;
139         }
140         
141     return NULL;
142 }
143
144 /* Get data being edited in Action Editor (depending on current 'mode') */
145 static short actedit_get_context (bAnimContext *ac, SpaceAction *saction)
146 {
147         /* sync settings with current view status, then return appropriate data */
148         switch (saction->mode) {
149                 case SACTCONT_ACTION: /* 'Action Editor' */
150                         /* if not pinned, sync with active object */
151                         if (saction->pin == 0) {
152                                 if (ac->obact && ac->obact->adt)
153                                         saction->action = ac->obact->adt->action;
154                                 else
155                                         saction->action= NULL;
156                         }
157                         
158                         ac->datatype= ANIMCONT_ACTION;
159                         ac->data= saction->action;
160                         
161                         ac->mode= saction->mode;
162                         return 1;
163                         
164                 case SACTCONT_SHAPEKEY: /* 'ShapeKey Editor' */
165                         ac->datatype= ANIMCONT_SHAPEKEY;
166                         ac->data= actedit_get_shapekeys(ac, saction);
167                         
168                         ac->mode= saction->mode;
169                         return 1;
170                         
171                 case SACTCONT_GPENCIL: /* Grease Pencil */ // XXX review how this mode is handled...
172                         ac->datatype=ANIMCONT_GPENCIL;
173                         //ac->data= CTX_wm_screen(C); // FIXME: add that dopesheet type thing here!
174                         ac->data= NULL; // !!!
175                         
176                         ac->mode= saction->mode;
177                         return 1;
178                         
179                 case SACTCONT_DOPESHEET: /* DopeSheet */
180                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
181                         saction->ads.source= (ID *)ac->scene;
182                         
183                         ac->datatype= ANIMCONT_DOPESHEET;
184                         ac->data= &saction->ads;
185                         
186                         ac->mode= saction->mode;
187                         return 1;
188                 
189                 default: /* unhandled yet */
190                         ac->datatype= ANIMCONT_NONE;
191                         ac->data= NULL;
192                         
193                         ac->mode= -1;
194                         return 0;
195         }
196 }
197
198 /* ----------- Private Stuff - Graph Editor ------------- */
199
200 /* Get data being edited in Graph Editor (depending on current 'mode') */
201 static short graphedit_get_context (bAnimContext *ac, SpaceIpo *sipo)
202 {
203         /* init dopesheet data if non-existant (i.e. for old files) */
204         if (sipo->ads == NULL)
205                 sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
206         
207         /* sync settings with current view status, then return appropriate data */
208         switch (sipo->mode) {
209                 case SIPO_MODE_ANIMATION:       /* Animation F-Curve Editor */
210                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
211                         sipo->ads->source= (ID *)ac->scene;
212                         sipo->ads->filterflag &= ~ADS_FILTER_ONLYDRIVERS;
213                         
214                         ac->datatype= ANIMCONT_FCURVES;
215                         ac->data= sipo->ads;
216                         
217                         ac->mode= sipo->mode;
218                         return 1;
219                 
220                 case SIPO_MODE_DRIVERS:         /* Driver F-Curve Editor */
221                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
222                         sipo->ads->source= (ID *)ac->scene;
223                         sipo->ads->filterflag |= ADS_FILTER_ONLYDRIVERS;
224                         
225                         ac->datatype= ANIMCONT_DRIVERS;
226                         ac->data= sipo->ads;
227                         
228                         ac->mode= sipo->mode;
229                         return 1;
230                 
231                 default: /* unhandled yet */
232                         ac->datatype= ANIMCONT_NONE;
233                         ac->data= NULL;
234                         
235                         ac->mode= -1;
236                         return 0;
237         }
238 }
239
240 /* ----------- Private Stuff - NLA Editor ------------- */
241
242 /* Get data being edited in Graph Editor (depending on current 'mode') */
243 static short nlaedit_get_context (bAnimContext *ac, SpaceNla *snla)
244 {
245         /* init dopesheet data if non-existant (i.e. for old files) */
246         if (snla->ads == NULL)
247                 snla->ads= MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
248         
249         /* sync settings with current view status, then return appropriate data */
250         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
251         snla->ads->source= (ID *)ac->scene;
252         snla->ads->filterflag |= ADS_FILTER_ONLYNLA;
253         
254         ac->datatype= ANIMCONT_NLA;
255         ac->data= snla->ads;
256         
257         return 1;
258 }
259
260 /* ----------- Public API --------------- */
261
262 /* Obtain current anim-data context, given that context info from Blender context has already been set 
263  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
264  *        allocation/freeing costs (which are not that avoidable with channels).
265  */
266 short ANIM_animdata_context_getdata (bAnimContext *ac)
267 {
268         ScrArea *sa= ac->sa;
269         short ok= 0;
270         
271         /* context depends on editor we are currently in */
272         if (sa) {
273                 switch (sa->spacetype) {
274                         case SPACE_ACTION:
275                         {
276                                 SpaceAction *saction= (SpaceAction *)sa->spacedata.first;
277                                 ok= actedit_get_context(ac, saction);
278                         }
279                                 break;
280                                 
281                         case SPACE_IPO:
282                         {
283                                 SpaceIpo *sipo= (SpaceIpo *)sa->spacedata.first;
284                                 ok= graphedit_get_context(ac, sipo);
285                         }
286                                 break;
287                                 
288                         case SPACE_NLA:
289                         {
290                                 SpaceNla *snla= (SpaceNla *)sa->spacedata.first;
291                                 ok= nlaedit_get_context(ac, snla);
292                         }
293                                 break;
294                 }
295         }
296         
297         /* check if there's any valid data */
298         if (ok && ac->data)
299                 return 1;
300         else
301                 return 0;
302 }
303
304 /* Obtain current anim-data context from Blender Context info 
305  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
306  *        allocation/freeing costs (which are not that avoidable with channels).
307  *      - Clears data and sets the information from Blender Context which is useful
308  */
309 short ANIM_animdata_get_context (const bContext *C, bAnimContext *ac)
310 {
311         ScrArea *sa= CTX_wm_area(C);
312         ARegion *ar= CTX_wm_region(C);
313         Scene *scene= CTX_data_scene(C);
314         
315         /* clear old context info */
316         if (ac == NULL) return 0;
317         memset(ac, 0, sizeof(bAnimContext));
318         
319         /* get useful default context settings from context */
320         ac->scene= scene;
321         if (scene) {
322                 ac->markers= &scene->markers;           
323                 ac->obact= (scene->basact)?  scene->basact->object : NULL;
324         }
325         ac->sa= sa;
326         ac->ar= ar;
327         ac->spacetype= (sa) ? sa->spacetype : 0;
328         ac->regiontype= (ar) ? ar->regiontype : 0;
329         
330         /* get data context info */
331         return ANIM_animdata_context_getdata(ac);
332 }
333
334 /* ************************************************************ */
335 /* Blender Data <-- Filter --> Channels to be operated on */
336
337 /* quick macro to test if AnimData is usable */
338 #define ANIMDATA_HAS_KEYS(id) ((id)->adt && (id)->adt->action)
339
340 /* quick macro to test if AnimData is usable for drivers */
341 #define ANIMDATA_HAS_DRIVERS(id) ((id)->adt && (id)->adt->drivers.first)
342
343 /* quick macro to test if AnimData is usable for NLA */
344 #define ANIMDATA_HAS_NLA(id) ((id)->adt && (id)->adt->nla_tracks.first)
345
346
347 /* Quick macro to test for all three avove usability tests, performing the appropriate provided 
348  * action for each when the AnimData context is appropriate. 
349  *
350  * Priority order for this goes (most important, to least): AnimData blocks, NLA, Drivers, Keyframes.
351  *
352  * For this to work correctly, a standard set of data needs to be available within the scope that this
353  * gets called in: 
354  *      - ListBase anim_data;
355  *      - bDopeSheet *ads;
356  *      - bAnimListElem *ale;
357  *      - int items;
358  *
359  *      - id: ID block which should have an AnimData pointer following it immediately, to use
360  *      - adtOk: line or block of code to execute for AnimData-blocks case (usually ANIMDATA_ADD_ANIMDATA)
361  *      - nlaOk: line or block of code to execute for NLA case
362  *      - driversOk: line or block of code to execute for Drivers case
363  *      - keysOk: line or block of code for Keyframes case
364  */
365 #define ANIMDATA_FILTER_CASES(id, adtOk, nlaOk, driversOk, keysOk) \
366         {\
367                 if (filter_mode & ANIMFILTER_ANIMDATA) {\
368                         if ((id)->adt) {\
369                                 adtOk\
370                         }\
371                 }\
372                 else if (ads->filterflag & ADS_FILTER_ONLYNLA) {\
373                         if (ANIMDATA_HAS_NLA(id)) {\
374                                 nlaOk\
375                         }\
376                         else if (!(ads->filterflag & ADS_FILTER_NLA_NOACT) && ANIMDATA_HAS_KEYS(id)) {\
377                                 nlaOk\
378                         }\
379                 }\
380                 else if (ads->filterflag & ADS_FILTER_ONLYDRIVERS) {\
381                         if (ANIMDATA_HAS_DRIVERS(id)) {\
382                                 driversOk\
383                         }\
384                 }\
385                 else {\
386                         if (ANIMDATA_HAS_KEYS(id)) {\
387                                 keysOk\
388                         }\
389                 }\
390         }
391
392
393 /* quick macro to add a pointer to an AnimData block as a channel */
394 #define ANIMDATA_ADD_ANIMDATA(id) \
395         {\
396                 ale= make_new_animlistelem((id)->adt, ANIMTYPE_ANIMDATA, NULL, ANIMTYPE_NONE, (ID *)id);\
397                 if (ale) {\
398                         BLI_addtail(anim_data, ale);\
399                         items++;\
400                 }\
401         }
402         
403
404
405 /* quick macro to test if a anim-channel (F-Curve, Group, etc.) is selected in an acceptable way */
406 #define ANIMCHANNEL_SELOK(test_func) \
407                 ( !(filter_mode & (ANIMFILTER_SEL|ANIMFILTER_UNSEL)) || \
408                   ((filter_mode & ANIMFILTER_SEL) && test_func) || \
409                   ((filter_mode & ANIMFILTER_UNSEL) && test_func==0) ) 
410
411 /* ----------- 'Private' Stuff --------------- */
412
413 /* this function allocates memory for a new bAnimListElem struct for the 
414  * provided animation channel-data. 
415  */
416 bAnimListElem *make_new_animlistelem (void *data, short datatype, void *owner, short ownertype, ID *owner_id)
417 {
418         bAnimListElem *ale= NULL;
419         
420         /* only allocate memory if there is data to convert */
421         if (data) {
422                 /* allocate and set generic data */
423                 ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
424                 
425                 ale->data= data;
426                 ale->type= datatype;
427                         // XXX what is the point of the owner data?
428                 ale->owner= owner;
429                 ale->ownertype= ownertype;
430                 
431                 ale->id= owner_id;
432                 
433                 /* do specifics */
434                 switch (datatype) {
435                         case ANIMTYPE_SCENE:
436                         {
437                                 Scene *sce= (Scene *)data;
438                                 
439                                 ale->flag= sce->flag;
440                                 
441                                 ale->key_data= sce;
442                                 ale->datatype= ALE_SCE;
443                         }
444                                 break;
445                         case ANIMTYPE_OBJECT:
446                         {
447                                 Base *base= (Base *)data;
448                                 Object *ob= base->object;
449                                 
450                                 ale->flag= ob->flag;
451                                 
452                                 ale->key_data= ob;
453                                 ale->datatype= ALE_OB;
454                         }
455                                 break;
456                         case ANIMTYPE_FILLACTD:
457                         {
458                                 bAction *act= (bAction *)data;
459                                 
460                                 ale->flag= act->flag;
461                                 
462                                 ale->key_data= act;
463                                 ale->datatype= ALE_ACT;
464                         }
465                                 break;
466                         case ANIMTYPE_FILLDRIVERS:
467                         {
468                                 AnimData *adt= (AnimData *)data;
469                                 
470                                 ale->flag= adt->flag;
471                                 
472                                         // XXX... drivers don't show summary for now
473                                 ale->key_data= NULL;
474                                 ale->datatype= ALE_NONE;
475                         }
476                                 break;
477                         case ANIMTYPE_FILLMATD:
478                         {
479                                 Object *ob= (Object *)data;
480                                 
481                                 ale->flag= FILTER_MAT_OBJC(ob);
482                                 
483                                 ale->key_data= NULL;
484                                 ale->datatype= ALE_NONE;
485                         }
486                                 break;
487                         
488                         case ANIMTYPE_DSMAT:
489                         {
490                                 Material *ma= (Material *)data;
491                                 AnimData *adt= ma->adt;
492                                 
493                                 ale->flag= FILTER_MAT_OBJD(ma);
494                                 
495                                 ale->key_data= (adt) ? adt->action : NULL;
496                                 ale->datatype= ALE_ACT;
497                         }
498                                 break;
499                         case ANIMTYPE_DSLAM:
500                         {
501                                 Lamp *la= (Lamp *)data;
502                                 AnimData *adt= la->adt;
503                                 
504                                 ale->flag= FILTER_LAM_OBJD(la);
505                                 
506                                 ale->key_data= (adt) ? adt->action : NULL;
507                                 ale->datatype= ALE_ACT;
508                         }
509                                 break;
510                         case ANIMTYPE_DSCAM:
511                         {
512                                 Camera *ca= (Camera *)data;
513                                 AnimData *adt= ca->adt;
514                                 
515                                 ale->flag= FILTER_CAM_OBJD(ca);
516                                 
517                                 ale->key_data= (adt) ? adt->action : NULL;
518                                 ale->datatype= ALE_ACT;
519                         }
520                                 break;
521                         case ANIMTYPE_DSCUR:
522                         {
523                                 Curve *cu= (Curve *)data;
524                                 AnimData *adt= cu->adt;
525                                 
526                                 ale->flag= FILTER_CUR_OBJD(cu);
527                                 
528                                 ale->key_data= (adt) ? adt->action : NULL;
529                                 ale->datatype= ALE_ACT;
530                         }
531                                 break;
532                         case ANIMTYPE_DSSKEY:
533                         {
534                                 Key *key= (Key *)data;
535                                 AnimData *adt= key->adt;
536                                 
537                                 ale->flag= FILTER_SKE_OBJD(key); 
538                                 
539                                 ale->key_data= (adt) ? adt->action : NULL;
540                                 ale->datatype= ALE_ACT;
541                         }
542                                 break;
543                         case ANIMTYPE_DSWOR:
544                         {
545                                 World *wo= (World *)data;
546                                 AnimData *adt= wo->adt;
547                                 
548                                 ale->flag= FILTER_WOR_SCED(wo); 
549                                 
550                                 ale->key_data= (adt) ? adt->action : NULL;
551                                 ale->datatype= ALE_ACT;
552                         }
553                                 break;
554                                 
555                         case ANIMTYPE_GROUP:
556                         {
557                                 bActionGroup *agrp= (bActionGroup *)data;
558                                 
559                                 ale->flag= agrp->flag;
560                                 
561                                 ale->key_data= NULL;
562                                 ale->datatype= ALE_GROUP;
563                         }
564                                 break;
565                         case ANIMTYPE_FCURVE:
566                         {
567                                 FCurve *fcu= (FCurve *)data;
568                                 
569                                 ale->flag= fcu->flag;
570                                 
571                                 ale->key_data= fcu;
572                                 ale->datatype= ALE_FCURVE;
573                         }
574                                 break;
575                         
576                         case ANIMTYPE_GPLAYER:
577                         {
578                                 bGPDlayer *gpl= (bGPDlayer *)data;
579                                 
580                                 ale->flag= gpl->flag;
581                                 
582                                 ale->key_data= NULL;
583                                 ale->datatype= ALE_GPFRAME;
584                         }
585                                 break;
586                                 
587                         case ANIMTYPE_NLATRACK:
588                         {
589                                 NlaTrack *nlt= (NlaTrack *)data;
590                                 
591                                 ale->flag= nlt->flag;
592                                 
593                                         // XXX or should this be done some other way?
594                                 ale->key_data= &nlt->strips;
595                                 ale->datatype= ALE_NLASTRIP;
596                         }
597                                 break;
598                         case ANIMTYPE_NLAACTION:
599                         {
600                                 /* nothing to include for now... nothing editable from NLA-perspective here */
601                                 ale->key_data= NULL;
602                                 ale->datatype= ALE_NONE;
603                         }
604                                 break;
605                 }
606         }
607         
608         /* return created datatype */
609         return ale;
610 }
611  
612 /* ----------------------------------------- */
613
614
615 static int animdata_filter_fcurves (ListBase *anim_data, FCurve *first, bActionGroup *grp, void *owner, short ownertype, int filter_mode, ID *owner_id)
616 {
617         bAnimListElem *ale = NULL;
618         FCurve *fcu;
619         int items = 0;
620         
621         /* loop over F-Curves - assume that the caller of this has already checked that these should be included 
622          * NOTE: we need to check if the F-Curves belong to the same group, as this gets called for groups too...
623          */
624         for (fcu= first; ((fcu) && (fcu->grp==grp)); fcu= fcu->next) {
625                 /* only include if visible (Graph Editor check, not channels check) */
626                 if (!(filter_mode & ANIMFILTER_CURVEVISIBLE) || (fcu->flag & FCURVE_VISIBLE)) {
627                         /* only work with this channel and its subchannels if it is editable */
628                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_FCU(fcu)) {
629                                 /* only include this curve if selected in a way consistent with the filtering requirements */
630                                 if ( ANIMCHANNEL_SELOK(SEL_FCU(fcu)) ) {
631                                         /* only include if this curve is active */
632                                         if (!(filter_mode & ANIMFILTER_ACTIVE) || (fcu->flag & FCURVE_ACTIVE)) {
633                                                 ale= make_new_animlistelem(fcu, ANIMTYPE_FCURVE, owner, ownertype, owner_id);
634                                                 
635                                                 if (ale) {
636                                                         BLI_addtail(anim_data, ale);
637                                                         items++;
638                                                 }
639                                         }
640                                 }
641                         }
642                 }
643         }
644         
645         /* return the number of items added to the list */
646         return items;
647 }
648
649 static int animdata_filter_action (ListBase *anim_data, bAction *act, int filter_mode, void *owner, short ownertype, ID *owner_id)
650 {
651         bAnimListElem *ale=NULL;
652         bActionGroup *agrp;
653         FCurve *lastchan=NULL;
654         int items = 0;
655         
656         /* loop over groups */
657         //       XXX in future, we need to be prepared for nestled groups...
658         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
659                 /* add this group as a channel first */
660                 if ((filter_mode & ANIMFILTER_CHANNELS) || !(filter_mode & ANIMFILTER_CURVESONLY)) {
661                         /* check if filtering by selection */
662                         if ( ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) ) {
663                                 ale= make_new_animlistelem(agrp, ANIMTYPE_GROUP, NULL, ANIMTYPE_NONE, owner_id);
664                                 if (ale) {
665                                         BLI_addtail(anim_data, ale);
666                                         items++;
667                                 }
668                         }
669                 }
670                 
671                 /* store reference to last channel of group */
672                 if (agrp->channels.last) 
673                         lastchan= agrp->channels.last;
674                 
675                 
676                 /* there are some situations, where only the channels of the action group should get considered */
677                 if (!(filter_mode & ANIMFILTER_ACTGROUPED) || (agrp->flag & AGRP_ACTIVE)) {
678                         /* filters here are a bit convoulted...
679                          *      - groups show a "summary" of keyframes beside their name which must accessable for tools which handle keyframes
680                          *      - groups can be collapsed (and those tools which are only interested in channels rely on knowing that group is closed)
681                          *
682                          * cases when we should include F-Curves inside group:
683                          *      - we don't care about visibility
684                          *      - group is expanded
685                          *      - we just need the F-Curves present
686                          */
687                         if ( (!(filter_mode & ANIMFILTER_VISIBLE) || EXPANDED_AGRP(agrp)) || (filter_mode & ANIMFILTER_CURVESONLY) ) 
688                         {
689                                 /* for the Graph Editor, curves may be set to not be visible in the view to lessen clutter,
690                                  * but to do this, we need to check that the group doesn't have it's not-visible flag set preventing 
691                                  * all its sub-curves to be shown
692                                  */
693                                 if ( !(filter_mode & ANIMFILTER_CURVEVISIBLE) || !(agrp->flag & AGRP_NOTVISIBLE) )
694                                 {
695                                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_AGRP(agrp)) {
696                                                 // XXX the 'owner' info here needs review...
697                                                 items += animdata_filter_fcurves(anim_data, agrp->channels.first, agrp, owner, ownertype, filter_mode, owner_id);
698                                                 
699                                                 /* remove group from filtered list if last element is group 
700                                                  * (i.e. only if group had channels, which were all hidden)
701                                                  */
702                                                 // XXX this is really hacky... it should be fixed in a much more elegant way!
703                                                 if ( (ale) && (anim_data->last == ale) && 
704                                                          (ale->data == agrp) && (agrp->channels.first) ) 
705                                                 {
706                                                         BLI_freelinkN(anim_data, ale);
707                                                         items--;
708                                                 }
709                                         }
710                                 }
711                         }
712                 }
713         }
714         
715         /* loop over un-grouped F-Curves (only if we're not only considering those channels in the animive group) */
716         if (!(filter_mode & ANIMFILTER_ACTGROUPED))  {
717                 // XXX the 'owner' info here needs review...
718                 items += animdata_filter_fcurves(anim_data, (lastchan)?(lastchan->next):(act->curves.first), NULL, owner, ownertype, filter_mode, owner_id);
719         }
720         
721         /* return the number of items added to the list */
722         return items;
723 }
724
725 /* Include NLA-Data for NLA-Editor:
726  *      - when ANIMFILTER_CHANNELS is used, that means we should be filtering the list for display
727  *        Although the evaluation order is from the first track to the last and then apply the Action on top,
728  *        we present this in the UI as the Active Action followed by the last track to the first so that we 
729  *        get the evaluation order presented as per a stack.
730  *      - for normal filtering (i.e. for editing), we only need the NLA-tracks but they can be in 'normal' evaluation
731  *        order, i.e. first to last. Otherwise, some tools may get screwed up.
732  */
733 static int animdata_filter_nla (ListBase *anim_data, AnimData *adt, int filter_mode, void *owner, short ownertype, ID *owner_id)
734 {
735         bAnimListElem *ale;
736         NlaTrack *nlt;
737         NlaTrack *first=NULL, *next=NULL;
738         int items = 0;
739         
740         /* if showing channels, include active action */
741         if (filter_mode & ANIMFILTER_CHANNELS) {
742                 /* there isn't really anything editable here, so skip if need editable */
743                 // TODO: currently, selection isn't checked since it doesn't matter
744                 if ((filter_mode & ANIMFILTER_FOREDIT) == 0) { 
745                         /* just add the action track now (this MUST appear for drawing)
746                          *      - as AnimData may not have an action, we pass a dummy pointer just to get the list elem created, then
747                          *        overwrite this with the real value - REVIEW THIS...
748                          */
749                         ale= make_new_animlistelem((void *)(&adt->action), ANIMTYPE_NLAACTION, owner, ownertype, owner_id);
750                         ale->data= (adt->action) ? adt->action : NULL;
751                                 
752                         if (ale) {
753                                 BLI_addtail(anim_data, ale);
754                                 items++;
755                         }
756                 }
757                 
758                 /* first track to include will be the last one if we're filtering by channels */
759                 first= adt->nla_tracks.last;
760         }
761         else {
762                 /* first track to include will the the first one (as per normal) */
763                 first= adt->nla_tracks.first;
764         }
765         
766         /* loop over NLA Tracks - assume that the caller of this has already checked that these should be included */
767         for (nlt= first; nlt; nlt= next) {
768                 /* 'next' NLA-Track to use depends on whether we're filtering for drawing or not */
769                 if (filter_mode & ANIMFILTER_CHANNELS) 
770                         next= nlt->prev;
771                 else
772                         next= nlt->next;
773                 
774                 /* if we're in NLA-tweakmode, don't show this track if it was disabled (due to tweaking) for now 
775                  *      - active track should still get shown though (even though it has disabled flag set)
776                  */
777                 // FIXME: the channels after should still get drawn, just 'differently', and after an active-action channel
778                 if ((adt->flag & ADT_NLA_EDIT_ON) && (nlt->flag & NLATRACK_DISABLED) && !(nlt->flag & NLATRACK_ACTIVE))
779                         continue;
780                 
781                 /* only work with this channel and its subchannels if it is editable */
782                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_NLT(nlt)) {
783                         /* only include this track if selected in a way consistent with the filtering requirements */
784                         if ( ANIMCHANNEL_SELOK(SEL_NLT(nlt)) ) {
785                                 /* only include if this track is active */
786                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (nlt->flag & NLATRACK_ACTIVE)) {
787                                         ale= make_new_animlistelem(nlt, ANIMTYPE_NLATRACK, owner, ownertype, owner_id);
788                                                 
789                                         if (ale) {
790                                                 BLI_addtail(anim_data, ale);
791                                                 items++;
792                                         }
793                                 }
794                         }
795                 }
796         }
797         
798         /* return the number of items added to the list */
799         return items;
800 }
801
802 static int animdata_filter_shapekey (ListBase *anim_data, Key *key, int filter_mode, void *owner, short ownertype, ID *owner_id)
803 {
804         bAnimListElem *ale;
805         KeyBlock *kb;
806         //FCurve *fcu;
807         int i, items=0;
808         
809         /* are we filtering for display or editing */
810         if (filter_mode & ANIMFILTER_CHANNELS) {
811                 /* for display - loop over shapekeys, adding ipo-curve references where needed */
812                 kb= key->block.first;
813                 
814                 /* loop through possible shapekeys, manually creating entries */
815                 for (i= 1; i < key->totkey; i++) {
816                         ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
817                         kb = kb->next; /* do this even on the first try, as the first is 'Basis' (which doesn't get included) */
818                         
819                         ale->data= kb;
820                         ale->type= ANIMTYPE_SHAPEKEY; /* 'abused' usage of this type */
821                         ale->owner= key;
822                         ale->ownertype= ANIMTYPE_SHAPEKEY;
823                         ale->datatype= ALE_NONE;
824                         ale->index = i;
825                         
826 #if 0 // XXX fixme... old system
827                         if (key->ipo) {
828                                 for (icu= key->ipo->curve.first; icu; icu=icu->next) {
829                                         if (icu->adrcode == i) {
830                                                 ale->key_data= icu;
831                                                 ale->datatype= ALE_ICU;
832                                                 break;
833                                         }
834                                 }
835                         }
836 #endif // XXX fixme... old system
837                         
838                         ale->id= owner_id;
839                         
840                         BLI_addtail(anim_data, ale);
841                         items++;
842                 }
843         }
844         else {
845 #if 0 // XXX fixme... old system
846                 /* loop over ipo curves if present - for editing */
847                 if (key->ipo) {
848                         if (filter_mode & ANIMFILTER_IPOKEYS) {
849                                 ale= make_new_animlistelem(key->ipo, ANIMTYPE_IPO, key, ANIMTYPE_SHAPEKEY);
850                                 if (ale) {
851                                         if (owned) ale->id= owner;
852                                         BLI_addtail(anim_data, ale);
853                                         items++;
854                                 }
855                         }
856                         else {
857                                 items += animdata_filter_ipocurves(anim_data, key->ipo, filter_mode, key, ANIMTYPE_SHAPEKEY, (owned)?(owner):(NULL));
858                         }
859                 }
860 #endif // XXX fixme... old system
861         }
862         
863         /* return the number of items added to the list */
864         return items;
865 }
866  
867 #if 0
868 // FIXME: switch this to use the bDopeSheet...
869 static int animdata_filter_gpencil (ListBase *anim_data, bScreen *sc, int filter_mode)
870 {
871         bAnimListElem *ale;
872         ScrArea *sa, *curarea;
873         bGPdata *gpd;
874         bGPDlayer *gpl;
875         int items = 0;
876         
877         /* check if filtering types are appropriate */
878         {
879                 /* special hack for fullscreen area (which must be this one then):
880                  *      - we use the curarea->full as screen to get spaces from, since the
881                  *        old (pre-fullscreen) screen was stored there...
882                  *      - this is needed as all data would otherwise disappear
883                  */
884                 // XXX need to get new alternative for curarea
885                 if ((curarea->full) && (curarea->spacetype==SPACE_ACTION))
886                         sc= curarea->full;
887                 
888                 /* loop over spaces in current screen, finding gpd blocks (could be slow!) */
889                 for (sa= sc->areabase.first; sa; sa= sa->next) {
890                         /* try to get gp data */
891                         // XXX need to put back grease pencil api...
892                         gpd= gpencil_data_getactive(sa);
893                         if (gpd == NULL) continue;
894                         
895                         /* add gpd as channel too (if for drawing, and it has layers) */
896                         if ((filter_mode & ANIMFILTER_CHANNELS) && (gpd->layers.first)) {
897                                 /* add to list */
898                                 ale= make_new_animlistelem(gpd, ANIMTYPE_GPDATABLOCK, sa, ANIMTYPE_SPECIALDATA);
899                                 if (ale) {
900                                         BLI_addtail(anim_data, ale);
901                                         items++;
902                                 }
903                         }
904                         
905                         /* only add layers if they will be visible (if drawing channels) */
906                         if ( !(filter_mode & ANIMFILTER_VISIBLE) || (EXPANDED_GPD(gpd)) ) {
907                                 /* loop over layers as the conditions are acceptable */
908                                 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
909                                         /* only if selected */
910                                         if ( ANIMCHANNEL_SELOK(SEL_GPL(gpl)) ) {
911                                                 /* only if editable */
912                                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
913                                                         /* add to list */
914                                                         ale= make_new_animlistelem(gpl, ANIMTYPE_GPLAYER, gpd, ANIMTYPE_GPDATABLOCK);
915                                                         if (ale) {
916                                                                 BLI_addtail(anim_data, ale);
917                                                                 items++;
918                                                         }
919                                                 }
920                                         }
921                                 }
922                         }
923                 }
924         }
925         
926         /* return the number of items added to the list */
927         return items;
928 }
929 #endif 
930
931
932 static int animdata_filter_dopesheet_mats (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
933 {
934         ListBase mats = {NULL, NULL};
935         LinkData *ld;
936         
937         bAnimListElem *ale=NULL;
938         Object *ob= base->object;
939         int items=0, a=0;
940         
941         /* firstly check that we actuallly have some materials, by gathering all materials in a temp list */
942         for (a=0; a < ob->totcol; a++) {
943                 Material *ma= give_current_material(ob, a);
944                 short ok = 0;
945                 
946                 /* for now, if no material returned, skip (this shouldn't confuse the user I hope) */
947                 if (ELEM(NULL, ma, ma->adt)) 
948                         continue;
949                 
950                 /* check if ok */
951                 ANIMDATA_FILTER_CASES(ma, 
952                         { /* AnimData blocks - do nothing... */ },
953                         ok=1;, 
954                         ok=1;, 
955                         ok=1;)
956                 if (ok == 0) continue;
957                 
958                 /* make a temp list elem for this */
959                 ld= MEM_callocN(sizeof(LinkData), "DopeSheet-MaterialCache");
960                 ld->data= ma;
961                 BLI_addtail(&mats, ld);
962         }
963         
964         /* if there were no channels found, no need to carry on */
965         if (mats.first == NULL)
966                 return 0;
967         
968         /* include materials-expand widget? */
969         if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
970                 ale= make_new_animlistelem(ob, ANIMTYPE_FILLMATD, base, ANIMTYPE_OBJECT, (ID *)ob);
971                 if (ale) {
972                         BLI_addtail(anim_data, ale);
973                         items++;
974                 }
975         }
976         
977         /* add materials? */
978         if (FILTER_MAT_OBJC(ob) || (filter_mode & ANIMFILTER_CURVESONLY)) {
979                 /* for each material in cache, add channels  */
980                 for (ld= mats.first; ld; ld= ld->next) {
981                         Material *ma= (Material *)ld->data;
982                         
983                         /* include material-expand widget? */
984                         // hmm... do we need to store the index of this material in the array anywhere?
985                         if (filter_mode & ANIMFILTER_CHANNELS) {
986                                 ale= make_new_animlistelem(ma, ANIMTYPE_DSMAT, base, ANIMTYPE_OBJECT, (ID *)ma);
987                                 if (ale) {
988                                         BLI_addtail(anim_data, ale);
989                                         items++;
990                                 }
991                         }
992                         
993                         /* add material's animation data */
994                         if (FILTER_MAT_OBJD(ma) || (filter_mode & ANIMFILTER_CURVESONLY)) {
995                                 ANIMDATA_FILTER_CASES(ma, 
996                                         { /* AnimData blocks - do nothing... */ },
997                                         items += animdata_filter_nla(anim_data, ma->adt, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);, 
998                                         items += animdata_filter_fcurves(anim_data, ma->adt->drivers.first, NULL, ma, ANIMTYPE_DSMAT, filter_mode, (ID *)ma);, 
999                                         items += animdata_filter_action(anim_data, ma->adt->action, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);)
1000                         }
1001                 }
1002         }
1003         
1004         /* free cache */
1005         BLI_freelistN(&mats);
1006         
1007         /* return the number of items added to the list */
1008         return items;
1009 }
1010
1011 static int animdata_filter_dopesheet_obdata (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1012 {
1013         bAnimListElem *ale=NULL;
1014         Object *ob= base->object;
1015         IdAdtTemplate *iat= ob->data;
1016         AnimData *adt= iat->adt;
1017         short type=0, expanded=0;
1018         int items= 0;
1019         
1020         /* get settings based on data type */
1021         switch (ob->type) {
1022                 case OB_CAMERA: /* ------- Camera ------------ */
1023                 {
1024                         Camera *ca= (Camera *)ob->data;
1025                         
1026                         type= ANIMTYPE_DSCAM;
1027                         expanded= FILTER_CAM_OBJD(ca);
1028                 }
1029                         break;
1030                 case OB_LAMP: /* ---------- Lamp ----------- */
1031                 {
1032                         Lamp *la= (Lamp *)ob->data;
1033                         
1034                         type= ANIMTYPE_DSLAM;
1035                         expanded= FILTER_LAM_OBJD(la);
1036                 }
1037                         break;
1038                 case OB_CURVE: /* ------- Curve ---------- */
1039                 {
1040                         Curve *cu= (Curve *)ob->data;
1041                         
1042                         type= ANIMTYPE_DSCUR;
1043                         expanded= FILTER_CUR_OBJD(cu);
1044                 }
1045                         break;
1046         }
1047         
1048         /* special exception for drivers instead of action */
1049         if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
1050                 expanded= EXPANDED_DRVD(adt);
1051         
1052         /* include data-expand widget? */
1053         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {               
1054                 ale= make_new_animlistelem(iat, type, base, ANIMTYPE_OBJECT, (ID *)iat);
1055                 if (ale) BLI_addtail(anim_data, ale);
1056         }
1057         
1058         /* add object-data animation channels? */
1059         if ((expanded) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1060                 /* filtering for channels - nla, drivers, keyframes */
1061                 ANIMDATA_FILTER_CASES(iat, 
1062                         { /* AnimData blocks - do nothing... */ },
1063                         items+= animdata_filter_nla(anim_data, iat->adt, filter_mode, iat, type, (ID *)iat);,
1064                         items+= animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, iat, type, filter_mode, (ID *)iat);, 
1065                         items += animdata_filter_action(anim_data, iat->adt->action, filter_mode, iat, type, (ID *)iat);)
1066         }
1067         
1068         /* return the number of items added to the list */
1069         return items;
1070 }
1071
1072 static int animdata_filter_dopesheet_ob (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1073 {
1074         bAnimListElem *ale=NULL;
1075         AnimData *adt = NULL;
1076         Object *ob= base->object;
1077         Key *key= ob_get_key(ob);
1078         short obdata_ok = 0;
1079         int items = 0;
1080         
1081         /* add this object as a channel first */
1082         if ((filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) == 0) {
1083                 /* check if filtering by selection */
1084                 if ANIMCHANNEL_SELOK((base->flag & SELECT)) {
1085                         ale= make_new_animlistelem(base, ANIMTYPE_OBJECT, NULL, ANIMTYPE_NONE, NULL);
1086                         if (ale) {
1087                                 BLI_addtail(anim_data, ale);
1088                                 items++;
1089                         }
1090                 }
1091         }
1092         
1093         /* if collapsed, don't go any further (unless adding keyframes only) */
1094         if ( (EXPANDED_OBJC(ob) == 0) && !(filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) )
1095                 return items;
1096         
1097         /* Action, Drivers, or NLA */
1098         if (ob->adt) {
1099                 adt= ob->adt;
1100                 ANIMDATA_FILTER_CASES(ob,
1101                         { /* AnimData blocks - do nothing... */ },
1102                         { /* nla */
1103                                 /* add NLA tracks */
1104                                 items += animdata_filter_nla(anim_data, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1105                         },
1106                         { /* drivers */
1107                                 /* include drivers-expand widget? */
1108                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1109                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, base, ANIMTYPE_OBJECT, (ID *)ob);
1110                                         if (ale) {
1111                                                 BLI_addtail(anim_data, ale);
1112                                                 items++;
1113                                         }
1114                                 }
1115                                 
1116                                 /* add F-Curve channels (drivers are F-Curves) */
1117                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1118                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1119                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, ob, ANIMTYPE_OBJECT, filter_mode, (ID *)ob);
1120                                 }
1121                         },
1122                         { /* action (keyframes) */
1123                                 /* include action-expand widget? */
1124                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1125                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, base, ANIMTYPE_OBJECT, (ID *)ob);
1126                                         if (ale) {
1127                                                 BLI_addtail(anim_data, ale);
1128                                                 items++;
1129                                         }
1130                                 }
1131                                 
1132                                 /* add F-Curve channels? */
1133                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1134                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1135                                         items += animdata_filter_action(anim_data, adt->action, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob); 
1136                                 }
1137                         }
1138                 );
1139         }
1140         
1141         
1142         /* ShapeKeys? */
1143         if ((key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
1144                 adt= key->adt;
1145                 ANIMDATA_FILTER_CASES(key,
1146                         { /* AnimData blocks - do nothing... */ },
1147                         { /* nla */
1148                                 /* add NLA tracks */
1149                                 items += animdata_filter_nla(anim_data, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1150                         },
1151                         { /* drivers */
1152                                 /* include shapekey-expand widget? */
1153                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1154                                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1155                                         if (ale) {
1156                                                 BLI_addtail(anim_data, ale);
1157                                                 items++;
1158                                         }
1159                                 }
1160                                 
1161                                 /* add channels */
1162                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1163                                         items += animdata_filter_shapekey(anim_data, key, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1164                                 }
1165                         },
1166                         { /* action (keyframes) */
1167                                 /* include shapekey-expand widget? */
1168                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1169                                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1170                                         if (ale) {
1171                                                 BLI_addtail(anim_data, ale);
1172                                                 items++;
1173                                         }
1174                                 }
1175                                 
1176                                 /* add channels */
1177                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1178                                         items += animdata_filter_shapekey(anim_data, key, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1179                                 }
1180                         }
1181                 );
1182         }
1183
1184         /* Materials? */
1185         if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT))
1186                 items += animdata_filter_dopesheet_mats(anim_data, ads, base, filter_mode);
1187         
1188         /* Object Data */
1189         switch (ob->type) {
1190                 case OB_CAMERA: /* ------- Camera ------------ */
1191                 {
1192                         Camera *ca= (Camera *)ob->data;
1193                         
1194                         if ((ads->filterflag & ADS_FILTER_NOCAM) == 0) {
1195                                 ANIMDATA_FILTER_CASES(ca,
1196                                         { /* AnimData blocks - do nothing... */ },
1197                                         obdata_ok= 1;,
1198                                         obdata_ok= 1;,
1199                                         obdata_ok= 1;)
1200                         }
1201                 }
1202                         break;
1203                 case OB_LAMP: /* ---------- Lamp ----------- */
1204                 {
1205                         Lamp *la= (Lamp *)ob->data;
1206                         
1207                         if ((ads->filterflag & ADS_FILTER_NOLAM) == 0) {
1208                                 ANIMDATA_FILTER_CASES(la,
1209                                         { /* AnimData blocks - do nothing... */ },
1210                                         obdata_ok= 1;,
1211                                         obdata_ok= 1;,
1212                                         obdata_ok= 1;)
1213                         }
1214                 }
1215                         break;
1216                 case OB_CURVE: /* ------- Curve ---------- */
1217                 {
1218                         Curve *cu= (Curve *)ob->data;
1219                         
1220                         if ((ads->filterflag & ADS_FILTER_NOCUR) == 0) {
1221                                 ANIMDATA_FILTER_CASES(cu,
1222                                         { /* AnimData blocks - do nothing... */ },
1223                                         obdata_ok= 1;,
1224                                         obdata_ok= 1;,
1225                                         obdata_ok= 1;)
1226                         }
1227                 }
1228                         break;
1229         }
1230         if (obdata_ok) 
1231                 items += animdata_filter_dopesheet_obdata(anim_data, ads, base, filter_mode);
1232         
1233         /* return the number of items added to the list */
1234         return items;
1235 }       
1236
1237 static int animdata_filter_dopesheet_scene (ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
1238 {
1239         World *wo= sce->world;
1240         AnimData *adt= NULL;
1241         bAnimListElem *ale;
1242         int items = 0;
1243         
1244         /* add scene as a channel first (even if we aren't showing scenes we still need to show the scene's sub-data */
1245         if ((filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) == 0) {
1246                 /* check if filtering by selection */
1247                 if (ANIMCHANNEL_SELOK( (sce->flag & SCE_DS_SELECTED) )) {
1248                         ale= make_new_animlistelem(sce, ANIMTYPE_SCENE, NULL, ANIMTYPE_NONE, NULL);
1249                         if (ale) {
1250                                 BLI_addtail(anim_data, ale);
1251                                 items++;
1252                         }
1253                 }
1254         }
1255         
1256         /* if collapsed, don't go any further (unless adding keyframes only) */
1257         if ( (EXPANDED_SCEC(sce) == 0) && !(filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) )
1258                 return items;
1259                 
1260         /* Action, Drivers, or NLA  for Scene */
1261         if ((ads->filterflag & ADS_FILTER_NOSCE) == 0) {
1262                 adt= sce->adt;
1263                 ANIMDATA_FILTER_CASES(sce,
1264                         { /* AnimData blocks - do nothing... */ },
1265                         { /* nla */
1266                                 /* add NLA tracks */
1267                                 items += animdata_filter_nla(anim_data, adt, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce);
1268                         },
1269                         { /* drivers */
1270                                 /* include drivers-expand widget? */
1271                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1272                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, sce, ANIMTYPE_SCENE, (ID *)sce);
1273                                         if (ale) {
1274                                                 BLI_addtail(anim_data, ale);
1275                                                 items++;
1276                                         }
1277                                 }
1278                                 
1279                                 /* add F-Curve channels (drivers are F-Curves) */
1280                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1281                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, sce, ANIMTYPE_SCENE, filter_mode, (ID *)sce);
1282                                 }
1283                         },
1284                         { /* action */
1285                                 /* include action-expand widget? */
1286                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1287                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, sce, ANIMTYPE_SCENE, (ID *)sce);
1288                                         if (ale) {
1289                                                 BLI_addtail(anim_data, ale);
1290                                                 items++;
1291                                         }
1292                                 }
1293                                 
1294                                 /* add F-Curve channels? */
1295                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1296                                         items += animdata_filter_action(anim_data, adt->action, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce); 
1297                                 }
1298                         }
1299                 )
1300         }
1301         
1302         /* world */
1303         if ((wo && wo->adt) && !(ads->filterflag & ADS_FILTER_NOWOR)) {
1304                 /* Action, Drivers, or NLA for World */
1305                 adt= wo->adt;
1306                 ANIMDATA_FILTER_CASES(wo,
1307                         { /* AnimData blocks - do nothing... */ },
1308                         { /* nla */
1309                                 /* add NLA tracks */
1310                                 items += animdata_filter_nla(anim_data, adt, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo);
1311                         },
1312                         { /* drivers */
1313                                 /* include world-expand widget? */
1314                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1315                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)wo);
1316                                         if (ale) {
1317                                                 BLI_addtail(anim_data, ale);
1318                                                 items++;
1319                                         }
1320                                 }
1321                                 
1322                                 /* add F-Curve channels (drivers are F-Curves) */
1323                                 if (FILTER_WOR_SCED(wo)/*EXPANDED_DRVD(adt)*/ || !(filter_mode & ANIMFILTER_CHANNELS)) {
1324                                         // XXX owner info is messed up now...
1325                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, wo, ANIMTYPE_DSWOR, filter_mode, (ID *)wo);
1326                                 }
1327                         },
1328                         { /* action */
1329                                 /* include world-expand widget? */
1330                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1331                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)sce);
1332                                         if (ale) {
1333                                                 BLI_addtail(anim_data, ale);
1334                                                 items++;
1335                                         }
1336                                 }
1337                                 
1338                                 /* add channels */
1339                                 if (FILTER_WOR_SCED(wo) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1340                                         items += animdata_filter_action(anim_data, adt->action, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo); 
1341                                 }
1342                         }
1343                 )
1344         }
1345         
1346         /* return the number of items added to the list */
1347         return items;
1348 }
1349
1350 // 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)
1351 static int animdata_filter_dopesheet (ListBase *anim_data, bDopeSheet *ads, int filter_mode)
1352 {
1353         Scene *sce= (Scene *)ads->source;
1354         Base *base;
1355         bAnimListElem *ale;
1356         int items = 0;
1357         
1358         /* check that we do indeed have a scene */
1359         if ((ads->source == NULL) || (GS(ads->source->name)!=ID_SCE)) {
1360                 printf("DopeSheet Error: Not scene! \n");
1361                 return 0;
1362         }
1363         
1364         /* scene-linked animation */
1365         // TODO: sequencer, composite nodes - are we to include those here too?
1366         {
1367                 short sceOk= 0, worOk= 0;
1368                 
1369                 /* check filtering-flags if ok */
1370                 ANIMDATA_FILTER_CASES(sce, 
1371                         {
1372                                 /* for the special AnimData blocks only case, we only need to add
1373                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1374                                  */
1375                                 ANIMDATA_ADD_ANIMDATA(sce);
1376                                 sceOk=0;
1377                         },
1378                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);, 
1379                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);, 
1380                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);)
1381                 if (sce->world) {
1382                         ANIMDATA_FILTER_CASES(sce->world, 
1383                                 {
1384                                         /* for the special AnimData blocks only case, we only need to add
1385                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1386                                          */
1387                                         ANIMDATA_ADD_ANIMDATA(sce->world);
1388                                         worOk=0;
1389                                 },
1390                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);, 
1391                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);, 
1392                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);)
1393                 }
1394                 
1395                 /* check if not all bad (i.e. so there is something to show) */
1396                 if ( !(!sceOk && !worOk) ) {
1397                         /* add scene data to the list of filtered channels */
1398                         items += animdata_filter_dopesheet_scene(anim_data, ads, sce, filter_mode);
1399                 }
1400         }
1401         
1402         
1403         /* loop over all bases in the scene */
1404         for (base= sce->base.first; base; base= base->next) {
1405                 /* check if there's an object (all the relevant checks are done in the ob-function) */
1406                 if (base->object) {
1407                         Object *ob= base->object;
1408                         Key *key= ob_get_key(ob);
1409                         short actOk=1, keyOk=1, dataOk=1, matOk=1;
1410                         
1411                         /* firstly, check if object can be included, by the following fanimors:
1412                          *      - if only visible, must check for layer and also viewport visibility
1413                          *      - if only selected, must check if object is selected 
1414                          *      - there must be animation data to edit
1415                          */
1416                         // TODO: if cache is implemented, just check name here, and then 
1417                         if (filter_mode & ANIMFILTER_VISIBLE) {
1418                                 /* layer visibility - we check both object and base, since these may not be in sync yet */
1419                                 if ((sce->lay & (ob->lay|base->lay))==0) continue;
1420                                 
1421                                 /* outliner restrict-flag */
1422                                 if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
1423                         }
1424                         
1425                         /* additionally, dopesheet filtering also affects what objects to consider */
1426                         if (ads->filterflag) {
1427                                 /* check selection and object type filters */
1428                                 if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) || (base == sce->basact)) )  {
1429                                         /* only selected should be shown */
1430                                         continue;
1431                                 }
1432                                 
1433                                 /* check filters for datatypes */
1434                                         /* object */
1435                                 actOk= 0;
1436                                 keyOk= 0;
1437                                 ANIMDATA_FILTER_CASES(ob, 
1438                                         {
1439                                                 /* for the special AnimData blocks only case, we only need to add
1440                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1441                                                  */
1442                                                 ANIMDATA_ADD_ANIMDATA(ob);
1443                                                 actOk=0;
1444                                         },
1445                                         actOk= 1;, 
1446                                         actOk= 1;, 
1447                                         actOk= 1;)
1448                                 if (key) {
1449                                         /* shapekeys */
1450                                         ANIMDATA_FILTER_CASES(key, 
1451                                                 {
1452                                                         /* for the special AnimData blocks only case, we only need to add
1453                                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1454                                                          */
1455                                                         ANIMDATA_ADD_ANIMDATA(key);
1456                                                         keyOk=0;
1457                                                 },
1458                                                 keyOk= 1;, 
1459                                                 keyOk= 1;, 
1460                                                 keyOk= 1;)
1461                                 }
1462                                 
1463                                 /* materials - only for geometric types */
1464                                 matOk= 0; /* by default, not ok... */
1465                                 if ( !(ads->filterflag & ADS_FILTER_NOMAT) && (ob->totcol) && 
1466                                          ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) ) 
1467                                 {
1468                                         int a;
1469                                         
1470                                         /* firstly check that we actuallly have some materials */
1471                                         for (a=0; a < ob->totcol; a++) {
1472                                                 Material *ma= give_current_material(ob, a);
1473                                                 
1474                                                 if (ma) {
1475                                                         /* if material has relevant animation data, break */
1476                                                         ANIMDATA_FILTER_CASES(ma, 
1477                                                                 {
1478                                                                         /* for the special AnimData blocks only case, we only need to add
1479                                                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1480                                                                          */
1481                                                                         ANIMDATA_ADD_ANIMDATA(ma);
1482                                                                         matOk=0;
1483                                                                 },
1484                                                                 matOk= 1;, 
1485                                                                 matOk= 1;, 
1486                                                                 matOk= 1;)
1487                                                 }
1488                                                         
1489                                                 if (matOk) 
1490                                                         break;
1491                                         }
1492                                 }
1493                                 
1494                                 /* data */
1495                                 switch (ob->type) {
1496                                         case OB_CAMERA: /* ------- Camera ------------ */
1497                                         {
1498                                                 Camera *ca= (Camera *)ob->data;
1499                                                 dataOk= 0;
1500                                                 ANIMDATA_FILTER_CASES(ca, 
1501                                                         if ((ads->filterflag & ADS_FILTER_NOCAM)==0) {
1502                                                                 /* for the special AnimData blocks only case, we only need to add
1503                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1504                                                                  */
1505                                                                 ANIMDATA_ADD_ANIMDATA(ca);
1506                                                                 dataOk=0;
1507                                                         },
1508                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);, 
1509                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);, 
1510                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);)
1511                                         }
1512                                                 break;
1513                                         case OB_LAMP: /* ---------- Lamp ----------- */
1514                                         {
1515                                                 Lamp *la= (Lamp *)ob->data;
1516                                                 dataOk= 0;
1517                                                 ANIMDATA_FILTER_CASES(la, 
1518                                                         if ((ads->filterflag & ADS_FILTER_NOLAM)==0) {
1519                                                                 /* for the special AnimData blocks only case, we only need to add
1520                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1521                                                                  */
1522                                                                 ANIMDATA_ADD_ANIMDATA(la);
1523                                                                 dataOk=0;
1524                                                         },
1525                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);, 
1526                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);, 
1527                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);)
1528                                         }
1529                                                 break;
1530                                         case OB_CURVE: /* ------- Curve ---------- */
1531                                         {
1532                                                 Curve *cu= (Curve *)ob->data;
1533                                                 dataOk= 0;
1534                                                 ANIMDATA_FILTER_CASES(cu, 
1535                                                         if ((ads->filterflag & ADS_FILTER_NOCUR)==0) {
1536                                                                 /* for the special AnimData blocks only case, we only need to add
1537                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1538                                                                  */
1539                                                                 ANIMDATA_ADD_ANIMDATA(cu);
1540                                                                 dataOk=0;
1541                                                         },
1542                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);, 
1543                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);, 
1544                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);)
1545                                         }
1546                                                 break;
1547                                         default: /* --- other --- */
1548                                                 dataOk= 0;
1549                                                 break;
1550                                 }
1551                                 
1552                                 /* check if all bad (i.e. nothing to show) */
1553                                 if (!actOk && !keyOk && !dataOk && !matOk)
1554                                         continue;
1555                         }
1556                         else {
1557                                 /* check data-types */
1558                                 actOk= ANIMDATA_HAS_KEYS(ob);
1559                                 keyOk= (key != NULL);
1560                                 
1561                                 /* materials - only for geometric types */
1562                                 matOk= 0; /* by default, not ok... */
1563                                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) && (ob->totcol)) 
1564                                 {
1565                                         int a;
1566                                         
1567                                         /* firstly check that we actuallly have some materials */
1568                                         for (a=0; a < ob->totcol; a++) {
1569                                                 Material *ma= give_current_material(ob, a);
1570                                                 
1571                                                 if ((ma) && ANIMDATA_HAS_KEYS(ma)) {
1572                                                         matOk= 1;
1573                                                         break;
1574                                                 }
1575                                         }
1576                                 }
1577                                 
1578                                 /* data */
1579                                 switch (ob->type) {
1580                                         case OB_CAMERA: /* ------- Camera ------------ */
1581                                         {
1582                                                 Camera *ca= (Camera *)ob->data;
1583                                                 dataOk= ANIMDATA_HAS_KEYS(ca);                                          
1584                                         }
1585                                                 break;
1586                                         case OB_LAMP: /* ---------- Lamp ----------- */
1587                                         {
1588                                                 Lamp *la= (Lamp *)ob->data;
1589                                                 dataOk= ANIMDATA_HAS_KEYS(la);  
1590                                         }
1591                                                 break;
1592                                         case OB_CURVE: /* -------- Curve ---------- */
1593                                         {
1594                                                 Curve *cu= (Curve *)ob->data;
1595                                                 dataOk= ANIMDATA_HAS_KEYS(cu);  
1596                                         }
1597                                                 break;
1598                                         default: /* --- other --- */
1599                                                 dataOk= 0;
1600                                                 break;
1601                                 }
1602                                 
1603                                 /* check if all bad (i.e. nothing to show) */
1604                                 if (!actOk && !keyOk && !dataOk && !matOk)
1605                                         continue;
1606                         }
1607                         
1608                         /* since we're still here, this object should be usable */
1609                         items += animdata_filter_dopesheet_ob(anim_data, ads, base, filter_mode);
1610                 }
1611         }
1612         
1613         /* return the number of items in the list */
1614         return items;
1615 }
1616
1617 /* ----------- Public API --------------- */
1618
1619 /* This function filters the active data source to leave only animation channels suitable for
1620  * usage by the caller. It will return the length of the list 
1621  * 
1622  *      *anim_data: is a pointer to a ListBase, to which the filtered animation channels
1623  *              will be placed for use.
1624  *      filter_mode: how should the data be filtered - bitmapping accessed flags
1625  */
1626 int ANIM_animdata_filter (bAnimContext *ac, ListBase *anim_data, int filter_mode, void *data, short datatype)
1627 {
1628         int items = 0;
1629         
1630         /* only filter data if there's somewhere to put it */
1631         if (data && anim_data) {
1632                 bAnimListElem *ale, *next;
1633                 Object *obact= (ac) ? ac->obact : NULL;
1634                 
1635                 /* firstly filter the data */
1636                 switch (datatype) {
1637                         case ANIMCONT_ACTION:
1638                                 items= animdata_filter_action(anim_data, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
1639                                 break;
1640                                 
1641                         case ANIMCONT_SHAPEKEY:
1642                                 items= animdata_filter_shapekey(anim_data, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
1643                                 break;
1644                                 
1645                         case ANIMCONT_GPENCIL:
1646                                 //items= animdata_filter_gpencil(anim_data, data, filter_mode);
1647                                 break;
1648                                 
1649                         case ANIMCONT_DOPESHEET:
1650                         case ANIMCONT_FCURVES:
1651                         case ANIMCONT_DRIVERS:
1652                         case ANIMCONT_NLA:
1653                                 items= animdata_filter_dopesheet(anim_data, data, filter_mode);
1654                                 break;
1655                 }
1656                         
1657                 /* remove any weedy entries */
1658                 // XXX this is weedy code!
1659                 for (ale= anim_data->first; ale; ale= next) {
1660                         next= ale->next;
1661                         
1662                         if (ale->type == ANIMTYPE_NONE) {
1663                                 items--;
1664                                 BLI_freelinkN(anim_data, ale);
1665                         }
1666                 }
1667         }
1668         
1669         /* return the number of items in the list */
1670         return items;
1671 }
1672
1673 /* ************************************************************ */