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