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