svn merge ^/trunk/blender -r46000:46100
[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 and/or 'Include Hidden' filtering
828  * NOTE: when this function returns true, the F-Curve is to be skipped 
829  */
830 static short skip_fcurve_selected_data (bDopeSheet *ads, FCurve *fcu, ID *owner_id, int filter_mode)
831 {
832         /* hidden items should be skipped if we only care about visible data, but we aren't interested in hidden stuff */
833         short skip_hidden = (filter_mode & ANIMFILTER_DATA_VISIBLE) && !(ads->filterflag & ADS_FILTER_INCL_HIDDEN);
834         
835         if (GS(owner_id->name) == ID_OB) {
836                 Object *ob= (Object *)owner_id;
837                 
838                 /* only consider if F-Curve involves pose.bones */
839                 if ((fcu->rna_path) && strstr(fcu->rna_path, "pose.bones")) {
840                         bPoseChannel *pchan;
841                         char *bone_name;
842                         
843                         /* get bone-name, and check if this bone is selected */
844                         bone_name= BLI_getQuotedStr(fcu->rna_path, "pose.bones[");
845                         pchan= get_pose_channel(ob->pose, bone_name);
846                         if (bone_name) MEM_freeN(bone_name);
847                         
848                         /* check whether to continue or skip */
849                         if ((pchan) && (pchan->bone)) {
850                                 /* if only visible channels, skip if bone not visible unless user wants channels from hidden data too */
851                                 if (skip_hidden) {
852                                         bArmature *arm= (bArmature *)ob->data;
853                                         
854                                         /* skipping - not visible on currently visible layers */
855                                         if ((arm->layer & pchan->bone->layer) == 0)
856                                                 return 1;
857                                         /* skipping - is currently hidden */
858                                         if (pchan->bone->flag & BONE_HIDDEN_P)
859                                                 return 1;
860                                 }
861                                 
862                                 /* can only add this F-Curve if it is selected */
863                                 if (ads->filterflag & ADS_FILTER_ONLYSEL) {
864                                         if ((pchan->bone->flag & BONE_SELECTED) == 0)
865                                                 return 1;
866                                 }
867                         }
868                 }
869         }
870         else if (GS(owner_id->name) == ID_SCE) {
871                 Scene *scene = (Scene *)owner_id;
872                 
873                 /* only consider if F-Curve involves sequence_editor.sequences */
874                 if ((fcu->rna_path) && strstr(fcu->rna_path, "sequences_all")) {
875                         Editing *ed= seq_give_editing(scene, FALSE);
876                         Sequence *seq;
877                         char *seq_name;
878                         
879                         /* get strip name, and check if this strip is selected */
880                         seq_name= BLI_getQuotedStr(fcu->rna_path, "sequences_all[");
881                         seq = get_seq_by_name(ed->seqbasep, seq_name, FALSE);
882                         if (seq_name) MEM_freeN(seq_name);
883                         
884                         /* can only add this F-Curve if it is selected */
885                         if (ads->filterflag & ADS_FILTER_ONLYSEL) {
886                                 if ((seq == NULL) || (seq->flag & SELECT)==0)
887                                         return 1;
888                         }
889                 }
890         }
891         else if (GS(owner_id->name) == ID_NT) {
892                 bNodeTree *ntree = (bNodeTree *)owner_id;
893                 
894                 /* check for selected nodes */
895                 if ((fcu->rna_path) && strstr(fcu->rna_path, "nodes")) {
896                         bNode *node;
897                         char *node_name;
898                         
899                         /* get strip name, and check if this strip is selected */
900                         node_name= BLI_getQuotedStr(fcu->rna_path, "nodes[");
901                         node = nodeFindNodebyName(ntree, node_name);
902                         if (node_name) MEM_freeN(node_name);
903                         
904                         /* can only add this F-Curve if it is selected */
905                         if (ads->filterflag & ADS_FILTER_ONLYSEL) {
906                                 if ((node) && (node->flag & NODE_SELECT)==0)
907                                         return 1;
908                         }
909                 }
910         }
911         return 0;
912 }
913
914 /* (Display-)Name-based F-Curve filtering
915  * NOTE: when this function returns true, the F-Curve is to be skipped 
916  */
917 static short skip_fcurve_with_name (bDopeSheet *ads, FCurve *fcu, ID *owner_id)
918 {
919         bAnimListElem ale_dummy = {NULL};
920         bAnimChannelType *acf;
921         
922         /* create a dummy wrapper for the F-Curve */
923         ale_dummy.type = ANIMTYPE_FCURVE;
924         ale_dummy.id = owner_id;
925         ale_dummy.data = fcu;
926         
927         /* get type info for channel */
928         acf = ANIM_channel_get_typeinfo(&ale_dummy);
929         if (acf && acf->name) {
930                 char name[256]; /* hopefully this will be enough! */
931                 
932                 /* get name */
933                 acf->name(&ale_dummy, name);
934                 
935                 /* check for partial match with the match string, assuming case insensitive filtering 
936                  * if match, this channel shouldn't be ignored!
937                  */
938                 return BLI_strcasestr(name, ads->searchstr) == NULL;
939         }
940         
941         /* just let this go... */
942         return 1;
943 }
944
945 /* find the next F-Curve that is usable for inclusion */
946 static FCurve *animfilter_fcurve_next (bDopeSheet *ads, FCurve *first, bActionGroup *grp, int filter_mode, ID *owner_id)
947 {
948         FCurve *fcu = NULL;
949         
950         /* loop over F-Curves - assume that the caller of this has already checked that these should be included 
951          * NOTE: we need to check if the F-Curves belong to the same group, as this gets called for groups too...
952          */
953         for (fcu= first; ((fcu) && (fcu->grp==grp)); fcu= fcu->next) {
954                 /* special exception for Pose-Channel/Sequence-Strip/Node Based F-Curves:
955                  *      - the 'Only Selected' and 'Include Hidden' data filters should be applied to sub-ID data which
956                  *        can be independently selected/hidden, such as Pose-Channels, Sequence Strips, and Nodes.
957                  *        Since these checks were traditionally done as first check for objects, we do the same here
958                  *      - we currently use an 'approximate' method for getting these F-Curves that doesn't require
959                  *        carefully checking the entire path
960                  *      - this will also affect things like Drivers, and also works for Bone Constraints
961                  */
962                 if (ads && owner_id) {
963                         if ((ads->filterflag & ADS_FILTER_ONLYSEL) || (ads->filterflag & ADS_FILTER_INCL_HIDDEN)==0) {
964                                 if (skip_fcurve_selected_data(ads, fcu, owner_id, filter_mode))
965                                         continue;
966                         }
967                 }       
968                 
969                 /* only include if visible (Graph Editor check, not channels check) */
970                 if (!(filter_mode & ANIMFILTER_CURVE_VISIBLE) || (fcu->flag & FCURVE_VISIBLE)) {
971                         /* only work with this channel and its subchannels if it is editable */
972                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_FCU(fcu)) {
973                                 /* only include this curve if selected in a way consistent with the filtering requirements */
974                                 if (ANIMCHANNEL_SELOK(SEL_FCU(fcu)) && ANIMCHANNEL_SELEDITOK(SEL_FCU(fcu))) {
975                                         /* only include if this curve is active */
976                                         if (!(filter_mode & ANIMFILTER_ACTIVE) || (fcu->flag & FCURVE_ACTIVE)) {
977                                                 /* name based filtering... */
978                                                 if ( ((ads) && (ads->filterflag & ADS_FILTER_BY_FCU_NAME)) && (owner_id) ) {
979                                                         if (skip_fcurve_with_name(ads, fcu, owner_id))
980                                                                 continue;
981                                                 }
982                                                 
983                                                 /* this F-Curve can be used, so return it */
984                                                 return fcu;
985                                         }
986                                 }
987                         }
988                 }
989         }
990         
991         /* no (more) F-Curves from the list are suitable... */
992         return NULL;
993 }
994
995 static size_t animfilter_fcurves (ListBase *anim_data, bDopeSheet *ads, FCurve *first, bActionGroup *grp, int filter_mode, ID *owner_id)
996 {
997         FCurve *fcu;
998         size_t items = 0;
999         
1000         /* loop over every F-Curve able to be included 
1001          *      - this for-loop works like this: 
1002          *              1) the starting F-Curve is assigned to the fcu pointer so that we have a starting point to search from
1003          *              2) the first valid F-Curve to start from (which may include the one given as 'first') in the remaining 
1004          *                 list of F-Curves is found, and verified to be non-null
1005          *              3) the F-Curve referenced by fcu pointer is added to the list
1006          *              4) the fcu pointer is set to the F-Curve after the one we just added, so that we can keep going through 
1007          *                 the rest of the F-Curve list without an eternal loop. Back to step 2 :)
1008          */
1009         for (fcu = first; ( (fcu = animfilter_fcurve_next(ads, fcu, grp, filter_mode, owner_id)) ); fcu = fcu->next) {
1010                 ANIMCHANNEL_NEW_CHANNEL(fcu, ANIMTYPE_FCURVE, owner_id);
1011         }
1012         
1013         /* return the number of items added to the list */
1014         return items;
1015 }
1016
1017 static size_t animfilter_act_group (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, bAction *UNUSED(act), bActionGroup *agrp, int filter_mode, ID *owner_id)
1018 {
1019         ListBase tmp_data = {NULL, NULL};
1020         size_t tmp_items = 0;
1021         size_t items = 0;
1022         //int ofilter = filter_mode;
1023         
1024         /* if we care about the selection status of the channels, 
1025          * but the group isn't expanded (1)...
1026          *      (1) this only matters if we actually care about the hierarchy though.
1027          *              - Hierarchy matters: this hack should be applied
1028          *              - Hierarchy ignored: cases like [#21276] won't work properly, unless we skip this hack
1029          */
1030         if ( ((filter_mode & ANIMFILTER_LIST_VISIBLE) && EXPANDED_AGRP(ac, agrp)==0) &&         /* care about hierarchy but group isn't expanded */
1031                   (filter_mode & (ANIMFILTER_SEL|ANIMFILTER_UNSEL)) )                                                   /* care about selection status */        
1032         {
1033                 /* if the group itself isn't selected appropriately, we shouldn't consider it's children either */
1034                 if (ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) == 0)
1035                         return 0;
1036                 
1037                 /* if we're still here, then the selection status of the curves within this group should not matter,
1038                  * since this creates too much overhead for animators (i.e. making a slow workflow)
1039                  *
1040                  * Tools affected by this at time of coding (2010 Feb 09):
1041                  *      - inserting keyframes on selected channels only
1042                  *      - pasting keyframes
1043                  *      - creating ghost curves in Graph Editor
1044                  */
1045                 filter_mode &= ~(ANIMFILTER_SEL|ANIMFILTER_UNSEL|ANIMFILTER_LIST_VISIBLE);
1046         }
1047         
1048         /* add grouped F-Curves */
1049         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_AGRP(ac, agrp))
1050         {
1051                 /* special filter so that we can get just the F-Curves within the active group */
1052                 if (!(filter_mode & ANIMFILTER_ACTGROUPED) || (agrp->flag & AGRP_ACTIVE)) {
1053                         /* for the Graph Editor, curves may be set to not be visible in the view to lessen clutter,
1054                          * but to do this, we need to check that the group doesn't have it's not-visible flag set preventing 
1055                          * all its sub-curves to be shown
1056                          */
1057                         if (!(filter_mode & ANIMFILTER_CURVE_VISIBLE) || !(agrp->flag & AGRP_NOTVISIBLE)) {
1058                                 /* group must be editable for its children to be editable (if we care about this) */
1059                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_AGRP(agrp)) {
1060                                         /* get first F-Curve which can be used here */
1061                                         FCurve *first_fcu = animfilter_fcurve_next(ads, agrp->channels.first, agrp, filter_mode, owner_id);
1062                                         
1063                                         /* filter list, starting from this F-Curve */
1064                                         tmp_items += animfilter_fcurves(&tmp_data, ads, first_fcu, agrp, filter_mode, owner_id);
1065                                 }
1066                         }
1067                 }
1068         }
1069         END_ANIMFILTER_SUBCHANNELS;
1070         
1071         /* did we find anything? */
1072         if (tmp_items) {
1073                 /* add this group as a channel first */
1074                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1075                         /* restore original filter mode so that this next step works ok... */
1076                         //filter_mode = ofilter;
1077                         
1078                         /* filter selection of channel specially here again, since may be open and not subject to previous test */
1079                         if ( ANIMCHANNEL_SELOK(SEL_AGRP(agrp)) ) {
1080                                 ANIMCHANNEL_NEW_CHANNEL(agrp, ANIMTYPE_GROUP, owner_id);
1081                         }
1082                 }
1083                 
1084                 /* now add the list of collected channels */
1085                 BLI_movelisttolist(anim_data, &tmp_data);
1086                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1087                 items += tmp_items;
1088         }
1089         
1090         /* return the number of items added to the list */
1091         return items;
1092 }
1093
1094 static size_t animfilter_action (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, bAction *act, int filter_mode, ID *owner_id)
1095 {
1096         bActionGroup *agrp;
1097         FCurve *lastchan = NULL;
1098         size_t items = 0;
1099         
1100         /* don't include anything from this action if it is linked in from another file,
1101          * and we're getting stuff for editing...
1102          */
1103         // TODO: need a way of tagging other channels that may also be affected...
1104         if ((filter_mode & ANIMFILTER_FOREDIT) && (act->id.lib))
1105                 return 0;
1106                 
1107         /* do groups */
1108         // TODO: do nested groups?
1109         for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1110                 /* store reference to last channel of group */
1111                 if (agrp->channels.last) 
1112                         lastchan= agrp->channels.last;
1113                         
1114                 /* action group's channels */
1115                 items += animfilter_act_group(ac, anim_data, ads, act, agrp, filter_mode, owner_id);
1116         }
1117         
1118         /* un-grouped F-Curves (only if we're not only considering those channels in the active group) */
1119         if (!(filter_mode & ANIMFILTER_ACTGROUPED)) {
1120                 FCurve *firstfcu = (lastchan)? (lastchan->next) : (act->curves.first);
1121                 items += animfilter_fcurves(anim_data, ads, firstfcu, NULL, filter_mode, owner_id);
1122         }
1123         
1124         /* return the number of items added to the list */
1125         return items;
1126 }
1127
1128 /* Include NLA-Data for NLA-Editor:
1129  *      - when ANIMFILTER_LIST_CHANNELS is used, that means we should be filtering the list for display
1130  *        Although the evaluation order is from the first track to the last and then apply the Action on top,
1131  *        we present this in the UI as the Active Action followed by the last track to the first so that we 
1132  *        get the evaluation order presented as per a stack.
1133  *      - for normal filtering (i.e. for editing), we only need the NLA-tracks but they can be in 'normal' evaluation
1134  *        order, i.e. first to last. Otherwise, some tools may get screwed up.
1135  */
1136 static size_t animfilter_nla (bAnimContext *UNUSED(ac), ListBase *anim_data, bDopeSheet *ads, AnimData *adt, int filter_mode, ID *owner_id)
1137 {
1138         NlaTrack *nlt;
1139         NlaTrack *first=NULL, *next=NULL;
1140         size_t items = 0;
1141         
1142         /* if showing channels, include active action */
1143         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1144                 /* if NLA action-line filtering is off, don't show unless there are keyframes, 
1145                  * in order to keep things more compact for doing transforms
1146                  */
1147                 if (!(ads->filterflag & ADS_FILTER_NLA_NOACT) || (adt->action)) {
1148                         /* there isn't really anything editable here, so skip if need editable */
1149                         if ((filter_mode & ANIMFILTER_FOREDIT) == 0) { 
1150                                 /* just add the action track now (this MUST appear for drawing)
1151                                  *      - as AnimData may not have an action, we pass a dummy pointer just to get the list elem created, then
1152                                  *        overwrite this with the real value - REVIEW THIS...
1153                                  */
1154                                 ANIMCHANNEL_NEW_CHANNEL_FULL((void *)(&adt->action), ANIMTYPE_NLAACTION, owner_id, 
1155                                         {
1156                                                 ale->data= adt->action ? adt->action : NULL; 
1157                                         });
1158                         }
1159                 }
1160                 
1161                 /* first track to include will be the last one if we're filtering by channels */
1162                 first= adt->nla_tracks.last;
1163         }
1164         else {
1165                 /* first track to include will the the first one (as per normal) */
1166                 first= adt->nla_tracks.first;
1167         }
1168         
1169         /* loop over NLA Tracks - assume that the caller of this has already checked that these should be included */
1170         for (nlt= first; nlt; nlt= next) {
1171                 /* 'next' NLA-Track to use depends on whether we're filtering for drawing or not */
1172                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) 
1173                         next= nlt->prev;
1174                 else
1175                         next= nlt->next;
1176                 
1177                 /* if we're in NLA-tweakmode, don't show this track if it was disabled (due to tweaking) for now 
1178                  *      - active track should still get shown though (even though it has disabled flag set)
1179                  */
1180                 // FIXME: the channels after should still get drawn, just 'differently', and after an active-action channel
1181                 if ((adt->flag & ADT_NLA_EDIT_ON) && (nlt->flag & NLATRACK_DISABLED) && !(nlt->flag & NLATRACK_ACTIVE))
1182                         continue;
1183                 
1184                 /* only work with this channel and its subchannels if it is editable */
1185                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_NLT(nlt)) {
1186                         /* only include this track if selected in a way consistent with the filtering requirements */
1187                         if (ANIMCHANNEL_SELOK(SEL_NLT(nlt))) {
1188                                 /* only include if this track is active */
1189                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (nlt->flag & NLATRACK_ACTIVE)) {
1190                                         ANIMCHANNEL_NEW_CHANNEL(nlt, ANIMTYPE_NLATRACK, owner_id);
1191                                 }
1192                         }
1193                 }
1194         }
1195         
1196         /* return the number of items added to the list */
1197         return items;
1198 }
1199
1200 /* determine what animation data from AnimData block should get displayed */
1201 static size_t animfilter_block_data (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *id, int filter_mode)
1202 {
1203         AnimData *adt = BKE_animdata_from_id(id);
1204         size_t items = 0;
1205
1206         /* image object datablocks have no anim-data so check for NULL */
1207         if (adt) {
1208                 IdAdtTemplate *iat = (IdAdtTemplate*)id;
1209
1210                 /* NOTE: this macro is used instead of inlining the logic here, since this sort of filtering is still needed
1211                  * in a few places in he rest of the code still - notably for the few cases where special mode-based
1212                  * different types of data expanders are required.
1213                  */
1214                 ANIMDATA_FILTER_CASES(iat,
1215                         { /* AnimData */
1216                                 /* specifically filter animdata block */
1217                                 ANIMCHANNEL_NEW_CHANNEL(adt, ANIMTYPE_ANIMDATA, id);
1218                         },
1219                         { /* NLA */
1220                                 items += animfilter_nla(ac, anim_data, ads, adt, filter_mode, id);
1221                         },
1222                         { /* Drivers */
1223                                 items += animfilter_fcurves(anim_data, ads, adt->drivers.first, NULL, filter_mode, id);
1224                         },
1225                         { /* Keyframes */
1226                                 items += animfilter_action(ac, anim_data, ads, adt->action, filter_mode, id);
1227                         }
1228                 );
1229         }
1230
1231         return items;
1232 }
1233
1234
1235
1236 /* Include ShapeKey Data for ShapeKey Editor */
1237 static size_t animdata_filter_shapekey (bAnimContext *ac, ListBase *anim_data, Key *key, int filter_mode)
1238 {
1239         size_t items = 0;
1240         
1241         /* check if channels or only F-Curves */
1242         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1243                 KeyBlock *kb;
1244                 
1245                 /* loop through the channels adding ShapeKeys as appropriate */
1246                 for (kb= key->block.first; kb; kb= kb->next) {
1247                         /* skip the first one, since that's the non-animatable basis */
1248                         // XXX maybe in future this may become handy?
1249                         if (kb == key->block.first) continue;
1250                         
1251                         /* only work with this channel and its subchannels if it is editable */
1252                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_SHAPEKEY(kb)) {
1253                                 /* only include this track if selected in a way consistent with the filtering requirements */
1254                                 if ( ANIMCHANNEL_SELOK(SEL_SHAPEKEY(kb)) ) {
1255                                         // TODO: consider 'active' too?
1256                                         
1257                                         /* owner-id here must be key so that the F-Curve can be resolved... */
1258                                         ANIMCHANNEL_NEW_CHANNEL(kb, ANIMTYPE_SHAPEKEY, key);
1259                                 }
1260                         }
1261                 }
1262         }
1263         else {
1264                 /* just use the action associated with the shapekey */
1265                 // TODO: somehow manage to pass dopesheet info down here too?
1266                 if (key->adt) {
1267                         if (filter_mode & ANIMFILTER_ANIMDATA) {
1268                                 ANIMCHANNEL_NEW_CHANNEL(key->adt, ANIMTYPE_ANIMDATA, key);
1269                         }
1270                         else if (key->adt->action) {
1271                                 items= animfilter_action(ac, anim_data, NULL, key->adt->action, filter_mode, (ID *)key);
1272                         }
1273                 }
1274         }
1275         
1276         /* return the number of items added to the list */
1277         return items;
1278 }
1279
1280 static size_t animdata_filter_gpencil_data (ListBase *anim_data, bGPdata *gpd, int filter_mode)
1281 {
1282         bGPDlayer *gpl;
1283         size_t items = 0;
1284         
1285         /* loop over layers as the conditions are acceptable */
1286         for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
1287                 /* only if selected */
1288                 if ( ANIMCHANNEL_SELOK(SEL_GPL(gpl)) ) {
1289                         /* only if editable */
1290                         if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
1291                                 /* active... */
1292                                 if (!(filter_mode & ANIMFILTER_ACTIVE) || (gpl->flag & GP_LAYER_ACTIVE)) {
1293                                         /* add to list */
1294                                         ANIMCHANNEL_NEW_CHANNEL(gpl, ANIMTYPE_GPLAYER, gpd);
1295                                 }
1296                         }
1297                 }
1298         }
1299         
1300         return items;
1301 }
1302
1303 /* Grab all Grease Pencil datablocks in file */
1304 // TODO: should this be amalgamated with the dopesheet filtering code?
1305 static size_t animdata_filter_gpencil (ListBase *anim_data, void *UNUSED(data), int filter_mode)
1306 {
1307         bGPdata *gpd;
1308         size_t items = 0;
1309         
1310         /* for now, grab grease pencil datablocks directly from main */
1311         // XXX: this is not good...
1312         for (gpd = G.main->gpencil.first; gpd; gpd = gpd->id.next) {
1313                 ListBase tmp_data = {NULL, NULL};
1314                 size_t tmp_items = 0;
1315                 
1316                 /* only show if gpd is used by something... */
1317                 if (ID_REAL_USERS(gpd) < 1)
1318                         continue;
1319                         
1320                 /* add gpencil animation channels */
1321                 BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_GPD(gpd))
1322                 {
1323                         tmp_items += animdata_filter_gpencil_data(&tmp_data, gpd, filter_mode);
1324                 }
1325                 END_ANIMFILTER_SUBCHANNELS;
1326                 
1327                 /* did we find anything? */
1328                 if (tmp_items) {
1329                         /* include data-expand widget first */
1330                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1331                                 /* add gpd as channel too (if for drawing, and it has layers) */
1332                                 ANIMCHANNEL_NEW_CHANNEL(gpd, ANIMTYPE_GPDATABLOCK, NULL);
1333                         }
1334                         
1335                         /* now add the list of collected channels */
1336                         BLI_movelisttolist(anim_data, &tmp_data);
1337                         BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1338                         items += tmp_items;
1339                 }
1340         }
1341         
1342         /* return the number of items added to the list */
1343         return items;
1344 }
1345
1346 /* NOTE: owner_id is scene, material, or texture block, which is the direct owner of the node tree in question */
1347 // TODO: how to handle group nodes is still unclear...
1348 static size_t animdata_filter_ds_nodetree (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *owner_id, bNodeTree *ntree, int filter_mode)
1349 {
1350         ListBase tmp_data = {NULL, NULL};
1351         size_t tmp_items = 0;
1352         size_t items = 0;
1353         
1354         /* add nodetree animation channels */
1355         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_NTREE_DATA(ntree))
1356         {
1357                 /* animation data filtering */
1358                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)ntree, filter_mode);
1359         }
1360         END_ANIMFILTER_SUBCHANNELS;
1361         
1362         /* did we find anything? */
1363         if (tmp_items) {
1364                 /* include data-expand widget first */
1365                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1366                         /* check if filtering by active status */
1367                         if (ANIMCHANNEL_ACTIVEOK(ntree)) {
1368                                 ANIMCHANNEL_NEW_CHANNEL(ntree, ANIMTYPE_DSNTREE, owner_id);
1369                         }
1370                 }
1371                 
1372                 /* now add the list of collected channels */
1373                 BLI_movelisttolist(anim_data, &tmp_data);
1374                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1375                 items += tmp_items;
1376         }
1377         
1378         /* return the number of items added to the list */
1379         return items;
1380 }
1381
1382 /* NOTE: owner_id is either material, lamp, or world block, which is the direct owner of the texture stack in question */
1383 static size_t animdata_filter_ds_textures (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, ID *owner_id, int filter_mode)
1384 {
1385         MTex **mtex = NULL;
1386         size_t items=0;
1387         int a=0;
1388         
1389         /* get datatype specific data first */
1390         if (owner_id == NULL)
1391                 return 0;
1392         
1393         switch (GS(owner_id->name)) {
1394                 case ID_MA:
1395                 {
1396                         Material *ma= (Material *)owner_id;
1397                         mtex= (MTex**)(&ma->mtex);
1398                 }
1399                         break;
1400                 case ID_LA:
1401                 {
1402                         Lamp *la= (Lamp *)owner_id;
1403                         mtex= (MTex**)(&la->mtex);
1404                 }
1405                         break;
1406                 case ID_WO:
1407                 {
1408                         World *wo= (World *)owner_id;
1409                         mtex= (MTex**)(&wo->mtex);
1410                 }
1411                         break;
1412                 default: 
1413                 {
1414                         /* invalid/unsupported option */
1415                         if (G.debug & G_DEBUG)
1416                                 printf("ERROR: unsupported owner_id (i.e. texture stack) for filter textures - %s\n", owner_id->name);
1417                         return 0;
1418                 }
1419         }
1420         
1421         /* firstly check that we actuallly have some textures, by gathering all textures in a temp list */
1422         for (a=0; a < MAX_MTEX; a++) {
1423                 Tex *tex= (mtex[a]) ? mtex[a]->tex : NULL;
1424                 ListBase tmp_data = {NULL, NULL};
1425                 size_t tmp_items = 0;
1426                 
1427                 /* for now, if no texture returned, skip (this shouldn't confuse the user I hope) */
1428                 if (tex == NULL) 
1429                         continue;
1430                 
1431                 /* add texture's animation data to temp collection */
1432                 BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_TEX_DATA(tex)) 
1433                 {
1434                         /* texture animdata */
1435                         tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)tex, filter_mode);
1436                         
1437                         /* nodes */
1438                         if ((tex->nodetree) && !(ads->filterflag & ADS_FILTER_NONTREE)) {
1439                                 /* owner_id as id instead of texture, since it'll otherwise be impossible to track the depth */
1440                                 // FIXME: perhaps as a result, textures should NOT be included under materials, but under their own section instead
1441                                 // so that free-floating textures can also be animated
1442                                 tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)tex, tex->nodetree, filter_mode);
1443                         }
1444                 }
1445                 END_ANIMFILTER_SUBCHANNELS;
1446                 
1447                 /* did we find anything? */
1448                 if (tmp_items) {
1449                         /* include texture-expand widget? */
1450                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1451                                 /* check if filtering by active status */
1452                                 if (ANIMCHANNEL_ACTIVEOK(tex)) {
1453                                         ANIMCHANNEL_NEW_CHANNEL(tex, ANIMTYPE_DSTEX, owner_id);
1454                                 }
1455                         }
1456                         
1457                         /* now add the list of collected channels */
1458                         BLI_movelisttolist(anim_data, &tmp_data);
1459                         BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1460                         items += tmp_items;
1461                 }
1462         }
1463         
1464         /* return the number of items added to the list */
1465         return items;
1466 }
1467
1468 static size_t animdata_filter_ds_materials (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
1469 {
1470         size_t items=0;
1471         int a=0;
1472         
1473         /* firstly check that we actuallly have some materials, by gathering all materials in a temp list */
1474         for (a=1; a <= ob->totcol; a++) {
1475                 Material *ma= give_current_material(ob, a);
1476                 ListBase tmp_data = {NULL, NULL};
1477                 size_t tmp_items = 0;
1478                 
1479                 /* if no material returned, skip - so that we don't get weird blank entries... */
1480                 if (ma == NULL) continue;
1481                 
1482                 /* add material's animation data to temp collection */
1483                 BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_MAT_OBJD(ma))
1484                 {
1485                         /* material's animation data */
1486                         tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)ma, filter_mode);
1487                                 
1488                         /* textures */
1489                         if (!(ads->filterflag & ADS_FILTER_NOTEX))
1490                                 tmp_items += animdata_filter_ds_textures(ac, &tmp_data, ads, (ID *)ma, filter_mode);
1491                                 
1492                         /* nodes */
1493                         if ((ma->nodetree) && !(ads->filterflag & ADS_FILTER_NONTREE)) 
1494                                 tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)ma, ma->nodetree, filter_mode);
1495                 }
1496                 END_ANIMFILTER_SUBCHANNELS;
1497                 
1498                 /* did we find anything? */
1499                 if (tmp_items) {
1500                         /* include material-expand widget first */
1501                         // hmm... do we need to store the index of this material in the array anywhere?
1502                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1503                                 /* check if filtering by active status */
1504                                 if (ANIMCHANNEL_ACTIVEOK(ma)) {
1505                                         ANIMCHANNEL_NEW_CHANNEL(ma, ANIMTYPE_DSMAT, ma);
1506                                 }
1507                         }
1508                         
1509                         /* now add the list of collected channels */
1510                         BLI_movelisttolist(anim_data, &tmp_data);
1511                         BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1512                         items += tmp_items;
1513                 }
1514         }
1515         
1516         /* return the number of items added to the list */
1517         return items;
1518 }
1519
1520 static size_t animdata_filter_ds_particles (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
1521 {
1522         ParticleSystem *psys;
1523         size_t items= 0;
1524
1525         for (psys = ob->particlesystem.first; psys; psys=psys->next) {
1526                 ListBase tmp_data = {NULL, NULL};
1527                 size_t tmp_items = 0;
1528                 
1529                 /* if no material returned, skip - so that we don't get weird blank entries... */
1530                 if (ELEM(NULL, psys->part, psys->part->adt))
1531                         continue;
1532                 
1533                 /* add particle-system's animation data to temp collection */
1534                 BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_PART_OBJD(psys->part))
1535                 {
1536                         /* material's animation data */
1537                         tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)psys->part, filter_mode);
1538                 }
1539                 END_ANIMFILTER_SUBCHANNELS;
1540                 
1541                 /* did we find anything? */
1542                 if (tmp_items) {
1543                         /* include particle-expand widget first */
1544                         if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1545                                 /* check if filtering by active status */
1546                                 if (ANIMCHANNEL_ACTIVEOK(psys->part)) {
1547                                         ANIMCHANNEL_NEW_CHANNEL(psys->part, ANIMTYPE_DSPART, psys->part);
1548                                 }
1549                         }
1550                         
1551                         /* now add the list of collected channels */
1552                         BLI_movelisttolist(anim_data, &tmp_data);
1553                         BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1554                         items += tmp_items;
1555                 }
1556         }
1557         
1558         /* return the number of items added to the list */
1559         return items;
1560 }
1561
1562 static size_t animdata_filter_ds_obdata (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
1563 {
1564         ListBase tmp_data = {NULL, NULL};
1565         size_t tmp_items = 0;
1566         size_t items= 0;
1567         
1568         IdAdtTemplate *iat= ob->data;
1569         short type=0, expanded=0;
1570         
1571         /* get settings based on data type */
1572         switch (ob->type) {
1573                 case OB_CAMERA: /* ------- Camera ------------ */
1574                 {
1575                         Camera *ca= (Camera *)ob->data;
1576                         
1577                         if (ads->filterflag & ADS_FILTER_NOCAM)
1578                                 return 0;
1579                         
1580                         type= ANIMTYPE_DSCAM;
1581                         expanded= FILTER_CAM_OBJD(ca);
1582                 }
1583                         break;
1584                 case OB_LAMP: /* ---------- Lamp ----------- */
1585                 {
1586                         Lamp *la= (Lamp *)ob->data;
1587                         
1588                         if (ads->filterflag & ADS_FILTER_NOLAM)
1589                                 return 0;
1590                         
1591                         type= ANIMTYPE_DSLAM;
1592                         expanded= FILTER_LAM_OBJD(la);
1593                 }
1594                         break;
1595                 case OB_CURVE: /* ------- Curve ---------- */
1596                 case OB_SURF: /* ------- Nurbs Surface ---------- */
1597                 case OB_FONT: /* ------- Text Curve ---------- */
1598                 {
1599                         Curve *cu= (Curve *)ob->data;
1600                         
1601                         if (ads->filterflag & ADS_FILTER_NOCUR)
1602                                 return 0;
1603                         
1604                         type= ANIMTYPE_DSCUR;
1605                         expanded= FILTER_CUR_OBJD(cu);
1606                 }
1607                         break;
1608                 case OB_MBALL: /* ------- MetaBall ---------- */
1609                 {
1610                         MetaBall *mb= (MetaBall *)ob->data;
1611                         
1612                         if (ads->filterflag & ADS_FILTER_NOMBA)
1613                                 return 0;
1614                         
1615                         type= ANIMTYPE_DSMBALL;
1616                         expanded= FILTER_MBALL_OBJD(mb);
1617                 }
1618                         break;
1619                 case OB_ARMATURE: /* ------- Armature ---------- */
1620                 {
1621                         bArmature *arm= (bArmature *)ob->data;
1622                         
1623                         if (ads->filterflag & ADS_FILTER_NOARM)
1624                                 return 0;
1625                         
1626                         type= ANIMTYPE_DSARM;
1627                         expanded= FILTER_ARM_OBJD(arm);
1628                 }
1629                         break;
1630                 case OB_MESH: /* ------- Mesh ---------- */
1631                 {
1632                         Mesh *me= (Mesh *)ob->data;
1633                         
1634                         if (ads->filterflag & ADS_FILTER_NOMESH)
1635                                 return 0;
1636                         
1637                         type= ANIMTYPE_DSMESH;
1638                         expanded= FILTER_MESH_OBJD(me);
1639                 }
1640                         break;
1641                 case OB_LATTICE: /* ---- Lattice ---- */
1642                 {
1643                         Lattice *lt = (Lattice *)ob->data;
1644                         
1645                         if (ads->filterflag & ADS_FILTER_NOLAT)
1646                                 return 0;
1647                         
1648                         type= ANIMTYPE_DSLAT;
1649                         expanded= FILTER_LATTICE_OBJD(lt);
1650                 }
1651                         break;
1652                 case OB_SPEAKER: /* ---------- Speaker ----------- */
1653                 {
1654                         Speaker *spk= (Speaker *)ob->data;
1655                         
1656                         type= ANIMTYPE_DSSPK;
1657                         expanded= FILTER_SPK_OBJD(spk);
1658                 }
1659                         break;
1660         }
1661         
1662         /* add object data animation channels */
1663         BEGIN_ANIMFILTER_SUBCHANNELS(expanded)
1664         {
1665                 /* animation data filtering */
1666                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)iat, filter_mode);
1667                 
1668                 /* sub-data filtering... */
1669                 switch (ob->type) {
1670                         case OB_LAMP:   /* lamp - textures */
1671                         {
1672                                 /* textures */
1673                                 if (!(ads->filterflag & ADS_FILTER_NOTEX))
1674                                         tmp_items += animdata_filter_ds_textures(ac, &tmp_data, ads, ob->data, filter_mode);
1675                         }
1676                                 break;
1677                 }
1678         }
1679         END_ANIMFILTER_SUBCHANNELS;
1680         
1681         /* did we find anything? */
1682         if (tmp_items) {
1683                 /* include data-expand widget first */
1684                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1685                         /* check if filtering by active status */
1686                         if (ANIMCHANNEL_ACTIVEOK(iat)) {
1687                                 ANIMCHANNEL_NEW_CHANNEL(iat, type, iat);
1688                         }
1689                 }
1690                 
1691                 /* now add the list of collected channels */
1692                 BLI_movelisttolist(anim_data, &tmp_data);
1693                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1694                 items += tmp_items;
1695         }
1696         
1697         /* return the number of items added to the list */
1698         return items;
1699 }
1700
1701 /* shapekey-level animation */
1702 static size_t animdata_filter_ds_keyanim (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, Key *key, int filter_mode)
1703 {
1704         ListBase tmp_data = {NULL, NULL};
1705         size_t tmp_items = 0;
1706         size_t items = 0;
1707         
1708         /* add shapekey-level animation channels */
1709         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_SKE_OBJD(key))
1710         {
1711                 /* animation data filtering */
1712                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)key, filter_mode);
1713         }
1714         END_ANIMFILTER_SUBCHANNELS;
1715         
1716         /* did we find anything? */
1717         if (tmp_items) {
1718                 /* include key-expand widget first */
1719                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1720                         if (ANIMCHANNEL_ACTIVEOK(key)) {
1721                                 ANIMCHANNEL_NEW_CHANNEL(key, ANIMTYPE_DSSKEY, ob);
1722                         }
1723                 }
1724                 
1725                 /* now add the list of collected channels */
1726                 BLI_movelisttolist(anim_data, &tmp_data);
1727                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1728                 items += tmp_items;
1729         }
1730         
1731         /* return the number of items added to the list */
1732         return items;
1733 }
1734
1735 /* object-level animation */
1736 static size_t animdata_filter_ds_obanim (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Object *ob, int filter_mode)
1737 {
1738         ListBase tmp_data = {NULL, NULL};
1739         size_t tmp_items = 0;
1740         size_t items = 0;
1741         
1742         AnimData *adt = ob->adt;
1743         short type=0, expanded=1;
1744         void *cdata = NULL;
1745         
1746         /* determine the type of expander channels to use */
1747         // this is the best way to do this for now...
1748         ANIMDATA_FILTER_CASES(ob,
1749                 {/* AnimData - no channel, but consider data */},
1750                 {/* NLA - no channel, but consider data */},
1751                 {/* Drivers */
1752                         type = ANIMTYPE_FILLDRIVERS;
1753                         cdata = adt;
1754                         expanded = EXPANDED_DRVD(adt);
1755                 },
1756                 {/* Keyframes */
1757                         type = ANIMTYPE_FILLACTD;
1758                         cdata = adt->action;
1759                         expanded = EXPANDED_ACTC(adt->action);
1760                 });
1761                 
1762         /* add object-level animation channels */
1763         BEGIN_ANIMFILTER_SUBCHANNELS(expanded)
1764         {
1765                 /* animation data filtering */
1766                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)ob, filter_mode);
1767         }
1768         END_ANIMFILTER_SUBCHANNELS;
1769         
1770         /* did we find anything? */
1771         if (tmp_items) {
1772                 /* include anim-expand widget first */
1773                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1774                         if (type != ANIMTYPE_NONE) {
1775                                 /* NOTE: active-status (and the associated checks) don't apply here... */
1776                                 ANIMCHANNEL_NEW_CHANNEL(cdata, type, ob);
1777                         }
1778                 }
1779                 
1780                 /* now add the list of collected channels */
1781                 BLI_movelisttolist(anim_data, &tmp_data);
1782                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1783                 items += tmp_items;
1784         }
1785         
1786         /* return the number of items added to the list */
1787         return items;
1788 }
1789
1790 /* get animation channels from object2 */
1791 static size_t animdata_filter_dopesheet_ob (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
1792 {
1793         ListBase tmp_data = {NULL, NULL};
1794         Object *ob= base->object;
1795         size_t tmp_items = 0;
1796         size_t items = 0;
1797         
1798         /* filter data contained under object first */
1799         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_OBJC(ob))
1800         {
1801                 Key *key= ob_get_key(ob);
1802                 
1803                 /* object-level animation */
1804                 if ((ob->adt) && !(ads->filterflag & ADS_FILTER_NOOBJ)) {
1805                         tmp_items += animdata_filter_ds_obanim(ac, &tmp_data, ads, ob, filter_mode);
1806                 }
1807                 
1808                 /* shape-key */
1809                 if ((key && key->adt) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
1810                         tmp_items += animdata_filter_ds_keyanim(ac, &tmp_data, ads, ob, key, filter_mode);
1811                 }
1812                 
1813                 /* materials */
1814                 if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT)) {
1815                         tmp_items += animdata_filter_ds_materials(ac, &tmp_data, ads, ob, filter_mode);
1816                 }
1817                 
1818                 /* object data */
1819                 if (ob->data) {
1820                         tmp_items += animdata_filter_ds_obdata(ac, &tmp_data, ads, ob, filter_mode);
1821                 }
1822                 
1823                 /* particles */
1824                 if ((ob->particlesystem.first) && !(ads->filterflag & ADS_FILTER_NOPART)) {
1825                         tmp_items += animdata_filter_ds_particles(ac, &tmp_data, ads, ob, filter_mode);
1826                 }
1827         }
1828         END_ANIMFILTER_SUBCHANNELS;
1829         
1830         
1831         /* if we collected some channels, add these to the new list... */
1832         if (tmp_items) {
1833                 /* firstly add object expander if required */
1834                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1835                         /* check if filtering by selection */
1836                         // XXX: double-check on this - most of the time, a lot of tools need to filter out these channels!
1837                         if (ANIMCHANNEL_SELOK((base->flag & SELECT))) {
1838                                 /* check if filtering by active status */
1839                                 if (ANIMCHANNEL_ACTIVEOK(ob)) {
1840                                         ANIMCHANNEL_NEW_CHANNEL(base, ANIMTYPE_OBJECT, ob);
1841                                 }
1842                         }
1843                 }
1844                 
1845                 /* now add the list of collected channels */
1846                 BLI_movelisttolist(anim_data, &tmp_data);
1847                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1848                 items += tmp_items;
1849         }
1850         
1851         /* return the number of items added */
1852         return items;
1853 }
1854
1855 static size_t animdata_filter_ds_world (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, World *wo, int filter_mode)
1856 {
1857         ListBase tmp_data = {NULL, NULL};
1858         size_t tmp_items = 0;
1859         size_t items = 0;
1860         
1861         /* add world animation channels */
1862         BEGIN_ANIMFILTER_SUBCHANNELS(FILTER_WOR_SCED(wo))
1863         {
1864                 /* animation data filtering */
1865                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)wo, filter_mode);
1866                 
1867                 /* textures for world */
1868                 if (!(ads->filterflag & ADS_FILTER_NOTEX))
1869                         items += animdata_filter_ds_textures(ac, &tmp_data, ads, (ID *)wo, filter_mode);
1870                         
1871                 /* nodes */
1872                 if ((wo->nodetree) && !(ads->filterflag & ADS_FILTER_NONTREE)) 
1873                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)wo, wo->nodetree, filter_mode);
1874         }
1875         END_ANIMFILTER_SUBCHANNELS;
1876         
1877         /* did we find anything? */
1878         if (tmp_items) {
1879                 /* include data-expand widget first */
1880                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1881                         /* check if filtering by active status */
1882                         if (ANIMCHANNEL_ACTIVEOK(wo)) {
1883                                 ANIMCHANNEL_NEW_CHANNEL(wo, ANIMTYPE_DSWOR, sce);
1884                         }
1885                 }
1886                 
1887                 /* now add the list of collected channels */
1888                 BLI_movelisttolist(anim_data, &tmp_data);
1889                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1890                 items += tmp_items;
1891         }
1892         
1893         /* return the number of items added to the list */
1894         return items;
1895 }
1896
1897 static size_t animdata_filter_ds_scene (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
1898 {
1899         ListBase tmp_data = {NULL, NULL};
1900         size_t tmp_items = 0;
1901         size_t items = 0;
1902         
1903         AnimData *adt = sce->adt;
1904         short type=0, expanded=1;
1905         void *cdata = NULL;
1906         
1907         /* determine the type of expander channels to use */
1908         // this is the best way to do this for now...
1909         ANIMDATA_FILTER_CASES(sce,
1910                 {/* AnimData - no channel, but consider data */},
1911                 {/* NLA - no channel, but consider data */},
1912                 {/* Drivers */
1913                         type = ANIMTYPE_FILLDRIVERS;
1914                         cdata = adt;
1915                         expanded = EXPANDED_DRVD(adt);
1916                 },
1917                 {/* Keyframes */
1918                         type = ANIMTYPE_FILLACTD;
1919                         cdata = adt->action;
1920                         expanded = EXPANDED_ACTC(adt->action);
1921                 });
1922                 
1923         /* add scene-level animation channels */
1924         BEGIN_ANIMFILTER_SUBCHANNELS(expanded)
1925         {
1926                 /* animation data filtering */
1927                 tmp_items += animfilter_block_data(ac, &tmp_data, ads, (ID *)sce, filter_mode);
1928         }
1929         END_ANIMFILTER_SUBCHANNELS;
1930         
1931         /* did we find anything? */
1932         if (tmp_items) {
1933                 /* include anim-expand widget first */
1934                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1935                         if (type != ANIMTYPE_NONE) {
1936                                 /* NOTE: active-status (and the associated checks) don't apply here... */
1937                                 ANIMCHANNEL_NEW_CHANNEL(cdata, type, sce);
1938                         }
1939                 }
1940                 
1941                 /* now add the list of collected channels */
1942                 BLI_movelisttolist(anim_data, &tmp_data);
1943                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1944                 items += tmp_items;
1945         }
1946         
1947         /* return the number of items added to the list */
1948         return items;
1949 }
1950
1951 static size_t animdata_filter_dopesheet_scene (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, Scene *sce, int filter_mode)
1952 {
1953         ListBase tmp_data = {NULL, NULL};
1954         size_t tmp_items = 0;
1955         size_t items = 0;
1956         
1957         /* filter data contained under object first */
1958         BEGIN_ANIMFILTER_SUBCHANNELS(EXPANDED_SCEC(sce))
1959         {
1960                 bNodeTree *ntree= sce->nodetree;
1961                 World *wo= sce->world;
1962                 
1963                 /* Action, Drivers, or NLA for Scene */
1964                 if ((ads->filterflag & ADS_FILTER_NOSCE) == 0) {
1965                         tmp_items += animdata_filter_ds_scene(ac, &tmp_data, ads, sce, filter_mode);
1966                 }
1967                 
1968                 /* world */
1969                 if ((wo) && !(ads->filterflag & ADS_FILTER_NOWOR)) {
1970                         tmp_items += animdata_filter_ds_world(ac, &tmp_data, ads, sce, wo, filter_mode);
1971                 }
1972                 
1973                 /* nodetree */
1974                 if ((ntree) && !(ads->filterflag & ADS_FILTER_NONTREE)) {
1975                         tmp_items += animdata_filter_ds_nodetree(ac, &tmp_data, ads, (ID *)sce, ntree, filter_mode);
1976                 }
1977                 
1978                 // TODO: one day, when sequencer becomes its own datatype, perhaps it should be included here
1979         }
1980         END_ANIMFILTER_SUBCHANNELS;
1981         
1982         /* if we collected some channels, add these to the new list... */
1983         if (tmp_items) {
1984                 /* firstly add object expander if required */
1985                 if (filter_mode & ANIMFILTER_LIST_CHANNELS) {
1986                         /* check if filtering by selection */
1987                         if (ANIMCHANNEL_SELOK((sce->flag & SCE_DS_SELECTED))) {
1988                                 /* NOTE: active-status doesn't matter for this! */
1989                                 ANIMCHANNEL_NEW_CHANNEL(sce, ANIMTYPE_SCENE, sce);
1990                         }
1991                 }
1992                 
1993                 /* now add the list of collected channels */
1994                 BLI_movelisttolist(anim_data, &tmp_data);
1995                 BLI_assert((tmp_data.first == tmp_data.last) && (tmp_data.first == NULL));
1996                 items += tmp_items;
1997         }
1998         
1999         /* return the number of items added */
2000         return items;
2001 }
2002
2003 // 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)
2004 static size_t animdata_filter_dopesheet (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, int filter_mode)
2005 {
2006         Scene *sce= (Scene *)ads->source;
2007         Base *base;
2008         size_t items = 0;
2009         
2010         /* check that we do indeed have a scene */
2011         if ((ads->source == NULL) || (GS(ads->source->name)!=ID_SCE)) {
2012                 printf("DopeSheet Error: Not scene!\n");
2013                 if (G.debug & G_DEBUG)
2014                         printf("\tPointer = %p, Name = '%s'\n", (void *)ads->source, (ads->source)?ads->source->name:NULL);
2015                 return 0;
2016         }
2017         
2018         /* augment the filter-flags with settings based on the dopesheet filterflags 
2019          * so that some temp settings can get added automagically...
2020          */
2021         if (ads->filterflag & ADS_FILTER_SELEDIT) {
2022                 /* only selected F-Curves should get their keyframes considered for editability */
2023                 filter_mode |= ANIMFILTER_SELEDIT;
2024         }
2025         
2026         /* scene-linked animation - e.g. world, compositing nodes, scene anim (including sequencer currently) */
2027         items += animdata_filter_dopesheet_scene(ac, anim_data, ads, sce, filter_mode);
2028         
2029         /* loop over all bases (i.e.objects) in the scene */
2030         for (base= sce->base.first; base; base= base->next) {
2031                 /* check if there's an object (all the relevant checks are done in the ob-function) */
2032                 if (base->object) {
2033                         Object *ob= base->object;
2034                         
2035                         /* firstly, check if object can be included, by the following factors:
2036                          *      - if only visible, must check for layer and also viewport visibility
2037                          *              --> while tools may demand only visible, user setting takes priority
2038                          *                      as user option controls whether sets of channels get included while
2039                          *                      tool-flag takes into account collapsed/open channels too
2040                          *      - if only selected, must check if object is selected 
2041                          *      - there must be animation data to edit (this is done recursively as we 
2042                          *        try to add the channels)
2043                          */
2044                         if ((filter_mode & ANIMFILTER_DATA_VISIBLE) && !(ads->filterflag & ADS_FILTER_INCL_HIDDEN)) {
2045                                 /* layer visibility - we check both object and base, since these may not be in sync yet */
2046                                 if ((sce->lay & (ob->lay|base->lay))==0) continue;
2047                                 
2048                                 /* outliner restrict-flag */
2049                                 if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
2050                         }
2051                         
2052                         /* if only F-Curves with visible flags set can be shown, check that 
2053                          * datablock hasn't been set to invisible 
2054                          */
2055                         if (filter_mode & ANIMFILTER_CURVE_VISIBLE) {
2056                                 if ((ob->adt) && (ob->adt->flag & ADT_CURVES_NOT_VISIBLE))
2057                                         continue;
2058                         }
2059                         
2060                         /* check selection and object type filters */
2061                         if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) /*|| (base == sce->basact)*/) ) {
2062                                 /* only selected should be shown */
2063                                 continue;
2064                         }
2065                         
2066                         /* check if object belongs to the filtering group if option to filter 
2067                          * objects by the grouped status is on
2068                          *      - used to ease the process of doing multiple-character choreographies
2069                          */
2070                         if (ads->filterflag & ADS_FILTER_ONLYOBGROUP) {
2071                                 if (object_in_group(ob, ads->filter_grp) == 0)
2072                                         continue;
2073                         }
2074                                 
2075                         /* since we're still here, this object should be usable */
2076                         items += animdata_filter_dopesheet_ob(ac, anim_data, ads, base, filter_mode);
2077                 }
2078         }
2079         
2080         /* return the number of items in the list */
2081         return items;
2082 }
2083
2084 /* Summary track for DopeSheet/Action Editor 
2085  *      - return code is whether the summary lets the other channels get drawn
2086  */
2087 static short animdata_filter_dopesheet_summary (bAnimContext *ac, ListBase *anim_data, int filter_mode, size_t *items)
2088 {
2089         bDopeSheet *ads = NULL;
2090         
2091         /* get the DopeSheet information to use 
2092          *      - we should only need to deal with the DopeSheet/Action Editor, 
2093          *        since all the other Animation Editors won't have this concept
2094          *        being applicable.
2095          */
2096         if ((ac && ac->sl) && (ac->spacetype == SPACE_ACTION)) {
2097                 SpaceAction *saction= (SpaceAction *)ac->sl;
2098                 ads= &saction->ads;
2099         }
2100         else {
2101                 /* invalid space type - skip this summary channels */
2102                 return 1;
2103         }
2104         
2105         /* dopesheet summary 
2106          *      - only for drawing and/or selecting keyframes in channels, but not for real editing 
2107          *      - only useful for DopeSheet/Action/etc. editors where it is actually useful
2108          */
2109         if ((filter_mode & ANIMFILTER_LIST_CHANNELS) && (ads->filterflag & ADS_FILTER_SUMMARY)) {
2110                 bAnimListElem *ale= make_new_animlistelem(ac, ANIMTYPE_SUMMARY, NULL);
2111                 if (ale) {
2112                         BLI_addtail(anim_data, ale);
2113                         (*items)++;
2114                 }
2115                 
2116                 /* if summary is collapsed, don't show other channels beneath this 
2117                  *      - this check is put inside the summary check so that it doesn't interfere with normal operation
2118                  */ 
2119                 if (ads->flag & ADS_FLAG_SUMMARY_COLLAPSED)
2120                         return 0;
2121         }
2122         
2123         /* the other channels beneath this can be shown */
2124         return 1;
2125 }  
2126
2127 /* ......................... */
2128
2129 /* filter data associated with a channel - usually for handling summary-channels in DopeSheet */
2130 static size_t animdata_filter_animchan (bAnimContext *ac, ListBase *anim_data, bDopeSheet *ads, bAnimListElem *channel, int filter_mode)
2131 {
2132         size_t items = 0;
2133         
2134         /* data to filter depends on channel type */
2135         // XXX: only common channel-types have been handled for now
2136         switch (channel->type) {
2137                 case ANIMTYPE_SUMMARY:
2138                         items += animdata_filter_dopesheet(ac, anim_data, ads, filter_mode);
2139                         break;
2140                         
2141                 case ANIMTYPE_SCENE:
2142                         items += animdata_filter_dopesheet_scene(ac, anim_data, ads, channel->data, filter_mode);
2143                         break;
2144                 
2145                 case ANIMTYPE_OBJECT:
2146                         items += animdata_filter_dopesheet_ob(ac, anim_data, ads, channel->data, filter_mode);
2147                         break;
2148         }
2149         
2150         return items;
2151 }
2152
2153 /* ----------- Cleanup API --------------- */
2154
2155 /* Remove entries with invalid types in animation channel list */
2156 static size_t animdata_filter_remove_invalid (ListBase *anim_data)
2157 {
2158         bAnimListElem *ale, *next;
2159         size_t items = 0;
2160         
2161         /* only keep entries with valid types */
2162         for (ale= anim_data->first; ale; ale= next) {
2163                 next= ale->next;
2164                 
2165                 if (ale->type == ANIMTYPE_NONE)
2166                         BLI_freelinkN(anim_data, ale);
2167                 else
2168                         items++;
2169         }
2170         
2171         return items;
2172 }
2173
2174 /* Remove duplicate entries in animation channel list */
2175 static size_t animdata_filter_remove_duplis (ListBase *anim_data)
2176 {
2177         bAnimListElem *ale, *next;
2178         GHash *gh;
2179         size_t items = 0;
2180         
2181         /* build new hashtable to efficiently store and retrieve which entries have been 
2182          * encountered already while searching
2183          */
2184         gh= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, "animdata_filter_duplis_remove gh");
2185         
2186         /* loop through items, removing them from the list if a similar item occurs already */
2187         for (ale = anim_data->first; ale; ale = next) {
2188                 next = ale->next;
2189                 
2190                 /* check if hash has any record of an entry like this 
2191                  *      - just use ale->data for now, though it would be nicer to involve 
2192                  *        ale->type in combination too to capture corner cases (where same data performs differently)
2193                  */
2194                 if (BLI_ghash_haskey(gh, ale->data) == 0) {
2195                         /* this entry is 'unique' and can be kept */
2196                         BLI_ghash_insert(gh, ale->data, NULL);
2197                         items++;
2198                 }
2199                 else {
2200                         /* this entry isn't needed anymore */
2201                         BLI_freelinkN(anim_data, ale);
2202                 }
2203         }
2204         
2205         /* free the hash... */
2206         BLI_ghash_free(gh, NULL, NULL);
2207         
2208         /* return the number of items still in the list */
2209         return items;
2210 }
2211
2212 /* ----------- Public API --------------- */
2213
2214 /* This function filters the active data source to leave only animation channels suitable for
2215  * usage by the caller. It will return the length of the list 
2216  * 
2217  *      *anim_data: is a pointer to a ListBase, to which the filtered animation channels
2218  *              will be placed for use.
2219  *      filter_mode: how should the data be filtered - bitmapping accessed flags
2220  */
2221 size_t ANIM_animdata_filter(bAnimContext *ac, ListBase *anim_data, int filter_mode, void *data, short datatype)
2222 {
2223         size_t items = 0;
2224         
2225         /* only filter data if there's somewhere to put it */
2226         if (data && anim_data) {
2227                 
2228                 /* firstly filter the data */
2229                 switch (datatype) {
2230                         case ANIMCONT_ACTION:   /* 'Action Editor' */
2231                         {
2232                                 Object *obact= ac->obact;
2233                                 SpaceAction *saction = (SpaceAction *)ac->sl;
2234                                 bDopeSheet *ads = (saction)? &saction->ads : NULL;
2235                                 
2236                                 /* the check for the DopeSheet summary is included here since the summary works here too */
2237                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2238                                         items += animfilter_action(ac, anim_data, ads, data, filter_mode, (ID *)obact);
2239                         }
2240                                 break;
2241                                 
2242                         case ANIMCONT_SHAPEKEY: /* 'ShapeKey Editor' */
2243                         {
2244                                 /* the check for the DopeSheet summary is included here since the summary works here too */
2245                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2246                                         items= animdata_filter_shapekey(ac, anim_data, data, filter_mode);
2247                         }
2248                                 break;
2249                                 
2250                         case ANIMCONT_GPENCIL:
2251                         {
2252                                 items= animdata_filter_gpencil(anim_data, data, filter_mode);
2253                         }
2254                                 break;
2255                                 
2256                         case ANIMCONT_DOPESHEET: /* 'DopeSheet Editor' */
2257                         {
2258                                 /* the DopeSheet editor is the primary place where the DopeSheet summaries are useful */
2259                                 if (animdata_filter_dopesheet_summary(ac, anim_data, filter_mode, &items))
2260                                         items += animdata_filter_dopesheet(ac, anim_data, data, filter_mode);
2261                         }
2262                                 break;
2263                                 
2264                         case ANIMCONT_FCURVES: /* Graph Editor -> FCurves/Animation Editing */
2265                         case ANIMCONT_DRIVERS: /* Graph Editor -> Drivers Editing */
2266                         case ANIMCONT_NLA: /* NLA Editor */
2267                         {
2268                                 /* all of these editors use the basic DopeSheet data for filtering options, but don't have all the same features */
2269                                 items = animdata_filter_dopesheet(ac, anim_data, data, filter_mode);
2270                         }
2271                                 break;
2272                         
2273                         case ANIMCONT_CHANNEL: /* animation channel */
2274                         {
2275                                 bDopeSheet *ads = ac->ads;
2276                                 
2277                                 /* based on the channel type, filter relevant data for this */
2278                                 items = animdata_filter_animchan(ac, anim_data, ads, data, filter_mode);
2279                         }
2280                                 break;
2281                 }
2282                         
2283                 /* remove any 'weedy' entries */
2284                 items = animdata_filter_remove_invalid(anim_data);
2285                 
2286                 /* remove duplicates (if required) */
2287                 if (filter_mode & ANIMFILTER_NODUPLIS)
2288                         items = animdata_filter_remove_duplis(anim_data);
2289         }
2290         
2291         /* return the number of items in the list */
2292         return items;
2293 }
2294
2295 /* ************************************************************ */