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