NLA SoC: Groundwork for NLA UI
[blender.git] / source / blender / editors / animation / anim_filter.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Joshua Leung
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /* This file contains a system used to provide a layer of abstraction between sources
30  * of animation data and tools in Animation Editors. The method used here involves 
31  * generating a list of edit structures which enable tools to naively perform the actions 
32  * they require without all the boiler-plate associated with loops within loops and checking 
33  * for cases to ignore. 
34  *
35  * While this is primarily used for the Action/Dopesheet Editor (and its accessory modes),
36  * the IPO Editor also uses this for it's channel list and for determining which curves
37  * are being edited.
38  *
39  * Note: much of the original system this was based on was built before the creation of the RNA
40  * system. In future, it would be interesting to replace some parts of this code with RNA queries,
41  * however, RNA does not eliminate some of the boiler-plate reduction benefits presented by this 
42  * system, so if any such work does occur, it should only be used for the internals used here...
43  *
44  * -- Joshua Leung, Dec 2008
45  */
46
47 #include <string.h>
48 #include <stdio.h>
49
50 #include "DNA_listBase.h"
51 #include "DNA_ID.h"
52 #include "DNA_anim_types.h"
53 #include "DNA_action_types.h"
54 #include "DNA_constraint_types.h"
55 #include "DNA_camera_types.h"
56 #include "DNA_curve_types.h"
57 #include "DNA_gpencil_types.h"
58 #include "DNA_ipo_types.h"
59 #include "DNA_lamp_types.h"
60 #include "DNA_lattice_types.h"
61 #include "DNA_key_types.h"
62 #include "DNA_material_types.h"
63 #include "DNA_mesh_types.h"
64 #include "DNA_object_types.h"
65 #include "DNA_space_types.h"
66 #include "DNA_scene_types.h"
67 #include "DNA_screen_types.h"
68 #include "DNA_windowmanager_types.h"
69 #include "DNA_world_types.h"
70
71 #include "MEM_guardedalloc.h"
72
73 #include "BLI_blenlib.h"
74
75 #include "BKE_context.h"
76 #include "BKE_global.h"
77 #include "BKE_key.h"
78 #include "BKE_object.h"
79 #include "BKE_material.h"
80 #include "BKE_screen.h"
81 #include "BKE_utildefines.h"
82
83 #include "ED_anim_api.h"
84 #include "ED_types.h"
85 #include "ED_util.h"
86
87 #include "WM_api.h"
88 #include "WM_types.h"
89
90 #include "BIF_gl.h"
91 #include "BIF_glutil.h"
92
93 #include "UI_interface.h"
94 #include "UI_resources.h"
95 #include "UI_view2d.h"
96
97 /* ************************************************************ */
98 /* Blender Context <-> Animation Context mapping */
99
100 /* ----------- Private Stuff - Action Editor ------------- */
101
102 /* Get shapekey data being edited (for Action Editor -> ShapeKey mode) */
103 /* Note: there's a similar function in key.c (ob_get_key) */
104 Key *actedit_get_shapekeys (bAnimContext *ac, SpaceAction *saction) 
105 {
106     Scene *scene= ac->scene;
107     Object *ob;
108     Key *key;
109         
110     ob = OBACT;
111     if (ob == NULL) 
112                 return NULL;
113         
114         /* XXX pinning is not available in 'ShapeKey' mode... */
115         //if (saction->pin) return NULL;
116         
117         /* shapekey data is stored with geometry data */
118         switch (ob->type) {
119                 case OB_MESH:
120                         key= ((Mesh *)ob->data)->key;
121                         break;
122                         
123                 case OB_LATTICE:
124                         key= ((Lattice *)ob->data)->key;
125                         break;
126                         
127                 case OB_CURVE:
128                 case OB_SURF:
129                         key= ((Curve *)ob->data)->key;
130                         break;
131                         
132                 default:
133                         return NULL;
134         }
135         
136         if (key) {
137                 if (key->type == KEY_RELATIVE)
138                         return key;
139         }
140         
141     return NULL;
142 }
143
144 /* Get data being edited in Action Editor (depending on current 'mode') */
145 static short actedit_get_context (bAnimContext *ac, SpaceAction *saction)
146 {
147         /* sync settings with current view status, then return appropriate data */
148         switch (saction->mode) {
149                 case SACTCONT_ACTION: /* 'Action Editor' */
150                         /* if not pinned, sync with active object */
151                         if (saction->pin == 0) {
152                                 if (ac->obact && ac->obact->adt)
153                                         saction->action = ac->obact->adt->action;
154                                 else
155                                         saction->action= NULL;
156                         }
157                         
158                         ac->datatype= ANIMCONT_ACTION;
159                         ac->data= saction->action;
160                         
161                         ac->mode= saction->mode;
162                         return 1;
163                         
164                 case SACTCONT_SHAPEKEY: /* 'ShapeKey Editor' */
165                         ac->datatype= ANIMCONT_SHAPEKEY;
166                         ac->data= actedit_get_shapekeys(ac, saction);
167                         
168                         ac->mode= saction->mode;
169                         return 1;
170                         
171                 case SACTCONT_GPENCIL: /* Grease Pencil */ // XXX review how this mode is handled...
172                         ac->datatype=ANIMCONT_GPENCIL;
173                         //ac->data= CTX_wm_screen(C); // FIXME: add that dopesheet type thing here!
174                         ac->data= NULL; // !!!
175                         
176                         ac->mode= saction->mode;
177                         return 1;
178                         
179                 case SACTCONT_DOPESHEET: /* DopeSheet */
180                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
181                         saction->ads.source= (ID *)ac->scene;
182                         
183                         ac->datatype= ANIMCONT_DOPESHEET;
184                         ac->data= &saction->ads;
185                         
186                         ac->mode= saction->mode;
187                         return 1;
188                 
189                 default: /* unhandled yet */
190                         ac->datatype= ANIMCONT_NONE;
191                         ac->data= NULL;
192                         
193                         ac->mode= -1;
194                         return 0;
195         }
196 }
197
198 /* ----------- Private Stuff - Graph Editor ------------- */
199
200 /* Get data being edited in Graph Editor (depending on current 'mode') */
201 static short graphedit_get_context (bAnimContext *ac, SpaceIpo *sipo)
202 {
203         /* init dopesheet data if non-existant (i.e. for old files) */
204         if (sipo->ads == NULL)
205                 sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
206         
207         /* sync settings with current view status, then return appropriate data */
208         switch (sipo->mode) {
209                 case SIPO_MODE_ANIMATION:       /* Animation F-Curve Editor */
210                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
211                         sipo->ads->source= (ID *)ac->scene;
212                         sipo->ads->filterflag &= ~ADS_FILTER_ONLYDRIVERS;
213                         
214                         ac->datatype= ANIMCONT_FCURVES;
215                         ac->data= sipo->ads;
216                         
217                         ac->mode= sipo->mode;
218                         return 1;
219                 
220                 case SIPO_MODE_DRIVERS:         /* Driver F-Curve Editor */
221                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
222                         sipo->ads->source= (ID *)ac->scene;
223                         sipo->ads->filterflag |= ADS_FILTER_ONLYDRIVERS;
224                         
225                         ac->datatype= ANIMCONT_DRIVERS;
226                         ac->data= sipo->ads;
227                         
228                         ac->mode= sipo->mode;
229                         return 1;
230                 
231                 default: /* unhandled yet */
232                         ac->datatype= ANIMCONT_NONE;
233                         ac->data= NULL;
234                         
235                         ac->mode= -1;
236                         return 0;
237         }
238 }
239
240 /* ----------- Private Stuff - NLA Editor ------------- */
241
242 /* Get data being edited in Graph Editor (depending on current 'mode') */
243 static short nlaedit_get_context (bAnimContext *ac, SpaceNla *snla)
244 {
245         /* init dopesheet data if non-existant (i.e. for old files) */
246         if (snla->ads == NULL)
247                 snla->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
248         
249         /* sync settings with current view status, then return appropriate data */
250         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
251         snla->ads->source= (ID *)ac->scene;
252         snla->ads->filterflag |= ADS_FILTER_ONLYNLA;
253         
254         ac->datatype= ANIMCONT_NLA;
255         ac->data= snla->ads;
256         
257         return 1;
258 }
259
260 /* ----------- Public API --------------- */
261
262 /* Obtain current anim-data context, given that context info from Blender context has already been set 
263  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
264  *        allocation/freeing costs (which are not that avoidable with channels).
265  */
266 short ANIM_animdata_context_getdata (bAnimContext *ac)
267 {
268         ScrArea *sa= ac->sa;
269         short ok= 0;
270         
271         /* context depends on editor we are currently in */
272         if (sa) {
273                 switch (sa->spacetype) {
274                         case SPACE_ACTION:
275                         {
276                                 SpaceAction *saction= (SpaceAction *)sa->spacedata.first;
277                                 ok= actedit_get_context(ac, saction);
278                         }
279                                 break;
280                                 
281                         case SPACE_IPO:
282                         {
283                                 SpaceIpo *sipo= (SpaceIpo *)sa->spacedata.first;
284                                 ok= graphedit_get_context(ac, sipo);
285                         }
286                                 break;
287                                 
288                         case SPACE_NLA:
289                         {
290                                 SpaceNla *snla= (SpaceNla *)sa->spacedata.first;
291                                 ok= nlaedit_get_context(ac, snla);
292                         }
293                                 break;
294                 }
295         }
296         
297         /* check if there's any valid data */
298         if (ok && ac->data)
299                 return 1;
300         else
301                 return 0;
302 }
303
304 /* Obtain current anim-data context from Blender Context info 
305  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
306  *        allocation/freeing costs (which are not that avoidable with channels).
307  *      - Clears data and sets the information from Blender Context which is useful
308  */
309 short ANIM_animdata_get_context (const bContext *C, bAnimContext *ac)
310 {
311         ScrArea *sa= CTX_wm_area(C);
312         ARegion *ar= CTX_wm_region(C);
313         Scene *scene= CTX_data_scene(C);
314         
315         /* clear old context info */
316         if (ac == NULL) return 0;
317         memset(ac, 0, sizeof(bAnimContext));
318         
319         /* get useful default context settings from context */
320         ac->scene= scene;
321         if (scene) {
322                 ac->markers= &scene->markers;           
323                 ac->obact= (scene->basact)?  scene->basact->object : NULL;
324         }
325         ac->sa= sa;
326         ac->ar= ar;
327         ac->spacetype= (sa) ? sa->spacetype : 0;
328         ac->regiontype= (ar) ? ar->regiontype : 0;
329         
330         /* get data context info */
331         return ANIM_animdata_context_getdata(ac);
332 }
333
334 /* ************************************************************ */
335 /* Blender Data <-- Filter --> Channels to be operated on */
336
337 /* quick macro to test if AnimData is usable */
338 #define ANIMDATA_HAS_KEYS(id) ((id)->adt && (id)->adt->action)
339
340 /* quick macro to test if AnimData is usable for drivers */
341 #define ANIMDATA_HAS_DRIVERS(id) ((id)->adt && (id)->adt->drivers.first)
342
343 /* quick macro to test if AnimData is usable for NLA */
344 #define ANIMDATA_HAS_NLA(id) ((id)->adt && (id)->adt->nla_tracks.first)
345
346 /* quick macro to test for all three avove usability tests, performing the appropriate provided 
347  * action for each when the AnimData context is appropriate. 
348  *
349  * Priority order for this goes (most important, to least): NLA, Drivers, Keyframes
350  *
351  *      - id: ID block which should have an AnimData pointer following it immediately, to use
352  *      - nlaOk: line or block of code to execute for NLA case
353  *      - driversOk: line or block of code to execute for Drivers case
354  *      - keysOk: line or block of code for Keyframes case
355  */
356 #define ANIMDATA_FILTER_CASES(id, nlaOk, driversOk, keysOk) \
357         {\
358                 if (ads->filterflag & ADS_FILTER_ONLYNLA) {\
359                         if (ANIMDATA_HAS_NLA(id)) {\
360                                 nlaOk\
361                         }\
362                 }\
363                 else if (ads->filterflag & ADS_FILTER_ONLYDRIVERS) {\
364                         if (ANIMDATA_HAS_DRIVERS(id)) {\
365                                 driversOk\
366                         }\
367                 }\
368                 else {\
369                         if (ANIMDATA_HAS_KEYS(id)) {\
370                                 keysOk\
371                         }\
372                 }\
373         }
374
375
376 /* quick macro to test if a anim-channel (F-Curve, Group, etc.) is selected in an acceptable way */
377 #define ANIMCHANNEL_SELOK(test_func) \
378                 ( !(filter_mode & (ANIMFILTER_SEL|ANIMFILTER_UNSEL)) || \
379                   ((filter_mode & ANIMFILTER_SEL) && test_func) || \
380                   ((filter_mode & ANIMFILTER_UNSEL) && test_func==0) ) 
381
382 /* ----------- 'Private' Stuff --------------- */
383
384 /* this function allocates memory for a new bAnimListElem struct for the 
385  * provided animation channel-data. 
386  */
387 bAnimListElem *make_new_animlistelem (void *data, short datatype, void *owner, short ownertype, ID *owner_id)
388 {
389         bAnimListElem *ale= NULL;
390         
391         /* only allocate memory if there is data to convert */
392         if (data) {
393                 /* allocate and set generic data */
394                 ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
395                 
396                 ale->data= data;
397                 ale->type= datatype;
398                         // XXX what is the point of the owner data?
399                 ale->owner= owner;
400                 ale->ownertype= ownertype;
401                 
402                 ale->id= owner_id;
403                 
404                 /* do specifics */
405                 switch (datatype) {
406                         case ANIMTYPE_SCENE:
407                         {
408                                 Scene *sce= (Scene *)data;
409                                 
410                                 ale->flag= sce->flag;
411                                 
412                                 ale->key_data= sce;
413                                 ale->datatype= ALE_SCE;
414                         }
415                                 break;
416                         case ANIMTYPE_OBJECT:
417                         {
418                                 Base *base= (Base *)data;
419                                 Object *ob= base->object;
420                                 
421                                 ale->flag= ob->flag;
422                                 
423                                 ale->key_data= ob;
424                                 ale->datatype= ALE_OB;
425                         }
426                                 break;
427                         case ANIMTYPE_FILLACTD:
428                         {
429                                 bAction *act= (bAction *)data;
430                                 
431                                 ale->flag= act->flag;
432                                 
433                                 ale->key_data= act;
434                                 ale->datatype= ALE_ACT;
435                         }
436                                 break;
437                         case ANIMTYPE_FILLDRIVERS:
438                         {
439                                 AnimData *adt= (AnimData *)data;
440                                 
441                                 ale->flag= adt->flag;
442                                 
443                                         // XXX... drivers don't show summary for now
444                                 ale->key_data= NULL;
445                                 ale->datatype= ALE_NONE;
446                         }
447                                 break;
448                         case ANIMTYPE_FILLMATD:
449                         {
450                                 Object *ob= (Object *)data;
451                                 
452                                 ale->flag= FILTER_MAT_OBJC(ob);
453                                 
454                                 ale->key_data= NULL;
455                                 ale->datatype= ALE_NONE;
456                         }
457                                 break;
458                         
459                         case ANIMTYPE_DSMAT:
460                         {
461                                 Material *ma= (Material *)data;
462                                 AnimData *adt= ma->adt;
463                                 
464                                 ale->flag= FILTER_MAT_OBJD(ma);
465                                 
466                                 ale->key_data= (adt) ? adt->action : NULL;
467                                 ale->datatype= ALE_ACT;
468                         }
469                                 break;
470                         case ANIMTYPE_DSLAM:
471                         {
472                                 Lamp *la= (Lamp *)data;
473                                 AnimData *adt= la->adt;
474                                 
475                                 ale->flag= FILTER_LAM_OBJD(la);
476                                 
477                                 ale->key_data= (adt) ? adt->action : NULL;
478                                 ale->datatype= ALE_ACT;
479                         }
480                                 break;
481                         case ANIMTYPE_DSCAM:
482                         {
483                                 Camera *ca= (Camera *)data;
484                                 AnimData *adt= ca->adt;
485                                 
486                                 ale->flag= FILTER_CAM_OBJD(ca);
487                                 
488                                 ale->key_data= (adt) ? adt->action : NULL;
489                                 ale->datatype= ALE_ACT;
490                         }
491                                 break;
492                         case ANIMTYPE_DSCUR:
493                         {
494                                 Curve *cu= (Curve *)data;
495                                 AnimData *adt= cu->adt;
496                                 
497                                 ale->flag= FILTER_CUR_OBJD(cu);
498                                 
499                                 ale->key_data= (adt) ? adt->action : NULL;
500                                 ale->datatype= ALE_ACT;
501                         }
502                                 break;
503                         case ANIMTYPE_DSSKEY:
504                         {
505                                 Key *key= (Key *)data;
506                                 AnimData *adt= key->adt;
507                                 
508                                 ale->flag= FILTER_SKE_OBJD(key); 
509                                 
510                                 ale->key_data= (adt) ? adt->action : NULL;
511                                 ale->datatype= ALE_ACT;
512                         }
513                                 break;
514                         case ANIMTYPE_DSWOR:
515                         {
516                                 World *wo= (World *)data;
517                                 AnimData *adt= wo->adt;
518                                 
519                                 ale->flag= FILTER_WOR_SCED(wo); 
520                                 
521                                 ale->key_data= (adt) ? adt->action : NULL;
522                                 ale->datatype= ALE_ACT;
523                         }
524                                 break;
525                                 
526                         case ANIMTYPE_GROUP:
527                         {
528                                 bActionGroup *agrp= (bActionGroup *)data;
529                                 
530                                 ale->flag= agrp->flag;
531                                 
532                                 ale->key_data= NULL;
533                                 ale->datatype= ALE_GROUP;
534                         }
535                                 break;
536                         case ANIMTYPE_FCURVE:
537                         {
538                                 FCurve *fcu= (FCurve *)data;
539                                 
540                                 ale->flag= fcu->flag;
541                                 
542                                 ale->key_data= fcu;
543                                 ale->datatype= ALE_FCURVE;
544                         }
545                                 break;
546                         
547                         case ANIMTYPE_GPLAYER:
548                         {
549                                 bGPDlayer *gpl= (bGPDlayer *)data;
550                                 
551                                 ale->flag= gpl->flag;
552                                 
553                                 ale->key_data= NULL;
554                                 ale->datatype= ALE_GPFRAME;
555                         }
556                                 break;
557                                 
558                         case ANIMTYPE_NLATRACK:
559                         {
560                                 NlaTrack *nlt= (NlaTrack *)data;
561                                 
562                                 ale->flag= nlt->flag;
563                                 
564                                         // XXX or should this be done some other way?
565                                 ale->key_data= &nlt->strips;
566                                 ale->datatype= ALE_NLASTRIP;
567                         }
568                                 break;
569                 }
570         }
571         
572         /* return created datatype */
573         return ale;
574 }
575  
576 /* ----------------------------------------- */
577
578
579 static int animdata_filter_fcurves (ListBase *anim_data, FCurve *first, bActionGroup *grp, void *owner, short ownertype, int filter_mode, ID *owner_id)
580 {
581         bAnimListElem *ale = NULL;
582         FCurve *fcu;
583         int items = 0;
584         
585         /* loop over F-Curves - assume that the caller of this has already checked that these should be included 
586          * NOTE: we need to check if the F-Curves belong to the same group, as this gets called for groups too...
587          */
588         for (fcu= first; ((fcu) && (fcu->grp==grp)); fcu= fcu->next) {
589                 /* only include if visible (Graph Editor check, not channels check) */
590                 if (!(filter_mode & ANIMFILTER_CURVEVISIBLE) || (fcu->flag & FCURVE_VISIBLE)) {
591                         /* only work with this channel and its subchannels if it is editable */
592                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_FCU(fcu)) {
593                                 /* only include this curve if selected in a way consistent with the filtering requirements */
594                                 if ( ANIMCHANNEL_SELOK(SEL_FCU(fcu)) ) {
595                                         /* only include if this curve is active */
596                                         if (!(filter_mode & ANIMFILTER_ACTIVE) || (fcu->flag & FCURVE_ACTIVE)) {
597                                                 /* owner/ownertype will be either object or action-channel, depending if it was dopesheet or part of an action */
598                                                 ale= make_new_animlistelem(fcu, ANIMTYPE_FCURVE, owner, ownertype, owner_id);
599                                                 
600                                                 if (ale) {
601                                                         BLI_addtail(anim_data, ale);
602                                                         items++;
603                                                 }
604                                         }
605                                 }
606                         }
607                 }
608         }
609         
610         /* return the number of items added to the list */
611         return items;
612 }
613
614 static int animdata_filter_action (ListBase *anim_data, bAction *act, int filter_mode, void *owner, short ownertype, ID *owner_id)
615 {
616         bAnimListElem *ale=NULL;
617         bActionGroup *agrp;
618         FCurve *lastchan=NULL;
619         int items = 0;
620         
621         /* loop over groups */
622         //       XXX in future, we need to be prepared for nestled groups...
623         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
624                 /* add this group as a channel first */
625                 if ((filter_mode & ANIMFILTER_CHANNELS) || !(filter_mode & ANIMFILTER_CURVESONLY)) {
626                         /* check if filtering by selection */
627                         if ( ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) ) {
628                                 ale= make_new_animlistelem(agrp, ANIMTYPE_GROUP, NULL, ANIMTYPE_NONE, owner_id);
629                                 if (ale) {
630                                         BLI_addtail(anim_data, ale);
631                                         items++;
632                                 }
633                         }
634                 }
635                 
636                 /* store reference to last channel of group */
637                 if (agrp->channels.last) 
638                         lastchan= agrp->channels.last;
639                 
640                 
641                 /* there are some situations, where only the channels of the action group should get considered */
642                 if (!(filter_mode & ANIMFILTER_ACTGROUPED) || (agrp->flag & AGRP_ACTIVE)) {
643                         /* filters here are a bit convoulted...
644                          *      - groups show a "summary" of keyframes beside their name which must accessable for tools which handle keyframes
645                          *      - groups can be collapsed (and those tools which are only interested in channels rely on knowing that group is closed)
646                          *
647                          * cases when we should include F-Curves inside group:
648                          *      - we don't care about visibility
649                          *      - group is expanded
650                          *      - we're interested in keyframes, but not if they appear in selected channels
651                          */
652                         // XXX what was the selection check here for again?
653                         if ( (!(filter_mode & ANIMFILTER_VISIBLE) || EXPANDED_AGRP(agrp)) || 
654                                  ( /*ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) &&*/ (filter_mode & ANIMFILTER_CURVESONLY) ) ) 
655                         {
656                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_AGRP(agrp)) {
657                                         // XXX the 'owner' info here needs review...
658                                         items += animdata_filter_fcurves(anim_data, agrp->channels.first, agrp, owner, ownertype, filter_mode, owner_id);
659                                         
660                                         /* remove group from filtered list if last element is group 
661                                          * (i.e. only if group had channels, which were all hidden)
662                                          */
663                                         // XXX this is really hacky... it should be fixed in a much more elegant way!
664                                         if ( (ale) && (anim_data->last == ale) && 
665                                                  (ale->data == agrp) && (agrp->channels.first) ) 
666                                         {
667                                                 BLI_freelinkN(anim_data, ale);
668                                                 items--;
669                                         }
670                                 }
671                         }
672                 }
673         }
674         
675         /* loop over un-grouped F-Curves (only if we're not only considering those channels in the animive group) */
676         if (!(filter_mode & ANIMFILTER_ACTGROUPED))  {
677                 // XXX the 'owner' info here needs review...
678                 items += animdata_filter_fcurves(anim_data, (lastchan)?(lastchan->next):(act->curves.first), NULL, owner, ownertype, filter_mode, owner_id);
679         }
680         
681         /* return the number of items added to the list */
682         return items;
683 }
684
685 static int animdata_filter_nla (ListBase *anim_data, NlaTrack *first, int filter_mode, void *owner, short ownertype, ID *owner_id)
686 {
687         // FIXME
688         return 0;
689 }
690
691 static int animdata_filter_shapekey (ListBase *anim_data, Key *key, int filter_mode, void *owner, short ownertype, ID *owner_id)
692 {
693         bAnimListElem *ale;
694         KeyBlock *kb;
695         //FCurve *fcu;
696         int i, items=0;
697         
698         /* are we filtering for display or editing */
699         if (filter_mode & ANIMFILTER_CHANNELS) {
700                 /* for display - loop over shapekeys, adding ipo-curve references where needed */
701                 kb= key->block.first;
702                 
703                 /* loop through possible shapekeys, manually creating entries */
704                 for (i= 1; i < key->totkey; i++) {
705                         ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
706                         kb = kb->next; /* do this even on the first try, as the first is 'Basis' (which doesn't get included) */
707                         
708                         ale->data= kb;
709                         ale->type= ANIMTYPE_SHAPEKEY; /* 'abused' usage of this type */
710                         ale->owner= key;
711                         ale->ownertype= ANIMTYPE_SHAPEKEY;
712                         ale->datatype= ALE_NONE;
713                         ale->index = i;
714                         
715 #if 0 // XXX fixme... old system
716                         if (key->ipo) {
717                                 for (icu= key->ipo->curve.first; icu; icu=icu->next) {
718                                         if (icu->adrcode == i) {
719                                                 ale->key_data= icu;
720                                                 ale->datatype= ALE_ICU;
721                                                 break;
722                                         }
723                                 }
724                         }
725 #endif // XXX fixme... old system
726                         
727                         ale->id= owner_id;
728                         
729                         BLI_addtail(anim_data, ale);
730                         items++;
731                 }
732         }
733         else {
734 #if 0 // XXX fixme... old system
735                 /* loop over ipo curves if present - for editing */
736                 if (key->ipo) {
737                         if (filter_mode & ANIMFILTER_IPOKEYS) {
738                                 ale= make_new_animlistelem(key->ipo, ANIMTYPE_IPO, key, ANIMTYPE_SHAPEKEY);
739                                 if (ale) {
740                                         if (owned) ale->id= owner;
741                                         BLI_addtail(anim_data, ale);
742                                         items++;
743                                 }
744                         }
745                         else {
746                                 items += animdata_filter_ipocurves(anim_data, key->ipo, filter_mode, key, ANIMTYPE_SHAPEKEY, (owned)?(owner):(NULL));
747                         }
748                 }
749 #endif // XXX fixme... old system
750         }
751         
752         /* return the number of items added to the list */
753         return items;
754 }
755  
756 #if 0
757 // FIXME: switch this to use the bDopeSheet...
758 static int animdata_filter_gpencil (ListBase *anim_data, bScreen *sc, int filter_mode)
759 {
760         bAnimListElem *ale;
761         ScrArea *sa, *curarea;
762         bGPdata *gpd;
763         bGPDlayer *gpl;
764         int items = 0;
765         
766         /* check if filtering types are appropriate */
767         {
768                 /* special hack for fullscreen area (which must be this one then):
769                  *      - we use the curarea->full as screen to get spaces from, since the
770                  *        old (pre-fullscreen) screen was stored there...
771                  *      - this is needed as all data would otherwise disappear
772                  */
773                 // XXX need to get new alternative for curarea
774                 if ((curarea->full) && (curarea->spacetype==SPACE_ACTION))
775                         sc= curarea->full;
776                 
777                 /* loop over spaces in current screen, finding gpd blocks (could be slow!) */
778                 for (sa= sc->areabase.first; sa; sa= sa->next) {
779                         /* try to get gp data */
780                         // XXX need to put back grease pencil api...
781                         gpd= gpencil_data_getactive(sa);
782                         if (gpd == NULL) continue;
783                         
784                         /* add gpd as channel too (if for drawing, and it has layers) */
785                         if ((filter_mode & ANIMFILTER_CHANNELS) && (gpd->layers.first)) {
786                                 /* add to list */
787                                 ale= make_new_animlistelem(gpd, ANIMTYPE_GPDATABLOCK, sa, ANIMTYPE_SPECIALDATA);
788                                 if (ale) {
789                                         BLI_addtail(anim_data, ale);
790                                         items++;
791                                 }
792                         }
793                         
794                         /* only add layers if they will be visible (if drawing channels) */
795                         if ( !(filter_mode & ANIMFILTER_VISIBLE) || (EXPANDED_GPD(gpd)) ) {
796                                 /* loop over layers as the conditions are acceptable */
797                                 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
798                                         /* only if selected */
799                                         if ( ANIMCHANNEL_SELOK(SEL_GPL(gpl)) ) {
800                                                 /* only if editable */
801                                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
802                                                         /* add to list */
803                                                         ale= make_new_animlistelem(gpl, ANIMTYPE_GPLAYER, gpd, ANIMTYPE_GPDATABLOCK);
804                                                         if (ale) {
805                                                                 BLI_addtail(anim_data, ale);
806                                                                 items++;
807                                                         }
808                                                 }
809                                         }
810                                 }
811                         }
812                 }
813         }
814         
815         /* return the number of items added to the list */
816         return items;
817 }
818 #endif 
819
820
821 static int animdata_filter_dopesheet_mats (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
822 {
823         ListBase mats = {NULL, NULL};
824         LinkData *ld;
825         
826         bAnimListElem *ale=NULL;
827         Object *ob= base->object;
828         int items=0, a=0;
829         
830         /* firstly check that we actuallly have some materials, by gathering all materials in a temp list */
831         for (a=0; a < ob->totcol; a++) {
832                 Material *ma= give_current_material(ob, a);
833                 short ok = 0;
834                 
835                 /* for now, if no material returned, skip (this shouldn't confuse the user I hope) */
836                 if (ELEM(NULL, ma, ma->adt)) 
837                         continue;
838                 
839                 /* check if ok */
840                 ANIMDATA_FILTER_CASES(ma, 
841                         ok=1;, 
842                         ok=1;, 
843                         ok=1;)
844                 if (ok == 0) continue;
845                 
846                 /* make a temp list elem for this */
847                 ld= MEM_callocN(sizeof(LinkData), "DopeSheet-MaterialCache");
848                 ld->data= ma;
849                 BLI_addtail(&mats, ld);
850         }
851         
852         /* if there were no channels found, no need to carry on */
853         if (mats.first == NULL)
854                 return 0;
855         
856         /* include materials-expand widget? */
857         if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
858                 ale= make_new_animlistelem(ob, ANIMTYPE_FILLMATD, base, ANIMTYPE_OBJECT, (ID *)ob);
859                 if (ale) {
860                         BLI_addtail(anim_data, ale);
861                         items++;
862                 }
863         }
864         
865         /* add materials? */
866         if (FILTER_MAT_OBJC(ob) || (filter_mode & ANIMFILTER_CURVESONLY)) {
867                 /* for each material in cache, add channels  */
868                 for (ld= mats.first; ld; ld= ld->next) {
869                         Material *ma= (Material *)ld->data;
870                         
871                         /* include material-expand widget? */
872                         // hmm... do we need to store the index of this material in the array anywhere?
873                         if (filter_mode & ANIMFILTER_CHANNELS) {
874                                 ale= make_new_animlistelem(ma, ANIMTYPE_DSMAT, base, ANIMTYPE_OBJECT, (ID *)ma);
875                                 if (ale) {
876                                         BLI_addtail(anim_data, ale);
877                                         items++;
878                                 }
879                         }
880                         
881                         /* add material's animation data */
882                         if (FILTER_MAT_OBJD(ma) || (filter_mode & ANIMFILTER_CURVESONLY)) {
883                                 ANIMDATA_FILTER_CASES(ma, 
884                                         items += animdata_filter_nla(anim_data, ma->adt->nla_tracks.first, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);, 
885                                         items += animdata_filter_fcurves(anim_data, ma->adt->drivers.first, NULL, ma, ANIMTYPE_DSMAT, filter_mode, (ID *)ma);, 
886                                         items += animdata_filter_action(anim_data, ma->adt->action, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);)
887                         }
888                 }
889         }
890         
891         /* free cache */
892         BLI_freelistN(&mats);
893         
894         /* return the number of items added to the list */
895         return items;
896 }
897
898 static int animdata_filter_dopesheet_obdata (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
899 {
900         bAnimListElem *ale=NULL;
901         Object *ob= base->object;
902         IdAdtTemplate *iat= ob->data;
903         AnimData *adt= iat->adt;
904         short type=0, expanded=0;
905         int items= 0;
906         
907         /* get settings based on data type */
908         switch (ob->type) {
909                 case OB_CAMERA: /* ------- Camera ------------ */
910                 {
911                         Camera *ca= (Camera *)ob->data;
912                         
913                         type= ANIMTYPE_DSCAM;
914                         expanded= FILTER_CAM_OBJD(ca);
915                 }
916                         break;
917                 case OB_LAMP: /* ---------- Lamp ----------- */
918                 {
919                         Lamp *la= (Lamp *)ob->data;
920                         
921                         type= ANIMTYPE_DSLAM;
922                         expanded= FILTER_LAM_OBJD(la);
923                 }
924                         break;
925                 case OB_CURVE: /* ------- Curve ---------- */
926                 {
927                         Curve *cu= (Curve *)ob->data;
928                         
929                         type= ANIMTYPE_DSCUR;
930                         expanded= FILTER_CUR_OBJD(cu);
931                 }
932                         break;
933         }
934         
935         /* special exception for drivers instead of action */
936         if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
937                 expanded= EXPANDED_DRVD(adt);
938         
939         /* include data-expand widget? */
940         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {               
941                 ale= make_new_animlistelem(iat, type, base, ANIMTYPE_OBJECT, (ID *)iat);
942                 if (ale) BLI_addtail(anim_data, ale);
943         }
944         
945         /* add object-data animation channels? */
946         if ((expanded) || (filter_mode & ANIMFILTER_CURVESONLY)) {
947                 /* filtering for channels - nla, drivers, keyframes */
948                 ANIMDATA_FILTER_CASES(iat, 
949                         items+= animdata_filter_nla(anim_data, iat->adt->nla_tracks.first, filter_mode, iat, type, (ID *)iat);,
950                         items+= animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, iat, type, filter_mode, (ID *)iat);, 
951                         items += animdata_filter_action(anim_data, iat->adt->action, filter_mode, iat, type, (ID *)iat);)
952         }
953         
954         /* return the number of items added to the list */
955         return items;
956 }
957
958 static int animdata_filter_dopesheet_ob (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
959 {
960         bAnimListElem *ale=NULL;
961         AnimData *adt = NULL;
962         Object *ob= base->object;
963         Key *key= ob_get_key(ob);
964         short obdata_ok = 0;
965         int items = 0;
966         
967         /* add this object as a channel first */
968         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {
969                 /* check if filtering by selection */
970                 if ANIMCHANNEL_SELOK((base->flag & SELECT)) {
971                         ale= make_new_animlistelem(base, ANIMTYPE_OBJECT, NULL, ANIMTYPE_NONE, NULL);
972                         if (ale) {
973                                 BLI_addtail(anim_data, ale);
974                                 items++;
975                         }
976                 }
977         }
978         
979         /* if collapsed, don't go any further (unless adding keyframes only) */
980         if ( (EXPANDED_OBJC(ob) == 0) && !(filter_mode & ANIMFILTER_CURVESONLY) )
981                 return items;
982         
983         /* Action, Drivers, or NLA */
984         if (ob->adt) {
985                 adt= ob->adt;
986                 ANIMDATA_FILTER_CASES(ob,
987                         { /* nla */
988 #if 0
989                                 /* include nla-expand widget? */
990                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
991                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLNLA, base, ANIMTYPE_OBJECT, (ID *)ob);
992                                         if (ale) {
993                                                 BLI_addtail(anim_data, ale);
994                                                 items++;
995                                         }
996                                 }
997 #endif
998                                 
999                                 /* add NLA tracks */
1000                                 if (/*EXPANDED_NLTD(adt) ||*/ !(filter_mode & ANIMFILTER_CHANNELS))
1001                                         items += animdata_filter_nla(anim_data, adt->nla_tracks.first, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1002                         },
1003                         { /* drivers */
1004                                 /* include drivers-expand widget? */
1005                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1006                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, base, ANIMTYPE_OBJECT, (ID *)ob);
1007                                         if (ale) {
1008                                                 BLI_addtail(anim_data, ale);
1009                                                 items++;
1010                                         }
1011                                 }
1012                                 
1013                                 /* add F-Curve channels (drivers are F-Curves) */
1014                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1015                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1016                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, ob, ANIMTYPE_OBJECT, filter_mode, (ID *)ob);
1017                                 }
1018                         },
1019                         { /* action (keyframes) */
1020                                 /* include action-expand widget? */
1021                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1022                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, base, ANIMTYPE_OBJECT, (ID *)ob);
1023                                         if (ale) {
1024                                                 BLI_addtail(anim_data, ale);
1025                                                 items++;
1026                                         }
1027                                 }
1028                                 
1029                                 /* add F-Curve channels? */
1030                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1031                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1032                                         items += animdata_filter_action(anim_data, adt->action, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob); 
1033                                 }
1034                         }
1035                 )
1036         }
1037         
1038         
1039         /* ShapeKeys? */
1040         if ((key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
1041                 adt= key->adt;
1042                 ANIMDATA_FILTER_CASES(key,
1043                         { /* nla */
1044 #if 0
1045                                 /* include nla-expand widget? */
1046                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1047                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLNLA, base, ANIMTYPE_OBJECT, (ID *)ob);
1048                                         if (ale) {
1049                                                 BLI_addtail(anim_data, ale);
1050                                                 items++;
1051                                         }
1052                                 }
1053 #endif
1054                                 
1055                                 /* add NLA tracks */
1056                                 if (/*EXPANDED_NLTD(adt) ||*/ !(filter_mode & ANIMFILTER_CHANNELS))
1057                                         items += animdata_filter_nla(anim_data, adt->nla_tracks.first, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1058                         },
1059                         { /* drivers */
1060                                 /* include shapekey-expand widget? */
1061                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1062                                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1063                                         if (ale) {
1064                                                 BLI_addtail(anim_data, ale);
1065                                                 items++;
1066                                         }
1067                                 }
1068                                 
1069                                 /* add channels */
1070                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1071                                         items += animdata_filter_shapekey(anim_data, key, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1072                                 }
1073                         },
1074                         { /* action (keyframes) */
1075                                 /* include shapekey-expand widget? */
1076                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1077                                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1078                                         if (ale) {
1079                                                 BLI_addtail(anim_data, ale);
1080                                                 items++;
1081                                         }
1082                                 }
1083                                 
1084                                 /* add channels */
1085                                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1086                                         items += animdata_filter_shapekey(anim_data, key, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1087                                 }
1088                         }
1089                 )
1090         }
1091
1092         /* Materials? */
1093         if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT))
1094                 items += animdata_filter_dopesheet_mats(anim_data, ads, base, filter_mode);
1095         
1096         /* Object Data */
1097         switch (ob->type) {
1098                 case OB_CAMERA: /* ------- Camera ------------ */
1099                 {
1100                         Camera *ca= (Camera *)ob->data;
1101                         
1102                         if ((ads->filterflag & ADS_FILTER_NOCAM) == 0) {
1103                                 ANIMDATA_FILTER_CASES(ca,
1104                                         obdata_ok= 1;,
1105                                         obdata_ok= 1;,
1106                                         obdata_ok= 1;)
1107                         }
1108                 }
1109                         break;
1110                 case OB_LAMP: /* ---------- Lamp ----------- */
1111                 {
1112                         Lamp *la= (Lamp *)ob->data;
1113                         
1114                         if ((ads->filterflag & ADS_FILTER_NOLAM) == 0) {
1115                                 ANIMDATA_FILTER_CASES(la,
1116                                         obdata_ok= 1;,
1117                                         obdata_ok= 1;,
1118                                         obdata_ok= 1;)
1119                         }
1120                 }
1121                         break;
1122                 case OB_CURVE: /* ------- Curve ---------- */
1123                 {
1124                         Curve *cu= (Curve *)ob->data;
1125                         
1126                         if ((ads->filterflag & ADS_FILTER_NOCUR) == 0) {
1127                                 ANIMDATA_FILTER_CASES(cu,
1128                                         obdata_ok= 1;,
1129                                         obdata_ok= 1;,
1130                                         obdata_ok= 1;)
1131                         }
1132                 }
1133                         break;
1134         }
1135         if (obdata_ok) 
1136                 items += animdata_filter_dopesheet_obdata(anim_data, ads, base, filter_mode);
1137         
1138         /* return the number of items added to the list */
1139         return items;
1140 }       
1141
1142 static int animdata_filter_dopesheet_scene (ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
1143 {
1144         World *wo= sce->world;
1145         AnimData *adt= NULL;
1146         bAnimListElem *ale;
1147         int items = 0;
1148         
1149         /* add scene as a channel first (even if we aren't showing scenes we still need to show the scene's sub-data */
1150         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {
1151                 /* check if filtering by selection */
1152                 if (ANIMCHANNEL_SELOK( (sce->flag & SCE_DS_SELECTED) )) {
1153                         ale= make_new_animlistelem(sce, ANIMTYPE_SCENE, NULL, ANIMTYPE_NONE, NULL);
1154                         if (ale) {
1155                                 BLI_addtail(anim_data, ale);
1156                                 items++;
1157                         }
1158                 }
1159         }
1160         
1161         /* if collapsed, don't go any further (unless adding keyframes only) */
1162         if ( (EXPANDED_SCEC(sce) == 0) && !(filter_mode & ANIMFILTER_CURVESONLY) )
1163                 return items;
1164                 
1165         /* Action, Drivers, or NLA  for Scene */
1166         if ((ads->filterflag & ADS_FILTER_NOSCE) == 0) {
1167                 adt= sce->adt;
1168                 ANIMDATA_FILTER_CASES(sce,
1169                         { /* nla */
1170 #if 0
1171                                 /* include nla-expand widget? */
1172                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1173                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLNLA, base, ANIMTYPE_SCENE (ID *)sce);
1174                                         if (ale) {
1175                                                 BLI_addtail(anim_data, ale);
1176                                                 items++;
1177                                         }
1178                                 }
1179 #endif
1180                                 
1181                                 /* add NLA tracks */
1182                                 if (/*EXPANDED_NLTD(adt) ||*/ !(filter_mode & ANIMFILTER_CHANNELS))
1183                                         items += animdata_filter_nla(anim_data, adt->nla_tracks.first, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce);
1184                         },
1185                         { /* drivers */
1186                                 /* include drivers-expand widget? */
1187                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1188                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, sce, ANIMTYPE_SCENE, (ID *)sce);
1189                                         if (ale) {
1190                                                 BLI_addtail(anim_data, ale);
1191                                                 items++;
1192                                         }
1193                                 }
1194                                 
1195                                 /* add F-Curve channels (drivers are F-Curves) */
1196                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1197                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, sce, ANIMTYPE_SCENE, filter_mode, (ID *)sce);
1198                                 }
1199                         },
1200                         { /* action */
1201                                 /* include action-expand widget? */
1202                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1203                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, sce, ANIMTYPE_SCENE, (ID *)sce);
1204                                         if (ale) {
1205                                                 BLI_addtail(anim_data, ale);
1206                                                 items++;
1207                                         }
1208                                 }
1209                                 
1210                                 /* add F-Curve channels? */
1211                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1212                                         items += animdata_filter_action(anim_data, adt->action, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce); 
1213                                 }
1214                         }
1215                 )
1216         }
1217         
1218         /* world */
1219         if ((wo && wo->adt) && !(ads->filterflag & ADS_FILTER_NOWOR)) {
1220                 /* Action, Drivers, or NLA  for World */
1221                 adt= wo->adt;
1222                 ANIMDATA_FILTER_CASES(wo,
1223                         { /* nla */
1224 #if 0
1225                                 /* include nla-expand widget? */
1226                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1227                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLNLA, base, ANIMTYPE_DSWOR (ID *)wo);
1228                                         if (ale) {
1229                                                 BLI_addtail(anim_data, ale);
1230                                                 items++;
1231                                         }
1232                                 }
1233 #endif
1234                                 
1235                                 /* add NLA tracks */
1236                                 if (/*EXPANDED_NLTD(adt) ||*/ !(filter_mode & ANIMFILTER_CHANNELS))
1237                                         items += animdata_filter_nla(anim_data, adt->nla_tracks.first, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo);
1238                         },
1239                         { /* drivers */
1240                                 /* include world-expand widget? */
1241                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1242                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)wo);
1243                                         if (ale) {
1244                                                 BLI_addtail(anim_data, ale);
1245                                                 items++;
1246                                         }
1247                                 }
1248                                 
1249                                 /* add F-Curve channels (drivers are F-Curves) */
1250                                 if (FILTER_WOR_SCED(wo)/*EXPANDED_DRVD(adt)*/ || !(filter_mode & ANIMFILTER_CHANNELS)) {
1251                                         // XXX owner info is messed up now...
1252                                         items += animdata_filter_fcurves(anim_data, adt->drivers.first, NULL, wo, ANIMTYPE_DSWOR, filter_mode, (ID *)wo);
1253                                 }
1254                         },
1255                         { /* action */
1256                                 /* include world-expand widget? */
1257                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1258                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)sce);
1259                                         if (ale) {
1260                                                 BLI_addtail(anim_data, ale);
1261                                                 items++;
1262                                         }
1263                                 }
1264                                 
1265                                 /* add channels */
1266                                 if (FILTER_WOR_SCED(wo) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1267                                         items += animdata_filter_action(anim_data, adt->action, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo); 
1268                                 }
1269                         }
1270                 )
1271         }
1272         
1273         /* return the number of items added to the list */
1274         return items;
1275 }
1276
1277 // 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)
1278 static int animdata_filter_dopesheet (ListBase *anim_data, bDopeSheet *ads, int filter_mode)
1279 {
1280         Scene *sce= (Scene *)ads->source;
1281         Base *base;
1282         int items = 0;
1283         
1284         /* check that we do indeed have a scene */
1285         if ((ads->source == NULL) || (GS(ads->source->name)!=ID_SCE)) {
1286                 printf("DopeSheet Error: Not scene! \n");
1287                 return 0;
1288         }
1289         
1290         /* scene-linked animation */
1291         // TODO: sequencer, composite nodes - are we to include those here too?
1292         {
1293                 short sceOk, worOk;
1294                 
1295                 /* check filtering-flags if ok */
1296                 if (ads->filterflag) {
1297                         if (ads->filterflag & ADS_FILTER_ONLYDRIVERS) {
1298                                 sceOk= (ANIMDATA_HAS_DRIVERS(sce) && !(ads->filterflag & ADS_FILTER_NOSCE));
1299                                 worOk= ((sce->world) && ANIMDATA_HAS_DRIVERS(sce->world) && !(ads->filterflag & ADS_FILTER_NOWOR));
1300                         }
1301                         else {
1302                                 sceOk= (ANIMDATA_HAS_KEYS(sce) && !(ads->filterflag & ADS_FILTER_NOSCE));
1303                                 worOk= ((sce->world) && ANIMDATA_HAS_KEYS(sce->world) && !(ads->filterflag & ADS_FILTER_NOWOR));
1304                         }
1305                 }
1306                 else {
1307                         sceOk= (ANIMDATA_HAS_KEYS(sce));
1308                         worOk= ((sce->world) && ANIMDATA_HAS_KEYS(sce->world));
1309                 }
1310                 
1311                 /* check if not all bad (i.e. so there is something to show) */
1312                 if ( !(!sceOk && !worOk) ) {
1313                         /* add scene data to the list of filtered channels */
1314                         items += animdata_filter_dopesheet_scene(anim_data, ads, sce, filter_mode);
1315                 }
1316         }
1317         
1318         
1319         /* loop over all bases in the scene */
1320         for (base= sce->base.first; base; base= base->next) {
1321                 /* check if there's an object (all the relevant checks are done in the ob-function) */
1322                 if (base->object) {
1323                         Object *ob= base->object;
1324                         Key *key= ob_get_key(ob);
1325                         short actOk=1, keyOk=1, dataOk=1, matOk=1;
1326                         
1327                         /* firstly, check if object can be included, by the following fanimors:
1328                          *      - if only visible, must check for layer and also viewport visibility
1329                          *      - if only selected, must check if object is selected 
1330                          *      - there must be animation data to edit
1331                          */
1332                         // TODO: if cache is implemented, just check name here, and then 
1333                         if (filter_mode & ANIMFILTER_VISIBLE) {
1334                                 /* layer visibility - we check both object and base, since these may not be in sync yet */
1335                                 if ((sce->lay & (ob->lay|base->lay))==0) continue;
1336                                 
1337                                 /* outliner restrict-flag */
1338                                 if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
1339                         }
1340                         
1341                         /* additionally, dopesheet filtering also affects what objects to consider */
1342                         if (ads->filterflag) {
1343                                 /* check selection and object type filters */
1344                                 if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) || (base == sce->basact)) )  {
1345                                         /* only selected should be shown */
1346                                         continue;
1347                                 }
1348                                 
1349                                 /* check filters for datatypes */
1350                                 if (ads->filterflag & ADS_FILTER_ONLYDRIVERS) {
1351                                         actOk= (ANIMDATA_HAS_DRIVERS(ob));
1352                                         keyOk= ((key) && ANIMDATA_HAS_DRIVERS(key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS));
1353                                 }
1354                                 else {
1355                                         actOk= ANIMDATA_HAS_KEYS(ob);
1356                                         keyOk= ((key) && ANIMDATA_HAS_KEYS(key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS));
1357                                 }
1358                                 
1359                                 /* materials - only for geometric types */
1360                                 matOk= 0; /* by default, not ok... */
1361                                 if ( !(ads->filterflag & ADS_FILTER_NOMAT) && (ob->totcol) && 
1362                                          ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) ) 
1363                                 {
1364                                         int a;
1365                                         
1366                                         /* firstly check that we actuallly have some materials */
1367                                         for (a=0; a < ob->totcol; a++) {
1368                                                 Material *ma= give_current_material(ob, a);
1369                                                 
1370                                                 /* if material has relevant animation data, break */
1371                                                 if (ads->filterflag & ADS_FILTER_ONLYDRIVERS) {
1372                                                         if (ANIMDATA_HAS_DRIVERS(ma)) {
1373                                                                 matOk= 1;
1374                                                                 break;
1375                                                         }
1376                                                 }
1377                                                 else {
1378                                                         if (ANIMDATA_HAS_KEYS(ma)) {
1379                                                                 matOk= 1;
1380                                                                 break;
1381                                                         }
1382                                                 }
1383                                         }
1384                                 }
1385                                 
1386                                 /* data */
1387                                 switch (ob->type) {
1388                                         case OB_CAMERA: /* ------- Camera ------------ */
1389                                         {
1390                                                 Camera *ca= (Camera *)ob->data;
1391                                                 if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
1392                                                         dataOk= (ANIMDATA_HAS_DRIVERS(ca) && !(ads->filterflag & ADS_FILTER_NOCAM));
1393                                                 else
1394                                                         dataOk= (ANIMDATA_HAS_KEYS(ca) && !(ads->filterflag & ADS_FILTER_NOCAM));                                               
1395                                         }
1396                                                 break;
1397                                         case OB_LAMP: /* ---------- Lamp ----------- */
1398                                         {
1399                                                 Lamp *la= (Lamp *)ob->data;
1400                                                 if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
1401                                                         dataOk= (ANIMDATA_HAS_DRIVERS(la) && !(ads->filterflag & ADS_FILTER_NOLAM));
1402                                                 else
1403                                                         dataOk= (ANIMDATA_HAS_KEYS(la) && !(ads->filterflag & ADS_FILTER_NOLAM));       
1404                                         }
1405                                                 break;
1406                                         default: /* --- other --- */
1407                                                 dataOk= 0;
1408                                                 break;
1409                                 }
1410                                 
1411                                 /* check if all bad (i.e. nothing to show) */
1412                                 if (!actOk && !keyOk && !dataOk && !matOk)
1413                                         continue;
1414                         }
1415                         else {
1416                                 /* check data-types */
1417                                 actOk= ANIMDATA_HAS_KEYS(ob);
1418                                 keyOk= (key != NULL);
1419                                 
1420                                 /* materials - only for geometric types */
1421                                 matOk= 0; /* by default, not ok... */
1422                                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) && (ob->totcol)) 
1423                                 {
1424                                         int a;
1425                                         
1426                                         /* firstly check that we actuallly have some materials */
1427                                         for (a=0; a < ob->totcol; a++) {
1428                                                 Material *ma= give_current_material(ob, a);
1429                                                 
1430                                                 if ((ma) && ANIMDATA_HAS_KEYS(ma)) {
1431                                                         matOk= 1;
1432                                                         break;
1433                                                 }
1434                                         }
1435                                 }
1436                                 
1437                                 /* data */
1438                                 switch (ob->type) {
1439                                         case OB_CAMERA: /* ------- Camera ------------ */
1440                                         {
1441                                                 Camera *ca= (Camera *)ob->data;
1442                                                 dataOk= ANIMDATA_HAS_KEYS(ca);                                          
1443                                         }
1444                                                 break;
1445                                         case OB_LAMP: /* ---------- Lamp ----------- */
1446                                         {
1447                                                 Lamp *la= (Lamp *)ob->data;
1448                                                 dataOk= ANIMDATA_HAS_KEYS(la);  
1449                                         }
1450                                                 break;
1451                                         case OB_CURVE: /* -------- Curve ---------- */
1452                                         {
1453                                                 Curve *cu= (Curve *)ob->data;
1454                                                 dataOk= ANIMDATA_HAS_KEYS(cu);  
1455                                         }
1456                                                 break;
1457                                         default: /* --- other --- */
1458                                                 dataOk= 0;
1459                                                 break;
1460                                 }
1461                                 
1462                                 /* check if all bad (i.e. nothing to show) */
1463                                 if (!actOk && !keyOk && !dataOk && !matOk)
1464                                         continue;
1465                         }
1466                         
1467                         /* since we're still here, this object should be usable */
1468                         items += animdata_filter_dopesheet_ob(anim_data, ads, base, filter_mode);
1469                 }
1470         }
1471         
1472         /* return the number of items in the list */
1473         return items;
1474 }
1475
1476 /* ----------- Public API --------------- */
1477
1478 /* This function filters the active data source to leave only animation channels suitable for
1479  * usage by the caller. It will return the length of the list 
1480  * 
1481  *      *anim_data: is a pointer to a ListBase, to which the filtered animation channels
1482  *              will be placed for use.
1483  *      filter_mode: how should the data be filtered - bitmapping accessed flags
1484  */
1485 int ANIM_animdata_filter (bAnimContext *ac, ListBase *anim_data, int filter_mode, void *data, short datatype)
1486 {
1487         int items = 0;
1488         
1489         /* only filter data if there's somewhere to put it */
1490         if (data && anim_data) {
1491                 bAnimListElem *ale, *next;
1492                 Object *obact= (ac) ? ac->obact : NULL;
1493                 
1494                 /* firstly filter the data */
1495                 switch (datatype) {
1496                         case ANIMCONT_ACTION:
1497                                 items= animdata_filter_action(anim_data, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
1498                                 break;
1499                                 
1500                         case ANIMCONT_SHAPEKEY:
1501                                 items= animdata_filter_shapekey(anim_data, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
1502                                 break;
1503                                 
1504                         case ANIMCONT_GPENCIL:
1505                                 //items= animdata_filter_gpencil(anim_data, data, filter_mode);
1506                                 break;
1507                                 
1508                         case ANIMCONT_DOPESHEET:
1509                         case ANIMCONT_FCURVES:
1510                         case ANIMCONT_DRIVERS:
1511                                 items= animdata_filter_dopesheet(anim_data, data, filter_mode);
1512                                 break;
1513                 }
1514                         
1515                 /* remove any weedy entries */
1516                 // XXX this is weedy code!
1517                 for (ale= anim_data->first; ale; ale= next) {
1518                         next= ale->next;
1519                         
1520                         if (ale->type == ANIMTYPE_NONE) {
1521                                 items--;
1522                                 BLI_freelinkN(anim_data, ale);
1523                         }
1524                 }
1525         }
1526         
1527         /* return the number of items in the list */
1528         return items;
1529 }
1530
1531 /* ************************************************************ */