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