Animato Bugfixes:
[blender.git] / source / blender / editors / animation / anim_filter.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Joshua Leung
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /* This file contains a system used to provide a layer of abstraction between sources
30  * of animation data and tools in Animation Editors. The method used here involves 
31  * generating a list of edit structures which enable tools to naively perform the actions 
32  * they require without all the boiler-plate associated with loops within loops and checking 
33  * for cases to ignore. 
34  *
35  * While this is primarily used for the Action/Dopesheet Editor (and its accessory modes),
36  * the IPO Editor also uses this for it's channel list and for determining which curves
37  * are being edited.
38  *
39  * Note: much of the original system this was based on was built before the creation of the RNA
40  * system. In future, it would be interesting to replace some parts of this code with RNA queries,
41  * however, RNA does not eliminate some of the boiler-plate reduction benefits presented by this 
42  * system, so if any such work does occur, it should only be used for the internals used here...
43  *
44  * -- Joshua Leung, Dec 2008
45  */
46
47 #include <string.h>
48 #include <stdio.h>
49
50 #include "DNA_listBase.h"
51 #include "DNA_ID.h"
52 #include "DNA_anim_types.h"
53 #include "DNA_action_types.h"
54 #include "DNA_constraint_types.h"
55 #include "DNA_camera_types.h"
56 #include "DNA_curve_types.h"
57 #include "DNA_gpencil_types.h"
58 #include "DNA_ipo_types.h"
59 #include "DNA_lamp_types.h"
60 #include "DNA_lattice_types.h"
61 #include "DNA_key_types.h"
62 #include "DNA_material_types.h"
63 #include "DNA_mesh_types.h"
64 #include "DNA_object_types.h"
65 #include "DNA_space_types.h"
66 #include "DNA_scene_types.h"
67 #include "DNA_screen_types.h"
68 #include "DNA_windowmanager_types.h"
69
70 #include "MEM_guardedalloc.h"
71
72 #include "BLI_blenlib.h"
73
74 #include "BKE_context.h"
75 #include "BKE_global.h"
76 #include "BKE_key.h"
77 #include "BKE_object.h"
78 #include "BKE_material.h"
79 #include "BKE_screen.h"
80 #include "BKE_utildefines.h"
81
82 #include "ED_anim_api.h"
83 #include "ED_types.h"
84 #include "ED_util.h"
85
86 #include "WM_api.h"
87 #include "WM_types.h"
88
89 #include "BIF_gl.h"
90 #include "BIF_glutil.h"
91
92 #include "UI_interface.h"
93 #include "UI_resources.h"
94 #include "UI_view2d.h"
95
96 /* ************************************************************ */
97 /* Blender Context <-> Animation Context mapping */
98
99 /* ----------- Private Stuff - Action Editor ------------- */
100
101 /* Get shapekey data being edited (for Action Editor -> ShapeKey mode) */
102 /* Note: there's a similar function in key.c (ob_get_key) */
103 Key *actedit_get_shapekeys (bAnimContext *ac, SpaceAction *saction) 
104 {
105     Scene *scene= ac->scene;
106     Object *ob;
107     Key *key;
108         
109     ob = OBACT;
110     if (ob == NULL) 
111                 return NULL;
112         
113         /* XXX pinning is not available in 'ShapeKey' mode... */
114         //if (saction->pin) return NULL;
115         
116         /* shapekey data is stored with geometry data */
117         switch (ob->type) {
118                 case OB_MESH:
119                         key= ((Mesh *)ob->data)->key;
120                         break;
121                         
122                 case OB_LATTICE:
123                         key= ((Lattice *)ob->data)->key;
124                         break;
125                         
126                 case OB_CURVE:
127                 case OB_SURF:
128                         key= ((Curve *)ob->data)->key;
129                         break;
130                         
131                 default:
132                         return NULL;
133         }
134         
135         if (key) {
136                 if (key->type == KEY_RELATIVE)
137                         return key;
138         }
139         
140     return NULL;
141 }
142
143 /* Get data being edited in Action Editor (depending on current 'mode') */
144 static short actedit_get_context (bAnimContext *ac, SpaceAction *saction)
145 {
146         /* sync settings with current view status, then return appropriate data */
147         switch (saction->mode) {
148                 case SACTCONT_ACTION: /* 'Action Editor' */
149                         /* if not pinned, sync with active object */
150                         if (saction->pin == 0) {
151                                 if (ac->obact && ac->obact->adt)
152                                         saction->action = ac->obact->adt->action;
153                                 else
154                                         saction->action= NULL;
155                         }
156                         
157                         ac->datatype= ANIMCONT_ACTION;
158                         ac->data= saction->action;
159                         
160                         ac->mode= saction->mode;
161                         return 1;
162                         
163                 case SACTCONT_SHAPEKEY: /* 'ShapeKey Editor' */
164                         ac->datatype= ANIMCONT_SHAPEKEY;
165                         ac->data= actedit_get_shapekeys(ac, saction);
166                         
167                         ac->mode= saction->mode;
168                         return 1;
169                         
170                 case SACTCONT_GPENCIL: /* Grease Pencil */ // XXX review how this mode is handled...
171                         ac->datatype=ANIMCONT_GPENCIL;
172                         //ac->data= CTX_wm_screen(C); // FIXME: add that dopesheet type thing here!
173                         ac->data= NULL; // !!!
174                         
175                         ac->mode= saction->mode;
176                         return 1;
177                         
178                 case SACTCONT_DOPESHEET: /* DopeSheet */
179                         /* update scene-pointer (no need to check for pinning yet, as not implemented) */
180                         saction->ads.source= (ID *)ac->scene;
181                         
182                         ac->datatype= ANIMCONT_DOPESHEET;
183                         ac->data= &saction->ads;
184                         
185                         ac->mode= saction->mode;
186                         return 1;
187                 
188                 default: /* unhandled yet */
189                         ac->datatype= ANIMCONT_NONE;
190                         ac->data= NULL;
191                         
192                         ac->mode= -1;
193                         return 0;
194         }
195 }
196
197 /* ----------- Private Stuff - IPO Editor ------------- */
198
199 /* Get data being edited in IPO Editor (depending on current 'mode') */
200 static short ipoedit_get_context (bAnimContext *ac, SpaceIpo *sipo)
201 {
202         // XXX FIXME...
203         return 0;
204 }
205
206 /* ----------- Public API --------------- */
207
208 /* Obtain current anim-data context, given that context info from Blender context has already been set 
209  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
210  *        allocation/freeing costs (which are not that avoidable with channels).
211  */
212 short ANIM_animdata_context_getdata (bAnimContext *ac)
213 {
214         ScrArea *sa= ac->sa;
215         short ok= 0;
216         
217         /* context depends on editor we are currently in */
218         if (sa) {
219                 switch (sa->spacetype) {
220                         case SPACE_ACTION:
221                         {
222                                 SpaceAction *saction= (SpaceAction *)sa->spacedata.first;
223                                 ok= actedit_get_context(ac, saction);
224                         }
225                                 break;
226                                 
227                         case SPACE_IPO:
228                         {
229                                 SpaceIpo *sipo= (SpaceIpo *)sa->spacedata.first;
230                                 ok= ipoedit_get_context(ac, sipo);
231                         }
232                                 break;
233                 }
234         }
235         
236         /* check if there's any valid data */
237         if (ok && ac->data)
238                 return 1;
239         else
240                 return 0;
241 }
242
243 /* Obtain current anim-data context from Blender Context info 
244  *      - AnimContext to write to is provided as pointer to var on stack so that we don't have
245  *        allocation/freeing costs (which are not that avoidable with channels).
246  *      - Clears data and sets the information from Blender Context which is useful
247  */
248 short ANIM_animdata_get_context (const bContext *C, bAnimContext *ac)
249 {
250         ScrArea *sa= CTX_wm_area(C);
251         ARegion *ar= CTX_wm_region(C);
252         Scene *scene= CTX_data_scene(C);
253         
254         /* clear old context info */
255         if (ac == NULL) return 0;
256         memset(ac, 0, sizeof(bAnimContext));
257         
258         /* get useful default context settings from context */
259         ac->scene= scene;
260         ac->obact= (scene && scene->basact)?  scene->basact->object : NULL;
261         ac->sa= sa;
262         ac->ar= ar;
263         ac->spacetype= (sa) ? sa->spacetype : 0;
264         ac->regiontype= (ar) ? ar->regiontype : 0;
265         
266         /* get data context info */
267         return ANIM_animdata_context_getdata(ac);
268 }
269
270 /* ************************************************************ */
271 /* Blender Data <-- Filter --> Channels to be operated on */
272
273 /* quick macro to test if AnimData is usable */
274 #define ANIMDATA_HAS_KEYS(id) ((id)->adt && (id)->adt->action)
275
276 /* ----------- 'Private' Stuff --------------- */
277
278 /* this function allocates memory for a new bAnimListElem struct for the 
279  * provided animation channel-data. 
280  */
281 bAnimListElem *make_new_animlistelem (void *data, short datatype, void *owner, short ownertype)
282 {
283         bAnimListElem *ale= NULL;
284         
285         /* only allocate memory if there is data to convert */
286         if (data) {
287                 /* allocate and set generic data */
288                 ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
289                 
290                 ale->data= data;
291                 ale->type= datatype;
292                         // XXX what is the point of the owner data?
293                 ale->owner= owner;
294                 ale->ownertype= ownertype;
295                 
296                 /* do specifics */
297                 switch (datatype) {
298                         case ANIMTYPE_OBJECT:
299                         {
300                                 Base *base= (Base *)data;
301                                 Object *ob= base->object;
302                                 
303                                 ale->flag= ob->flag;
304                                 
305                                 ale->key_data= ob;
306                                 ale->datatype= ALE_OB;
307                         }
308                                 break;
309                         case ANIMTYPE_FILLACTD:
310                         {
311                                 bAction *act= (bAction *)data;
312                                 
313                                 ale->flag= act->flag;
314                                 
315                                 ale->key_data= act;
316                                 ale->datatype= ALE_ACT;
317                         }
318                                 break;
319                         case ANIMTYPE_FILLMATD:
320                         {
321                                 Object *ob= (Object *)data;
322                                 
323                                 ale->flag= FILTER_MAT_OBJC(ob);
324                                 
325                                 ale->key_data= NULL;
326                                 ale->datatype= ALE_NONE;
327                         }
328                                 break;
329                         
330                         case ANIMTYPE_DSMAT:
331                         {
332                                 Material *ma= (Material *)data;
333                                 AnimData *adt= ma->adt;
334                                 
335                                 ale->flag= FILTER_MAT_OBJD(ma);
336                                 
337                                 ale->key_data= adt->action;
338                                 ale->datatype= ALE_ACT;
339                         }
340                                 break;
341                         case ANIMTYPE_DSLAM:
342                         {
343                                 Lamp *la= (Lamp *)data;
344                                 AnimData *adt= la->adt;
345                                 
346                                 ale->flag= FILTER_LAM_OBJD(la);
347                                 
348                                 ale->key_data= adt->action;
349                                 ale->datatype= ALE_ACT;
350                         }
351                                 break;
352                         case ANIMTYPE_DSCAM:
353                         {
354                                 Camera *ca= (Camera *)data;
355                                 AnimData *adt= ca->adt;
356                                 
357                                 ale->flag= FILTER_CAM_OBJD(ca);
358                                 
359                                 ale->key_data= adt->action;
360                                 ale->datatype= ALE_ACT;
361                         }
362                                 break;
363                         case ANIMTYPE_DSCUR:
364                         {
365                                 Curve *cu= (Curve *)data;
366                                 AnimData *adt= cu->adt;
367                                 
368                                 ale->flag= FILTER_CUR_OBJD(cu);
369                                 
370                                 ale->key_data= adt->action;
371                                 ale->datatype= ALE_ACT;
372                         }
373                                 break;
374                         case ANIMTYPE_DSSKEY:
375                         {
376                                 Key *key= (Key *)data;
377                                 AnimData *adt= key->adt;
378                                 
379                                 ale->flag= FILTER_SKE_OBJD(key); 
380                                 
381                                 ale->key_data= adt->action;
382                                 ale->datatype= ALE_ACT;
383                         }
384                                 break;
385                                 
386                         case ANIMTYPE_GROUP:
387                         {
388                                 bActionGroup *agrp= (bActionGroup *)data;
389                                 
390                                 ale->flag= agrp->flag;
391                                 
392                                 ale->key_data= NULL;
393                                 ale->datatype= ALE_GROUP;
394                         }
395                                 break;
396                         case ANIMTYPE_FCURVE:
397                         {
398                                 FCurve *fcu= (FCurve *)data;
399                                 
400                                 ale->flag= fcu->flag;
401                                 
402                                 ale->key_data= fcu;
403                                 ale->datatype= ALE_FCURVE;
404                         }
405                                 break;
406                         
407                         case ANIMTYPE_GPLAYER:
408                         {
409                                 bGPDlayer *gpl= (bGPDlayer *)data;
410                                 
411                                 ale->flag= gpl->flag;
412                                 
413                                 ale->key_data= NULL;
414                                 ale->datatype= ALE_GPFRAME;
415                         }
416                                 break;
417                 }
418         }
419         
420         /* return created datatype */
421         return ale;
422 }
423  
424 /* ----------------------------------------- */
425
426
427 static int animdata_filter_fcurves (ListBase *anim_data, FCurve *first, bActionGroup *grp, int filter_mode, ID *owner_id)
428 {
429         bAnimListElem *ale = NULL;
430         FCurve *fcu;
431         int items = 0;
432         
433         /* loop over F-Curves - assume that the caller of this has already checked that these should be included 
434          * NOTE: we need to check if the F-Curves belong to the same group, as this gets called for groups too...
435          */
436         for (fcu= first; ((fcu) && (fcu->grp==grp)); fcu= fcu->next) {
437                 /* only work with this channel and its subchannels if it is editable */
438                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_FCU(fcu)) {
439                         /* only include this curve if selected */
440                         if (!(filter_mode & ANIMFILTER_SEL) || (SEL_FCU(fcu))) {
441                                 /* owner/ownertype will be either object or action-channel, depending if it was dopesheet or part of an action */
442                                 ale= make_new_animlistelem(fcu, ANIMTYPE_FCURVE, fcu, ANIMTYPE_FCURVE);
443                                 
444                                 if (ale) {
445                                         /* ID will only be Object if data to write to directly belongs there, otherwise, another pointer will be used */
446                                         ale->id= owner_id;
447                                         BLI_addtail(anim_data, ale);
448                                         items++;
449                                 }
450                         }
451                 }
452         }
453         
454         /* return the number of items added to the list */
455         return items;
456 }
457
458 static int animdata_filter_action (ListBase *anim_data, bAction *act, int filter_mode, void *owner, short ownertype)
459 {
460         bAnimListElem *ale=NULL;
461         bActionGroup *agrp;
462         FCurve *lastchan=NULL;
463         short owned= (owner && ownertype) ? 1 : 0;
464         int items = 0;
465         
466         /* loop over groups */
467         //       XXX in future, we need to be prepared for nestled groups...
468         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
469                 /* add this group as a channel first */
470                 if ((filter_mode & ANIMFILTER_CHANNELS) || !(filter_mode & ANIMFILTER_CURVESONLY)) {
471                         /* check if filtering by selection */
472                         if ( !(filter_mode & ANIMFILTER_SEL) || SEL_AGRP(agrp) ) {
473                                 ale= make_new_animlistelem(agrp, ANIMTYPE_GROUP, NULL, ANIMTYPE_NONE);
474                                 if (ale) {
475                                         if (owned) ale->id= owner;
476                                         BLI_addtail(anim_data, ale);
477                                         items++;
478                                 }
479                         }
480                 }
481                 
482                 /* store reference to last channel of group */
483                 if (agrp->channels.last) 
484                         lastchan= agrp->channels.last;
485                 
486                 
487                 /* there are some situations, where only the channels of the animive group should get considered */
488                 if (!(filter_mode & ANIMFILTER_ACTGROUPED) || (agrp->flag & AGRP_ACTIVE)) {
489                         /* filters here are a bit convoulted...
490                          *      - groups show a "summary" of keyframes beside their name which must accessable for tools which handle keyframes
491                          *      - groups can be collapsed (and those tools which are only interested in channels rely on knowing that group is closed)
492                          *
493                          * cases when we should include F-Curves inside group:
494                          *      - we don't care about visibility
495                          *      - group is expanded
496                          *      - we're interested in keyframes, but not if they appear in selected channels
497                          */
498                         if ( (!(filter_mode & ANIMFILTER_VISIBLE) || EXPANDED_AGRP(agrp)) || 
499                                  ( (!(filter_mode & ANIMFILTER_SEL) || (SEL_AGRP(agrp))) && 
500                                    (filter_mode & ANIMFILTER_CURVESONLY) ) ) 
501                         {
502                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_AGRP(agrp)) {
503                                         // XXX the 'owner' info here needs review...
504                                         items += animdata_filter_fcurves(anim_data, agrp->channels.first, agrp, filter_mode, ((owned)?owner:NULL));
505                                         
506                                         /* remove group from filtered list if last element is group 
507                                          * (i.e. only if group had channels, which were all hidden)
508                                          */
509                                         // XXX this is really hacky... it should be fixed in a much more elegant way!
510                                         if ( (ale) && (anim_data->last == ale) && 
511                                                  (ale->data == agrp) && (agrp->channels.first) ) 
512                                         {
513                                                 BLI_freelinkN(anim_data, ale);
514                                                 items--;
515                                         }
516                                 }
517                         }
518                 }
519         }
520         
521         /* loop over un-grouped F-Curves (only if we're not only considering those channels in the animive group) */
522         if (!(filter_mode & ANIMFILTER_ACTGROUPED))  {
523                 // XXX the 'owner' info here needs review...
524                 items += animdata_filter_fcurves(anim_data, (lastchan)?(lastchan->next):(act->curves.first), NULL, filter_mode, ((owned)?owner:NULL));
525         }
526         
527         /* return the number of items added to the list */
528         return items;
529 }
530
531 static int animdata_filter_shapekey (ListBase *anim_data, Key *key, int filter_mode, void *owner, short ownertype)
532 {
533         bAnimListElem *ale;
534         KeyBlock *kb;
535         //FCurve *fcu;
536         short owned= (owner && ownertype)? 1 : 0;
537         int i, items=0;
538         
539         /* are we filtering for display or editing */
540         if (filter_mode & ANIMFILTER_CHANNELS) {
541                 /* for display - loop over shapekeys, adding ipo-curve references where needed */
542                 kb= key->block.first;
543                 
544                 /* loop through possible shapekeys, manually creating entries */
545                 for (i= 1; i < key->totkey; i++) {
546                         ale= MEM_callocN(sizeof(bAnimListElem), "bAnimListElem");
547                         kb = kb->next; /* do this even on the first try, as the first is 'Basis' (which doesn't get included) */
548                         
549                         ale->data= kb;
550                         ale->type= ANIMTYPE_SHAPEKEY; /* 'abused' usage of this type */
551                         ale->owner= key;
552                         ale->ownertype= ANIMTYPE_SHAPEKEY;
553                         ale->datatype= ALE_NONE;
554                         ale->index = i;
555                         
556 #if 0 // XXX fixme... old system
557                         if (key->ipo) {
558                                 for (icu= key->ipo->curve.first; icu; icu=icu->next) {
559                                         if (icu->adrcode == i) {
560                                                 ale->key_data= icu;
561                                                 ale->datatype= ALE_ICU;
562                                                 break;
563                                         }
564                                 }
565                         }
566 #endif // XXX fixme... old system
567                         
568                         if (owned) ale->id= owner;
569                         
570                         BLI_addtail(anim_data, ale);
571                         items++;
572                 }
573         }
574         else {
575 #if 0 // XXX fixme... old system
576                 /* loop over ipo curves if present - for editing */
577                 if (key->ipo) {
578                         if (filter_mode & ANIMFILTER_IPOKEYS) {
579                                 ale= make_new_animlistelem(key->ipo, ANIMTYPE_IPO, key, ANIMTYPE_SHAPEKEY);
580                                 if (ale) {
581                                         if (owned) ale->id= owner;
582                                         BLI_addtail(anim_data, ale);
583                                         items++;
584                                 }
585                         }
586                         else {
587                                 items += animdata_filter_ipocurves(anim_data, key->ipo, filter_mode, key, ANIMTYPE_SHAPEKEY, (owned)?(owner):(NULL));
588                         }
589                 }
590 #endif // XXX fixme... old system
591         }
592         
593         /* return the number of items added to the list */
594         return items;
595 }
596  
597 #if 0
598 // FIXME: switch this to use the bDopeSheet...
599 static int animdata_filter_gpencil (ListBase *anim_data, bScreen *sc, int filter_mode)
600 {
601         bAnimListElem *ale;
602         ScrArea *sa, *curarea;
603         bGPdata *gpd;
604         bGPDlayer *gpl;
605         int items = 0;
606         
607         /* check if filtering types are appropriate */
608         {
609                 /* special hack for fullscreen area (which must be this one then):
610                  *      - we use the curarea->full as screen to get spaces from, since the
611                  *        old (pre-fullscreen) screen was stored there...
612                  *      - this is needed as all data would otherwise disappear
613                  */
614                 // XXX need to get new alternative for curarea
615                 if ((curarea->full) && (curarea->spacetype==SPACE_ACTION))
616                         sc= curarea->full;
617                 
618                 /* loop over spaces in current screen, finding gpd blocks (could be slow!) */
619                 for (sa= sc->areabase.first; sa; sa= sa->next) {
620                         /* try to get gp data */
621                         // XXX need to put back grease pencil api...
622                         gpd= gpencil_data_getactive(sa);
623                         if (gpd == NULL) continue;
624                         
625                         /* add gpd as channel too (if for drawing, and it has layers) */
626                         if ((filter_mode & ANIMFILTER_CHANNELS) && (gpd->layers.first)) {
627                                 /* add to list */
628                                 ale= make_new_animlistelem(gpd, ANIMTYPE_GPDATABLOCK, sa, ANIMTYPE_SPECIALDATA);
629                                 if (ale) {
630                                         BLI_addtail(anim_data, ale);
631                                         items++;
632                                 }
633                         }
634                         
635                         /* only add layers if they will be visible (if drawing channels) */
636                         if ( !(filter_mode & ANIMFILTER_VISIBLE) || (EXPANDED_GPD(gpd)) ) {
637                                 /* loop over layers as the conditions are acceptable */
638                                 for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
639                                         /* only if selected */
640                                         if (!(filter_mode & ANIMFILTER_SEL) || SEL_GPL(gpl)) {
641                                                 /* only if editable */
642                                                 if (!(filter_mode & ANIMFILTER_FOREDIT) || EDITABLE_GPL(gpl)) {
643                                                         /* add to list */
644                                                         ale= make_new_animlistelem(gpl, ANIMTYPE_GPLAYER, gpd, ANIMTYPE_GPDATABLOCK);
645                                                         if (ale) {
646                                                                 BLI_addtail(anim_data, ale);
647                                                                 items++;
648                                                         }
649                                                 }
650                                         }
651                                 }
652                         }
653                 }
654         }
655         
656         /* return the number of items added to the list */
657         return items;
658 }
659 #endif 
660
661
662 static int animdata_filter_dopesheet_mats (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
663 {
664         bAnimListElem *ale=NULL;
665         Object *ob= base->object;
666         int items = 0;
667         
668         /* include materials-expand widget? */
669         if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
670                 ale= make_new_animlistelem(ob, ANIMTYPE_FILLMATD, base, ANIMTYPE_OBJECT);
671                 if (ale) {
672                         BLI_addtail(anim_data, ale);
673                         items++;
674                 }
675         }
676         
677         /* add materials? */
678         if (FILTER_MAT_OBJC(ob) || (filter_mode & ANIMFILTER_CURVESONLY)) {
679                 short a;
680                 
681                 /* for each material, either add channels separately, or as ipo-block */
682                 for (a=0; a<ob->totcol; a++) {
683                         Material *ma= give_current_material(ob, a);
684                         
685                         /* for now, if no material returned, skip (this shouldn't confuse the user I hope) */
686                         if (ELEM(NULL, ma, ma->adt)) continue;
687                         
688                         /* include material-expand widget? */
689                         // hmm... do we need to store the index of this material in the array anywhere?
690                         if (filter_mode & ANIMFILTER_CHANNELS) {
691                                 ale= make_new_animlistelem(ma, ANIMTYPE_DSMAT, base, ANIMTYPE_OBJECT);
692                                 if (ale) {
693                                         BLI_addtail(anim_data, ale);
694                                         items++;
695                                 }
696                         }
697                         
698                         /* add material's ipo-curve channels? */
699                         if (FILTER_MAT_OBJD(ma) || (filter_mode & ANIMFILTER_CURVESONLY)) {
700                                 //items += animdata_filter_ipocurves(anim_data, ma->ipo, filter_mode, base, ANIMTYPE_OBJECT, (ID *)ma);
701                         }
702                 }
703
704         }
705         
706         /* return the number of items added to the list */
707         return items;
708 }
709
710 #if 0 // XXX old anim sys
711
712 static int animdata_filter_dopesheet_cam (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
713 {
714         bAnimListElem *ale=NULL;
715         Object *ob= base->object;
716         Camera *ca= (Camera *)ob->data;
717         int items = 0;
718         
719         /* include camera-expand widget? */
720         if (filter_mode & (ANIMFILTER_CHANNELS|ANIMFILTER_IPOKEYS)) {
721                 ale= make_new_animlistelem(ca, ANIMTYPE_DSCAM, base, ANIMTYPE_OBJECT);
722                 if (ale) {
723                         BLI_addtail(anim_data, ale);
724                         items++;
725                 }
726         }
727         
728         /* add camera ipo-curve channels? */
729         if ( (FILTER_CAM_OBJD(ca) || (filter_mode & ANIMFILTER_ONLYFCU)) && 
730                   !(filter_mode & ANIMFILTER_IPOKEYS) ) 
731         {
732                 items += animdata_filter_ipocurves(anim_data, ca->ipo, filter_mode, base, ANIMTYPE_OBJECT, (ID *)ca);
733         }
734         
735         /* return the number of items added to the list */
736         return items;
737 }
738
739 static int animdata_filter_dopesheet_lamp (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
740 {
741         bAnimListElem *ale=NULL;
742         Object *ob= base->object;
743         Lamp *la= (Lamp *)ob->data;
744         int items = 0;
745         
746         /* include lamp-expand widget? */
747         if (filter_mode & (ANIMFILTER_CHANNELS|ANIMFILTER_IPOKEYS)) {
748                 ale= make_new_animlistelem(la, ANIMTYPE_DSLAM, base, ANIMTYPE_OBJECT);
749                 if (ale) {
750                         BLI_addtail(anim_data, ale);
751                         items++;
752                 }
753         }
754         
755         /* add lamp ipo-curve channels? */
756         if ( (FILTER_LAM_OBJD(la) || (filter_mode & ANIMFILTER_ONLYFCU)) && 
757                   !(filter_mode & ANIMFILTER_IPOKEYS) ) 
758         {
759                 items += animdata_filter_ipocurves(anim_data, la->ipo, filter_mode, base, ANIMTYPE_OBJECT, (ID *)la);
760         }
761         
762         /* return the number of items added to the list */
763         return items;
764 }
765
766 static int animdata_filter_dopesheet_curve (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
767 {
768         bAnimListElem *ale=NULL;
769         Object *ob= base->object;
770         Curve *cu= (Curve *)ob->data;
771         int items = 0;
772         
773         /* include curve-expand widget? */
774         if (filter_mode & (ANIMFILTER_CHANNELS|ANIMFILTER_IPOKEYS)) {
775                 ale= make_new_animlistelem(cu, ANIMTYPE_DSCUR, base, ANIMTYPE_OBJECT);
776                 if (ale) {
777                         BLI_addtail(anim_data, ale);
778                         items++;
779                 }
780         }
781         
782         /* add curve ipo-curve channels? */
783         if ( (FILTER_CUR_OBJD(cu) || (filter_mode & ANIMFILTER_ONLYFCU)) && 
784                   !(filter_mode & ANIMFILTER_IPOKEYS) ) 
785         {
786                 items += animdata_filter_ipocurves(anim_data, cu->ipo, filter_mode, base, ANIMTYPE_OBJECT, (ID *)cu);
787         }
788         
789         /* return the number of items added to the list */
790         return items;
791 }
792 #endif // XXX old anim sys
793
794 static int animdata_filter_dopesheet_ob (ListBase *anim_data, bDopeSheet *ads, Base *base, int filter_mode)
795 {
796         bAnimListElem *ale=NULL;
797         Scene *sce= (Scene *)ads->source;
798         Object *ob= base->object;
799 //      Key *key= ob_get_key(ob);
800         int items = 0;
801         
802         /* add this object as a channel first */
803         if ((filter_mode & ANIMFILTER_CURVESONLY) == 0) {
804                 /* check if filtering by selection */
805                 if ( !(filter_mode & ANIMFILTER_SEL) || ((base->flag & SELECT) || (base == sce->basact)) ) {
806                         ale= make_new_animlistelem(base, ANIMTYPE_OBJECT, NULL, ANIMTYPE_NONE);
807                         if (ale) {
808                                 BLI_addtail(anim_data, ale);
809                                 items++;
810                         }
811                 }
812         }
813         
814         /* if collapsed, don't go any further (unless adding keyframes only) */
815         if ( (EXPANDED_OBJC(ob) == 0) && !(filter_mode & ANIMFILTER_CURVESONLY) )
816                 return items;
817         
818         /* Action? */
819         if (ANIMDATA_HAS_KEYS(ob) /*&& !(ads->filterflag & ADS_FILTER_NOACTS)*/) {
820                 AnimData *adt= ob->adt;
821                 
822                 /* include action-expand widget? */
823                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
824                         ale= make_new_animlistelem(adt->action, ANIMTYPE_FILLACTD, base, ANIMTYPE_OBJECT);
825                         if (ale) {
826                                 ale->id= (ID *)ob; // err.... is this a good idea?
827                                 BLI_addtail(anim_data, ale);
828                                 items++;
829                         }
830                 }
831                 
832                 /* add F-Curve channels? */
833                 if (EXPANDED_ACTC(adt->action) || !(filter_mode & ANIMFILTER_CHANNELS)) {
834                         // need to make the ownertype normal object here... (maybe type should be a separate one for clarity?)
835                         items += animdata_filter_action(anim_data, adt->action, filter_mode, ob, ANIMTYPE_OBJECT); 
836                 }
837         }
838         
839 #if 0 // XXX fixme... 
840         /* ShapeKeys? */
841         if ((key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS)) {
842                 /* include shapekey-expand widget? */
843                 if ((filter_mode & ANIMFILTER_CHANNELS) && !(filter_mode & ANIMFILTER_CURVESONLY)) {
844                         ale= make_new_animlistelem(key, ANIMTYPE_DSSKEY, base, ANIMTYPE_OBJECT);
845                         if (ale) {
846                                 BLI_addtail(anim_data, ale);
847                                 items++;
848                         }
849                 }
850                 
851                 /* add channels */
852                 if (FILTER_SKE_OBJD(key) || (filter_mode & ANIMFILTER_CURVESONLY)) {
853                         items += animdata_filter_shapekey(anim_data, key, filter_mode, ob, ANIMTYPE_OBJECT);
854                 }
855         }
856 #endif
857         
858
859         /* Materials? */
860         if ((ob->totcol) && !(ads->filterflag & ADS_FILTER_NOMAT))
861                 items += animdata_filter_dopesheet_mats(anim_data, ads, base, filter_mode);
862         
863 #if 0
864         /* Object Data */
865         switch (ob->type) {
866                 case OB_CAMERA: /* ------- Camera ------------ */
867                 {
868                         Camera *ca= (Camera *)ob->data;
869                         if ((ca->ipo) && !(ads->filterflag & ADS_FILTER_NOCAM))
870                                 items += animdata_filter_dopesheet_cam(anim_data, ads, base, filter_mode);
871                 }
872                         break;
873                 case OB_LAMP: /* ---------- Lamp ----------- */
874                 {
875                         Lamp *la= (Lamp *)ob->data;
876                         if ((la->ipo) && !(ads->filterflag & ADS_FILTER_NOLAM))
877                                 items += animdata_filter_dopesheet_lamp(anim_data, ads, base, filter_mode);
878                 }
879                         break;
880                 case OB_CURVE: /* ------- Curve ---------- */
881                 {
882                         Curve *cu= (Curve *)ob->data;
883                         if ((cu->ipo) && !(ads->filterflag & ADS_FILTER_NOCUR))
884                                 items += animdata_filter_dopesheet_curve(anim_data, ads, base, filter_mode);
885                 }
886                         break;
887         }
888 #endif
889         
890         /* return the number of items added to the list */
891         return items;
892 }       
893
894 // 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)
895 static int animdata_filter_dopesheet (ListBase *anim_data, bDopeSheet *ads, int filter_mode)
896 {
897         Scene *sce= (Scene *)ads->source;
898         Base *base;
899         int items = 0;
900         
901         /* check that we do indeed have a scene */
902         if ((ads->source == NULL) || (GS(ads->source->name)!=ID_SCE)) {
903                 printf("DopeSheet Error: Not scene! \n");
904                 return 0;
905         }
906         
907         /* loop over all bases in the scene */
908         for (base= sce->base.first; base; base= base->next) {
909                 /* check if there's an object (all the relevant checks are done in the ob-function) */
910                 if (base->object) {
911                         Object *ob= base->object;
912                         Key *key= ob_get_key(ob);
913                         short actOk, keyOk, dataOk;
914                         
915                         /* firstly, check if object can be included, by the following fanimors:
916                          *      - if only visible, must check for layer and also viewport visibility
917                          *      - if only selected, must check if object is selected 
918                          *      - there must be animation data to edit
919                          */
920                         // TODO: if cache is implemented, just check name here, and then 
921                         if (filter_mode & ANIMFILTER_VISIBLE) {
922                                 /* layer visibility */
923                                 if ((ob->lay & sce->lay)==0) continue;
924                                 
925                                 /* outliner restrict-flag */
926                                 if (ob->restrictflag & OB_RESTRICT_VIEW) continue;
927                         }
928                         
929                         /* additionally, dopesheet filtering also affects what objects to consider */
930                         if (ads->filterflag) {
931                                 /* check selection and object type filters */
932                                 if ( (ads->filterflag & ADS_FILTER_ONLYSEL) && !((base->flag & SELECT) || (base == sce->basact)) )  {
933                                         /* only selected should be shown */
934                                         continue;
935                                 }
936 #if 0
937                                 if ((ads->filterflag & ADS_FILTER_NOARM) && (ob->type == OB_ARMATURE)) {
938                                         /* not showing armatures  */
939                                         continue;
940                                 }
941                                 if ((ads->filterflag & ADS_FILTER_NOOBJ) && (ob->type != OB_ARMATURE)) {
942                                         /* not showing objects that aren't armatures */
943                                         continue;
944                                 }
945 #endif
946                                 
947                                 /* check filters for datatypes */
948                                 actOk= (ANIMDATA_HAS_KEYS(ob) /*&& !(ads->filterflag & ADS_FILTER_NOACTS)*/);
949                                 keyOk= ((key) && !(ads->filterflag & ADS_FILTER_NOSHAPEKEYS));
950                                 
951                                 switch (ob->type) {
952                                         case OB_CAMERA: /* ------- Camera ------------ */
953                                         {
954                                                 Camera *ca= (Camera *)ob->data;
955                                                 dataOk= (ANIMDATA_HAS_KEYS(ca) && !(ads->filterflag & ADS_FILTER_NOCAM));                                               
956                                         }
957                                                 break;
958                                         case OB_LAMP: /* ---------- Lamp ----------- */
959                                         {
960                                                 Lamp *la= (Lamp *)ob->data;
961                                                 dataOk= (ANIMDATA_HAS_KEYS(la) && !(ads->filterflag & ADS_FILTER_NOLAM));
962                                         }
963                                                 break;
964                                         case OB_CURVE: /* -------- Curve ---------- */
965                                         {
966                                                 Curve *cu= (Curve *)ob->data;
967                                                 dataOk= (ANIMDATA_HAS_KEYS(cu) && !(ads->filterflag & ADS_FILTER_NOCUR));
968                                         }
969                                                 break;
970                                         default: /* --- other --- */
971                                                 dataOk= 0;
972                                                 break;
973                                 }
974                                 
975                                 /* check if all bad (i.e. nothing to show) */
976                                 if (!actOk && !keyOk && !dataOk)
977                                         continue;
978                         }
979                         else {
980                                 /* check data-types */
981                                 actOk= ANIMDATA_HAS_KEYS(ob);
982                                 keyOk= (key != NULL);
983                                 
984                                 switch (ob->type) {
985                                         case OB_CAMERA: /* ------- Camera ------------ */
986                                         {
987                                                 Camera *ca= (Camera *)ob->data;
988                                                 dataOk= ANIMDATA_HAS_KEYS(ca);                                          
989                                         }
990                                                 break;
991                                         case OB_LAMP: /* ---------- Lamp ----------- */
992                                         {
993                                                 Lamp *la= (Lamp *)ob->data;
994                                                 dataOk= ANIMDATA_HAS_KEYS(la);
995                                         }
996                                                 break;
997                                         case OB_CURVE: /* -------- Curve ---------- */
998                                         {
999                                                 Curve *cu= (Curve *)ob->data;
1000                                                 dataOk= ANIMDATA_HAS_KEYS(cu);
1001                                         }
1002                                                 break;
1003                                         default: /* --- other --- */
1004                                                 dataOk= 0;
1005                                                 break;
1006                                 }
1007                                 
1008                                 /* check if all bad (i.e. nothing to show) */
1009                                 if (!actOk && !keyOk && !dataOk)
1010                                         continue;
1011                         }
1012                         
1013                         /* since we're still here, this object should be usable */
1014                         items += animdata_filter_dopesheet_ob(anim_data, ads, base, filter_mode);
1015                 }
1016         }
1017         
1018         /* return the number of items in the list */
1019         return items;
1020 }
1021
1022 /* ----------- Public API --------------- */
1023
1024 /* This function filters the active data source to leave only animation channels suitable for
1025  * usage by the caller. It will return the length of the list 
1026  * 
1027  *      *act_data: is a pointer to a ListBase, to which the filtered animation channels
1028  *              will be placed for use.
1029  *      filter_mode: how should the data be filtered - bitmapping accessed flags
1030  */
1031 int ANIM_animdata_filter (ListBase *anim_data, int filter_mode, void *data, short datatype)
1032 {
1033         int items = 0;
1034         
1035         /* only filter data if there's somewhere to put it */
1036         if (data && anim_data) {
1037                 bAnimListElem *ale, *next;
1038                 
1039                 /* firstly filter the data */
1040                 switch (datatype) {
1041                         case ANIMCONT_ACTION:
1042                                 items= animdata_filter_action(anim_data, data, filter_mode, NULL, ANIMTYPE_NONE);
1043                                 break;
1044                         case ANIMCONT_SHAPEKEY:
1045                                 items= animdata_filter_shapekey(anim_data, data, filter_mode, NULL, ANIMTYPE_NONE);
1046                                 break;
1047                         case ANIMCONT_GPENCIL:
1048                                 //items= animdata_filter_gpencil(anim_data, data, filter_mode);
1049                                 break;
1050                         case ANIMCONT_DOPESHEET:
1051                                 items= animdata_filter_dopesheet(anim_data, data, filter_mode);
1052                                 break;
1053                                 
1054                         case ANIMCONT_IPO:
1055                                 // FIXME: this will be used for showing a single IPO-block (not too useful from animator perspective though!)
1056                                 //items= 0;
1057                                 break;
1058                 }
1059                         
1060                 /* remove any weedy entries */
1061                 // XXX this is weedy code!
1062                 for (ale= anim_data->first; ale; ale= next) {
1063                         next= ale->next;
1064                         
1065                         if (ale->type == ANIMTYPE_NONE) {
1066                                 items--;
1067                                 BLI_freelinkN(anim_data, ale);
1068                         }
1069                 }
1070         }
1071         
1072         /* return the number of items in the list */
1073         return items;
1074 }
1075
1076 /* ************************************************************ */