f12e9a51f32db87bd8f197977c4ef2382910b8d4
[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 /* quick macro to test for all three avove usability tests, performing the appropriate provided 
347  * action for each when the AnimData context is appropriate. 
348  *
349  * Priority order for this goes (most important, to least): NLA, Drivers, Keyframes
350  *
351  *      - id: ID block which should have an AnimData pointer following it immediately, to use
352  *      - nlaOk: line or block of code to execute for NLA case
353  *      - driversOk: line or block of code to execute for Drivers case
354  *      - keysOk: line or block of code for Keyframes case
355  */
356 #define ANIMDATA_FILTER_CASES(id, nlaOk, driversOk, keysOk) \
357         {\
358                 if (ads->filterflag & ADS_FILTER_ONLYNLA) {\
359                         if (ANIMDATA_HAS_NLA(id)) {\
360                                 nlaOk\
361                         }\
362                         else if (!(ads->filterflag & ADS_FILTER_NLA_NOACT) && ANIMDATA_HAS_KEYS(id)) {\
363                                 nlaOk\
364                         }\
365                 }\
366                 else if (ads->filterflag & ADS_FILTER_ONLYDRIVERS) {\
367                         if (ANIMDATA_HAS_DRIVERS(id)) {\
368                                 driversOk\
369                         }\
370                 }\
371                 else {\
372                         if (ANIMDATA_HAS_KEYS(id)) {\
373                                 keysOk\
374                         }\
375                 }\
376         }
377
378
379 /* quick macro to test if a anim-channel (F-Curve, Group, etc.) is selected in an acceptable way */
380 #define ANIMCHANNEL_SELOK(test_func) \
381                 ( !(filter_mode & (ANIMFILTER_SEL|ANIMFILTER_UNSEL)) || \
382                   ((filter_mode & ANIMFILTER_SEL) && test_func) || \
383                   ((filter_mode & ANIMFILTER_UNSEL) && test_func==0) ) 
384
385 /* ----------- 'Private' Stuff --------------- */
386
387 /* this function allocates memory for a new bAnimListElem struct for the 
388  * provided animation channel-data. 
389  */
390 bAnimListElem *make_new_animlistelem (void *data, short datatype, void *owner, short ownertype, ID *owner_id)
391 {
392         bAnimListElem *ale= NULL;
393         
394         /* only allocate memory if there is data to convert */
395         if (data) {
396                 /* allocate and set generic data */
397                 ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
398                 
399                 ale->data= data;
400                 ale->type= datatype;
401                         // XXX what is the point of the owner data?
402                 ale->owner= owner;
403                 ale->ownertype= ownertype;
404                 
405                 ale->id= owner_id;
406                 
407                 /* do specifics */
408                 switch (datatype) {
409                         case ANIMTYPE_SCENE:
410                         {
411                                 Scene *sce= (Scene *)data;
412                                 
413                                 ale->flag= sce->flag;
414                                 
415                                 ale->key_data= sce;
416                                 ale->datatype= ALE_SCE;
417                         }
418                                 break;
419                         case ANIMTYPE_OBJECT:
420                         {
421                                 Base *base= (Base *)data;
422                                 Object *ob= base->object;
423                                 
424                                 ale->flag= ob->flag;
425                                 
426                                 ale->key_data= ob;
427                                 ale->datatype= ALE_OB;
428                         }
429                                 break;
430                         case ANIMTYPE_FILLACTD:
431                         {
432                                 bAction *act= (bAction *)data;
433                                 
434                                 ale->flag= act->flag;
435                                 
436                                 ale->key_data= act;
437                                 ale->datatype= ALE_ACT;
438                         }
439                                 break;
440                         case ANIMTYPE_FILLDRIVERS:
441                         {
442                                 AnimData *adt= (AnimData *)data;
443                                 
444                                 ale->flag= adt->flag;
445                                 
446                                         // XXX... drivers don't show summary for now
447                                 ale->key_data= NULL;
448                                 ale->datatype= ALE_NONE;
449                         }
450                                 break;
451                         case ANIMTYPE_FILLMATD:
452                         {
453                                 Object *ob= (Object *)data;
454                                 
455                                 ale->flag= FILTER_MAT_OBJC(ob);
456                                 
457                                 ale->key_data= NULL;
458                                 ale->datatype= ALE_NONE;
459                         }
460                                 break;
461                         
462                         case ANIMTYPE_DSMAT:
463                         {
464                                 Material *ma= (Material *)data;
465                                 AnimData *adt= ma->adt;
466                                 
467                                 ale->flag= FILTER_MAT_OBJD(ma);
468                                 
469                                 ale->key_data= (adt) ? adt->action : NULL;
470                                 ale->datatype= ALE_ACT;
471                         }
472                                 break;
473                         case ANIMTYPE_DSLAM:
474                         {
475                                 Lamp *la= (Lamp *)data;
476                                 AnimData *adt= la->adt;
477                                 
478                                 ale->flag= FILTER_LAM_OBJD(la);
479                                 
480                                 ale->key_data= (adt) ? adt->action : NULL;
481                                 ale->datatype= ALE_ACT;
482                         }
483                                 break;
484                         case ANIMTYPE_DSCAM:
485                         {
486                                 Camera *ca= (Camera *)data;
487                                 AnimData *adt= ca->adt;
488                                 
489                                 ale->flag= FILTER_CAM_OBJD(ca);
490                                 
491                                 ale->key_data= (adt) ? adt->action : NULL;
492                                 ale->datatype= ALE_ACT;
493                         }
494                                 break;
495                         case ANIMTYPE_DSCUR:
496                         {
497                                 Curve *cu= (Curve *)data;
498                                 AnimData *adt= cu->adt;
499                                 
500                                 ale->flag= FILTER_CUR_OBJD(cu);
501                                 
502                                 ale->key_data= (adt) ? adt->action : NULL;
503                                 ale->datatype= ALE_ACT;
504                         }
505                                 break;
506                         case ANIMTYPE_DSSKEY:
507                         {
508                                 Key *key= (Key *)data;
509                                 AnimData *adt= key->adt;
510                                 
511                                 ale->flag= FILTER_SKE_OBJD(key); 
512                                 
513                                 ale->key_data= (adt) ? adt->action : NULL;
514                                 ale->datatype= ALE_ACT;
515                         }
516                                 break;
517                         case ANIMTYPE_DSWOR:
518                         {
519                                 World *wo= (World *)data;
520                                 AnimData *adt= wo->adt;
521                                 
522                                 ale->flag= FILTER_WOR_SCED(wo); 
523                                 
524                                 ale->key_data= (adt) ? adt->action : NULL;
525                                 ale->datatype= ALE_ACT;
526                         }
527                                 break;
528                                 
529                         case ANIMTYPE_GROUP:
530                         {
531                                 bActionGroup *agrp= (bActionGroup *)data;
532                                 
533                                 ale->flag= agrp->flag;
534                                 
535                                 ale->key_data= NULL;
536                                 ale->datatype= ALE_GROUP;
537                         }
538                                 break;
539                         case ANIMTYPE_FCURVE:
540                         {
541                                 FCurve *fcu= (FCurve *)data;
542                                 
543                                 ale->flag= fcu->flag;
544                                 
545                                 ale->key_data= fcu;
546                                 ale->datatype= ALE_FCURVE;
547                         }
548                                 break;
549                         
550                         case ANIMTYPE_GPLAYER:
551                         {
552                                 bGPDlayer *gpl= (bGPDlayer *)data;
553                                 
554                                 ale->flag= gpl->flag;
555                                 
556                                 ale->key_data= NULL;
557                                 ale->datatype= ALE_GPFRAME;
558                         }
559                                 break;
560                                 
561                         case ANIMTYPE_NLATRACK:
562                         {
563                                 NlaTrack *nlt= (NlaTrack *)data;
564                                 
565                                 ale->flag= nlt->flag;
566                                 
567                                         // XXX or should this be done some other way?
568                                 ale->key_data= &nlt->strips;
569                                 ale->datatype= ALE_NLASTRIP;
570                         }
571                                 break;
572                         case ANIMTYPE_NLAACTION:
573                         {
574                                 /* nothing to include for now... nothing editable from NLA-perspective here */
575                                 ale->key_data= NULL;
576                                 ale->datatype= ALE_NONE;
577                         }
578                                 break;
579                 }
580         }
581         
582         /* return created datatype */
583         return ale;
584 }
585  
586 /* ----------------------------------------- */
587
588
589 static int animdata_filter_fcurves (ListBase *anim_data, FCurve *first, bActionGroup *grp, void *owner, short ownertype, int filter_mode, ID *owner_id)
590 {
591         bAnimListElem *ale = NULL;
592         FCurve *fcu;
593         int items = 0;
594         
595         /* loop over F-Curves - assume that the caller of this has already checked that these should be included 
596          * NOTE: we need to check if the F-Curves belong to the same group, as this gets called for groups too...
597          */
598         for (fcu= first; ((fcu) && (fcu->grp==grp)); fcu= fcu->next) {
599                 /* only include if visible (Graph Editor check, not channels check) */
600                 if (!(filter_mode & ANIMFILTER_CURVEVISIBLE) || (fcu->flag & FCURVE_VISIBLE)) {
601                         /* only work with this channel and its subchannels if it is editable */
602                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_FCU(fcu)) {
603                                 /* only include this curve if selected in a way consistent with the filtering requirements */
604                                 if ( ANIMCHANNEL_SELOK(SEL_FCU(fcu)) ) {
605                                         /* only include if this curve is active */
606                                         if (!(filter_mode & ANIMFILTER_ACTIVE) || (fcu->flag & FCURVE_ACTIVE)) {
607                                                 ale= make_new_animlistelem(fcu, ANIMTYPE_FCURVE, owner, ownertype, owner_id);
608                                                 
609                                                 if (ale) {
610                                                         BLI_addtail(anim_data, ale);
611                                                         items++;
612                                                 }
613                                         }
614                                 }
615                         }
616                 }
617         }
618         
619         /* return the number of items added to the list */
620         return items;
621 }
622
623 static int animdata_filter_action (ListBase *anim_data, bAction *act, int filter_mode, void *owner, short ownertype, ID *owner_id)
624 {
625         bAnimListElem *ale=NULL;
626         bActionGroup *agrp;
627         FCurve *lastchan=NULL;
628         int items = 0;
629         
630         /* loop over groups */
631         //       XXX in future, we need to be prepared for nestled groups...
632         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
633                 /* add this group as a channel first */
634                 if ((filter_mode & ANIMFILTER_CHANNELS) || !(filter_mode & ANIMFILTER_CURVESONLY)) {
635                         /* check if filtering by selection */
636                         if ( ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) ) {
637                                 ale= make_new_animlistelem(agrp, ANIMTYPE_GROUP, NULL, ANIMTYPE_NONE, owner_id);
638                                 if (ale) {
639                                         BLI_addtail(anim_data, ale);
640                                         items++;
641                                 }
642                         }
643                 }
644                 
645                 /* store reference to last channel of group */
646                 if (agrp->channels.last) 
647                         lastchan= agrp->channels.last;
648                 
649                 
650                 /* there are some situations, where only the channels of the action group should get considered */
651                 if (!(filter_mode & ANIMFILTER_ACTGROUPED) || (agrp->flag & AGRP_ACTIVE)) {
652                         /* filters here are a bit convoulted...
653                          *      - groups show a "summary" of keyframes beside their name which must accessable for tools which handle keyframes
654                          *      - groups can be collapsed (and those tools which are only interested in channels rely on knowing that group is closed)
655                          *
656                          * cases when we should include F-Curves inside group:
657                          *      - we don't care about visibility
658                          *      - group is expanded
659                          *      - we're interested in keyframes, but not if they appear in selected channels
660                          */
661                         // XXX what was the selection check here for again?
662                         if ( (!(filter_mode & ANIMFILTER_VISIBLE) || EXPANDED_AGRP(agrp)) || 
663                                  ( /*ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) &&*/ (filter_mode & ANIMFILTER_CURVESONLY) ) ) 
664                         {
665                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_AGRP(agrp)) {
666                                         // XXX the 'owner' info here needs review...
667                                         items += animdata_filter_fcurves(anim_data, agrp->channels.first, agrp, owner, ownertype, filter_mode, owner_id);
668                                         
669                                         /* remove group from filtered list if last element is group 
670                                          * (i.e. only if group had channels, which were all hidden)
671                                          */
672                                         // XXX this is really hacky... it should be fixed in a much more elegant way!
673                                         if ( (ale) && (anim_data->last == ale) && 
674                                                  (ale->data == agrp) && (agrp->channels.first) ) 
675                                         {
676                                                 BLI_freelinkN(anim_data, ale);
677                                                 items--;
678                                         }
679                                 }
680                         }
681                 }
682         }
683         
684         /* loop over un-grouped F-Curves (only if we're not only considering those channels in the animive group) */
685         if (!(filter_mode & ANIMFILTER_ACTGROUPED))  {
686                 // XXX the 'owner' info here needs review...
687                 items += animdata_filter_fcurves(anim_data, (lastchan)?(lastchan->next):(act->curves.first), NULL, owner, ownertype, filter_mode, owner_id);
688         }
689         
690         /* return the number of items added to the list */
691         return items;
692 }
693
694 static int animdata_filter_nla (ListBase *anim_data, AnimData *adt, int filter_mode, void *owner, short ownertype, ID *owner_id)
695 {
696         bAnimListElem *ale;
697         NlaTrack *nlt;
698         int items = 0;
699         
700         /* loop over NLA Tracks - assume that the caller of this has already checked that these should be included */
701         for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next) {
702                 /* only work with this channel and its subchannels if it is editable */
703                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_NLT(nlt)) {
704                         /* only include this track if selected in a way consistent with the filtering requirements */
705                         if ( ANIMCHANNEL_SELOK(SEL_NLT(nlt)) ) {
706                                 /* only include if this track is active */
707                                 // XXX keep this?
708                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (nlt->flag & NLATRACK_ACTIVE)) {
709                                         ale= make_new_animlistelem(nlt, ANIMTYPE_NLATRACK, owner, ownertype, owner_id);
710                                                 
711                                         if (ale) {
712                                                 BLI_addtail(anim_data, ale);
713                                                 items++;
714                                         }
715                                 }
716                         }
717                 }
718         }
719         
720         /* if showing channels, include active action */
721         if (filter_mode & ANIMFILTER_CHANNELS) {
722                 /* there isn't really anything editable here, so skip if need editable */
723                 // TODO: currently, selection isn't checked since it doesn't matter
724                 if ((filter_mode & ANIMFILTER_FOREDIT) == 0) { 
725                         /* just add the action track now */
726                         ale= make_new_animlistelem(adt->action, ANIMTYPE_NLAACTION, owner, ownertype, owner_id);
727                                 
728                         if (ale) {
729                                 BLI_addtail(anim_data, ale);
730                                 items++;
731                         }
732                 }
733         }
734         
735         /* return the number of items added to the list */
736         return items;
737 }
738
739 static int animdata_filter_shapekey (ListBase *anim_data, Key *key, int filter_mode, void *owner, short ownertype, ID *owner_id)
740 {
741         bAnimListElem *ale;
742         KeyBlock *kb;
743         //FCurve *fcu;
744         int i, items=0;
745         
746         /* are we filtering for display or editing */
747         if (filter_mode & ANIMFILTER_CHANNELS) {
748                 /* for display - loop over shapekeys, adding ipo-curve references where needed */
749                 kb= key->block.first;
750                 
751                 /* loop through possible shapekeys, manually creating entries */
752                 for (i= 1; i < key->totkey; i++) {
753                         ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
754                         kb = kb->next; /* do this even on the first try, as the first is 'Basis' (which doesn't get included) */
755                         
756                         ale->data= kb;
757                         ale->type= ANIMTYPE_SHAPEKEY; /* 'abused' usage of this type */
758                         ale->owner= key;
759                         ale->ownertype= ANIMTYPE_SHAPEKEY;
760                         ale->datatype= ALE_NONE;
761                         ale->index = i;
762                         
763 #if 0 // XXX fixme... old system
764                         if (key->ipo) {
765                                 for (icu= key->ipo->curve.first; icu; icu=icu->next) {
766                                         if (icu->adrcode == i) {
767                                                 ale->key_data= icu;
768                                                 ale->datatype= ALE_ICU;
769                                                 break;
770                                         }
771                                 }
772                         }
773 #endif // XXX fixme... old system
774                         
775                         ale->id= owner_id;
776                         
777                         BLI_addtail(anim_data, ale);
778                         items++;
779                 }
780         }
781         else {
782 #if 0 // XXX fixme... old system
783                 /* loop over ipo curves if present - for editing */
784                 if (key->ipo) {
785                         if (filter_mode & ANIMFILTER_IPOKEYS) {
786                                 ale= make_new_animlistelem(key->ipo, ANIMTYPE_IPO, key, ANIMTYPE_SHAPEKEY);
787                                 if (ale) {
788                                         if (owned) ale->id= owner;
789                                         BLI_addtail(anim_data, ale);
790                                         items++;
791                                 }
792                         }
793                         else {
794                                 items += animdata_filter_ipocurves(anim_data, key->ipo, filter_mode, key, ANIMTYPE_SHAPEKEY, (owned)?(owner):(NULL));
795                         }
796                 }
797 #endif // XXX fixme... old system
798         }
799         
800         /* return the number of items added to the list */
801         return items;
802 }
803  
804 #if 0
805 // FIXME: switch this to use the bDopeSheet...
806 static int animdata_filter_gpencil (ListBase *anim_data, bScreen *sc, int filter_mode)
807 {
808         bAnimListElem *ale;
809         ScrArea *sa, *curarea;
810         bGPdata *gpd;
811         bGPDlayer *gpl;
812         int items = 0;
813         
814         /* check if filtering types are appropriate */
815         {
816                 /* special hack for fullscreen area (which must be this one then):
817                  *      - we use the curarea->full as screen to get spaces from, since the
818                  *        old (pre-fullscreen) screen was stored there...
819                  *      - this is needed as all data would otherwise disappear
820                  */
821                 // XXX need to get new alternative for curarea
822                 if ((curarea->full) && (curarea->spacetype==SPACE_ACTION))
823                         sc= curarea->full;
824                 
825                 /* loop over spaces in current screen, finding gpd blocks (could be slow!) */
826                 for (sa= sc->areabase.first; sa; sa= sa->next) {
827                         /* try to get gp data */
828                         // XXX need to put back grease pencil api...
829                         gpd= gpencil_data_getactive(sa);
830                         if (gpd == NULL) continue;
831                         
832                         /* add gpd as channel too (if for drawing, and it has layers) */
833                         if ((filter_mode & ANIMFILTER_CHANNELS) && (gpd->layers.first)) {
834                                 /* add to list */
835                                 ale= make_new_animlistelem(gpd, ANIMTYPE_GPDATABLOCK, sa, ANIMTYPE_SPECIALDATA);
836                                 if (ale) {
837                                         BLI_addtail(anim_data, ale);
838                                         items++;
839                                 }
840                         }
841                         
842                         /* only add layers if they will be visible (if drawing channels) */
843                         if ( !(filter_mode & ANIMFILTER_VISIBLE) || (EXPANDED_GPD(gpd)) ) {
844                                 /* loop over layers as the conditions are acceptable */
845                                 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
846                                         /* only if selected */
847                                         if ( ANIMCHANNEL_SELOK(SEL_GPL(gpl)) ) {
848                                                 /* only if editable */
849                                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
850                                                         /* add to list */
851                                                         ale= make_new_animlistelem(gpl, ANIMTYPE_GPLAYER, gpd, ANIMTYPE_GPDATABLOCK);
852                                                         if (ale) {
853                                                                 BLI_addtail(anim_data, ale);
854                                                                 items++;
855                                                         }
856                                                 }
857                                         }
858                                 }
859                         }
860                 }
861         }
862         
863         /* return the number of items added to the list */
864         return items;
865 }
866 #endif 
867
868
869 static int animdata_filter_dopesheet_mats (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
870 {
871         ListBase mats = {NULL, NULL};
872         LinkData *ld;
873         
874         bAnimListElem *ale=NULL;
875         Object *ob= base->object;
876         int items=0, a=0;
877         
878         /* firstly check that we actuallly have some materials, by gathering all materials in a temp list */
879         for (a=0; a < ob->totcol; a++) {
880                 Material *ma= give_current_material(ob, a);
881                 short ok = 0;
882                 
883                 /* for now, if no material returned, skip (this shouldn't confuse the user I hope) */
884                 if (ELEM(NULL, ma, ma->adt)) 
885                         continue;
886                 
887                 /* check if ok */
888                 ANIMDATA_FILTER_CASES(ma, 
889                         ok=1;, 
890                         ok=1;, 
891                         ok=1;)
892                 if (ok == 0) continue;
893                 
894                 /* make a temp list elem for this */
895                 ld= MEM_callocN(sizeof(LinkData), "DopeSheet-MaterialCache");
896                 ld->data= ma;
897                 BLI_addtail(&mats, ld);
898         }
899         
900         /* if there were no channels found, no need to carry on */
901         if (mats.first == NULL)
902                 return 0;
903         
904         /* include materials-expand widget? */
905         if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
906                 ale= make_new_animlistelem(ob, ANIMTYPE_FILLMATD, base, ANIMTYPE_OBJECT, (ID *)ob);
907                 if (ale) {
908                         BLI_addtail(anim_data, ale);
909                         items++;
910                 }
911         }
912         
913         /* add materials? */
914         if (FILTER_MAT_OBJC(ob) || (filter_mode & ANIMFILTER_CURVESONLY)) {
915                 /* for each material in cache, add channels  */
916                 for (ld= mats.first; ld; ld= ld->next) {
917                         Material *ma= (Material *)ld->data;
918                         
919                         /* include material-expand widget? */
920                         // hmm... do we need to store the index of this material in the array anywhere?
921                         if (filter_mode & ANIMFILTER_CHANNELS) {
922                                 ale= make_new_animlistelem(ma, ANIMTYPE_DSMAT, base, ANIMTYPE_OBJECT, (ID *)ma);
923                                 if (ale) {
924                                         BLI_addtail(anim_data, ale);
925                                         items++;
926                                 }
927                         }
928                         
929                         /* add material's animation data */
930                         if (FILTER_MAT_OBJD(ma) || (filter_mode & ANIMFILTER_CURVESONLY)) {
931                                 ANIMDATA_FILTER_CASES(ma, 
932                                         items += animdata_filter_nla(anim_data, ma->adt, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);, 
933                                         items += animdata_filter_fcurves(anim_data, ma->adt->drivers.first, NULL, ma, ANIMTYPE_DSMAT, filter_mode, (ID *)ma);, 
934                                         items += animdata_filter_action(anim_data, ma->adt->action, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);)
935                         }
936                 }
937         }
938         
939         /* free cache */
940         BLI_freelistN(&mats);
941         
942         /* return the number of items added to the list */
943         return items;
944 }
945
946 static int animdata_filter_dopesheet_obdata (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
947 {
948         bAnimListElem *ale=NULL;
949         Object *ob= base->object;
950         IdAdtTemplate *iat= ob->data;
951         AnimData *adt= iat->adt;
952         short type=0, expanded=0;
953         int items= 0;
954         
955         /* get settings based on data type */
956         switch (ob->type) {
957                 case OB_CAMERA: /* ------- Camera ------------ */
958                 {
959                         Camera *ca= (Camera *)ob->data;
960                         
961                         type= ANIMTYPE_DSCAM;
962                         expanded= FILTER_CAM_OBJD(ca);
963                 }
964                         break;
965                 case OB_LAMP: /* ---------- Lamp ----------- */
966                 {
967                         Lamp *la= (Lamp *)ob->data;
968                         
969                         type= ANIMTYPE_DSLAM;
970                         expanded= FILTER_LAM_OBJD(la);
971                 }
972                         break;
973                 case OB_CURVE: /* ------- Curve ---------- */
974                 {
975                         Curve *cu= (Curve *)ob->data;
976                         
977                         type= ANIMTYPE_DSCUR;
978                         expanded= FILTER_CUR_OBJD(cu);
979                 }
980                         break;
981         }
982         
983         /* special exception for drivers instead of action */
984         if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
985                 expanded= EXPANDED_DRVD(adt);
986         
987         /* include data-expand widget? */
988         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {               
989                 ale= make_new_animlistelem(iat, type, base, ANIMTYPE_OBJECT, (ID *)iat);
990                 if (ale) BLI_addtail(anim_data, ale);
991         }
992         
993         /* add object-data animation channels? */
994         if ((expanded) || (filter_mode & ANIMFILTER_CURVESONLY)) {
995                 /* filtering for channels - nla, drivers, keyframes */
996                 ANIMDATA_FILTER_CASES(iat, 
997                         items+= animdata_filter_nla(anim_data, iat->adt, filter_mode, iat, type, (ID *)iat);,
998                         items+= animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, iat, type, filter_mode, (ID *)iat);, 
999                         items += animdata_filter_action(anim_data, iat->adt->action, filter_mode, iat, type, (ID *)iat);)
1000         }
1001         
1002         /* return the number of items added to the list */
1003         return items;
1004 }
1005
1006 static int animdata_filter_dopesheet_ob (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1007 {
1008         bAnimListElem *ale=NULL;
1009         AnimData *adt = NULL;
1010         Object *ob= base->object;
1011         Key *key= ob_get_key(ob);
1012         short obdata_ok = 0;
1013         int items = 0;
1014         
1015         /* add this object as a channel first */
1016         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {
1017                 /* check if filtering by selection */
1018                 if ANIMCHANNEL_SELOK((base->flag & SELECT)) {
1019                         ale= make_new_animlistelem(base, ANIMTYPE_OBJECT, NULL, ANIMTYPE_NONE, NULL);
1020                         if (ale) {
1021                                 BLI_addtail(anim_data, ale);
1022                                 items++;
1023                         }
1024                 }
1025         }
1026         
1027         /* if collapsed, don't go any further (unless adding keyframes only) */
1028         if ( (EXPANDED_OBJC(ob) == 0) && !(filter_mode & ANIMFILTER_CURVESONLY) )
1029                 return items;
1030         
1031         /* Action, Drivers, or NLA */
1032         if (ob->adt) {
1033                 adt= ob->adt;
1034                 ANIMDATA_FILTER_CASES(ob,
1035                         { /* nla */
1036 #if 0
1037                                 /* include nla-expand widget? */
1038                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1039                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLNLA, base, ANIMTYPE_OBJECT, (ID *)ob);
1040                                         if (ale) {
1041                                                 BLI_addtail(anim_data, ale);
1042                                                 items++;
1043                                         }
1044                                 }
1045 #endif
1046                                 
1047                                 /* add NLA tracks */
1048                                 items += animdata_filter_nla(anim_data, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1049                         },
1050                         { /* drivers */
1051                                 /* include drivers-expand widget? */
1052                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1053                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, base, ANIMTYPE_OBJECT, (ID *)ob);
1054                                         if (ale) {
1055                                                 BLI_addtail(anim_data, ale);
1056                                                 items++;
1057                                         }
1058                                 }
1059                                 
1060                                 /* add F-Curve channels (drivers are F-Curves) */
1061                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1062                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1063                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, ob, ANIMTYPE_OBJECT, filter_mode, (ID *)ob);
1064                                 }
1065                         },
1066                         { /* action (keyframes) */
1067                                 /* include action-expand widget? */
1068                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1069                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, base, ANIMTYPE_OBJECT, (ID *)ob);
1070                                         if (ale) {
1071                                                 BLI_addtail(anim_data, ale);
1072                                                 items++;
1073                                         }
1074                                 }
1075                                 
1076                                 /* add F-Curve channels? */
1077                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1078                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1079                                         items += animdata_filter_action(anim_data, adt->action, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob); 
1080                                 }
1081                         }
1082                 )
1083         }
1084         
1085         
1086         /* ShapeKeys? */
1087         if ((key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
1088                 adt= key->adt;
1089                 ANIMDATA_FILTER_CASES(key,
1090                         { /* nla */
1091 #if 0
1092                                 /* include nla-expand widget? */
1093                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1094                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLNLA, base, ANIMTYPE_OBJECT, (ID *)ob);
1095                                         if (ale) {
1096                                                 BLI_addtail(anim_data, ale);
1097                                                 items++;
1098                                         }
1099                                 }
1100 #endif
1101                                 
1102                                 /* add NLA tracks */
1103                                 items += animdata_filter_nla(anim_data, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1104                         },
1105                         { /* drivers */
1106                                 /* include shapekey-expand widget? */
1107                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1108                                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1109                                         if (ale) {
1110                                                 BLI_addtail(anim_data, ale);
1111                                                 items++;
1112                                         }
1113                                 }
1114                                 
1115                                 /* add channels */
1116                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1117                                         items += animdata_filter_shapekey(anim_data, key, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1118                                 }
1119                         },
1120                         { /* action (keyframes) */
1121                                 /* include shapekey-expand widget? */
1122                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1123                                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1124                                         if (ale) {
1125                                                 BLI_addtail(anim_data, ale);
1126                                                 items++;
1127                                         }
1128                                 }
1129                                 
1130                                 /* add channels */
1131                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1132                                         items += animdata_filter_shapekey(anim_data, key, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1133                                 }
1134                         }
1135                 )
1136         }
1137
1138         /* Materials? */
1139         if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT))
1140                 items += animdata_filter_dopesheet_mats(anim_data, ads, base, filter_mode);
1141         
1142         /* Object Data */
1143         switch (ob->type) {
1144                 case OB_CAMERA: /* ------- Camera ------------ */
1145                 {
1146                         Camera *ca= (Camera *)ob->data;
1147                         
1148                         if ((ads->filterflag & ADS_FILTER_NOCAM) == 0) {
1149                                 ANIMDATA_FILTER_CASES(ca,
1150                                         obdata_ok= 1;,
1151                                         obdata_ok= 1;,
1152                                         obdata_ok= 1;)
1153                         }
1154                 }
1155                         break;
1156                 case OB_LAMP: /* ---------- Lamp ----------- */
1157                 {
1158                         Lamp *la= (Lamp *)ob->data;
1159                         
1160                         if ((ads->filterflag & ADS_FILTER_NOLAM) == 0) {
1161                                 ANIMDATA_FILTER_CASES(la,
1162                                         obdata_ok= 1;,
1163                                         obdata_ok= 1;,
1164                                         obdata_ok= 1;)
1165                         }
1166                 }
1167                         break;
1168                 case OB_CURVE: /* ------- Curve ---------- */
1169                 {
1170                         Curve *cu= (Curve *)ob->data;
1171                         
1172                         if ((ads->filterflag & ADS_FILTER_NOCUR) == 0) {
1173                                 ANIMDATA_FILTER_CASES(cu,
1174                                         obdata_ok= 1;,
1175                                         obdata_ok= 1;,
1176                                         obdata_ok= 1;)
1177                         }
1178                 }
1179                         break;
1180         }
1181         if (obdata_ok) 
1182                 items += animdata_filter_dopesheet_obdata(anim_data, ads, base, filter_mode);
1183         
1184         /* return the number of items added to the list */
1185         return items;
1186 }       
1187
1188 static int animdata_filter_dopesheet_scene (ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
1189 {
1190         World *wo= sce->world;
1191         AnimData *adt= NULL;
1192         bAnimListElem *ale;
1193         int items = 0;
1194         
1195         /* add scene as a channel first (even if we aren't showing scenes we still need to show the scene's sub-data */
1196         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {
1197                 /* check if filtering by selection */
1198                 if (ANIMCHANNEL_SELOK( (sce->flag & SCE_DS_SELECTED) )) {
1199                         ale= make_new_animlistelem(sce, ANIMTYPE_SCENE, NULL, ANIMTYPE_NONE, NULL);
1200                         if (ale) {
1201                                 BLI_addtail(anim_data, ale);
1202                                 items++;
1203                         }
1204                 }
1205         }
1206         
1207         /* if collapsed, don't go any further (unless adding keyframes only) */
1208         if ( (EXPANDED_SCEC(sce) == 0) && !(filter_mode & ANIMFILTER_CURVESONLY) )
1209                 return items;
1210                 
1211         /* Action, Drivers, or NLA  for Scene */
1212         if ((ads->filterflag & ADS_FILTER_NOSCE) == 0) {
1213                 adt= sce->adt;
1214                 ANIMDATA_FILTER_CASES(sce,
1215                         { /* nla */
1216 #if 0
1217                                 /* include nla-expand widget? */
1218                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1219                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLNLA, base, ANIMTYPE_SCENE (ID *)sce);
1220                                         if (ale) {
1221                                                 BLI_addtail(anim_data, ale);
1222                                                 items++;
1223                                         }
1224                                 }
1225 #endif
1226                                 
1227                                 /* add NLA tracks */
1228                                 items += animdata_filter_nla(anim_data, adt, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce);
1229                         },
1230                         { /* drivers */
1231                                 /* include drivers-expand widget? */
1232                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1233                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, sce, ANIMTYPE_SCENE, (ID *)sce);
1234                                         if (ale) {
1235                                                 BLI_addtail(anim_data, ale);
1236                                                 items++;
1237                                         }
1238                                 }
1239                                 
1240                                 /* add F-Curve channels (drivers are F-Curves) */
1241                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1242                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, sce, ANIMTYPE_SCENE, filter_mode, (ID *)sce);
1243                                 }
1244                         },
1245                         { /* action */
1246                                 /* include action-expand widget? */
1247                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1248                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, sce, ANIMTYPE_SCENE, (ID *)sce);
1249                                         if (ale) {
1250                                                 BLI_addtail(anim_data, ale);
1251                                                 items++;
1252                                         }
1253                                 }
1254                                 
1255                                 /* add F-Curve channels? */
1256                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1257                                         items += animdata_filter_action(anim_data, adt->action, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce); 
1258                                 }
1259                         }
1260                 )
1261         }
1262         
1263         /* world */
1264         if ((wo && wo->adt) && !(ads->filterflag & ADS_FILTER_NOWOR)) {
1265                 /* Action, Drivers, or NLA  for World */
1266                 adt= wo->adt;
1267                 ANIMDATA_FILTER_CASES(wo,
1268                         { /* nla */
1269 #if 0
1270                                 /* include nla-expand widget? */
1271                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1272                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLNLA, base, ANIMTYPE_DSWOR (ID *)wo);
1273                                         if (ale) {
1274                                                 BLI_addtail(anim_data, ale);
1275                                                 items++;
1276                                         }
1277                                 }
1278 #endif
1279                                 
1280                                 /* add NLA tracks */
1281                                 items += animdata_filter_nla(anim_data, adt, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo);
1282                         },
1283                         { /* drivers */
1284                                 /* include world-expand widget? */
1285                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1286                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)wo);
1287                                         if (ale) {
1288                                                 BLI_addtail(anim_data, ale);
1289                                                 items++;
1290                                         }
1291                                 }
1292                                 
1293                                 /* add F-Curve channels (drivers are F-Curves) */
1294                                 if (FILTER_WOR_SCED(wo)/*EXPANDED_DRVD(adt)*/ || !(filter_mode & ANIMFILTER_CHANNELS)) {
1295                                         // XXX owner info is messed up now...
1296                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, wo, ANIMTYPE_DSWOR, filter_mode, (ID *)wo);
1297                                 }
1298                         },
1299                         { /* action */
1300                                 /* include world-expand widget? */
1301                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1302                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)sce);
1303                                         if (ale) {
1304                                                 BLI_addtail(anim_data, ale);
1305                                                 items++;
1306                                         }
1307                                 }
1308                                 
1309                                 /* add channels */
1310                                 if (FILTER_WOR_SCED(wo) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1311                                         items += animdata_filter_action(anim_data, adt->action, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo); 
1312                                 }
1313                         }
1314                 )
1315         }
1316         
1317         /* return the number of items added to the list */
1318         return items;
1319 }
1320
1321 // 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)
1322 static int animdata_filter_dopesheet (ListBase *anim_data, bDopeSheet *ads, int filter_mode)
1323 {
1324         Scene *sce= (Scene *)ads->source;
1325         Base *base;
1326         int items = 0;
1327         
1328         /* check that we do indeed have a scene */
1329         if ((ads->source == NULL) || (GS(ads->source->name)!=ID_SCE)) {
1330                 printf("DopeSheet Error: Not scene! \n");
1331                 return 0;
1332         }
1333         
1334         /* scene-linked animation */
1335         // TODO: sequencer, composite nodes - are we to include those here too?
1336         {
1337                 short sceOk, worOk;
1338                 
1339                 /* check filtering-flags if ok */
1340                 if (ads->filterflag) {
1341                         if (ads->filterflag & ADS_FILTER_ONLYDRIVERS) {
1342                                 sceOk= (ANIMDATA_HAS_DRIVERS(sce) && !(ads->filterflag & ADS_FILTER_NOSCE));
1343                                 worOk= ((sce->world) && ANIMDATA_HAS_DRIVERS(sce->world) && !(ads->filterflag & ADS_FILTER_NOWOR));
1344                         }
1345                         else {
1346                                 sceOk= (ANIMDATA_HAS_KEYS(sce) && !(ads->filterflag & ADS_FILTER_NOSCE));
1347                                 worOk= ((sce->world) && ANIMDATA_HAS_KEYS(sce->world) && !(ads->filterflag & ADS_FILTER_NOWOR));
1348                         }
1349                 }
1350                 else {
1351                         sceOk= (ANIMDATA_HAS_KEYS(sce));
1352                         worOk= ((sce->world) && ANIMDATA_HAS_KEYS(sce->world));
1353                 }
1354                 
1355                 /* check if not all bad (i.e. so there is something to show) */
1356                 if ( !(!sceOk && !worOk) ) {
1357                         /* add scene data to the list of filtered channels */
1358                         items += animdata_filter_dopesheet_scene(anim_data, ads, sce, filter_mode);
1359                 }
1360         }
1361         
1362         
1363         /* loop over all bases in the scene */
1364         for (base= sce->base.first; base; base= base->next) {
1365                 /* check if there's an object (all the relevant checks are done in the ob-function) */
1366                 if (base->object) {
1367                         Object *ob= base->object;
1368                         Key *key= ob_get_key(ob);
1369                         short actOk=1, keyOk=1, dataOk=1, matOk=1;
1370                         
1371                         /* firstly, check if object can be included, by the following fanimors:
1372                          *      - if only visible, must check for layer and also viewport visibility
1373                          *      - if only selected, must check if object is selected 
1374                          *      - there must be animation data to edit
1375                          */
1376                         // TODO: if cache is implemented, just check name here, and then 
1377                         if (filter_mode & ANIMFILTER_VISIBLE) {
1378                                 /* layer visibility - we check both object and base, since these may not be in sync yet */
1379                                 if ((sce->lay & (ob->lay|base->lay))==0) continue;
1380                                 
1381                                 /* outliner restrict-flag */
1382                                 if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
1383                         }
1384                         
1385                         /* additionally, dopesheet filtering also affects what objects to consider */
1386                         if (ads->filterflag) {
1387                                 /* check selection and object type filters */
1388                                 if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) || (base == sce->basact)) )  {
1389                                         /* only selected should be shown */
1390                                         continue;
1391                                 }
1392                                 
1393                                 /* check filters for datatypes */
1394                                 if (ads->filterflag & ADS_FILTER_ONLYDRIVERS) {
1395                                         actOk= (ANIMDATA_HAS_DRIVERS(ob));
1396                                         keyOk= ((key) && ANIMDATA_HAS_DRIVERS(key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS));
1397                                 }
1398                                 else {
1399                                         actOk= ANIMDATA_HAS_KEYS(ob);
1400                                         keyOk= ((key) && ANIMDATA_HAS_KEYS(key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS));
1401                                 }
1402                                 
1403                                 /* materials - only for geometric types */
1404                                 matOk= 0; /* by default, not ok... */
1405                                 if ( !(ads->filterflag & ADS_FILTER_NOMAT) && (ob->totcol) && 
1406                                          ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) ) 
1407                                 {
1408                                         int a;
1409                                         
1410                                         /* firstly check that we actuallly have some materials */
1411                                         for (a=0; a < ob->totcol; a++) {
1412                                                 Material *ma= give_current_material(ob, a);
1413                                                 
1414                                                 /* if material has relevant animation data, break */
1415                                                 if (ads->filterflag & ADS_FILTER_ONLYDRIVERS) {
1416                                                         if (ANIMDATA_HAS_DRIVERS(ma)) {
1417                                                                 matOk= 1;
1418                                                                 break;
1419                                                         }
1420                                                 }
1421                                                 else {
1422                                                         if (ANIMDATA_HAS_KEYS(ma)) {
1423                                                                 matOk= 1;
1424                                                                 break;
1425                                                         }
1426                                                 }
1427                                         }
1428                                 }
1429                                 
1430                                 /* data */
1431                                 switch (ob->type) {
1432                                         case OB_CAMERA: /* ------- Camera ------------ */
1433                                         {
1434                                                 Camera *ca= (Camera *)ob->data;
1435                                                 if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
1436                                                         dataOk= (ANIMDATA_HAS_DRIVERS(ca) && !(ads->filterflag & ADS_FILTER_NOCAM));
1437                                                 else
1438                                                         dataOk= (ANIMDATA_HAS_KEYS(ca) && !(ads->filterflag & ADS_FILTER_NOCAM));                                               
1439                                         }
1440                                                 break;
1441                                         case OB_LAMP: /* ---------- Lamp ----------- */
1442                                         {
1443                                                 Lamp *la= (Lamp *)ob->data;
1444                                                 if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
1445                                                         dataOk= (ANIMDATA_HAS_DRIVERS(la) && !(ads->filterflag & ADS_FILTER_NOLAM));
1446                                                 else
1447                                                         dataOk= (ANIMDATA_HAS_KEYS(la) && !(ads->filterflag & ADS_FILTER_NOLAM));       
1448                                         }
1449                                                 break;
1450                                         default: /* --- other --- */
1451                                                 dataOk= 0;
1452                                                 break;
1453                                 }
1454                                 
1455                                 /* check if all bad (i.e. nothing to show) */
1456                                 if (!actOk && !keyOk && !dataOk && !matOk)
1457                                         continue;
1458                         }
1459                         else {
1460                                 /* check data-types */
1461                                 actOk= ANIMDATA_HAS_KEYS(ob);
1462                                 keyOk= (key != NULL);
1463                                 
1464                                 /* materials - only for geometric types */
1465                                 matOk= 0; /* by default, not ok... */
1466                                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) && (ob->totcol)) 
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) && ANIMDATA_HAS_KEYS(ma)) {
1475                                                         matOk= 1;
1476                                                         break;
1477                                                 }
1478                                         }
1479                                 }
1480                                 
1481                                 /* data */
1482                                 switch (ob->type) {
1483                                         case OB_CAMERA: /* ------- Camera ------------ */
1484                                         {
1485                                                 Camera *ca= (Camera *)ob->data;
1486                                                 dataOk= ANIMDATA_HAS_KEYS(ca);                                          
1487                                         }
1488                                                 break;
1489                                         case OB_LAMP: /* ---------- Lamp ----------- */
1490                                         {
1491                                                 Lamp *la= (Lamp *)ob->data;
1492                                                 dataOk= ANIMDATA_HAS_KEYS(la);  
1493                                         }
1494                                                 break;
1495                                         case OB_CURVE: /* -------- Curve ---------- */
1496                                         {
1497                                                 Curve *cu= (Curve *)ob->data;
1498                                                 dataOk= ANIMDATA_HAS_KEYS(cu);  
1499                                         }
1500                                                 break;
1501                                         default: /* --- other --- */
1502                                                 dataOk= 0;
1503                                                 break;
1504                                 }
1505                                 
1506                                 /* check if all bad (i.e. nothing to show) */
1507                                 if (!actOk && !keyOk && !dataOk && !matOk)
1508                                         continue;
1509                         }
1510                         
1511                         /* since we're still here, this object should be usable */
1512                         items += animdata_filter_dopesheet_ob(anim_data, ads, base, filter_mode);
1513                 }
1514         }
1515         
1516         /* return the number of items in the list */
1517         return items;
1518 }
1519
1520 /* ----------- Public API --------------- */
1521
1522 /* This function filters the active data source to leave only animation channels suitable for
1523  * usage by the caller. It will return the length of the list 
1524  * 
1525  *      *anim_data: is a pointer to a ListBase, to which the filtered animation channels
1526  *              will be placed for use.
1527  *      filter_mode: how should the data be filtered - bitmapping accessed flags
1528  */
1529 int ANIM_animdata_filter (bAnimContext *ac, ListBase *anim_data, int filter_mode, void *data, short datatype)
1530 {
1531         int items = 0;
1532         
1533         /* only filter data if there's somewhere to put it */
1534         if (data && anim_data) {
1535                 bAnimListElem *ale, *next;
1536                 Object *obact= (ac) ? ac->obact : NULL;
1537                 
1538                 /* firstly filter the data */
1539                 switch (datatype) {
1540                         case ANIMCONT_ACTION:
1541                                 items= animdata_filter_action(anim_data, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
1542                                 break;
1543                                 
1544                         case ANIMCONT_SHAPEKEY:
1545                                 items= animdata_filter_shapekey(anim_data, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
1546                                 break;
1547                                 
1548                         case ANIMCONT_GPENCIL:
1549                                 //items= animdata_filter_gpencil(anim_data, data, filter_mode);
1550                                 break;
1551                                 
1552                         case ANIMCONT_DOPESHEET:
1553                         case ANIMCONT_FCURVES:
1554                         case ANIMCONT_DRIVERS:
1555                         case ANIMCONT_NLA:
1556                                 items= animdata_filter_dopesheet(anim_data, data, filter_mode);
1557                                 break;
1558                 }
1559                         
1560                 /* remove any weedy entries */
1561                 // XXX this is weedy code!
1562                 for (ale= anim_data->first; ale; ale= next) {
1563                         next= ale->next;
1564                         
1565                         if (ale->type == ANIMTYPE_NONE) {
1566                                 items--;
1567                                 BLI_freelinkN(anim_data, ale);
1568                         }
1569                 }
1570         }
1571         
1572         /* return the number of items in the list */
1573         return items;
1574 }
1575
1576 /* ************************************************************ */