commit before doing some hefty shapekey change, will break compilation
[blender-staging.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, Joshua Leung
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Joshua Leung (original author)
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 Graph Editor also uses this for its channel list and for determining which curves
37  * are being edited. Likewise, the NLA Editor also uses this for its channel list and in
38  * its operators.
39  *
40  * Note: much of the original system this was based on was built before the creation of the RNA
41  * system. In future, it would be interesting to replace some parts of this code with RNA queries,
42  * however, RNA does not eliminate some of the boiler-plate reduction benefits presented by this 
43  * system, so if any such work does occur, it should only be used for the internals used here...
44  *
45  * -- Joshua Leung, Dec 2008 (Last revision July 2009)
46  */
47
48 #include <string.h>
49 #include <stdio.h>
50
51 #include "DNA_listBase.h"
52 #include "DNA_ID.h"
53 #include "DNA_anim_types.h"
54 #include "DNA_action_types.h"
55 #include "DNA_armature_types.h"
56 #include "DNA_constraint_types.h"
57 #include "DNA_camera_types.h"
58 #include "DNA_curve_types.h"
59 #include "DNA_gpencil_types.h"
60 #include "DNA_ipo_types.h"
61 #include "DNA_lamp_types.h"
62 #include "DNA_lattice_types.h"
63 #include "DNA_key_types.h"
64 #include "DNA_material_types.h"
65 #include "DNA_mesh_types.h"
66 #include "DNA_meta_types.h"
67 #include "DNA_object_types.h"
68 #include "DNA_particle_types.h"
69 #include "DNA_space_types.h"
70 #include "DNA_scene_types.h"
71 #include "DNA_screen_types.h"
72 #include "DNA_windowmanager_types.h"
73 #include "DNA_world_types.h"
74
75 #include "MEM_guardedalloc.h"
76
77 #include "BLI_blenlib.h"
78
79 #include "BKE_animsys.h"
80 #include "BKE_action.h"
81 #include "BKE_fcurve.h"
82 #include "BKE_context.h"
83 #include "BKE_global.h"
84 #include "BKE_key.h"
85 #include "BKE_object.h"
86 #include "BKE_material.h"
87 #include "BKE_screen.h"
88 #include "BKE_utildefines.h"
89
90 #include "ED_anim_api.h"
91 #include "ED_types.h"
92 #include "ED_util.h"
93
94 #include "WM_api.h"
95 #include "WM_types.h"
96
97 #include "BIF_gl.h"
98 #include "BIF_glutil.h"
99
100 #include "UI_interface.h"
101 #include "UI_resources.h"
102 #include "UI_view2d.h"
103
104 /* ************************************************************ */
105 /* Blender Context <-> Animation Context mapping */
106
107 /* ----------- Private Stuff - Action Editor ------------- */
108
109 /* Get shapekey data being edited (for Action Editor -> ShapeKey mode) */
110 /* Note: there's a similar function in key.c (ob_get_key) */
111 Key *actedit_get_shapekeys (bAnimContext *ac, SpaceAction *saction) 
112 {
113     Scene *scene= ac->scene;
114     Object *ob;
115     Key *key;
116         
117     ob = OBACT;
118     if (ob == NULL) 
119                 return NULL;
120         
121         /* XXX pinning is not available in 'ShapeKey' mode... */
122         //if (saction->pin) return NULL;
123         
124         /* shapekey data is stored with geometry data */
125         switch (ob->type) {
126                 case OB_MESH:
127                         key= ((Mesh *)ob->data)->key;
128                         break;
129                         
130                 case OB_LATTICE:
131                         key= ((Lattice *)ob->data)->key;
132                         break;
133                         
134                 case OB_CURVE:
135                 case OB_SURF:
136                         key= ((Curve *)ob->data)->key;
137                         break;
138                         
139                 default:
140                         return NULL;
141         }
142         
143         if (key) {
144                 if (key->type == KEY_RELATIVE)
145                         return key;
146         }
147         
148     return NULL;
149 }
150
151 /* Get data being edited in Action Editor (depending on current 'mode') */
152 static short actedit_get_context (bAnimContext *ac, SpaceAction *saction)
153 {
154         /* sync settings with current view status, then return appropriate data */
155         switch (saction->mode) {
156                 case SACTCONT_ACTION: /* 'Action Editor' */
157                         /* if not pinned, sync with active object */
158                         if (saction->pin == 0) {
159                                 if (ac->obact && ac->obact->adt)
160                                         saction->action = ac->obact->adt->action;
161                                 else
162                                         saction->action= NULL;
163                         }
164                         
165                         ac->datatype= ANIMCONT_ACTION;
166                         ac->data= saction->action;
167                         
168                         ac->mode= saction->mode;
169                         return 1;
170                         
171                 case SACTCONT_SHAPEKEY: /* 'ShapeKey Editor' */
172                         ac->datatype= ANIMCONT_SHAPEKEY;
173                         ac->data= actedit_get_shapekeys(ac, saction);
174                         
175                         ac->mode= saction->mode;
176                         return 1;
177                         
178                 case SACTCONT_GPENCIL: /* Grease Pencil */ // XXX review how this mode is handled...
179                         ac->datatype=ANIMCONT_GPENCIL;
180                         //ac->data= CTX_wm_screen(C); // FIXME: add that dopesheet type thing here!
181                         ac->data= NULL; // !!!
182                         
183                         ac->mode= saction->mode;
184                         return 1;
185                         
186                 case SACTCONT_DOPESHEET: /* DopeSheet */
187                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
188                         saction->ads.source= (ID *)ac->scene;
189                         
190                         ac->datatype= ANIMCONT_DOPESHEET;
191                         ac->data= &saction->ads;
192                         
193                         ac->mode= saction->mode;
194                         return 1;
195                 
196                 default: /* unhandled yet */
197                         ac->datatype= ANIMCONT_NONE;
198                         ac->data= NULL;
199                         
200                         ac->mode= -1;
201                         return 0;
202         }
203 }
204
205 /* ----------- Private Stuff - Graph Editor ------------- */
206
207 /* Get data being edited in Graph Editor (depending on current 'mode') */
208 static short graphedit_get_context (bAnimContext *ac, SpaceIpo *sipo)
209 {
210         /* init dopesheet data if non-existant (i.e. for old files) */
211         if (sipo->ads == NULL) {
212                 sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
213                 sipo->ads->source= (ID *)ac->scene;
214         }
215         
216         /* set settings for Graph Editor - "Selected = Editable" */
217         if (sipo->flag & SIPO_SELCUVERTSONLY)
218                 sipo->ads->filterflag |= ADS_FILTER_SELEDIT;
219         else
220                 sipo->ads->filterflag &= ~ADS_FILTER_SELEDIT;
221         
222         /* sync settings with current view status, then return appropriate data */
223         switch (sipo->mode) {
224                 case SIPO_MODE_ANIMATION:       /* Animation F-Curve Editor */
225                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
226                         sipo->ads->source= (ID *)ac->scene;
227                         sipo->ads->filterflag &= ~ADS_FILTER_ONLYDRIVERS;
228                         
229                         ac->datatype= ANIMCONT_FCURVES;
230                         ac->data= sipo->ads;
231                         
232                         ac->mode= sipo->mode;
233                         return 1;
234                 
235                 case SIPO_MODE_DRIVERS:         /* Driver F-Curve Editor */
236                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
237                         sipo->ads->source= (ID *)ac->scene;
238                         sipo->ads->filterflag |= ADS_FILTER_ONLYDRIVERS;
239                         
240                         ac->datatype= ANIMCONT_DRIVERS;
241                         ac->data= sipo->ads;
242                         
243                         ac->mode= sipo->mode;
244                         return 1;
245                 
246                 default: /* unhandled yet */
247                         ac->datatype= ANIMCONT_NONE;
248                         ac->data= NULL;
249                         
250                         ac->mode= -1;
251                         return 0;
252         }
253 }
254
255 /* ----------- Private Stuff - NLA Editor ------------- */
256
257 /* Get data being edited in Graph Editor (depending on current 'mode') */
258 static short nlaedit_get_context (bAnimContext *ac, SpaceNla *snla)
259 {
260         /* init dopesheet data if non-existant (i.e. for old files) */
261         if (snla->ads == NULL)
262                 snla->ads= MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
263         
264         /* sync settings with current view status, then return appropriate data */
265         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
266         snla->ads->source= (ID *)ac->scene;
267         snla->ads->filterflag |= ADS_FILTER_ONLYNLA;
268         
269         ac->datatype= ANIMCONT_NLA;
270         ac->data= snla->ads;
271         
272         return 1;
273 }
274
275 /* ----------- Public API --------------- */
276
277 /* Obtain current anim-data context, given that context info from Blender context has already been set 
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  */
281 short ANIM_animdata_context_getdata (bAnimContext *ac)
282 {
283         ScrArea *sa= ac->sa;
284         short ok= 0;
285         
286         /* context depends on editor we are currently in */
287         if (sa) {
288                 switch (sa->spacetype) {
289                         case SPACE_ACTION:
290                         {
291                                 SpaceAction *saction= (SpaceAction *)sa->spacedata.first;
292                                 ok= actedit_get_context(ac, saction);
293                         }
294                                 break;
295                                 
296                         case SPACE_IPO:
297                         {
298                                 SpaceIpo *sipo= (SpaceIpo *)sa->spacedata.first;
299                                 ok= graphedit_get_context(ac, sipo);
300                         }
301                                 break;
302                                 
303                         case SPACE_NLA:
304                         {
305                                 SpaceNla *snla= (SpaceNla *)sa->spacedata.first;
306                                 ok= nlaedit_get_context(ac, snla);
307                         }
308                                 break;
309                 }
310         }
311         
312         /* check if there's any valid data */
313         if (ok && ac->data)
314                 return 1;
315         else
316                 return 0;
317 }
318
319 /* Obtain current anim-data context from Blender Context info 
320  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
321  *        allocation/freeing costs (which are not that avoidable with channels).
322  *      - Clears data and sets the information from Blender Context which is useful
323  */
324 short ANIM_animdata_get_context (const bContext *C, bAnimContext *ac)
325 {
326         ScrArea *sa= CTX_wm_area(C);
327         ARegion *ar= CTX_wm_region(C);
328         Scene *scene= CTX_data_scene(C);
329         
330         /* clear old context info */
331         if (ac == NULL) return 0;
332         memset(ac, 0, sizeof(bAnimContext));
333         
334         /* get useful default context settings from context */
335         ac->scene= scene;
336         if (scene) {
337                 ac->markers= &scene->markers;           
338                 ac->obact= (scene->basact)?  scene->basact->object : NULL;
339         }
340         ac->sa= sa;
341         ac->ar= ar;
342         ac->spacetype= (sa) ? sa->spacetype : 0;
343         ac->regiontype= (ar) ? ar->regiontype : 0;
344         
345         /* get data context info */
346         return ANIM_animdata_context_getdata(ac);
347 }
348
349 /* ************************************************************ */
350 /* Blender Data <-- Filter --> Channels to be operated on */
351
352 /* quick macro to test if AnimData is usable */
353 #define ANIMDATA_HAS_KEYS(id) ((id)->adt && (id)->adt->action)
354
355 /* quick macro to test if AnimData is usable for drivers */
356 #define ANIMDATA_HAS_DRIVERS(id) ((id)->adt && (id)->adt->drivers.first)
357
358 /* quick macro to test if AnimData is usable for NLA */
359 #define ANIMDATA_HAS_NLA(id) ((id)->adt && (id)->adt->nla_tracks.first)
360
361
362 /* Quick macro to test for all three avove usability tests, performing the appropriate provided 
363  * action for each when the AnimData context is appropriate. 
364  *
365  * Priority order for this goes (most important, to least): AnimData blocks, NLA, Drivers, Keyframes.
366  *
367  * For this to work correctly, a standard set of data needs to be available within the scope that this
368  * gets called in: 
369  *      - ListBase anim_data;
370  *      - bDopeSheet *ads;
371  *      - bAnimListElem *ale;
372  *      - int items;
373  *
374  *      - id: ID block which should have an AnimData pointer following it immediately, to use
375  *      - adtOk: line or block of code to execute for AnimData-blocks case (usually ANIMDATA_ADD_ANIMDATA)
376  *      - nlaOk: line or block of code to execute for NLA tracks+strips case
377  *      - driversOk: line or block of code to execute for Drivers case
378  *      - keysOk: line or block of code for Keyframes case
379  *
380  * The checks for the various cases are as follows:
381  *      0) top level: checks for animdata and also that all the F-Curves for the block will be visible
382  *      1) animdata check: for filtering animdata blocks only
383  *      2A) nla tracks: include animdata block's data as there are NLA tracks+strips there
384  *      2B) actions to convert to nla: include animdata block's data as there is an action that can be 
385  *              converted to a new NLA strip, and the filtering options allow this
386  *      3) drivers: include drivers from animdata block (for Drivers mode in Graph Editor)
387  *      4) normal keyframes: only when there is an active action
388  */
389 #define ANIMDATA_FILTER_CASES(id, adtOk, nlaOk, driversOk, keysOk) \
390         {\
391                 if ((id)->adt) {\
392                         if (!(filter_mode & ANIMFILTER_CURVEVISIBLE) || !((id)->adt->flag & ADT_CURVES_NOT_VISIBLE)) {\
393                                 if (filter_mode & ANIMFILTER_ANIMDATA) {\
394                                         adtOk\
395                                 }\
396                                 else if (ads->filterflag & ADS_FILTER_ONLYNLA) {\
397                                         if (ANIMDATA_HAS_NLA(id)) {\
398                                                 nlaOk\
399                                         }\
400                                         else if (!(ads->filterflag & ADS_FILTER_NLA_NOACT) && ANIMDATA_HAS_KEYS(id)) {\
401                                                 nlaOk\
402                                         }\
403                                 }\
404                                 else if (ads->filterflag & ADS_FILTER_ONLYDRIVERS) {\
405                                         if (ANIMDATA_HAS_DRIVERS(id)) {\
406                                                 driversOk\
407                                         }\
408                                 }\
409                                 else {\
410                                         if (ANIMDATA_HAS_KEYS(id)) {\
411                                                 keysOk\
412                                         }\
413                                 }\
414                         }\
415                 }\
416         }
417
418
419 /* quick macro to add a pointer to an AnimData block as a channel */
420 #define ANIMDATA_ADD_ANIMDATA(id) \
421         {\
422                 ale= make_new_animlistelem((id)->adt, ANIMTYPE_ANIMDATA, NULL, ANIMTYPE_NONE, (ID *)id);\
423                 if (ale) {\
424                         BLI_addtail(anim_data, ale);\
425                         items++;\
426                 }\
427         }
428         
429 /* quick macro to test if an anim-channel representing an AnimData block is suitably active */
430 #define ANIMCHANNEL_ACTIVEOK(ale) \
431         ( !(filter_mode & ANIMFILTER_ACTIVE) || !(ale->adt) || (ale->adt->flag & ADT_UI_ACTIVE) )
432
433 /* quick macro to test if an anim-channel (F-Curve, Group, etc.) is selected in an acceptable way */
434 #define ANIMCHANNEL_SELOK(test_func) \
435                 ( !(filter_mode & (ANIMFILTER_SEL|ANIMFILTER_UNSEL)) || \
436                   ((filter_mode & ANIMFILTER_SEL) && test_func) || \
437                   ((filter_mode & ANIMFILTER_UNSEL) && test_func==0) ) 
438                   
439 /* quick macro to test if an anim-channel (F-Curve) is selected ok for editing purposes 
440  *      - _SELEDIT means that only selected curves will have visible+editable keyframes
441  */
442 // FIXME: this doesn't work cleanly yet...
443 #define ANIMCHANNEL_SELEDITOK(test_func) \
444                 ( !(filter_mode & ANIMFILTER_SELEDIT) || \
445                   (filter_mode & ANIMFILTER_CHANNELS) || \
446                   (test_func) )
447
448 /* ----------- 'Private' Stuff --------------- */
449
450 /* this function allocates memory for a new bAnimListElem struct for the 
451  * provided animation channel-data. 
452  */
453 bAnimListElem *make_new_animlistelem (void *data, short datatype, void *owner, short ownertype, ID *owner_id)
454 {
455         bAnimListElem *ale= NULL;
456         
457         /* only allocate memory if there is data to convert */
458         if (data) {
459                 /* allocate and set generic data */
460                 ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
461                 
462                 ale->data= data;
463                 ale->type= datatype;
464                         // XXX what is the point of the owner data?
465                 ale->owner= owner;
466                 ale->ownertype= ownertype;
467                 
468                 ale->id= owner_id;
469                 ale->adt= BKE_animdata_from_id(owner_id);
470                 
471                 /* do specifics */
472                 switch (datatype) {
473                         case ANIMTYPE_SUMMARY:
474                         {
475                                 /* nothing to include for now... this is just a dummy wrappy around all the other channels 
476                                  * in the DopeSheet, and gets included at the start of the list
477                                  */
478                                 ale->key_data= NULL;
479                                 ale->datatype= ALE_ALL;
480                         }
481                                 break;
482                         
483                         case ANIMTYPE_SCENE:
484                         {
485                                 Scene *sce= (Scene *)data;
486                                 
487                                 ale->flag= sce->flag;
488                                 
489                                 ale->key_data= sce;
490                                 ale->datatype= ALE_SCE;
491                                 
492                                 ale->adt= BKE_animdata_from_id(data);
493                         }
494                                 break;
495                         case ANIMTYPE_OBJECT:
496                         {
497                                 Base *base= (Base *)data;
498                                 Object *ob= base->object;
499                                 
500                                 ale->flag= ob->flag;
501                                 
502                                 ale->key_data= ob;
503                                 ale->datatype= ALE_OB;
504                                 
505                                 ale->adt= BKE_animdata_from_id(&ob->id);
506                         }
507                                 break;
508                         case ANIMTYPE_FILLACTD:
509                         {
510                                 bAction *act= (bAction *)data;
511                                 
512                                 ale->flag= act->flag;
513                                 
514                                 ale->key_data= act;
515                                 ale->datatype= ALE_ACT;
516                         }
517                                 break;
518                         case ANIMTYPE_FILLDRIVERS:
519                         {
520                                 AnimData *adt= (AnimData *)data;
521                                 
522                                 ale->flag= adt->flag;
523                                 
524                                         // XXX... drivers don't show summary for now
525                                 ale->key_data= NULL;
526                                 ale->datatype= ALE_NONE;
527                         }
528                                 break;
529                         case ANIMTYPE_FILLMATD:
530                         {
531                                 Object *ob= (Object *)data;
532                                 
533                                 ale->flag= FILTER_MAT_OBJC(ob);
534                                 
535                                 ale->key_data= NULL;
536                                 ale->datatype= ALE_NONE;
537                         }
538                                 break;
539                         case ANIMTYPE_FILLPARTD:
540                         {
541                                 Object *ob= (Object *)data;
542                                 
543                                 ale->flag= FILTER_PART_OBJC(ob);
544                                 
545                                 ale->key_data= NULL;
546                                 ale->datatype= ALE_NONE;
547                         }
548                                 break;
549                         
550                         case ANIMTYPE_DSMAT:
551                         {
552                                 Material *ma= (Material *)data;
553                                 AnimData *adt= ma->adt;
554                                 
555                                 ale->flag= FILTER_MAT_OBJD(ma);
556                                 
557                                 ale->key_data= (adt) ? adt->action : NULL;
558                                 ale->datatype= ALE_ACT;
559                                 
560                                 ale->adt= BKE_animdata_from_id(data);
561                         }
562                                 break;
563                         case ANIMTYPE_DSLAM:
564                         {
565                                 Lamp *la= (Lamp *)data;
566                                 AnimData *adt= la->adt;
567                                 
568                                 ale->flag= FILTER_LAM_OBJD(la);
569                                 
570                                 ale->key_data= (adt) ? adt->action : NULL;
571                                 ale->datatype= ALE_ACT;
572                                 
573                                 ale->adt= BKE_animdata_from_id(data);
574                         }
575                                 break;
576                         case ANIMTYPE_DSCAM:
577                         {
578                                 Camera *ca= (Camera *)data;
579                                 AnimData *adt= ca->adt;
580                                 
581                                 ale->flag= FILTER_CAM_OBJD(ca);
582                                 
583                                 ale->key_data= (adt) ? adt->action : NULL;
584                                 ale->datatype= ALE_ACT;
585                                 
586                                 ale->adt= BKE_animdata_from_id(data);
587                         }
588                                 break;
589                         case ANIMTYPE_DSCUR:
590                         {
591                                 Curve *cu= (Curve *)data;
592                                 AnimData *adt= cu->adt;
593                                 
594                                 ale->flag= FILTER_CUR_OBJD(cu);
595                                 
596                                 ale->key_data= (adt) ? adt->action : NULL;
597                                 ale->datatype= ALE_ACT;
598                                 
599                                 ale->adt= BKE_animdata_from_id(data);
600                         }
601                                 break;
602                         case ANIMTYPE_DSSKEY:
603                         {
604                                 Key *key= (Key *)data;
605                                 AnimData *adt= key->adt;
606                                 
607                                 ale->flag= FILTER_SKE_OBJD(key); 
608                                 
609                                 ale->key_data= (adt) ? adt->action : NULL;
610                                 ale->datatype= ALE_ACT;
611                                 
612                                 ale->adt= BKE_animdata_from_id(data);
613                         }
614                                 break;
615                         case ANIMTYPE_DSWOR:
616                         {
617                                 World *wo= (World *)data;
618                                 AnimData *adt= wo->adt;
619                                 
620                                 ale->flag= FILTER_WOR_SCED(wo); 
621                                 
622                                 ale->key_data= (adt) ? adt->action : NULL;
623                                 ale->datatype= ALE_ACT;
624                                 
625                                 ale->adt= BKE_animdata_from_id(data);
626                         }
627                                 break;
628                         case ANIMTYPE_DSPART:
629                         {
630                                 ParticleSettings *part= (ParticleSettings*)ale->data;
631                                 AnimData *adt= part->adt;
632                                 
633                                 ale->flag= FILTER_PART_OBJD(part); 
634                                 
635                                 ale->key_data= (adt) ? adt->action : NULL;
636                                 ale->datatype= ALE_ACT;
637                                 
638                                 ale->adt= BKE_animdata_from_id(data);
639                         }
640                                 break;
641                                 
642                         case ANIMTYPE_GROUP:
643                         {
644                                 bActionGroup *agrp= (bActionGroup *)data;
645                                 
646                                 ale->flag= agrp->flag;
647                                 
648                                 ale->key_data= NULL;
649                                 ale->datatype= ALE_GROUP;
650                         }
651                                 break;
652                         case ANIMTYPE_FCURVE:
653                         {
654                                 FCurve *fcu= (FCurve *)data;
655                                 
656                                 ale->flag= fcu->flag;
657                                 
658                                 ale->key_data= fcu;
659                                 ale->datatype= ALE_FCURVE;
660                         }
661                                 break;
662                                 
663                         case ANIMTYPE_SHAPEKEY:
664                         {
665                                 KeyBlock *kb= (KeyBlock *)data;
666                                 Key *key= (Key *)ale->id;
667                                 
668                                 ale->flag= kb->flag;
669                                 
670                                 /* whether we have keyframes depends on whether there is a Key block to find it from */
671                                 if (key) {
672                                         /* index of shapekey is defined by place in key's list */
673                                         ale->index= BLI_findindex(&key->block, kb);
674                                         
675                                         /* the corresponding keyframes are from the animdata */
676                                         if (ale->adt && ale->adt->action) {
677                                                 bAction *act= ale->adt->action;
678                                                 char *rna_path = key_get_curValue_rnaPath(key, kb);
679                                                 
680                                                 /* try to find the F-Curve which corresponds to this exactly,
681                                                  * then free the MEM_alloc'd string
682                                                  */
683                                                 if (rna_path) {
684                                                         ale->key_data= (void *)list_find_fcurve(&act->curves, rna_path, 0);
685                                                         MEM_freeN(rna_path);
686                                                 }
687                                         }
688                                         ale->datatype= (ale->key_data)? ALE_FCURVE : ALE_NONE;
689                                 }
690                         }       
691                                 break;
692                         
693                         case ANIMTYPE_GPLAYER:
694                         {
695                                 bGPDlayer *gpl= (bGPDlayer *)data;
696                                 
697                                 ale->flag= gpl->flag;
698                                 
699                                 ale->key_data= NULL;
700                                 ale->datatype= ALE_GPFRAME;
701                         }
702                                 break;
703                                 
704                         case ANIMTYPE_NLATRACK:
705                         {
706                                 NlaTrack *nlt= (NlaTrack *)data;
707                                 
708                                 ale->flag= nlt->flag;
709                                 
710                                         // XXX or should this be done some other way?
711                                 ale->key_data= &nlt->strips;
712                                 ale->datatype= ALE_NLASTRIP;
713                         }
714                                 break;
715                         case ANIMTYPE_NLAACTION:
716                         {
717                                 /* nothing to include for now... nothing editable from NLA-perspective here */
718                                 ale->key_data= NULL;
719                                 ale->datatype= ALE_NONE;
720                         }
721                                 break;
722                 }
723         }
724         
725         /* return created datatype */
726         return ale;
727 }
728  
729 /* ----------------------------------------- */
730
731
732 static int animdata_filter_fcurves (ListBase *anim_data, bDopeSheet *ads, FCurve *first, bActionGroup *grp, void *owner, short ownertype, int filter_mode, ID *owner_id)
733 {
734         bAnimListElem *ale = NULL;
735         FCurve *fcu;
736         int items = 0;
737         
738         /* loop over F-Curves - assume that the caller of this has already checked that these should be included 
739          * NOTE: we need to check if the F-Curves belong to the same group, as this gets called for groups too...
740          */
741         for (fcu= first; ((fcu) && (fcu->grp==grp)); fcu= fcu->next) {
742                 /* special exception for Pose-Channel Based F-Curves:
743                  *      - the 'Only Selected' data filter should be applied to Pose-Channel data too, but those are
744                  *        represented as F-Curves. The way the filter for objects worked was to be the first check
745                  *        after 'normal' visibility, so this is done first here too...
746                  *      - we currently use an 'approximate' method for getting these F-Curves that doesn't require
747                  *        carefully checking the entire path
748                  *      - this will also affect things like Drivers, and also works for Bone Constraints
749                  */
750                 if ( ((ads) && (ads->filterflag & ADS_FILTER_ONLYSEL)) && 
751                          ((owner_id) && (GS(owner_id->name) == ID_OB)) ) 
752                 {
753                         Object *ob= (Object *)owner_id;
754                         
755                         /* only consider if F-Curve involves pose_channels */
756                         if ((fcu->rna_path) && strstr(fcu->rna_path, "pose_channels")) {
757                                 bPoseChannel *pchan;
758                                 char *bone_name;
759                                 
760                                 /* get bone-name, and check if this bone is selected */
761                                 bone_name= BLI_getQuotedStr(fcu->rna_path, "pose_channels[");
762                                 pchan= get_pose_channel(ob->pose, bone_name);
763                                 if (bone_name) MEM_freeN(bone_name);
764                                 
765                                 /* can only add this F-Curve if it is selected */
766                                 if ((pchan) && (pchan->bone) && (pchan->bone->flag & BONE_SELECTED)==0)
767                                         continue;
768                         }
769                 }
770                 
771                 /* only include if visible (Graph Editor check, not channels check) */
772                 if (!(filter_mode & ANIMFILTER_CURVEVISIBLE) || (fcu->flag & FCURVE_VISIBLE)) {
773                         /* only work with this channel and its subchannels if it is editable */
774                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_FCU(fcu)) {
775                                 /* only include this curve if selected in a way consistent with the filtering requirements */
776                                 if ( ANIMCHANNEL_SELOK(SEL_FCU(fcu)) && ANIMCHANNEL_SELEDITOK(SEL_FCU(fcu)) ) {
777                                         /* only include if this curve is active */
778                                         if (!(filter_mode & ANIMFILTER_ACTIVE) || (fcu->flag & FCURVE_ACTIVE)) {
779                                                 ale= make_new_animlistelem(fcu, ANIMTYPE_FCURVE, owner, ownertype, owner_id);
780                                                 
781                                                 if (ale) {
782                                                         BLI_addtail(anim_data, ale);
783                                                         items++;
784                                                 }
785                                         }
786                                 }
787                         }
788                 }
789         }
790         
791         /* return the number of items added to the list */
792         return items;
793 }
794
795 static int animdata_filter_action (ListBase *anim_data, bDopeSheet *ads, bAction *act, int filter_mode, void *owner, short ownertype, ID *owner_id)
796 {
797         bAnimListElem *ale=NULL;
798         bActionGroup *agrp;
799         FCurve *lastchan=NULL;
800         int items = 0;
801         
802         /* loop over groups */
803         // TODO: in future, should we expect to need nested groups?
804         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
805                 /* add this group as a channel first */
806                 if ((filter_mode & ANIMFILTER_CHANNELS) || !(filter_mode & ANIMFILTER_CURVESONLY)) {
807                         /* check if filtering by selection */
808                         if ( ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) ) {
809                                 ale= make_new_animlistelem(agrp, ANIMTYPE_GROUP, NULL, ANIMTYPE_NONE, owner_id);
810                                 if (ale) {
811                                         BLI_addtail(anim_data, ale);
812                                         items++;
813                                 }
814                         }
815                 }
816                 
817                 /* store reference to last channel of group */
818                 if (agrp->channels.last) 
819                         lastchan= agrp->channels.last;
820                 
821                 
822                 /* there are some situations, where only the channels of the action group should get considered */
823                 if (!(filter_mode & ANIMFILTER_ACTGROUPED) || (agrp->flag & AGRP_ACTIVE)) {
824                         /* filters here are a bit convoulted...
825                          *      - groups show a "summary" of keyframes beside their name which must accessable for tools which handle keyframes
826                          *      - groups can be collapsed (and those tools which are only interested in channels rely on knowing that group is closed)
827                          *
828                          * cases when we should include F-Curves inside group:
829                          *      - we don't care about visibility
830                          *      - group is expanded
831                          *      - we just need the F-Curves present
832                          */
833                         if ( (!(filter_mode & ANIMFILTER_VISIBLE) || EXPANDED_AGRP(agrp)) || (filter_mode & ANIMFILTER_CURVESONLY) ) 
834                         {
835                                 /* for the Graph Editor, curves may be set to not be visible in the view to lessen clutter,
836                                  * but to do this, we need to check that the group doesn't have it's not-visible flag set preventing 
837                                  * all its sub-curves to be shown
838                                  */
839                                 if ( !(filter_mode & ANIMFILTER_CURVEVISIBLE) || !(agrp->flag & AGRP_NOTVISIBLE) )
840                                 {
841                                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_AGRP(agrp)) {
842                                                 items += animdata_filter_fcurves(anim_data, ads, agrp->channels.first, agrp, owner, ownertype, filter_mode, owner_id);
843                                         }
844                                 }
845                         }
846                 }
847                 
848                 // TODO: but we still need to deal with the case when the group may be closed, but it has no visible curves too
849         }
850         
851         /* loop over un-grouped F-Curves (only if we're not only considering those channels in the animive group) */
852         if (!(filter_mode & ANIMFILTER_ACTGROUPED))  {
853                 // XXX the 'owner' info here needs review...
854                 items += animdata_filter_fcurves(anim_data, ads, (lastchan)?(lastchan->next):(act->curves.first), NULL, owner, ownertype, filter_mode, owner_id);
855         }
856         
857         /* return the number of items added to the list */
858         return items;
859 }
860
861 /* Include NLA-Data for NLA-Editor:
862  *      - when ANIMFILTER_CHANNELS is used, that means we should be filtering the list for display
863  *        Although the evaluation order is from the first track to the last and then apply the Action on top,
864  *        we present this in the UI as the Active Action followed by the last track to the first so that we 
865  *        get the evaluation order presented as per a stack.
866  *      - for normal filtering (i.e. for editing), we only need the NLA-tracks but they can be in 'normal' evaluation
867  *        order, i.e. first to last. Otherwise, some tools may get screwed up.
868  */
869 static int animdata_filter_nla (ListBase *anim_data, bDopeSheet *ads, AnimData *adt, int filter_mode, void *owner, short ownertype, ID *owner_id)
870 {
871         bAnimListElem *ale;
872         NlaTrack *nlt;
873         NlaTrack *first=NULL, *next=NULL;
874         int items = 0;
875         
876         /* if showing channels, include active action */
877         if (filter_mode & ANIMFILTER_CHANNELS) {
878                 /* there isn't really anything editable here, so skip if need editable */
879                 // TODO: currently, selection isn't checked since it doesn't matter
880                 if ((filter_mode & ANIMFILTER_FOREDIT) == 0) { 
881                         /* just add the action track now (this MUST appear for drawing)
882                          *      - as AnimData may not have an action, we pass a dummy pointer just to get the list elem created, then
883                          *        overwrite this with the real value - REVIEW THIS...
884                          */
885                         ale= make_new_animlistelem((void *)(&adt->action), ANIMTYPE_NLAACTION, owner, ownertype, owner_id);
886                         ale->data= (adt->action) ? adt->action : NULL;
887                                 
888                         if (ale) {
889                                 BLI_addtail(anim_data, ale);
890                                 items++;
891                         }
892                 }
893                 
894                 /* first track to include will be the last one if we're filtering by channels */
895                 first= adt->nla_tracks.last;
896         }
897         else {
898                 /* first track to include will the the first one (as per normal) */
899                 first= adt->nla_tracks.first;
900         }
901         
902         /* loop over NLA Tracks - assume that the caller of this has already checked that these should be included */
903         for (nlt= first; nlt; nlt= next) {
904                 /* 'next' NLA-Track to use depends on whether we're filtering for drawing or not */
905                 if (filter_mode & ANIMFILTER_CHANNELS) 
906                         next= nlt->prev;
907                 else
908                         next= nlt->next;
909                 
910                 /* if we're in NLA-tweakmode, don't show this track if it was disabled (due to tweaking) for now 
911                  *      - active track should still get shown though (even though it has disabled flag set)
912                  */
913                 // FIXME: the channels after should still get drawn, just 'differently', and after an active-action channel
914                 if ((adt->flag & ADT_NLA_EDIT_ON) && (nlt->flag & NLATRACK_DISABLED) && !(nlt->flag & NLATRACK_ACTIVE))
915                         continue;
916                 
917                 /* only work with this channel and its subchannels if it is editable */
918                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_NLT(nlt)) {
919                         /* only include this track if selected in a way consistent with the filtering requirements */
920                         if ( ANIMCHANNEL_SELOK(SEL_NLT(nlt)) ) {
921                                 /* only include if this track is active */
922                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (nlt->flag & NLATRACK_ACTIVE)) {
923                                         ale= make_new_animlistelem(nlt, ANIMTYPE_NLATRACK, owner, ownertype, owner_id);
924                                                 
925                                         if (ale) {
926                                                 BLI_addtail(anim_data, ale);
927                                                 items++;
928                                         }
929                                 }
930                         }
931                 }
932         }
933         
934         /* return the number of items added to the list */
935         return items;
936 }
937
938 /* Include ShapeKey Data for ShapeKey Editor */
939 static int animdata_filter_shapekey (ListBase *anim_data, Key *key, int filter_mode)
940 {
941         bAnimListElem *ale;
942         int items = 0;
943         
944         /* check if channels or only F-Curves */
945         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {
946                 KeyBlock *kb;
947                 
948                 /* loop through the channels adding ShapeKeys as appropriate */
949                 for (kb= key->block.first; kb; kb= kb->next) {
950                         /* skip the first one, since that's the non-animateable basis */
951                         // XXX maybe in future this may become handy?
952                         if (kb == key->block.first) continue;
953                         
954                         /* only work with this channel and its subchannels if it is editable */
955                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_SHAPEKEY(kb)) {
956                                 /* only include this track if selected in a way consistent with the filtering requirements */
957                                 if ( ANIMCHANNEL_SELOK(SEL_SHAPEKEY(kb)) ) {
958                                         // TODO: consider 'active' too?
959                                         
960                                         /* owner-id here must be key so that the F-Curve can be resolved... */
961                                         ale= make_new_animlistelem(kb, ANIMTYPE_SHAPEKEY, NULL, ANIMTYPE_NONE, (ID *)key);
962                                         
963                                         if (ale) {
964                                                 BLI_addtail(anim_data, ale);
965                                                 items++;
966                                         }
967                                 }
968                         }
969                 }
970         }
971         else {
972                 /* just use the action associated with the shapekey */
973                 // FIXME: is owner-id and having no owner/dopesheet really fine?
974                 if (key->adt) {
975                         if (filter_mode & ANIMFILTER_ANIMDATA)
976                                 ANIMDATA_ADD_ANIMDATA(key)
977                         else if (key->adt->action)
978                                 items= animdata_filter_action(anim_data, NULL, key->adt->action, filter_mode, NULL, ANIMTYPE_NONE, (ID *)key);
979                 }
980         }
981         
982         /* return the number of items added to the list */
983         return items;
984 }
985
986 #if 0
987 // FIXME: switch this to use the bDopeSheet...
988 static int animdata_filter_gpencil (ListBase *anim_data, bScreen *sc, int filter_mode)
989 {
990         bAnimListElem *ale;
991         ScrArea *sa, *curarea;
992         bGPdata *gpd;
993         bGPDlayer *gpl;
994         int items = 0;
995         
996         /* check if filtering types are appropriate */
997         {
998                 /* special hack for fullscreen area (which must be this one then):
999                  *      - we use the curarea->full as screen to get spaces from, since the
1000                  *        old (pre-fullscreen) screen was stored there...
1001                  *      - this is needed as all data would otherwise disappear
1002                  */
1003                 // XXX need to get new alternative for curarea
1004                 if ((curarea->full) && (curarea->spacetype==SPACE_ACTION))
1005                         sc= curarea->full;
1006                 
1007                 /* loop over spaces in current screen, finding gpd blocks (could be slow!) */
1008                 for (sa= sc->areabase.first; sa; sa= sa->next) {
1009                         /* try to get gp data */
1010                         // XXX need to put back grease pencil api...
1011                         gpd= gpencil_data_get_active(sa);
1012                         if (gpd == NULL) continue;
1013                         
1014                         /* add gpd as channel too (if for drawing, and it has layers) */
1015                         if ((filter_mode & ANIMFILTER_CHANNELS) && (gpd->layers.first)) {
1016                                 /* add to list */
1017                                 ale= make_new_animlistelem(gpd, ANIMTYPE_GPDATABLOCK, sa, ANIMTYPE_SPECIALDATA);
1018                                 if (ale) {
1019                                         BLI_addtail(anim_data, ale);
1020                                         items++;
1021                                 }
1022                         }
1023                         
1024                         /* only add layers if they will be visible (if drawing channels) */
1025                         if ( !(filter_mode & ANIMFILTER_VISIBLE) || (EXPANDED_GPD(gpd)) ) {
1026                                 /* loop over layers as the conditions are acceptable */
1027                                 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
1028                                         /* only if selected */
1029                                         if ( ANIMCHANNEL_SELOK(SEL_GPL(gpl)) ) {
1030                                                 /* only if editable */
1031                                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
1032                                                         /* add to list */
1033                                                         ale= make_new_animlistelem(gpl, ANIMTYPE_GPLAYER, gpd, ANIMTYPE_GPDATABLOCK);
1034                                                         if (ale) {
1035                                                                 BLI_addtail(anim_data, ale);
1036                                                                 items++;
1037                                                         }
1038                                                 }
1039                                         }
1040                                 }
1041                         }
1042                 }
1043         }
1044         
1045         /* return the number of items added to the list */
1046         return items;
1047 }
1048 #endif 
1049
1050
1051 static int animdata_filter_dopesheet_mats (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1052 {
1053         ListBase mats = {NULL, NULL};
1054         LinkData *ld;
1055         
1056         bAnimListElem *ale=NULL;
1057         Object *ob= base->object;
1058         int items=0, a=0;
1059         
1060         /* firstly check that we actuallly have some materials, by gathering all materials in a temp list */
1061         for (a=0; a < ob->totcol; a++) {
1062                 Material *ma= give_current_material(ob, a);
1063                 short ok = 0;
1064                 
1065                 /* for now, if no material returned, skip (this shouldn't confuse the user I hope) */
1066                 if (ELEM(NULL, ma, ma->adt)) 
1067                         continue;
1068                 
1069                 /* check if ok */
1070                 ANIMDATA_FILTER_CASES(ma, 
1071                         { /* AnimData blocks - do nothing... */ },
1072                         ok=1;, 
1073                         ok=1;, 
1074                         ok=1;)
1075                 if (ok == 0) continue;
1076                 
1077                 /* make a temp list elem for this */
1078                 ld= MEM_callocN(sizeof(LinkData), "DopeSheet-MaterialCache");
1079                 ld->data= ma;
1080                 BLI_addtail(&mats, ld);
1081         }
1082         
1083         /* if there were no channels found, no need to carry on */
1084         if (mats.first == NULL)
1085                 return 0;
1086         
1087         /* include materials-expand widget? */
1088         if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1089                 ale= make_new_animlistelem(ob, ANIMTYPE_FILLMATD, base, ANIMTYPE_OBJECT, (ID *)ob);
1090                 if (ale) {
1091                         BLI_addtail(anim_data, ale);
1092                         items++;
1093                 }
1094         }
1095         
1096         /* add materials? */
1097         if (FILTER_MAT_OBJC(ob) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1098                 /* for each material in cache, add channels  */
1099                 for (ld= mats.first; ld; ld= ld->next) {
1100                         Material *ma= (Material *)ld->data;
1101                         
1102                         /* include material-expand widget? */
1103                         // hmm... do we need to store the index of this material in the array anywhere?
1104                         if (filter_mode & ANIMFILTER_CHANNELS) {
1105                                 /* check if filtering by active status */
1106                                 if ANIMCHANNEL_ACTIVEOK(ma) {
1107                                         ale= make_new_animlistelem(ma, ANIMTYPE_DSMAT, base, ANIMTYPE_OBJECT, (ID *)ma);
1108                                         if (ale) {
1109                                                 BLI_addtail(anim_data, ale);
1110                                                 items++;
1111                                         }
1112                                 }
1113                         }
1114                         
1115                         /* add material's animation data */
1116                         if (!(filter_mode & ANIMFILTER_VISIBLE) || FILTER_MAT_OBJD(ma) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1117                                 ANIMDATA_FILTER_CASES(ma, 
1118                                         { /* AnimData blocks - do nothing... */ },
1119                                         items += animdata_filter_nla(anim_data, ads, ma->adt, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);, 
1120                                         items += animdata_filter_fcurves(anim_data, ads, ma->adt->drivers.first, NULL, ma, ANIMTYPE_DSMAT, filter_mode, (ID *)ma);, 
1121                                         items += animdata_filter_action(anim_data, ads, ma->adt->action, filter_mode, ma, ANIMTYPE_DSMAT, (ID *)ma);)
1122                         }
1123                 }
1124         }
1125         
1126         /* free cache */
1127         BLI_freelistN(&mats);
1128         
1129         /* return the number of items added to the list */
1130         return items;
1131 }
1132
1133 static int animdata_filter_dopesheet_particles (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1134 {
1135         bAnimListElem *ale=NULL;
1136         Object *ob= base->object;
1137         ParticleSystem *psys = ob->particlesystem.first;
1138         int items= 0, first = 1;
1139
1140         for(; psys; psys=psys->next) {
1141                 short ok = 0;
1142
1143                 if(ELEM(NULL, psys->part, psys->part->adt))
1144                         continue;
1145
1146                 ANIMDATA_FILTER_CASES(psys->part,
1147                         { /* AnimData blocks - do nothing... */ },
1148                         ok=1;, 
1149                         ok=1;, 
1150                         ok=1;)
1151                 if (ok == 0) continue;
1152
1153                 /* include particles-expand widget? */
1154                 if (first && (filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1155                         ale= make_new_animlistelem(ob, ANIMTYPE_FILLPARTD, base, ANIMTYPE_OBJECT, (ID *)ob);
1156                         if (ale) {
1157                                 BLI_addtail(anim_data, ale);
1158                                 items++;
1159                         }
1160                         first = 0;
1161                 }
1162                 
1163                 /* add particle settings? */
1164                 if (FILTER_PART_OBJC(ob) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1165                         if ((filter_mode & ANIMFILTER_CHANNELS)) {
1166                                 /* check if filtering by active status */
1167                                 if ANIMCHANNEL_ACTIVEOK(psys->part) {
1168                                         ale = make_new_animlistelem(psys->part, ANIMTYPE_DSPART, base, ANIMTYPE_OBJECT, (ID *)psys->part);
1169                                         if (ale) {
1170                                                 BLI_addtail(anim_data, ale);
1171                                                 items++;
1172                                         }
1173                                 }
1174                         }
1175                         
1176                         if (!(filter_mode & ANIMFILTER_VISIBLE) || FILTER_PART_OBJD(psys->part) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1177                                 ANIMDATA_FILTER_CASES(psys->part,
1178                                         { /* AnimData blocks - do nothing... */ },
1179                                         items += animdata_filter_nla(anim_data, ads, psys->part->adt, filter_mode, psys->part, ANIMTYPE_DSPART, (ID *)psys->part);, 
1180                                         items += animdata_filter_fcurves(anim_data, ads, psys->part->adt->drivers.first, NULL, psys->part, ANIMTYPE_DSPART, filter_mode, (ID *)psys->part);, 
1181                                         items += animdata_filter_action(anim_data, ads, psys->part->adt->action, filter_mode, psys->part, ANIMTYPE_DSPART, (ID *)psys->part);)
1182                         }
1183                 }
1184         }
1185         
1186         /* return the number of items added to the list */
1187         return items;
1188 }
1189
1190 static int animdata_filter_dopesheet_obdata (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1191 {
1192         bAnimListElem *ale=NULL;
1193         Object *ob= base->object;
1194         IdAdtTemplate *iat= ob->data;
1195         AnimData *adt= iat->adt;
1196         short type=0, expanded=0;
1197         int items= 0;
1198         
1199         /* get settings based on data type */
1200         switch (ob->type) {
1201                 case OB_CAMERA: /* ------- Camera ------------ */
1202                 {
1203                         Camera *ca= (Camera *)ob->data;
1204                         
1205                         type= ANIMTYPE_DSCAM;
1206                         expanded= FILTER_CAM_OBJD(ca);
1207                 }
1208                         break;
1209                 case OB_LAMP: /* ---------- Lamp ----------- */
1210                 {
1211                         Lamp *la= (Lamp *)ob->data;
1212                         
1213                         type= ANIMTYPE_DSLAM;
1214                         expanded= FILTER_LAM_OBJD(la);
1215                 }
1216                         break;
1217                 case OB_CURVE: /* ------- Curve ---------- */
1218                 {
1219                         Curve *cu= (Curve *)ob->data;
1220                         
1221                         type= ANIMTYPE_DSCUR;
1222                         expanded= FILTER_CUR_OBJD(cu);
1223                 }
1224                         break;
1225                 case OB_MBALL: /* ------- MetaBall ---------- */
1226                 {
1227                         MetaBall *mb= (MetaBall *)ob->data;
1228                         
1229                         type= ANIMTYPE_DSMBALL;
1230                         expanded= FILTER_MBALL_OBJD(mb);
1231                 }
1232                         break;
1233                 case OB_ARMATURE: /* ------- Armature ---------- */
1234                 {
1235                         bArmature *arm= (bArmature *)ob->data;
1236                         
1237                         type= ANIMTYPE_DSARM;
1238                         expanded= FILTER_ARM_OBJD(arm);
1239                 }
1240                         break;
1241         }
1242         
1243         /* special exception for drivers instead of action */
1244         if (ads->filterflag & ADS_FILTER_ONLYDRIVERS)
1245                 expanded= EXPANDED_DRVD(adt);
1246         
1247         /* include data-expand widget? */
1248         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {       
1249                 /* check if filtering by active status */
1250                 if ANIMCHANNEL_ACTIVEOK(iat) {
1251                         ale= make_new_animlistelem(iat, type, base, ANIMTYPE_OBJECT, (ID *)iat);
1252                         if (ale) BLI_addtail(anim_data, ale);
1253                 }
1254         }
1255         
1256         /* add object-data animation channels? */
1257         if (!(filter_mode & ANIMFILTER_VISIBLE) || (expanded) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1258                 /* filtering for channels - nla, drivers, keyframes */
1259                 ANIMDATA_FILTER_CASES(iat, 
1260                         { /* AnimData blocks - do nothing... */ },
1261                         items+= animdata_filter_nla(anim_data, ads, iat->adt, filter_mode, iat, type, (ID *)iat);,
1262                         items+= animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, iat, type, filter_mode, (ID *)iat);, 
1263                         items += animdata_filter_action(anim_data, ads, iat->adt->action, filter_mode, iat, type, (ID *)iat);)
1264         }
1265         
1266         /* return the number of items added to the list */
1267         return items;
1268 }
1269
1270 static int animdata_filter_dopesheet_ob (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1271 {
1272         bAnimListElem *ale=NULL;
1273         AnimData *adt = NULL;
1274         Object *ob= base->object;
1275         Key *key= ob_get_key(ob);
1276         short obdata_ok = 0;
1277         int items = 0;
1278         
1279         /* add this object as a channel first */
1280         if ((filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) == 0) {
1281                 /* check if filtering by selection */
1282                 if ANIMCHANNEL_SELOK((base->flag & SELECT)) {
1283                         /* check if filtering by active status */
1284                         if ANIMCHANNEL_ACTIVEOK(ob) {
1285                                 ale= make_new_animlistelem(base, ANIMTYPE_OBJECT, NULL, ANIMTYPE_NONE, (ID *)ob);
1286                                 if (ale) {
1287                                         BLI_addtail(anim_data, ale);
1288                                         items++;
1289                                 }
1290                         }
1291                 }
1292         }
1293         
1294         /* if collapsed, don't go any further (unless adding keyframes only) */
1295         if ( ((filter_mode & ANIMFILTER_VISIBLE) && EXPANDED_OBJC(ob) == 0) &&
1296                  !(filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) )
1297                 return items;
1298         
1299         /* Action, Drivers, or NLA */
1300         if (ob->adt) {
1301                 adt= ob->adt;
1302                 ANIMDATA_FILTER_CASES(ob,
1303                         { /* AnimData blocks - do nothing... */ },
1304                         { /* nla */
1305                                 /* add NLA tracks */
1306                                 items += animdata_filter_nla(anim_data, ads, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1307                         },
1308                         { /* drivers */
1309                                 /* include drivers-expand widget? */
1310                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1311                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, base, ANIMTYPE_OBJECT, (ID *)ob);
1312                                         if (ale) {
1313                                                 BLI_addtail(anim_data, ale);
1314                                                 items++;
1315                                         }
1316                                 }
1317                                 
1318                                 /* add F-Curve channels (drivers are F-Curves) */
1319                                 if (!(filter_mode & ANIMFILTER_VISIBLE) || EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1320                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1321                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, ob, ANIMTYPE_OBJECT, filter_mode, (ID *)ob);
1322                                 }
1323                         },
1324                         { /* action (keyframes) */
1325                                 /* include action-expand widget? */
1326                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1327                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, base, ANIMTYPE_OBJECT, (ID *)ob);
1328                                         if (ale) {
1329                                                 BLI_addtail(anim_data, ale);
1330                                                 items++;
1331                                         }
1332                                 }
1333                                 
1334                                 /* add F-Curve channels? */
1335                                 if (!(filter_mode & ANIMFILTER_VISIBLE) || EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1336                                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
1337                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob); 
1338                                 }
1339                         }
1340                 );
1341         }
1342         
1343         
1344         /* ShapeKeys? */
1345         if ((key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
1346                 adt= key->adt;
1347                 ANIMDATA_FILTER_CASES(key,
1348                         { /* AnimData blocks - do nothing... */ },
1349                         { /* nla */
1350                                 /* include shapekey-expand widget? */
1351                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1352                                         /* check if filtering by active status */
1353                                         if ANIMCHANNEL_ACTIVEOK(key) {
1354                                                 ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1355                                                 if (ale) {
1356                                                         BLI_addtail(anim_data, ale);
1357                                                         items++;
1358                                                 }
1359                                         }
1360                                 }
1361                                 
1362                                 /* add NLA tracks - only if expanded or so */
1363                                 if (!(filter_mode & ANIMFILTER_VISIBLE) || FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY))
1364                                         items += animdata_filter_nla(anim_data, ads, adt, filter_mode, ob, ANIMTYPE_OBJECT, (ID *)ob);
1365                         },
1366                         { /* drivers */
1367                                 /* include shapekey-expand widget? */
1368                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1369                                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1370                                         if (ale) {
1371                                                 BLI_addtail(anim_data, ale);
1372                                                 items++;
1373                                         }
1374                                 }
1375                                 
1376                                 /* add channels */
1377                                 if (!(filter_mode & ANIMFILTER_VISIBLE) || FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1378                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, key, ANIMTYPE_DSSKEY, filter_mode, (ID *)key);
1379                                 }
1380                         },
1381                         { /* action (keyframes) */
1382                                 /* include shapekey-expand widget? */
1383                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1384                                         /* check if filtering by active status */
1385                                         if ANIMCHANNEL_ACTIVEOK(key) {
1386                                                 ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT, (ID *)ob);
1387                                                 if (ale) {
1388                                                         BLI_addtail(anim_data, ale);
1389                                                         items++;
1390                                                 }
1391                                         }
1392                                 }
1393                                 
1394                                 /* add channels */
1395                                 if (!(filter_mode & ANIMFILTER_VISIBLE) || FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1396                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, key, ANIMTYPE_DSSKEY, (ID *)key); 
1397                                 }
1398                         }
1399                 );
1400         }
1401
1402         /* Materials? */
1403         if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT))
1404                 items += animdata_filter_dopesheet_mats(anim_data, ads, base, filter_mode);
1405         
1406         /* Object Data */
1407         switch (ob->type) {
1408                 case OB_CAMERA: /* ------- Camera ------------ */
1409                 {
1410                         Camera *ca= (Camera *)ob->data;
1411                         
1412                         if ((ads->filterflag & ADS_FILTER_NOCAM) == 0) {
1413                                 ANIMDATA_FILTER_CASES(ca,
1414                                         { /* AnimData blocks - do nothing... */ },
1415                                         obdata_ok= 1;,
1416                                         obdata_ok= 1;,
1417                                         obdata_ok= 1;)
1418                         }
1419                 }
1420                         break;
1421                 case OB_LAMP: /* ---------- Lamp ----------- */
1422                 {
1423                         Lamp *la= (Lamp *)ob->data;
1424                         
1425                         if ((ads->filterflag & ADS_FILTER_NOLAM) == 0) {
1426                                 ANIMDATA_FILTER_CASES(la,
1427                                         { /* AnimData blocks - do nothing... */ },
1428                                         obdata_ok= 1;,
1429                                         obdata_ok= 1;,
1430                                         obdata_ok= 1;)
1431                         }
1432                 }
1433                         break;
1434                 case OB_CURVE: /* ------- Curve ---------- */
1435                 {
1436                         Curve *cu= (Curve *)ob->data;
1437                         
1438                         if ((ads->filterflag & ADS_FILTER_NOCUR) == 0) {
1439                                 ANIMDATA_FILTER_CASES(cu,
1440                                         { /* AnimData blocks - do nothing... */ },
1441                                         obdata_ok= 1;,
1442                                         obdata_ok= 1;,
1443                                         obdata_ok= 1;)
1444                         }
1445                 }
1446                         break;
1447                 case OB_MBALL: /* ------- MetaBall ---------- */
1448                 {
1449                         MetaBall *mb= (MetaBall *)ob->data;
1450                         
1451                         if ((ads->filterflag & ADS_FILTER_NOMBA) == 0) {
1452                                 ANIMDATA_FILTER_CASES(mb,
1453                                         { /* AnimData blocks - do nothing... */ },
1454                                         obdata_ok= 1;,
1455                                         obdata_ok= 1;,
1456                                         obdata_ok= 1;)
1457                         }
1458                 }
1459                         break;
1460                 case OB_ARMATURE: /* ------- Armature ---------- */
1461                 {
1462                         bArmature *arm= (bArmature *)ob->data;
1463                         
1464                         if ((ads->filterflag & ADS_FILTER_NOARM) == 0) {
1465                                 ANIMDATA_FILTER_CASES(arm,
1466                                         { /* AnimData blocks - do nothing... */ },
1467                                         obdata_ok= 1;,
1468                                         obdata_ok= 1;,
1469                                         obdata_ok= 1;)
1470                         }
1471                 }
1472                         break;
1473         }
1474         if (obdata_ok) 
1475                 items += animdata_filter_dopesheet_obdata(anim_data, ads, base, filter_mode);
1476
1477         /* particles */
1478         if (ob->particlesystem.first && !(ads->filterflag & ADS_FILTER_NOPART))
1479                 items += animdata_filter_dopesheet_particles(anim_data, ads, base, filter_mode);
1480         
1481         /* return the number of items added to the list */
1482         return items;
1483 }       
1484
1485 static int animdata_filter_dopesheet_scene (ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
1486 {
1487         World *wo= sce->world;
1488         AnimData *adt= NULL;
1489         bAnimListElem *ale;
1490         int items = 0;
1491         
1492         /* add scene as a channel first (even if we aren't showing scenes we still need to show the scene's sub-data */
1493         if ((filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) == 0) {
1494                 /* check if filtering by selection */
1495                 if (ANIMCHANNEL_SELOK( (sce->flag & SCE_DS_SELECTED) )) {
1496                         ale= make_new_animlistelem(sce, ANIMTYPE_SCENE, NULL, ANIMTYPE_NONE, NULL);
1497                         if (ale) {
1498                                 BLI_addtail(anim_data, ale);
1499                                 items++;
1500                         }
1501                 }
1502         }
1503         
1504         /* if collapsed, don't go any further (unless adding keyframes only) */
1505         if ( (EXPANDED_SCEC(sce) == 0) && !(filter_mode & (ANIMFILTER_CURVESONLY|ANIMFILTER_NLATRACKS)) )
1506                 return items;
1507                 
1508         /* Action, Drivers, or NLA for Scene */
1509         if ((ads->filterflag & ADS_FILTER_NOSCE) == 0) {
1510                 adt= sce->adt;
1511                 ANIMDATA_FILTER_CASES(sce,
1512                         { /* AnimData blocks - do nothing... */ },
1513                         { /* nla */
1514                                 /* add NLA tracks */
1515                                 items += animdata_filter_nla(anim_data, ads, adt, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce);
1516                         },
1517                         { /* drivers */
1518                                 /* include drivers-expand widget? */
1519                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1520                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLDRIVERS, sce, ANIMTYPE_SCENE, (ID *)sce);
1521                                         if (ale) {
1522                                                 BLI_addtail(anim_data, ale);
1523                                                 items++;
1524                                         }
1525                                 }
1526                                 
1527                                 /* add F-Curve channels (drivers are F-Curves) */
1528                                 if (EXPANDED_DRVD(adt) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1529                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, sce, ANIMTYPE_SCENE, filter_mode, (ID *)sce);
1530                                 }
1531                         },
1532                         { /* action */
1533                                 /* include action-expand widget? */
1534                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1535                                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, sce, ANIMTYPE_SCENE, (ID *)sce);
1536                                         if (ale) {
1537                                                 BLI_addtail(anim_data, ale);
1538                                                 items++;
1539                                         }
1540                                 }
1541                                 
1542                                 /* add F-Curve channels? */
1543                                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
1544                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, sce, ANIMTYPE_SCENE, (ID *)sce); 
1545                                 }
1546                         }
1547                 )
1548         }
1549         
1550         /* world */
1551         if ((wo && wo->adt) && !(ads->filterflag & ADS_FILTER_NOWOR)) {
1552                 /* Action, Drivers, or NLA for World */
1553                 adt= wo->adt;
1554                 ANIMDATA_FILTER_CASES(wo,
1555                         { /* AnimData blocks - do nothing... */ },
1556                         { /* nla */
1557                                 /* add NLA tracks */
1558                                 items += animdata_filter_nla(anim_data, ads, adt, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo);
1559                         },
1560                         { /* drivers */
1561                                 /* include world-expand widget? */
1562                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1563                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)wo);
1564                                         if (ale) {
1565                                                 BLI_addtail(anim_data, ale);
1566                                                 items++;
1567                                         }
1568                                 }
1569                                 
1570                                 /* add F-Curve channels (drivers are F-Curves) */
1571                                 if (FILTER_WOR_SCED(wo)/*EXPANDED_DRVD(adt)*/ || !(filter_mode & ANIMFILTER_CHANNELS)) {
1572                                         // XXX owner info is messed up now...
1573                                         items += animdata_filter_fcurves(anim_data, ads, adt->drivers.first, NULL, wo, ANIMTYPE_DSWOR, filter_mode, (ID *)wo);
1574                                 }
1575                         },
1576                         { /* action */
1577                                 /* include world-expand widget? */
1578                                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
1579                                         ale= make_new_animlistelem(wo, ANIMTYPE_DSWOR, sce, ANIMTYPE_SCENE, (ID *)sce);
1580                                         if (ale) {
1581                                                 BLI_addtail(anim_data, ale);
1582                                                 items++;
1583                                         }
1584                                 }
1585                                 
1586                                 /* add channels */
1587                                 if (FILTER_WOR_SCED(wo) || (filter_mode & ANIMFILTER_CURVESONLY)) {
1588                                         items += animdata_filter_action(anim_data, ads, adt->action, filter_mode, wo, ANIMTYPE_DSWOR, (ID *)wo); 
1589                                 }
1590                         }
1591                 )
1592         }
1593         
1594         // TODO: scene compositing nodes (these aren't standard node-trees)
1595         
1596         /* return the number of items added to the list */
1597         return items;
1598 }
1599
1600 // 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)
1601 static int animdata_filter_dopesheet (ListBase *anim_data, bAnimContext *ac, bDopeSheet *ads, int filter_mode)
1602 {
1603         Scene *sce= (Scene *)ads->source;
1604         Base *base;
1605         bAnimListElem *ale;
1606         int items = 0;
1607         
1608         /* check that we do indeed have a scene */
1609         if ((ads->source == NULL) || (GS(ads->source->name)!=ID_SCE)) {
1610                 printf("DopeSheet Error: Not scene!\n");
1611                 if (G.f & G_DEBUG)
1612                         printf("\tPointer = %p, Name = '%s' \n", ads->source, (ads->source)?ads->source->name:NULL);
1613                 return 0;
1614         }
1615         
1616         /* scene-linked animation */
1617         // TODO: sequencer, composite nodes - are we to include those here too?
1618         {
1619                 short sceOk= 0, worOk= 0;
1620                 
1621                 /* check filtering-flags if ok */
1622                 ANIMDATA_FILTER_CASES(sce, 
1623                         {
1624                                 /* for the special AnimData blocks only case, we only need to add
1625                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1626                                  */
1627                                 ANIMDATA_ADD_ANIMDATA(sce);
1628                                 sceOk=0;
1629                         },
1630                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);, 
1631                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);, 
1632                         sceOk= !(ads->filterflag & ADS_FILTER_NOSCE);)
1633                 if (sce->world) {
1634                         ANIMDATA_FILTER_CASES(sce->world, 
1635                                 {
1636                                         /* for the special AnimData blocks only case, we only need to add
1637                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1638                                          */
1639                                         ANIMDATA_ADD_ANIMDATA(sce->world);
1640                                         worOk=0;
1641                                 },
1642                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);, 
1643                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);, 
1644                                 worOk= !(ads->filterflag & ADS_FILTER_NOWOR);)
1645                 }
1646                 
1647                 /* if only F-Curves with visible flags set can be shown, check that 
1648                  * datablocks haven't been set to invisible 
1649                  */
1650                 if (filter_mode & ANIMFILTER_CURVEVISIBLE) {
1651                         if ((sce->adt) && (sce->adt->flag & ADT_CURVES_NOT_VISIBLE))
1652                                 sceOk= worOk= 0;
1653                 }
1654                 
1655                 /* check if not all bad (i.e. so there is something to show) */
1656                 if ( !(!sceOk && !worOk) ) {
1657                         /* add scene data to the list of filtered channels */
1658                         items += animdata_filter_dopesheet_scene(anim_data, ads, sce, filter_mode);
1659                 }
1660         }
1661         
1662         
1663         /* loop over all bases in the scene */
1664         for (base= sce->base.first; base; base= base->next) {
1665                 /* check if there's an object (all the relevant checks are done in the ob-function) */
1666                 if (base->object) {
1667                         Object *ob= base->object;
1668                         Key *key= ob_get_key(ob);
1669                         short actOk=1, keyOk=1, dataOk=1, matOk=1, partOk=1;
1670                         
1671                         /* firstly, check if object can be included, by the following factors:
1672                          *      - if only visible, must check for layer and also viewport visibility
1673                          *      - if only selected, must check if object is selected 
1674                          *      - there must be animation data to edit
1675                          */
1676                         // TODO: if cache is implemented, just check name here, and then 
1677                         if (filter_mode & ANIMFILTER_VISIBLE) {
1678                                 /* layer visibility - we check both object and base, since these may not be in sync yet */
1679                                 if ((sce->lay & (ob->lay|base->lay))==0) continue;
1680                                 
1681                                 /* outliner restrict-flag */
1682                                 if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
1683                         }
1684                         
1685                         /* if only F-Curves with visible flags set can be shown, check that 
1686                          * datablock hasn't been set to invisible 
1687                          */
1688                         if (filter_mode & ANIMFILTER_CURVEVISIBLE) {
1689                                 if ((ob->adt) && (ob->adt->flag & ADT_CURVES_NOT_VISIBLE))
1690                                         continue;
1691                         }
1692                         
1693                         /* additionally, dopesheet filtering also affects what objects to consider */
1694                         if (ads->filterflag) {
1695                                 /* check selection and object type filters */
1696                                 if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) || (base == sce->basact)) )  {
1697                                         /* only selected should be shown */
1698                                         continue;
1699                                 }
1700                                 
1701                                 /* check filters for datatypes */
1702                                         /* object */
1703                                 actOk= 0;
1704                                 keyOk= 0;
1705                                 ANIMDATA_FILTER_CASES(ob, 
1706                                         {
1707                                                 /* for the special AnimData blocks only case, we only need to add
1708                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1709                                                  */
1710                                                 ANIMDATA_ADD_ANIMDATA(ob);
1711                                                 actOk=0;
1712                                         },
1713                                         actOk= 1;, 
1714                                         actOk= 1;, 
1715                                         actOk= 1;)
1716                                 if (key) {
1717                                         /* shapekeys */
1718                                         ANIMDATA_FILTER_CASES(key, 
1719                                                 {
1720                                                         /* for the special AnimData blocks only case, we only need to add
1721                                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1722                                                          */
1723                                                         ANIMDATA_ADD_ANIMDATA(key);
1724                                                         keyOk=0;
1725                                                 },
1726                                                 keyOk= 1;, 
1727                                                 keyOk= 1;, 
1728                                                 keyOk= 1;)
1729                                 }
1730                                 
1731                                 /* materials - only for geometric types */
1732                                 matOk= 0; /* by default, not ok... */
1733                                 if ( !(ads->filterflag & ADS_FILTER_NOMAT) && (ob->totcol) && 
1734                                          ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) ) 
1735                                 {
1736                                         int a;
1737                                         
1738                                         /* firstly check that we actuallly have some materials */
1739                                         for (a=0; a < ob->totcol; a++) {
1740                                                 Material *ma= give_current_material(ob, a);
1741                                                 
1742                                                 if (ma) {
1743                                                         /* if material has relevant animation data, break */
1744                                                         ANIMDATA_FILTER_CASES(ma, 
1745                                                                 {
1746                                                                         /* for the special AnimData blocks only case, we only need to add
1747                                                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1748                                                                          */
1749                                                                         ANIMDATA_ADD_ANIMDATA(ma);
1750                                                                         matOk=0;
1751                                                                 },
1752                                                                 matOk= 1;, 
1753                                                                 matOk= 1;, 
1754                                                                 matOk= 1;)
1755                                                 }
1756                                                         
1757                                                 if (matOk) 
1758                                                         break;
1759                                         }
1760                                 }
1761                                 
1762                                 /* data */
1763                                 switch (ob->type) {
1764                                         case OB_CAMERA: /* ------- Camera ------------ */
1765                                         {
1766                                                 Camera *ca= (Camera *)ob->data;
1767                                                 dataOk= 0;
1768                                                 ANIMDATA_FILTER_CASES(ca, 
1769                                                         if ((ads->filterflag & ADS_FILTER_NOCAM)==0) {
1770                                                                 /* for the special AnimData blocks only case, we only need to add
1771                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1772                                                                  */
1773                                                                 ANIMDATA_ADD_ANIMDATA(ca);
1774                                                                 dataOk=0;
1775                                                         },
1776                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);, 
1777                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);, 
1778                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCAM);)
1779                                         }
1780                                                 break;
1781                                         case OB_LAMP: /* ---------- Lamp ----------- */
1782                                         {
1783                                                 Lamp *la= (Lamp *)ob->data;
1784                                                 dataOk= 0;
1785                                                 ANIMDATA_FILTER_CASES(la, 
1786                                                         if ((ads->filterflag & ADS_FILTER_NOLAM)==0) {
1787                                                                 /* for the special AnimData blocks only case, we only need to add
1788                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1789                                                                  */
1790                                                                 ANIMDATA_ADD_ANIMDATA(la);
1791                                                                 dataOk=0;
1792                                                         },
1793                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);, 
1794                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);, 
1795                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOLAM);)
1796                                         }
1797                                                 break;
1798                                         case OB_CURVE: /* ------- Curve ---------- */
1799                                         {
1800                                                 Curve *cu= (Curve *)ob->data;
1801                                                 dataOk= 0;
1802                                                 ANIMDATA_FILTER_CASES(cu, 
1803                                                         if ((ads->filterflag & ADS_FILTER_NOCUR)==0) {
1804                                                                 /* for the special AnimData blocks only case, we only need to add
1805                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1806                                                                  */
1807                                                                 ANIMDATA_ADD_ANIMDATA(cu);
1808                                                                 dataOk=0;
1809                                                         },
1810                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);, 
1811                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);, 
1812                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOCUR);)
1813                                         }
1814                                                 break;
1815                                         case OB_MBALL: /* ------- MetaBall ---------- */
1816                                         {
1817                                                 MetaBall *mb= (MetaBall *)ob->data;
1818                                                 dataOk= 0;
1819                                                 ANIMDATA_FILTER_CASES(mb, 
1820                                                         if ((ads->filterflag & ADS_FILTER_NOMBA)==0) {
1821                                                                 /* for the special AnimData blocks only case, we only need to add
1822                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1823                                                                  */
1824                                                                 ANIMDATA_ADD_ANIMDATA(mb);
1825                                                                 dataOk=0;
1826                                                         },
1827                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOMBA);, 
1828                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOMBA);, 
1829                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOMBA);)
1830                                         }
1831                                                 break;
1832                                         case OB_ARMATURE: /* ------- Armature ---------- */
1833                                         {
1834                                                 bArmature *arm= (bArmature *)ob->data;
1835                                                 dataOk= 0;
1836                                                 ANIMDATA_FILTER_CASES(arm, 
1837                                                         if ((ads->filterflag & ADS_FILTER_NOARM)==0) {
1838                                                                 /* for the special AnimData blocks only case, we only need to add
1839                                                                  * the block if it is valid... then other cases just get skipped (hence ok=0)
1840                                                                  */
1841                                                                 ANIMDATA_ADD_ANIMDATA(arm);
1842                                                                 dataOk=0;
1843                                                         },
1844                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOARM);, 
1845                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOARM);, 
1846                                                         dataOk= !(ads->filterflag & ADS_FILTER_NOARM);)
1847                                         }
1848                                                 break;
1849                                         default: /* --- other --- */
1850                                                 dataOk= 0;
1851                                                 break;
1852                                 }
1853                                 
1854                                 /* particles */
1855                                 partOk = 0;
1856                                 if (!(ads->filterflag & ADS_FILTER_NOPART) && ob->particlesystem.first) {
1857                                         ParticleSystem *psys = ob->particlesystem.first;
1858                                         for(; psys; psys=psys->next) {
1859                                                 if (psys->part) {
1860                                                         /* if particlesettings has relevant animation data, break */
1861                                                         ANIMDATA_FILTER_CASES(psys->part, 
1862                                                                 {
1863                                                                         /* for the special AnimData blocks only case, we only need to add
1864                                                                          * the block if it is valid... then other cases just get skipped (hence ok=0)
1865                                                                          */
1866                                                                         ANIMDATA_ADD_ANIMDATA(psys->part);
1867                                                                         partOk=0;
1868                                                                 },
1869                                                                 partOk= 1;, 
1870                                                                 partOk= 1;, 
1871                                                                 partOk= 1;)
1872                                                 }
1873                                                         
1874                                                 if (partOk) 
1875                                                         break;
1876                                         }
1877                                 }
1878                                 
1879                                 /* check if all bad (i.e. nothing to show) */
1880                                 if (!actOk && !keyOk && !dataOk && !matOk && !partOk)
1881                                         continue;
1882                         }
1883                         else {
1884                                 /* check data-types */
1885                                 actOk= ANIMDATA_HAS_KEYS(ob);
1886                                 keyOk= (key != NULL);
1887                                 
1888                                 /* materials - only for geometric types */
1889                                 matOk= 0; /* by default, not ok... */
1890                                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL) && (ob->totcol)) 
1891                                 {
1892                                         int a;
1893                                         
1894                                         /* firstly check that we actuallly have some materials */
1895                                         for (a=0; a < ob->totcol; a++) {
1896                                                 Material *ma= give_current_material(ob, a);
1897                                                 
1898                                                 if ((ma) && ANIMDATA_HAS_KEYS(ma)) {
1899                                                         matOk= 1;
1900                                                         break;
1901                                                 }
1902                                         }
1903                                 }
1904                                 
1905                                 /* data */
1906                                 switch (ob->type) {
1907                                         case OB_CAMERA: /* ------- Camera ------------ */
1908                                         {
1909                                                 Camera *ca= (Camera *)ob->data;
1910                                                 dataOk= ANIMDATA_HAS_KEYS(ca);                                          
1911                                         }
1912                                                 break;
1913                                         case OB_LAMP: /* ---------- Lamp ----------- */
1914                                         {
1915                                                 Lamp *la= (Lamp *)ob->data;
1916                                                 dataOk= ANIMDATA_HAS_KEYS(la);  
1917                                         }
1918                                                 break;
1919                                         case OB_CURVE: /* -------- Curve ---------- */
1920                                         {
1921                                                 Curve *cu= (Curve *)ob->data;
1922                                                 dataOk= ANIMDATA_HAS_KEYS(cu);  
1923                                         }
1924                                                 break;
1925                                         case OB_MBALL: /* -------- Metas ---------- */
1926                                         {
1927                                                 MetaBall *mb= (MetaBall *)ob->data;
1928                                                 dataOk= ANIMDATA_HAS_KEYS(mb);  
1929                                         }
1930                                                 break;
1931                                         case OB_ARMATURE: /* -------- Armature ---------- */
1932                                         {
1933                                                 bArmature *arm= (bArmature *)ob->data;
1934                                                 dataOk= ANIMDATA_HAS_KEYS(arm); 
1935                                         }
1936                                                 break;
1937                                         default: /* --- other --- */
1938                                                 dataOk= 0;
1939                                                 break;
1940                                 }
1941                                 
1942                                 /* particles */
1943                                 partOk = 0;
1944                                 if (ob->particlesystem.first) {
1945                                         ParticleSystem *psys = ob->particlesystem.first;
1946                                         for(; psys; psys=psys->next) {
1947                                                 if(psys->part && ANIMDATA_HAS_KEYS(psys->part)) {
1948                                                         partOk = 1;
1949                                                         break;
1950                                                 }
1951                                         }
1952                                 }
1953                                 
1954                                 /* check if all bad (i.e. nothing to show) */
1955                                 if (!actOk && !keyOk && !dataOk && !matOk && !partOk)
1956                                         continue;
1957                         }
1958                         
1959                         /* since we're still here, this object should be usable */
1960                         items += animdata_filter_dopesheet_ob(anim_data, ads, base, filter_mode);
1961                 }
1962         }
1963         
1964         /* return the number of items in the list */
1965         return items;
1966 }
1967
1968 /* Summary track for DopeSheet/Action Editor 
1969  *      - return code is whether the summary lets the other channels get drawn
1970  */
1971 static short animdata_filter_dopesheet_summary (bAnimContext *ac, ListBase *anim_data, int filter_mode, int *items)
1972 {
1973         bDopeSheet *ads = NULL;
1974         
1975         /* get the DopeSheet information to use 
1976          *      - we should only need to deal with the DopeSheet/Action Editor, 
1977          *        since all the other Animation Editors won't have this concept
1978          *        being applicable.
1979          */
1980         if ((ac && ac->sa) && (ac->sa->spacetype == SPACE_ACTION)) {
1981                 SpaceAction *saction= (SpaceAction *)ac->sa->spacedata.first;
1982                 ads= &saction->ads;
1983         }
1984         else {
1985                 /* invalid space type - skip this summary channels */
1986                 return 1;
1987         }
1988         
1989         /* dopesheet summary 
1990          *      - only for drawing and/or selecting keyframes in channels, but not for real editing 
1991          *      - only useful for DopeSheet Editor, where the summary is useful
1992          */
1993         // TODO: we should really check if some other prohibited filters are also active, but that can be for later
1994         if ((filter_mode & ANIMFILTER_CHANNELS) && (ads->filterflag & ADS_FILTER_SUMMARY)) {
1995                 bAnimListElem *ale= make_new_animlistelem(ac, ANIMTYPE_SUMMARY, NULL, ANIMTYPE_NONE, NULL);
1996                 if (ale) {
1997                         BLI_addtail(anim_data, ale);
1998                         (*items)++;
1999                 }
2000                 
2001                 /* if summary is collapsed, don't show other channels beneath this 
2002                  *      - this check is put inside the summary check so that it doesn't interfere with normal operation
2003                  */ 
2004                 if (ads->flag & ADS_FLAG_SUMMARY_COLLAPSED)
2005                         return 0;
2006         }
2007         
2008         /* the other channels beneath this can be shown */
2009         return 1;
2010 }  
2011
2012 /* ----------- Public API --------------- */
2013
2014 /* This function filters the active data source to leave only animation channels suitable for
2015  * usage by the caller. It will return the length of the list 
2016  * 
2017  *      *anim_data: is a pointer to a ListBase, to which the filtered animation channels
2018  *              will be placed for use.
2019  *      filter_mode: how should the data be filtered - bitmapping accessed flags
2020  */
2021 int ANIM_animdata_filter (bAnimContext *ac, ListBase *anim_data, int filter_mode, void *data, short datatype)
2022 {
2023         int items = 0;
2024         
2025         /* only filter data if there's somewhere to put it */
2026         if (data && anim_data) {
2027                 bAnimListElem *ale, *next;
2028                 Object *obact= (ac) ? ac->obact : NULL;
2029                 
2030                 /* firstly filter the data */
2031                 switch (datatype) {
2032                         case ANIMCONT_ACTION:   /* 'Action Editor' */
2033                         {
2034                                 /* the check for the DopeSheet summary is included here since the summary works here too */
2035                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2036                                         items += animdata_filter_action(anim_data, NULL, data, filter_mode, NULL, ANIMTYPE_NONE, (ID *)obact);
2037                         }
2038                                 break;
2039                                 
2040                         case ANIMCONT_SHAPEKEY: /* 'ShapeKey Editor' */
2041                         {
2042                                 /* the check for the DopeSheet summary is included here since the summary works here too */
2043                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2044                                         items= animdata_filter_shapekey(anim_data, data, filter_mode);
2045                         }
2046                                 break;
2047                                 
2048                         case ANIMCONT_GPENCIL:
2049                         {
2050                                 //items= animdata_filter_gpencil(anim_data, data, filter_mode);
2051                         }
2052                                 break;
2053                                 
2054                         case ANIMCONT_DOPESHEET: /* 'DopeSheet Editor' */
2055                         {
2056                                 /* the DopeSheet editor is the primary place where the DopeSheet summaries are useful */
2057                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2058                                         items += animdata_filter_dopesheet(anim_data, ac, data, filter_mode);
2059                         }
2060                                 break;
2061                                 
2062                         case ANIMCONT_FCURVES: /* Graph Editor -> FCurves/Animation Editing */
2063                         case ANIMCONT_DRIVERS: /* Graph Editor -> Drivers Editing */
2064                         case ANIMCONT_NLA: /* NLA Editor */
2065                         {
2066                                 /* all of these editors use the basic DopeSheet data for filtering options, but don't have all the same features */
2067                                 items = animdata_filter_dopesheet(anim_data, ac, data, filter_mode);
2068                         }
2069                                 break;
2070                 }
2071                         
2072                 /* remove any weedy entries */
2073                 // XXX this is weedy code!
2074                 for (ale= anim_data->first; ale; ale= next) {
2075                         next= ale->next;
2076                         
2077                         if (ale->type == ANIMTYPE_NONE) {
2078                                 items--;
2079                                 BLI_freelinkN(anim_data, ale);
2080                         }
2081                 }
2082         }
2083         
2084         /* return the number of items in the list */
2085         return items;
2086 }
2087
2088 /* ************************************************************ */