At last... this merge should finally do the trick!
[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're interested in keyframes, but not if they appear in selected channels
686                          */
687                         // XXX what was the selection check here for again?
688                         if ( (!(filter_mode & ANIMFILTER_VISIBLE) || EXPANDED_AGRP(agrp)) || 
689                                  ( /*ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) &&*/ (filter_mode & ANIMFILTER_CURVESONLY) ) ) 
690                         {
691                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_AGRP(agrp)) {
692                                         // XXX the 'owner' info here needs review...
693                                         items += animdata_filter_fcurves(anim_data, agrp->channels.first, agrp, owner, ownertype, filter_mode, owner_id);
694                                         
695                                         /* remove group from filtered list if last element is group 
696                                          * (i.e. only if group had channels, which were all hidden)
697                                          */
698                                         // XXX this is really hacky... it should be fixed in a much more elegant way!
699                                         if ( (ale) && (anim_data->last == ale) && 
700                                                  (ale->data == agrp) && (agrp->channels.first) ) 
701                                         {
702                                                 BLI_freelinkN(anim_data, ale);
703                                                 items--;
704                                         }
705                                 }
706                         }
707                 }
708         }
709         
710         /* loop over un-grouped F-Curves (only if we're not only considering those channels in the animive group) */
711         if (!(filter_mode & ANIMFILTER_ACTGROUPED))  {
712                 // XXX the 'owner' info here needs review...
713                 items += animdata_filter_fcurves(anim_data, (lastchan)?(lastchan->next):(act->curves.first), NULL, owner, ownertype, filter_mode, owner_id);
714         }
715         
716         /* return the number of items added to the list */
717         return items;
718 }
719
720 /* Include NLA-Data for NLA-Editor:
721  *      - when ANIMFILTER_CHANNELS is used, that means we should be filtering the list for display
722  *        Although the evaluation order is from the first track to the last and then apply the Action on top,
723  *        we present this in the UI as the Active Action followed by the last track to the first so that we 
724  *        get the evaluation order presented as per a stack.
725  *      - for normal filtering (i.e. for editing), we only need the NLA-tracks but they can be in 'normal' evaluation
726  *        order, i.e. first to last. Otherwise, some tools may get screwed up.
727  */
728 static int animdata_filter_nla (ListBase *anim_data, AnimData *adt, int filter_mode, void *owner, short ownertype, ID *owner_id)
729 {
730         bAnimListElem *ale;
731         NlaTrack *nlt;
732         NlaTrack *first=NULL, *next=NULL;
733         int items = 0;
734         
735         /* if showing channels, include active action */
736         if (filter_mode & ANIMFILTER_CHANNELS) {
737                 /* there isn't really anything editable here, so skip if need editable */
738                 // TODO: currently, selection isn't checked since it doesn't matter
739                 if ((filter_mode & ANIMFILTER_FOREDIT) == 0) { 
740                         /* just add the action track now (this MUST appear for drawing)
741                          *      - as AnimData may not have an action, we pass a dummy pointer just to get the list elem created, then
742                          *        overwrite this with the real value - REVIEW THIS...
743                          */
744                         ale= make_new_animlistelem((void *)(&adt->action), ANIMTYPE_NLAACTION, owner, ownertype, owner_id);
745                         ale->data= (adt->action) ? adt->action : NULL;
746                                 
747                         if (ale) {
748                                 BLI_addtail(anim_data, ale);
749                                 items++;
750                         }
751                 }
752                 
753                 /* first track to include will be the last one if we're filtering by channels */
754                 first= adt->nla_tracks.last;
755         }
756         else {
757                 /* first track to include will the the first one (as per normal) */
758                 first= adt->nla_tracks.first;
759         }
760         
761         /* loop over NLA Tracks - assume that the caller of this has already checked that these should be included */
762         for (nlt= first; nlt; nlt= next) {
763                 /* 'next' NLA-Track to use depends on whether we're filtering for drawing or not */
764                 if (filter_mode & ANIMFILTER_CHANNELS) 
765                         next= nlt->prev;
766                 else
767                         next= nlt->next;
768                 
769                 /* if we're in NLA-tweakmode, don't show this track if it was disabled (due to tweaking) for now 
770                  *      - active track should still get shown though (even though it has disabled flag set)
771                  */
772                 // FIXME: the channels after should still get drawn, just 'differently', and after an active-action channel
773                 if ((adt->flag & ADT_NLA_EDIT_ON) && (nlt->flag & NLATRACK_DISABLED) && !(nlt->flag & NLATRACK_ACTIVE))
774                         continue;
775                 
776                 /* only work with this channel and its subchannels if it is editable */
777                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_NLT(nlt)) {
778                         /* only include this track if selected in a way consistent with the filtering requirements */
779                         if ( ANIMCHANNEL_SELOK(SEL_NLT(nlt)) ) {
780                                 /* only include if this track is active */
781                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (nlt->flag & NLATRACK_ACTIVE)) {
782                                         ale= make_new_animlistelem(nlt, ANIMTYPE_NLATRACK, owner, ownertype, owner_id);
783                                                 
784                                         if (ale) {
785                                                 BLI_addtail(anim_data, ale);
786                                                 items++;
787                                         }
788                                 }
789                         }
790                 }
791         }
792         
793         /* return the number of items added to the list */
794         return items;
795 }
796
797 static int animdata_filter_shapekey (ListBase *anim_data, Key *key, int filter_mode, void *owner, short ownertype, ID *owner_id)
798 {
799         bAnimListElem *ale;
800         KeyBlock *kb;
801         //FCurve *fcu;
802         int i, items=0;
803         
804         /* are we filtering for display or editing */
805         if (filter_mode & ANIMFILTER_CHANNELS) {
806                 /* for display - loop over shapekeys, adding ipo-curve references where needed */
807                 kb= key->block.first;
808                 
809                 /* loop through possible shapekeys, manually creating entries */
810                 for (i= 1; i < key->totkey; i++) {
811                         ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
812                         kb = kb->next; /* do this even on the first try, as the first is 'Basis' (which doesn't get included) */
813                         
814                         ale->data= kb;
815                         ale->type= ANIMTYPE_SHAPEKEY; /* 'abused' usage of this type */
816                         ale->owner= key;
817                         ale->ownertype= ANIMTYPE_SHAPEKEY;
818                         ale->datatype= ALE_NONE;
819                         ale->index = i;
820                         
821 #if 0 // XXX fixme... old system
822                         if (key->ipo) {
823                                 for (icu= key->ipo->curve.first; icu; icu=icu->next) {
824                                         if (icu->adrcode == i) {
825                                                 ale->key_data= icu;
826                                                 ale->datatype= ALE_ICU;
827                                                 break;
828                                         }
829                                 }
830                         }
831 #endif // XXX fixme... old system
832                         
833                         ale->id= owner_id;
834                         
835                         BLI_addtail(anim_data, ale);
836                         items++;
837                 }
838         }
839         else {
840 #if 0 // XXX fixme... old system
841                 /* loop over ipo curves if present - for editing */
842                 if (key->ipo) {
843                         if (filter_mode & ANIMFILTER_IPOKEYS) {
844                                 ale= make_new_animlistelem(key->ipo, ANIMTYPE_IPO, key, ANIMTYPE_SHAPEKEY);
845                                 if (ale) {
846                                         if (owned) ale->id= owner;
847                                         BLI_addtail(anim_data, ale);
848                                         items++;
849                                 }
850                         }
851                         else {
852                                 items += animdata_filter_ipocurves(anim_data, key->ipo, filter_mode, key, ANIMTYPE_SHAPEKEY, (owned)?(owner):(NULL));
853                         }
854                 }
855 #endif // XXX fixme... old system
856         }
857         
858         /* return the number of items added to the list */
859         return items;
860 }
861  
862 #if 0
863 // FIXME: switch this to use the bDopeSheet...
864 static int animdata_filter_gpencil (ListBase *anim_data, bScreen *sc, int filter_mode)
865 {
866         bAnimListElem *ale;
867         ScrArea *sa, *curarea;
868         bGPdata *gpd;
869         bGPDlayer *gpl;
870         int items = 0;
871         
872         /* check if filtering types are appropriate */
873         {
874                 /* special hack for fullscreen area (which must be this one then):
875                  *      - we use the curarea->full as screen to get spaces from, since the
876                  *        old (pre-fullscreen) screen was stored there...
877                  *      - this is needed as all data would otherwise disappear
878                  */
879                 // XXX need to get new alternative for curarea
880                 if ((curarea->full) && (curarea->spacetype==SPACE_ACTION))
881                         sc= curarea->full;
882                 
883                 /* loop over spaces in current screen, finding gpd blocks (could be slow!) */
884                 for (sa= sc->areabase.first; sa; sa= sa->next) {
885                         /* try to get gp data */
886                         // XXX need to put back grease pencil api...
887                         gpd= gpencil_data_getactive(sa);
888                         if (gpd == NULL) continue;
889                         
890                         /* add gpd as channel too (if for drawing, and it has layers) */
891                         if ((filter_mode & ANIMFILTER_CHANNELS) && (gpd->layers.first)) {
892                                 /* add to list */
893                                 ale= make_new_animlistelem(gpd, ANIMTYPE_GPDATABLOCK, sa, ANIMTYPE_SPECIALDATA);
894                                 if (ale) {
895                                         BLI_addtail(anim_data, ale);
896                                         items++;
897                                 }
898                         }
899                         
900                         /* only add layers if they will be visible (if drawing channels) */
901                         if ( !(filter_mode & ANIMFILTER_VISIBLE) || (EXPANDED_GPD(gpd)) ) {
902                                 /* loop over layers as the conditions are acceptable */
903                                 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
904                                         /* only if selected */
905                                         if ( ANIMCHANNEL_SELOK(SEL_GPL(gpl)) ) {
906                                                 /* only if editable */
907                                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
908                                                         /* add to list */
909                                                         ale= make_new_animlistelem(gpl, ANIMTYPE_GPLAYER, gpd, ANIMTYPE_GPDATABLOCK);
910                                                         if (ale) {
911                                                                 BLI_addtail(anim_data, ale);
912                                                                 items++;
913                                                         }
914                                                 }
915                                         }
916                                 }
917                         }
918                 }
919         }
920         
921         /* return the number of items added to the list */
922         return items;
923 }
924 #endif 
925
926
927 static int animdata_filter_dopesheet_mats (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
928 {
929         ListBase mats = {NULL, NULL};
930         LinkData *ld;
931         
932         bAnimListElem *ale=NULL;
933         Object *ob= base->object;
934         int items=0, a=0;
935         
936         /* firstly check that we actuallly have some materials, by gathering all materials in a temp list */
937         for (a=0; a < ob->totcol; a++) {
938                 Material *ma= give_current_material(ob, a);
939                 short ok = 0;
940                 
941                 /* for now, if no material returned, skip (this shouldn't confuse the user I hope) */
942                 if (ELEM(NULL, ma, ma->adt)) 
943                         continue;
944                 
945                 /* check if ok */
946                 ANIMDATA_FILTER_CASES(ma, 
947                         { /* AnimData blocks - do nothing... */ },
948                         ok=1;, 
949                         ok=1;, 
950                         ok=1;)
951                 if (ok == 0) continue;
952                 
953                 /* make a temp list elem for this */
954                 ld= MEM_callocN(sizeof(LinkData), "DopeSheet-MaterialCache");
955                 ld->data= ma;
956                 BLI_addtail(&mats, ld);
957         }
958         
959         /* if there were no channels found, no need to carry on */
960         if (mats.first == NULL)
961                 return 0;
962         
963         /* include materials-expand widget? */
964         if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
965                 ale= make_new_animlistelem(ob, ANIMTYPE_FILLMATD, base, ANIMTYPE_OBJECT, (ID *)ob);
966                 if (ale) {
967                         BLI_addtail(anim_data, ale);
968                         items++;
969                 }
970         }
971         
972         /* add materials? */
973         if (FILTER_MAT_OBJC(ob) || (filter_mode & ANIMFILTER_CURVESONLY)) {
974                 /* for each material in cache, add channels  */
975                 for (ld= mats.first; ld; ld= ld->next) {
976                         Material *ma= (Material *)ld->data;
977                         
978                         /* include material-expand widget? */
979                         // hmm... do we need to store the index of this material in the array anywhere?
980                         if (filter_mode & ANIMFILTER_CHANNELS) {
981                                 ale= make_new_animlistelem(ma, ANIMTYPE_DSMAT, base, ANIMTYPE_OBJECT, (ID *)ma);
982                                 if (ale) {
983                                         BLI_addtail(anim_data, ale);
984                                         items++;
985                                 }
986                         }
987                         
988                         /* add material's animation data */
989                         if (FILTER_MAT_OBJD(ma) || (filter_mode & ANIMFILTER_CURVESONLY)) {
990                                 ANIMDATA_FILTER_CASES(ma, 
991                                         { /* AnimData blocks - do nothing... */ },
992                                         items += animdata_filter_nla(anim_data, ma->adt, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);, 
993                                         items += animdata_filter_fcurves(anim_data, ma->adt->drivers.first, NULL, ma, ANIMTYPE_DSMAT, filter_mode, (ID *)ma);, 
994                                         items += animdata_filter_action(anim_data, ma->adt->action, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);)
995                         }
996                 }
997         }
998         
999         /* free cache */
1000         BLI_freelistN(&mats);
1001         
1002         /* return the number of items added to the list */
1003         return items;
1004 }
1005
1006 static int animdata_filter_dopesheet_obdata (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1007 {
1008         bAnimListElem *ale=NULL;
1009         Object *ob= base->object;
1010         IdAdtTemplate *iat= ob->data;
1011         AnimData *adt= iat->adt;
1012         short type=0, expanded=0;
1013         int items= 0;
1014         
1015         /* get settings based on data type */
1016         switch (ob->type) {
1017                 case OB_CAMERA: /* ------- Camera ------------ */
1018                 {
1019                         Camera *ca= (Camera *)ob->data;
1020                         
1021                         type= ANIMTYPE_DSCAM;
1022                         expanded= FILTER_CAM_OBJD(ca);
1023                 }
1024                         break;
1025                 case OB_LAMP: /* ---------- Lamp ----------- */
1026                 {
1027                         Lamp *la= (Lamp *)ob->data;
1028                         
1029                         type= ANIMTYPE_DSLAM;
1030                         expanded= FILTER_LAM_OBJD(la);
1031                 }
1032                         break;
1033                 case OB_CURVE: /* ------- Curve ---------- */
1034                 {
1035                         Curve *cu= (Curve *)ob->data;
1036                         
1037                         type= ANIMTYPE_DSCUR;
1038                         expanded= FILTER_CUR_OBJD(cu);
1039                 }
1040                         break;
1041         }
1042         
1043         /* special exception for drivers instead of action */
1044         if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
1045                 expanded= EXPANDED_DRVD(adt);
1046         
1047         /* include data-expand widget? */
1048         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {               
1049                 ale= make_new_animlistelem(iat, type, base, ANIMTYPE_OBJECT, (ID *)iat);
1050                 if (ale) BLI_addtail(anim_data, ale);
1051         }
1052         
1053         /* add object-data animation channels? */
1054         if ((expanded) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1055                 /* filtering for channels - nla, drivers, keyframes */
1056                 ANIMDATA_FILTER_CASES(iat, 
1057                         { /* AnimData blocks - do nothing... */ },
1058                         items+= animdata_filter_nla(anim_data, iat->adt, filter_mode, iat, type, (ID *)iat);,
1059                         items+= animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, iat, type, filter_mode, (ID *)iat);, 
1060                         items += animdata_filter_action(anim_data, iat->adt->action, filter_mode, iat, type, (ID *)iat);)
1061         }
1062         
1063         /* return the number of items added to the list */
1064         return items;
1065 }
1066
1067 static int animdata_filter_dopesheet_ob (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1068 {
1069         bAnimListElem *ale=NULL;
1070         AnimData *adt = NULL;
1071         Object *ob= base->object;
1072         Key *key= ob_get_key(ob);
1073         short obdata_ok = 0;
1074         int items = 0;
1075         
1076         /* add this object as a channel first */
1077         if ((filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) == 0) {
1078                 /* check if filtering by selection */
1079                 if ANIMCHANNEL_SELOK((base->flag & SELECT)) {
1080                         ale= make_new_animlistelem(base, ANIMTYPE_OBJECT, NULL, ANIMTYPE_NONE, NULL);
1081                         if (ale) {
1082                                 BLI_addtail(anim_data, ale);
1083                                 items++;
1084                         }
1085                 }
1086         }
1087         
1088         /* if collapsed, don't go any further (unless adding keyframes only) */
1089         if ( (EXPANDED_OBJC(ob) == 0) && !(filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) )
1090                 return items;
1091         
1092         /* Action, Drivers, or NLA */
1093         if (ob->adt) {
1094                 adt= ob->adt;
1095                 ANIMDATA_FILTER_CASES(ob,
1096                         { /* AnimData blocks - do nothing... */ },
1097                         { /* nla */
1098                                 /* add NLA tracks */
1099                                 items += animdata_filter_nla(anim_data, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1100                         },
1101                         { /* drivers */
1102                                 /* include drivers-expand widget? */
1103                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1104                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, base, ANIMTYPE_OBJECT, (ID *)ob);
1105                                         if (ale) {
1106                                                 BLI_addtail(anim_data, ale);
1107                                                 items++;
1108                                         }
1109                                 }
1110                                 
1111                                 /* add F-Curve channels (drivers are F-Curves) */
1112                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1113                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1114                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, ob, ANIMTYPE_OBJECT, filter_mode, (ID *)ob);
1115                                 }
1116                         },
1117                         { /* action (keyframes) */
1118                                 /* include action-expand widget? */
1119                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1120                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, base, ANIMTYPE_OBJECT, (ID *)ob);
1121                                         if (ale) {
1122                                                 BLI_addtail(anim_data, ale);
1123                                                 items++;
1124                                         }
1125                                 }
1126                                 
1127                                 /* add F-Curve channels? */
1128                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1129                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1130                                         items += animdata_filter_action(anim_data, adt->action, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob); 
1131                                 }
1132                         }
1133                 );
1134         }
1135         
1136         
1137         /* ShapeKeys? */
1138         if ((key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
1139                 adt= key->adt;
1140                 ANIMDATA_FILTER_CASES(key,
1141                         { /* AnimData blocks - do nothing... */ },
1142                         { /* nla */
1143                                 /* add NLA tracks */
1144                                 items += animdata_filter_nla(anim_data, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1145                         },
1146                         { /* drivers */
1147                                 /* include shapekey-expand widget? */
1148                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1149                                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1150                                         if (ale) {
1151                                                 BLI_addtail(anim_data, ale);
1152                                                 items++;
1153                                         }
1154                                 }
1155                                 
1156                                 /* add channels */
1157                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1158                                         items += animdata_filter_shapekey(anim_data, key, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1159                                 }
1160                         },
1161                         { /* action (keyframes) */
1162                                 /* include shapekey-expand widget? */
1163                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1164                                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1165                                         if (ale) {
1166                                                 BLI_addtail(anim_data, ale);
1167                                                 items++;
1168                                         }
1169                                 }
1170                                 
1171                                 /* add channels */
1172                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1173                                         items += animdata_filter_shapekey(anim_data, key, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1174                                 }
1175                         }
1176                 );
1177         }
1178
1179         /* Materials? */
1180         if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT))
1181                 items += animdata_filter_dopesheet_mats(anim_data, ads, base, filter_mode);
1182         
1183         /* Object Data */
1184         switch (ob->type) {
1185                 case OB_CAMERA: /* ------- Camera ------------ */
1186                 {
1187                         Camera *ca= (Camera *)ob->data;
1188                         
1189                         if ((ads->filterflag & ADS_FILTER_NOCAM) == 0) {
1190                                 ANIMDATA_FILTER_CASES(ca,
1191                                         { /* AnimData blocks - do nothing... */ },
1192                                         obdata_ok= 1;,
1193                                         obdata_ok= 1;,
1194                                         obdata_ok= 1;)
1195                         }
1196                 }
1197                         break;
1198                 case OB_LAMP: /* ---------- Lamp ----------- */
1199                 {
1200                         Lamp *la= (Lamp *)ob->data;
1201                         
1202                         if ((ads->filterflag & ADS_FILTER_NOLAM) == 0) {
1203                                 ANIMDATA_FILTER_CASES(la,
1204                                         { /* AnimData blocks - do nothing... */ },
1205                                         obdata_ok= 1;,
1206                                         obdata_ok= 1;,
1207                                         obdata_ok= 1;)
1208                         }
1209                 }
1210                         break;
1211                 case OB_CURVE: /* ------- Curve ---------- */
1212                 {
1213                         Curve *cu= (Curve *)ob->data;
1214                         
1215                         if ((ads->filterflag & ADS_FILTER_NOCUR) == 0) {
1216                                 ANIMDATA_FILTER_CASES(cu,
1217                                         { /* AnimData blocks - do nothing... */ },
1218                                         obdata_ok= 1;,
1219                                         obdata_ok= 1;,
1220                                         obdata_ok= 1;)
1221                         }
1222                 }
1223                         break;
1224         }
1225         if (obdata_ok) 
1226                 items += animdata_filter_dopesheet_obdata(anim_data, ads, base, filter_mode);
1227         
1228         /* return the number of items added to the list */
1229         return items;
1230 }       
1231
1232 static int animdata_filter_dopesheet_scene (ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
1233 {
1234         World *wo= sce->world;
1235         AnimData *adt= NULL;
1236         bAnimListElem *ale;
1237         int items = 0;
1238         
1239         /* add scene as a channel first (even if we aren't showing scenes we still need to show the scene's sub-data */
1240         if ((filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) == 0) {
1241                 /* check if filtering by selection */
1242                 if (ANIMCHANNEL_SELOK( (sce->flag & SCE_DS_SELECTED) )) {
1243                         ale= make_new_animlistelem(sce, ANIMTYPE_SCENE, NULL, ANIMTYPE_NONE, NULL);
1244                         if (ale) {
1245                                 BLI_addtail(anim_data, ale);
1246                                 items++;
1247                         }
1248                 }
1249         }
1250         
1251         /* if collapsed, don't go any further (unless adding keyframes only) */
1252         if ( (EXPANDED_SCEC(sce) == 0) && !(filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) )
1253                 return items;
1254                 
1255         /* Action, Drivers, or NLA  for Scene */
1256         if ((ads->filterflag & ADS_FILTER_NOSCE) == 0) {
1257                 adt= sce->adt;
1258                 ANIMDATA_FILTER_CASES(sce,
1259                         { /* AnimData blocks - do nothing... */ },
1260                         { /* nla */
1261                                 /* add NLA tracks */
1262                                 items += animdata_filter_nla(anim_data, adt, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce);
1263                         },
1264                         { /* drivers */
1265                                 /* include drivers-expand widget? */
1266                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1267                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, sce, ANIMTYPE_SCENE, (ID *)sce);
1268                                         if (ale) {
1269                                                 BLI_addtail(anim_data, ale);
1270                                                 items++;
1271                                         }
1272                                 }
1273                                 
1274                                 /* add F-Curve channels (drivers are F-Curves) */
1275                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1276                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, sce, ANIMTYPE_SCENE, filter_mode, (ID *)sce);
1277                                 }
1278                         },
1279                         { /* action */
1280                                 /* include action-expand widget? */
1281                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1282                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, sce, ANIMTYPE_SCENE, (ID *)sce);
1283                                         if (ale) {
1284                                                 BLI_addtail(anim_data, ale);
1285                                                 items++;
1286                                         }
1287                                 }
1288                                 
1289                                 /* add F-Curve channels? */
1290                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1291                                         items += animdata_filter_action(anim_data, adt->action, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce); 
1292                                 }
1293                         }
1294                 )
1295         }
1296         
1297         /* world */
1298         if ((wo && wo->adt) && !(ads->filterflag & ADS_FILTER_NOWOR)) {
1299                 /* Action, Drivers, or NLA for World */
1300                 adt= wo->adt;
1301                 ANIMDATA_FILTER_CASES(wo,
1302                         { /* AnimData blocks - do nothing... */ },
1303                         { /* nla */
1304                                 /* add NLA tracks */
1305                                 items += animdata_filter_nla(anim_data, adt, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo);
1306                         },
1307                         { /* drivers */
1308                                 /* include world-expand widget? */
1309                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1310                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)wo);
1311                                         if (ale) {
1312                                                 BLI_addtail(anim_data, ale);
1313                                                 items++;
1314                                         }
1315                                 }
1316                                 
1317                                 /* add F-Curve channels (drivers are F-Curves) */
1318                                 if (FILTER_WOR_SCED(wo)/*EXPANDED_DRVD(adt)*/ || !(filter_mode & ANIMFILTER_CHANNELS)) {
1319                                         // XXX owner info is messed up now...
1320                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, wo, ANIMTYPE_DSWOR, filter_mode, (ID *)wo);
1321                                 }
1322                         },
1323                         { /* action */
1324                                 /* include world-expand widget? */
1325                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1326                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)sce);
1327                                         if (ale) {
1328                                                 BLI_addtail(anim_data, ale);
1329                                                 items++;
1330                                         }
1331                                 }
1332                                 
1333                                 /* add channels */
1334                                 if (FILTER_WOR_SCED(wo) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1335                                         items += animdata_filter_action(anim_data, adt->action, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo); 
1336                                 }
1337                         }
1338                 )
1339         }
1340         
1341         /* return the number of items added to the list */
1342         return items;
1343 }
1344
1345 // 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)
1346 static int animdata_filter_dopesheet (ListBase *anim_data, bDopeSheet *ads, int filter_mode)
1347 {
1348         Scene *sce= (Scene *)ads->source;
1349         Base *base;
1350         bAnimListElem *ale;
1351         int items = 0;
1352         
1353         /* check that we do indeed have a scene */
1354         if ((ads->source == NULL) || (GS(ads->source->name)!=ID_SCE)) {
1355                 printf("DopeSheet Error: Not scene! \n");
1356                 return 0;
1357         }
1358         
1359         /* scene-linked animation */
1360         // TODO: sequencer, composite nodes - are we to include those here too?
1361         {
1362                 short sceOk= 0, worOk= 0;
1363                 
1364                 /* check filtering-flags if ok */
1365                 ANIMDATA_FILTER_CASES(sce, 
1366                         {
1367                                 /* for the special AnimData blocks only case, we only need to add
1368                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1369                                  */
1370                                 ANIMDATA_ADD_ANIMDATA(sce);
1371                                 sceOk=0;
1372                         },
1373                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);, 
1374                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);, 
1375                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);)
1376                 if (sce->world) {
1377                         ANIMDATA_FILTER_CASES(sce->world, 
1378                                 {
1379                                         /* for the special AnimData blocks only case, we only need to add
1380                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1381                                          */
1382                                         ANIMDATA_ADD_ANIMDATA(sce->world);
1383                                         worOk=0;
1384                                 },
1385                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);, 
1386                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);, 
1387                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);)
1388                 }
1389                 
1390                 /* check if not all bad (i.e. so there is something to show) */
1391                 if ( !(!sceOk && !worOk) ) {
1392                         /* add scene data to the list of filtered channels */
1393                         items += animdata_filter_dopesheet_scene(anim_data, ads, sce, filter_mode);
1394                 }
1395         }
1396         
1397         
1398         /* loop over all bases in the scene */
1399         for (base= sce->base.first; base; base= base->next) {
1400                 /* check if there's an object (all the relevant checks are done in the ob-function) */
1401                 if (base->object) {
1402                         Object *ob= base->object;
1403                         Key *key= ob_get_key(ob);
1404                         short actOk=1, keyOk=1, dataOk=1, matOk=1;
1405                         
1406                         /* firstly, check if object can be included, by the following fanimors:
1407                          *      - if only visible, must check for layer and also viewport visibility
1408                          *      - if only selected, must check if object is selected 
1409                          *      - there must be animation data to edit
1410                          */
1411                         // TODO: if cache is implemented, just check name here, and then 
1412                         if (filter_mode & ANIMFILTER_VISIBLE) {
1413                                 /* layer visibility - we check both object and base, since these may not be in sync yet */
1414                                 if ((sce->lay & (ob->lay|base->lay))==0) continue;
1415                                 
1416                                 /* outliner restrict-flag */
1417                                 if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
1418                         }
1419                         
1420                         /* additionally, dopesheet filtering also affects what objects to consider */
1421                         if (ads->filterflag) {
1422                                 /* check selection and object type filters */
1423                                 if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) || (base == sce->basact)) )  {
1424                                         /* only selected should be shown */
1425                                         continue;
1426                                 }
1427                                 
1428                                 /* check filters for datatypes */
1429                                         /* object */
1430                                 actOk= 0;
1431                                 keyOk= 0;
1432                                 ANIMDATA_FILTER_CASES(ob, 
1433                                         {
1434                                                 /* for the special AnimData blocks only case, we only need to add
1435                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1436                                                  */
1437                                                 ANIMDATA_ADD_ANIMDATA(ob);
1438                                                 actOk=0;
1439                                         },
1440                                         actOk= 1;, 
1441                                         actOk= 1;, 
1442                                         actOk= 1;)
1443                                 if (key) {
1444                                         /* shapekeys */
1445                                         ANIMDATA_FILTER_CASES(key, 
1446                                                 {
1447                                                         /* for the special AnimData blocks only case, we only need to add
1448                                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1449                                                          */
1450                                                         ANIMDATA_ADD_ANIMDATA(key);
1451                                                         keyOk=0;
1452                                                 },
1453                                                 keyOk= 1;, 
1454                                                 keyOk= 1;, 
1455                                                 keyOk= 1;)
1456                                 }
1457                                 
1458                                 /* materials - only for geometric types */
1459                                 matOk= 0; /* by default, not ok... */
1460                                 if ( !(ads->filterflag & ADS_FILTER_NOMAT) && (ob->totcol) && 
1461                                          ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) ) 
1462                                 {
1463                                         int a;
1464                                         
1465                                         /* firstly check that we actuallly have some materials */
1466                                         for (a=0; a < ob->totcol; a++) {
1467                                                 Material *ma= give_current_material(ob, a);
1468                                                 
1469                                                 /* if material has relevant animation data, break */
1470                                                 ANIMDATA_FILTER_CASES(ma, 
1471                                                         {
1472                                                                 /* for the special AnimData blocks only case, we only need to add
1473                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1474                                                                  */
1475                                                                 ANIMDATA_ADD_ANIMDATA(ma);
1476                                                                 matOk=0;
1477                                                         },
1478                                                         matOk= 1;, 
1479                                                         matOk= 1;, 
1480                                                         matOk= 1;)
1481                                                         
1482                                                 if (matOk) 
1483                                                         break;
1484                                         }
1485                                 }
1486                                 
1487                                 /* data */
1488                                 switch (ob->type) {
1489                                         case OB_CAMERA: /* ------- Camera ------------ */
1490                                         {
1491                                                 Camera *ca= (Camera *)ob->data;
1492                                                 dataOk= 0;
1493                                                 ANIMDATA_FILTER_CASES(ca, 
1494                                                         if ((ads->filterflag & ADS_FILTER_NOCAM)==0) {
1495                                                                 /* for the special AnimData blocks only case, we only need to add
1496                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1497                                                                  */
1498                                                                 ANIMDATA_ADD_ANIMDATA(ca);
1499                                                                 dataOk=0;
1500                                                         },
1501                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);, 
1502                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);, 
1503                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);)
1504                                         }
1505                                                 break;
1506                                         case OB_LAMP: /* ---------- Lamp ----------- */
1507                                         {
1508                                                 Lamp *la= (Lamp *)ob->data;
1509                                                 dataOk= 0;
1510                                                 ANIMDATA_FILTER_CASES(la, 
1511                                                         if ((ads->filterflag & ADS_FILTER_NOLAM)==0) {
1512                                                                 /* for the special AnimData blocks only case, we only need to add
1513                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1514                                                                  */
1515                                                                 ANIMDATA_ADD_ANIMDATA(la);
1516                                                                 dataOk=0;
1517                                                         },
1518                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);, 
1519                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);, 
1520                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);)
1521                                         }
1522                                                 break;
1523                                         case OB_CURVE: /* ------- Curve ---------- */
1524                                         {
1525                                                 Curve *cu= (Curve *)ob->data;
1526                                                 dataOk= 0;
1527                                                 ANIMDATA_FILTER_CASES(cu, 
1528                                                         if ((ads->filterflag & ADS_FILTER_NOCUR)==0) {
1529                                                                 /* for the special AnimData blocks only case, we only need to add
1530                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1531                                                                  */
1532                                                                 ANIMDATA_ADD_ANIMDATA(cu);
1533                                                                 dataOk=0;
1534                                                         },
1535                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);, 
1536                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);, 
1537                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);)
1538                                         }
1539                                                 break;
1540                                         default: /* --- other --- */
1541                                                 dataOk= 0;
1542                                                 break;
1543                                 }
1544                                 
1545                                 /* check if all bad (i.e. nothing to show) */
1546                                 if (!actOk && !keyOk && !dataOk && !matOk)
1547                                         continue;
1548                         }
1549                         else {
1550                                 /* check data-types */
1551                                 actOk= ANIMDATA_HAS_KEYS(ob);
1552                                 keyOk= (key != NULL);
1553                                 
1554                                 /* materials - only for geometric types */
1555                                 matOk= 0; /* by default, not ok... */
1556                                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) && (ob->totcol)) 
1557                                 {
1558                                         int a;
1559                                         
1560                                         /* firstly check that we actuallly have some materials */
1561                                         for (a=0; a < ob->totcol; a++) {
1562                                                 Material *ma= give_current_material(ob, a);
1563                                                 
1564                                                 if ((ma) && ANIMDATA_HAS_KEYS(ma)) {
1565                                                         matOk= 1;
1566                                                         break;
1567                                                 }
1568                                         }
1569                                 }
1570                                 
1571                                 /* data */
1572                                 switch (ob->type) {
1573                                         case OB_CAMERA: /* ------- Camera ------------ */
1574                                         {
1575                                                 Camera *ca= (Camera *)ob->data;
1576                                                 dataOk= ANIMDATA_HAS_KEYS(ca);                                          
1577                                         }
1578                                                 break;
1579                                         case OB_LAMP: /* ---------- Lamp ----------- */
1580                                         {
1581                                                 Lamp *la= (Lamp *)ob->data;
1582                                                 dataOk= ANIMDATA_HAS_KEYS(la);  
1583                                         }
1584                                                 break;
1585                                         case OB_CURVE: /* -------- Curve ---------- */
1586                                         {
1587                                                 Curve *cu= (Curve *)ob->data;
1588                                                 dataOk= ANIMDATA_HAS_KEYS(cu);  
1589                                         }
1590                                                 break;
1591                                         default: /* --- other --- */
1592                                                 dataOk= 0;
1593                                                 break;
1594                                 }
1595                                 
1596                                 /* check if all bad (i.e. nothing to show) */
1597                                 if (!actOk && !keyOk && !dataOk && !matOk)
1598                                         continue;
1599                         }
1600                         
1601                         /* since we're still here, this object should be usable */
1602                         items += animdata_filter_dopesheet_ob(anim_data, ads, base, filter_mode);
1603                 }
1604         }
1605         
1606         /* return the number of items in the list */
1607         return items;
1608 }
1609
1610 /* ----------- Public API --------------- */
1611
1612 /* This function filters the active data source to leave only animation channels suitable for
1613  * usage by the caller. It will return the length of the list 
1614  * 
1615  *      *anim_data: is a pointer to a ListBase, to which the filtered animation channels
1616  *              will be placed for use.
1617  *      filter_mode: how should the data be filtered - bitmapping accessed flags
1618  */
1619 int ANIM_animdata_filter (bAnimContext *ac, ListBase *anim_data, int filter_mode, void *data, short datatype)
1620 {
1621         int items = 0;
1622         
1623         /* only filter data if there's somewhere to put it */
1624         if (data && anim_data) {
1625                 bAnimListElem *ale, *next;
1626                 Object *obact= (ac) ? ac->obact : NULL;
1627                 
1628                 /* firstly filter the data */
1629                 switch (datatype) {
1630                         case ANIMCONT_ACTION:
1631                                 items= animdata_filter_action(anim_data, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
1632                                 break;
1633                                 
1634                         case ANIMCONT_SHAPEKEY:
1635                                 items= animdata_filter_shapekey(anim_data, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
1636                                 break;
1637                                 
1638                         case ANIMCONT_GPENCIL:
1639                                 //items= animdata_filter_gpencil(anim_data, data, filter_mode);
1640                                 break;
1641                                 
1642                         case ANIMCONT_DOPESHEET:
1643                         case ANIMCONT_FCURVES:
1644                         case ANIMCONT_DRIVERS:
1645                         case ANIMCONT_NLA:
1646                                 items= animdata_filter_dopesheet(anim_data, data, filter_mode);
1647                                 break;
1648                 }
1649                         
1650                 /* remove any weedy entries */
1651                 // XXX this is weedy code!
1652                 for (ale= anim_data->first; ale; ale= next) {
1653                         next= ale->next;
1654                         
1655                         if (ale->type == ANIMTYPE_NONE) {
1656                                 items--;
1657                                 BLI_freelinkN(anim_data, ale);
1658                         }
1659                 }
1660         }
1661         
1662         /* return the number of items in the list */
1663         return items;
1664 }
1665
1666 /* ************************************************************ */