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