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);
1164 if (array_len && array_index >= array_len) {
1165 if (G.debug & G_DEBUG) {
1166 printf("Animato: Invalid array index. ID = '%s', '%s[%d]', array length is %d\n",
1167 (ptr && ptr->id.data) ? (((ID *)ptr->id.data)->name + 2) : "<No ID>",
1168 path, array_index, array_len - 1);
1174 switch (RNA_property_type(prop)) {
1177 RNA_property_boolean_set_index(&new_ptr, prop, array_index, ANIMSYS_FLOAT_AS_BOOL(value));
1179 RNA_property_boolean_set(&new_ptr, prop, ANIMSYS_FLOAT_AS_BOOL(value));
1183 RNA_property_int_set_index(&new_ptr, prop, array_index, (int)value);
1185 RNA_property_int_set(&new_ptr, prop, (int)value);
1189 RNA_property_float_set_index(&new_ptr, prop, array_index, value);
1191 RNA_property_float_set(&new_ptr, prop, value);
1194 RNA_property_enum_set(&new_ptr, prop, (int)value);
1197 /* nothing can be done here... so it is unsuccessful? */
1201 /* RNA property update disabled for now - [#28525] [#28690] [#28774] [#28777] */
1203 /* buffer property update for later flushing */
1204 if (RNA_property_update_check(prop)) {
1205 short skip_updates_hack = 0;
1207 /* optimization hacks: skip property updates for those properties
1208 * for we know that which the updates in RNA were really just for
1209 * flushing property editing via UI/Py
1211 if (new_ptr.type == &RNA_PoseBone) {
1212 /* bone transforms - update pose (i.e. tag depsgraph) */
1213 skip_updates_hack = 1;
1216 if (skip_updates_hack == 0)
1217 RNA_property_update_cache_add(&new_ptr, prop);
1221 /* as long as we don't do property update, we still tag datablock
1222 * as having been updated. this flag does not cause any updates to
1223 * be run, it's for e.g. render engines to synchronize data */
1224 if (new_ptr.id.data) {
1225 ID *id = new_ptr.id.data;
1226 id->flag |= LIB_ID_RECALC;
1227 DAG_id_type_tag(G.main, GS(id->name));
1235 /* failed to get path */
1236 /* XXX don't tag as failed yet though, as there are some legit situations (Action Constraint)
1237 * where some channels will not exist, but shouldn't lock up Action */
1238 if (G.debug & G_DEBUG) {
1239 printf("Animato: Invalid path. ID = '%s', '%s[%d]'\n",
1240 (ptr && ptr->id.data) ? (((ID *)ptr->id.data)->name + 2) : "<No ID>",
1247 /* Simple replacement based data-setting of the FCurve using RNA */
1248 static short animsys_execute_fcurve(PointerRNA *ptr, AnimMapper *remap, FCurve *fcu)
1251 short free_path = 0;
1254 /* get path, remapped as appropriate to work in its new environment */
1255 free_path = animsys_remap_path(remap, fcu->rna_path, &path);
1257 /* write value to setting */
1259 ok = animsys_write_rna_setting(ptr, path, fcu->array_index, fcu->curval);
1261 /* free temp path-info */
1265 /* return whether we were successful */
1269 /* Evaluate all the F-Curves in the given list
1270 * This performs a set of standard checks. If extra checks are required, separate code should be used
1272 static void animsys_evaluate_fcurves(PointerRNA *ptr, ListBase *list, AnimMapper *remap, float ctime)
1276 /* calculate then execute each curve */
1277 for (fcu = list->first; fcu; fcu = fcu->next) {
1278 /* check if this F-Curve doesn't belong to a muted group */
1279 if ((fcu->grp == NULL) || (fcu->grp->flag & AGRP_MUTED) == 0) {
1280 /* check if this curve should be skipped */
1281 if ((fcu->flag & (FCURVE_MUTED | FCURVE_DISABLED)) == 0) {
1282 calculate_fcurve(fcu, ctime);
1283 animsys_execute_fcurve(ptr, remap, fcu);
1289 /* ***************************************** */
1290 /* Driver Evaluation */
1292 /* Evaluate Drivers */
1293 static void animsys_evaluate_drivers(PointerRNA *ptr, AnimData *adt, float ctime)
1297 /* drivers are stored as F-Curves, but we cannot use the standard code, as we need to check if
1298 * the depsgraph requested that this driver be evaluated...
1300 for (fcu = adt->drivers.first; fcu; fcu = fcu->next) {
1301 ChannelDriver *driver = fcu->driver;
1304 /* check if this driver's curve should be skipped */
1305 if ((fcu->flag & (FCURVE_MUTED | FCURVE_DISABLED)) == 0) {
1306 /* check if driver itself is tagged for recalculation */
1307 /* XXX driver recalc flag is not set yet by depsgraph! */
1308 if ((driver) && !(driver->flag & DRIVER_FLAG_INVALID) /*&& (driver->flag & DRIVER_FLAG_RECALC)*/) {
1309 /* evaluate this using values set already in other places
1310 * NOTE: for 'layering' option later on, we should check if we should remove old value before adding
1311 * new to only be done when drivers only changed */
1312 calculate_fcurve(fcu, ctime);
1313 ok = animsys_execute_fcurve(ptr, NULL, fcu);
1315 /* clear recalc flag */
1316 driver->flag &= ~DRIVER_FLAG_RECALC;
1318 /* set error-flag if evaluation failed */
1320 driver->flag |= DRIVER_FLAG_INVALID;
1326 /* ***************************************** */
1327 /* Actions Evaluation */
1329 /* strictly not necessary for actual "evaluation", but it is a useful safety check
1330 * to reduce the amount of times that users end up having to "revive" wrongly-assigned
1333 static void action_idcode_patch_check(ID *id, bAction *act)
1338 if (ELEM(NULL, id, act))
1341 idcode = GS(id->name);
1343 /* the actual checks... hopefully not too much of a performance hit in the long run... */
1344 if (act->idroot == 0) {
1345 /* use the current root if not set already (i.e. newly created actions and actions from 2.50-2.57 builds)
1346 * - this has problems if there are 2 users, and the first one encountered is the invalid one
1347 * in which case, the user will need to manually fix this (?)
1349 act->idroot = idcode;
1351 else if (act->idroot != idcode) {
1352 /* only report this error if debug mode is enabled (to save performance everywhere else) */
1353 if (G.debug & G_DEBUG) {
1354 printf("AnimSys Safety Check Failed: Action '%s' is not meant to be used from ID-Blocks of type %d such as '%s'\n",
1355 act->id.name + 2, idcode, id->name);
1360 /* ----------------------------------------- */
1362 /* Evaluate Action Group */
1363 void animsys_evaluate_action_group(PointerRNA *ptr, bAction *act, bActionGroup *agrp, AnimMapper *remap, float ctime)
1367 /* check if mapper is appropriate for use here (we set to NULL if it's inappropriate) */
1368 if (ELEM(NULL, act, agrp)) return;
1369 if ((remap) && (remap->target != act)) remap = NULL;
1371 action_idcode_patch_check(ptr->id.data, act);
1373 /* if group is muted, don't evaluated any of the F-Curve */
1374 if (agrp->flag & AGRP_MUTED)
1377 /* calculate then execute each curve */
1378 for (fcu = agrp->channels.first; (fcu) && (fcu->grp == agrp); fcu = fcu->next) {
1379 /* check if this curve should be skipped */
1380 if ((fcu->flag & (FCURVE_MUTED | FCURVE_DISABLED)) == 0) {
1381 calculate_fcurve(fcu, ctime);
1382 animsys_execute_fcurve(ptr, remap, fcu);
1387 /* Evaluate Action (F-Curve Bag) */
1388 void animsys_evaluate_action(PointerRNA *ptr, bAction *act, AnimMapper *remap, float ctime)
1390 /* check if mapper is appropriate for use here (we set to NULL if it's inappropriate) */
1391 if (act == NULL) return;
1392 if ((remap) && (remap->target != act)) remap = NULL;
1394 action_idcode_patch_check(ptr->id.data, act);
1396 /* calculate then execute each curve */
1397 animsys_evaluate_fcurves(ptr, &act->curves, remap, ctime);
1400 /* ***************************************** */
1401 /* NLA System - Evaluation */
1403 /* calculate influence of strip based for given frame based on blendin/out values */
1404 static float nlastrip_get_influence(NlaStrip *strip, float cframe)
1406 /* sanity checks - normalize the blendin/out values? */
1407 strip->blendin = fabsf(strip->blendin);
1408 strip->blendout = fabsf(strip->blendout);
1410 /* result depends on where frame is in respect to blendin/out values */
1411 if (IS_EQ(strip->blendin, 0) == 0 && (cframe <= (strip->start + strip->blendin))) {
1412 /* there is some blend-in */
1413 return fabsf(cframe - strip->start) / (strip->blendin);
1415 else if (IS_EQ(strip->blendout, 0) == 0 && (cframe >= (strip->end - strip->blendout))) {
1416 /* there is some blend-out */
1417 return fabsf(strip->end - cframe) / (strip->blendout);
1420 /* in the middle of the strip, we should be full strength */
1425 /* evaluate the evaluation time and influence for the strip, storing the results in the strip */
1426 static void nlastrip_evaluate_controls(NlaStrip *strip, float ctime)
1428 /* firstly, analytically generate values for influence and time (if applicable) */
1429 if ((strip->flag & NLASTRIP_FLAG_USR_TIME) == 0)
1430 strip->strip_time = nlastrip_get_frame(strip, ctime, NLATIME_CONVERT_EVAL);
1431 if ((strip->flag & NLASTRIP_FLAG_USR_INFLUENCE) == 0)
1432 strip->influence = nlastrip_get_influence(strip, ctime);
1434 /* now strip's evaluate F-Curves for these settings (if applicable) */
1435 if (strip->fcurves.first) {
1436 PointerRNA strip_ptr;
1438 /* create RNA-pointer needed to set values */
1439 RNA_pointer_create(NULL, &RNA_NlaStrip, strip, &strip_ptr);
1441 /* execute these settings as per normal */
1442 animsys_evaluate_fcurves(&strip_ptr, &strip->fcurves, NULL, ctime);
1445 /* if user can control the evaluation time (using F-Curves), consider the option which allows this time to be clamped
1446 * to lie within extents of the action-clip, so that a steady changing rate of progress through several cycles of the clip
1447 * can be achieved easily
1449 /* NOTE: if we add any more of these special cases, we better group them up nicely... */
1450 if ((strip->flag & NLASTRIP_FLAG_USR_TIME) && (strip->flag & NLASTRIP_FLAG_USR_TIME_CYCLIC))
1451 strip->strip_time = fmod(strip->strip_time - strip->actstart, strip->actend - strip->actstart);
1454 /* gets the strip active at the current time for a list of strips for evaluation purposes */
1455 NlaEvalStrip *nlastrips_ctime_get_strip(ListBase *list, ListBase *strips, short index, float ctime)
1457 NlaStrip *strip, *estrip = NULL;
1461 /* loop over strips, checking if they fall within the range */
1462 for (strip = strips->first; strip; strip = strip->next) {
1463 /* check if current time occurs within this strip */
1464 if (IN_RANGE_INCL(ctime, strip->start, strip->end)) {
1465 /* this strip is active, so try to use it */
1467 side = NES_TIME_WITHIN;
1471 /* if time occurred before current strip... */
1472 if (ctime < strip->start) {
1473 if (strip == strips->first) {
1474 /* before first strip - only try to use it if it extends backwards in time too */
1475 if (strip->extendmode == NLASTRIP_EXTEND_HOLD)
1478 /* side is 'before' regardless of whether there's a useful strip */
1479 side = NES_TIME_BEFORE;
1482 /* before next strip - previous strip has ended, but next hasn't begun,
1483 * so blending mode depends on whether strip is being held or not...
1484 * - only occurs when no transition strip added, otherwise the transition would have
1485 * been picked up above...
1487 strip = strip->prev;
1489 if (strip->extendmode != NLASTRIP_EXTEND_NOTHING)
1491 side = NES_TIME_AFTER;
1496 /* if time occurred after current strip... */
1497 if (ctime > strip->end) {
1498 /* only if this is the last strip should we do anything, and only if that is being held */
1499 if (strip == strips->last) {
1500 if (strip->extendmode != NLASTRIP_EXTEND_NOTHING)
1503 side = NES_TIME_AFTER;
1507 /* otherwise, skip... as the 'before' case will catch it more elegantly! */
1511 /* check if a valid strip was found
1512 * - must not be muted (i.e. will have contribution
1514 if ((estrip == NULL) || (estrip->flag & NLASTRIP_FLAG_MUTED))
1517 /* if ctime was not within the boundaries of the strip, clamp! */
1519 case NES_TIME_BEFORE: /* extend first frame only */
1520 ctime = estrip->start;
1522 case NES_TIME_AFTER: /* extend last frame only */
1523 ctime = estrip->end;
1527 /* evaluate strip's evaluation controls
1528 * - skip if no influence (i.e. same effect as muting the strip)
1529 * - negative influence is not supported yet... how would that be defined?
1531 /* TODO: this sounds a bit hacky having a few isolated F-Curves stuck on some data it operates on... */
1532 nlastrip_evaluate_controls(estrip, ctime);
1533 if (estrip->influence <= 0.0f)
1536 /* check if strip has valid data to evaluate,
1537 * and/or perform any additional type-specific actions
1539 switch (estrip->type) {
1540 case NLASTRIP_TYPE_CLIP:
1541 /* clip must have some action to evaluate */
1542 if (estrip->act == NULL)
1545 case NLASTRIP_TYPE_TRANSITION:
1546 /* there must be strips to transition from and to (i.e. prev and next required) */
1547 if (ELEM(NULL, estrip->prev, estrip->next))
1550 /* evaluate controls for the relevant extents of the bordering strips... */
1551 nlastrip_evaluate_controls(estrip->prev, estrip->start);
1552 nlastrip_evaluate_controls(estrip->next, estrip->end);
1556 /* add to list of strips we need to evaluate */
1557 nes = MEM_callocN(sizeof(NlaEvalStrip), "NlaEvalStrip");
1559 nes->strip = estrip;
1560 nes->strip_mode = side;
1561 nes->track_index = index;
1562 nes->strip_time = estrip->strip_time;
1565 BLI_addtail(list, nes);
1570 /* ---------------------- */
1572 /* find an NlaEvalChannel that matches the given criteria
1573 * - ptr and prop are the RNA data to find a match for
1575 static NlaEvalChannel *nlaevalchan_find_match(ListBase *channels, PointerRNA *ptr, PropertyRNA *prop, int array_index)
1577 NlaEvalChannel *nec;
1580 if (channels == NULL)
1583 /* loop through existing channels, checking for a channel which affects the same property */
1584 for (nec = channels->first; nec; nec = nec->next) {
1585 /* - comparing the PointerRNA's is done by comparing the pointers
1586 * to the actual struct the property resides in, since that all the
1587 * other data stored in PointerRNA cannot allow us to definitively
1590 if ((nec->ptr.data == ptr->data) && (nec->prop == prop) && (nec->index == array_index))
1598 /* verify that an appropriate NlaEvalChannel for this F-Curve exists */
1599 static NlaEvalChannel *nlaevalchan_verify(PointerRNA *ptr, ListBase *channels, NlaEvalStrip *nes, FCurve *fcu, short *newChan)
1601 NlaEvalChannel *nec;
1602 NlaStrip *strip = nes->strip;
1606 /* short free_path = 0; */
1609 if (channels == NULL)
1612 /* get RNA pointer+property info from F-Curve for more convenient handling */
1613 /* get path, remapped as appropriate to work in its new environment */
1614 /* free_path = */ /* UNUSED */ animsys_remap_path(strip->remap, fcu->rna_path, &path);
1616 /* a valid property must be available, and it must be animatable */
1617 if (RNA_path_resolve(ptr, path, &new_ptr, &prop) == 0) {
1618 if (G.debug & G_DEBUG) printf("NLA Strip Eval: Cannot resolve path\n");
1621 /* only ok if animatable */
1622 else if (RNA_property_animateable(&new_ptr, prop) == 0) {
1623 if (G.debug & G_DEBUG) printf("NLA Strip Eval: Property not animatable\n");
1627 /* try to find a match */
1628 nec = nlaevalchan_find_match(channels, &new_ptr, prop, fcu->array_index);
1630 /* allocate a new struct for this if none found */
1632 nec = MEM_callocN(sizeof(NlaEvalChannel), "NlaEvalChannel");
1634 BLI_addtail(channels, nec);
1638 nec->index = fcu->array_index;
1643 /* we can now return */
1647 /* accumulate (i.e. blend) the given value on to the channel it affects */
1648 static void nlaevalchan_accumulate(NlaEvalChannel *nec, NlaEvalStrip *nes, short newChan, float value)
1650 NlaStrip *strip = nes->strip;
1651 short blendmode = strip->blendmode;
1652 float inf = strip->influence;
1654 /* if channel is new, just store value regardless of blending factors, etc. */
1660 /* if this is being performed as part of transition evaluation, incorporate
1661 * an additional weighting factor for the influence
1663 if (nes->strip_mode == NES_TIME_TRANSITION_END)
1664 inf *= nes->strip_time;
1666 /* premultiply the value by the weighting factor */
1667 if (IS_EQ(inf, 0)) return;
1670 /* perform blending */
1671 switch (blendmode) {
1672 case NLASTRIP_MODE_ADD:
1673 /* simply add the scaled value on to the stack */
1674 nec->value += value;
1677 case NLASTRIP_MODE_SUBTRACT:
1678 /* simply subtract the scaled value from the stack */
1679 nec->value -= value;
1682 case NLASTRIP_MODE_MULTIPLY:
1683 /* multiply the scaled value with the stack */
1684 nec->value *= value;
1687 case NLASTRIP_MODE_REPLACE:
1688 default: /* TODO: do we really want to blend by default? it seems more uses might prefer add... */
1689 /* do linear interpolation
1690 * - the influence of the accumulated data (elsewhere, that is called dstweight)
1691 * is 1 - influence, since the strip's influence is srcweight
1693 nec->value = nec->value * (1.0f - inf) + value;
1698 /* accumulate the results of a temporary buffer with the results of the full-buffer */
1699 static void nlaevalchan_buffers_accumulate(ListBase *channels, ListBase *tmp_buffer, NlaEvalStrip *nes)
1701 NlaEvalChannel *nec, *necn, *necd;
1703 /* optimize - abort if no channels */
1704 if (tmp_buffer->first == NULL)
1707 /* accumulate results in tmp_channels buffer to the accumulation buffer */
1708 for (nec = tmp_buffer->first; nec; nec = necn) {
1709 /* get pointer to next channel in case we remove the current channel from the temp-buffer */
1712 /* try to find an existing matching channel for this setting in the accumulation buffer */
1713 necd = nlaevalchan_find_match(channels, &nec->ptr, nec->prop, nec->index);
1715 /* if there was a matching channel already in the buffer, accumulate to it,
1716 * otherwise, add the current channel to the buffer for efficiency
1719 nlaevalchan_accumulate(necd, nes, 0, nec->value);
1721 BLI_remlink(tmp_buffer, nec);
1722 BLI_addtail(channels, nec);
1726 /* free temp-channels that haven't been assimilated into the buffer */
1727 BLI_freelistN(tmp_buffer);
1730 /* ---------------------- */
1731 /* F-Modifier stack joining/separation utilities - should we generalise these for BLI_listbase.h interface? */
1733 /* Temporarily join two lists of modifiers together, storing the result in a third list */
1734 static void nlaeval_fmodifiers_join_stacks(ListBase *result, ListBase *list1, ListBase *list2)
1736 FModifier *fcm1, *fcm2;
1738 /* if list1 is invalid... */
1739 if (ELEM(NULL, list1, list1->first)) {
1740 if (list2 && list2->first) {
1741 result->first = list2->first;
1742 result->last = list2->last;
1745 /* if list 2 is invalid... */
1746 else if (ELEM(NULL, list2, list2->first)) {
1747 result->first = list1->first;
1748 result->last = list1->last;
1751 /* list1 should be added first, and list2 second, with the endpoints of these being the endpoints for result
1752 * - the original lists must be left unchanged though, as we need that fact for restoring
1754 result->first = list1->first;
1755 result->last = list2->last;
1758 fcm2 = list2->first;
1765 /* Split two temporary lists of modifiers */
1766 static void nlaeval_fmodifiers_split_stacks(ListBase *list1, ListBase *list2)
1768 FModifier *fcm1, *fcm2;
1770 /* if list1/2 is invalid... just skip */
1771 if (ELEM(NULL, list1, list2))
1773 if (ELEM(NULL, list1->first, list2->first))
1778 fcm2 = list2->first;
1780 /* clear their links */
1785 /* ---------------------- */
1787 /* evaluate action-clip strip */
1788 static void nlastrip_evaluate_actionclip(PointerRNA *ptr, ListBase *channels, ListBase *modifiers, NlaEvalStrip *nes)
1790 ListBase tmp_modifiers = {NULL, NULL};
1791 NlaStrip *strip = nes->strip;
1795 /* sanity checks for action */
1799 if (strip->act == NULL) {
1800 printf("NLA-Strip Eval Error: Strip '%s' has no Action\n", strip->name);
1804 action_idcode_patch_check(ptr->id.data, strip->act);
1806 /* join this strip's modifiers to the parent's modifiers (own modifiers first) */
1807 nlaeval_fmodifiers_join_stacks(&tmp_modifiers, &strip->modifiers, modifiers);
1809 /* evaluate strip's modifiers which modify time to evaluate the base curves at */
1810 evaltime = evaluate_time_fmodifiers(&tmp_modifiers, NULL, 0.0f, strip->strip_time);
1812 /* evaluate all the F-Curves in the action, saving the relevant pointers to data that will need to be used */
1813 for (fcu = strip->act->curves.first; fcu; fcu = fcu->next) {
1814 NlaEvalChannel *nec;
1818 /* check if this curve should be skipped */
1819 if (fcu->flag & (FCURVE_MUTED | FCURVE_DISABLED))
1821 if ((fcu->grp) && (fcu->grp->flag & AGRP_MUTED))
1824 /* evaluate the F-Curve's value for the time given in the strip
1825 * NOTE: we use the modified time here, since strip's F-Curve Modifiers are applied on top of this
1827 value = evaluate_fcurve(fcu, evaltime);
1829 /* apply strip's F-Curve Modifiers on this value
1830 * NOTE: we apply the strip's original evaluation time not the modified one (as per standard F-Curve eval)
1832 evaluate_value_fmodifiers(&tmp_modifiers, fcu, &value, strip->strip_time);
1835 /* get an NLA evaluation channel to work with, and accumulate the evaluated value with the value(s)
1836 * stored in this channel if it has been used already
1838 nec = nlaevalchan_verify(ptr, channels, nes, fcu, &newChan);
1840 nlaevalchan_accumulate(nec, nes, newChan, value);
1843 /* unlink this strip's modifiers from the parent's modifiers again */
1844 nlaeval_fmodifiers_split_stacks(&strip->modifiers, modifiers);
1847 /* evaluate transition strip */
1848 static void nlastrip_evaluate_transition(PointerRNA *ptr, ListBase *channels, ListBase *modifiers, NlaEvalStrip *nes)
1850 ListBase tmp_channels = {NULL, NULL};
1851 ListBase tmp_modifiers = {NULL, NULL};
1852 NlaEvalStrip tmp_nes;
1855 /* join this strip's modifiers to the parent's modifiers (own modifiers first) */
1856 nlaeval_fmodifiers_join_stacks(&tmp_modifiers, &nes->strip->modifiers, modifiers);
1858 /* get the two strips to operate on
1859 * - we use the endpoints of the strips directly flanking our strip
1860 * using these as the endpoints of the transition (destination and source)
1861 * - these should have already been determined to be valid...
1862 * - if this strip is being played in reverse, we need to swap these endpoints
1863 * otherwise they will be interpolated wrong
1865 if (nes->strip->flag & NLASTRIP_FLAG_REVERSE) {
1866 s1 = nes->strip->next;
1867 s2 = nes->strip->prev;
1870 s1 = nes->strip->prev;
1871 s2 = nes->strip->next;
1874 /* prepare template for 'evaluation strip'
1875 * - based on the transition strip's evaluation strip data
1876 * - strip_mode is NES_TIME_TRANSITION_* based on which endpoint
1877 * - strip_time is the 'normalized' (i.e. in-strip) time for evaluation,
1878 * which doubles up as an additional weighting factor for the strip influences
1879 * which allows us to appear to be 'interpolating' between the two extremes
1883 /* evaluate these strips into a temp-buffer (tmp_channels) */
1884 /* FIXME: modifier evalation here needs some work... */
1886 tmp_nes.strip_mode = NES_TIME_TRANSITION_START;
1888 nlastrip_evaluate(ptr, &tmp_channels, &tmp_modifiers, &tmp_nes);
1891 tmp_nes.strip_mode = NES_TIME_TRANSITION_END;
1893 nlastrip_evaluate(ptr, &tmp_channels, &tmp_modifiers, &tmp_nes);
1896 /* assumulate temp-buffer and full-buffer, using the 'real' strip */
1897 nlaevalchan_buffers_accumulate(channels, &tmp_channels, nes);
1899 /* unlink this strip's modifiers from the parent's modifiers again */
1900 nlaeval_fmodifiers_split_stacks(&nes->strip->modifiers, modifiers);
1903 /* evaluate meta-strip */
1904 static void nlastrip_evaluate_meta(PointerRNA *ptr, ListBase *channels, ListBase *modifiers, NlaEvalStrip *nes)
1906 ListBase tmp_channels = {NULL, NULL};
1907 ListBase tmp_modifiers = {NULL, NULL};
1908 NlaStrip *strip = nes->strip;
1909 NlaEvalStrip *tmp_nes;
1912 /* meta-strip was calculated normally to have some time to be evaluated at
1913 * and here we 'look inside' the meta strip, treating it as a decorated window to
1914 * it's child strips, which get evaluated as if they were some tracks on a strip
1915 * (but with some extra modifiers to apply).
1917 * NOTE: keep this in sync with animsys_evaluate_nla()
1920 /* join this strip's modifiers to the parent's modifiers (own modifiers first) */
1921 nlaeval_fmodifiers_join_stacks(&tmp_modifiers, &strip->modifiers, modifiers);
1923 /* find the child-strip to evaluate */
1924 evaltime = (nes->strip_time * (strip->end - strip->start)) + strip->start;
1925 tmp_nes = nlastrips_ctime_get_strip(NULL, &strip->strips, -1, evaltime);
1926 if (tmp_nes == NULL)
1929 /* evaluate child-strip into tmp_channels buffer before accumulating
1930 * in the accumulation buffer
1932 nlastrip_evaluate(ptr, &tmp_channels, &tmp_modifiers, tmp_nes);
1934 /* assumulate temp-buffer and full-buffer, using the 'real' strip */
1935 nlaevalchan_buffers_accumulate(channels, &tmp_channels, nes);
1937 /* free temp eval-strip */
1940 /* unlink this strip's modifiers from the parent's modifiers again */
1941 nlaeval_fmodifiers_split_stacks(&strip->modifiers, modifiers);
1944 /* evaluates the given evaluation strip */
1945 void nlastrip_evaluate(PointerRNA *ptr, ListBase *channels, ListBase *modifiers, NlaEvalStrip *nes)
1947 NlaStrip *strip = nes->strip;
1949 /* to prevent potential infinite recursion problems (i.e. transition strip, beside meta strip containing a transition
1950 * several levels deep inside it), we tag the current strip as being evaluated, and clear this when we leave
1952 /* TODO: be careful with this flag, since some edit tools may be running and have set this while animplayback was running */
1953 if (strip->flag & NLASTRIP_FLAG_EDIT_TOUCHED)
1955 strip->flag |= NLASTRIP_FLAG_EDIT_TOUCHED;
1957 /* actions to take depend on the type of strip */
1958 switch (strip->type) {
1959 case NLASTRIP_TYPE_CLIP: /* action-clip */
1960 nlastrip_evaluate_actionclip(ptr, channels, modifiers, nes);
1962 case NLASTRIP_TYPE_TRANSITION: /* transition */
1963 nlastrip_evaluate_transition(ptr, channels, modifiers, nes);
1965 case NLASTRIP_TYPE_META: /* meta */
1966 nlastrip_evaluate_meta(ptr, channels, modifiers, nes);
1969 default: /* do nothing */
1973 /* clear temp recursion safe-check */
1974 strip->flag &= ~NLASTRIP_FLAG_EDIT_TOUCHED;
1977 /* write the accumulated settings to */
1978 void nladata_flush_channels(ListBase *channels)
1980 NlaEvalChannel *nec;
1983 if (channels == NULL)
1986 /* for each channel with accumulated values, write its value on the property it affects */
1987 for (nec = channels->first; nec; nec = nec->next) {
1988 PointerRNA *ptr = &nec->ptr;
1989 PropertyRNA *prop = nec->prop;
1990 int array_index = nec->index;
1991 float value = nec->value;
1993 /* write values - see animsys_write_rna_setting() to sync the code */
1994 switch (RNA_property_type(prop)) {
1996 if (RNA_property_array_length(ptr, prop))
1997 RNA_property_boolean_set_index(ptr, prop, array_index, ANIMSYS_FLOAT_AS_BOOL(value));
1999 RNA_property_boolean_set(ptr, prop, ANIMSYS_FLOAT_AS_BOOL(value));
2002 if (RNA_property_array_length(ptr, prop))
2003 RNA_property_int_set_index(ptr, prop, array_index, (int)value);
2005 RNA_property_int_set(ptr, prop, (int)value);
2008 if (RNA_property_array_length(ptr, prop))
2009 RNA_property_float_set_index(ptr, prop, array_index, value);
2011 RNA_property_float_set(ptr, prop, value);
2014 RNA_property_enum_set(ptr, prop, (int)value);
2017 /* can't do anything with other types of property.... */
2023 /* ---------------------- */
2025 /* NLA Evaluation function - values are calculated and stored in temporary "NlaEvalChannels"
2026 * ! This is exported so that keyframing code can use this for make use of it for anim layers support
2027 * > echannels: (list<NlaEvalChannels>) evaluation channels with calculated values
2029 static void animsys_evaluate_nla(ListBase *echannels, PointerRNA *ptr, AnimData *adt, float ctime)
2032 short track_index = 0;
2033 short has_strips = 0;
2035 ListBase estrips = {NULL, NULL};
2038 /* 1. get the stack of strips to evaluate at current time (influence calculated here) */
2039 for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next, track_index++) {
2040 /* stop here if tweaking is on and this strip is the tweaking track (it will be the first one that's 'disabled')... */
2041 if ((adt->flag & ADT_NLA_EDIT_ON) && (nlt->flag & NLATRACK_DISABLED))
2044 /* skip if we're only considering a track tagged 'solo' */
2045 if ((adt->flag & ADT_NLA_SOLO_TRACK) && (nlt->flag & NLATRACK_SOLO) == 0)
2047 /* skip if track is muted */
2048 if (nlt->flag & NLATRACK_MUTED)
2051 /* if this track has strips (but maybe they won't be suitable), set has_strips
2052 * - used for mainly for still allowing normal action evaluation...
2054 if (nlt->strips.first)
2057 /* otherwise, get strip to evaluate for this channel */
2058 nes = nlastrips_ctime_get_strip(&estrips, &nlt->strips, track_index, ctime);
2059 if (nes) nes->track = nlt;
2062 /* add 'active' Action (may be tweaking track) as last strip to evaluate in NLA stack
2063 * - only do this if we're not exclusively evaluating the 'solo' NLA-track
2064 * - however, if the 'solo' track houses the current 'tweaking' strip,
2065 * then we should allow this to play, otherwise nothing happens
2067 if ((adt->action) && ((adt->flag & ADT_NLA_SOLO_TRACK) == 0 || (adt->flag & ADT_NLA_EDIT_ON))) {
2068 /* if there are strips, evaluate action as per NLA rules */
2069 if ((has_strips) || (adt->actstrip)) {
2070 /* make dummy NLA strip, and add that to the stack */
2071 NlaStrip dummy_strip = {NULL};
2072 ListBase dummy_trackslist;
2074 dummy_trackslist.first = dummy_trackslist.last = &dummy_strip;
2076 if ((nlt) && !(adt->flag & ADT_NLA_EDIT_NOMAP)) {
2077 /* edit active action in-place according to its active strip, so copy the data */
2078 memcpy(&dummy_strip, adt->actstrip, sizeof(NlaStrip));
2079 dummy_strip.next = dummy_strip.prev = NULL;
2082 /* set settings of dummy NLA strip from AnimData settings */
2083 dummy_strip.act = adt->action;
2084 dummy_strip.remap = adt->remap;
2086 /* action range is calculated taking F-Modifiers into account (which making new strips doesn't do due to the troublesome nature of that) */
2087 calc_action_range(dummy_strip.act, &dummy_strip.actstart, &dummy_strip.actend, 1);
2088 dummy_strip.start = dummy_strip.actstart;
2089 dummy_strip.end = (IS_EQF(dummy_strip.actstart, dummy_strip.actend)) ? (dummy_strip.actstart + 1.0f) : (dummy_strip.actend);
2091 dummy_strip.blendmode = adt->act_blendmode;
2092 dummy_strip.extendmode = adt->act_extendmode;
2093 dummy_strip.influence = adt->act_influence;
2096 /* add this to our list of evaluation strips */
2097 nlastrips_ctime_get_strip(&estrips, &dummy_trackslist, -1, ctime);
2100 /* special case - evaluate as if there isn't any NLA data */
2101 /* TODO: this is really just a stop-gap measure... */
2102 animsys_evaluate_action(ptr, adt->action, adt->remap, ctime);
2107 /* only continue if there are strips to evaluate */
2108 if (estrips.first == NULL)
2112 /* 2. for each strip, evaluate then accumulate on top of existing channels, but don't set values yet */
2113 for (nes = estrips.first; nes; nes = nes->next)
2114 nlastrip_evaluate(ptr, echannels, NULL, nes);
2116 /* 3. free temporary evaluation data that's not used elsewhere */
2117 BLI_freelistN(&estrips);
2120 /* NLA Evaluation function (mostly for use through do_animdata)
2121 * - All channels that will be affected are not cleared anymore. Instead, we just evaluate into
2122 * some temp channels, where values can be accumulated in one go.
2124 static void animsys_calculate_nla(PointerRNA *ptr, AnimData *adt, float ctime)
2126 ListBase echannels = {NULL, NULL};
2128 /* TODO: need to zero out all channels used, otherwise we have problems with threadsafety
2129 * and also when the user jumps between different times instead of moving sequentially... */
2131 /* evaluate the NLA stack, obtaining a set of values to flush */
2132 animsys_evaluate_nla(&echannels, ptr, adt, ctime);
2134 /* flush effects of accumulating channels in NLA to the actual data they affect */
2135 nladata_flush_channels(&echannels);
2137 /* free temp data */
2138 BLI_freelistN(&echannels);
2141 /* ***************************************** */
2142 /* Overrides System - Public API */
2144 /* Clear all overides */
2146 /* Add or get existing Override for given setting */
2148 AnimOverride *BKE_animsys_validate_override(PointerRNA *UNUSED(ptr), char *UNUSED(path), int UNUSED(array_index))
2150 /* FIXME: need to define how to get overrides */
2155 /* -------------------- */
2157 /* Evaluate Overrides */
2158 static void animsys_evaluate_overrides(PointerRNA *ptr, AnimData *adt)
2162 /* for each override, simply execute... */
2163 for (aor = adt->overrides.first; aor; aor = aor->next)
2164 animsys_write_rna_setting(ptr, aor->rna_path, aor->array_index, aor->value);
2167 /* ***************************************** */
2168 /* Evaluation System - Public API */
2170 /* Overview of how this system works:
2171 * 1) Depsgraph sorts data as necessary, so that data is in an order that means
2172 * that all dependencies are resolved before dependants.
2173 * 2) All normal animation is evaluated, so that drivers have some basis values to
2175 * a. NLA stacks are done first, as the Active Actions act as 'tweaking' tracks
2176 * which modify the effects of the NLA-stacks
2177 * b. Active Action is evaluated as per normal, on top of the results of the NLA tracks
2179 * --------------< often in a separate phase... >------------------
2181 * 3) Drivers/expressions are evaluated on top of this, in an order where dependencies are
2183 * Note: it may be necessary to have some tools to handle the cases where some higher-level
2184 * drivers are added and cause some problematic dependencies that didn't exist in the local levels...
2186 * --------------< always executed >------------------
2188 * Maintenance of editability of settings (XXX):
2189 * In order to ensure that settings that are animated can still be manipulated in the UI without requiring
2190 * that keyframes are added to prevent these values from being overwritten, we use 'overrides'.
2192 * Unresolved things:
2193 * - Handling of multi-user settings (i.e. time-offset, group-instancing) -> big cache grids or nodal system? but stored where?
2194 * - Multiple-block dependencies (i.e. drivers for settings are in both local and higher levels) -> split into separate lists?
2197 * - Currently (as of September 2009), overrides we haven't needed to (fully) implement overrides.
2198 * However, the code for this is relatively harmless, so is left in the code for now.
2201 /* Evaluation loop for evaluation animation data
2203 * This assumes that the animation-data provided belongs to the ID block in question,
2204 * and that the flags for which parts of the anim-data settings need to be recalculated
2205 * have been set already by the depsgraph. Now, we use the recalc
2207 void BKE_animsys_evaluate_animdata(Scene *scene, ID *id, AnimData *adt, float ctime, short recalc)
2212 if (ELEM(NULL, id, adt))
2215 /* get pointer to ID-block for RNA to use */
2216 RNA_id_pointer_create(id, &id_ptr);
2218 /* recalculate keyframe data:
2219 * - NLA before Active Action, as Active Action behaves as 'tweaking track'
2220 * that overrides 'rough' work in NLA
2222 /* TODO: need to double check that this all works correctly */
2223 if ((recalc & ADT_RECALC_ANIM) || (adt->recalc & ADT_RECALC_ANIM)) {
2224 /* evaluate NLA data */
2225 if ((adt->nla_tracks.first) && !(adt->flag & ADT_NLA_EVAL_OFF)) {
2226 /* evaluate NLA-stack
2227 * - active action is evaluated as part of the NLA stack as the last item
2229 animsys_calculate_nla(&id_ptr, adt, ctime);
2231 /* evaluate Active Action only */
2232 else if (adt->action)
2233 animsys_evaluate_action(&id_ptr, adt->action, adt->remap, ctime);
2236 adt->recalc &= ~ADT_RECALC_ANIM;
2239 /* recalculate drivers
2240 * - Drivers need to be evaluated afterwards, as they can either override
2241 * or be layered on top of existing animation data.
2242 * - Drivers should be in the appropriate order to be evaluated without problems...
2244 if ((recalc & ADT_RECALC_DRIVERS)
2245 /* XXX for now, don't check yet, as depsgraph hasn't been updated */
2246 /* && (adt->recalc & ADT_RECALC_DRIVERS)*/)
2248 animsys_evaluate_drivers(&id_ptr, adt, ctime);
2251 /* always execute 'overrides'
2252 * - Overrides allow editing, by overwriting the value(s) set from animation-data, with the
2253 * value last set by the user (and not keyframed yet).
2254 * - Overrides are cleared upon frame change and/or keyframing
2255 * - It is best that we execute this everytime, so that no errors are likely to occur.
2257 animsys_evaluate_overrides(&id_ptr, adt);
2259 /* execute and clear all cached property update functions */
2261 Main *bmain = G.main; // xxx - to get passed in!
2262 RNA_property_update_cache_flush(bmain, scene);
2263 RNA_property_update_cache_free();
2266 /* clear recalc flag now */
2270 /* Evaluation of all ID-blocks with Animation Data blocks - Animation Data Only
2272 * This will evaluate only the animation info available in the animation data-blocks
2273 * encountered. In order to enforce the system by which some settings controlled by a
2274 * 'local' (i.e. belonging in the nearest ID-block that setting is related to, not a
2275 * standard 'root') block are overridden by a larger 'user'
2277 void BKE_animsys_evaluate_all_animation(Main *main, Scene *scene, float ctime)
2281 if (G.debug & G_DEBUG)
2282 printf("Evaluate all animation - %f\n", ctime);
2284 /* macros for less typing
2285 * - only evaluate animation data for id if it has users (and not just fake ones)
2286 * - whether animdata exists is checked for by the evaluation function, though taking
2287 * this outside of the function may make things slightly faster?
2289 #define EVAL_ANIM_IDS(first, aflag) \
2290 for (id = first; id; id = id->next) { \
2291 if (ID_REAL_USERS(id) > 0) { \
2292 AnimData *adt = BKE_animdata_from_id(id); \
2293 BKE_animsys_evaluate_animdata(scene, id, adt, ctime, aflag); \
2297 /* another macro for the "embedded" nodetree cases
2298 * - this is like EVAL_ANIM_IDS, but this handles the case "embedded nodetrees"
2299 * (i.e. scene/material/texture->nodetree) which we need a special exception
2300 * for, otherwise they'd get skipped
2301 * - ntp = "node tree parent" = datablock where node tree stuff resides
2303 #define EVAL_ANIM_NODETREE_IDS(first, NtId_Type, aflag) \
2304 for (id = first; id; id = id->next) { \
2305 if (ID_REAL_USERS(id) > 0) { \
2306 AnimData *adt = BKE_animdata_from_id(id); \
2307 NtId_Type *ntp = (NtId_Type *)id; \
2308 if (ntp->nodetree) { \
2309 AnimData *adt2 = BKE_animdata_from_id((ID *)ntp->nodetree); \
2310 BKE_animsys_evaluate_animdata(scene, (ID *)ntp->nodetree, adt2, ctime, ADT_RECALC_ANIM); \
2312 BKE_animsys_evaluate_animdata(scene, id, adt, ctime, aflag); \
2317 * when there are no actions, don't go over database and loop over heaps of datablocks,
2318 * which should ultimately be empty, since it is not possible for now to have any animation
2319 * without some actions, and drivers wouldn't get affected by any state changes
2321 * however, if there are some curves, we will need to make sure that their 'ctime' property gets
2322 * set correctly, so this optimization must be skipped in that case...
2324 if ((main->action.first == NULL) && (main->curve.first == NULL)) {
2325 if (G.debug & G_DEBUG)
2326 printf("\tNo Actions, so no animation needs to be evaluated...\n");
2332 EVAL_ANIM_IDS(main->nodetree.first, ADT_RECALC_ANIM);
2335 EVAL_ANIM_NODETREE_IDS(main->tex.first, Tex, ADT_RECALC_ANIM);
2338 EVAL_ANIM_NODETREE_IDS(main->lamp.first, Lamp, ADT_RECALC_ANIM);
2341 EVAL_ANIM_NODETREE_IDS(main->mat.first, Material, ADT_RECALC_ANIM);
2344 EVAL_ANIM_IDS(main->camera.first, ADT_RECALC_ANIM);
2347 EVAL_ANIM_IDS(main->key.first, ADT_RECALC_ANIM);
2350 EVAL_ANIM_IDS(main->mball.first, ADT_RECALC_ANIM);
2353 EVAL_ANIM_IDS(main->curve.first, ADT_RECALC_ANIM);
2356 EVAL_ANIM_IDS(main->armature.first, ADT_RECALC_ANIM);
2359 EVAL_ANIM_IDS(main->latt.first, ADT_RECALC_ANIM);
2362 EVAL_ANIM_IDS(main->mesh.first, ADT_RECALC_ANIM);
2365 EVAL_ANIM_IDS(main->particle.first, ADT_RECALC_ANIM);
2368 EVAL_ANIM_IDS(main->speaker.first, ADT_RECALC_ANIM);
2371 EVAL_ANIM_IDS(main->movieclip.first, ADT_RECALC_ANIM);
2374 /* ADT_RECALC_ANIM doesn't need to be supplied here, since object AnimData gets
2375 * this tagged by Depsgraph on framechange. This optimization means that objects
2376 * linked from other (not-visible) scenes will not need their data calculated.
2378 EVAL_ANIM_IDS(main->object.first, 0);
2381 EVAL_ANIM_IDS(main->mask.first, ADT_RECALC_ANIM);
2384 EVAL_ANIM_NODETREE_IDS(main->world.first, World, ADT_RECALC_ANIM);
2387 EVAL_ANIM_NODETREE_IDS(main->scene.first, Scene, ADT_RECALC_ANIM);
2390 /* ***************************************** */