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