4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2009 Blender Foundation, Joshua Leung
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): Joshua Leung (full recode)
27 * ***** END GPL LICENSE BLOCK *****
36 #include "MEM_guardedalloc.h"
38 #include "BLI_blenlib.h"
39 #include "BLI_dynstr.h"
41 #include "DNA_anim_types.h"
42 #include "DNA_scene_types.h"
44 #include "BKE_animsys.h"
45 #include "BKE_action.h"
46 #include "BKE_fcurve.h"
48 #include "BKE_global.h"
50 #include "BKE_utildefines.h"
52 #include "RNA_access.h"
54 #include "nla_private.h"
56 /* ***************************************** */
59 /* Getter/Setter -------------------------------------------- */
61 /* Check if ID can have AnimData */
62 short id_type_can_have_animdata (ID *id)
68 /* Only some ID-blocks have this info for now */
69 // TODO: finish adding this for the other blocktypes
70 switch (GS(id->name)) {
73 case ID_ME: case ID_MB: case ID_CU: case ID_AR:
76 case ID_MA: case ID_TE: case ID_NT:
77 case ID_LA: case ID_CA: case ID_WO:
90 /* Get AnimData from the given ID-block. In order for this to work, we assume that
91 * the AnimData pointer is stored immediately after the given ID-block in the struct,
92 * as per IdAdtTemplate.
94 AnimData *BKE_animdata_from_id (ID *id)
96 /* only some ID-blocks have this info for now, so we cast the
97 * types that do to be of type IdAdtTemplate, and extract the
100 if (id_type_can_have_animdata(id)) {
101 IdAdtTemplate *iat= (IdAdtTemplate *)id;
108 /* Add AnimData to the given ID-block. In order for this to work, we assume that
109 * the AnimData pointer is stored immediately after the given ID-block in the struct,
110 * as per IdAdtTemplate. Also note that
112 AnimData *BKE_id_add_animdata (ID *id)
114 /* Only some ID-blocks have this info for now, so we cast the
115 * types that do to be of type IdAdtTemplate, and add the AnimData
116 * to it using the template
118 if (id_type_can_have_animdata(id)) {
119 IdAdtTemplate *iat= (IdAdtTemplate *)id;
121 /* check if there's already AnimData, in which case, don't add */
122 if (iat->adt == NULL) {
126 adt= iat->adt= MEM_callocN(sizeof(AnimData), "AnimData");
128 /* set default settings */
129 adt->act_influence= 1.0f;
138 /* Freeing -------------------------------------------- */
140 /* Free AnimData used by the nominated ID-block, and clear ID-block's AnimData pointer */
141 void BKE_free_animdata (ID *id)
143 /* Only some ID-blocks have this info for now, so we cast the
144 * types that do to be of type IdAdtTemplate
146 if (id_type_can_have_animdata(id)) {
147 IdAdtTemplate *iat= (IdAdtTemplate *)id;
148 AnimData *adt= iat->adt;
150 /* check if there's any AnimData to start with */
152 /* unlink action (don't free, as it's in its own list) */
154 adt->action->id.us--;
155 /* same goes for the temporarily displaced action */
157 adt->tmpact->id.us--;
160 free_nladata(&adt->nla_tracks);
162 /* free drivers - stored as a list of F-Curves */
163 free_fcurves(&adt->drivers);
168 /* free animdata now */
175 /* Freeing -------------------------------------------- */
177 /* Make a copy of the given AnimData - to be used when copying datablocks */
178 AnimData *BKE_copy_animdata (AnimData *adt)
182 /* sanity check before duplicating struct */
185 dadt= MEM_dupallocN(adt);
187 /* make a copy of action - at worst, user has to delete copies... */
188 dadt->action= copy_action(adt->action);
189 dadt->tmpact= copy_action(adt->tmpact);
191 /* duplicate NLA data */
192 copy_nladata(&dadt->nla_tracks, &adt->nla_tracks);
194 /* duplicate drivers (F-Curves) */
195 copy_fcurves(&dadt->drivers, &adt->drivers);
197 /* don't copy overrides */
198 dadt->overrides.first= dadt->overrides.last= NULL;
204 int BKE_copy_animdata_id(struct ID *id_to, struct ID *id_from)
208 if((id_to && id_from) && (GS(id_to->name) != GS(id_from->name)))
211 BKE_free_animdata(id_to);
213 adt = BKE_animdata_from_id(id_from);
215 IdAdtTemplate *iat = (IdAdtTemplate *)id_to;
216 iat->adt= BKE_copy_animdata(adt);
224 /* Make Local -------------------------------------------- */
226 static void make_local_strips(ListBase *strips)
230 for (strip=strips->first; strip; strip=strip->next) {
231 if (strip->act) make_local_action(strip->act);
232 if (strip->remap && strip->remap->target) make_local_action(strip->remap->target);
234 make_local_strips(&strip->strips);
238 /* Use local copy instead of linked copy of various ID-blocks */
239 void BKE_animdata_make_local(AnimData *adt)
243 /* Actions - Active and Temp */
244 if (adt->action) make_local_action(adt->action);
245 if (adt->tmpact) make_local_action(adt->tmpact);
247 if (adt->remap && adt->remap->target) make_local_action(adt->remap->target);
250 // TODO: need to remap the ID-targets too?
253 for (nlt=adt->nla_tracks.first; nlt; nlt=nlt->next)
254 make_local_strips(&nlt->strips);
257 /* Path Validation -------------------------------------------- */
259 /* Check if a given RNA Path is valid, by tracing it from the given ID, and seeing if we can resolve it */
260 static short check_rna_path_is_valid (ID *owner_id, char *path)
262 PointerRNA id_ptr, ptr;
263 PropertyRNA *prop=NULL;
265 /* make initial RNA pointer to start resolving from */
266 RNA_id_pointer_create(owner_id, &id_ptr);
269 return RNA_path_resolve(&id_ptr, path, &ptr, &prop);
272 /* Check if some given RNA Path needs fixing - free the given path and set a new one as appropriate
273 * NOTE: we assume that oldName and newName have [" "] padding around them
275 static char *rna_path_rename_fix (ID *owner_id, char *prefix, char *oldName, char *newName, char *oldpath, int verify_paths)
277 char *prefixPtr= strstr(oldpath, prefix);
278 char *oldNamePtr= strstr(oldpath, oldName);
279 int prefixLen= strlen(prefix);
280 int oldNameLen= strlen(oldName);
282 /* only start fixing the path if the prefix and oldName feature in the path,
283 * and prefix occurs immediately before oldName
285 if ( (prefixPtr && oldNamePtr) && (prefixPtr+prefixLen == oldNamePtr) ) {
286 /* if we haven't aren't able to resolve the path now, try again after fixing it */
287 if (!verify_paths || check_rna_path_is_valid(owner_id, oldpath) == 0) {
288 DynStr *ds= BLI_dynstr_new();
289 char *postfixPtr= oldNamePtr+oldNameLen;
290 char *newPath = NULL;
293 /* add the part of the string that goes up to the start of the prefix */
294 if (prefixPtr > oldpath) {
295 oldChar= prefixPtr[0];
297 BLI_dynstr_append(ds, oldpath);
298 prefixPtr[0]= oldChar;
302 BLI_dynstr_append(ds, prefix);
304 /* add the new name (complete with brackets) */
305 BLI_dynstr_append(ds, newName);
307 /* add the postfix */
308 BLI_dynstr_append(ds, postfixPtr);
310 /* create new path, and cleanup old data */
311 newPath= BLI_dynstr_get_cstring(ds);
314 /* check if the new path will solve our problems */
315 // TODO: will need to check whether this step really helps in practice
316 if (!verify_paths || check_rna_path_is_valid(owner_id, newPath)) {
317 /* free the old path, and return the new one, since we've solved the issues */
322 /* still couldn't resolve the path... so, might as well just leave it alone */
328 /* the old path doesn't need to be changed */
332 /* Check RNA-Paths for a list of F-Curves */
333 static void fcurves_path_rename_fix (ID *owner_id, char *prefix, char *oldName, char *newName, ListBase *curves, int verify_paths)
337 /* we need to check every curve... */
338 for (fcu= curves->first; fcu; fcu= fcu->next) {
339 /* firstly, handle the F-Curve's own path */
341 fcu->rna_path= rna_path_rename_fix(owner_id, prefix, oldName, newName, fcu->rna_path, verify_paths);
345 ChannelDriver *driver= fcu->driver;
348 /* driver variables */
349 for (dvar= driver->variables.first; dvar; dvar=dvar->next) {
350 /* only change the used targets, since the others will need fixing manually anyway */
351 DRIVER_TARGETS_USED_LOOPER(dvar)
353 /* rename RNA path */
355 dtar->rna_path= rna_path_rename_fix(dtar->id, prefix, oldName, newName, dtar->rna_path, verify_paths);
357 /* also fix the bone-name (if applicable) */
358 // XXX this has been disabled because the old/new names have padding which means this check will fail
359 //if ( ((dtar->id) && (GS(dtar->id->name) == ID_OB)) &&
360 // (dtar->pchan_name[0]) && (strcmp(oldName, dtar->pchan_name)==0) )
362 // BLI_strncpy(dtar->pchan_name, newName, sizeof(dtar->pchan_name));
365 DRIVER_TARGETS_LOOPER_END
371 /* Fix all RNA-Paths for Actions linked to NLA Strips */
372 static void nlastrips_path_rename_fix (ID *owner_id, char *prefix, char *oldName, char *newName, ListBase *strips, int verify_paths)
376 /* recursively check strips, fixing only actions... */
377 for (strip= strips->first; strip; strip= strip->next) {
378 /* fix strip's action */
380 fcurves_path_rename_fix(owner_id, prefix, oldName, newName, &strip->act->curves, verify_paths);
381 /* ignore own F-Curves, since those are local... */
383 /* check sub-strips (if metas) */
384 nlastrips_path_rename_fix(owner_id, prefix, oldName, newName, &strip->strips, verify_paths);
388 /* Fix all RNA-Paths in the AnimData block used by the given ID block
389 * NOTE: it is assumed that the structure we're replacing is <prefix><["><name><"]>
390 * i.e. pose.bones["Bone"]
392 void BKE_animdata_fix_paths_rename (ID *owner_id, AnimData *adt, char *prefix, char *oldName, char *newName, int oldSubscript, int newSubscript, int verify_paths)
397 /* if no AnimData, no need to proceed */
398 if (ELEM(NULL, owner_id, adt))
401 if (oldName != NULL && newName != NULL) {
402 /* pad the names with [" "] so that only exact matches are made */
403 oldN= BLI_sprintfN("[\"%s\"]", oldName);
404 newN= BLI_sprintfN("[\"%s\"]", newName);
406 oldN= BLI_sprintfN("[%d]", oldSubscript);
407 newN= BLI_sprintfN("[%d]", newSubscript);
410 /* Active action and temp action */
412 fcurves_path_rename_fix(owner_id, prefix, oldN, newN, &adt->action->curves, verify_paths);
414 fcurves_path_rename_fix(owner_id, prefix, oldN, newN, &adt->tmpact->curves, verify_paths);
416 /* Drivers - Drivers are really F-Curves */
417 fcurves_path_rename_fix(owner_id, prefix, oldN, newN, &adt->drivers, verify_paths);
419 /* NLA Data - Animation Data for Strips */
420 for (nlt= adt->nla_tracks.first; nlt; nlt= nlt->next)
421 nlastrips_path_rename_fix(owner_id, prefix, oldN, newN, &nlt->strips, verify_paths);
423 /* free the temp names */
428 /* Whole Database Ops -------------------------------------------- */
430 /* apply the given callback function on all data in main database */
431 void BKE_animdata_main_cb (Main *main, ID_AnimData_Edit_Callback func, void *user_data)
435 #define ANIMDATA_IDS_CB(first) \
436 for (id= first; id; id= id->next) { \
437 AnimData *adt= BKE_animdata_from_id(id); \
438 if (adt) func(id, adt, user_data); \
441 ANIMDATA_IDS_CB(main->nodetree.first); /* nodes */
442 ANIMDATA_IDS_CB(main->tex.first); /* textures */
443 ANIMDATA_IDS_CB(main->lamp.first); /* lamps */
444 ANIMDATA_IDS_CB(main->mat.first); /* materials */
445 ANIMDATA_IDS_CB(main->camera.first); /* cameras */
446 ANIMDATA_IDS_CB(main->key.first); /* shapekeys */
447 ANIMDATA_IDS_CB(main->mball.first); /* metaballs */
448 ANIMDATA_IDS_CB(main->curve.first); /* curves */
449 ANIMDATA_IDS_CB(main->armature.first); /* armatures */
450 ANIMDATA_IDS_CB(main->mesh.first); /* meshes */
451 ANIMDATA_IDS_CB(main->particle.first); /* particles */
452 ANIMDATA_IDS_CB(main->object.first); /* objects */
453 ANIMDATA_IDS_CB(main->world.first); /* worlds */
456 for (id= main->scene.first; id; id= id->next) {
457 AnimData *adt= BKE_animdata_from_id(id);
458 Scene *scene= (Scene *)id;
460 /* do compositing nodes first (since these aren't included in main tree) */
461 if (scene->nodetree) {
462 AnimData *adt2= BKE_animdata_from_id((ID *)scene->nodetree);
463 if (adt2) func(id, adt2, user_data);
466 /* now fix scene animation data as per normal */
467 if (adt) func((ID *)id, adt, user_data);
471 /* Fix all RNA-Paths throughout the database (directly access the Global.main version)
472 * NOTE: it is assumed that the structure we're replacing is <prefix><["><name><"]>
473 * i.e. pose.bones["Bone"]
475 /* TODO: use BKE_animdata_main_cb for looping over all data */
476 void BKE_all_animdata_fix_paths_rename (char *prefix, char *oldName, char *newName)
478 Main *mainptr= G.main;
481 /* macro for less typing
482 * - whether animdata exists is checked for by the main renaming callback, though taking
483 * this outside of the function may make things slightly faster?
485 #define RENAMEFIX_ANIM_IDS(first) \
486 for (id= first; id; id= id->next) { \
487 AnimData *adt= BKE_animdata_from_id(id); \
488 BKE_animdata_fix_paths_rename(id, adt, prefix, oldName, newName, 0, 0, 1);\
492 RENAMEFIX_ANIM_IDS(mainptr->nodetree.first);
495 RENAMEFIX_ANIM_IDS(mainptr->tex.first);
498 RENAMEFIX_ANIM_IDS(mainptr->lamp.first);
501 RENAMEFIX_ANIM_IDS(mainptr->mat.first);
504 RENAMEFIX_ANIM_IDS(mainptr->camera.first);
507 RENAMEFIX_ANIM_IDS(mainptr->key.first);
510 RENAMEFIX_ANIM_IDS(mainptr->mball.first);
513 RENAMEFIX_ANIM_IDS(mainptr->curve.first);
516 RENAMEFIX_ANIM_IDS(mainptr->armature.first);
522 RENAMEFIX_ANIM_IDS(mainptr->particle.first);
525 RENAMEFIX_ANIM_IDS(mainptr->object.first);
528 RENAMEFIX_ANIM_IDS(mainptr->world.first);
531 for (id= mainptr->scene.first; id; id= id->next) {
532 AnimData *adt= BKE_animdata_from_id(id);
533 Scene *scene= (Scene *)id;
535 /* do compositing nodes first (since these aren't included in main tree) */
536 if (scene->nodetree) {
537 AnimData *adt2= BKE_animdata_from_id((ID *)scene->nodetree);
538 BKE_animdata_fix_paths_rename((ID *)scene->nodetree, adt2, prefix, oldName, newName, 0, 0, 1);
541 /* now fix scene animation data as per normal */
542 BKE_animdata_fix_paths_rename((ID *)id, adt, prefix, oldName, newName, 0, 0, 1);
546 /* *********************************** */
549 /* Finding Tools --------------------------- */
551 /* Find the first path that matches the given criteria */
552 // TODO: do we want some method to perform partial matches too?
553 KS_Path *BKE_keyingset_find_path (KeyingSet *ks, ID *id, const char group_name[], const char rna_path[], int array_index, int group_mode)
558 if ELEM3(NULL, ks, rna_path, id)
561 /* loop over paths in the current KeyingSet, finding the first one where all settings match
562 * (i.e. the first one where none of the checks fail and equal 0)
564 for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
565 short eq_id=1, eq_path=1, eq_index=1, eq_group=1;
572 if ((ksp->rna_path==0) || strcmp(rna_path, ksp->rna_path))
575 /* index - need to compare whole-array setting too... */
576 if (ksp->array_index != array_index)
581 // FIXME: these checks need to be coded... for now, it's not too important though
584 /* if all aspects are ok, return */
585 if (eq_id && eq_path && eq_index && eq_group)
593 /* Defining Tools --------------------------- */
595 /* Used to create a new 'custom' KeyingSet for the user, that will be automatically added to the stack */
596 KeyingSet *BKE_keyingset_add (ListBase *list, const char name[], short flag, short keyingflag)
600 /* allocate new KeyingSet */
601 ks= MEM_callocN(sizeof(KeyingSet), "KeyingSet");
604 strncpy(ks->name, name, sizeof(ks->name));
606 strcpy(ks->name, "KeyingSet");
609 ks->keyingflag= keyingflag;
611 /* add KeyingSet to list */
612 BLI_addtail(list, ks);
614 /* make sure KeyingSet has a unique name (this helps with identification) */
615 BLI_uniquename(list, ks, "KeyingSet", '.', offsetof(KeyingSet, name), sizeof(ks->name));
617 /* return new KeyingSet for further editing */
621 /* Add a path to a KeyingSet. Nothing is returned for now...
622 * Checks are performed to ensure that destination is appropriate for the KeyingSet in question
624 KS_Path *BKE_keyingset_add_path (KeyingSet *ks, ID *id, const char group_name[], const char rna_path[], int array_index, short flag, short groupmode)
629 if ELEM(NULL, ks, rna_path) {
630 printf("ERROR: no Keying Set and/or RNA Path to add path with \n");
634 /* ID is required for all types of KeyingSets */
636 printf("ERROR: No ID provided for Keying Set Path. \n");
640 /* don't add if there is already a matching KS_Path in the KeyingSet */
641 if (BKE_keyingset_find_path(ks, id, group_name, rna_path, array_index, groupmode)) {
643 printf("ERROR: destination already exists in Keying Set \n");
647 /* allocate a new KeyingSet Path */
648 ksp= MEM_callocN(sizeof(KS_Path), "KeyingSet Path");
650 /* just store absolute info */
653 BLI_snprintf(ksp->group, 64, group_name);
655 strcpy(ksp->group, "");
657 /* store additional info for relative paths (just in case user makes the set relative) */
659 ksp->idtype= GS(id->name);
661 /* just copy path info */
662 // TODO: should array index be checked too?
663 ksp->rna_path= BLI_strdupn(rna_path, strlen(rna_path));
664 ksp->array_index= array_index;
668 ksp->groupmode= groupmode;
670 /* add KeyingSet path to KeyingSet */
671 BLI_addtail(&ks->paths, ksp);
673 /* return this path */
677 /* Free the given Keying Set path */
678 void BKE_keyingset_free_path (KeyingSet *ks, KS_Path *ksp)
681 if ELEM(NULL, ks, ksp)
684 /* free RNA-path info */
685 MEM_freeN(ksp->rna_path);
687 /* free path itself */
688 BLI_freelinkN(&ks->paths, ksp);
691 /* Copy all KeyingSets in the given list */
692 void BKE_keyingsets_copy (ListBase *newlist, ListBase *list)
697 BLI_duplicatelist(newlist, list);
699 for (ksn=newlist->first; ksn; ksn=ksn->next) {
700 BLI_duplicatelist(&ksn->paths, &ksn->paths);
702 for (kspn=ksn->paths.first; kspn; kspn=kspn->next)
703 kspn->rna_path= MEM_dupallocN(kspn->rna_path);
707 /* Freeing Tools --------------------------- */
709 /* Free data for KeyingSet but not set itself */
710 void BKE_keyingset_free (KeyingSet *ks)
718 /* free each path as we go to avoid looping twice */
719 for (ksp= ks->paths.first; ksp; ksp= kspn) {
721 BKE_keyingset_free_path(ks, ksp);
725 /* Free all the KeyingSets in the given list */
726 void BKE_keyingsets_free (ListBase *list)
734 /* loop over KeyingSets freeing them
735 * - BKE_keyingset_free() doesn't free the set itself, but it frees its sub-data
737 for (ks= list->first; ks; ks= ksn) {
739 BKE_keyingset_free(ks);
740 BLI_freelinkN(list, ks);
744 /* ***************************************** */
745 /* Evaluation Data-Setting Backend */
747 /* Retrieve string to act as RNA-path, adjusted using mapping-table if provided
748 * It returns whether the string needs to be freed (i.e. if it was a temp remapped one)
749 * // FIXME: maybe it would be faster if we didn't have to alloc/free strings like this all the time, but for now it's safer
751 * - remap: remapping table to use
752 * - path: original path string (as stored in F-Curve data)
753 * - dst: destination string to write data to
755 static short animsys_remap_path (AnimMapper *remap, char *path, char **dst)
757 /* is there a valid remapping table to use? */
759 /* find a matching entry... to use to remap */
763 /* nothing suitable found, so just set dst to look at path (i.e. no alloc/free needed) */
769 /* Write the given value to a setting using RNA, and return success */
770 static short animsys_write_rna_setting (PointerRNA *ptr, char *path, int array_index, float value)
772 // printf("%p %s %i %f\n", ptr, path, array_index, value);
777 /* get property to write to */
778 if (RNA_path_resolve(ptr, path, &new_ptr, &prop))
780 /* set value - only for animatable numerical values */
781 if (RNA_property_animateable(&new_ptr, prop))
783 int array_len= RNA_property_array_length(&new_ptr, prop);
785 if(array_len && array_index >= array_len)
788 printf("Animato: Invalid array index. ID = '%s', '%s[%d]', array length is %d \n",
789 (ptr && ptr->id.data) ? (((ID *)ptr->id.data)->name+2) : "<No ID>",
790 path, array_index, array_len-1);
796 switch (RNA_property_type(prop))
800 RNA_property_boolean_set_index(&new_ptr, prop, array_index, (int)value);
802 RNA_property_boolean_set(&new_ptr, prop, (int)value);
806 RNA_property_int_set_index(&new_ptr, prop, array_index, (int)value);
808 RNA_property_int_set(&new_ptr, prop, (int)value);
812 RNA_property_float_set_index(&new_ptr, prop, array_index, value);
814 RNA_property_float_set(&new_ptr, prop, value);
817 RNA_property_enum_set(&new_ptr, prop, (int)value);
820 /* nothing can be done here... so it is unsuccessful? */
829 /* failed to get path */
830 // XXX don't tag as failed yet though, as there are some legit situations (Action Constraint)
831 // where some channels will not exist, but shouldn't lock up Action
833 printf("Animato: Invalid path. ID = '%s', '%s[%d]' \n",
834 (ptr && ptr->id.data) ? (((ID *)ptr->id.data)->name+2) : "<No ID>",
841 /* Simple replacement based data-setting of the FCurve using RNA */
842 static short animsys_execute_fcurve (PointerRNA *ptr, AnimMapper *remap, FCurve *fcu)
848 /* get path, remapped as appropriate to work in its new environment */
849 free_path= animsys_remap_path(remap, fcu->rna_path, &path);
851 /* write value to setting */
853 ok= animsys_write_rna_setting(ptr, path, fcu->array_index, fcu->curval);
855 /* free temp path-info */
859 /* return whether we were successful */
863 /* Evaluate all the F-Curves in the given list
864 * This performs a set of standard checks. If extra checks are required, separate code should be used
866 static void animsys_evaluate_fcurves (PointerRNA *ptr, ListBase *list, AnimMapper *remap, float ctime)
870 /* calculate then execute each curve */
871 for (fcu= list->first; fcu; fcu= fcu->next)
873 /* check if this F-Curve doesn't belong to a muted group */
874 if ((fcu->grp == NULL) || (fcu->grp->flag & AGRP_MUTED)==0) {
875 /* check if this curve should be skipped */
876 if ((fcu->flag & (FCURVE_MUTED|FCURVE_DISABLED)) == 0)
878 calculate_fcurve(fcu, ctime);
879 animsys_execute_fcurve(ptr, remap, fcu);
885 /* ***************************************** */
886 /* Driver Evaluation */
888 /* Evaluate Drivers */
889 static void animsys_evaluate_drivers (PointerRNA *ptr, AnimData *adt, float ctime)
893 /* drivers are stored as F-Curves, but we cannot use the standard code, as we need to check if
894 * the depsgraph requested that this driver be evaluated...
896 for (fcu= adt->drivers.first; fcu; fcu= fcu->next)
898 ChannelDriver *driver= fcu->driver;
901 /* check if this driver's curve should be skipped */
902 if ((fcu->flag & (FCURVE_MUTED|FCURVE_DISABLED)) == 0)
904 /* check if driver itself is tagged for recalculation */
905 if ((driver) && !(driver->flag & DRIVER_FLAG_INVALID)/*&& (driver->flag & DRIVER_FLAG_RECALC)*/) { // XXX driver recalc flag is not set yet by depsgraph!
906 /* evaluate this using values set already in other places */
907 // NOTE: for 'layering' option later on, we should check if we should remove old value before adding new to only be done when drivers only changed
908 calculate_fcurve(fcu, ctime);
909 ok= animsys_execute_fcurve(ptr, NULL, fcu);
911 /* clear recalc flag */
912 driver->flag &= ~DRIVER_FLAG_RECALC;
914 /* set error-flag if evaluation failed */
916 driver->flag |= DRIVER_FLAG_INVALID;
922 /* ***************************************** */
923 /* Actions Evaluation */
925 /* Evaluate Action Group */
926 void animsys_evaluate_action_group (PointerRNA *ptr, bAction *act, bActionGroup *agrp, AnimMapper *remap, float ctime)
930 /* check if mapper is appropriate for use here (we set to NULL if it's inappropriate) */
931 if ELEM(NULL, act, agrp) return;
932 if ((remap) && (remap->target != act)) remap= NULL;
934 /* if group is muted, don't evaluated any of the F-Curve */
935 if (agrp->flag & AGRP_MUTED)
938 /* calculate then execute each curve */
939 for (fcu= agrp->channels.first; (fcu) && (fcu->grp == agrp); fcu= fcu->next)
941 /* check if this curve should be skipped */
942 if ((fcu->flag & (FCURVE_MUTED|FCURVE_DISABLED)) == 0)
944 calculate_fcurve(fcu, ctime);
945 animsys_execute_fcurve(ptr, remap, fcu);
950 /* Evaluate Action (F-Curve Bag) */
951 void animsys_evaluate_action (PointerRNA *ptr, bAction *act, AnimMapper *remap, float ctime)
953 /* check if mapper is appropriate for use here (we set to NULL if it's inappropriate) */
954 if (act == NULL) return;
955 if ((remap) && (remap->target != act)) remap= NULL;
957 /* calculate then execute each curve */
958 animsys_evaluate_fcurves(ptr, &act->curves, remap, ctime);
961 /* ***************************************** */
962 /* NLA System - Evaluation */
964 /* calculate influence of strip based for given frame based on blendin/out values */
965 static float nlastrip_get_influence (NlaStrip *strip, float cframe)
967 /* sanity checks - normalise the blendin/out values? */
968 strip->blendin= (float)fabs(strip->blendin);
969 strip->blendout= (float)fabs(strip->blendout);
971 /* result depends on where frame is in respect to blendin/out values */
972 if (IS_EQ(strip->blendin, 0)==0 && (cframe <= (strip->start + strip->blendin))) {
973 /* there is some blend-in */
974 return (float)fabs(cframe - strip->start) / (strip->blendin);
976 else if (IS_EQ(strip->blendout, 0)==0 && (cframe >= (strip->end - strip->blendout))) {
977 /* there is some blend-out */
978 return (float)fabs(strip->end - cframe) / (strip->blendout);
981 /* in the middle of the strip, we should be full strength */
986 /* evaluate the evaluation time and influence for the strip, storing the results in the strip */
987 static void nlastrip_evaluate_controls (NlaStrip *strip, float ctime)
989 /* firstly, analytically generate values for influence and time (if applicable) */
990 if ((strip->flag & NLASTRIP_FLAG_USR_TIME) == 0)
991 strip->strip_time= nlastrip_get_frame(strip, ctime, NLATIME_CONVERT_EVAL);
992 if ((strip->flag & NLASTRIP_FLAG_USR_INFLUENCE) == 0)
993 strip->influence= nlastrip_get_influence(strip, ctime);
995 /* now strip's evaluate F-Curves for these settings (if applicable) */
996 if (strip->fcurves.first) {
997 PointerRNA strip_ptr;
999 /* create RNA-pointer needed to set values */
1000 RNA_pointer_create(NULL, &RNA_NlaStrip, strip, &strip_ptr);
1002 /* execute these settings as per normal */
1003 animsys_evaluate_fcurves(&strip_ptr, &strip->fcurves, NULL, ctime);
1006 if (strip->flag & NLASTRIP_FLAG_USR_TIME && strip->flag & NLASTRIP_FLAG_USR_TIME_CYCLIC)
1007 strip->strip_time= fmod(strip->strip_time - strip->actstart, strip->actend - strip->actstart);
1010 /* gets the strip active at the current time for a list of strips for evaluation purposes */
1011 NlaEvalStrip *nlastrips_ctime_get_strip (ListBase *list, ListBase *strips, short index, float ctime)
1013 NlaStrip *strip, *estrip=NULL;
1017 /* loop over strips, checking if they fall within the range */
1018 for (strip= strips->first; strip; strip= strip->next) {
1019 /* check if current time occurs within this strip */
1020 if (IN_RANGE_INCL(ctime, strip->start, strip->end)) {
1021 /* this strip is active, so try to use it */
1023 side= NES_TIME_WITHIN;
1027 /* if time occurred before current strip... */
1028 if (ctime < strip->start) {
1029 if (strip == strips->first) {
1030 /* before first strip - only try to use it if it extends backwards in time too */
1031 if (strip->extendmode == NLASTRIP_EXTEND_HOLD)
1034 /* side is 'before' regardless of whether there's a useful strip */
1035 side= NES_TIME_BEFORE;
1038 /* before next strip - previous strip has ended, but next hasn't begun,
1039 * so blending mode depends on whether strip is being held or not...
1040 * - only occurs when no transition strip added, otherwise the transition would have
1041 * been picked up above...
1045 if (strip->extendmode != NLASTRIP_EXTEND_NOTHING)
1047 side= NES_TIME_AFTER;
1052 /* if time occurred after current strip... */
1053 if (ctime > strip->end) {
1054 /* only if this is the last strip should we do anything, and only if that is being held */
1055 if (strip == strips->last) {
1056 if (strip->extendmode != NLASTRIP_EXTEND_NOTHING)
1059 side= NES_TIME_AFTER;
1063 /* otherwise, skip... as the 'before' case will catch it more elegantly! */
1067 /* check if a valid strip was found
1068 * - must not be muted (i.e. will have contribution
1070 if ((estrip == NULL) || (estrip->flag & NLASTRIP_FLAG_MUTED))
1073 /* if ctime was not within the boundaries of the strip, clamp! */
1075 case NES_TIME_BEFORE: /* extend first frame only */
1076 ctime= estrip->start;
1078 case NES_TIME_AFTER: /* extend last frame only */
1083 /* evaluate strip's evaluation controls
1084 * - skip if no influence (i.e. same effect as muting the strip)
1085 * - negative influence is not supported yet... how would that be defined?
1087 // TODO: this sounds a bit hacky having a few isolated F-Curves stuck on some data it operates on...
1088 nlastrip_evaluate_controls(estrip, ctime);
1089 if (estrip->influence <= 0.0f)
1092 /* check if strip has valid data to evaluate,
1093 * and/or perform any additional type-specific actions
1095 switch (estrip->type) {
1096 case NLASTRIP_TYPE_CLIP:
1097 /* clip must have some action to evaluate */
1098 if (estrip->act == NULL)
1101 case NLASTRIP_TYPE_TRANSITION:
1102 /* there must be strips to transition from and to (i.e. prev and next required) */
1103 if (ELEM(NULL, estrip->prev, estrip->next))
1106 /* evaluate controls for the relevant extents of the bordering strips... */
1107 nlastrip_evaluate_controls(estrip->prev, estrip->start);
1108 nlastrip_evaluate_controls(estrip->next, estrip->end);
1112 /* add to list of strips we need to evaluate */
1113 nes= MEM_callocN(sizeof(NlaEvalStrip), "NlaEvalStrip");
1116 nes->strip_mode= side;
1117 nes->track_index= index;
1118 nes->strip_time= estrip->strip_time;
1121 BLI_addtail(list, nes);
1126 /* ---------------------- */
1128 /* find an NlaEvalChannel that matches the given criteria
1129 * - ptr and prop are the RNA data to find a match for
1131 static NlaEvalChannel *nlaevalchan_find_match (ListBase *channels, PointerRNA *ptr, PropertyRNA *prop, int array_index)
1133 NlaEvalChannel *nec;
1136 if (channels == NULL)
1139 /* loop through existing channels, checking for a channel which affects the same property */
1140 for (nec= channels->first; nec; nec= nec->next) {
1141 /* - comparing the PointerRNA's is done by comparing the pointers
1142 * to the actual struct the property resides in, since that all the
1143 * other data stored in PointerRNA cannot allow us to definitively
1146 if ((nec->ptr.data == ptr->data) && (nec->prop == prop) && (nec->index == array_index))
1154 /* verify that an appropriate NlaEvalChannel for this F-Curve exists */
1155 static NlaEvalChannel *nlaevalchan_verify (PointerRNA *ptr, ListBase *channels, NlaEvalStrip *nes, FCurve *fcu, short *newChan)
1157 NlaEvalChannel *nec;
1158 NlaStrip *strip= nes->strip;
1165 if (channels == NULL)
1168 /* get RNA pointer+property info from F-Curve for more convenient handling */
1169 /* get path, remapped as appropriate to work in its new environment */
1170 free_path= animsys_remap_path(strip->remap, fcu->rna_path, &path);
1172 /* a valid property must be available, and it must be animateable */
1173 if (RNA_path_resolve(ptr, path, &new_ptr, &prop) == 0) {
1174 if (G.f & G_DEBUG) printf("NLA Strip Eval: Cannot resolve path \n");
1177 /* only ok if animateable */
1178 else if (RNA_property_animateable(&new_ptr, prop) == 0) {
1179 if (G.f & G_DEBUG) printf("NLA Strip Eval: Property not animateable \n");
1183 /* try to find a match */
1184 nec= nlaevalchan_find_match(channels, &new_ptr, prop, fcu->array_index);
1186 /* allocate a new struct for this if none found */
1188 nec= MEM_callocN(sizeof(NlaEvalChannel), "NlaEvalChannel");
1190 BLI_addtail(channels, nec);
1194 nec->index= fcu->array_index;
1199 /* we can now return */
1203 /* accumulate (i.e. blend) the given value on to the channel it affects */
1204 static void nlaevalchan_accumulate (NlaEvalChannel *nec, NlaEvalStrip *nes, short newChan, float value)
1206 NlaStrip *strip= nes->strip;
1207 short blendmode= strip->blendmode;
1208 float inf= strip->influence;
1210 /* if channel is new, just store value regardless of blending factors, etc. */
1216 /* if this is being performed as part of transition evaluation, incorporate
1217 * an additional weighting factor for the influence
1219 if (nes->strip_mode == NES_TIME_TRANSITION_END)
1220 inf *= nes->strip_time;
1222 /* premultiply the value by the weighting factor */
1223 if (IS_EQ(inf, 0)) return;
1226 /* perform blending */
1227 switch (blendmode) {
1228 case NLASTRIP_MODE_ADD:
1229 /* simply add the scaled value on to the stack */
1230 nec->value += value;
1233 case NLASTRIP_MODE_SUBTRACT:
1234 /* simply subtract the scaled value from the stack */
1235 nec->value -= value;
1238 case NLASTRIP_MODE_MULTIPLY:
1239 /* multiply the scaled value with the stack */
1240 nec->value *= value;
1243 case NLASTRIP_MODE_REPLACE:
1244 default: // TODO: do we really want to blend by default? it seems more uses might prefer add...
1245 /* do linear interpolation
1246 * - the influence of the accumulated data (elsewhere, that is called dstweight)
1247 * is 1 - influence, since the strip's influence is srcweight
1249 nec->value= nec->value * (1.0f - inf) + value;
1254 /* accumulate the results of a temporary buffer with the results of the full-buffer */
1255 static void nlaevalchan_buffers_accumulate (ListBase *channels, ListBase *tmp_buffer, NlaEvalStrip *nes)
1257 NlaEvalChannel *nec, *necn, *necd;
1259 /* optimise - abort if no channels */
1260 if (tmp_buffer->first == NULL)
1263 /* accumulate results in tmp_channels buffer to the accumulation buffer */
1264 for (nec= tmp_buffer->first; nec; nec= necn) {
1265 /* get pointer to next channel in case we remove the current channel from the temp-buffer */
1268 /* try to find an existing matching channel for this setting in the accumulation buffer */
1269 necd= nlaevalchan_find_match(channels, &nec->ptr, nec->prop, nec->index);
1271 /* if there was a matching channel already in the buffer, accumulate to it,
1272 * otherwise, add the current channel to the buffer for efficiency
1275 nlaevalchan_accumulate(necd, nes, 0, nec->value);
1277 BLI_remlink(tmp_buffer, nec);
1278 BLI_addtail(channels, nec);
1282 /* free temp-channels that haven't been assimilated into the buffer */
1283 BLI_freelistN(tmp_buffer);
1286 /* ---------------------- */
1287 /* F-Modifier stack joining/separation utilities - should we generalise these for BLI_listbase.h interface? */
1289 /* Temporarily join two lists of modifiers together, storing the result in a third list */
1290 static void nlaeval_fmodifiers_join_stacks (ListBase *result, ListBase *list1, ListBase *list2)
1292 FModifier *fcm1, *fcm2;
1294 /* if list1 is invalid... */
1295 if ELEM(NULL, list1, list1->first) {
1296 if (list2 && list2->first) {
1297 result->first= list2->first;
1298 result->last= list2->last;
1301 /* if list 2 is invalid... */
1302 else if ELEM(NULL, list2, list2->first) {
1303 result->first= list1->first;
1304 result->last= list1->last;
1307 /* list1 should be added first, and list2 second, with the endpoints of these being the endpoints for result
1308 * - the original lists must be left unchanged though, as we need that fact for restoring
1310 result->first= list1->first;
1311 result->last= list2->last;
1321 /* Split two temporary lists of modifiers */
1322 static void nlaeval_fmodifiers_split_stacks (ListBase *list1, ListBase *list2)
1324 FModifier *fcm1, *fcm2;
1326 /* if list1/2 is invalid... just skip */
1327 if ELEM(NULL, list1, list2)
1329 if ELEM(NULL, list1->first, list2->first)
1336 /* clear their links */
1341 /* ---------------------- */
1343 /* evaluate action-clip strip */
1344 static void nlastrip_evaluate_actionclip (PointerRNA *ptr, ListBase *channels, ListBase *modifiers, NlaEvalStrip *nes)
1346 ListBase tmp_modifiers = {NULL, NULL};
1347 NlaStrip *strip= nes->strip;
1351 /* join this strip's modifiers to the parent's modifiers (own modifiers first) */
1352 nlaeval_fmodifiers_join_stacks(&tmp_modifiers, &strip->modifiers, modifiers);
1354 /* evaluate strip's modifiers which modify time to evaluate the base curves at */
1355 evaltime= evaluate_time_fmodifiers(&tmp_modifiers, NULL, 0.0f, strip->strip_time);
1357 /* evaluate all the F-Curves in the action, saving the relevant pointers to data that will need to be used */
1358 for (fcu= strip->act->curves.first; fcu; fcu= fcu->next) {
1359 NlaEvalChannel *nec;
1363 /* check if this curve should be skipped */
1364 if (fcu->flag & (FCURVE_MUTED|FCURVE_DISABLED))
1366 if ((fcu->grp) && (fcu->grp->flag & AGRP_MUTED))
1369 /* evaluate the F-Curve's value for the time given in the strip
1370 * NOTE: we use the modified time here, since strip's F-Curve Modifiers are applied on top of this
1372 value= evaluate_fcurve(fcu, evaltime);
1374 /* apply strip's F-Curve Modifiers on this value
1375 * NOTE: we apply the strip's original evaluation time not the modified one (as per standard F-Curve eval)
1377 evaluate_value_fmodifiers(&tmp_modifiers, fcu, &value, strip->strip_time);
1380 /* get an NLA evaluation channel to work with, and accumulate the evaluated value with the value(s)
1381 * stored in this channel if it has been used already
1383 nec= nlaevalchan_verify(ptr, channels, nes, fcu, &newChan);
1385 nlaevalchan_accumulate(nec, nes, newChan, value);
1388 /* unlink this strip's modifiers from the parent's modifiers again */
1389 nlaeval_fmodifiers_split_stacks(&strip->modifiers, modifiers);
1392 /* evaluate transition strip */
1393 static void nlastrip_evaluate_transition (PointerRNA *ptr, ListBase *channels, ListBase *modifiers, NlaEvalStrip *nes)
1395 ListBase tmp_channels = {NULL, NULL};
1396 ListBase tmp_modifiers = {NULL, NULL};
1397 NlaEvalStrip tmp_nes;
1400 /* join this strip's modifiers to the parent's modifiers (own modifiers first) */
1401 nlaeval_fmodifiers_join_stacks(&tmp_modifiers, &nes->strip->modifiers, modifiers);
1403 /* get the two strips to operate on
1404 * - we use the endpoints of the strips directly flanking our strip
1405 * using these as the endpoints of the transition (destination and source)
1406 * - these should have already been determined to be valid...
1407 * - if this strip is being played in reverse, we need to swap these endpoints
1408 * otherwise they will be interpolated wrong
1410 if (nes->strip->flag & NLASTRIP_FLAG_REVERSE) {
1411 s1= nes->strip->next;
1412 s2= nes->strip->prev;
1415 s1= nes->strip->prev;
1416 s2= nes->strip->next;
1419 /* prepare template for 'evaluation strip'
1420 * - based on the transition strip's evaluation strip data
1421 * - strip_mode is NES_TIME_TRANSITION_* based on which endpoint
1422 * - strip_time is the 'normalised' (i.e. in-strip) time for evaluation,
1423 * which doubles up as an additional weighting factor for the strip influences
1424 * which allows us to appear to be 'interpolating' between the two extremes
1428 /* evaluate these strips into a temp-buffer (tmp_channels) */
1429 // FIXME: modifier evalation here needs some work...
1431 tmp_nes.strip_mode= NES_TIME_TRANSITION_START;
1433 nlastrip_evaluate(ptr, &tmp_channels, &tmp_modifiers, &tmp_nes);
1436 tmp_nes.strip_mode= NES_TIME_TRANSITION_END;
1438 nlastrip_evaluate(ptr, &tmp_channels, &tmp_modifiers, &tmp_nes);
1441 /* assumulate temp-buffer and full-buffer, using the 'real' strip */
1442 nlaevalchan_buffers_accumulate(channels, &tmp_channels, nes);
1444 /* unlink this strip's modifiers from the parent's modifiers again */
1445 nlaeval_fmodifiers_split_stacks(&nes->strip->modifiers, modifiers);
1448 /* evaluate meta-strip */
1449 static void nlastrip_evaluate_meta (PointerRNA *ptr, ListBase *channels, ListBase *modifiers, NlaEvalStrip *nes)
1451 ListBase tmp_channels = {NULL, NULL};
1452 ListBase tmp_modifiers = {NULL, NULL};
1453 NlaStrip *strip= nes->strip;
1454 NlaEvalStrip *tmp_nes;
1457 /* meta-strip was calculated normally to have some time to be evaluated at
1458 * and here we 'look inside' the meta strip, treating it as a decorated window to
1459 * it's child strips, which get evaluated as if they were some tracks on a strip
1460 * (but with some extra modifiers to apply).
1462 * NOTE: keep this in sync with animsys_evaluate_nla()
1465 /* join this strip's modifiers to the parent's modifiers (own modifiers first) */
1466 nlaeval_fmodifiers_join_stacks(&tmp_modifiers, &strip->modifiers, modifiers);
1468 /* find the child-strip to evaluate */
1469 evaltime= (nes->strip_time * (strip->end - strip->start)) + strip->start;
1470 tmp_nes= nlastrips_ctime_get_strip(NULL, &strip->strips, -1, evaltime);
1471 if (tmp_nes == NULL)
1474 /* evaluate child-strip into tmp_channels buffer before accumulating
1475 * in the accumulation buffer
1477 nlastrip_evaluate(ptr, &tmp_channels, &tmp_modifiers, tmp_nes);
1479 /* assumulate temp-buffer and full-buffer, using the 'real' strip */
1480 nlaevalchan_buffers_accumulate(channels, &tmp_channels, nes);
1482 /* free temp eval-strip */
1485 /* unlink this strip's modifiers from the parent's modifiers again */
1486 nlaeval_fmodifiers_split_stacks(&strip->modifiers, modifiers);
1489 /* evaluates the given evaluation strip */
1490 void nlastrip_evaluate (PointerRNA *ptr, ListBase *channels, ListBase *modifiers, NlaEvalStrip *nes)
1492 NlaStrip *strip= nes->strip;
1494 /* to prevent potential infinite recursion problems (i.e. transition strip, beside meta strip containing a transition
1495 * several levels deep inside it), we tag the current strip as being evaluated, and clear this when we leave
1497 // TODO: be careful with this flag, since some edit tools may be running and have set this while animplayback was running
1498 if (strip->flag & NLASTRIP_FLAG_EDIT_TOUCHED)
1500 strip->flag |= NLASTRIP_FLAG_EDIT_TOUCHED;
1502 /* actions to take depend on the type of strip */
1503 switch (strip->type) {
1504 case NLASTRIP_TYPE_CLIP: /* action-clip */
1505 nlastrip_evaluate_actionclip(ptr, channels, modifiers, nes);
1507 case NLASTRIP_TYPE_TRANSITION: /* transition */
1508 nlastrip_evaluate_transition(ptr, channels, modifiers, nes);
1510 case NLASTRIP_TYPE_META: /* meta */
1511 nlastrip_evaluate_meta(ptr, channels, modifiers, nes);
1515 /* clear temp recursion safe-check */
1516 strip->flag &= ~NLASTRIP_FLAG_EDIT_TOUCHED;
1519 /* write the accumulated settings to */
1520 void nladata_flush_channels (ListBase *channels)
1522 NlaEvalChannel *nec;
1525 if (channels == NULL)
1528 /* for each channel with accumulated values, write its value on the property it affects */
1529 for (nec= channels->first; nec; nec= nec->next) {
1530 PointerRNA *ptr= &nec->ptr;
1531 PropertyRNA *prop= nec->prop;
1532 int array_index= nec->index;
1533 float value= nec->value;
1535 /* write values - see animsys_write_rna_setting() to sync the code */
1536 switch (RNA_property_type(prop))
1539 if (RNA_property_array_length(ptr, prop))
1540 RNA_property_boolean_set_index(ptr, prop, array_index, (int)value);
1542 RNA_property_boolean_set(ptr, prop, (int)value);
1545 if (RNA_property_array_length(ptr, prop))
1546 RNA_property_int_set_index(ptr, prop, array_index, (int)value);
1548 RNA_property_int_set(ptr, prop, (int)value);
1551 if (RNA_property_array_length(ptr, prop))
1552 RNA_property_float_set_index(ptr, prop, array_index, value);
1554 RNA_property_float_set(ptr, prop, value);
1557 RNA_property_enum_set(ptr, prop, (int)value);
1560 // can't do anything with other types of property....
1566 /* ---------------------- */
1568 /* NLA Evaluation function (mostly for use through do_animdata)
1569 * - All channels that will be affected are not cleared anymore. Instead, we just evaluate into
1570 * some temp channels, where values can be accumulated in one go.
1572 static void animsys_evaluate_nla (PointerRNA *ptr, AnimData *adt, float ctime)
1574 ListBase dummy_trackslist = {NULL, NULL};
1575 NlaStrip dummy_strip;
1578 short track_index=0;
1579 short has_strips = 0;
1581 ListBase estrips= {NULL, NULL};
1582 ListBase echannels= {NULL, NULL};
1585 // TODO: need to zero out all channels used, otherwise we have problems with threadsafety
1586 // and also when the user jumps between different times instead of moving sequentially...
1588 /* 1. get the stack of strips to evaluate at current time (influence calculated here) */
1589 for (nlt=adt->nla_tracks.first; nlt; nlt=nlt->next, track_index++) {
1590 /* if tweaking is on and this strip is the tweaking track, stop on this one */
1591 if ((adt->flag & ADT_NLA_EDIT_ON) && (nlt->flag & NLATRACK_DISABLED))
1594 /* skip if we're only considering a track tagged 'solo' */
1595 if ((adt->flag & ADT_NLA_SOLO_TRACK) && (nlt->flag & NLATRACK_SOLO)==0)
1597 /* skip if track is muted */
1598 if (nlt->flag & NLATRACK_MUTED)
1601 /* if this track has strips (but maybe they won't be suitable), set has_strips
1602 * - used for mainly for still allowing normal action evaluation...
1604 if (nlt->strips.first)
1607 /* otherwise, get strip to evaluate for this channel */
1608 nes= nlastrips_ctime_get_strip(&estrips, &nlt->strips, track_index, ctime);
1609 if (nes) nes->track= nlt;
1612 /* add 'active' Action (may be tweaking track) as last strip to evaluate in NLA stack
1613 * - only do this if we're not exclusively evaluating the 'solo' NLA-track
1615 if ((adt->action) && !(adt->flag & ADT_NLA_SOLO_TRACK)) {
1616 /* if there are strips, evaluate action as per NLA rules */
1618 /* make dummy NLA strip, and add that to the stack */
1619 memset(&dummy_strip, 0, sizeof(NlaStrip));
1620 dummy_trackslist.first= dummy_trackslist.last= &dummy_strip;
1622 dummy_strip.act= adt->action;
1623 dummy_strip.remap= adt->remap;
1625 /* action range is calculated taking F-Modifiers into account (which making new strips doesn't do due to the troublesome nature of that) */
1626 calc_action_range(dummy_strip.act, &dummy_strip.actstart, &dummy_strip.actend, 1);
1627 dummy_strip.start = dummy_strip.actstart;
1628 dummy_strip.end = (IS_EQ(dummy_strip.actstart, dummy_strip.actend)) ? (dummy_strip.actstart + 1.0f): (dummy_strip.actend);
1630 dummy_strip.blendmode= adt->act_blendmode;
1631 dummy_strip.extendmode= adt->act_extendmode;
1632 dummy_strip.influence= adt->act_influence;
1634 /* add this to our list of evaluation strips */
1635 nlastrips_ctime_get_strip(&estrips, &dummy_trackslist, -1, ctime);
1638 /* special case - evaluate as if there isn't any NLA data */
1639 // TODO: this is really just a stop-gap measure...
1640 animsys_evaluate_action(ptr, adt->action, adt->remap, ctime);
1645 /* only continue if there are strips to evaluate */
1646 if (estrips.first == NULL)
1650 /* 2. for each strip, evaluate then accumulate on top of existing channels, but don't set values yet */
1651 for (nes= estrips.first; nes; nes= nes->next)
1652 nlastrip_evaluate(ptr, &echannels, NULL, nes);
1654 /* 3. flush effects of accumulating channels in NLA to the actual data they affect */
1655 nladata_flush_channels(&echannels);
1657 /* 4. free temporary evaluation data */
1658 BLI_freelistN(&estrips);
1659 BLI_freelistN(&echannels);
1662 /* ***************************************** */
1663 /* Overrides System - Public API */
1665 /* Clear all overides */
1667 /* Add or get existing Override for given setting */
1668 AnimOverride *BKE_animsys_validate_override (PointerRNA *ptr, char *path, int array_index)
1670 // FIXME: need to define how to get overrides
1674 /* -------------------- */
1676 /* Evaluate Overrides */
1677 static void animsys_evaluate_overrides (PointerRNA *ptr, AnimData *adt, float ctime)
1681 /* for each override, simply execute... */
1682 for (aor= adt->overrides.first; aor; aor= aor->next)
1683 animsys_write_rna_setting(ptr, aor->rna_path, aor->array_index, aor->value);
1686 /* ***************************************** */
1687 /* Evaluation System - Public API */
1689 /* Overview of how this system works:
1690 * 1) Depsgraph sorts data as necessary, so that data is in an order that means
1691 * that all dependences are resolved before dependants.
1692 * 2) All normal animation is evaluated, so that drivers have some basis values to
1694 * a. NLA stacks are done first, as the Active Actions act as 'tweaking' tracks
1695 * which modify the effects of the NLA-stacks
1696 * b. Active Action is evaluated as per normal, on top of the results of the NLA tracks
1698 * --------------< often in a separate phase... >------------------
1700 * 3) Drivers/expressions are evaluated on top of this, in an order where dependences are
1702 * Note: it may be necessary to have some tools to handle the cases where some higher-level
1703 * drivers are added and cause some problematic dependencies that didn't exist in the local levels...
1705 * --------------< always executed >------------------
1707 * Maintainance of editability of settings (XXX):
1708 * In order to ensure that settings that are animated can still be manipulated in the UI without requiring
1709 * that keyframes are added to prevent these values from being overwritten, we use 'overrides'.
1711 * Unresolved things:
1712 * - Handling of multi-user settings (i.e. time-offset, group-instancing) -> big cache grids or nodal system? but stored where?
1713 * - Multiple-block dependencies (i.e. drivers for settings are in both local and higher levels) -> split into separate lists?
1716 * - Currently (as of September 2009), overrides we haven't needed to (fully) implement overrides.
1717 * However, the code fo this is relatively harmless, so is left in the code for now.
1720 /* Evaluation loop for evaluation animation data
1722 * This assumes that the animation-data provided belongs to the ID block in question,
1723 * and that the flags for which parts of the anim-data settings need to be recalculated
1724 * have been set already by the depsgraph. Now, we use the recalc
1726 void BKE_animsys_evaluate_animdata (ID *id, AnimData *adt, float ctime, short recalc)
1731 if ELEM(NULL, id, adt)
1734 /* get pointer to ID-block for RNA to use */
1735 RNA_id_pointer_create(id, &id_ptr);
1737 /* recalculate keyframe data:
1738 * - NLA before Active Action, as Active Action behaves as 'tweaking track'
1739 * that overrides 'rough' work in NLA
1741 // TODO: need to double check that this all works correctly
1742 if ((recalc & ADT_RECALC_ANIM) || (adt->recalc & ADT_RECALC_ANIM))
1744 /* evaluate NLA data */
1745 if ((adt->nla_tracks.first) && !(adt->flag & ADT_NLA_EVAL_OFF))
1747 /* evaluate NLA-stack
1748 * - active action is evaluated as part of the NLA stack as the last item
1750 animsys_evaluate_nla(&id_ptr, adt, ctime);
1752 /* evaluate Active Action only */
1753 else if (adt->action)
1754 animsys_evaluate_action(&id_ptr, adt->action, adt->remap, ctime);
1757 adt->recalc &= ~ADT_RECALC_ANIM;
1760 /* recalculate drivers
1761 * - Drivers need to be evaluated afterwards, as they can either override
1762 * or be layered on top of existing animation data.
1763 * - Drivers should be in the appropriate order to be evaluated without problems...
1765 if ((recalc & ADT_RECALC_DRIVERS) /*&& (adt->recalc & ADT_RECALC_DRIVERS)*/) // XXX for now, don't check yet, as depsgraph hasn't been updated
1767 animsys_evaluate_drivers(&id_ptr, adt, ctime);
1770 /* always execute 'overrides'
1771 * - Overrides allow editing, by overwriting the value(s) set from animation-data, with the
1772 * value last set by the user (and not keyframed yet).
1773 * - Overrides are cleared upon frame change and/or keyframing
1774 * - It is best that we execute this everytime, so that no errors are likely to occur.
1776 animsys_evaluate_overrides(&id_ptr, adt, ctime);
1778 /* clear recalc flag now */
1782 /* Evaluation of all ID-blocks with Animation Data blocks - Animation Data Only
1784 * This will evaluate only the animation info available in the animation data-blocks
1785 * encountered. In order to enforce the system by which some settings controlled by a
1786 * 'local' (i.e. belonging in the nearest ID-block that setting is related to, not a
1787 * standard 'root') block are overridden by a larger 'user'
1789 void BKE_animsys_evaluate_all_animation (Main *main, float ctime)
1794 printf("Evaluate all animation - %f \n", ctime);
1796 /* macro for less typing
1797 * - only evaluate animation data for id if it has users (and not just fake ones)
1798 * - whether animdata exists is checked for by the evaluation function, though taking
1799 * this outside of the function may make things slightly faster?
1801 #define EVAL_ANIM_IDS(first, aflag) \
1802 for (id= first; id; id= id->next) { \
1803 AnimData *adt= BKE_animdata_from_id(id); \
1804 if ( (id->us > 1) || (id->us && !(id->flag & LIB_FAKEUSER)) ) \
1805 BKE_animsys_evaluate_animdata(id, adt, ctime, aflag); \
1809 * when there are no actions, don't go over database and loop over heaps of datablocks,
1810 * which should ultimately be empty, since it is not possible for now to have any animation
1811 * without some actions, and drivers wouldn't get affected by any state changes
1813 * however, if there are some curves, we will need to make sure that their 'ctime' property gets
1814 * set correctly, so this optimisation must be skipped in that case...
1816 if ((main->action.first == NULL) && (main->curve.first == NULL)) {
1818 printf("\tNo Actions, so no animation needs to be evaluated...\n");
1824 EVAL_ANIM_IDS(main->nodetree.first, ADT_RECALC_ANIM);
1827 EVAL_ANIM_IDS(main->tex.first, ADT_RECALC_ANIM);
1830 EVAL_ANIM_IDS(main->lamp.first, ADT_RECALC_ANIM);
1833 EVAL_ANIM_IDS(main->mat.first, ADT_RECALC_ANIM);
1836 EVAL_ANIM_IDS(main->camera.first, ADT_RECALC_ANIM);
1839 // TODO: we probably need the same hack as for curves (ctime-hack)
1840 EVAL_ANIM_IDS(main->key.first, ADT_RECALC_ANIM);
1843 EVAL_ANIM_IDS(main->mball.first, ADT_RECALC_ANIM);
1846 /* we need to perform a special hack here to ensure that the ctime
1847 * value of the curve gets set in case there's no animation for that
1848 * - it needs to be set before animation is evaluated just so that
1849 * animation can successfully override...
1850 * - it shouldn't get set when calculating drivers...
1852 for (id= main->curve.first; id; id= id->next) {
1853 AnimData *adt= BKE_animdata_from_id(id);
1854 Curve *cu= (Curve *)id;
1856 /* set ctime variable for curve */
1859 /* now execute animation data on top of this as per normal */
1860 BKE_animsys_evaluate_animdata(id, adt, ctime, ADT_RECALC_ANIM);
1864 EVAL_ANIM_IDS(main->armature.first, ADT_RECALC_ANIM);
1867 EVAL_ANIM_IDS(main->mesh.first, ADT_RECALC_ANIM);
1870 EVAL_ANIM_IDS(main->particle.first, ADT_RECALC_ANIM);
1873 /* ADT_RECALC_ANIM doesn't need to be supplied here, since object AnimData gets
1874 * this tagged by Depsgraph on framechange. This optimisation means that objects
1875 * linked from other (not-visible) scenes will not need their data calculated.
1877 EVAL_ANIM_IDS(main->object.first, 0);
1880 EVAL_ANIM_IDS(main->world.first, ADT_RECALC_ANIM);
1883 for (id= main->scene.first; id; id= id->next) {
1884 AnimData *adt= BKE_animdata_from_id(id);
1885 Scene *scene= (Scene *)id;
1887 /* do compositing nodes first (since these aren't included in main tree) */
1888 if (scene->nodetree) {
1889 AnimData *adt2= BKE_animdata_from_id((ID *)scene->nodetree);
1890 BKE_animsys_evaluate_animdata((ID *)scene->nodetree, adt2, ctime, ADT_RECALC_ANIM);
1893 /* now execute scene animation data as per normal */
1894 BKE_animsys_evaluate_animdata(id, adt, ctime, ADT_RECALC_ANIM);
1898 /* ***************************************** */