2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2009 Blender Foundation, Joshua Leung
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): Joshua Leung (full recode)
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/anim_sys.c
39 #include "MEM_guardedalloc.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_dynstr.h"
43 #include "BLI_utildefines.h"
45 #include "DNA_anim_types.h"
46 #include "DNA_lamp_types.h"
47 #include "DNA_material_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_texture_types.h"
51 #include "DNA_world_types.h"
53 #include "BKE_animsys.h"
54 #include "BKE_action.h"
55 #include "BKE_depsgraph.h"
56 #include "BKE_fcurve.h"
58 #include "BKE_global.h"
60 #include "BKE_library.h"
61 #include "BKE_report.h"
63 #include "RNA_access.h"
65 #include "nla_private.h"
67 /* ***************************************** */
70 /* Getter/Setter -------------------------------------------- */
72 /* Check if ID can have AnimData */
73 short id_type_can_have_animdata(ID *id)
79 /* Only some ID-blocks have this info for now */
80 /* TODO: finish adding this for the other blocktypes */
81 switch (GS(id->name)) {
84 case ID_ME: case ID_MB: case ID_CU: case ID_AR: case ID_LT:
87 case ID_MA: case ID_TE: case ID_NT:
88 case ID_LA: case ID_CA: case ID_WO:
104 /* Get AnimData from the given ID-block. In order for this to work, we assume that
105 * the AnimData pointer is stored immediately after the given ID-block in the struct,
106 * as per IdAdtTemplate.
108 AnimData *BKE_animdata_from_id(ID *id)
110 /* only some ID-blocks have this info for now, so we cast the
111 * types that do to be of type IdAdtTemplate, and extract the
114 if (id_type_can_have_animdata(id)) {
115 IdAdtTemplate *iat = (IdAdtTemplate *)id;
122 /* Add AnimData to the given ID-block. In order for this to work, we assume that
123 * the AnimData pointer is stored immediately after the given ID-block in the struct,
124 * as per IdAdtTemplate. Also note that
126 AnimData *BKE_id_add_animdata(ID *id)
128 /* Only some ID-blocks have this info for now, so we cast the
129 * types that do to be of type IdAdtTemplate, and add the AnimData
130 * to it using the template
132 if (id_type_can_have_animdata(id)) {
133 IdAdtTemplate *iat = (IdAdtTemplate *)id;
135 /* check if there's already AnimData, in which case, don't add */
136 if (iat->adt == NULL) {
140 adt = iat->adt = MEM_callocN(sizeof(AnimData), "AnimData");
142 /* set default settings */
143 adt->act_influence = 1.0f;
152 /* Action Setter --------------------------------------- */
154 /* Called when user tries to change the active action of an AnimData block (via RNA, Outliner, etc.) */
155 short BKE_animdata_set_action(ReportList *reports, ID *id, bAction *act)
157 AnimData *adt = BKE_animdata_from_id(id);
160 /* animdata validity check */
162 BKE_report(reports, RPT_WARNING, "No AnimData to set action on");
166 /* active action is only editable when it is not a tweaking strip
167 * see rna_AnimData_action_editable() in rna_animation.c
169 if ((adt->flag & ADT_NLA_EDIT_ON) || (adt->actstrip) || (adt->tmpact)) {
170 /* cannot remove, otherwise things turn to custard */
171 BKE_report(reports, RPT_ERROR, "Cannot change action, as it is still being edited in NLA");
175 /* manage usercount for current action */
177 id_us_min((ID *)adt->action);
179 /* assume that AnimData's action can in fact be edited... */
181 /* action must have same type as owner */
182 if (ELEM(act->idroot, 0, GS(id->name))) {
185 id_us_plus((ID *)adt->action);
190 BKE_reportf(reports, RPT_ERROR,
191 "Couldn't set Action '%s' onto ID '%s', as it doesn't have suitably rooted paths for this purpose",
192 act->id.name + 2, id->name);
197 /* just clearing the action... */
205 /* Freeing -------------------------------------------- */
207 /* Free AnimData used by the nominated ID-block, and clear ID-block's AnimData pointer */
208 void BKE_free_animdata(ID *id)
210 /* Only some ID-blocks have this info for now, so we cast the
211 * types that do to be of type IdAdtTemplate
213 if (id_type_can_have_animdata(id)) {
214 IdAdtTemplate *iat = (IdAdtTemplate *)id;
215 AnimData *adt = iat->adt;
217 /* check if there's any AnimData to start with */
219 /* unlink action (don't free, as it's in its own list) */
221 adt->action->id.us--;
222 /* same goes for the temporarily displaced action */
224 adt->tmpact->id.us--;
227 free_nladata(&adt->nla_tracks);
229 /* free drivers - stored as a list of F-Curves */
230 free_fcurves(&adt->drivers);
235 /* free animdata now */
242 /* Freeing -------------------------------------------- */
244 /* Make a copy of the given AnimData - to be used when copying datablocks */
245 AnimData *BKE_copy_animdata(AnimData *adt, const short do_action)
249 /* sanity check before duplicating struct */
252 dadt = MEM_dupallocN(adt);
254 /* make a copy of action - at worst, user has to delete copies... */
256 dadt->action = BKE_action_copy(adt->action);
257 dadt->tmpact = BKE_action_copy(adt->tmpact);
260 id_us_plus((ID *)dadt->action);
261 id_us_plus((ID *)dadt->tmpact);
264 /* duplicate NLA data */
265 copy_nladata(&dadt->nla_tracks, &adt->nla_tracks);
267 /* duplicate drivers (F-Curves) */
268 copy_fcurves(&dadt->drivers, &adt->drivers);
270 /* don't copy overrides */
271 dadt->overrides.first = dadt->overrides.last = NULL;
277 int BKE_copy_animdata_id(ID *id_to, ID *id_from, const short do_action)
281 if ((id_to && id_from) && (GS(id_to->name) != GS(id_from->name)))
284 BKE_free_animdata(id_to);
286 adt = BKE_animdata_from_id(id_from);
288 IdAdtTemplate *iat = (IdAdtTemplate *)id_to;
289 iat->adt = BKE_copy_animdata(adt, do_action);
295 void BKE_copy_animdata_id_action(ID *id)
297 AnimData *adt = BKE_animdata_from_id(id);
300 id_us_min((ID *)adt->action);
301 adt->action = BKE_action_copy(adt->action);
304 id_us_min((ID *)adt->tmpact);
305 adt->tmpact = BKE_action_copy(adt->tmpact);
310 /* Make Local -------------------------------------------- */
312 static void make_local_strips(ListBase *strips)
316 for (strip = strips->first; strip; strip = strip->next) {
317 if (strip->act) BKE_action_make_local(strip->act);
318 if (strip->remap && strip->remap->target) BKE_action_make_local(strip->remap->target);
320 make_local_strips(&strip->strips);
324 /* Use local copy instead of linked copy of various ID-blocks */
325 void BKE_animdata_make_local(AnimData *adt)
329 /* Actions - Active and Temp */
330 if (adt->action) BKE_action_make_local(adt->action);
331 if (adt->tmpact) BKE_action_make_local(adt->tmpact);
333 if (adt->remap && adt->remap->target) BKE_action_make_local(adt->remap->target);
336 /* TODO: need to remap the ID-targets too? */
339 for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next)
340 make_local_strips(&nlt->strips);
344 /* When duplicating data (i.e. objects), drivers referring to the original data will
345 * get updated to point to the duplicated data (if drivers belong to the new data)
347 void BKE_relink_animdata(AnimData *adt)
354 if (adt->drivers.first) {
357 /* check each driver against all the base paths to see if any should go */
358 for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
359 ChannelDriver *driver = fcu->driver;
362 /* driver variables */
363 for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
364 /* only change the used targets, since the others will need fixing manually anyway */
365 DRIVER_TARGETS_USED_LOOPER(dvar)
367 if (dtar->id && dtar->id->newid) {
368 dtar->id = dtar->id->newid;
371 DRIVER_TARGETS_LOOPER_END
377 /* Sub-ID Regrouping ------------------------------------------- */
379 /* helper heuristic for determining if a path is compatible with the basepath
380 * < path: (str) full RNA-path from some data (usually an F-Curve) to compare
381 * < basepath: (str) shorter path fragment to look for
382 * > returns (bool) whether there is a match
384 static short animpath_matches_basepath(const char path[], const char basepath[])
386 /* we need start of path to be basepath */
387 return (path && basepath) && (strstr(path, basepath) == path);
390 /* Move F-Curves in src action to dst action, setting up all the necessary groups
391 * for this to happen, but only if the F-Curves being moved have the appropriate
393 * - This is used when data moves from one datablock to another, causing the
394 * F-Curves to need to be moved over too
396 void action_move_fcurves_by_basepath(bAction *srcAct, bAction *dstAct, const char basepath[])
398 FCurve *fcu, *fcn = NULL;
401 if (ELEM3(NULL, srcAct, dstAct, basepath)) {
402 if (G.debug & G_DEBUG) {
403 printf("ERROR: action_partition_fcurves_by_basepath(%p, %p, %p) has insufficient info to work with\n",
404 (void *)srcAct, (void *)dstAct, (void *)basepath);
409 /* clear 'temp' flags on all groups in src, as we'll be needing them later
410 * to identify groups that we've managed to empty out here
412 action_groups_clear_tempflags(srcAct);
414 /* iterate over all src F-Curves, moving over the ones that need to be moved */
415 for (fcu = srcAct->curves.first; fcu; fcu = fcn) {
416 /* store next pointer in case we move stuff */
419 /* should F-Curve be moved over?
420 * - we only need the start of the path to match basepath
422 if (animpath_matches_basepath(fcu->rna_path, basepath)) {
423 bActionGroup *agrp = NULL;
427 /* make sure there will be a matching group on the other side for the migrants */
428 agrp = BKE_action_group_find_name(dstAct, fcu->grp->name);
431 /* add a new one with a similar name (usually will be the same though) */
432 agrp = action_groups_add_new(dstAct, fcu->grp->name);
435 /* old groups should be tagged with 'temp' flags so they can be removed later
436 * if we remove everything from them
438 fcu->grp->flag |= AGRP_TEMP;
441 /* perform the migration now */
442 action_groups_remove_channel(srcAct, fcu);
445 action_groups_add_channel(dstAct, agrp, fcu);
447 BLI_addtail(&dstAct->curves, fcu);
451 /* cleanup groups (if present) */
452 if (srcAct->groups.first) {
453 bActionGroup *agrp, *grp = NULL;
455 for (agrp = srcAct->groups.first; agrp; agrp = grp) {
458 /* only tagged groups need to be considered - clearing these tags or removing them */
459 if (agrp->flag & AGRP_TEMP) {
460 /* if group is empty and tagged, then we can remove as this operation
461 * moved out all the channels that were formerly here
463 if (agrp->channels.first == NULL)
464 BLI_freelinkN(&srcAct->groups, agrp);
466 agrp->flag &= ~AGRP_TEMP;
472 /* Transfer the animation data from srcID to dstID where the srcID
473 * animation data is based off "basepath", creating new AnimData and
474 * associated data as necessary
476 void BKE_animdata_separate_by_basepath(ID *srcID, ID *dstID, ListBase *basepaths)
478 AnimData *srcAdt = NULL, *dstAdt = NULL;
482 if (ELEM(NULL, srcID, dstID)) {
483 if (G.debug & G_DEBUG)
484 printf("ERROR: no source or destination ID to separate AnimData with\n");
488 /* get animdata from src, and create for destination (if needed) */
489 srcAdt = BKE_animdata_from_id(srcID);
490 dstAdt = BKE_id_add_animdata(dstID);
492 if (ELEM(NULL, srcAdt, dstAdt)) {
493 if (G.debug & G_DEBUG)
494 printf("ERROR: no AnimData for this pair of ID's\n");
499 if (srcAdt->action) {
500 /* set up an action if necessary, and name it in a similar way so that it can be easily found again */
501 if (dstAdt->action == NULL) {
502 dstAdt->action = add_empty_action(srcAdt->action->id.name + 2);
504 else if (dstAdt->action == srcAdt->action) {
505 printf("Argh! Source and Destination share animation! ('%s' and '%s' both use '%s') Making new empty action\n",
506 srcID->name, dstID->name, srcAdt->action->id.name);
508 /* TODO: review this... */
509 id_us_min(&dstAdt->action->id);
510 dstAdt->action = add_empty_action(dstAdt->action->id.name + 2);
513 /* loop over base paths, trying to fix for each one... */
514 for (ld = basepaths->first; ld; ld = ld->next) {
515 const char *basepath = (const char *)ld->data;
516 action_move_fcurves_by_basepath(srcAdt->action, dstAdt->action, basepath);
521 if (srcAdt->drivers.first) {
522 FCurve *fcu, *fcn = NULL;
524 /* check each driver against all the base paths to see if any should go */
525 for (fcu = srcAdt->drivers.first; fcu; fcu = fcn) {
528 /* try each basepath in turn, but stop on the first one which works */
529 for (ld = basepaths->first; ld; ld = ld->next) {
530 const char *basepath = (const char *)ld->data;
532 if (animpath_matches_basepath(fcu->rna_path, basepath)) {
533 /* just need to change lists */
534 BLI_remlink(&srcAdt->drivers, fcu);
535 BLI_addtail(&dstAdt->drivers, fcu);
537 /* TODO: add depsgraph flushing calls? */
539 /* can stop now, as moved already */
547 /* Path Validation -------------------------------------------- */
549 /* Check if a given RNA Path is valid, by tracing it from the given ID, and seeing if we can resolve it */
550 static short check_rna_path_is_valid(ID *owner_id, const char *path)
552 PointerRNA id_ptr, ptr;
553 PropertyRNA *prop = NULL;
555 /* make initial RNA pointer to start resolving from */
556 RNA_id_pointer_create(owner_id, &id_ptr);
559 return RNA_path_resolve(&id_ptr, path, &ptr, &prop);
562 /* Check if some given RNA Path needs fixing - free the given path and set a new one as appropriate
563 * NOTE: we assume that oldName and newName have [" "] padding around them
565 static char *rna_path_rename_fix(ID *owner_id, const char *prefix, const char *oldName, const char *newName, char *oldpath, int verify_paths)
567 char *prefixPtr = strstr(oldpath, prefix);
568 char *oldNamePtr = strstr(oldpath, oldName);
569 int prefixLen = strlen(prefix);
570 int oldNameLen = strlen(oldName);
572 /* only start fixing the path if the prefix and oldName feature in the path,
573 * and prefix occurs immediately before oldName
575 if ( (prefixPtr && oldNamePtr) && (prefixPtr + prefixLen == oldNamePtr) ) {
576 /* if we haven't aren't able to resolve the path now, try again after fixing it */
577 if (!verify_paths || check_rna_path_is_valid(owner_id, oldpath) == 0) {
578 DynStr *ds = BLI_dynstr_new();
579 char *postfixPtr = oldNamePtr + oldNameLen;
580 char *newPath = NULL;
583 /* add the part of the string that goes up to the start of the prefix */
584 if (prefixPtr > oldpath) {
585 oldChar = prefixPtr[0];
587 BLI_dynstr_append(ds, oldpath);
588 prefixPtr[0] = oldChar;
592 BLI_dynstr_append(ds, prefix);
594 /* add the new name (complete with brackets) */
595 BLI_dynstr_append(ds, newName);
597 /* add the postfix */
598 BLI_dynstr_append(ds, postfixPtr);
600 /* create new path, and cleanup old data */
601 newPath = BLI_dynstr_get_cstring(ds);
604 /* check if the new path will solve our problems */
605 /* TODO: will need to check whether this step really helps in practice */
606 if (!verify_paths || check_rna_path_is_valid(owner_id, newPath)) {
607 /* free the old path, and return the new one, since we've solved the issues */
612 /* still couldn't resolve the path... so, might as well just leave it alone */
618 /* the old path doesn't need to be changed */
622 /* Check RNA-Paths for a list of F-Curves */
623 static void fcurves_path_rename_fix(ID *owner_id, const char *prefix, const char *oldName, const char *newName,
624 const char *oldKey, const char *newKey, ListBase *curves, int verify_paths)
628 /* we need to check every curve... */
629 for (fcu = curves->first; fcu; fcu = fcu->next) {
631 char *old_path = fcu->rna_path;
633 /* firstly, handle the F-Curve's own path */
634 fcu->rna_path = rna_path_rename_fix(owner_id, prefix, oldKey, newKey, fcu->rna_path, verify_paths);
636 /* if path changed and the F-Curve is grouped, check if its group also needs renaming
637 * (i.e. F-Curve is first of a bone's F-Curves; hence renaming this should also trigger rename)
639 if (fcu->rna_path != old_path) {
640 bActionGroup *agrp = fcu->grp;
642 if ((agrp) && strcmp(oldName, agrp->name)==0) {
643 BLI_strncpy(agrp->name, newName, sizeof(agrp->name));
650 /* Check RNA-Paths for a list of Drivers */
651 static void drivers_path_rename_fix(ID *owner_id, ID *ref_id, const char *prefix, const char *oldName, const char *newName,
652 const char *oldKey, const char *newKey, ListBase *curves, int verify_paths)
656 /* we need to check every curve - drivers are F-Curves too! */
657 for (fcu = curves->first; fcu; fcu = fcu->next) {
658 /* firstly, handle the F-Curve's own path */
660 fcu->rna_path = rna_path_rename_fix(owner_id, prefix, oldKey, newKey, fcu->rna_path, verify_paths);
664 ChannelDriver *driver = fcu->driver;
667 /* driver variables */
668 for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
669 /* only change the used targets, since the others will need fixing manually anyway */
670 DRIVER_TARGETS_USED_LOOPER(dvar)
672 /* rename RNA path */
673 if (dtar->rna_path && dtar->id)
674 dtar->rna_path = rna_path_rename_fix(dtar->id, prefix, oldKey, newKey, dtar->rna_path, verify_paths);
676 /* also fix the bone-name (if applicable) */
677 if (strstr(prefix, "bones")) {
678 if ( ((dtar->id) && (GS(dtar->id->name) == ID_OB) && (!ref_id || ((Object *)(dtar->id))->data == ref_id)) &&
679 (dtar->pchan_name[0]) && (strcmp(oldName, dtar->pchan_name) == 0) )
681 BLI_strncpy(dtar->pchan_name, newName, sizeof(dtar->pchan_name));
685 DRIVER_TARGETS_LOOPER_END
691 /* Fix all RNA-Paths for Actions linked to NLA Strips */
692 static void nlastrips_path_rename_fix(ID *owner_id, const char *prefix, const char *oldName, const char *newName,
693 const char *oldKey, const char *newKey, ListBase *strips, int verify_paths)
697 /* recursively check strips, fixing only actions... */
698 for (strip = strips->first; strip; strip = strip->next) {
699 /* fix strip's action */
701 fcurves_path_rename_fix(owner_id, prefix, oldName, newName, oldKey, newKey, &strip->act->curves, verify_paths);
702 /* ignore own F-Curves, since those are local... */
704 /* check sub-strips (if metas) */
705 nlastrips_path_rename_fix(owner_id, prefix, oldName, newName, oldKey, newKey, &strip->strips, verify_paths);
709 /* Fix all RNA-Paths in the AnimData block used by the given ID block
710 * NOTE: it is assumed that the structure we're replacing is <prefix><["><name><"]>
711 * i.e. pose.bones["Bone"]
713 void BKE_animdata_fix_paths_rename(ID *owner_id, AnimData *adt, ID *ref_id, const char *prefix, const char *oldName,
714 const char *newName, int oldSubscript, int newSubscript, int verify_paths)
719 /* if no AnimData, no need to proceed */
720 if (ELEM(NULL, owner_id, adt))
723 if ((oldName != NULL) && (newName != NULL)) {
724 /* pad the names with [" "] so that only exact matches are made */
725 oldN = BLI_sprintfN("[\"%s\"]", oldName);
726 newN = BLI_sprintfN("[\"%s\"]", newName);
729 oldN = BLI_sprintfN("[%d]", oldSubscript);
730 newN = BLI_sprintfN("[%d]", newSubscript);
733 /* Active action and temp action */
735 fcurves_path_rename_fix(owner_id, prefix, oldName, newName, oldN, newN, &adt->action->curves, verify_paths);
737 fcurves_path_rename_fix(owner_id, prefix, oldName, newName, oldN, newN, &adt->tmpact->curves, verify_paths);
739 /* Drivers - Drivers are really F-Curves */
740 drivers_path_rename_fix(owner_id, ref_id, prefix, oldName, newName, oldN, newN, &adt->drivers, verify_paths);
742 /* NLA Data - Animation Data for Strips */
743 for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next)
744 nlastrips_path_rename_fix(owner_id, prefix, oldName, newName, oldN, newN, &nlt->strips, verify_paths);
746 /* free the temp names */
751 /* Whole Database Ops -------------------------------------------- */
753 /* apply the given callback function on all data in main database */
754 void BKE_animdata_main_cb(Main *mainptr, ID_AnimData_Edit_Callback func, void *user_data)
758 /* standard data version */
759 #define ANIMDATA_IDS_CB(first) \
760 for (id = first; id; id = id->next) { \
761 AnimData *adt = BKE_animdata_from_id(id); \
762 if (adt) func(id, adt, user_data); \
765 /* "embedded" nodetree cases (i.e. scene/material/texture->nodetree) */
766 #define ANIMDATA_NODETREE_IDS_CB(first, NtId_Type) \
767 for (id = first; id; id = id->next) { \
768 AnimData *adt = BKE_animdata_from_id(id); \
769 NtId_Type *ntp = (NtId_Type *)id; \
770 if (ntp->nodetree) { \
771 AnimData *adt2 = BKE_animdata_from_id((ID *)ntp); \
772 if (adt2) func(id, adt2, user_data); \
774 if (adt) func(id, adt, user_data); \
778 ANIMDATA_IDS_CB(mainptr->nodetree.first);
781 ANIMDATA_NODETREE_IDS_CB(mainptr->tex.first, Tex);
784 ANIMDATA_IDS_CB(mainptr->lamp.first);
787 ANIMDATA_NODETREE_IDS_CB(mainptr->mat.first, Material);
790 ANIMDATA_IDS_CB(mainptr->camera.first);
793 ANIMDATA_IDS_CB(mainptr->key.first);
796 ANIMDATA_IDS_CB(mainptr->mball.first);
799 ANIMDATA_IDS_CB(mainptr->curve.first);
802 ANIMDATA_IDS_CB(mainptr->armature.first);
805 ANIMDATA_IDS_CB(mainptr->latt.first);
808 ANIMDATA_IDS_CB(mainptr->mesh.first);
811 ANIMDATA_IDS_CB(mainptr->particle.first);
814 ANIMDATA_IDS_CB(mainptr->speaker.first);
817 ANIMDATA_IDS_CB(mainptr->movieclip.first);
820 ANIMDATA_IDS_CB(mainptr->object.first);
823 ANIMDATA_IDS_CB(mainptr->mask.first);
826 ANIMDATA_IDS_CB(mainptr->world.first);
829 ANIMDATA_NODETREE_IDS_CB(mainptr->scene.first, Scene);
832 /* Fix all RNA-Paths throughout the database (directly access the Global.main version)
833 * NOTE: it is assumed that the structure we're replacing is <prefix><["><name><"]>
834 * i.e. pose.bones["Bone"]
836 /* TODO: use BKE_animdata_main_cb for looping over all data */
837 void BKE_all_animdata_fix_paths_rename(ID *ref_id, const char *prefix, const char *oldName, const char *newName)
839 Main *mainptr = G.main;
842 /* macro for less typing
843 * - whether animdata exists is checked for by the main renaming callback, though taking
844 * this outside of the function may make things slightly faster?
846 #define RENAMEFIX_ANIM_IDS(first) \
847 for (id = first; id; id = id->next) { \
848 AnimData *adt = BKE_animdata_from_id(id); \
849 BKE_animdata_fix_paths_rename(id, adt, ref_id, prefix, oldName, newName, 0, 0, 1); \
852 /* another version of this macro for nodetrees */
853 #define RENAMEFIX_ANIM_NODETREE_IDS(first, NtId_Type) \
854 for (id = first; id; id = id->next) { \
855 AnimData *adt = BKE_animdata_from_id(id); \
856 NtId_Type *ntp = (NtId_Type *)id; \
857 if (ntp->nodetree) { \
858 AnimData *adt2 = BKE_animdata_from_id((ID *)ntp); \
859 BKE_animdata_fix_paths_rename((ID *)ntp, adt2, ref_id, prefix, oldName, newName, 0, 0, 1); \
861 BKE_animdata_fix_paths_rename(id, adt, ref_id, prefix, oldName, newName, 0, 0, 1); \
865 RENAMEFIX_ANIM_IDS(mainptr->nodetree.first);
868 RENAMEFIX_ANIM_NODETREE_IDS(mainptr->tex.first, Tex);
871 RENAMEFIX_ANIM_IDS(mainptr->lamp.first);
874 RENAMEFIX_ANIM_NODETREE_IDS(mainptr->mat.first, Material);
877 RENAMEFIX_ANIM_IDS(mainptr->camera.first);
880 RENAMEFIX_ANIM_IDS(mainptr->key.first);
883 RENAMEFIX_ANIM_IDS(mainptr->mball.first);
886 RENAMEFIX_ANIM_IDS(mainptr->curve.first);
889 RENAMEFIX_ANIM_IDS(mainptr->armature.first);
892 RENAMEFIX_ANIM_IDS(mainptr->latt.first);
895 RENAMEFIX_ANIM_IDS(mainptr->mesh.first);
898 RENAMEFIX_ANIM_IDS(mainptr->particle.first);
901 RENAMEFIX_ANIM_IDS(mainptr->speaker.first);
904 RENAMEFIX_ANIM_IDS(mainptr->movieclip.first);
907 RENAMEFIX_ANIM_IDS(mainptr->object.first);
910 RENAMEFIX_ANIM_IDS(mainptr->mask.first);
913 RENAMEFIX_ANIM_IDS(mainptr->world.first);
916 RENAMEFIX_ANIM_NODETREE_IDS(mainptr->scene.first, Scene);
919 /* *********************************** */
922 /* Finding Tools --------------------------- */
924 /* Find the first path that matches the given criteria */
925 /* TODO: do we want some method to perform partial matches too? */
926 KS_Path *BKE_keyingset_find_path(KeyingSet *ks, ID *id, const char group_name[], const char rna_path[], int array_index, int UNUSED(group_mode))
931 if (ELEM3(NULL, ks, rna_path, id))
934 /* loop over paths in the current KeyingSet, finding the first one where all settings match
935 * (i.e. the first one where none of the checks fail and equal 0)
937 for (ksp = ks->paths.first; ksp; ksp = ksp->next) {
938 short eq_id = 1, eq_path = 1, eq_index = 1, eq_group = 1;
945 if ((ksp->rna_path == NULL) || strcmp(rna_path, ksp->rna_path))
948 /* index - need to compare whole-array setting too... */
949 if (ksp->array_index != array_index)
954 /* FIXME: these checks need to be coded... for now, it's not too important though */
957 /* if all aspects are ok, return */
958 if (eq_id && eq_path && eq_index && eq_group)
966 /* Defining Tools --------------------------- */
968 /* Used to create a new 'custom' KeyingSet for the user, that will be automatically added to the stack */
969 KeyingSet *BKE_keyingset_add(ListBase *list, const char idname[], const char name[], short flag, short keyingflag)
973 /* allocate new KeyingSet */
974 ks = MEM_callocN(sizeof(KeyingSet), "KeyingSet");
976 BLI_strncpy(ks->idname, idname ? idname : name ? name : "KeyingSet", sizeof(ks->idname));
978 BLI_strncpy(ks->name, name ? name : idname ? idname : "Keying Set", sizeof(ks->name));
981 ks->keyingflag = keyingflag;
983 /* add KeyingSet to list */
984 BLI_addtail(list, ks);
986 /* Make sure KeyingSet has a unique idname. */
987 BLI_uniquename(list, ks, "KeyingSet", '.', offsetof(KeyingSet, idname), sizeof(ks->idname));
989 /* Make sure KeyingSet has a unique label (this helps with identification). */
990 BLI_uniquename(list, ks, "Keying Set", '.', offsetof(KeyingSet, name), sizeof(ks->name));
992 /* return new KeyingSet for further editing */
996 /* Add a path to a KeyingSet. Nothing is returned for now...
997 * Checks are performed to ensure that destination is appropriate for the KeyingSet in question
999 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)
1004 if (ELEM(NULL, ks, rna_path)) {
1005 printf("ERROR: no Keying Set and/or RNA Path to add path with\n");
1009 /* ID is required for all types of KeyingSets */
1011 printf("ERROR: No ID provided for Keying Set Path\n");
1015 /* don't add if there is already a matching KS_Path in the KeyingSet */
1016 if (BKE_keyingset_find_path(ks, id, group_name, rna_path, array_index, groupmode)) {
1017 if (G.debug & G_DEBUG)
1018 printf("ERROR: destination already exists in Keying Set\n");
1022 /* allocate a new KeyingSet Path */
1023 ksp = MEM_callocN(sizeof(KS_Path), "KeyingSet Path");
1025 /* just store absolute info */
1028 BLI_strncpy(ksp->group, group_name, sizeof(ksp->group));
1030 ksp->group[0] = '\0';
1032 /* store additional info for relative paths (just in case user makes the set relative) */
1034 ksp->idtype = GS(id->name);
1036 /* just copy path info */
1037 /* TODO: should array index be checked too? */
1038 ksp->rna_path = BLI_strdupn(rna_path, strlen(rna_path));
1039 ksp->array_index = array_index;
1043 ksp->groupmode = groupmode;
1045 /* add KeyingSet path to KeyingSet */
1046 BLI_addtail(&ks->paths, ksp);
1048 /* return this path */
1052 /* Free the given Keying Set path */
1053 void BKE_keyingset_free_path(KeyingSet *ks, KS_Path *ksp)
1056 if (ELEM(NULL, ks, ksp))
1059 /* free RNA-path info */
1061 MEM_freeN(ksp->rna_path);
1063 /* free path itself */
1064 BLI_freelinkN(&ks->paths, ksp);
1067 /* Copy all KeyingSets in the given list */
1068 void BKE_keyingsets_copy(ListBase *newlist, ListBase *list)
1073 BLI_duplicatelist(newlist, list);
1075 for (ksn = newlist->first; ksn; ksn = ksn->next) {
1076 BLI_duplicatelist(&ksn->paths, &ksn->paths);
1078 for (kspn = ksn->paths.first; kspn; kspn = kspn->next)
1079 kspn->rna_path = MEM_dupallocN(kspn->rna_path);
1083 /* Freeing Tools --------------------------- */
1085 /* Free data for KeyingSet but not set itself */
1086 void BKE_keyingset_free(KeyingSet *ks)
1088 KS_Path *ksp, *kspn;
1094 /* free each path as we go to avoid looping twice */
1095 for (ksp = ks->paths.first; ksp; ksp = kspn) {
1097 BKE_keyingset_free_path(ks, ksp);
1101 /* Free all the KeyingSets in the given list */
1102 void BKE_keyingsets_free(ListBase *list)
1104 KeyingSet *ks, *ksn;
1110 /* loop over KeyingSets freeing them
1111 * - BKE_keyingset_free() doesn't free the set itself, but it frees its sub-data
1113 for (ks = list->first; ks; ks = ksn) {
1115 BKE_keyingset_free(ks);
1116 BLI_freelinkN(list, ks);
1120 /* ***************************************** */
1121 /* Evaluation Data-Setting Backend */
1123 /* Retrieve string to act as RNA-path, adjusted using mapping-table if provided
1124 * It returns whether the string needs to be freed (i.e. if it was a temp remapped one)
1125 * // 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
1127 * - remap: remapping table to use
1128 * - path: original path string (as stored in F-Curve data)
1129 * - dst: destination string to write data to
1131 static short animsys_remap_path(AnimMapper *UNUSED(remap), char *path, char **dst)
1133 /* is there a valid remapping table to use? */
1136 /* find a matching entry... to use to remap */
1141 /* nothing suitable found, so just set dst to look at path (i.e. no alloc/free needed) */
1147 /* less then 1.0 evaluates to false, use epsilon to avoid float error */
1148 #define ANIMSYS_FLOAT_AS_BOOL(value) ((value) > ((1.0f - FLT_EPSILON)))
1150 /* Write the given value to a setting using RNA, and return success */
1151 static short animsys_write_rna_setting(PointerRNA *ptr, char *path, int array_index, float value)
1156 //printf("%p %s %i %f\n", ptr, path, array_index, value);
1158 /* get property to write to */
1159 if (RNA_path_resolve(ptr, path, &new_ptr, &prop)) {
1160 /* set value - only for animatable numerical values */
1161 if (RNA_property_animateable(&new_ptr, prop)) {
1162 int array_len = RNA_property_array_length(&new_ptr, prop);
1163 int written = FALSE;
1165 if (array_len && array_index >= array_len) {
1166 if (G.debug & G_DEBUG) {
1167 printf("Animato: Invalid array index. ID = '%s', '%s[%d]', array length is %d\n",
1168 (ptr && ptr->id.data) ? (((ID *)ptr->id.data)->name + 2) : "<No ID>",
1169 path, array_index, array_len - 1);
1175 switch (RNA_property_type(prop)) {
1178 if (RNA_property_boolean_get_index(&new_ptr, prop, array_index) != ANIMSYS_FLOAT_AS_BOOL(value)) {
1179 RNA_property_boolean_set_index(&new_ptr, prop, array_index, ANIMSYS_FLOAT_AS_BOOL(value));
1184 if (RNA_property_boolean_get(&new_ptr, prop) != ANIMSYS_FLOAT_AS_BOOL(value)) {
1185 RNA_property_boolean_set(&new_ptr, prop, ANIMSYS_FLOAT_AS_BOOL(value));
1192 if (RNA_property_int_get_index(&new_ptr, prop, array_index) != (int)value) {
1193 RNA_property_int_set_index(&new_ptr, prop, array_index, (int)value);
1198 if (RNA_property_int_get(&new_ptr, prop) != (int)value) {
1199 RNA_property_int_set(&new_ptr, prop, (int)value);
1206 if (RNA_property_float_get_index(&new_ptr, prop, array_index) != value) {
1207 RNA_property_float_set_index(&new_ptr, prop, array_index, value);
1212 if (RNA_property_float_get(&new_ptr, prop) != value) {
1213 RNA_property_float_set(&new_ptr, prop, value);
1219 if (RNA_property_enum_get(&new_ptr, prop) != (int)value) {
1220 RNA_property_enum_set(&new_ptr, prop, (int)value);
1225 /* nothing can be done here... so it is unsuccessful? */
1229 /* RNA property update disabled for now - [#28525] [#28690] [#28774] [#28777] */
1231 /* buffer property update for later flushing */
1232 if (written && RNA_property_update_check(prop)) {
1233 short skip_updates_hack = 0;
1235 /* optimization hacks: skip property updates for those properties
1236 * for we know that which the updates in RNA were really just for
1237 * flushing property editing via UI/Py
1239 if (new_ptr.type == &RNA_PoseBone) {
1240 /* bone transforms - update pose (i.e. tag depsgraph) */
1241 skip_updates_hack = 1;
1244 if (skip_updates_hack == 0)
1245 RNA_property_update_cache_add(&new_ptr, prop);
1249 /* as long as we don't do property update, we still tag datablock
1250 * as having been updated. this flag does not cause any updates to
1251 * be run, it's for e.g. render engines to synchronize data */
1252 if (written && new_ptr.id.data) {
1253 ID *id = new_ptr.id.data;
1254 id->flag |= LIB_ID_RECALC;
1255 DAG_id_type_tag(G.main, GS(id->name));
1263 /* failed to get path */
1264 /* XXX don't tag as failed yet though, as there are some legit situations (Action Constraint)
1265 * where some channels will not exist, but shouldn't lock up Action */
1266 if (G.debug & G_DEBUG) {
1267 printf("Animato: Invalid path. ID = '%s', '%s[%d]'\n",
1268 (ptr && ptr->id.data) ? (((ID *)ptr->id.data)->name + 2) : "<No ID>",
1275 /* Simple replacement based data-setting of the FCurve using RNA */
1276 static short animsys_execute_fcurve(PointerRNA *ptr, AnimMapper *remap, FCurve *fcu)
1279 short free_path = 0;
1282 /* get path, remapped as appropriate to work in its new environment */
1283 free_path = animsys_remap_path(remap, fcu->rna_path, &path);
1285 /* write value to setting */
1287 ok = animsys_write_rna_setting(ptr, path, fcu->array_index, fcu->curval);
1289 /* free temp path-info */
1293 /* return whether we were successful */
1297 /* Evaluate all the F-Curves in the given list
1298 * This performs a set of standard checks. If extra checks are required, separate code should be used
1300 static void animsys_evaluate_fcurves(PointerRNA *ptr, ListBase *list, AnimMapper *remap, float ctime)
1304 /* calculate then execute each curve */
1305 for (fcu = list->first; fcu; fcu = fcu->next) {
1306 /* check if this F-Curve doesn't belong to a muted group */
1307 if ((fcu->grp == NULL) || (fcu->grp->flag & AGRP_MUTED) == 0) {
1308 /* check if this curve should be skipped */
1309 if ((fcu->flag & (FCURVE_MUTED | FCURVE_DISABLED)) == 0) {
1310 calculate_fcurve(fcu, ctime);
1311 animsys_execute_fcurve(ptr, remap, fcu);
1317 /* ***************************************** */
1318 /* Driver Evaluation */
1320 /* Evaluate Drivers */
1321 static void animsys_evaluate_drivers(PointerRNA *ptr, AnimData *adt, float ctime)
1325 /* drivers are stored as F-Curves, but we cannot use the standard code, as we need to check if
1326 * the depsgraph requested that this driver be evaluated...
1328 for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
1329 ChannelDriver *driver = fcu->driver;
1332 /* check if this driver's curve should be skipped */
1333 if ((fcu->flag & (FCURVE_MUTED | FCURVE_DISABLED)) == 0) {
1334 /* check if driver itself is tagged for recalculation */
1335 /* XXX driver recalc flag is not set yet by depsgraph! */
1336 if ((driver) && !(driver->flag & DRIVER_FLAG_INVALID) /*&& (driver->flag & DRIVER_FLAG_RECALC)*/) {
1337 /* evaluate this using values set already in other places
1338 * NOTE: for 'layering' option later on, we should check if we should remove old value before adding
1339 * new to only be done when drivers only changed */
1340 calculate_fcurve(fcu, ctime);
1341 ok = animsys_execute_fcurve(ptr, NULL, fcu);
1343 /* clear recalc flag */
1344 driver->flag &= ~DRIVER_FLAG_RECALC;
1346 /* set error-flag if evaluation failed */
1348 driver->flag |= DRIVER_FLAG_INVALID;
1354 /* ***************************************** */
1355 /* Actions Evaluation */
1357 /* strictly not necessary for actual "evaluation", but it is a useful safety check
1358 * to reduce the amount of times that users end up having to "revive" wrongly-assigned
1361 static void action_idcode_patch_check(ID *id, bAction *act)
1366 if (ELEM(NULL, id, act))
1369 idcode = GS(id->name);
1371 /* the actual checks... hopefully not too much of a performance hit in the long run... */
1372 if (act->idroot == 0) {
1373 /* use the current root if not set already (i.e. newly created actions and actions from 2.50-2.57 builds)
1374 * - this has problems if there are 2 users, and the first one encountered is the invalid one
1375 * in which case, the user will need to manually fix this (?)
1377 act->idroot = idcode;
1379 else if (act->idroot != idcode) {
1380 /* only report this error if debug mode is enabled (to save performance everywhere else) */
1381 if (G.debug & G_DEBUG) {
1382 printf("AnimSys Safety Check Failed: Action '%s' is not meant to be used from ID-Blocks of type %d such as '%s'\n",
1383 act->id.name + 2, idcode, id->name);
1388 /* ----------------------------------------- */
1390 /* Evaluate Action Group */
1391 void animsys_evaluate_action_group(PointerRNA *ptr, bAction *act, bActionGroup *agrp, AnimMapper *remap, float ctime)
1395 /* check if mapper is appropriate for use here (we set to NULL if it's inappropriate) */
1396 if (ELEM(NULL, act, agrp)) return;
1397 if ((remap) && (remap->target != act)) remap = NULL;
1399 action_idcode_patch_check(ptr->id.data, act);
1401 /* if group is muted, don't evaluated any of the F-Curve */
1402 if (agrp->flag & AGRP_MUTED)
1405 /* calculate then execute each curve */
1406 for (fcu = agrp->channels.first; (fcu) && (fcu->grp == agrp); fcu = fcu->next) {
1407 /* check if this curve should be skipped */
1408 if ((fcu->flag & (FCURVE_MUTED | FCURVE_DISABLED)) == 0) {
1409 calculate_fcurve(fcu, ctime);
1410 animsys_execute_fcurve(ptr, remap, fcu);
1415 /* Evaluate Action (F-Curve Bag) */
1416 void animsys_evaluate_action(PointerRNA *ptr, bAction *act, AnimMapper *remap, float ctime)
1418 /* check if mapper is appropriate for use here (we set to NULL if it's inappropriate) */
1419 if (act == NULL) return;
1420 if ((remap) && (remap->target != act)) remap = NULL;
1422 action_idcode_patch_check(ptr->id.data, act);
1424 /* calculate then execute each curve */
1425 animsys_evaluate_fcurves(ptr, &act->curves, remap, ctime);
1428 /* ***************************************** */
1429 /* NLA System - Evaluation */
1431 /* calculate influence of strip based for given frame based on blendin/out values */
1432 static float nlastrip_get_influence(NlaStrip *strip, float cframe)
1434 /* sanity checks - normalize the blendin/out values? */
1435 strip->blendin = fabsf(strip->blendin);
1436 strip->blendout = fabsf(strip->blendout);
1438 /* result depends on where frame is in respect to blendin/out values */
1439 if (IS_EQ(strip->blendin, 0) == 0 && (cframe <= (strip->start + strip->blendin))) {
1440 /* there is some blend-in */
1441 return fabsf(cframe - strip->start) / (strip->blendin);
1443 else if (IS_EQ(strip->blendout, 0) == 0 && (cframe >= (strip->end - strip->blendout))) {
1444 /* there is some blend-out */
1445 return fabsf(strip->end - cframe) / (strip->blendout);
1448 /* in the middle of the strip, we should be full strength */
1453 /* evaluate the evaluation time and influence for the strip, storing the results in the strip */
1454 static void nlastrip_evaluate_controls(NlaStrip *strip, float ctime)
1456 /* firstly, analytically generate values for influence and time (if applicable) */
1457 if ((strip->flag & NLASTRIP_FLAG_USR_TIME) == 0)
1458 strip->strip_time = nlastrip_get_frame(strip, ctime, NLATIME_CONVERT_EVAL);
1459 if ((strip->flag & NLASTRIP_FLAG_USR_INFLUENCE) == 0)
1460 strip->influence = nlastrip_get_influence(strip, ctime);
1462 /* now strip's evaluate F-Curves for these settings (if applicable) */
1463 if (strip->fcurves.first) {
1464 PointerRNA strip_ptr;
1466 /* create RNA-pointer needed to set values */
1467 RNA_pointer_create(NULL, &RNA_NlaStrip, strip, &strip_ptr);
1469 /* execute these settings as per normal */
1470 animsys_evaluate_fcurves(&strip_ptr, &strip->fcurves, NULL, ctime);
1473 /* if user can control the evaluation time (using F-Curves), consider the option which allows this time to be clamped
1474 * to lie within extents of the action-clip, so that a steady changing rate of progress through several cycles of the clip
1475 * can be achieved easily
1477 /* NOTE: if we add any more of these special cases, we better group them up nicely... */
1478 if ((strip->flag & NLASTRIP_FLAG_USR_TIME) && (strip->flag & NLASTRIP_FLAG_USR_TIME_CYCLIC))
1479 strip->strip_time = fmod(strip->strip_time - strip->actstart, strip->actend - strip->actstart);
1482 /* gets the strip active at the current time for a list of strips for evaluation purposes */
1483 NlaEvalStrip *nlastrips_ctime_get_strip(ListBase *list, ListBase *strips, short index, float ctime)
1485 NlaStrip *strip, *estrip = NULL;
1489 /* loop over strips, checking if they fall within the range */
1490 for (strip = strips->first; strip; strip = strip->next) {
1491 /* check if current time occurs within this strip */
1492 if (IN_RANGE_INCL(ctime, strip->start, strip->end)) {
1493 /* this strip is active, so try to use it */
1495 side = NES_TIME_WITHIN;
1499 /* if time occurred before current strip... */
1500 if (ctime < strip->start) {
1501 if (strip == strips->first) {
1502 /* before first strip - only try to use it if it extends backwards in time too */
1503 if (strip->extendmode == NLASTRIP_EXTEND_HOLD)
1506 /* side is 'before' regardless of whether there's a useful strip */
1507 side = NES_TIME_BEFORE;
1510 /* before next strip - previous strip has ended, but next hasn't begun,
1511 * so blending mode depends on whether strip is being held or not...
1512 * - only occurs when no transition strip added, otherwise the transition would have
1513 * been picked up above...
1515 strip = strip->prev;
1517 if (strip->extendmode != NLASTRIP_EXTEND_NOTHING)
1519 side = NES_TIME_AFTER;
1524 /* if time occurred after current strip... */
1525 if (ctime > strip->end) {
1526 /* only if this is the last strip should we do anything, and only if that is being held */
1527 if (strip == strips->last) {
1528 if (strip->extendmode != NLASTRIP_EXTEND_NOTHING)
1531 side = NES_TIME_AFTER;
1535 /* otherwise, skip... as the 'before' case will catch it more elegantly! */
1539 /* check if a valid strip was found
1540 * - must not be muted (i.e. will have contribution
1542 if ((estrip == NULL) || (estrip->flag & NLASTRIP_FLAG_MUTED))
1545 /* if ctime was not within the boundaries of the strip, clamp! */
1547 case NES_TIME_BEFORE: /* extend first frame only */
1548 ctime = estrip->start;
1550 case NES_TIME_AFTER: /* extend last frame only */
1551 ctime = estrip->end;
1555 /* evaluate strip's evaluation controls
1556 * - skip if no influence (i.e. same effect as muting the strip)
1557 * - negative influence is not supported yet... how would that be defined?
1559 /* TODO: this sounds a bit hacky having a few isolated F-Curves stuck on some data it operates on... */
1560 nlastrip_evaluate_controls(estrip, ctime);
1561 if (estrip->influence <= 0.0f)
1564 /* check if strip has valid data to evaluate,
1565 * and/or perform any additional type-specific actions
1567 switch (estrip->type) {
1568 case NLASTRIP_TYPE_CLIP:
1569 /* clip must have some action to evaluate */
1570 if (estrip->act == NULL)
1573 case NLASTRIP_TYPE_TRANSITION:
1574 /* there must be strips to transition from and to (i.e. prev and next required) */
1575 if (ELEM(NULL, estrip->prev, estrip->next))
1578 /* evaluate controls for the relevant extents of the bordering strips... */
1579 nlastrip_evaluate_controls(estrip->prev, estrip->start);
1580 nlastrip_evaluate_controls(estrip->next, estrip->end);
1584 /* add to list of strips we need to evaluate */
1585 nes = MEM_callocN(sizeof(NlaEvalStrip), "NlaEvalStrip");
1587 nes->strip = estrip;
1588 nes->strip_mode = side;
1589 nes->track_index = index;
1590 nes->strip_time = estrip->strip_time;
1593 BLI_addtail(list, nes);
1598 /* ---------------------- */
1600 /* find an NlaEvalChannel that matches the given criteria
1601 * - ptr and prop are the RNA data to find a match for
1603 static NlaEvalChannel *nlaevalchan_find_match(ListBase *channels, PointerRNA *ptr, PropertyRNA *prop, int array_index)
1605 NlaEvalChannel *nec;
1608 if (channels == NULL)
1611 /* loop through existing channels, checking for a channel which affects the same property */
1612 for (nec = channels->first; nec; nec = nec->next) {
1613 /* - comparing the PointerRNA's is done by comparing the pointers
1614 * to the actual struct the property resides in, since that all the
1615 * other data stored in PointerRNA cannot allow us to definitively
1618 if ((nec->ptr.data == ptr->data) && (nec->prop == prop) && (nec->index == array_index))
1626 /* verify that an appropriate NlaEvalChannel for this F-Curve exists */
1627 static NlaEvalChannel *nlaevalchan_verify(PointerRNA *ptr, ListBase *channels, NlaEvalStrip *nes, FCurve *fcu, short *newChan)
1629 NlaEvalChannel *nec;
1630 NlaStrip *strip = nes->strip;
1634 /* short free_path = 0; */
1637 if (channels == NULL)
1640 /* get RNA pointer+property info from F-Curve for more convenient handling */
1641 /* get path, remapped as appropriate to work in its new environment */
1642 /* free_path = */ /* UNUSED */ animsys_remap_path(strip->remap, fcu->rna_path, &path);
1644 /* a valid property must be available, and it must be animatable */
1645 if (RNA_path_resolve(ptr, path, &new_ptr, &prop) == 0) {
1646 if (G.debug & G_DEBUG) printf("NLA Strip Eval: Cannot resolve path\n");
1649 /* only ok if animatable */
1650 else if (RNA_property_animateable(&new_ptr, prop) == 0) {
1651 if (G.debug & G_DEBUG) printf("NLA Strip Eval: Property not animatable\n");
1655 /* try to find a match */
1656 nec = nlaevalchan_find_match(channels, &new_ptr, prop, fcu->array_index);
1658 /* allocate a new struct for this if none found */
1660 nec = MEM_callocN(sizeof(NlaEvalChannel), "NlaEvalChannel");
1662 BLI_addtail(channels, nec);
1666 nec->index = fcu->array_index;
1671 /* we can now return */
1675 /* accumulate (i.e. blend) the given value on to the channel it affects */
1676 static void nlaevalchan_accumulate(NlaEvalChannel *nec, NlaEvalStrip *nes, short newChan, float value)
1678 NlaStrip *strip = nes->strip;
1679 short blendmode = strip->blendmode;
1680 float inf = strip->influence;
1682 /* if channel is new, just store value regardless of blending factors, etc. */
1688 /* if this is being performed as part of transition evaluation, incorporate
1689 * an additional weighting factor for the influence
1691 if (nes->strip_mode == NES_TIME_TRANSITION_END)
1692 inf *= nes->strip_time;
1694 /* premultiply the value by the weighting factor */
1695 if (IS_EQ(inf, 0)) return;
1698 /* perform blending */
1699 switch (blendmode) {
1700 case NLASTRIP_MODE_ADD:
1701 /* simply add the scaled value on to the stack */
1702 nec->value += value;
1705 case NLASTRIP_MODE_SUBTRACT:
1706 /* simply subtract the scaled value from the stack */
1707 nec->value -= value;
1710 case NLASTRIP_MODE_MULTIPLY:
1711 /* multiply the scaled value with the stack */
1712 nec->value *= value;
1715 case NLASTRIP_MODE_REPLACE:
1716 default: /* TODO: do we really want to blend by default? it seems more uses might prefer add... */
1717 /* do linear interpolation
1718 * - the influence of the accumulated data (elsewhere, that is called dstweight)
1719 * is 1 - influence, since the strip's influence is srcweight
1721 nec->value = nec->value * (1.0f - inf) + value;
1726 /* accumulate the results of a temporary buffer with the results of the full-buffer */
1727 static void nlaevalchan_buffers_accumulate(ListBase *channels, ListBase *tmp_buffer, NlaEvalStrip *nes)
1729 NlaEvalChannel *nec, *necn, *necd;
1731 /* optimize - abort if no channels */
1732 if (tmp_buffer->first == NULL)
1735 /* accumulate results in tmp_channels buffer to the accumulation buffer */
1736 for (nec = tmp_buffer->first; nec; nec = necn) {
1737 /* get pointer to next channel in case we remove the current channel from the temp-buffer */
1740 /* try to find an existing matching channel for this setting in the accumulation buffer */
1741 necd = nlaevalchan_find_match(channels, &nec->ptr, nec->prop, nec->index);
1743 /* if there was a matching channel already in the buffer, accumulate to it,
1744 * otherwise, add the current channel to the buffer for efficiency
1747 nlaevalchan_accumulate(necd, nes, 0, nec->value);
1749 BLI_remlink(tmp_buffer, nec);
1750 BLI_addtail(channels, nec);
1754 /* free temp-channels that haven't been assimilated into the buffer */
1755 BLI_freelistN(tmp_buffer);
1758 /* ---------------------- */
1759 /* F-Modifier stack joining/separation utilities - should we generalise these for BLI_listbase.h interface? */
1761 /* Temporarily join two lists of modifiers together, storing the result in a third list */
1762 static void nlaeval_fmodifiers_join_stacks(ListBase *result, ListBase *list1, ListBase *list2)
1764 FModifier *fcm1, *fcm2;
1766 /* if list1 is invalid... */
1767 if (ELEM(NULL, list1, list1->first)) {
1768 if (list2 && list2->first) {
1769 result->first = list2->first;
1770 result->last = list2->last;
1773 /* if list 2 is invalid... */
1774 else if (ELEM(NULL, list2, list2->first)) {
1775 result->first = list1->first;
1776 result->last = list1->last;
1779 /* list1 should be added first, and list2 second, with the endpoints of these being the endpoints for result
1780 * - the original lists must be left unchanged though, as we need that fact for restoring
1782 result->first = list1->first;
1783 result->last = list2->last;
1786 fcm2 = list2->first;
1793 /* Split two temporary lists of modifiers */
1794 static void nlaeval_fmodifiers_split_stacks(ListBase *list1, ListBase *list2)
1796 FModifier *fcm1, *fcm2;
1798 /* if list1/2 is invalid... just skip */
1799 if (ELEM(NULL, list1, list2))
1801 if (ELEM(NULL, list1->first, list2->first))
1806 fcm2 = list2->first;
1808 /* clear their links */
1813 /* ---------------------- */
1815 /* evaluate action-clip strip */
1816 static void nlastrip_evaluate_actionclip(PointerRNA *ptr, ListBase *channels, ListBase *modifiers, NlaEvalStrip *nes)
1818 ListBase tmp_modifiers = {NULL, NULL};
1819 NlaStrip *strip = nes->strip;
1823 /* sanity checks for action */
1827 if (strip->act == NULL) {
1828 printf("NLA-Strip Eval Error: Strip '%s' has no Action\n", strip->name);
1832 action_idcode_patch_check(ptr->id.data, strip->act);
1834 /* join this strip's modifiers to the parent's modifiers (own modifiers first) */
1835 nlaeval_fmodifiers_join_stacks(&tmp_modifiers, &strip->modifiers, modifiers);
1837 /* evaluate strip's modifiers which modify time to evaluate the base curves at */
1838 evaltime = evaluate_time_fmodifiers(&tmp_modifiers, NULL, 0.0f, strip->strip_time);
1840 /* evaluate all the F-Curves in the action, saving the relevant pointers to data that will need to be used */
1841 for (fcu = strip->act->curves.first; fcu; fcu = fcu->next) {
1842 NlaEvalChannel *nec;
1846 /* check if this curve should be skipped */
1847 if (fcu->flag & (FCURVE_MUTED | FCURVE_DISABLED))
1849 if ((fcu->grp) && (fcu->grp->flag & AGRP_MUTED))
1852 /* evaluate the F-Curve's value for the time given in the strip
1853 * NOTE: we use the modified time here, since strip's F-Curve Modifiers are applied on top of this
1855 value = evaluate_fcurve(fcu, evaltime);
1857 /* apply strip's F-Curve Modifiers on this value
1858 * NOTE: we apply the strip's original evaluation time not the modified one (as per standard F-Curve eval)
1860 evaluate_value_fmodifiers(&tmp_modifiers, fcu, &value, strip->strip_time);
1863 /* get an NLA evaluation channel to work with, and accumulate the evaluated value with the value(s)
1864 * stored in this channel if it has been used already
1866 nec = nlaevalchan_verify(ptr, channels, nes, fcu, &newChan);
1868 nlaevalchan_accumulate(nec, nes, newChan, value);
1871 /* unlink this strip's modifiers from the parent's modifiers again */
1872 nlaeval_fmodifiers_split_stacks(&strip->modifiers, modifiers);
1875 /* evaluate transition strip */
1876 static void nlastrip_evaluate_transition(PointerRNA *ptr, ListBase *channels, ListBase *modifiers, NlaEvalStrip *nes)
1878 ListBase tmp_channels = {NULL, NULL};
1879 ListBase tmp_modifiers = {NULL, NULL};
1880 NlaEvalStrip tmp_nes;
1883 /* join this strip's modifiers to the parent's modifiers (own modifiers first) */
1884 nlaeval_fmodifiers_join_stacks(&tmp_modifiers, &nes->strip->modifiers, modifiers);
1886 /* get the two strips to operate on
1887 * - we use the endpoints of the strips directly flanking our strip
1888 * using these as the endpoints of the transition (destination and source)
1889 * - these should have already been determined to be valid...
1890 * - if this strip is being played in reverse, we need to swap these endpoints
1891 * otherwise they will be interpolated wrong
1893 if (nes->strip->flag & NLASTRIP_FLAG_REVERSE) {
1894 s1 = nes->strip->next;
1895 s2 = nes->strip->prev;
1898 s1 = nes->strip->prev;
1899 s2 = nes->strip->next;
1902 /* prepare template for 'evaluation strip'
1903 * - based on the transition strip's evaluation strip data
1904 * - strip_mode is NES_TIME_TRANSITION_* based on which endpoint
1905 * - strip_time is the 'normalized' (i.e. in-strip) time for evaluation,
1906 * which doubles up as an additional weighting factor for the strip influences
1907 * which allows us to appear to be 'interpolating' between the two extremes
1911 /* evaluate these strips into a temp-buffer (tmp_channels) */
1912 /* FIXME: modifier evalation here needs some work... */
1914 tmp_nes.strip_mode = NES_TIME_TRANSITION_START;
1916 nlastrip_evaluate(ptr, &tmp_channels, &tmp_modifiers, &tmp_nes);
1919 tmp_nes.strip_mode = NES_TIME_TRANSITION_END;
1921 nlastrip_evaluate(ptr, &tmp_channels, &tmp_modifiers, &tmp_nes);
1924 /* assumulate temp-buffer and full-buffer, using the 'real' strip */
1925 nlaevalchan_buffers_accumulate(channels, &tmp_channels, nes);
1927 /* unlink this strip's modifiers from the parent's modifiers again */
1928 nlaeval_fmodifiers_split_stacks(&nes->strip->modifiers, modifiers);
1931 /* evaluate meta-strip */
1932 static void nlastrip_evaluate_meta(PointerRNA *ptr, ListBase *channels, ListBase *modifiers, NlaEvalStrip *nes)
1934 ListBase tmp_channels = {NULL, NULL};
1935 ListBase tmp_modifiers = {NULL, NULL};
1936 NlaStrip *strip = nes->strip;
1937 NlaEvalStrip *tmp_nes;
1940 /* meta-strip was calculated normally to have some time to be evaluated at
1941 * and here we 'look inside' the meta strip, treating it as a decorated window to
1942 * it's child strips, which get evaluated as if they were some tracks on a strip
1943 * (but with some extra modifiers to apply).
1945 * NOTE: keep this in sync with animsys_evaluate_nla()
1948 /* join this strip's modifiers to the parent's modifiers (own modifiers first) */
1949 nlaeval_fmodifiers_join_stacks(&tmp_modifiers, &strip->modifiers, modifiers);
1951 /* find the child-strip to evaluate */
1952 evaltime = (nes->strip_time * (strip->end - strip->start)) + strip->start;
1953 tmp_nes = nlastrips_ctime_get_strip(NULL, &strip->strips, -1, evaltime);
1954 if (tmp_nes == NULL)
1957 /* evaluate child-strip into tmp_channels buffer before accumulating
1958 * in the accumulation buffer
1960 nlastrip_evaluate(ptr, &tmp_channels, &tmp_modifiers, tmp_nes);
1962 /* assumulate temp-buffer and full-buffer, using the 'real' strip */
1963 nlaevalchan_buffers_accumulate(channels, &tmp_channels, nes);
1965 /* free temp eval-strip */
1968 /* unlink this strip's modifiers from the parent's modifiers again */
1969 nlaeval_fmodifiers_split_stacks(&strip->modifiers, modifiers);
1972 /* evaluates the given evaluation strip */
1973 void nlastrip_evaluate(PointerRNA *ptr, ListBase *channels, ListBase *modifiers, NlaEvalStrip *nes)
1975 NlaStrip *strip = nes->strip;
1977 /* to prevent potential infinite recursion problems (i.e. transition strip, beside meta strip containing a transition
1978 * several levels deep inside it), we tag the current strip as being evaluated, and clear this when we leave
1980 /* TODO: be careful with this flag, since some edit tools may be running and have set this while animplayback was running */
1981 if (strip->flag & NLASTRIP_FLAG_EDIT_TOUCHED)
1983 strip->flag |= NLASTRIP_FLAG_EDIT_TOUCHED;
1985 /* actions to take depend on the type of strip */
1986 switch (strip->type) {
1987 case NLASTRIP_TYPE_CLIP: /* action-clip */
1988 nlastrip_evaluate_actionclip(ptr, channels, modifiers, nes);
1990 case NLASTRIP_TYPE_TRANSITION: /* transition */
1991 nlastrip_evaluate_transition(ptr, channels, modifiers, nes);
1993 case NLASTRIP_TYPE_META: /* meta */
1994 nlastrip_evaluate_meta(ptr, channels, modifiers, nes);
1997 default: /* do nothing */
2001 /* clear temp recursion safe-check */
2002 strip->flag &= ~NLASTRIP_FLAG_EDIT_TOUCHED;
2005 /* write the accumulated settings to */
2006 void nladata_flush_channels(ListBase *channels)
2008 NlaEvalChannel *nec;
2011 if (channels == NULL)
2014 /* for each channel with accumulated values, write its value on the property it affects */
2015 for (nec = channels->first; nec; nec = nec->next) {
2016 PointerRNA *ptr = &nec->ptr;
2017 PropertyRNA *prop = nec->prop;
2018 int array_index = nec->index;
2019 float value = nec->value;
2021 /* write values - see animsys_write_rna_setting() to sync the code */
2022 switch (RNA_property_type(prop)) {
2024 if (RNA_property_array_length(ptr, prop))
2025 RNA_property_boolean_set_index(ptr, prop, array_index, ANIMSYS_FLOAT_AS_BOOL(value));
2027 RNA_property_boolean_set(ptr, prop, ANIMSYS_FLOAT_AS_BOOL(value));
2030 if (RNA_property_array_length(ptr, prop))
2031 RNA_property_int_set_index(ptr, prop, array_index, (int)value);
2033 RNA_property_int_set(ptr, prop, (int)value);
2036 if (RNA_property_array_length(ptr, prop))
2037 RNA_property_float_set_index(ptr, prop, array_index, value);
2039 RNA_property_float_set(ptr, prop, value);
2042 RNA_property_enum_set(ptr, prop, (int)value);
2045 /* can't do anything with other types of property.... */
2051 /* ---------------------- */
2053 /* NLA Evaluation function - values are calculated and stored in temporary "NlaEvalChannels"
2054 * ! This is exported so that keyframing code can use this for make use of it for anim layers support
2055 * > echannels: (list<NlaEvalChannels>) evaluation channels with calculated values
2057 static void animsys_evaluate_nla(ListBase *echannels, PointerRNA *ptr, AnimData *adt, float ctime)
2060 short track_index = 0;
2061 short has_strips = 0;
2063 ListBase estrips = {NULL, NULL};
2066 /* 1. get the stack of strips to evaluate at current time (influence calculated here) */
2067 for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next, track_index++) {
2068 /* stop here if tweaking is on and this strip is the tweaking track (it will be the first one that's 'disabled')... */
2069 if ((adt->flag & ADT_NLA_EDIT_ON) && (nlt->flag & NLATRACK_DISABLED))
2072 /* skip if we're only considering a track tagged 'solo' */
2073 if ((adt->flag & ADT_NLA_SOLO_TRACK) && (nlt->flag & NLATRACK_SOLO) == 0)
2075 /* skip if track is muted */
2076 if (nlt->flag & NLATRACK_MUTED)
2079 /* if this track has strips (but maybe they won't be suitable), set has_strips
2080 * - used for mainly for still allowing normal action evaluation...
2082 if (nlt->strips.first)
2085 /* otherwise, get strip to evaluate for this channel */
2086 nes = nlastrips_ctime_get_strip(&estrips, &nlt->strips, track_index, ctime);
2087 if (nes) nes->track = nlt;
2090 /* add 'active' Action (may be tweaking track) as last strip to evaluate in NLA stack
2091 * - only do this if we're not exclusively evaluating the 'solo' NLA-track
2092 * - however, if the 'solo' track houses the current 'tweaking' strip,
2093 * then we should allow this to play, otherwise nothing happens
2095 if ((adt->action) && ((adt->flag & ADT_NLA_SOLO_TRACK) == 0 || (adt->flag & ADT_NLA_EDIT_ON))) {
2096 /* if there are strips, evaluate action as per NLA rules */
2097 if ((has_strips) || (adt->actstrip)) {
2098 /* make dummy NLA strip, and add that to the stack */
2099 NlaStrip dummy_strip = {NULL};
2100 ListBase dummy_trackslist;
2102 dummy_trackslist.first = dummy_trackslist.last = &dummy_strip;
2104 if ((nlt) && !(adt->flag & ADT_NLA_EDIT_NOMAP)) {
2105 /* edit active action in-place according to its active strip, so copy the data */
2106 memcpy(&dummy_strip, adt->actstrip, sizeof(NlaStrip));
2107 dummy_strip.next = dummy_strip.prev = NULL;
2110 /* set settings of dummy NLA strip from AnimData settings */
2111 dummy_strip.act = adt->action;
2112 dummy_strip.remap = adt->remap;
2114 /* action range is calculated taking F-Modifiers into account (which making new strips doesn't do due to the troublesome nature of that) */
2115 calc_action_range(dummy_strip.act, &dummy_strip.actstart, &dummy_strip.actend, 1);
2116 dummy_strip.start = dummy_strip.actstart;
2117 dummy_strip.end = (IS_EQF(dummy_strip.actstart, dummy_strip.actend)) ? (dummy_strip.actstart + 1.0f) : (dummy_strip.actend);
2119 dummy_strip.blendmode = adt->act_blendmode;
2120 dummy_strip.extendmode = adt->act_extendmode;
2121 dummy_strip.influence = adt->act_influence;
2124 /* add this to our list of evaluation strips */
2125 nlastrips_ctime_get_strip(&estrips, &dummy_trackslist, -1, ctime);
2128 /* special case - evaluate as if there isn't any NLA data */
2129 /* TODO: this is really just a stop-gap measure... */
2130 animsys_evaluate_action(ptr, adt->action, adt->remap, ctime);
2135 /* only continue if there are strips to evaluate */
2136 if (estrips.first == NULL)
2140 /* 2. for each strip, evaluate then accumulate on top of existing channels, but don't set values yet */
2141 for (nes = estrips.first; nes; nes = nes->next)
2142 nlastrip_evaluate(ptr, echannels, NULL, nes);
2144 /* 3. free temporary evaluation data that's not used elsewhere */
2145 BLI_freelistN(&estrips);
2148 /* NLA Evaluation function (mostly for use through do_animdata)
2149 * - All channels that will be affected are not cleared anymore. Instead, we just evaluate into
2150 * some temp channels, where values can be accumulated in one go.
2152 static void animsys_calculate_nla(PointerRNA *ptr, AnimData *adt, float ctime)
2154 ListBase echannels = {NULL, NULL};
2156 /* TODO: need to zero out all channels used, otherwise we have problems with threadsafety
2157 * and also when the user jumps between different times instead of moving sequentially... */
2159 /* evaluate the NLA stack, obtaining a set of values to flush */
2160 animsys_evaluate_nla(&echannels, ptr, adt, ctime);
2162 /* flush effects of accumulating channels in NLA to the actual data they affect */
2163 nladata_flush_channels(&echannels);
2165 /* free temp data */
2166 BLI_freelistN(&echannels);
2169 /* ***************************************** */
2170 /* Overrides System - Public API */
2172 /* Clear all overides */
2174 /* Add or get existing Override for given setting */
2176 AnimOverride *BKE_animsys_validate_override(PointerRNA *UNUSED(ptr), char *UNUSED(path), int UNUSED(array_index))
2178 /* FIXME: need to define how to get overrides */
2183 /* -------------------- */
2185 /* Evaluate Overrides */
2186 static void animsys_evaluate_overrides(PointerRNA *ptr, AnimData *adt)
2190 /* for each override, simply execute... */
2191 for (aor = adt->overrides.first; aor; aor = aor->next)
2192 animsys_write_rna_setting(ptr, aor->rna_path, aor->array_index, aor->value);
2195 /* ***************************************** */
2196 /* Evaluation System - Public API */
2198 /* Overview of how this system works:
2199 * 1) Depsgraph sorts data as necessary, so that data is in an order that means
2200 * that all dependencies are resolved before dependants.
2201 * 2) All normal animation is evaluated, so that drivers have some basis values to
2203 * a. NLA stacks are done first, as the Active Actions act as 'tweaking' tracks
2204 * which modify the effects of the NLA-stacks
2205 * b. Active Action is evaluated as per normal, on top of the results of the NLA tracks
2207 * --------------< often in a separate phase... >------------------
2209 * 3) Drivers/expressions are evaluated on top of this, in an order where dependencies are
2211 * Note: it may be necessary to have some tools to handle the cases where some higher-level
2212 * drivers are added and cause some problematic dependencies that didn't exist in the local levels...
2214 * --------------< always executed >------------------
2216 * Maintenance of editability of settings (XXX):
2217 * In order to ensure that settings that are animated can still be manipulated in the UI without requiring
2218 * that keyframes are added to prevent these values from being overwritten, we use 'overrides'.
2220 * Unresolved things:
2221 * - Handling of multi-user settings (i.e. time-offset, group-instancing) -> big cache grids or nodal system? but stored where?
2222 * - Multiple-block dependencies (i.e. drivers for settings are in both local and higher levels) -> split into separate lists?
2225 * - Currently (as of September 2009), overrides we haven't needed to (fully) implement overrides.
2226 * However, the code for this is relatively harmless, so is left in the code for now.
2229 /* Evaluation loop for evaluation animation data
2231 * This assumes that the animation-data provided belongs to the ID block in question,
2232 * and that the flags for which parts of the anim-data settings need to be recalculated
2233 * have been set already by the depsgraph. Now, we use the recalc
2235 void BKE_animsys_evaluate_animdata(Scene *scene, ID *id, AnimData *adt, float ctime, short recalc)
2240 if (ELEM(NULL, id, adt))
2243 /* get pointer to ID-block for RNA to use */
2244 RNA_id_pointer_create(id, &id_ptr);
2246 /* recalculate keyframe data:
2247 * - NLA before Active Action, as Active Action behaves as 'tweaking track'
2248 * that overrides 'rough' work in NLA
2250 /* TODO: need to double check that this all works correctly */
2251 if ((recalc & ADT_RECALC_ANIM) || (adt->recalc & ADT_RECALC_ANIM)) {
2252 /* evaluate NLA data */
2253 if ((adt->nla_tracks.first) && !(adt->flag & ADT_NLA_EVAL_OFF)) {
2254 /* evaluate NLA-stack
2255 * - active action is evaluated as part of the NLA stack as the last item
2257 animsys_calculate_nla(&id_ptr, adt, ctime);
2259 /* evaluate Active Action only */
2260 else if (adt->action)
2261 animsys_evaluate_action(&id_ptr, adt->action, adt->remap, ctime);
2264 adt->recalc &= ~ADT_RECALC_ANIM;
2267 /* recalculate drivers
2268 * - Drivers need to be evaluated afterwards, as they can either override
2269 * or be layered on top of existing animation data.
2270 * - Drivers should be in the appropriate order to be evaluated without problems...
2272 if ((recalc & ADT_RECALC_DRIVERS)
2273 /* XXX for now, don't check yet, as depsgraph hasn't been updated */
2274 /* && (adt->recalc & ADT_RECALC_DRIVERS)*/)
2276 animsys_evaluate_drivers(&id_ptr, adt, ctime);
2279 /* always execute 'overrides'
2280 * - Overrides allow editing, by overwriting the value(s) set from animation-data, with the
2281 * value last set by the user (and not keyframed yet).
2282 * - Overrides are cleared upon frame change and/or keyframing
2283 * - It is best that we execute this everytime, so that no errors are likely to occur.
2285 animsys_evaluate_overrides(&id_ptr, adt);
2287 /* execute and clear all cached property update functions */
2289 Main *bmain = G.main; // xxx - to get passed in!
2290 RNA_property_update_cache_flush(bmain, scene);
2291 RNA_property_update_cache_free();
2294 /* clear recalc flag now */
2298 /* Evaluation of all ID-blocks with Animation Data blocks - Animation Data Only
2300 * This will evaluate only the animation info available in the animation data-blocks
2301 * encountered. In order to enforce the system by which some settings controlled by a
2302 * 'local' (i.e. belonging in the nearest ID-block that setting is related to, not a
2303 * standard 'root') block are overridden by a larger 'user'
2305 void BKE_animsys_evaluate_all_animation(Main *main, Scene *scene, float ctime)
2309 if (G.debug & G_DEBUG)
2310 printf("Evaluate all animation - %f\n", ctime);
2312 /* macros for less typing
2313 * - only evaluate animation data for id if it has users (and not just fake ones)
2314 * - whether animdata exists is checked for by the evaluation function, though taking
2315 * this outside of the function may make things slightly faster?
2317 #define EVAL_ANIM_IDS(first, aflag) \
2318 for (id = first; id; id = id->next) { \
2319 if (ID_REAL_USERS(id) > 0) { \
2320 AnimData *adt = BKE_animdata_from_id(id); \
2321 BKE_animsys_evaluate_animdata(scene, id, adt, ctime, aflag); \
2325 /* another macro for the "embedded" nodetree cases
2326 * - this is like EVAL_ANIM_IDS, but this handles the case "embedded nodetrees"
2327 * (i.e. scene/material/texture->nodetree) which we need a special exception
2328 * for, otherwise they'd get skipped
2329 * - ntp = "node tree parent" = datablock where node tree stuff resides
2331 #define EVAL_ANIM_NODETREE_IDS(first, NtId_Type, aflag) \
2332 for (id = first; id; id = id->next) { \
2333 if (ID_REAL_USERS(id) > 0) { \
2334 AnimData *adt = BKE_animdata_from_id(id); \
2335 NtId_Type *ntp = (NtId_Type *)id; \
2336 if (ntp->nodetree) { \
2337 AnimData *adt2 = BKE_animdata_from_id((ID *)ntp->nodetree); \
2338 BKE_animsys_evaluate_animdata(scene, (ID *)ntp->nodetree, adt2, ctime, ADT_RECALC_ANIM); \
2340 BKE_animsys_evaluate_animdata(scene, id, adt, ctime, aflag); \
2345 * when there are no actions, don't go over database and loop over heaps of datablocks,
2346 * which should ultimately be empty, since it is not possible for now to have any animation
2347 * without some actions, and drivers wouldn't get affected by any state changes
2349 * however, if there are some curves, we will need to make sure that their 'ctime' property gets
2350 * set correctly, so this optimization must be skipped in that case...
2352 if ((main->action.first == NULL) && (main->curve.first == NULL)) {
2353 if (G.debug & G_DEBUG)
2354 printf("\tNo Actions, so no animation needs to be evaluated...\n");
2360 EVAL_ANIM_IDS(main->nodetree.first, ADT_RECALC_ANIM);
2363 EVAL_ANIM_NODETREE_IDS(main->tex.first, Tex, ADT_RECALC_ANIM);
2366 EVAL_ANIM_NODETREE_IDS(main->lamp.first, Lamp, ADT_RECALC_ANIM);
2369 EVAL_ANIM_NODETREE_IDS(main->mat.first, Material, ADT_RECALC_ANIM);
2372 EVAL_ANIM_IDS(main->camera.first, ADT_RECALC_ANIM);
2375 EVAL_ANIM_IDS(main->key.first, ADT_RECALC_ANIM);
2378 EVAL_ANIM_IDS(main->mball.first, ADT_RECALC_ANIM);
2381 EVAL_ANIM_IDS(main->curve.first, ADT_RECALC_ANIM);
2384 EVAL_ANIM_IDS(main->armature.first, ADT_RECALC_ANIM);
2387 EVAL_ANIM_IDS(main->latt.first, ADT_RECALC_ANIM);
2390 EVAL_ANIM_IDS(main->mesh.first, ADT_RECALC_ANIM);
2393 EVAL_ANIM_IDS(main->particle.first, ADT_RECALC_ANIM);
2396 EVAL_ANIM_IDS(main->speaker.first, ADT_RECALC_ANIM);
2399 EVAL_ANIM_IDS(main->movieclip.first, ADT_RECALC_ANIM);
2402 /* ADT_RECALC_ANIM doesn't need to be supplied here, since object AnimData gets
2403 * this tagged by Depsgraph on framechange. This optimization means that objects
2404 * linked from other (not-visible) scenes will not need their data calculated.
2406 EVAL_ANIM_IDS(main->object.first, 0);
2409 EVAL_ANIM_IDS(main->mask.first, ADT_RECALC_ANIM);
2412 EVAL_ANIM_NODETREE_IDS(main->world.first, World, ADT_RECALC_ANIM);
2415 EVAL_ANIM_NODETREE_IDS(main->scene.first, Scene, ADT_RECALC_ANIM);
2418 /* ***************************************** */