Blender  V3.3
anim_data.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2009 Blender Foundation, Joshua Leung. All rights reserved. */
3 
7 #include "MEM_guardedalloc.h"
8 
9 #include <string.h>
10 
11 #include "BKE_action.h"
12 #include "BKE_anim_data.h"
13 #include "BKE_animsys.h"
14 #include "BKE_context.h"
15 #include "BKE_fcurve.h"
16 #include "BKE_fcurve_driver.h"
17 #include "BKE_global.h"
18 #include "BKE_idtype.h"
19 #include "BKE_lib_id.h"
20 #include "BKE_lib_query.h"
21 #include "BKE_main.h"
22 #include "BKE_nla.h"
23 #include "BKE_node.h"
24 #include "BKE_report.h"
25 
26 #include "DNA_ID.h"
27 #include "DNA_anim_types.h"
28 #include "DNA_light_types.h"
29 #include "DNA_material_types.h"
30 #include "DNA_node_types.h"
31 #include "DNA_space_types.h"
33 #include "DNA_world_types.h"
34 
35 #include "BLI_alloca.h"
36 #include "BLI_dynstr.h"
37 #include "BLI_listbase.h"
38 #include "BLI_string.h"
39 #include "BLI_utildefines.h"
40 
41 #include "DEG_depsgraph.h"
42 
43 #include "BLO_read_write.h"
44 
45 #include "RNA_access.h"
46 #include "RNA_path.h"
47 
48 #include "CLG_log.h"
49 
50 static CLG_LogRef LOG = {"bke.anim_sys"};
51 
52 /* ***************************************** */
53 /* AnimData API */
54 
55 /* Getter/Setter -------------------------------------------- */
56 
57 bool id_type_can_have_animdata(const short id_type)
58 {
59  const IDTypeInfo *typeinfo = BKE_idtype_get_info_from_idcode(id_type);
60  if (typeinfo != NULL) {
61  return (typeinfo->flags & IDTYPE_FLAGS_NO_ANIMDATA) == 0;
62  }
63  return false;
64 }
65 
66 bool id_can_have_animdata(const ID *id)
67 {
68  /* sanity check */
69  if (id == NULL) {
70  return false;
71  }
72 
74 }
75 
77 {
78  /* In order for this to work, we assume that the #AnimData pointer is stored
79  * immediately after the given ID-block in the struct, as per IdAdtTemplate. */
80 
81  /* Only some ID-blocks have this info for now, so we cast the types that do
82  * to be of type IdAdtTemplate, and add the AnimData to it using the template. */
83  if (id_can_have_animdata(id)) {
84  IdAdtTemplate *iat = (IdAdtTemplate *)id;
85  return iat->adt;
86  }
87  return NULL;
88 }
89 
91 {
92  /* In order for this to work, we assume that the #AnimData pointer is stored
93  * immediately after the given ID-block in the struct, as per IdAdtTemplate. */
94 
95  /* Only some ID-blocks have this info for now, so we cast the types that do
96  * to be of type IdAdtTemplate, and add the AnimData to it using the template. */
97  if (id_can_have_animdata(id)) {
98  IdAdtTemplate *iat = (IdAdtTemplate *)id;
99 
100  /* check if there's already AnimData, in which case, don't add */
101  if (iat->adt == NULL) {
102  AnimData *adt;
103 
104  /* add animdata */
105  adt = iat->adt = MEM_callocN(sizeof(AnimData), "AnimData");
106 
107  /* set default settings */
108  adt->act_influence = 1.0f;
109  }
110 
111  return iat->adt;
112  }
113  return NULL;
114 }
115 
116 /* Action Setter --------------------------------------- */
117 
118 bool BKE_animdata_set_action(ReportList *reports, ID *id, bAction *act)
119 {
120  AnimData *adt = BKE_animdata_from_id(id);
121 
122  /* Animdata validity check. */
123  if (adt == NULL) {
124  BKE_report(reports, RPT_WARNING, "No AnimData to set action on");
125  return false;
126  }
127 
128  if (adt->action == act) {
129  /* Don't bother reducing and increasing the user count when there is nothing changing. */
130  return true;
131  }
132 
133  if (!BKE_animdata_action_editable(adt)) {
134  /* Cannot remove, otherwise things turn to custard. */
135  BKE_report(reports, RPT_ERROR, "Cannot change action, as it is still being edited in NLA");
136  return false;
137  }
138 
139  /* Reduce usercount for current action. */
140  if (adt->action) {
141  id_us_min((ID *)adt->action);
142  }
143 
144  if (act == NULL) {
145  /* Just clearing the action. */
146  adt->action = NULL;
147  return true;
148  }
149 
150  /* Action must have same type as owner. */
151  if (!BKE_animdata_action_ensure_idroot(id, act)) {
152  /* Cannot set to this type. */
153  BKE_reportf(
154  reports,
155  RPT_ERROR,
156  "Could not set action '%s' onto ID '%s', as it does not have suitably rooted paths "
157  "for this purpose",
158  act->id.name + 2,
159  id->name);
160  return false;
161  }
162 
163  adt->action = act;
164  id_us_plus((ID *)adt->action);
165 
166  return true;
167 }
168 
170 {
171  /* Active action is only editable when it is not a tweaking strip. */
172  const bool is_tweaking_strip = (adt->flag & ADT_NLA_EDIT_ON) || adt->actstrip != NULL ||
173  adt->tmpact != NULL;
174  return !is_tweaking_strip;
175 }
176 
177 bool BKE_animdata_action_ensure_idroot(const ID *owner, bAction *action)
178 {
179  const int idcode = GS(owner->name);
180 
181  if (action == NULL) {
182  /* A NULL action is usable by any ID type. */
183  return true;
184  }
185 
186  if (action->idroot == 0) {
187  /* First time this Action is assigned, lock it to this ID type. */
188  action->idroot = idcode;
189  return true;
190  }
191 
192  return (action->idroot == idcode);
193 }
194 
195 /* Freeing -------------------------------------------- */
196 
197 void BKE_animdata_free(ID *id, const bool do_id_user)
198 {
199  /* Only some ID-blocks have this info for now, so we cast the
200  * types that do to be of type IdAdtTemplate
201  */
202  if (id_can_have_animdata(id)) {
203  IdAdtTemplate *iat = (IdAdtTemplate *)id;
204  AnimData *adt = iat->adt;
205 
206  /* check if there's any AnimData to start with */
207  if (adt) {
208  if (do_id_user) {
209  /* unlink action (don't free, as it's in its own list) */
210  if (adt->action) {
211  id_us_min(&adt->action->id);
212  }
213  /* same goes for the temporarily displaced action */
214  if (adt->tmpact) {
215  id_us_min(&adt->tmpact->id);
216  }
217  }
218 
219  /* free nla data */
220  BKE_nla_tracks_free(&adt->nla_tracks, do_id_user);
221 
222  /* free drivers - stored as a list of F-Curves */
223  BKE_fcurves_free(&adt->drivers);
224 
225  /* free driver array cache */
227 
228  /* free overrides */
229  /* TODO... */
230 
231  /* free animdata now */
232  MEM_freeN(adt);
233  iat->adt = NULL;
234  }
235  }
236 }
237 
238 bool BKE_animdata_id_is_animated(const struct ID *id)
239 {
240  if (id == NULL) {
241  return false;
242  }
243 
244  const AnimData *adt = BKE_animdata_from_id((ID *)id);
245  if (adt == NULL) {
246  return false;
247  }
248 
249  if (adt->action != NULL && !BLI_listbase_is_empty(&adt->action->curves)) {
250  return true;
251  }
252 
255 }
256 
258 {
259  LISTBASE_FOREACH (FCurve *, fcu, &adt->drivers) {
261  }
262 
265 
266  LISTBASE_FOREACH (NlaTrack *, nla_track, &adt->nla_tracks) {
267  LISTBASE_FOREACH (NlaStrip *, nla_strip, &nla_track->strips) {
268  BKE_nla_strip_foreach_id(nla_strip, data);
269  }
270  }
271 }
272 
273 /* Copying -------------------------------------------- */
274 
275 AnimData *BKE_animdata_copy(Main *bmain, AnimData *adt, const int flag)
276 {
277  AnimData *dadt;
278 
279  const bool do_action = (flag & LIB_ID_COPY_ACTIONS) != 0 && (flag & LIB_ID_CREATE_NO_MAIN) == 0;
280  const bool do_id_user = (flag & LIB_ID_CREATE_NO_USER_REFCOUNT) == 0;
281 
282  /* sanity check before duplicating struct */
283  if (adt == NULL) {
284  return NULL;
285  }
286  dadt = MEM_dupallocN(adt);
287 
288  /* make a copy of action - at worst, user has to delete copies... */
289  if (do_action) {
290  /* Recursive copy of 'real' IDs is a bit hairy. Even if do not want to deal with usercount
291  * when copying ID's data itself, we still need to do so with sub-IDs, since those will not be
292  * handled by later 'update usercounts of used IDs' code as used e.g. at end of
293  * BKE_id_copy_ex().
294  * So in case we do copy the ID and its sub-IDs in bmain, silence the 'no usercount' flag for
295  * the sub-IDs copying.
296  * NOTE: This is a bit weak, as usually when it comes to recursive ID copy. Should work for
297  * now, but we may have to revisit this at some point and add a proper extra flag to deal with
298  * that situation. Or refactor completely the way we handle such recursion, by flattening it
299  * e.g. */
300  const int id_copy_flag = (flag & LIB_ID_CREATE_NO_MAIN) == 0 ?
302  flag;
303  BLI_assert(bmain != NULL);
304  BLI_assert(dadt->action == NULL || dadt->action != dadt->tmpact);
305  dadt->action = (bAction *)BKE_id_copy_ex(bmain, (ID *)dadt->action, NULL, id_copy_flag);
306  dadt->tmpact = (bAction *)BKE_id_copy_ex(bmain, (ID *)dadt->tmpact, NULL, id_copy_flag);
307  }
308  else if (do_id_user) {
309  id_us_plus((ID *)dadt->action);
310  id_us_plus((ID *)dadt->tmpact);
311  }
312 
313  /* duplicate NLA data */
314  BKE_nla_tracks_copy_from_adt(bmain, dadt, adt, flag);
315 
316  /* duplicate drivers (F-Curves) */
317  BKE_fcurves_copy(&dadt->drivers, &adt->drivers);
318  dadt->driver_array = NULL;
319 
320  /* don't copy overrides */
322 
323  /* return */
324  return dadt;
325 }
326 
327 bool BKE_animdata_copy_id(Main *bmain, ID *id_to, ID *id_from, const int flag)
328 {
329  AnimData *adt;
330 
331  if ((id_to && id_from) && (GS(id_to->name) != GS(id_from->name))) {
332  return false;
333  }
334 
336 
337  adt = BKE_animdata_from_id(id_from);
338  if (adt) {
339  IdAdtTemplate *iat = (IdAdtTemplate *)id_to;
340  iat->adt = BKE_animdata_copy(bmain, adt, flag);
341  }
342 
343  return true;
344 }
345 
346 static void animdata_copy_id_action(Main *bmain,
347  ID *id,
348  const bool set_newid,
349  const bool do_linked_id)
350 {
351  AnimData *adt = BKE_animdata_from_id(id);
352  if (adt) {
353  if (adt->action && (do_linked_id || !ID_IS_LINKED(adt->action))) {
354  id_us_min((ID *)adt->action);
355  adt->action = set_newid ? ID_NEW_SET(adt->action, BKE_id_copy(bmain, &adt->action->id)) :
356  BKE_id_copy(bmain, &adt->action->id);
357  }
358  if (adt->tmpact && (do_linked_id || !ID_IS_LINKED(adt->tmpact))) {
359  id_us_min((ID *)adt->tmpact);
360  adt->tmpact = set_newid ? ID_NEW_SET(adt->tmpact, BKE_id_copy(bmain, &adt->tmpact->id)) :
361  BKE_id_copy(bmain, &adt->tmpact->id);
362  }
363  }
364  bNodeTree *ntree = ntreeFromID(id);
365  if (ntree) {
366  animdata_copy_id_action(bmain, &ntree->id, set_newid, do_linked_id);
367  }
368  /* Note that collections are not animatable currently, so no need to handle scenes' master
369  * collection here. */
370 }
371 
373 {
374  const bool is_id_liboverride = ID_IS_OVERRIDE_LIBRARY(id);
375  animdata_copy_id_action(bmain, id, false, !is_id_liboverride);
376 }
377 
379  struct ID *id,
380  const eDupli_ID_Flags duplicate_flags)
381 {
382  if (duplicate_flags & USER_DUP_ACT) {
383  animdata_copy_id_action(bmain, id, true, (duplicate_flags & USER_DUP_LINKED_ID) != 0);
384  }
385 }
386 
388  Main *bmain, ID *dst_id, ID *src_id, eAnimData_MergeCopy_Modes action_mode, bool fix_drivers)
389 {
390  AnimData *src = BKE_animdata_from_id(src_id);
391  AnimData *dst = BKE_animdata_from_id(dst_id);
392 
393  /* sanity checks */
394  if (ELEM(NULL, dst, src)) {
395  return;
396  }
397 
398  /* TODO: we must unset all "tweak-mode" flags. */
399  if ((src->flag & ADT_NLA_EDIT_ON) || (dst->flag & ADT_NLA_EDIT_ON)) {
400  CLOG_ERROR(
401  &LOG,
402  "Merging AnimData blocks while editing NLA is dangerous as it may cause data corruption");
403  return;
404  }
405 
406  /* handle actions... */
407  if (action_mode == ADT_MERGECOPY_SRC_COPY) {
408  /* make a copy of the actions */
409  dst->action = (bAction *)BKE_id_copy(bmain, &src->action->id);
410  dst->tmpact = (bAction *)BKE_id_copy(bmain, &src->tmpact->id);
411  }
412  else if (action_mode == ADT_MERGECOPY_SRC_REF) {
413  /* make a reference to it */
414  dst->action = src->action;
415  id_us_plus((ID *)dst->action);
416 
417  dst->tmpact = src->tmpact;
418  id_us_plus((ID *)dst->tmpact);
419  }
420 
421  /* duplicate NLA data */
422  if (src->nla_tracks.first) {
423  ListBase tracks = {NULL, NULL};
424 
425  BKE_nla_tracks_copy(bmain, &tracks, &src->nla_tracks, 0);
427  }
428 
429  /* duplicate drivers (F-Curves) */
430  if (src->drivers.first) {
431  ListBase drivers = {NULL, NULL};
432 
433  BKE_fcurves_copy(&drivers, &src->drivers);
434 
435  /* Fix up all driver targets using the old target id
436  * - This assumes that the src ID is being merged into the dst ID
437  */
438  if (fix_drivers) {
439  FCurve *fcu;
440 
441  for (fcu = drivers.first; fcu; fcu = fcu->next) {
442  ChannelDriver *driver = fcu->driver;
443  DriverVar *dvar;
444 
445  for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
447  if (dtar->id == src_id) {
448  dtar->id = dst_id;
449  }
450  }
452  }
453  }
454  }
455 
456  BLI_movelisttolist(&dst->drivers, &drivers);
457  }
458 }
459 
460 /* Sub-ID Regrouping ------------------------------------------- */
461 
469 static bool animpath_matches_basepath(const char path[], const char basepath[])
470 {
471  /* we need start of path to be basepath */
472  return (path && basepath) && STRPREFIX(path, basepath);
473 }
474 
476  const char *old_basepath,
477  const char *new_basepath)
478 {
479  BLI_assert(animpath_matches_basepath(fcu->rna_path, old_basepath));
480  if (STREQ(old_basepath, new_basepath)) {
481  return;
482  }
483 
484  char *new_path = BLI_sprintfN("%s%s", new_basepath, fcu->rna_path + strlen(old_basepath));
485  MEM_freeN(fcu->rna_path);
486  fcu->rna_path = new_path;
487 }
488 
489 /* Move F-Curves in src action to dst action, setting up all the necessary groups
490  * for this to happen, but only if the F-Curves being moved have the appropriate
491  * "base path".
492  * - This is used when data moves from one data-block to another, causing the
493  * F-Curves to need to be moved over too
494  */
496  bAction *dstAct,
497  const char *src_basepath,
498  const char *dst_basepath)
499 {
500  FCurve *fcu, *fcn = NULL;
501 
502  /* sanity checks */
503  if (ELEM(NULL, srcAct, dstAct, src_basepath, dst_basepath)) {
504  if (G.debug & G_DEBUG) {
505  CLOG_ERROR(&LOG,
506  "srcAct: %p, dstAct: %p, src_basepath: %p, dst_basepath: %p has insufficient "
507  "info to work with",
508  (void *)srcAct,
509  (void *)dstAct,
510  (void *)src_basepath,
511  (void *)dst_basepath);
512  }
513  return;
514  }
515 
516  /* clear 'temp' flags on all groups in src, as we'll be needing them later
517  * to identify groups that we've managed to empty out here
518  */
520 
521  /* iterate over all src F-Curves, moving over the ones that need to be moved */
522  for (fcu = srcAct->curves.first; fcu; fcu = fcn) {
523  /* store next pointer in case we move stuff */
524  fcn = fcu->next;
525 
526  /* should F-Curve be moved over?
527  * - we only need the start of the path to match basepath
528  */
529  if (animpath_matches_basepath(fcu->rna_path, src_basepath)) {
530  bActionGroup *agrp = NULL;
531 
532  /* if grouped... */
533  if (fcu->grp) {
534  /* make sure there will be a matching group on the other side for the migrants */
535  agrp = BKE_action_group_find_name(dstAct, fcu->grp->name);
536 
537  if (agrp == NULL) {
538  /* add a new one with a similar name (usually will be the same though) */
539  agrp = action_groups_add_new(dstAct, fcu->grp->name);
540  }
541 
542  /* old groups should be tagged with 'temp' flags so they can be removed later
543  * if we remove everything from them
544  */
545  fcu->grp->flag |= AGRP_TEMP;
546  }
547 
548  /* perform the migration now */
549  action_groups_remove_channel(srcAct, fcu);
550 
551  animpath_update_basepath(fcu, src_basepath, dst_basepath);
552 
553  if (agrp) {
554  action_groups_add_channel(dstAct, agrp, fcu);
555  }
556  else {
557  BLI_addtail(&dstAct->curves, fcu);
558  }
559  }
560  }
561 
562  /* cleanup groups (if present) */
563  if (srcAct->groups.first) {
564  bActionGroup *agrp, *grp = NULL;
565 
566  for (agrp = srcAct->groups.first; agrp; agrp = grp) {
567  grp = agrp->next;
568 
569  /* only tagged groups need to be considered - clearing these tags or removing them */
570  if (agrp->flag & AGRP_TEMP) {
571  /* if group is empty and tagged, then we can remove as this operation
572  * moved out all the channels that were formerly here
573  */
574  if (BLI_listbase_is_empty(&agrp->channels)) {
575  BLI_freelinkN(&srcAct->groups, agrp);
576  }
577  else {
578  agrp->flag &= ~AGRP_TEMP;
579  }
580  }
581  }
582  }
583 }
584 
586  AnimData *dstAdt,
587  const char *src_basepath,
588  const char *dst_basepath)
589 {
590  LISTBASE_FOREACH_MUTABLE (FCurve *, fcu, &srcAdt->drivers) {
591  if (animpath_matches_basepath(fcu->rna_path, src_basepath)) {
592  animpath_update_basepath(fcu, src_basepath, dst_basepath);
593  BLI_remlink(&srcAdt->drivers, fcu);
594  BLI_addtail(&dstAdt->drivers, fcu);
595 
596  /* TODO: add depsgraph flushing calls? */
597  }
598  }
599 }
600 
601 void BKE_animdata_transfer_by_basepath(Main *bmain, ID *srcID, ID *dstID, ListBase *basepaths)
602 {
603  AnimData *srcAdt = NULL, *dstAdt = NULL;
604 
605  /* sanity checks */
606  if (ELEM(NULL, srcID, dstID)) {
607  if (G.debug & G_DEBUG) {
608  CLOG_ERROR(&LOG, "no source or destination ID to separate AnimData with");
609  }
610  return;
611  }
612 
613  /* get animdata from src, and create for destination (if needed) */
614  srcAdt = BKE_animdata_from_id(srcID);
615  dstAdt = BKE_animdata_ensure_id(dstID);
616 
617  if (ELEM(NULL, srcAdt, dstAdt)) {
618  if (G.debug & G_DEBUG) {
619  CLOG_ERROR(&LOG, "no AnimData for this pair of ID's");
620  }
621  return;
622  }
623 
624  /* active action */
625  if (srcAdt->action) {
626  /* Set up an action if necessary,
627  * and name it in a similar way so that it can be easily found again. */
628  if (dstAdt->action == NULL) {
629  dstAdt->action = BKE_action_add(bmain, srcAdt->action->id.name + 2);
630  BKE_animdata_action_ensure_idroot(dstID, dstAdt->action);
631  }
632  else if (dstAdt->action == srcAdt->action) {
633  CLOG_WARN(&LOG,
634  "Argh! Source and Destination share animation! "
635  "('%s' and '%s' both use '%s') Making new empty action",
636  srcID->name,
637  dstID->name,
638  srcAdt->action->id.name);
639 
640  /* TODO: review this... */
641  id_us_min(&dstAdt->action->id);
642  dstAdt->action = BKE_action_add(bmain, dstAdt->action->id.name + 2);
643  BKE_animdata_action_ensure_idroot(dstID, dstAdt->action);
644  }
645 
646  /* loop over base paths, trying to fix for each one... */
647  LISTBASE_FOREACH (const AnimationBasePathChange *, basepath_change, basepaths) {
649  dstAdt->action,
650  basepath_change->src_basepath,
651  basepath_change->dst_basepath);
652  }
653  }
654 
655  /* drivers */
656  if (srcAdt->drivers.first) {
657  LISTBASE_FOREACH (const AnimationBasePathChange *, basepath_change, basepaths) {
659  srcAdt, dstAdt, basepath_change->src_basepath, basepath_change->dst_basepath);
660  }
661  }
662  /* Tag source action because list of fcurves changed. */
664 }
665 
666 /* Path Validation -------------------------------------------- */
667 
668 /* Check if a given RNA Path is valid, by tracing it from the given ID,
669  * and seeing if we can resolve it. */
670 static bool check_rna_path_is_valid(ID *owner_id, const char *path)
671 {
672  PointerRNA id_ptr, ptr;
673  PropertyRNA *prop = NULL;
674 
675  /* make initial RNA pointer to start resolving from */
676  RNA_id_pointer_create(owner_id, &id_ptr);
677 
678  /* try to resolve */
679  return RNA_path_resolve_property(&id_ptr, path, &ptr, &prop);
680 }
681 
682 /* Check if some given RNA Path needs fixing - free the given path and set a new one as appropriate
683  * NOTE: we assume that oldName and newName have [" "] padding around them
684  */
685 static char *rna_path_rename_fix(ID *owner_id,
686  const char *prefix,
687  const char *oldName,
688  const char *newName,
689  char *oldpath,
690  bool verify_paths)
691 {
692  char *prefixPtr = strstr(oldpath, prefix);
693  char *oldNamePtr = strstr(oldpath, oldName);
694  int prefixLen = strlen(prefix);
695  int oldNameLen = strlen(oldName);
696 
697  /* only start fixing the path if the prefix and oldName feature in the path,
698  * and prefix occurs immediately before oldName
699  */
700  if ((prefixPtr && oldNamePtr) && (prefixPtr + prefixLen == oldNamePtr)) {
701  /* if we haven't aren't able to resolve the path now, try again after fixing it */
702  if (!verify_paths || check_rna_path_is_valid(owner_id, oldpath) == 0) {
703  DynStr *ds = BLI_dynstr_new();
704  const char *postfixPtr = oldNamePtr + oldNameLen;
705  char *newPath = NULL;
706 
707  /* add the part of the string that goes up to the start of the prefix */
708  if (prefixPtr > oldpath) {
709  BLI_dynstr_nappend(ds, oldpath, prefixPtr - oldpath);
710  }
711 
712  /* add the prefix */
713  BLI_dynstr_append(ds, prefix);
714 
715  /* add the new name (complete with brackets) */
716  BLI_dynstr_append(ds, newName);
717 
718  /* add the postfix */
719  BLI_dynstr_append(ds, postfixPtr);
720 
721  /* create new path, and cleanup old data */
722  newPath = BLI_dynstr_get_cstring(ds);
723  BLI_dynstr_free(ds);
724 
725  /* check if the new path will solve our problems */
726  /* TODO: will need to check whether this step really helps in practice */
727  if (!verify_paths || check_rna_path_is_valid(owner_id, newPath)) {
728  /* free the old path, and return the new one, since we've solved the issues */
729  MEM_freeN(oldpath);
730  return newPath;
731  }
732 
733  /* still couldn't resolve the path... so, might as well just leave it alone */
734  MEM_freeN(newPath);
735  }
736  }
737 
738  /* the old path doesn't need to be changed */
739  return oldpath;
740 }
741 
742 /* Check RNA-Paths for a list of F-Curves */
743 static bool fcurves_path_rename_fix(ID *owner_id,
744  const char *prefix,
745  const char *oldName,
746  const char *newName,
747  const char *oldKey,
748  const char *newKey,
749  ListBase *curves,
750  bool verify_paths)
751 {
752  FCurve *fcu;
753  bool is_changed = false;
754  /* We need to check every curve. */
755  for (fcu = curves->first; fcu; fcu = fcu->next) {
756  if (fcu->rna_path == NULL) {
757  continue;
758  }
759  const char *old_path = fcu->rna_path;
760  /* Firstly, handle the F-Curve's own path. */
762  owner_id, prefix, oldKey, newKey, fcu->rna_path, verify_paths);
763  /* if path changed and the F-Curve is grouped, check if its group also needs renaming
764  * (i.e. F-Curve is first of a bone's F-Curves;
765  * hence renaming this should also trigger rename) */
766  if (fcu->rna_path != old_path) {
767  bActionGroup *agrp = fcu->grp;
768  is_changed = true;
769  if (oldName != NULL && (agrp != NULL) && STREQ(oldName, agrp->name)) {
770  BLI_strncpy(agrp->name, newName, sizeof(agrp->name));
771  }
772  }
773  }
774  return is_changed;
775 }
776 
777 /* Check RNA-Paths for a list of Drivers */
778 static bool drivers_path_rename_fix(ID *owner_id,
779  ID *ref_id,
780  const char *prefix,
781  const char *oldName,
782  const char *newName,
783  const char *oldKey,
784  const char *newKey,
785  ListBase *curves,
786  bool verify_paths)
787 {
788  bool is_changed = false;
789  FCurve *fcu;
790  /* We need to check every curve - drivers are F-Curves too. */
791  for (fcu = curves->first; fcu; fcu = fcu->next) {
792  /* firstly, handle the F-Curve's own path */
793  if (fcu->rna_path != NULL) {
794  const char *old_rna_path = fcu->rna_path;
796  owner_id, prefix, oldKey, newKey, fcu->rna_path, verify_paths);
797  is_changed |= (fcu->rna_path != old_rna_path);
798  }
799  if (fcu->driver == NULL) {
800  continue;
801  }
802  ChannelDriver *driver = fcu->driver;
803  DriverVar *dvar;
804  /* driver variables */
805  for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
806  /* only change the used targets, since the others will need fixing manually anyway */
808  /* rename RNA path */
809  if (dtar->rna_path && dtar->id) {
810  const char *old_rna_path = dtar->rna_path;
811  dtar->rna_path = rna_path_rename_fix(
812  dtar->id, prefix, oldKey, newKey, dtar->rna_path, verify_paths);
813  is_changed |= (dtar->rna_path != old_rna_path);
814  }
815  /* also fix the bone-name (if applicable) */
816  if (strstr(prefix, "bones")) {
817  if (((dtar->id) && (GS(dtar->id->name) == ID_OB) &&
818  (!ref_id || ((Object *)(dtar->id))->data == ref_id)) &&
819  (dtar->pchan_name[0]) && STREQ(oldName, dtar->pchan_name)) {
820  is_changed = true;
821  BLI_strncpy(dtar->pchan_name, newName, sizeof(dtar->pchan_name));
822  }
823  }
824  }
826  }
827  }
828  return is_changed;
829 }
830 
831 /* Fix all RNA-Paths for Actions linked to NLA Strips */
832 static bool nlastrips_path_rename_fix(ID *owner_id,
833  const char *prefix,
834  const char *oldName,
835  const char *newName,
836  const char *oldKey,
837  const char *newKey,
838  ListBase *strips,
839  bool verify_paths)
840 {
841  NlaStrip *strip;
842  bool is_changed = false;
843  /* Recursively check strips, fixing only actions. */
844  for (strip = strips->first; strip; strip = strip->next) {
845  /* fix strip's action */
846  if (strip->act != NULL) {
847  is_changed |= fcurves_path_rename_fix(
848  owner_id, prefix, oldName, newName, oldKey, newKey, &strip->act->curves, verify_paths);
849  }
850  /* Ignore own F-Curves, since those are local. */
851  /* Check sub-strips (if meta-strips). */
852  is_changed |= nlastrips_path_rename_fix(
853  owner_id, prefix, oldName, newName, oldKey, newKey, &strip->strips, verify_paths);
854  }
855  return is_changed;
856 }
857 
858 /* Rename Sub-ID Entities in RNA Paths ----------------------- */
859 
861  char *old_path,
862  const char *prefix,
863  const char *oldName,
864  const char *newName,
865  int oldSubscript,
866  int newSubscript,
867  bool verify_paths)
868 {
869  char *oldN, *newN;
870  char *result;
871 
872  /* if no action, no need to proceed */
873  if (ELEM(NULL, owner_id, old_path)) {
874  if (G.debug & G_DEBUG) {
875  CLOG_WARN(&LOG, "early abort");
876  }
877  return old_path;
878  }
879 
880  /* Name sanitation logic - copied from BKE_animdata_fix_paths_rename() */
881  if ((oldName != NULL) && (newName != NULL)) {
882  /* pad the names with [" "] so that only exact matches are made */
883  const size_t name_old_len = strlen(oldName);
884  const size_t name_new_len = strlen(newName);
885  char *name_old_esc = BLI_array_alloca(name_old_esc, (name_old_len * 2) + 1);
886  char *name_new_esc = BLI_array_alloca(name_new_esc, (name_new_len * 2) + 1);
887 
888  BLI_str_escape(name_old_esc, oldName, (name_old_len * 2) + 1);
889  BLI_str_escape(name_new_esc, newName, (name_new_len * 2) + 1);
890  oldN = BLI_sprintfN("[\"%s\"]", name_old_esc);
891  newN = BLI_sprintfN("[\"%s\"]", name_new_esc);
892  }
893  else {
894  oldN = BLI_sprintfN("[%d]", oldSubscript);
895  newN = BLI_sprintfN("[%d]", newSubscript);
896  }
897 
898  /* fix given path */
899  if (G.debug & G_DEBUG) {
900  printf("%s | %s | oldpath = %p ", oldN, newN, old_path);
901  }
902  result = rna_path_rename_fix(owner_id, prefix, oldN, newN, old_path, verify_paths);
903  if (G.debug & G_DEBUG) {
904  printf("path rename result = %p\n", result);
905  }
906 
907  /* free the temp names */
908  MEM_freeN(oldN);
909  MEM_freeN(newN);
910 
911  /* return the resulting path - may be the same path again if nothing changed */
912  return result;
913 }
914 
916  bAction *act,
917  const char *prefix,
918  const char *oldName,
919  const char *newName,
920  int oldSubscript,
921  int newSubscript,
922  bool verify_paths)
923 {
924  char *oldN, *newN;
925 
926  /* if no action, no need to proceed */
927  if (ELEM(NULL, owner_id, act)) {
928  return;
929  }
930 
931  /* Name sanitation logic - copied from BKE_animdata_fix_paths_rename() */
932  if ((oldName != NULL) && (newName != NULL)) {
933  /* pad the names with [" "] so that only exact matches are made */
934  const size_t name_old_len = strlen(oldName);
935  const size_t name_new_len = strlen(newName);
936  char *name_old_esc = BLI_array_alloca(name_old_esc, (name_old_len * 2) + 1);
937  char *name_new_esc = BLI_array_alloca(name_new_esc, (name_new_len * 2) + 1);
938 
939  BLI_str_escape(name_old_esc, oldName, (name_old_len * 2) + 1);
940  BLI_str_escape(name_new_esc, newName, (name_new_len * 2) + 1);
941  oldN = BLI_sprintfN("[\"%s\"]", name_old_esc);
942  newN = BLI_sprintfN("[\"%s\"]", name_new_esc);
943  }
944  else {
945  oldN = BLI_sprintfN("[%d]", oldSubscript);
946  newN = BLI_sprintfN("[%d]", newSubscript);
947  }
948 
949  /* fix paths in action */
951  owner_id, prefix, oldName, newName, oldN, newN, &act->curves, verify_paths);
952 
953  /* free the temp names */
954  MEM_freeN(oldN);
955  MEM_freeN(newN);
956 }
957 
959  AnimData *adt,
960  ID *ref_id,
961  const char *prefix,
962  const char *oldName,
963  const char *newName,
964  int oldSubscript,
965  int newSubscript,
966  bool verify_paths)
967 {
968  NlaTrack *nlt;
969  char *oldN, *newN;
970  /* If no AnimData, no need to proceed. */
971  if (ELEM(NULL, owner_id, adt)) {
972  return;
973  }
974  bool is_self_changed = false;
975  /* Name sanitation logic - shared with BKE_action_fix_paths_rename(). */
976  if ((oldName != NULL) && (newName != NULL)) {
977  /* Pad the names with [" "] so that only exact matches are made. */
978  const size_t name_old_len = strlen(oldName);
979  const size_t name_new_len = strlen(newName);
980  char *name_old_esc = BLI_array_alloca(name_old_esc, (name_old_len * 2) + 1);
981  char *name_new_esc = BLI_array_alloca(name_new_esc, (name_new_len * 2) + 1);
982 
983  BLI_str_escape(name_old_esc, oldName, (name_old_len * 2) + 1);
984  BLI_str_escape(name_new_esc, newName, (name_new_len * 2) + 1);
985  oldN = BLI_sprintfN("[\"%s\"]", name_old_esc);
986  newN = BLI_sprintfN("[\"%s\"]", name_new_esc);
987  }
988  else {
989  oldN = BLI_sprintfN("[%d]", oldSubscript);
990  newN = BLI_sprintfN("[%d]", newSubscript);
991  }
992  /* Active action and temp action. */
993  if (adt->action != NULL) {
995  owner_id, prefix, oldName, newName, oldN, newN, &adt->action->curves, verify_paths)) {
997  }
998  }
999  if (adt->tmpact) {
1001  owner_id, prefix, oldName, newName, oldN, newN, &adt->tmpact->curves, verify_paths)) {
1003  }
1004  }
1005  /* Drivers - Drivers are really F-Curves */
1006  is_self_changed |= drivers_path_rename_fix(
1007  owner_id, ref_id, prefix, oldName, newName, oldN, newN, &adt->drivers, verify_paths);
1008  /* NLA Data - Animation Data for Strips */
1009  for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
1010  is_self_changed |= nlastrips_path_rename_fix(
1011  owner_id, prefix, oldName, newName, oldN, newN, &nlt->strips, verify_paths);
1012  }
1013  /* Tag owner ID if it */
1014  if (is_self_changed) {
1016  }
1017  /* free the temp names */
1018  MEM_freeN(oldN);
1019  MEM_freeN(newN);
1020 }
1021 
1022 /* Remove FCurves with Prefix -------------------------------------- */
1023 
1024 /* Check RNA-Paths for a list of F-Curves */
1025 static bool fcurves_path_remove_fix(const char *prefix, ListBase *curves)
1026 {
1027  FCurve *fcu, *fcn;
1028  bool any_removed = false;
1029  if (!prefix) {
1030  return any_removed;
1031  }
1032 
1033  /* we need to check every curve... */
1034  for (fcu = curves->first; fcu; fcu = fcn) {
1035  fcn = fcu->next;
1036 
1037  if (fcu->rna_path) {
1038  if (STRPREFIX(fcu->rna_path, prefix)) {
1039  BLI_remlink(curves, fcu);
1040  BKE_fcurve_free(fcu);
1041  any_removed = true;
1042  }
1043  }
1044  }
1045  return any_removed;
1046 }
1047 
1048 /* Check RNA-Paths for a list of F-Curves */
1049 static bool nlastrips_path_remove_fix(const char *prefix, ListBase *strips)
1050 {
1051  NlaStrip *strip;
1052  bool any_removed = false;
1053 
1054  /* recursively check strips, fixing only actions... */
1055  for (strip = strips->first; strip; strip = strip->next) {
1056  /* fix strip's action */
1057  if (strip->act) {
1058  any_removed |= fcurves_path_remove_fix(prefix, &strip->act->curves);
1059  }
1060 
1061  /* Check sub-strips (if meta-strips). */
1062  any_removed |= nlastrips_path_remove_fix(prefix, &strip->strips);
1063  }
1064  return any_removed;
1065 }
1066 
1067 bool BKE_animdata_fix_paths_remove(ID *id, const char *prefix)
1068 {
1069  /* Only some ID-blocks have this info for now, so we cast the
1070  * types that do to be of type IdAdtTemplate
1071  */
1072  if (!id_can_have_animdata(id)) {
1073  return false;
1074  }
1075  bool any_removed = false;
1076  IdAdtTemplate *iat = (IdAdtTemplate *)id;
1077  AnimData *adt = iat->adt;
1078  /* check if there's any AnimData to start with */
1079  if (adt) {
1080  /* free fcurves */
1081  if (adt->action != NULL) {
1082  any_removed |= fcurves_path_remove_fix(prefix, &adt->action->curves);
1083  }
1084  if (adt->tmpact != NULL) {
1085  any_removed |= fcurves_path_remove_fix(prefix, &adt->tmpact->curves);
1086  }
1087  /* free drivers - stored as a list of F-Curves */
1088  any_removed |= fcurves_path_remove_fix(prefix, &adt->drivers);
1089  /* NLA Data - Animation Data for Strips */
1090  LISTBASE_FOREACH (NlaTrack *, nlt, &adt->nla_tracks) {
1091  any_removed |= nlastrips_path_remove_fix(prefix, &nlt->strips);
1092  }
1093  }
1094  return any_removed;
1095 }
1096 
1097 /* Apply Op to All FCurves in Database --------------------------- */
1098 
1099 /* "User-Data" wrapper used by BKE_fcurves_main_cb() */
1100 typedef struct AllFCurvesCbWrapper {
1101  ID_FCurve_Edit_Callback func; /* Operation to apply on F-Curve */
1102  void *user_data; /* Custom data for that operation */
1104 
1105 /* Helper for adt_apply_all_fcurves_cb() - Apply wrapped operator to list of F-Curves */
1106 static void fcurves_apply_cb(ID *id,
1107  ListBase *fcurves,
1109  void *user_data)
1110 {
1111  FCurve *fcu;
1112 
1113  for (fcu = fcurves->first; fcu; fcu = fcu->next) {
1114  func(id, fcu, user_data);
1115  }
1116 }
1117 
1118 /* Helper for adt_apply_all_fcurves_cb() - Recursively go through each NLA strip */
1120 {
1121  NlaStrip *strip;
1122 
1123  for (strip = strips->first; strip; strip = strip->next) {
1124  /* fix strip's action */
1125  if (strip->act) {
1126  fcurves_apply_cb(id, &strip->act->curves, wrapper->func, wrapper->user_data);
1127  }
1128 
1129  /* Check sub-strips (if meta-strips). */
1130  nlastrips_apply_all_curves_cb(id, &strip->strips, wrapper);
1131  }
1132 }
1133 
1134 /* Helper for BKE_fcurves_main_cb() - Dispatch wrapped operator to all F-Curves */
1135 static void adt_apply_all_fcurves_cb(ID *id, AnimData *adt, void *wrapper_data)
1136 {
1137  AllFCurvesCbWrapper *wrapper = wrapper_data;
1138  NlaTrack *nlt;
1139 
1140  if (adt->action) {
1141  fcurves_apply_cb(id, &adt->action->curves, wrapper->func, wrapper->user_data);
1142  }
1143 
1144  if (adt->tmpact) {
1145  fcurves_apply_cb(id, &adt->tmpact->curves, wrapper->func, wrapper->user_data);
1146  }
1147 
1148  /* free drivers - stored as a list of F-Curves */
1149  fcurves_apply_cb(id, &adt->drivers, wrapper->func, wrapper->user_data);
1150 
1151  /* NLA Data - Animation Data for Strips */
1152  for (nlt = adt->nla_tracks.first; nlt; nlt = nlt->next) {
1153  nlastrips_apply_all_curves_cb(id, &nlt->strips, wrapper);
1154  }
1155 }
1156 
1158 {
1159  AnimData *adt = BKE_animdata_from_id(id);
1160  if (adt != NULL) {
1161  AllFCurvesCbWrapper wrapper = {func, user_data};
1162  adt_apply_all_fcurves_cb(id, adt, &wrapper);
1163  }
1164 }
1165 
1167 {
1168  /* Wrap F-Curve operation stuff to pass to the general AnimData-level func */
1169  AllFCurvesCbWrapper wrapper = {func, user_data};
1170 
1171  /* Use the AnimData-based function so that we don't have to reimplement all that stuff */
1173 }
1174 
1175 /* Whole Database Ops -------------------------------------------- */
1176 
1178 {
1179  ID *id;
1180 
1181  /* standard data version */
1182 #define ANIMDATA_IDS_CB(first) \
1183  for (id = first; id; id = id->next) { \
1184  AnimData *adt = BKE_animdata_from_id(id); \
1185  if (adt) { \
1186  func(id, adt, user_data); \
1187  } \
1188  } \
1189  (void)0
1190 
1191  /* "embedded" nodetree cases (i.e. scene/material/texture->nodetree) */
1192 #define ANIMDATA_NODETREE_IDS_CB(first, NtId_Type) \
1193  for (id = first; id; id = id->next) { \
1194  AnimData *adt = BKE_animdata_from_id(id); \
1195  NtId_Type *ntp = (NtId_Type *)id; \
1196  if (ntp->nodetree) { \
1197  AnimData *adt2 = BKE_animdata_from_id((ID *)ntp->nodetree); \
1198  if (adt2) { \
1199  func(id, adt2, user_data); \
1200  } \
1201  } \
1202  if (adt) { \
1203  func(id, adt, user_data); \
1204  } \
1205  } \
1206  (void)0
1207 
1208  /* nodes */
1210 
1211  /* textures */
1213 
1214  /* lights */
1216 
1217  /* materials */
1219 
1220  /* cameras */
1221  ANIMDATA_IDS_CB(bmain->cameras.first);
1222 
1223  /* shapekeys */
1225 
1226  /* metaballs */
1228 
1229  /* curves */
1230  ANIMDATA_IDS_CB(bmain->curves.first);
1231 
1232  /* armatures */
1234 
1235  /* lattices */
1236  ANIMDATA_IDS_CB(bmain->lattices.first);
1237 
1238  /* meshes */
1239  ANIMDATA_IDS_CB(bmain->meshes.first);
1240 
1241  /* particles */
1243 
1244  /* speakers */
1245  ANIMDATA_IDS_CB(bmain->speakers.first);
1246 
1247  /* movie clips */
1249 
1250  /* objects */
1251  ANIMDATA_IDS_CB(bmain->objects.first);
1252 
1253  /* masks */
1254  ANIMDATA_IDS_CB(bmain->masks.first);
1255 
1256  /* worlds */
1258 
1259  /* scenes */
1261 
1262  /* line styles */
1264 
1265  /* grease pencil */
1266  ANIMDATA_IDS_CB(bmain->gpencils.first);
1267 
1268  /* palettes */
1269  ANIMDATA_IDS_CB(bmain->palettes.first);
1270 
1271  /* cache files */
1273 
1274  /* Hair Curves. */
1276 
1277  /* pointclouds */
1279 
1280  /* volumes */
1281  ANIMDATA_IDS_CB(bmain->volumes.first);
1282 
1283  /* simulations */
1285 }
1286 
1288  const char *prefix,
1289  const char *oldName,
1290  const char *newName)
1291 {
1292  Main *bmain = G.main; /* XXX UGLY! */
1293  BKE_animdata_fix_paths_rename_all_ex(bmain, ref_id, prefix, oldName, newName, 0, 0, 1);
1294 }
1295 
1297  ID *ref_id,
1298  const char *prefix,
1299  const char *oldName,
1300  const char *newName,
1301  const int oldSubscript,
1302  const int newSubscript,
1303  const bool verify_paths)
1304 {
1305  /* TODO: use BKE_animdata_main_cb for looping over all data. */
1306 
1307  ID *id;
1308 
1309  /* macro for less typing
1310  * - whether animdata exists is checked for by the main renaming callback, though taking
1311  * this outside of the function may make things slightly faster?
1312  */
1313 #define RENAMEFIX_ANIM_IDS(first) \
1314  for (id = first; id; id = id->next) { \
1315  AnimData *adt = BKE_animdata_from_id(id); \
1316  BKE_animdata_fix_paths_rename( \
1317  id, adt, ref_id, prefix, oldName, newName, oldSubscript, newSubscript, verify_paths); \
1318  } \
1319  (void)0
1320 
1321  /* another version of this macro for nodetrees */
1322 #define RENAMEFIX_ANIM_NODETREE_IDS(first, NtId_Type) \
1323  for (id = first; id; id = id->next) { \
1324  AnimData *adt = BKE_animdata_from_id(id); \
1325  NtId_Type *ntp = (NtId_Type *)id; \
1326  if (ntp->nodetree) { \
1327  AnimData *adt2 = BKE_animdata_from_id((ID *)ntp->nodetree); \
1328  BKE_animdata_fix_paths_rename((ID *)ntp->nodetree, \
1329  adt2, \
1330  ref_id, \
1331  prefix, \
1332  oldName, \
1333  newName, \
1334  oldSubscript, \
1335  newSubscript, \
1336  verify_paths); \
1337  } \
1338  BKE_animdata_fix_paths_rename( \
1339  id, adt, ref_id, prefix, oldName, newName, oldSubscript, newSubscript, verify_paths); \
1340  } \
1341  (void)0
1342 
1343  /* nodes */
1345 
1346  /* textures */
1348 
1349  /* lights */
1351 
1352  /* materials */
1354 
1355  /* cameras */
1357 
1358  /* shapekeys */
1360 
1361  /* metaballs */
1363 
1364  /* curves */
1366 
1367  /* armatures */
1369 
1370  /* lattices */
1372 
1373  /* meshes */
1375 
1376  /* particles */
1378 
1379  /* speakers */
1381 
1382  /* movie clips */
1384 
1385  /* objects */
1387 
1388  /* masks */
1389  RENAMEFIX_ANIM_IDS(bmain->masks.first);
1390 
1391  /* worlds */
1393 
1394  /* linestyles */
1396 
1397  /* grease pencil */
1399 
1400  /* cache files */
1402 
1403  /* Hair Curves. */
1405 
1406  /* pointclouds */
1408 
1409  /* volumes */
1411 
1412  /* simulations */
1414 
1415  /* scenes */
1417 }
1418 
1419 /* .blend file API -------------------------------------------- */
1420 
1422 {
1423  /* firstly, just write the AnimData block */
1424  BLO_write_struct(writer, AnimData, adt);
1425 
1426  /* write drivers */
1427  BKE_fcurve_blend_write(writer, &adt->drivers);
1428 
1429  /* write overrides */
1430  /* FIXME: are these needed? */
1431  LISTBASE_FOREACH (AnimOverride *, aor, &adt->overrides) {
1432  /* overrides consist of base data + rna_path */
1433  BLO_write_struct(writer, AnimOverride, aor);
1434  BLO_write_string(writer, aor->rna_path);
1435  }
1436 
1437  /* TODO: write the remaps (if they are needed). */
1438 
1439  /* write NLA data */
1440  BKE_nla_blend_write(writer, &adt->nla_tracks);
1441 }
1442 
1444 {
1445  /* NOTE: must have called BLO_read_data_address already before doing this... */
1446  if (adt == NULL) {
1447  return;
1448  }
1449 
1450  /* link drivers */
1451  BLO_read_list(reader, &adt->drivers);
1452  BKE_fcurve_blend_read_data(reader, &adt->drivers);
1453  adt->driver_array = NULL;
1454 
1455  /* link overrides */
1456  /* TODO... */
1457 
1458  /* link NLA-data */
1459  BLO_read_list(reader, &adt->nla_tracks);
1460  BKE_nla_blend_read_data(reader, &adt->nla_tracks);
1461 
1462  /* relink active track/strip - even though strictly speaking this should only be used
1463  * if we're in 'tweaking mode', we need to be able to have this loaded back for
1464  * undo, but also since users may not exit tweak-mode before saving (T24535).
1465  */
1466  /* TODO: it's not really nice that anyone should be able to save the file in this
1467  * state, but it's going to be too hard to enforce this single case. */
1468  BLO_read_data_address(reader, &adt->act_track);
1469  BLO_read_data_address(reader, &adt->actstrip);
1470 }
1471 
1473 {
1474  if (adt == NULL) {
1475  return;
1476  }
1477 
1478  /* link action data */
1479  BLO_read_id_address(reader, id->lib, &adt->action);
1480  BLO_read_id_address(reader, id->lib, &adt->tmpact);
1481 
1482  /* link drivers */
1483  BKE_fcurve_blend_read_lib(reader, id, &adt->drivers);
1484 
1485  /* overrides don't have lib-link for now, so no need to do anything */
1486 
1487  /* link NLA-data */
1488  BKE_nla_blend_read_lib(reader, id, &adt->nla_tracks);
1489 }
1490 
1492 {
1493  /* own action */
1494  BLO_expand(expander, adt->action);
1495  BLO_expand(expander, adt->tmpact);
1496 
1497  /* drivers - assume that these F-Curves have driver data to be in this list... */
1498  BKE_fcurve_blend_read_expand(expander, &adt->drivers);
1499 
1500  /* NLA data - referenced actions. */
1501  BKE_nla_blend_read_expand(expander, &adt->nla_tracks);
1502 }
Blender kernel action and pose functionality.
struct bActionGroup * action_groups_add_new(struct bAction *act, const char name[])
Definition: action.c:410
void action_groups_remove_channel(struct bAction *act, struct FCurve *fcu)
Definition: action.c:542
void action_groups_clear_tempflags(struct bAction *act)
Definition: action.c:593
struct bActionGroup * BKE_action_group_find_name(struct bAction *act, const char name[])
Definition: action.c:582
struct bAction * BKE_action_add(struct Main *bmain, const char name[])
Definition: action.c:332
void action_groups_add_channel(struct bAction *act, struct bActionGroup *agrp, struct FCurve *fcurve)
Definition: action.c:435
eAnimData_MergeCopy_Modes
@ ADT_MERGECOPY_SRC_COPY
@ ADT_MERGECOPY_SRC_REF
void(* ID_AnimData_Edit_Callback)(struct ID *id, struct AnimData *adt, void *user_data)
Definition: BKE_animsys.h:218
void(* ID_FCurve_Edit_Callback)(struct ID *id, struct FCurve *fcu, void *user_data)
Definition: BKE_animsys.h:221
void BKE_fcurve_blend_write(struct BlendWriter *writer, struct ListBase *fcurves)
Definition: fcurve.c:2344
void BKE_fcurve_blend_read_data(struct BlendDataReader *reader, struct ListBase *fcurves)
Definition: fcurve.c:2383
void BKE_fcurve_free(struct FCurve *fcu)
Definition: fcurve.c:65
void BKE_fcurve_foreach_id(struct FCurve *fcu, struct LibraryForeachIDData *data)
Definition: fcurve.c:165
void BKE_fcurve_blend_read_lib(struct BlendLibReader *reader, struct ID *id, struct ListBase *fcurves)
Definition: fcurve.c:2438
void BKE_fcurve_blend_read_expand(struct BlendExpander *expander, struct ListBase *fcurves)
Definition: fcurve.c:2468
void BKE_fcurves_free(ListBase *list)
Definition: fcurve.c:86
void BKE_fcurves_copy(ListBase *dst, ListBase *src)
Definition: fcurve.c:146
#define DRIVER_TARGETS_USED_LOOPER_BEGIN(dvar)
#define DRIVER_TARGETS_LOOPER_END
@ G_DEBUG
Definition: BKE_global.h:174
const struct IDTypeInfo * BKE_idtype_get_info_from_idcode(short id_code)
Definition: idtype.c:112
@ IDTYPE_FLAGS_NO_ANIMDATA
Definition: BKE_idtype.h:41
struct ID * BKE_id_copy(struct Main *bmain, const struct ID *id)
@ LIB_ID_COPY_ACTIONS
Definition: BKE_lib_id.h:166
@ LIB_ID_CREATE_NO_USER_REFCOUNT
Definition: BKE_lib_id.h:126
@ LIB_ID_CREATE_NO_MAIN
Definition: BKE_lib_id.h:122
struct ID * BKE_id_copy_ex(struct Main *bmain, const struct ID *id, struct ID **r_newid, int flag)
void id_us_min(struct ID *id)
Definition: lib_id.c:313
void id_us_plus(struct ID *id)
Definition: lib_id.c:305
#define BKE_LIB_FOREACHID_PROCESS_IDSUPER(_data, _id_super, _cb_flag)
#define BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL(_data, _func_call)
@ IDWALK_CB_USER
Definition: BKE_lib_query.h:73
void BKE_nla_strip_foreach_id(struct NlaStrip *strip, struct LibraryForeachIDData *data)
Definition: nla.c:513
void BKE_nla_blend_read_data(struct BlendDataReader *reader, struct ListBase *tracks)
Definition: nla.c:2228
void BKE_nla_tracks_free(ListBase *tracks, bool do_id_user)
Definition: nla.c:127
void BKE_nla_tracks_copy_from_adt(struct Main *bmain, struct AnimData *adt_dest, const struct AnimData *adt_source, int flag)
void BKE_nla_tracks_copy(struct Main *bmain, ListBase *dst, const ListBase *src, int flag)
Definition: nla.c:226
void BKE_nla_blend_write(struct BlendWriter *writer, struct ListBase *tracks)
Definition: nla.c:2216
void BKE_nla_blend_read_lib(struct BlendLibReader *reader, struct ID *id, struct ListBase *tracks)
Definition: nla.c:2239
void BKE_nla_blend_read_expand(struct BlendExpander *expander, struct ListBase *tracks)
Definition: nla.c:2252
struct bNodeTree * ntreeFromID(struct ID *id)
Definition: node.cc:3231
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition: report.c:83
#define BLI_array_alloca(arr, realsize)
Definition: BLI_alloca.h:22
#define BLI_assert(a)
Definition: BLI_assert.h:46
A dynamically sized string ADT.
DynStr * BLI_dynstr_new(void) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition: BLI_dynstr.c:50
void BLI_dynstr_nappend(DynStr *__restrict ds, const char *cstr, int len) ATTR_NONNULL()
Definition: BLI_dynstr.c:94
char * BLI_dynstr_get_cstring(const DynStr *ds) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: BLI_dynstr.c:256
void BLI_dynstr_free(DynStr *ds) ATTR_NONNULL()
Definition: BLI_dynstr.c:281
void BLI_dynstr_append(DynStr *__restrict ds, const char *cstr) ATTR_NONNULL()
Definition: BLI_dynstr.c:75
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:269
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:239
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
Definition: BLI_listbase.h:354
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
Definition: BLI_listbase.h:273
void void void BLI_movelisttolist(struct ListBase *dst, struct ListBase *src) ATTR_NONNULL(1
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:100
size_t size_t char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
size_t size_t char size_t BLI_str_escape(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL()
Definition: string.c:250
#define STRPREFIX(a, b)
#define ELEM(...)
#define STREQ(a, b)
#define BLO_read_data_address(reader, ptr_p)
#define BLO_write_struct(writer, struct_name, data_ptr)
void BLO_read_list(BlendDataReader *reader, struct ListBase *list)
Definition: readfile.c:5172
void BLO_write_string(BlendWriter *writer, const char *data_ptr)
Definition: writefile.c:1601
#define BLO_read_id_address(reader, lib, id_ptr_p)
#define BLO_expand(expander, id)
#define CLOG_ERROR(clg_ref,...)
Definition: CLG_log.h:190
#define CLOG_WARN(clg_ref,...)
Definition: CLG_log.h:189
void DEG_id_tag_update(struct ID *id, int flag)
ID and Library types, which are fundamental for sdna.
@ ID_RECALC_COPY_ON_WRITE
Definition: DNA_ID.h:834
#define ID_IS_LINKED(_id)
Definition: DNA_ID.h:566
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition: DNA_ID.h:588
#define ID_NEW_SET(_id, _idn)
Definition: DNA_ID.h:617
@ ID_OB
Definition: DNA_ID_enums.h:47
@ AGRP_TEMP
@ ADT_NLA_EDIT_ON
eDupli_ID_Flags
@ USER_DUP_LINKED_ID
@ USER_DUP_ACT
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to curves
bool BKE_animdata_copy_id(Main *bmain, ID *id_to, ID *id_from, const int flag)
Definition: anim_data.c:327
static void animdata_copy_id_action(Main *bmain, ID *id, const bool set_newid, const bool do_linked_id)
Definition: anim_data.c:346
bool BKE_animdata_action_ensure_idroot(const ID *owner, bAction *action)
Definition: anim_data.c:177
void BKE_animdata_blend_read_lib(BlendLibReader *reader, ID *id, AnimData *adt)
Definition: anim_data.c:1472
bool BKE_animdata_fix_paths_remove(ID *id, const char *prefix)
Definition: anim_data.c:1067
void BKE_animdata_fix_paths_rename_all_ex(Main *bmain, ID *ref_id, const char *prefix, const char *oldName, const char *newName, const int oldSubscript, const int newSubscript, const bool verify_paths)
Definition: anim_data.c:1296
void BKE_animdata_merge_copy(Main *bmain, ID *dst_id, ID *src_id, eAnimData_MergeCopy_Modes action_mode, bool fix_drivers)
Definition: anim_data.c:387
#define RENAMEFIX_ANIM_NODETREE_IDS(first, NtId_Type)
static bool drivers_path_rename_fix(ID *owner_id, ID *ref_id, const char *prefix, const char *oldName, const char *newName, const char *oldKey, const char *newKey, ListBase *curves, bool verify_paths)
Definition: anim_data.c:778
static bool check_rna_path_is_valid(ID *owner_id, const char *path)
Definition: anim_data.c:670
static bool fcurves_path_rename_fix(ID *owner_id, const char *prefix, const char *oldName, const char *newName, const char *oldKey, const char *newKey, ListBase *curves, bool verify_paths)
Definition: anim_data.c:743
static bool nlastrips_path_rename_fix(ID *owner_id, const char *prefix, const char *oldName, const char *newName, const char *oldKey, const char *newKey, ListBase *strips, bool verify_paths)
Definition: anim_data.c:832
bool id_can_have_animdata(const ID *id)
Definition: anim_data.c:66
static bool fcurves_path_remove_fix(const char *prefix, ListBase *curves)
Definition: anim_data.c:1025
void BKE_action_fix_paths_rename(ID *owner_id, bAction *act, const char *prefix, const char *oldName, const char *newName, int oldSubscript, int newSubscript, bool verify_paths)
Definition: anim_data.c:915
void BKE_animdata_foreach_id(AnimData *adt, LibraryForeachIDData *data)
Definition: anim_data.c:257
#define ANIMDATA_NODETREE_IDS_CB(first, NtId_Type)
bool id_type_can_have_animdata(const short id_type)
Definition: anim_data.c:57
static void adt_apply_all_fcurves_cb(ID *id, AnimData *adt, void *wrapper_data)
Definition: anim_data.c:1135
static void fcurves_apply_cb(ID *id, ListBase *fcurves, ID_FCurve_Edit_Callback func, void *user_data)
Definition: anim_data.c:1106
void BKE_animdata_blend_write(BlendWriter *writer, struct AnimData *adt)
Definition: anim_data.c:1421
void BKE_animdata_main_cb(Main *bmain, ID_AnimData_Edit_Callback func, void *user_data)
Definition: anim_data.c:1177
bool BKE_animdata_action_editable(const AnimData *adt)
Definition: anim_data.c:169
struct AllFCurvesCbWrapper AllFCurvesCbWrapper
#define RENAMEFIX_ANIM_IDS(first)
bool BKE_animdata_set_action(ReportList *reports, ID *id, bAction *act)
Definition: anim_data.c:118
void BKE_fcurves_id_cb(ID *id, ID_FCurve_Edit_Callback func, void *user_data)
Definition: anim_data.c:1157
bool BKE_animdata_id_is_animated(const struct ID *id)
Definition: anim_data.c:238
void BKE_animdata_free(ID *id, const bool do_id_user)
Definition: anim_data.c:197
static bool animpath_matches_basepath(const char path[], const char basepath[])
Definition: anim_data.c:469
static bool nlastrips_path_remove_fix(const char *prefix, ListBase *strips)
Definition: anim_data.c:1049
AnimData * BKE_animdata_copy(Main *bmain, AnimData *adt, const int flag)
Definition: anim_data.c:275
void BKE_animdata_duplicate_id_action(struct Main *bmain, struct ID *id, const eDupli_ID_Flags duplicate_flags)
Definition: anim_data.c:378
void BKE_animdata_blend_read_data(BlendDataReader *reader, AnimData *adt)
Definition: anim_data.c:1443
AnimData * BKE_animdata_ensure_id(ID *id)
Definition: anim_data.c:90
static void animpath_update_basepath(FCurve *fcu, const char *old_basepath, const char *new_basepath)
Definition: anim_data.c:475
void BKE_animdata_blend_read_expand(struct BlendExpander *expander, AnimData *adt)
Definition: anim_data.c:1491
static CLG_LogRef LOG
Definition: anim_data.c:50
#define ANIMDATA_IDS_CB(first)
void BKE_animdata_transfer_by_basepath(Main *bmain, ID *srcID, ID *dstID, ListBase *basepaths)
Definition: anim_data.c:601
void BKE_animdata_fix_paths_rename_all(ID *ref_id, const char *prefix, const char *oldName, const char *newName)
Definition: anim_data.c:1287
AnimData * BKE_animdata_from_id(const ID *id)
Definition: anim_data.c:76
void BKE_fcurves_main_cb(Main *bmain, ID_FCurve_Edit_Callback func, void *user_data)
Definition: anim_data.c:1166
void BKE_animdata_copy_id_action(Main *bmain, ID *id)
Definition: anim_data.c:372
static char * rna_path_rename_fix(ID *owner_id, const char *prefix, const char *oldName, const char *newName, char *oldpath, bool verify_paths)
Definition: anim_data.c:685
static void animdata_move_drivers_by_basepath(AnimData *srcAdt, AnimData *dstAdt, const char *src_basepath, const char *dst_basepath)
Definition: anim_data.c:585
void BKE_animdata_fix_paths_rename(ID *owner_id, AnimData *adt, ID *ref_id, const char *prefix, const char *oldName, const char *newName, int oldSubscript, int newSubscript, bool verify_paths)
Definition: anim_data.c:958
static void nlastrips_apply_all_curves_cb(ID *id, ListBase *strips, AllFCurvesCbWrapper *wrapper)
Definition: anim_data.c:1119
char * BKE_animsys_fix_rna_path_rename(ID *owner_id, char *old_path, const char *prefix, const char *oldName, const char *newName, int oldSubscript, int newSubscript, bool verify_paths)
Definition: anim_data.c:860
static void action_move_fcurves_by_basepath(bAction *srcAct, bAction *dstAct, const char *src_basepath, const char *dst_basepath)
Definition: anim_data.c:495
void * user_data
SyclQueue void void * src
bNodeTree * ntree
#define GS(x)
Definition: iris.c:225
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:28
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
#define G(x, y, z)
void RNA_id_pointer_create(ID *id, PointerRNA *r_ptr)
Definition: rna_access.c:112
bool RNA_path_resolve_property(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
Definition: rna_path.cc:531
ID_FCurve_Edit_Callback func
Definition: anim_data.c:1101
bAction * action
NlaStrip * actstrip
ListBase overrides
float act_influence
FCurve ** driver_array
NlaTrack * act_track
bAction * tmpact
ListBase drivers
ListBase nla_tracks
ListBase variables
struct DriverVar * next
struct FCurve * next
bActionGroup * grp
char * rna_path
ChannelDriver * driver
uint32_t flags
Definition: BKE_idtype.h:139
Definition: DNA_ID.h:368
struct Library * lib
Definition: DNA_ID.h:372
char name[66]
Definition: DNA_ID.h:378
AnimData * adt
void * first
Definition: DNA_listBase.h:31
Definition: BKE_main.h:121
ListBase volumes
Definition: BKE_main.h:210
ListBase masks
Definition: BKE_main.h:200
ListBase scenes
Definition: BKE_main.h:168
ListBase textures
Definition: BKE_main.h:175
ListBase meshes
Definition: BKE_main.h:171
ListBase movieclips
Definition: BKE_main.h:199
ListBase hair_curves
Definition: BKE_main.h:208
ListBase lights
Definition: BKE_main.h:178
ListBase nodetrees
Definition: BKE_main.h:192
ListBase particles
Definition: BKE_main.h:194
ListBase materials
Definition: BKE_main.h:174
ListBase linestyles
Definition: BKE_main.h:201
ListBase pointclouds
Definition: BKE_main.h:209
ListBase lattices
Definition: BKE_main.h:177
ListBase shapekeys
Definition: BKE_main.h:181
ListBase cameras
Definition: BKE_main.h:179
ListBase armatures
Definition: BKE_main.h:190
ListBase speakers
Definition: BKE_main.h:186
ListBase curves
Definition: BKE_main.h:172
ListBase worlds
Definition: BKE_main.h:182
ListBase palettes
Definition: BKE_main.h:195
ListBase metaballs
Definition: BKE_main.h:173
ListBase simulations
Definition: BKE_main.h:211
ListBase gpencils
Definition: BKE_main.h:198
ListBase objects
Definition: BKE_main.h:170
ListBase cachefiles
Definition: BKE_main.h:202
struct NlaStrip * next
ListBase strips
bAction * act
ListBase strips
struct NlaTrack * next
struct bActionGroup * next
ListBase curves
ListBase groups
ListBase tracks
Definition: tracking.c:60
PointerRNA * ptr
Definition: wm_files.c:3480