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