style cleanup: changes to brace placement / newlines - for/while/if/switch
[blender.git] / source / blender / editors / animation / anim_filter.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation, Joshua Leung
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Joshua Leung (original author)
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/animation/anim_filter.c
28  *  \ingroup edanimation
29  */
30
31
32 /* This file contains a system used to provide a layer of abstraction between sources
33  * of animation data and tools in Animation Editors. The method used here involves 
34  * generating a list of edit structures which enable tools to naively perform the actions 
35  * they require without all the boiler-plate associated with loops within loops and checking 
36  * for cases to ignore. 
37  *
38  * While this is primarily used for the Action/Dopesheet Editor (and its accessory modes),
39  * the Graph Editor also uses this for its channel list and for determining which curves
40  * are being edited. Likewise, the NLA Editor also uses this for its channel list and in
41  * its operators.
42  *
43  * Note: much of the original system this was based on was built before the creation of the RNA
44  * system. In future, it would be interesting to replace some parts of this code with RNA queries,
45  * however, RNA does not eliminate some of the boiler-plate reduction benefits presented by this 
46  * system, so if any such work does occur, it should only be used for the internals used here...
47  *
48  * -- Joshua Leung, Dec 2008 (Last revision July 2009)
49  */
50
51 #include <string.h>
52
53 #include "DNA_anim_types.h"
54 #include "DNA_armature_types.h"
55 #include "DNA_camera_types.h"
56 #include "DNA_lamp_types.h"
57 #include "DNA_lattice_types.h"
58 #include "DNA_key_types.h"
59 #include "DNA_material_types.h"
60 #include "DNA_mesh_types.h"
61 #include "DNA_meta_types.h"
62 #include "DNA_node_types.h"
63 #include "DNA_particle_types.h"
64 #include "DNA_space_types.h"
65 #include "DNA_sequence_types.h"
66 #include "DNA_scene_types.h"
67 #include "DNA_screen_types.h"
68 #include "DNA_speaker_types.h"
69 #include "DNA_world_types.h"
70 #include "DNA_gpencil_types.h"
71 #include "DNA_object_types.h"
72
73 #include "MEM_guardedalloc.h"
74
75 #include "BLI_blenlib.h"
76 #include "BLI_utildefines.h"
77 #include "BLI_ghash.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_group.h"
85 #include "BKE_key.h"
86 #include "BKE_main.h"
87 #include "BKE_material.h"
88 #include "BKE_node.h"
89 #include "BKE_sequencer.h"
90 #include "BKE_utildefines.h"
91
92 #include "ED_anim_api.h"
93 #include "ED_markers.h"
94
95 /* ************************************************************ */
96 /* Blender Context <-> Animation Context mapping */
97
98 /* ----------- Private Stuff - Action Editor ------------- */
99
100 /* Get shapekey data being edited (for Action Editor -> ShapeKey mode) */
101 /* Note: there's a similar function in key.c (ob_get_key) */
102 static Key *actedit_get_shapekeys (bAnimContext *ac) 
103 {
104         Scene *scene= ac->scene;
105         Object *ob;
106         Key *key;
107         
108         ob = OBACT;
109         if (ob == NULL) 
110                 return NULL;
111         
112         /* XXX pinning is not available in 'ShapeKey' mode... */
113         //if (saction->pin) return NULL;
114         
115         /* shapekey data is stored with geometry data */
116         key= ob_get_key(ob);
117         
118         if (key) {
119                 if (key->type == KEY_RELATIVE)
120                         return key;
121         }
122         
123         return NULL;
124 }
125
126 /* Get data being edited in Action Editor (depending on current 'mode') */
127 static short actedit_get_context (bAnimContext *ac, SpaceAction *saction)
128 {
129         /* get dopesheet */
130         ac->ads = &saction->ads;
131         
132         /* sync settings with current view status, then return appropriate data */
133         switch (saction->mode) {
134                 case SACTCONT_ACTION: /* 'Action Editor' */
135                         /* if not pinned, sync with active object */
136                         if (/*saction->pin == 0*/1) {
137                                 if (ac->obact && ac->obact->adt)
138                                         saction->action = ac->obact->adt->action;
139                                 else
140                                         saction->action= NULL;
141                         }
142                         
143                         ac->datatype= ANIMCONT_ACTION;
144                         ac->data= saction->action;
145                         
146                         ac->mode= saction->mode;
147                         return 1;
148                         
149                 case SACTCONT_SHAPEKEY: /* 'ShapeKey Editor' */
150                         ac->datatype= ANIMCONT_SHAPEKEY;
151                         ac->data= actedit_get_shapekeys(ac);
152                         
153                         ac->mode= saction->mode;
154                         return 1;
155                         
156                 case SACTCONT_GPENCIL: /* Grease Pencil */ // XXX review how this mode is handled...
157                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
158                         saction->ads.source= (ID *)ac->scene;
159                         
160                         ac->datatype= ANIMCONT_GPENCIL;
161                         ac->data= &saction->ads;
162                         
163                         ac->mode= saction->mode;
164                         return 1;
165                         
166                 case SACTCONT_DOPESHEET: /* DopeSheet */
167                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
168                         saction->ads.source= (ID *)ac->scene;
169                         
170                         ac->datatype= ANIMCONT_DOPESHEET;
171                         ac->data= &saction->ads;
172                         
173                         ac->mode= saction->mode;
174                         return 1;
175                 
176                 default: /* unhandled yet */
177                         ac->datatype= ANIMCONT_NONE;
178                         ac->data= NULL;
179                         
180                         ac->mode= -1;
181                         return 0;
182         }
183 }
184
185 /* ----------- Private Stuff - Graph Editor ------------- */
186
187 /* Get data being edited in Graph Editor (depending on current 'mode') */
188 static short graphedit_get_context (bAnimContext *ac, SpaceIpo *sipo)
189 {
190         /* init dopesheet data if non-existant (i.e. for old files) */
191         if (sipo->ads == NULL) {
192                 sipo->ads= MEM_callocN(sizeof(bDopeSheet), "GraphEdit DopeSheet");
193                 sipo->ads->source= (ID *)ac->scene;
194         }
195         ac->ads = sipo->ads;
196         
197         /* set settings for Graph Editor - "Selected = Editable" */
198         if (sipo->flag & SIPO_SELCUVERTSONLY)
199                 sipo->ads->filterflag |= ADS_FILTER_SELEDIT;
200         else
201                 sipo->ads->filterflag &= ~ADS_FILTER_SELEDIT;
202         
203         /* sync settings with current view status, then return appropriate data */
204         switch (sipo->mode) {
205                 case SIPO_MODE_ANIMATION:       /* Animation F-Curve Editor */
206                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
207                         sipo->ads->source= (ID *)ac->scene;
208                         sipo->ads->filterflag &= ~ADS_FILTER_ONLYDRIVERS;
209                         
210                         ac->datatype= ANIMCONT_FCURVES;
211                         ac->data= sipo->ads;
212                         
213                         ac->mode= sipo->mode;
214                         return 1;
215                 
216                 case SIPO_MODE_DRIVERS:         /* Driver F-Curve Editor */
217                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
218                         sipo->ads->source= (ID *)ac->scene;
219                         sipo->ads->filterflag |= ADS_FILTER_ONLYDRIVERS;
220                         
221                         ac->datatype= ANIMCONT_DRIVERS;
222                         ac->data= sipo->ads;
223                         
224                         ac->mode= sipo->mode;
225                         return 1;
226                 
227                 default: /* unhandled yet */
228                         ac->datatype= ANIMCONT_NONE;
229                         ac->data= NULL;
230                         
231                         ac->mode= -1;
232                         return 0;
233         }
234 }
235
236 /* ----------- Private Stuff - NLA Editor ------------- */
237
238 /* Get data being edited in Graph Editor (depending on current 'mode') */
239 static short nlaedit_get_context (bAnimContext *ac, SpaceNla *snla)
240 {
241         /* init dopesheet data if non-existant (i.e. for old files) */
242         if (snla->ads == NULL)
243                 snla->ads= MEM_callocN(sizeof(bDopeSheet), "NlaEdit DopeSheet");
244         ac->ads = snla->ads;
245         
246         /* sync settings with current view status, then return appropriate data */
247         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
248         snla->ads->source= (ID *)ac->scene;
249         snla->ads->filterflag |= ADS_FILTER_ONLYNLA;
250         
251         ac->datatype= ANIMCONT_NLA;
252         ac->data= snla->ads;
253         
254         return 1;
255 }
256
257 /* ----------- Public API --------------- */
258
259 /* Obtain current anim-data context, given that context info from Blender context has already been set 
260  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
261  *        allocation/freeing costs (which are not that avoidable with channels).
262  */
263 short ANIM_animdata_context_getdata (bAnimContext *ac)
264 {
265         SpaceLink *sl = ac->sl;
266         short ok= 0;
267         
268         /* context depends on editor we are currently in */
269         if (sl) {
270                 switch (ac->spacetype) {
271                         case SPACE_ACTION:
272                         {
273                                 SpaceAction *saction= (SpaceAction *)sl;
274                                 ok= actedit_get_context(ac, saction);
275                         }
276                                 break;
277                                 
278                         case SPACE_IPO:
279                         {
280                                 SpaceIpo *sipo= (SpaceIpo *)sl;
281                                 ok= graphedit_get_context(ac, sipo);
282                         }
283                                 break;
284                                 
285                         case SPACE_NLA:
286                         {
287                                 SpaceNla *snla= (SpaceNla *)sl;
288                                 ok= nlaedit_get_context(ac, snla);
289                         }
290                                 break;
291                 }
292         }
293         
294         /* check if there's any valid data */
295         if (ok && ac->data)
296                 return 1;
297         else
298                 return 0;
299 }
300
301 /* Obtain current anim-data context from Blender Context info 
302  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
303  *        allocation/freeing costs (which are not that avoidable with channels).
304  *      - Clears data and sets the information from Blender Context which is useful
305  */
306 short ANIM_animdata_get_context (const bContext *C, bAnimContext *ac)
307 {
308         ScrArea *sa= CTX_wm_area(C);
309         ARegion *ar= CTX_wm_region(C);
310         SpaceLink *sl= CTX_wm_space_data(C);
311         Scene *scene= CTX_data_scene(C);
312         
313         /* clear old context info */
314         if (ac == NULL) return 0;
315         memset(ac, 0, sizeof(bAnimContext));
316         
317         /* get useful default context settings from context */
318         ac->scene= scene;
319         if (scene) {
320                 ac->markers= ED_context_get_markers(C);         
321                 ac->obact= (scene->basact)?  scene->basact->object : NULL;
322         }
323         ac->sa= sa;
324         ac->ar= ar;
325         ac->sl= sl;
326         ac->spacetype= (sa) ? sa->spacetype : 0;
327         ac->regiontype= (ar) ? ar->regiontype : 0;
328         
329         /* get data context info */
330         return ANIM_animdata_context_getdata(ac);
331 }
332
333 /* ************************************************************ */
334 /* Blender Data <-- Filter --> Channels to be operated on */
335
336 /* macros to use before/after getting the sub-channels of some channel,
337  * to abstract away some of the tricky logic involved
338  *
339  * cases:
340  *      1) Graph Edit main area (just data) OR channels visible in Channel List
341  *      2) If not showing channels, we're only interested in the data (Action Editor's editing)
342  *      3) We don't care what data, we just care there is some (so that a collapsed 
343  *         channel can be kept around). No need to clear channels-flag in order to 
344  *         keep expander channels with no sub-data out, as those cases should get
345  *         dealt with by the recursive detection idiom in place.
346  *
347  * Implementation Note:
348  *      YES the _doSubChannels variable is NOT read anywhere. BUT, this is NOT an excuse
349  *      to go steamrolling the logic into a single-line expression as from experience,
350  *      those are notoriously difficult to read + debug when extending later on. The code
351  *      below is purposefully laid out so that each case noted above corresponds clearly to
352  *      one case below.
353  */
354 #define BEGIN_ANIMFILTER_SUBCHANNELS(expanded_check) \
355         { \
356                 int _filter = filter_mode; \
357                 short _doSubChannels = 0; \
358                 if (!(filter_mode & ANIMFILTER_LIST_VISIBLE) || (expanded_check)) \
359                         _doSubChannels=1; \
360                 else if (!(filter_mode & ANIMFILTER_LIST_CHANNELS)) \
361                         _doSubChannels=2; \
362                 else {\
363                         filter_mode |= ANIMFILTER_TMP_PEEK; \
364                 } \
365                   \
366                 { \
367                         (void) _doSubChannels; \
368                 }
369                 /* ... standard sub-channel filtering can go on here now ... */
370 #define END_ANIMFILTER_SUBCHANNELS \
371                 filter_mode = _filter; \
372         }
373
374 /* ............................... */
375
376 /* quick macro to test if AnimData is usable */
377 #define ANIMDATA_HAS_KEYS(id) ((id)->adt && (id)->adt->action)
378
379 /* quick macro to test if AnimData is usable for drivers */
380 #define ANIMDATA_HAS_DRIVERS(id) ((id)->adt && (id)->adt->drivers.first)
381
382 /* quick macro to test if AnimData is usable for NLA */
383 #define ANIMDATA_HAS_NLA(id) ((id)->adt && (id)->adt->nla_tracks.first)
384
385 /* Quick macro to test for all three above usability tests, performing the appropriate provided 
386  * action for each when the AnimData context is appropriate. 
387  *
388  * Priority order for this goes (most important, to least): AnimData blocks, NLA, Drivers, Keyframes.
389  *
390  * For this to work correctly, a standard set of data needs to be available within the scope that this
391  * gets called in: 
392  *      - ListBase anim_data;
393  *      - bDopeSheet *ads;
394  *      - bAnimListElem *ale;
395  *      - size_t items;
396  *
397  *      - id: ID block which should have an AnimData pointer following it immediately, to use
398  *      - adtOk: line or block of code to execute for AnimData-blocks case (usually ANIMDATA_ADD_ANIMDATA)
399  *      - nlaOk: line or block of code to execute for NLA tracks+strips case
400  *      - driversOk: line or block of code to execute for Drivers case
401  *      - keysOk: line or block of code for Keyframes case
402  *
403  * The checks for the various cases are as follows:
404  *      0) top level: checks for animdata and also that all the F-Curves for the block will be visible
405  *      1) animdata check: for filtering animdata blocks only
406  *      2A) nla tracks: include animdata block's data as there are NLA tracks+strips there
407  *      2B) actions to convert to nla: include animdata block's data as there is an action that can be 
408  *              converted to a new NLA strip, and the filtering options allow this
409  *      2C) allow non-animated datablocks to be included so that datablocks can be added
410  *      3) drivers: include drivers from animdata block (for Drivers mode in Graph Editor)
411  *      4) normal keyframes: only when there is an active action
412  */
413 #define ANIMDATA_FILTER_CASES(id, adtOk, nlaOk, driversOk, keysOk) \
414         {\
415                 if ((id)->adt) {\
416                         if (!(filter_mode & ANIMFILTER_CURVE_VISIBLE) || !((id)->adt->flag & ADT_CURVES_NOT_VISIBLE)) {\
417                                 if (filter_mode & ANIMFILTER_ANIMDATA) {\
418                                         adtOk\
419                                 }\
420                                 else if (ads->filterflag & ADS_FILTER_ONLYNLA) {\
421                                         if (ANIMDATA_HAS_NLA(id)) {\
422                                                 nlaOk\
423                                         }\
424                                         else if (!(ads->filterflag & ADS_FILTER_NLA_NOACT) && ANIMDATA_HAS_KEYS(id)) {\
425                                                 nlaOk\
426                                         }\
427                                 }\
428                                 else if (ads->filterflag & ADS_FILTER_ONLYDRIVERS) {\
429                                         if (ANIMDATA_HAS_DRIVERS(id)) {\
430                                                 driversOk\
431                                         }\
432                                 }\
433                                 else {\
434                                         if (ANIMDATA_HAS_KEYS(id)) {\
435                                                 keysOk\
436                                         }\
437                                 }\
438                         }\
439                 }\
440         }
441
442 /* ............................... */
443
444 /* Add a new animation channel, taking into account the "peek" flag, which is used to just check 
445  * whether any channels will be added (but without needing them to actually get created).
446  *
447  * ! This causes the calling function to return early if we're only "peeking" for channels
448  */
449 // XXX: ale_statement stuff is really a hack for one special case. It shouldn't really be needed...
450 #define ANIMCHANNEL_NEW_CHANNEL_FULL(channel_data, channel_type, owner_id, ale_statement) \
451         if (filter_mode & ANIMFILTER_TMP_PEEK) \
452                 return 1; \
453         else { \
454                 bAnimListElem *ale= make_new_animlistelem(channel_data, channel_type, (ID *)owner_id); \
455                 if (ale) {\
456                         BLI_addtail(anim_data, ale); \
457                         items++; \
458                         ale_statement \
459                 } \
460         }
461         
462 #define ANIMCHANNEL_NEW_CHANNEL(channel_data, channel_type, owner_id) \
463         ANIMCHANNEL_NEW_CHANNEL_FULL(channel_data, channel_type, owner_id, {})
464         
465 /* ............................... */
466         
467 /* quick macro to test if an anim-channel representing an AnimData block is suitably active */
468 #define ANIMCHANNEL_ACTIVEOK(ale) \
469         ( !(filter_mode & ANIMFILTER_ACTIVE) || !(ale->adt) || (ale->adt->flag & ADT_UI_ACTIVE) )
470
471 /* quick macro to test if an anim-channel (F-Curve, Group, etc.) is selected in an acceptable way */
472 #define ANIMCHANNEL_SELOK(test_func) \
473                 ( !(filter_mode & (ANIMFILTER_SEL|ANIMFILTER_UNSEL)) || \
474                   ((filter_mode & ANIMFILTER_SEL) && test_func) || \
475                   ((filter_mode & ANIMFILTER_UNSEL) && test_func==0) ) 
476                   
477 /* quick macro to test if an anim-channel (F-Curve) is selected ok for editing purposes 
478  *      - _SELEDIT means that only selected curves will have visible+editable keyframes
479  *
480  * checks here work as follows:
481  *      1) seledit off - don't need to consider the implications of this option
482  *      2) foredit off - we're not considering editing, so channel is ok still
483  *      3) test_func (i.e. selection test) - only if selected, this test will pass
484  */
485 #define ANIMCHANNEL_SELEDITOK(test_func) \
486                 ( !(filter_mode & ANIMFILTER_SELEDIT) || \
487                   !(filter_mode & ANIMFILTER_FOREDIT) || \
488                   (test_func) )
489
490 /* ----------- 'Private' Stuff --------------- */
491
492 /* this function allocates memory for a new bAnimListElem struct for the 
493  * provided animation channel-data. 
494  */
495 static bAnimListElem *make_new_animlistelem (void *data, short datatype, ID *owner_id)
496 {
497         bAnimListElem *ale= NULL;
498         
499         /* only allocate memory if there is data to convert */
500         if (data) {
501                 /* allocate and set generic data */
502                 ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
503                 
504                 ale->data= data;
505                 ale->type= datatype;
506                 
507                 ale->id= owner_id;
508                 ale->adt= BKE_animdata_from_id(owner_id);
509                 
510                 /* do specifics */
511                 switch (datatype) {
512                         case ANIMTYPE_SUMMARY:
513                         {
514                                 /* nothing to include for now... this is just a dummy wrappy around all the other channels 
515                                  * in the DopeSheet, and gets included at the start of the list
516                                  */
517                                 ale->key_data= NULL;
518                                 ale->datatype= ALE_ALL;
519                         }
520                                 break;
521                         
522                         case ANIMTYPE_SCENE:
523                         {
524                                 Scene *sce= (Scene *)data;
525                                 
526                                 ale->flag= sce->flag;
527                                 
528                                 ale->key_data= sce;
529                                 ale->datatype= ALE_SCE;
530                                 
531                                 ale->adt= BKE_animdata_from_id(data);
532                         }
533                                 break;
534                         case ANIMTYPE_OBJECT:
535                         {
536                                 Base *base= (Base *)data;
537                                 Object *ob= base->object;
538                                 
539                                 ale->flag= ob->flag;
540                                 
541                                 ale->key_data= ob;
542                                 ale->datatype= ALE_OB;
543                                 
544                                 ale->adt= BKE_animdata_from_id(&ob->id);
545                         }
546                                 break;
547                         case ANIMTYPE_FILLACTD:
548                         {
549                                 bAction *act= (bAction *)data;
550                                 
551                                 ale->flag= act->flag;
552                                 
553                                 ale->key_data= act;
554                                 ale->datatype= ALE_ACT;
555                         }
556                                 break;
557                         case ANIMTYPE_FILLDRIVERS:
558                         {
559                                 AnimData *adt= (AnimData *)data;
560                                 
561                                 ale->flag= adt->flag;
562                                 
563                                         // XXX... drivers don't show summary for now
564                                 ale->key_data= NULL;
565                                 ale->datatype= ALE_NONE;
566                         }
567                                 break;
568                         
569                         case ANIMTYPE_DSMAT:
570                         {
571                                 Material *ma= (Material *)data;
572                                 AnimData *adt= ma->adt;
573                                 
574                                 ale->flag= FILTER_MAT_OBJD(ma);
575                                 
576                                 ale->key_data= (adt) ? adt->action : NULL;
577                                 ale->datatype= ALE_ACT;
578                                 
579                                 ale->adt= BKE_animdata_from_id(data);
580                         }
581                                 break;
582                         case ANIMTYPE_DSLAM:
583                         {
584                                 Lamp *la= (Lamp *)data;
585                                 AnimData *adt= la->adt;
586                                 
587                                 ale->flag= FILTER_LAM_OBJD(la);
588                                 
589                                 ale->key_data= (adt) ? adt->action : NULL;
590                                 ale->datatype= ALE_ACT;
591                                 
592                                 ale->adt= BKE_animdata_from_id(data);
593                         }
594                                 break;
595                         case ANIMTYPE_DSCAM:
596                         {
597                                 Camera *ca= (Camera *)data;
598                                 AnimData *adt= ca->adt;
599                                 
600                                 ale->flag= FILTER_CAM_OBJD(ca);
601                                 
602                                 ale->key_data= (adt) ? adt->action : NULL;
603                                 ale->datatype= ALE_ACT;
604                                 
605                                 ale->adt= BKE_animdata_from_id(data);
606                         }
607                                 break;
608                         case ANIMTYPE_DSCUR:
609                         {
610                                 Curve *cu= (Curve *)data;
611                                 AnimData *adt= cu->adt;
612                                 
613                                 ale->flag= FILTER_CUR_OBJD(cu);
614                                 
615                                 ale->key_data= (adt) ? adt->action : NULL;
616                                 ale->datatype= ALE_ACT;
617                                 
618                                 ale->adt= BKE_animdata_from_id(data);
619                         }
620                                 break;
621                         case ANIMTYPE_DSARM:
622                         {
623                                 bArmature *arm= (bArmature *)data;
624                                 AnimData *adt= arm->adt;
625                                 
626                                 ale->flag= FILTER_ARM_OBJD(arm);
627                                 
628                                 ale->key_data= (adt) ? adt->action : NULL;
629                                 ale->datatype= ALE_ACT;
630                                 
631                                 ale->adt= BKE_animdata_from_id(data);
632                         }
633                                 break;
634                         case ANIMTYPE_DSMESH:
635                         {
636                                 Mesh *me= (Mesh *)data;
637                                 AnimData *adt= me->adt;
638                                 
639                                 ale->flag= FILTER_MESH_OBJD(me);
640                                 
641                                 ale->key_data= (adt) ? adt->action : NULL;
642                                 ale->datatype= ALE_ACT;
643                                 
644                                 ale->adt= BKE_animdata_from_id(data);
645                         }
646                                 break;
647                         case ANIMTYPE_DSLAT:
648                         {
649                                 Lattice *lt= (Lattice *)data;
650                                 AnimData *adt= lt->adt;
651                                 
652                                 ale->flag= FILTER_LATTICE_OBJD(lt);
653                                 
654                                 ale->key_data= (adt) ? adt->action : NULL;
655                                 ale->datatype= ALE_ACT;
656                                 
657                                 ale->adt= BKE_animdata_from_id(data);
658                         }       
659                                 break;
660                         case ANIMTYPE_DSSPK:
661                         {
662                                 Speaker *spk= (Speaker *)data;
663                                 AnimData *adt= spk->adt;
664
665                                 ale->flag= FILTER_SPK_OBJD(spk);
666
667                                 ale->key_data= (adt) ? adt->action : NULL;
668                                 ale->datatype= ALE_ACT;
669
670                                 ale->adt= BKE_animdata_from_id(data);
671                         }
672                                 break;
673                         case ANIMTYPE_DSSKEY:
674                         {
675                                 Key *key= (Key *)data;
676                                 AnimData *adt= key->adt;
677                                 
678                                 ale->flag= FILTER_SKE_OBJD(key); 
679                                 
680                                 ale->key_data= (adt) ? adt->action : NULL;
681                                 ale->datatype= ALE_ACT;
682                                 
683                                 ale->adt= BKE_animdata_from_id(data);
684                         }
685                                 break;
686                         case ANIMTYPE_DSWOR:
687                         {
688                                 World *wo= (World *)data;
689                                 AnimData *adt= wo->adt;
690                                 
691                                 ale->flag= FILTER_WOR_SCED(wo); 
692                                 
693                                 ale->key_data= (adt) ? adt->action : NULL;
694                                 ale->datatype= ALE_ACT;
695                                 
696                                 ale->adt= BKE_animdata_from_id(data);
697                         }
698                                 break;
699                         case ANIMTYPE_DSNTREE:
700                         {
701                                 bNodeTree *ntree= (bNodeTree *)data;
702                                 AnimData *adt= ntree->adt;
703                                 
704                                 ale->flag= FILTER_NTREE_DATA(ntree); 
705                                 
706                                 ale->key_data= (adt) ? adt->action : NULL;
707                                 ale->datatype= ALE_ACT;
708                                 
709                                 ale->adt= BKE_animdata_from_id(data);
710                         }
711                                 break;
712                         case ANIMTYPE_DSPART:
713                         {
714                                 ParticleSettings *part= (ParticleSettings*)ale->data;
715                                 AnimData *adt= part->adt;
716                                 
717                                 ale->flag= FILTER_PART_OBJD(part); 
718                                 
719                                 ale->key_data= (adt) ? adt->action : NULL;
720                                 ale->datatype= ALE_ACT;
721                                 
722                                 ale->adt= BKE_animdata_from_id(data);
723                         }
724                                 break;
725                         case ANIMTYPE_DSTEX:
726                         {
727                                 Tex *tex= (Tex *)data;
728                                 AnimData *adt= tex->adt;
729                                 
730                                 ale->flag= FILTER_TEX_DATA(tex); 
731                                 
732                                 ale->key_data= (adt) ? adt->action : NULL;
733                                 ale->datatype= ALE_ACT;
734                                 
735                                 ale->adt= BKE_animdata_from_id(data);
736                         }
737                                 break;
738                                 
739                         case ANIMTYPE_GROUP:
740                         {
741                                 bActionGroup *agrp= (bActionGroup *)data;
742                                 
743                                 ale->flag= agrp->flag;
744                                 
745                                 ale->key_data= NULL;
746                                 ale->datatype= ALE_GROUP;
747                         }
748                                 break;
749                         case ANIMTYPE_FCURVE:
750                         {
751                                 FCurve *fcu= (FCurve *)data;
752                                 
753                                 ale->flag= fcu->flag;
754                                 
755                                 ale->key_data= fcu;
756                                 ale->datatype= ALE_FCURVE;
757                         }
758                                 break;
759                                 
760                         case ANIMTYPE_SHAPEKEY:
761                         {
762                                 KeyBlock *kb= (KeyBlock *)data;
763                                 Key *key= (Key *)ale->id;
764                                 
765                                 ale->flag= kb->flag;
766                                 
767                                 /* whether we have keyframes depends on whether there is a Key block to find it from */
768                                 if (key) {
769                                         /* index of shapekey is defined by place in key's list */
770                                         ale->index= BLI_findindex(&key->block, kb);
771                                         
772                                         /* the corresponding keyframes are from the animdata */
773                                         if (ale->adt && ale->adt->action) {
774                                                 bAction *act= ale->adt->action;
775                                                 char *rna_path = key_get_curValue_rnaPath(key, kb);
776                                                 
777                                                 /* try to find the F-Curve which corresponds to this exactly,
778                                                  * then free the MEM_alloc'd string
779                                                  */
780                                                 if (rna_path) {
781                                                         ale->key_data= (void *)list_find_fcurve(&act->curves, rna_path, 0);
782                                                         MEM_freeN(rna_path);
783                                                 }
784                                         }
785                                         ale->datatype= (ale->key_data)? ALE_FCURVE : ALE_NONE;
786                                 }
787                         }       
788                                 break;
789                         
790                         case ANIMTYPE_GPLAYER:
791                         {
792                                 bGPDlayer *gpl= (bGPDlayer *)data;
793                                 
794                                 ale->flag= gpl->flag;
795                                 
796                                 ale->key_data= NULL;
797                                 ale->datatype= ALE_GPFRAME;
798                         }
799                                 break;
800                                 
801                         case ANIMTYPE_NLATRACK:
802                         {
803                                 NlaTrack *nlt= (NlaTrack *)data;
804                                 
805                                 ale->flag= nlt->flag;
806                                 
807                                 ale->key_data= &nlt->strips;
808                                 ale->datatype= ALE_NLASTRIP;
809                         }
810                                 break;
811                         case ANIMTYPE_NLAACTION:
812                         {
813                                 /* nothing to include for now... nothing editable from NLA-perspective here */
814                                 ale->key_data= NULL;
815                                 ale->datatype= ALE_NONE;
816                         }
817                                 break;
818                 }
819         }
820         
821         /* return created datatype */
822         return ale;
823 }
824  
825 /* ----------------------------------------- */
826
827 /* 'Only Selected' selected data filtering
828  * NOTE: when this function returns true, the F-Curve is to be skipped 
829  */
830 static size_t skip_fcurve_selected_data (bDopeSheet *ads, FCurve *fcu, ID *owner_id, int filter_mode)
831 {
832         if (GS(owner_id->name) == ID_OB) {
833                 Object *ob= (Object *)owner_id;
834                 
835                 /* only consider if F-Curve involves pose.bones */
836                 if ((fcu->rna_path) && strstr(fcu->rna_path, "pose.bones")) {
837                         bPoseChannel *pchan;
838                         char *bone_name;
839                         
840                         /* get bone-name, and check if this bone is selected */
841                         bone_name= BLI_getQuotedStr(fcu->rna_path, "pose.bones[");
842                         pchan= get_pose_channel(ob->pose, bone_name);
843                         if (bone_name) MEM_freeN(bone_name);
844                         
845                         /* check whether to continue or skip */
846                         if ((pchan) && (pchan->bone)) {
847                                 /* if only visible channels, skip if bone not visible unless user wants channels from hidden data too */
848                                 if ((filter_mode & ANIMFILTER_DATA_VISIBLE) && !(ads->filterflag & ADS_FILTER_INCL_HIDDEN)) {
849                                         bArmature *arm= (bArmature *)ob->data;
850                                         
851                                         if ((arm->layer & pchan->bone->layer) == 0)
852                                                 return 1;
853                                         // TODO: manually hidden using flags
854                                 }
855                                 
856                                 /* can only add this F-Curve if it is selected */
857                                 if ((pchan->bone->flag & BONE_SELECTED) == 0)
858                                         return 1;
859                         }
860                 }
861         }
862         else if (GS(owner_id->name) == ID_SCE) {
863                 Scene *scene = (Scene *)owner_id;
864                 
865                 /* only consider if F-Curve involves sequence_editor.sequences */
866                 if ((fcu->rna_path) && strstr(fcu->rna_path, "sequences_all")) {
867                         Editing *ed= seq_give_editing(scene, FALSE);
868                         Sequence *seq;
869                         char *seq_name;
870                         
871                         /* get strip name, and check if this strip is selected */
872                         seq_name= BLI_getQuotedStr(fcu->rna_path, "sequences_all[");
873                         seq = get_seq_by_name(ed->seqbasep, seq_name, FALSE);
874                         if (seq_name) MEM_freeN(seq_name);
875                         
876                         /* can only add this F-Curve if it is selected */
877                         if (seq==NULL || (seq->flag & SELECT)==0)
878                                 return 1;
879                 }
880         }
881         else if (GS(owner_id->name) == ID_NT) {
882                 bNodeTree *ntree = (bNodeTree *)owner_id;
883                 
884                 /* check for selected  nodes */
885                 if ((fcu->rna_path) && strstr(fcu->rna_path, "nodes")) {
886                         bNode *node;
887                         char *node_name;
888                         
889                         /* get strip name, and check if this strip is selected */
890                         node_name= BLI_getQuotedStr(fcu->rna_path, "nodes[");
891                         node = nodeFindNodebyName(ntree, node_name);
892                         if (node_name) MEM_freeN(node_name);
893                         
894                         /* can only add this F-Curve if it is selected */
895                         if ((node) && (node->flag & NODE_SELECT)==0)
896                                 return 1;
897                 }
898         }
899         return 0;
900 }
901
902 /* (Display-)Name-based F-Curve filtering
903  * NOTE: when this function returns true, the F-Curve is to be skipped 
904  */
905 static short skip_fcurve_with_name (bDopeSheet *ads, FCurve *fcu, ID *owner_id)
906 {
907         bAnimListElem ale_dummy = {NULL};
908         bAnimChannelType *acf;
909         
910         /* create a dummy wrapper for the F-Curve */
911         ale_dummy.type = ANIMTYPE_FCURVE;
912         ale_dummy.id = owner_id;
913         ale_dummy.data = fcu;
914         
915         /* get type info for channel */
916         acf = ANIM_channel_get_typeinfo(&ale_dummy);
917         if (acf && acf->name) {
918                 char name[256]; /* hopefully this will be enough! */
919                 
920                 /* get name */
921                 acf->name(&ale_dummy, name);
922                 
923                 /* check for partial match with the match string, assuming case insensitive filtering 
924                  * if match, this channel shouldn't be ignored!
925                  */
926                 return BLI_strcasestr(name, ads->searchstr) == NULL;
927         }
928         
929         /* just let this go... */
930         return 1;
931 }
932
933 /* find the next F-Curve that is usable for inclusion */
934 static FCurve *animfilter_fcurve_next (bDopeSheet *ads, FCurve *first, bActionGroup *grp, int filter_mode, ID *owner_id)
935 {
936         FCurve *fcu = NULL;
937         
938         /* loop over F-Curves - assume that the caller of this has already checked that these should be included 
939          * NOTE: we need to check if the F-Curves belong to the same group, as this gets called for groups too...
940          */
941         for (fcu= first; ((fcu) && (fcu->grp==grp)); fcu= fcu->next) {
942                 /* special exception for Pose-Channel/Sequence-Strip/Node Based F-Curves:
943                  *      - the 'Only Selected' data filter should be applied to Pose-Channel data too, but those are
944                  *        represented as F-Curves. The way the filter for objects worked was to be the first check
945                  *        after 'normal' visibility, so this is done first here too...
946                  *      - we currently use an 'approximate' method for getting these F-Curves that doesn't require
947                  *        carefully checking the entire path
948                  *      - this will also affect things like Drivers, and also works for Bone Constraints
949                  */
950                 if ( ((ads) && (ads->filterflag & ADS_FILTER_ONLYSEL)) && (owner_id) ) {
951                         if (skip_fcurve_selected_data(ads, fcu, owner_id, filter_mode))
952                                 continue;
953                 }
954                 
955                 /* only include if visible (Graph Editor check, not channels check) */
956                 if (!(filter_mode & ANIMFILTER_CURVE_VISIBLE) || (fcu->flag & FCURVE_VISIBLE)) {
957                         /* only work with this channel and its subchannels if it is editable */
958                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_FCU(fcu)) {
959                                 /* only include this curve if selected in a way consistent with the filtering requirements */
960                                 if (ANIMCHANNEL_SELOK(SEL_FCU(fcu)) && ANIMCHANNEL_SELEDITOK(SEL_FCU(fcu))) {
961                                         /* only include if this curve is active */
962                                         if (!(filter_mode & ANIMFILTER_ACTIVE) || (fcu->flag & FCURVE_ACTIVE)) {
963                                                 /* name based filtering... */
964                                                 if ( ((ads) && (ads->filterflag & ADS_FILTER_BY_FCU_NAME)) && (owner_id) ) {
965                                                         if (skip_fcurve_with_name(ads, fcu, owner_id))
966                                                                 continue;
967                                                 }
968                                                 
969                                                 /* this F-Curve can be used, so return it */
970                                                 return fcu;
971                                         }
972                                 }
973                         }
974                 }
975         }
976         
977         /* no (more) F-Curves from the list are suitable... */
978         return NULL;
979 }
980
981 static size_t animfilter_fcurves (ListBase *anim_data, bDopeSheet *ads, FCurve *first, bActionGroup *grp, int filter_mode, ID *owner_id)
982 {
983         FCurve *fcu;
984         size_t items = 0;
985         
986         /* loop over every F-Curve able to be included 
987          *      - this for-loop works like this: 
988          *              1) the starting F-Curve is assigned to the fcu pointer so that we have a starting point to search from
989          *              2) the first valid F-Curve to start from (which may include the one given as 'first') in the remaining 
990          *                 list of F-Curves is found, and verified to be non-null
991          *              3) the F-Curve referenced by fcu pointer is added to the list
992          *              4) the fcu pointer is set to the F-Curve after the one we just added, so that we can keep going through 
993          *                 the rest of the F-Curve list without an eternal loop. Back to step 2 :)
994          */
995         for (fcu = first; ( (fcu = animfilter_fcurve_next(ads, fcu, grp, filter_mode, owner_id)) ); fcu = fcu->next) {
996                 ANIMCHANNEL_NEW_CHANNEL(fcu, ANIMTYPE_FCURVE, owner_id);
997         }
998         
999         /* return the number of items added to the list */
1000         return items;
1001 }
1002
1003 static size_t animfilter_act_group (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, bAction *UNUSED(act), bActionGroup *agrp, int filter_mode, ID *owner_id)
1004 {
1005         ListBase tmp_data = {NULL, NULL};
1006         size_t tmp_items = 0;
1007         size_t items = 0;
1008         //int ofilter = filter_mode;
1009         
1010         /* if we care about the selection status of the channels, 
1011          * but the group isn't expanded (1)...
1012          *      (1) this only matters if we actually care about the hierarchy though.
1013          *              - Hierarchy matters: this hack should be applied
1014          *              - Hierarchy ignored: cases like [#21276] won't work properly, unless we skip this hack
1015          */
1016         if ( ((filter_mode & ANIMFILTER_LIST_VISIBLE) && EXPANDED_AGRP(ac, agrp)==0) &&         /* care about hierarchy but group isn't expanded */
1017                   (filter_mode & (ANIMFILTER_SEL|ANIMFILTER_UNSEL)) )                                                   /* care about selection status */        
1018         {
1019                 /* if the group itself isn't selected appropriately, we shouldn't consider it's children either */
1020                 if (ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) == 0)
1021                         return 0;
1022                 
1023                 /* if we're still here, then the selection status of the curves within this group should not matter,
1024                  * since this creates too much overhead for animators (i.e. making a slow workflow)
1025                  *
1026                  * Tools affected by this at time of coding (2010 Feb 09):
1027                  *      - inserting keyframes on selected channels only
1028                  *      - pasting keyframes
1029                  *      - creating ghost curves in Graph Editor
1030                  */
1031                 filter_mode &= ~(ANIMFILTER_SEL|ANIMFILTER_UNSEL|ANIMFILTER_LIST_VISIBLE);
1032         }
1033         
1034         /* add grouped F-Curves */
1035         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_AGRP(ac, agrp))
1036         {
1037                 /* special filter so that we can get just the F-Curves within the active group */
1038                 if (!(filter_mode & ANIMFILTER_ACTGROUPED) || (agrp->flag & AGRP_ACTIVE)) {
1039                         /* for the Graph Editor, curves may be set to not be visible in the view to lessen clutter,
1040                          * but to do this, we need to check that the group doesn't have it's not-visible flag set preventing 
1041                          * all its sub-curves to be shown
1042                          */
1043                         if (!(filter_mode & ANIMFILTER_CURVE_VISIBLE) || !(agrp->flag & AGRP_NOTVISIBLE)) {
1044                                 /* group must be editable for its children to be editable (if we care about this) */
1045                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_AGRP(agrp)) {
1046                                         /* get first F-Curve which can be used here */
1047                                         FCurve *first_fcu = animfilter_fcurve_next(ads, agrp->channels.first, agrp, filter_mode, owner_id);
1048                                         
1049                                         /* filter list, starting from this F-Curve */
1050                                         tmp_items += animfilter_fcurves(&tmp_data, ads, first_fcu, agrp, filter_mode, owner_id);
1051                                 }
1052                         }
1053                 }
1054         }
1055         END_ANIMFILTER_SUBCHANNELS;
1056         
1057         /* did we find anything? */
1058         if (tmp_items) {
1059                 /* add this group as a channel first */
1060                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1061                         /* restore original filter mode so that this next step works ok... */
1062                         //filter_mode = ofilter;
1063                         
1064                         /* filter selection of channel specially here again, since may be open and not subject to previous test */
1065                         if ( ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) ) {
1066                                 ANIMCHANNEL_NEW_CHANNEL(agrp, ANIMTYPE_GROUP, owner_id);
1067                         }
1068                 }
1069                 
1070                 /* now add the list of collected channels */
1071                 BLI_movelisttolist(anim_data, &tmp_data);
1072                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1073                 items += tmp_items;
1074         }
1075         
1076         /* return the number of items added to the list */
1077         return items;
1078 }
1079
1080 static size_t animfilter_action (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, bAction *act, int filter_mode, ID *owner_id)
1081 {
1082         bActionGroup *agrp;
1083         FCurve *lastchan = NULL;
1084         size_t items = 0;
1085         
1086         /* don't include anything from this action if it is linked in from another file,
1087          * and we're getting stuff for editing...
1088          */
1089         // TODO: need a way of tagging other channels that may also be affected...
1090         if ((filter_mode & ANIMFILTER_FOREDIT) && (act->id.lib))
1091                 return 0;
1092                 
1093         /* do groups */
1094         // TODO: do nested groups?
1095         for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1096                 /* store reference to last channel of group */
1097                 if (agrp->channels.last) 
1098                         lastchan= agrp->channels.last;
1099                         
1100                 /* action group's channels */
1101                 items += animfilter_act_group(ac, anim_data, ads, act, agrp, filter_mode, owner_id);
1102         }
1103         
1104         /* un-grouped F-Curves (only if we're not only considering those channels in the active group) */
1105         if (!(filter_mode & ANIMFILTER_ACTGROUPED)) {
1106                 FCurve *firstfcu = (lastchan)? (lastchan->next) : (act->curves.first);
1107                 items += animfilter_fcurves(anim_data, ads, firstfcu, NULL, filter_mode, owner_id);
1108         }
1109         
1110         /* return the number of items added to the list */
1111         return items;
1112 }
1113
1114 /* Include NLA-Data for NLA-Editor:
1115  *      - when ANIMFILTER_LIST_CHANNELS is used, that means we should be filtering the list for display
1116  *        Although the evaluation order is from the first track to the last and then apply the Action on top,
1117  *        we present this in the UI as the Active Action followed by the last track to the first so that we 
1118  *        get the evaluation order presented as per a stack.
1119  *      - for normal filtering (i.e. for editing), we only need the NLA-tracks but they can be in 'normal' evaluation
1120  *        order, i.e. first to last. Otherwise, some tools may get screwed up.
1121  */
1122 static size_t animfilter_nla (bAnimContext *UNUSED(ac), ListBase *anim_data, bDopeSheet *ads, AnimData *adt, int filter_mode, ID *owner_id)
1123 {
1124         NlaTrack *nlt;
1125         NlaTrack *first=NULL, *next=NULL;
1126         size_t items = 0;
1127         
1128         /* if showing channels, include active action */
1129         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1130                 /* if NLA action-line filtering is off, don't show unless there are keyframes, 
1131                  * in order to keep things more compact for doing transforms
1132                  */
1133                 if (!(ads->filterflag & ADS_FILTER_NLA_NOACT) || (adt->action)) {
1134                         /* there isn't really anything editable here, so skip if need editable */
1135                         if ((filter_mode & ANIMFILTER_FOREDIT) == 0) { 
1136                                 /* just add the action track now (this MUST appear for drawing)
1137                                  *      - as AnimData may not have an action, we pass a dummy pointer just to get the list elem created, then
1138                                  *        overwrite this with the real value - REVIEW THIS...
1139                                  */
1140                                 ANIMCHANNEL_NEW_CHANNEL_FULL((void *)(&adt->action), ANIMTYPE_NLAACTION, owner_id, 
1141                                         {
1142                                                 ale->data= adt->action ? adt->action : NULL; 
1143                                         });
1144                         }
1145                 }
1146                 
1147                 /* first track to include will be the last one if we're filtering by channels */
1148                 first= adt->nla_tracks.last;
1149         }
1150         else {
1151                 /* first track to include will the the first one (as per normal) */
1152                 first= adt->nla_tracks.first;
1153         }
1154         
1155         /* loop over NLA Tracks - assume that the caller of this has already checked that these should be included */
1156         for (nlt= first; nlt; nlt= next) {
1157                 /* 'next' NLA-Track to use depends on whether we're filtering for drawing or not */
1158                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) 
1159                         next= nlt->prev;
1160                 else
1161                         next= nlt->next;
1162                 
1163                 /* if we're in NLA-tweakmode, don't show this track if it was disabled (due to tweaking) for now 
1164                  *      - active track should still get shown though (even though it has disabled flag set)
1165                  */
1166                 // FIXME: the channels after should still get drawn, just 'differently', and after an active-action channel
1167                 if ((adt->flag & ADT_NLA_EDIT_ON) && (nlt->flag & NLATRACK_DISABLED) && !(nlt->flag & NLATRACK_ACTIVE))
1168                         continue;
1169                 
1170                 /* only work with this channel and its subchannels if it is editable */
1171                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_NLT(nlt)) {
1172                         /* only include this track if selected in a way consistent with the filtering requirements */
1173                         if (ANIMCHANNEL_SELOK(SEL_NLT(nlt))) {
1174                                 /* only include if this track is active */
1175                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (nlt->flag & NLATRACK_ACTIVE)) {
1176                                         ANIMCHANNEL_NEW_CHANNEL(nlt, ANIMTYPE_NLATRACK, owner_id);
1177                                 }
1178                         }
1179                 }
1180         }
1181         
1182         /* return the number of items added to the list */
1183         return items;
1184 }
1185
1186 /* determine what animation data from AnimData block should get displayed */
1187 static size_t animfilter_block_data (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *id, int filter_mode)
1188 {
1189         AnimData *adt = BKE_animdata_from_id(id);
1190         size_t items = 0;
1191
1192         /* image object datablocks have no anim-data so check for NULL */
1193         if (adt) {
1194                 IdAdtTemplate *iat = (IdAdtTemplate*)id;
1195
1196                 /* NOTE: this macro is used instead of inlining the logic here, since this sort of filtering is still needed
1197                  * in a few places in he rest of the code still - notably for the few cases where special mode-based
1198                  * different types of data expanders are required.
1199                  */
1200                 ANIMDATA_FILTER_CASES(iat,
1201                         { /* AnimData */
1202                                 /* specifically filter animdata block */
1203                                 ANIMCHANNEL_NEW_CHANNEL(adt, ANIMTYPE_ANIMDATA, id);
1204                         },
1205                         { /* NLA */
1206                                 items += animfilter_nla(ac, anim_data, ads, adt, filter_mode, id);
1207                         },
1208                         { /* Drivers */
1209                                 items += animfilter_fcurves(anim_data, ads, adt->drivers.first, NULL, filter_mode, id);
1210                         },
1211                         { /* Keyframes */
1212                                 items += animfilter_action(ac, anim_data, ads, adt->action, filter_mode, id);
1213                         }
1214                 );
1215         }
1216
1217         return items;
1218 }
1219
1220
1221
1222 /* Include ShapeKey Data for ShapeKey Editor */
1223 static size_t animdata_filter_shapekey (bAnimContext *ac, ListBase *anim_data, Key *key, int filter_mode)
1224 {
1225         size_t items = 0;
1226         
1227         /* check if channels or only F-Curves */
1228         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1229                 KeyBlock *kb;
1230                 
1231                 /* loop through the channels adding ShapeKeys as appropriate */
1232                 for (kb= key->block.first; kb; kb= kb->next) {
1233                         /* skip the first one, since that's the non-animatable basis */
1234                         // XXX maybe in future this may become handy?
1235                         if (kb == key->block.first) continue;
1236                         
1237                         /* only work with this channel and its subchannels if it is editable */
1238                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_SHAPEKEY(kb)) {
1239                                 /* only include this track if selected in a way consistent with the filtering requirements */
1240                                 if ( ANIMCHANNEL_SELOK(SEL_SHAPEKEY(kb)) ) {
1241                                         // TODO: consider 'active' too?
1242                                         
1243                                         /* owner-id here must be key so that the F-Curve can be resolved... */
1244                                         ANIMCHANNEL_NEW_CHANNEL(kb, ANIMTYPE_SHAPEKEY, key);
1245                                 }
1246                         }
1247                 }
1248         }
1249         else {
1250                 /* just use the action associated with the shapekey */
1251                 // TODO: somehow manage to pass dopesheet info down here too?
1252                 if (key->adt) {
1253                         if (filter_mode & ANIMFILTER_ANIMDATA) {
1254                                 ANIMCHANNEL_NEW_CHANNEL(key->adt, ANIMTYPE_ANIMDATA, key);
1255                         }
1256                         else if (key->adt->action) {
1257                                 items= animfilter_action(ac, anim_data, NULL, key->adt->action, filter_mode, (ID *)key);
1258                         }
1259                 }
1260         }
1261         
1262         /* return the number of items added to the list */
1263         return items;
1264 }
1265
1266 static size_t animdata_filter_gpencil_data (ListBase *anim_data, bGPdata *gpd, int filter_mode)
1267 {
1268         bGPDlayer *gpl;
1269         size_t items = 0;
1270         
1271         /* loop over layers as the conditions are acceptable */
1272         for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
1273                 /* only if selected */
1274                 if ( ANIMCHANNEL_SELOK(SEL_GPL(gpl)) ) {
1275                         /* only if editable */
1276                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
1277                                 /* active... */
1278                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (gpl->flag & GP_LAYER_ACTIVE)) {
1279                                         /* add to list */
1280                                         ANIMCHANNEL_NEW_CHANNEL(gpl, ANIMTYPE_GPLAYER, gpd);
1281                                 }
1282                         }
1283                 }
1284         }
1285         
1286         return items;
1287 }
1288
1289 /* Grab all Grease Pencil datablocks in file */
1290 // TODO: should this be amalgamated with the dopesheet filtering code?
1291 static size_t animdata_filter_gpencil (ListBase *anim_data, void *UNUSED(data), int filter_mode)
1292 {
1293         bGPdata *gpd;
1294         size_t items = 0;
1295         
1296         /* for now, grab grease pencil datablocks directly from main */
1297         // XXX: this is not good...
1298         for (gpd = G.main->gpencil.first; gpd; gpd = gpd->id.next) {
1299                 ListBase tmp_data = {NULL, NULL};
1300                 size_t tmp_items = 0;
1301                 
1302                 /* only show if gpd is used by something... */
1303                 if (ID_REAL_USERS(gpd) < 1)
1304                         continue;
1305                         
1306                 /* add gpencil animation channels */
1307                 BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_GPD(gpd))
1308                 {
1309                         tmp_items += animdata_filter_gpencil_data(&tmp_data, gpd, filter_mode);
1310                 }
1311                 END_ANIMFILTER_SUBCHANNELS;
1312                 
1313                 /* did we find anything? */
1314                 if (tmp_items) {
1315                         /* include data-expand widget first */
1316                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1317                                 /* add gpd as channel too (if for drawing, and it has layers) */
1318                                 ANIMCHANNEL_NEW_CHANNEL(gpd, ANIMTYPE_GPDATABLOCK, NULL);
1319                         }
1320                         
1321                         /* now add the list of collected channels */
1322                         BLI_movelisttolist(anim_data, &tmp_data);
1323                         BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1324                         items += tmp_items;
1325                 }
1326         }
1327         
1328         /* return the number of items added to the list */
1329         return items;
1330 }
1331
1332 /* NOTE: owner_id is scene, material, or texture block, which is the direct owner of the node tree in question */
1333 // TODO: how to handle group nodes is still unclear...
1334 static size_t animdata_filter_ds_nodetree (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *owner_id, bNodeTree *ntree, int filter_mode)
1335 {
1336         ListBase tmp_data = {NULL, NULL};
1337         size_t tmp_items = 0;
1338         size_t items = 0;
1339         
1340         /* add nodetree animation channels */
1341         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_NTREE_DATA(ntree))
1342         {
1343                 /* animation data filtering */
1344                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)ntree, filter_mode);
1345         }
1346         END_ANIMFILTER_SUBCHANNELS;
1347         
1348         /* did we find anything? */
1349         if (tmp_items) {
1350                 /* include data-expand widget first */
1351                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1352                         /* check if filtering by active status */
1353                         if (ANIMCHANNEL_ACTIVEOK(ntree)) {
1354                                 ANIMCHANNEL_NEW_CHANNEL(ntree, ANIMTYPE_DSNTREE, owner_id);
1355                         }
1356                 }
1357                 
1358                 /* now add the list of collected channels */
1359                 BLI_movelisttolist(anim_data, &tmp_data);
1360                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1361                 items += tmp_items;
1362         }
1363         
1364         /* return the number of items added to the list */
1365         return items;
1366 }
1367
1368 /* NOTE: owner_id is either material, lamp, or world block, which is the direct owner of the texture stack in question */
1369 static size_t animdata_filter_ds_textures (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *owner_id, int filter_mode)
1370 {
1371         MTex **mtex = NULL;
1372         size_t items=0;
1373         int a=0;
1374         
1375         /* get datatype specific data first */
1376         if (owner_id == NULL)
1377                 return 0;
1378         
1379         switch (GS(owner_id->name)) {
1380                 case ID_MA:
1381                 {
1382                         Material *ma= (Material *)owner_id;
1383                         mtex= (MTex**)(&ma->mtex);
1384                 }
1385                         break;
1386                 case ID_LA:
1387                 {
1388                         Lamp *la= (Lamp *)owner_id;
1389                         mtex= (MTex**)(&la->mtex);
1390                 }
1391                         break;
1392                 case ID_WO:
1393                 {
1394                         World *wo= (World *)owner_id;
1395                         mtex= (MTex**)(&wo->mtex);
1396                 }
1397                         break;
1398                 default: 
1399                 {
1400                         /* invalid/unsupported option */
1401                         if (G.debug & G_DEBUG)
1402                                 printf("ERROR: unsupported owner_id (i.e. texture stack) for filter textures - %s\n", owner_id->name);
1403                         return 0;
1404                 }
1405         }
1406         
1407         /* firstly check that we actuallly have some textures, by gathering all textures in a temp list */
1408         for (a=0; a < MAX_MTEX; a++) {
1409                 Tex *tex= (mtex[a]) ? mtex[a]->tex : NULL;
1410                 ListBase tmp_data = {NULL, NULL};
1411                 size_t tmp_items = 0;
1412                 
1413                 /* for now, if no texture returned, skip (this shouldn't confuse the user I hope) */
1414                 if (tex == NULL) 
1415                         continue;
1416                 
1417                 /* add texture's animation data to temp collection */
1418                 BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_TEX_DATA(tex)) 
1419                 {
1420                         /* texture animdata */
1421                         tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)tex, filter_mode);
1422                         
1423                         /* nodes */
1424                         if ((tex->nodetree) && !(ads->filterflag & ADS_FILTER_NONTREE)) {
1425                                 /* owner_id as id instead of texture, since it'll otherwise be impossible to track the depth */
1426                                 // FIXME: perhaps as a result, textures should NOT be included under materials, but under their own section instead
1427                                 // so that free-floating textures can also be animated
1428                                 tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)tex, tex->nodetree, filter_mode);
1429                         }
1430                 }
1431                 END_ANIMFILTER_SUBCHANNELS;
1432                 
1433                 /* did we find anything? */
1434                 if (tmp_items) {
1435                         /* include texture-expand widget? */
1436                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1437                                 /* check if filtering by active status */
1438                                 if (ANIMCHANNEL_ACTIVEOK(tex)) {
1439                                         ANIMCHANNEL_NEW_CHANNEL(tex, ANIMTYPE_DSTEX, owner_id);
1440                                 }
1441                         }
1442                         
1443                         /* now add the list of collected channels */
1444                         BLI_movelisttolist(anim_data, &tmp_data);
1445                         BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1446                         items += tmp_items;
1447                 }
1448         }
1449         
1450         /* return the number of items added to the list */
1451         return items;
1452 }
1453
1454 static size_t animdata_filter_ds_materials (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
1455 {
1456         size_t items=0;
1457         int a=0;
1458         
1459         /* firstly check that we actuallly have some materials, by gathering all materials in a temp list */
1460         for (a=1; a <= ob->totcol; a++) {
1461                 Material *ma= give_current_material(ob, a);
1462                 ListBase tmp_data = {NULL, NULL};
1463                 size_t tmp_items = 0;
1464                 
1465                 /* if no material returned, skip - so that we don't get weird blank entries... */
1466                 if (ma == NULL) continue;
1467                 
1468                 /* add material's animation data to temp collection */
1469                 BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_MAT_OBJD(ma))
1470                 {
1471                         /* material's animation data */
1472                         tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)ma, filter_mode);
1473                                 
1474                         /* textures */
1475                         if (!(ads->filterflag & ADS_FILTER_NOTEX))
1476                                 tmp_items += animdata_filter_ds_textures(ac, &tmp_data, ads, (ID *)ma, filter_mode);
1477                                 
1478                         /* nodes */
1479                         if ((ma->nodetree) && !(ads->filterflag & ADS_FILTER_NONTREE)) 
1480                                 tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)ma, ma->nodetree, filter_mode);
1481                 }
1482                 END_ANIMFILTER_SUBCHANNELS;
1483                 
1484                 /* did we find anything? */
1485                 if (tmp_items) {
1486                         /* include material-expand widget first */
1487                         // hmm... do we need to store the index of this material in the array anywhere?
1488                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1489                                 /* check if filtering by active status */
1490                                 if (ANIMCHANNEL_ACTIVEOK(ma)) {
1491                                         ANIMCHANNEL_NEW_CHANNEL(ma, ANIMTYPE_DSMAT, ma);
1492                                 }
1493                         }
1494                         
1495                         /* now add the list of collected channels */
1496                         BLI_movelisttolist(anim_data, &tmp_data);
1497                         BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1498                         items += tmp_items;
1499                 }
1500         }
1501         
1502         /* return the number of items added to the list */
1503         return items;
1504 }
1505
1506 static size_t animdata_filter_ds_particles (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
1507 {
1508         ParticleSystem *psys;
1509         size_t items= 0;
1510
1511         for (psys = ob->particlesystem.first; psys; psys=psys->next) {
1512                 ListBase tmp_data = {NULL, NULL};
1513                 size_t tmp_items = 0;
1514                 
1515                 /* if no material returned, skip - so that we don't get weird blank entries... */
1516                 if (ELEM(NULL, psys->part, psys->part->adt))
1517                         continue;
1518                 
1519                 /* add particle-system's animation data to temp collection */
1520                 BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_PART_OBJD(psys->part))
1521                 {
1522                         /* material's animation data */
1523                         tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)psys->part, filter_mode);
1524                 }
1525                 END_ANIMFILTER_SUBCHANNELS;
1526                 
1527                 /* did we find anything? */
1528                 if (tmp_items) {
1529                         /* include particle-expand widget first */
1530                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1531                                 /* check if filtering by active status */
1532                                 if (ANIMCHANNEL_ACTIVEOK(psys->part)) {
1533                                         ANIMCHANNEL_NEW_CHANNEL(psys->part, ANIMTYPE_DSPART, psys->part);
1534                                 }
1535                         }
1536                         
1537                         /* now add the list of collected channels */
1538                         BLI_movelisttolist(anim_data, &tmp_data);
1539                         BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1540                         items += tmp_items;
1541                 }
1542         }
1543         
1544         /* return the number of items added to the list */
1545         return items;
1546 }
1547
1548 static size_t animdata_filter_ds_obdata (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
1549 {
1550         ListBase tmp_data = {NULL, NULL};
1551         size_t tmp_items = 0;
1552         size_t items= 0;
1553         
1554         IdAdtTemplate *iat= ob->data;
1555         short type=0, expanded=0;
1556         
1557         /* get settings based on data type */
1558         switch (ob->type) {
1559                 case OB_CAMERA: /* ------- Camera ------------ */
1560                 {
1561                         Camera *ca= (Camera *)ob->data;
1562                         
1563                         if (ads->filterflag & ADS_FILTER_NOCAM)
1564                                 return 0;
1565                         
1566                         type= ANIMTYPE_DSCAM;
1567                         expanded= FILTER_CAM_OBJD(ca);
1568                 }
1569                         break;
1570                 case OB_LAMP: /* ---------- Lamp ----------- */
1571                 {
1572                         Lamp *la= (Lamp *)ob->data;
1573                         
1574                         if (ads->filterflag & ADS_FILTER_NOLAM)
1575                                 return 0;
1576                         
1577                         type= ANIMTYPE_DSLAM;
1578                         expanded= FILTER_LAM_OBJD(la);
1579                 }
1580                         break;
1581                 case OB_CURVE: /* ------- Curve ---------- */
1582                 case OB_SURF: /* ------- Nurbs Surface ---------- */
1583                 case OB_FONT: /* ------- Text Curve ---------- */
1584                 {
1585                         Curve *cu= (Curve *)ob->data;
1586                         
1587                         if (ads->filterflag & ADS_FILTER_NOCUR)
1588                                 return 0;
1589                         
1590                         type= ANIMTYPE_DSCUR;
1591                         expanded= FILTER_CUR_OBJD(cu);
1592                 }
1593                         break;
1594                 case OB_MBALL: /* ------- MetaBall ---------- */
1595                 {
1596                         MetaBall *mb= (MetaBall *)ob->data;
1597                         
1598                         if (ads->filterflag & ADS_FILTER_NOMBA)
1599                                 return 0;
1600                         
1601                         type= ANIMTYPE_DSMBALL;
1602                         expanded= FILTER_MBALL_OBJD(mb);
1603                 }
1604                         break;
1605                 case OB_ARMATURE: /* ------- Armature ---------- */
1606                 {
1607                         bArmature *arm= (bArmature *)ob->data;
1608                         
1609                         if (ads->filterflag & ADS_FILTER_NOARM)
1610                                 return 0;
1611                         
1612                         type= ANIMTYPE_DSARM;
1613                         expanded= FILTER_ARM_OBJD(arm);
1614                 }
1615                         break;
1616                 case OB_MESH: /* ------- Mesh ---------- */
1617                 {
1618                         Mesh *me= (Mesh *)ob->data;
1619                         
1620                         if (ads->filterflag & ADS_FILTER_NOMESH)
1621                                 return 0;
1622                         
1623                         type= ANIMTYPE_DSMESH;
1624                         expanded= FILTER_MESH_OBJD(me);
1625                 }
1626                         break;
1627                 case OB_LATTICE: /* ---- Lattice ---- */
1628                 {
1629                         Lattice *lt = (Lattice *)ob->data;
1630                         
1631                         if (ads->filterflag & ADS_FILTER_NOLAT)
1632                                 return 0;
1633                         
1634                         type= ANIMTYPE_DSLAT;
1635                         expanded= FILTER_LATTICE_OBJD(lt);
1636                 }
1637                         break;
1638                 case OB_SPEAKER: /* ---------- Speaker ----------- */
1639                 {
1640                         Speaker *spk= (Speaker *)ob->data;
1641                         
1642                         type= ANIMTYPE_DSSPK;
1643                         expanded= FILTER_SPK_OBJD(spk);
1644                 }
1645                         break;
1646         }
1647         
1648         /* add object data animation channels */
1649         BEGIN_ANIMFILTER_SUBCHANNELS(expanded)
1650         {
1651                 /* animation data filtering */
1652                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)iat, filter_mode);
1653                 
1654                 /* sub-data filtering... */
1655                 switch (ob->type) {
1656                         case OB_LAMP:   /* lamp - textures */
1657                         {
1658                                 /* textures */
1659                                 if (!(ads->filterflag & ADS_FILTER_NOTEX))
1660                                         tmp_items += animdata_filter_ds_textures(ac, &tmp_data, ads, ob->data, filter_mode);
1661                         }
1662                                 break;
1663                 }
1664         }
1665         END_ANIMFILTER_SUBCHANNELS;
1666         
1667         /* did we find anything? */
1668         if (tmp_items) {
1669                 /* include data-expand widget first */
1670                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1671                         /* check if filtering by active status */
1672                         if (ANIMCHANNEL_ACTIVEOK(iat)) {
1673                                 ANIMCHANNEL_NEW_CHANNEL(iat, type, iat);
1674                         }
1675                 }
1676                 
1677                 /* now add the list of collected channels */
1678                 BLI_movelisttolist(anim_data, &tmp_data);
1679                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1680                 items += tmp_items;
1681         }
1682         
1683         /* return the number of items added to the list */
1684         return items;
1685 }
1686
1687 /* shapekey-level animation */
1688 static size_t animdata_filter_ds_keyanim (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, Key *key, int filter_mode)
1689 {
1690         ListBase tmp_data = {NULL, NULL};
1691         size_t tmp_items = 0;
1692         size_t items = 0;
1693         
1694         /* add shapekey-level animation channels */
1695         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_SKE_OBJD(key))
1696         {
1697                 /* animation data filtering */
1698                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)key, filter_mode);
1699         }
1700         END_ANIMFILTER_SUBCHANNELS;
1701         
1702         /* did we find anything? */
1703         if (tmp_items) {
1704                 /* include key-expand widget first */
1705                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1706                         if (ANIMCHANNEL_ACTIVEOK(key)) {
1707                                 ANIMCHANNEL_NEW_CHANNEL(key, ANIMTYPE_DSSKEY, ob);
1708                         }
1709                 }
1710                 
1711                 /* now add the list of collected channels */
1712                 BLI_movelisttolist(anim_data, &tmp_data);
1713                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1714                 items += tmp_items;
1715         }
1716         
1717         /* return the number of items added to the list */
1718         return items;
1719 }
1720
1721 /* object-level animation */
1722 static size_t animdata_filter_ds_obanim (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
1723 {
1724         ListBase tmp_data = {NULL, NULL};
1725         size_t tmp_items = 0;
1726         size_t items = 0;
1727         
1728         AnimData *adt = ob->adt;
1729         short type=0, expanded=1;
1730         void *cdata = NULL;
1731         
1732         /* determine the type of expander channels to use */
1733         // this is the best way to do this for now...
1734         ANIMDATA_FILTER_CASES(ob,
1735                 {/* AnimData - no channel, but consider data */},
1736                 {/* NLA - no channel, but consider data */},
1737                 {/* Drivers */
1738                         type = ANIMTYPE_FILLDRIVERS;
1739                         cdata = adt;
1740                         expanded = EXPANDED_DRVD(adt);
1741                 },
1742                 {/* Keyframes */
1743                         type = ANIMTYPE_FILLACTD;
1744                         cdata = adt->action;
1745                         expanded = EXPANDED_ACTC(adt->action);
1746                 });
1747                 
1748         /* add object-level animation channels */
1749         BEGIN_ANIMFILTER_SUBCHANNELS(expanded)
1750         {
1751                 /* animation data filtering */
1752                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)ob, filter_mode);
1753         }
1754         END_ANIMFILTER_SUBCHANNELS;
1755         
1756         /* did we find anything? */
1757         if (tmp_items) {
1758                 /* include anim-expand widget first */
1759                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1760                         if (type != ANIMTYPE_NONE) {
1761                                 /* NOTE: active-status (and the associated checks) don't apply here... */
1762                                 ANIMCHANNEL_NEW_CHANNEL(cdata, type, ob);
1763                         }
1764                 }
1765                 
1766                 /* now add the list of collected channels */
1767                 BLI_movelisttolist(anim_data, &tmp_data);
1768                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1769                 items += tmp_items;
1770         }
1771         
1772         /* return the number of items added to the list */
1773         return items;
1774 }
1775
1776 /* get animation channels from object2 */
1777 static size_t animdata_filter_dopesheet_ob (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1778 {
1779         ListBase tmp_data = {NULL, NULL};
1780         Object *ob= base->object;
1781         size_t tmp_items = 0;
1782         size_t items = 0;
1783         
1784         /* filter data contained under object first */
1785         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_OBJC(ob))
1786         {
1787                 Key *key= ob_get_key(ob);
1788                 
1789                 /* object-level animation */
1790                 if ((ob->adt) && !(ads->filterflag & ADS_FILTER_NOOBJ)) {
1791                         tmp_items += animdata_filter_ds_obanim(ac, &tmp_data, ads, ob, filter_mode);
1792                 }
1793                 
1794                 /* shape-key */
1795                 if ((key && key->adt) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
1796                         tmp_items += animdata_filter_ds_keyanim(ac, &tmp_data, ads, ob, key, filter_mode);
1797                 }
1798                 
1799                 /* materials */
1800                 if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT)) {
1801                         tmp_items += animdata_filter_ds_materials(ac, &tmp_data, ads, ob, filter_mode);
1802                 }
1803                 
1804                 /* object data */
1805                 if (ob->data) {
1806                         tmp_items += animdata_filter_ds_obdata(ac, &tmp_data, ads, ob, filter_mode);
1807                 }
1808                 
1809                 /* particles */
1810                 if ((ob->particlesystem.first) && !(ads->filterflag & ADS_FILTER_NOPART)) {
1811                         tmp_items += animdata_filter_ds_particles(ac, &tmp_data, ads, ob, filter_mode);
1812                 }
1813         }
1814         END_ANIMFILTER_SUBCHANNELS;
1815         
1816         
1817         /* if we collected some channels, add these to the new list... */
1818         if (tmp_items) {
1819                 /* firstly add object expander if required */
1820                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1821                         /* check if filtering by selection */
1822                         // XXX: double-check on this - most of the time, a lot of tools need to filter out these channels!
1823                         if (ANIMCHANNEL_SELOK((base->flag & SELECT))) {
1824                                 /* check if filtering by active status */
1825                                 if (ANIMCHANNEL_ACTIVEOK(ob)) {
1826                                         ANIMCHANNEL_NEW_CHANNEL(base, ANIMTYPE_OBJECT, ob);
1827                                 }
1828                         }
1829                 }
1830                 
1831                 /* now add the list of collected channels */
1832                 BLI_movelisttolist(anim_data, &tmp_data);
1833                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1834                 items += tmp_items;
1835         }
1836         
1837         /* return the number of items added */
1838         return items;
1839 }
1840
1841 static size_t animdata_filter_ds_world (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, World *wo, int filter_mode)
1842 {
1843         ListBase tmp_data = {NULL, NULL};
1844         size_t tmp_items = 0;
1845         size_t items = 0;
1846         
1847         /* add world animation channels */
1848         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_WOR_SCED(wo))
1849         {
1850                 /* animation data filtering */
1851                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)wo, filter_mode);
1852                 
1853                 /* textures for world */
1854                 if (!(ads->filterflag & ADS_FILTER_NOTEX))
1855                         items += animdata_filter_ds_textures(ac, &tmp_data, ads, (ID *)wo, filter_mode);
1856                         
1857                 /* nodes */
1858                 if ((wo->nodetree) && !(ads->filterflag & ADS_FILTER_NONTREE)) 
1859                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)wo, wo->nodetree, filter_mode);
1860         }
1861         END_ANIMFILTER_SUBCHANNELS;
1862         
1863         /* did we find anything? */
1864         if (tmp_items) {
1865                 /* include data-expand widget first */
1866                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1867                         /* check if filtering by active status */
1868                         if (ANIMCHANNEL_ACTIVEOK(wo)) {
1869                                 ANIMCHANNEL_NEW_CHANNEL(wo, ANIMTYPE_DSWOR, sce);
1870                         }
1871                 }
1872                 
1873                 /* now add the list of collected channels */
1874                 BLI_movelisttolist(anim_data, &tmp_data);
1875                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1876                 items += tmp_items;
1877         }
1878         
1879         /* return the number of items added to the list */
1880         return items;
1881 }
1882
1883 static size_t animdata_filter_ds_scene (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
1884 {
1885         ListBase tmp_data = {NULL, NULL};
1886         size_t tmp_items = 0;
1887         size_t items = 0;
1888         
1889         AnimData *adt = sce->adt;
1890         short type=0, expanded=1;
1891         void *cdata = NULL;
1892         
1893         /* determine the type of expander channels to use */
1894         // this is the best way to do this for now...
1895         ANIMDATA_FILTER_CASES(sce,
1896                 {/* AnimData - no channel, but consider data */},
1897                 {/* NLA - no channel, but consider data */},
1898                 {/* Drivers */
1899                         type = ANIMTYPE_FILLDRIVERS;
1900                         cdata = adt;
1901                         expanded = EXPANDED_DRVD(adt);
1902                 },
1903                 {/* Keyframes */
1904                         type = ANIMTYPE_FILLACTD;
1905                         cdata = adt->action;
1906                         expanded = EXPANDED_ACTC(adt->action);
1907                 });
1908                 
1909         /* add scene-level animation channels */
1910         BEGIN_ANIMFILTER_SUBCHANNELS(expanded)
1911         {
1912                 /* animation data filtering */
1913                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)sce, filter_mode);
1914         }
1915         END_ANIMFILTER_SUBCHANNELS;
1916         
1917         /* did we find anything? */
1918         if (tmp_items) {
1919                 /* include anim-expand widget first */
1920                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1921                         if (type != ANIMTYPE_NONE) {
1922                                 /* NOTE: active-status (and the associated checks) don't apply here... */
1923                                 ANIMCHANNEL_NEW_CHANNEL(cdata, type, sce);
1924                         }
1925                 }
1926                 
1927                 /* now add the list of collected channels */
1928                 BLI_movelisttolist(anim_data, &tmp_data);
1929                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1930                 items += tmp_items;
1931         }
1932         
1933         /* return the number of items added to the list */
1934         return items;
1935 }
1936
1937 static size_t animdata_filter_dopesheet_scene (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
1938 {
1939         ListBase tmp_data = {NULL, NULL};
1940         size_t tmp_items = 0;
1941         size_t items = 0;
1942         
1943         /* filter data contained under object first */
1944         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_SCEC(sce))
1945         {
1946                 bNodeTree *ntree= sce->nodetree;
1947                 World *wo= sce->world;
1948                 
1949                 /* Action, Drivers, or NLA for Scene */
1950                 if ((ads->filterflag & ADS_FILTER_NOSCE) == 0) {
1951                         tmp_items += animdata_filter_ds_scene(ac, &tmp_data, ads, sce, filter_mode);
1952                 }
1953                 
1954                 /* world */
1955                 if ((wo) && !(ads->filterflag & ADS_FILTER_NOWOR)) {
1956                         tmp_items += animdata_filter_ds_world(ac, &tmp_data, ads, sce, wo, filter_mode);
1957                 }
1958                 
1959                 /* nodetree */
1960                 if ((ntree) && !(ads->filterflag & ADS_FILTER_NONTREE)) {
1961                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)sce, ntree, filter_mode);
1962                 }
1963                 
1964                 // TODO: one day, when sequencer becomes its own datatype, perhaps it should be included here
1965         }
1966         END_ANIMFILTER_SUBCHANNELS;
1967         
1968         /* if we collected some channels, add these to the new list... */
1969         if (tmp_items) {
1970                 /* firstly add object expander if required */
1971                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1972                         /* check if filtering by selection */
1973                         if (ANIMCHANNEL_SELOK((sce->flag & SCE_DS_SELECTED))) {
1974                                 /* NOTE: active-status doesn't matter for this! */
1975                                 ANIMCHANNEL_NEW_CHANNEL(sce, ANIMTYPE_SCENE, sce);
1976                         }
1977                 }
1978                 
1979                 /* now add the list of collected channels */
1980                 BLI_movelisttolist(anim_data, &tmp_data);
1981                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1982                 items += tmp_items;
1983         }
1984         
1985         /* return the number of items added */
1986         return items;
1987 }
1988
1989 // 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)
1990 static size_t animdata_filter_dopesheet (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, int filter_mode)
1991 {
1992         Scene *sce= (Scene *)ads->source;
1993         Base *base;
1994         size_t items = 0;
1995         
1996         /* check that we do indeed have a scene */
1997         if ((ads->source == NULL) || (GS(ads->source->name)!=ID_SCE)) {
1998                 printf("DopeSheet Error: Not scene!\n");
1999                 if (G.debug & G_DEBUG)
2000                         printf("\tPointer = %p, Name = '%s'\n", (void *)ads->source, (ads->source)?ads->source->name:NULL);
2001                 return 0;
2002         }
2003         
2004         /* augment the filter-flags with settings based on the dopesheet filterflags 
2005          * so that some temp settings can get added automagically...
2006          */
2007         if (ads->filterflag & ADS_FILTER_SELEDIT) {
2008                 /* only selected F-Curves should get their keyframes considered for editability */
2009                 filter_mode |= ANIMFILTER_SELEDIT;
2010         }
2011         
2012         /* scene-linked animation - e.g. world, compositing nodes, scene anim (including sequencer currently) */
2013         items += animdata_filter_dopesheet_scene(ac, anim_data, ads, sce, filter_mode);
2014         
2015         /* loop over all bases (i.e.objects) in the scene */
2016         for (base= sce->base.first; base; base= base->next) {
2017                 /* check if there's an object (all the relevant checks are done in the ob-function) */
2018                 if (base->object) {
2019                         Object *ob= base->object;
2020                         
2021                         /* firstly, check if object can be included, by the following factors:
2022                          *      - if only visible, must check for layer and also viewport visibility
2023                          *              --> while tools may demand only visible, user setting takes priority
2024                          *                      as user option controls whether sets of channels get included while
2025                          *                      tool-flag takes into account collapsed/open channels too
2026                          *      - if only selected, must check if object is selected 
2027                          *      - there must be animation data to edit (this is done recursively as we 
2028                          *        try to add the channels)
2029                          */
2030                         if ((filter_mode & ANIMFILTER_DATA_VISIBLE) && !(ads->filterflag & ADS_FILTER_INCL_HIDDEN)) {
2031                                 /* layer visibility - we check both object and base, since these may not be in sync yet */
2032                                 if ((sce->lay & (ob->lay|base->lay))==0) continue;
2033                                 
2034                                 /* outliner restrict-flag */
2035                                 if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
2036                         }
2037                         
2038                         /* if only F-Curves with visible flags set can be shown, check that 
2039                          * datablock hasn't been set to invisible 
2040                          */
2041                         if (filter_mode & ANIMFILTER_CURVE_VISIBLE) {
2042                                 if ((ob->adt) && (ob->adt->flag & ADT_CURVES_NOT_VISIBLE))
2043                                         continue;
2044                         }
2045                         
2046                         /* check selection and object type filters */
2047                         if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) /*|| (base == sce->basact)*/) ) {
2048                                 /* only selected should be shown */
2049                                 continue;
2050                         }
2051                         
2052                         /* check if object belongs to the filtering group if option to filter 
2053                          * objects by the grouped status is on
2054                          *      - used to ease the process of doing multiple-character choreographies
2055                          */
2056                         if (ads->filterflag & ADS_FILTER_ONLYOBGROUP) {
2057                                 if (object_in_group(ob, ads->filter_grp) == 0)
2058                                         continue;
2059                         }
2060                                 
2061                         /* since we're still here, this object should be usable */
2062                         items += animdata_filter_dopesheet_ob(ac, anim_data, ads, base, filter_mode);
2063                 }
2064         }
2065         
2066         /* return the number of items in the list */
2067         return items;
2068 }
2069
2070 /* Summary track for DopeSheet/Action Editor 
2071  *      - return code is whether the summary lets the other channels get drawn
2072  */
2073 static short animdata_filter_dopesheet_summary (bAnimContext *ac, ListBase *anim_data, int filter_mode, size_t *items)
2074 {
2075         bDopeSheet *ads = NULL;
2076         
2077         /* get the DopeSheet information to use 
2078          *      - we should only need to deal with the DopeSheet/Action Editor, 
2079          *        since all the other Animation Editors won't have this concept
2080          *        being applicable.
2081          */
2082         if ((ac && ac->sl) && (ac->spacetype == SPACE_ACTION)) {
2083                 SpaceAction *saction= (SpaceAction *)ac->sl;
2084                 ads= &saction->ads;
2085         }
2086         else {
2087                 /* invalid space type - skip this summary channels */
2088                 return 1;
2089         }
2090         
2091         /* dopesheet summary 
2092          *      - only for drawing and/or selecting keyframes in channels, but not for real editing 
2093          *      - only useful for DopeSheet/Action/etc. editors where it is actually useful
2094          */
2095         if ((filter_mode & ANIMFILTER_LIST_CHANNELS) && (ads->filterflag & ADS_FILTER_SUMMARY)) {
2096                 bAnimListElem *ale= make_new_animlistelem(ac, ANIMTYPE_SUMMARY, NULL);
2097                 if (ale) {
2098                         BLI_addtail(anim_data, ale);
2099                         (*items)++;
2100                 }
2101                 
2102                 /* if summary is collapsed, don't show other channels beneath this 
2103                  *      - this check is put inside the summary check so that it doesn't interfere with normal operation
2104                  */ 
2105                 if (ads->flag & ADS_FLAG_SUMMARY_COLLAPSED)
2106                         return 0;
2107         }
2108         
2109         /* the other channels beneath this can be shown */
2110         return 1;
2111 }  
2112
2113 /* ......................... */
2114
2115 /* filter data associated with a channel - usually for handling summary-channels in DopeSheet */
2116 static size_t animdata_filter_animchan (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, bAnimListElem *channel, int filter_mode)
2117 {
2118         size_t items = 0;
2119         
2120         /* data to filter depends on channel type */
2121         // XXX: only common channel-types have been handled for now
2122         switch (channel->type) {
2123                 case ANIMTYPE_SUMMARY:
2124                         items += animdata_filter_dopesheet(ac, anim_data, ads, filter_mode);
2125                         break;
2126                         
2127                 case ANIMTYPE_SCENE:
2128                         items += animdata_filter_dopesheet_scene(ac, anim_data, ads, channel->data, filter_mode);
2129                         break;
2130                 
2131                 case ANIMTYPE_OBJECT:
2132                         items += animdata_filter_dopesheet_ob(ac, anim_data, ads, channel->data, filter_mode);
2133                         break;
2134         }
2135         
2136         return items;
2137 }
2138
2139 /* ----------- Cleanup API --------------- */
2140
2141 /* Remove entries with invalid types in animation channel list */
2142 static size_t animdata_filter_remove_invalid (ListBase *anim_data)
2143 {
2144         bAnimListElem *ale, *next;
2145         size_t items = 0;
2146         
2147         /* only keep entries with valid types */
2148         for (ale= anim_data->first; ale; ale= next) {
2149                 next= ale->next;
2150                 
2151                 if (ale->type == ANIMTYPE_NONE)
2152                         BLI_freelinkN(anim_data, ale);
2153                 else
2154                         items++;
2155         }
2156         
2157         return items;
2158 }
2159
2160 /* Remove duplicate entries in animation channel list */
2161 static size_t animdata_filter_remove_duplis (ListBase *anim_data)
2162 {
2163         bAnimListElem *ale, *next;
2164         GHash *gh;
2165         size_t items = 0;
2166         
2167         /* build new hashtable to efficiently store and retrieve which entries have been 
2168          * encountered already while searching
2169          */
2170         gh= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "animdata_filter_duplis_remove gh");
2171         
2172         /* loop through items, removing them from the list if a similar item occurs already */
2173         for (ale = anim_data->first; ale; ale = next) {
2174                 next = ale->next;
2175                 
2176                 /* check if hash has any record of an entry like this 
2177                  *      - just use ale->data for now, though it would be nicer to involve 
2178                  *        ale->type in combination too to capture corner cases (where same data performs differently)
2179                  */
2180                 if (BLI_ghash_haskey(gh, ale->data) == 0) {
2181                         /* this entry is 'unique' and can be kept */
2182                         BLI_ghash_insert(gh, ale->data, NULL);
2183                         items++;
2184                 }
2185                 else {
2186                         /* this entry isn't needed anymore */
2187                         BLI_freelinkN(anim_data, ale);
2188                 }
2189         }
2190         
2191         /* free the hash... */
2192         BLI_ghash_free(gh, NULL, NULL);
2193         
2194         /* return the number of items still in the list */
2195         return items;
2196 }
2197
2198 /* ----------- Public API --------------- */
2199
2200 /* This function filters the active data source to leave only animation channels suitable for
2201  * usage by the caller. It will return the length of the list 
2202  * 
2203  *      *anim_data: is a pointer to a ListBase, to which the filtered animation channels
2204  *              will be placed for use.
2205  *      filter_mode: how should the data be filtered - bitmapping accessed flags
2206  */
2207 size_t ANIM_animdata_filter (bAnimContext *ac, ListBase *anim_data, int filter_mode, void *data, short datatype)
2208 {
2209         size_t items = 0;
2210         
2211         /* only filter data if there's somewhere to put it */
2212         if (data && anim_data) {
2213                 
2214                 /* firstly filter the data */
2215                 switch (datatype) {
2216                         case ANIMCONT_ACTION:   /* 'Action Editor' */
2217                         {
2218                                 Object *obact= ac->obact;
2219                                 SpaceAction *saction = (SpaceAction *)ac->sl;
2220                                 bDopeSheet *ads = (saction)? &saction->ads : NULL;
2221                                 
2222                                 /* the check for the DopeSheet summary is included here since the summary works here too */
2223                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2224                                         items += animfilter_action(ac, anim_data, ads, data, filter_mode, (ID *)obact);
2225                         }
2226                                 break;
2227                                 
2228                         case ANIMCONT_SHAPEKEY: /* 'ShapeKey Editor' */
2229                         {
2230                                 /* the check for the DopeSheet summary is included here since the summary works here too */
2231                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2232                                         items= animdata_filter_shapekey(ac, anim_data, data, filter_mode);
2233                         }
2234                                 break;
2235                                 
2236                         case ANIMCONT_GPENCIL:
2237                         {
2238                                 items= animdata_filter_gpencil(anim_data, data, filter_mode);
2239                         }
2240                                 break;
2241                                 
2242                         case ANIMCONT_DOPESHEET: /* 'DopeSheet Editor' */
2243                         {
2244                                 /* the DopeSheet editor is the primary place where the DopeSheet summaries are useful */
2245                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2246                                         items += animdata_filter_dopesheet(ac, anim_data, data, filter_mode);
2247                         }
2248                                 break;
2249                                 
2250                         case ANIMCONT_FCURVES: /* Graph Editor -> FCurves/Animation Editing */
2251                         case ANIMCONT_DRIVERS: /* Graph Editor -> Drivers Editing */
2252                         case ANIMCONT_NLA: /* NLA Editor */
2253                         {
2254                                 /* all of these editors use the basic DopeSheet data for filtering options, but don't have all the same features */
2255                                 items = animdata_filter_dopesheet(ac, anim_data, data, filter_mode);
2256                         }
2257                                 break;
2258                         
2259                         case ANIMCONT_CHANNEL: /* animation channel */
2260                         {
2261                                 bDopeSheet *ads = ac->ads;
2262                                 
2263                                 /* based on the channel type, filter relevant data for this */
2264                                 items = animdata_filter_animchan(ac, anim_data, ads, data, filter_mode);
2265                         }
2266                                 break;
2267                 }
2268                         
2269                 /* remove any 'weedy' entries */
2270                 items = animdata_filter_remove_invalid(anim_data);
2271                 
2272                 /* remove duplicates (if required) */
2273                 if (filter_mode & ANIMFILTER_NODUPLIS)
2274                         items = animdata_filter_remove_duplis(anim_data);
2275         }
2276         
2277         /* return the number of items in the list */
2278         return items;
2279 }
2280
2281 /* ************************************************************ */