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