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.
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.
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.
16 * The Original Code is Copyright (C) 2016 by Blender Foundation.
17 * All rights reserved.
27 #include "MEM_guardedalloc.h"
30 #include "DNA_object_types.h"
32 #include "DEG_depsgraph.h"
34 #include "BKE_armature.h"
35 #include "BKE_lib_id.h"
36 #include "BKE_lib_override.h"
37 #include "BKE_lib_remap.h"
40 #include "BLI_utildefines.h"
41 #include "BLI_ghash.h"
42 #include "BLI_listbase.h"
43 #include "BLI_string.h"
45 #include "RNA_access.h"
46 #include "RNA_types.h"
49 #include "PIL_time_utildefines.h"
51 #define OVERRIDE_AUTO_CHECK_DELAY 0.2 /* 200ms between auto-override checks. */
53 static void lib_override_library_property_copy(IDOverrideLibraryProperty *op_dst,
54 IDOverrideLibraryProperty *op_src);
55 static void lib_override_library_property_operation_copy(
56 IDOverrideLibraryPropertyOperation *opop_dst, IDOverrideLibraryPropertyOperation *opop_src);
58 static void lib_override_library_property_clear(IDOverrideLibraryProperty *op);
59 static void lib_override_library_property_operation_clear(
60 IDOverrideLibraryPropertyOperation *opop);
62 /* Temp, for until library override is ready and tested enough to go 'public',
63 * we hide it by default in UI and such. */
64 static bool _lib_override_library_enabled = true;
66 void BKE_lib_override_library_enable(const bool do_enable)
68 _lib_override_library_enabled = do_enable;
71 bool BKE_lib_override_library_is_enabled()
73 return _lib_override_library_enabled;
76 /** Initialize empty overriding of \a reference_id by \a local_id. */
77 IDOverrideLibrary *BKE_lib_override_library_init(ID *local_id, ID *reference_id)
79 /* If reference_id is NULL, we are creating an override template for purely local data.
80 * Else, reference *must* be linked data. */
81 BLI_assert(reference_id == NULL || reference_id->lib != NULL);
82 BLI_assert(local_id->override_library == NULL);
85 for (ancestor_id = reference_id; ancestor_id != NULL && ancestor_id->override_library != NULL &&
86 ancestor_id->override_library->reference != NULL;
87 ancestor_id = ancestor_id->override_library->reference) {
91 if (ancestor_id != NULL && ancestor_id->override_library != NULL) {
92 /* Original ID has a template, use it! */
93 BKE_lib_override_library_copy(local_id, ancestor_id);
94 if (local_id->override_library->reference != reference_id) {
95 id_us_min(local_id->override_library->reference);
96 local_id->override_library->reference = reference_id;
97 id_us_plus(local_id->override_library->reference);
99 return local_id->override_library;
102 /* Else, generate new empty override. */
103 local_id->override_library = MEM_callocN(sizeof(*local_id->override_library), __func__);
104 local_id->override_library->reference = reference_id;
105 id_us_plus(local_id->override_library->reference);
106 local_id->tag &= ~LIB_TAG_OVERRIDE_LIBRARY_REFOK;
107 /* TODO do we want to add tag or flag to referee to mark it as such? */
108 return local_id->override_library;
111 /** Deep copy of a whole override from \a src_id to \a dst_id. */
112 void BKE_lib_override_library_copy(ID *dst_id, const ID *src_id)
114 BLI_assert(src_id->override_library != NULL);
116 if (dst_id->override_library != NULL) {
117 if (src_id->override_library == NULL) {
118 BKE_lib_override_library_free(&dst_id->override_library, true);
122 BKE_lib_override_library_clear(dst_id->override_library, true);
125 else if (src_id->override_library == NULL) {
129 BKE_lib_override_library_init(dst_id, NULL);
132 /* Source is already overriding data, we copy it but reuse its reference for dest ID.
133 * otherwise, source is only an override template, it then becomes reference of dest ID. */
134 dst_id->override_library->reference = src_id->override_library->reference ?
135 src_id->override_library->reference :
137 id_us_plus(dst_id->override_library->reference);
139 BLI_duplicatelist(&dst_id->override_library->properties, &src_id->override_library->properties);
140 for (IDOverrideLibraryProperty *op_dst = dst_id->override_library->properties.first,
141 *op_src = src_id->override_library->properties.first;
143 op_dst = op_dst->next, op_src = op_src->next) {
144 lib_override_library_property_copy(op_dst, op_src);
147 dst_id->tag &= ~LIB_TAG_OVERRIDE_LIBRARY_REFOK;
150 /** Clear any overriding data from given \a override. */
151 void BKE_lib_override_library_clear(IDOverrideLibrary *override, const bool do_id_user)
153 BLI_assert(override != NULL);
155 if (override->runtime != NULL) {
156 BLI_ghash_clear(override->runtime, NULL, NULL);
159 for (IDOverrideLibraryProperty *op = override->properties.first; op; op = op->next) {
160 lib_override_library_property_clear(op);
162 BLI_freelistN(&override->properties);
165 id_us_min(override->reference);
166 /* override->storage should never be refcounted... */
170 /** Free given \a override. */
171 void BKE_lib_override_library_free(struct IDOverrideLibrary **override, const bool do_id_user)
173 BLI_assert(*override != NULL);
175 if ((*override)->runtime != NULL) {
176 BLI_ghash_free((*override)->runtime, NULL, NULL);
177 (*override)->runtime = NULL;
180 BKE_lib_override_library_clear(*override, do_id_user);
181 MEM_freeN(*override);
185 static ID *lib_override_library_create_from(Main *bmain, ID *reference_id)
189 if (!BKE_id_copy(bmain, reference_id, (ID **)&local_id)) {
194 BKE_lib_override_library_init(local_id, reference_id);
195 local_id->override_library->flag |= OVERRIDE_LIBRARY_AUTO;
200 /** Create an overridden local copy of linked reference. */
201 ID *BKE_lib_override_library_create_from_id(Main *bmain,
203 const bool do_tagged_remap)
205 BLI_assert(reference_id != NULL);
206 BLI_assert(reference_id->lib != NULL);
208 ID *local_id = lib_override_library_create_from(bmain, reference_id);
210 if (do_tagged_remap) {
212 FOREACH_MAIN_ID_BEGIN (bmain, other_id) {
213 if ((other_id->tag & LIB_TAG_DOIT) != 0 && other_id->lib == NULL) {
214 /* Note that using ID_REMAP_SKIP_INDIRECT_USAGE below is superfluous, as we only remap
215 * local IDs usages anyway... */
216 BKE_libblock_relink_ex(bmain,
220 ID_REMAP_SKIP_INDIRECT_USAGE | ID_REMAP_SKIP_OVERRIDE_LIBRARY);
230 * Create overridden local copies of all tagged data-blocks in given Main.
232 * \note Set id->newid of overridden libs with newly created overrides,
233 * caller is responsible to clean those pointers before/after usage as needed.
235 * \note By default, it will only remap newly created local overriding data-blocks between
236 * themselves, to avoid 'enforcing' those overrides into all other usages of the linked data in
237 * main. You can add more local IDs to be remapped to use new overriding ones by setting their
240 * \return \a true on success, \a false otherwise.
242 bool BKE_lib_override_library_create_from_tag(Main *bmain)
247 ListBase todo_ids = {NULL};
248 LinkData *todo_id_iter;
250 /* Get all IDs we want to override. */
251 FOREACH_MAIN_ID_BEGIN (bmain, reference_id) {
252 if ((reference_id->tag & LIB_TAG_DOIT) != 0 && reference_id->lib != NULL) {
253 todo_id_iter = MEM_callocN(sizeof(*todo_id_iter), __func__);
254 todo_id_iter->data = reference_id;
255 BLI_addtail(&todo_ids, todo_id_iter);
260 /* Override the IDs. */
261 for (todo_id_iter = todo_ids.first; todo_id_iter != NULL; todo_id_iter = todo_id_iter->next) {
262 reference_id = todo_id_iter->data;
263 if ((reference_id->newid = lib_override_library_create_from(bmain, reference_id)) == NULL) {
267 /* We also tag the new IDs so that in next step we can remap their pointers too. */
268 reference_id->newid->tag |= LIB_TAG_DOIT;
272 /* Only remap new local ID's pointers, we don't want to force our new overrides onto our whole
273 * existing linked IDs usages. */
274 for (todo_id_iter = todo_ids.first; todo_id_iter != NULL; todo_id_iter = todo_id_iter->next) {
276 reference_id = todo_id_iter->data;
278 if (reference_id->newid == NULL) {
282 /* Still checking the whole Main, that way we can tag other local IDs as needing to be remapped
283 * to use newly created overriding IDs, if needed. */
284 FOREACH_MAIN_ID_BEGIN (bmain, other_id) {
285 if ((other_id->tag & LIB_TAG_DOIT) != 0 && other_id->lib == NULL) {
286 ID *local_id = reference_id->newid;
287 /* Note that using ID_REMAP_SKIP_INDIRECT_USAGE below is superfluous, as we only remap
288 * local IDs usages anyway... */
289 BKE_libblock_relink_ex(bmain,
293 ID_REMAP_SKIP_INDIRECT_USAGE | ID_REMAP_SKIP_OVERRIDE_LIBRARY);
299 BLI_freelistN(&todo_ids);
304 /* We only build override GHash on request. */
305 BLI_INLINE IDOverrideLibraryRuntime *override_library_rna_path_mapping_ensure(
306 IDOverrideLibrary *override)
308 if (override->runtime == NULL) {
309 override->runtime = BLI_ghash_new(BLI_ghashutil_strhash_p, BLI_ghashutil_strcmp, __func__);
310 for (IDOverrideLibraryProperty *op = override->properties.first; op != NULL; op = op->next) {
311 BLI_ghash_insert(override->runtime, op->rna_path, op);
315 return override->runtime;
319 * Find override property from given RNA path, if it exists.
321 IDOverrideLibraryProperty *BKE_lib_override_library_property_find(IDOverrideLibrary *override,
322 const char *rna_path)
324 IDOverrideLibraryRuntime *override_runtime = override_library_rna_path_mapping_ensure(override);
325 return BLI_ghash_lookup(override_runtime, rna_path);
329 * Find override property from given RNA path, or create it if it does not exist.
331 IDOverrideLibraryProperty *BKE_lib_override_library_property_get(IDOverrideLibrary *override,
332 const char *rna_path,
335 IDOverrideLibraryProperty *op = BKE_lib_override_library_property_find(override, rna_path);
338 op = MEM_callocN(sizeof(IDOverrideLibraryProperty), __func__);
339 op->rna_path = BLI_strdup(rna_path);
340 BLI_addtail(&override->properties, op);
342 IDOverrideLibraryRuntime *override_runtime = override_library_rna_path_mapping_ensure(
344 BLI_ghash_insert(override_runtime, op->rna_path, op);
350 else if (r_created) {
357 void lib_override_library_property_copy(IDOverrideLibraryProperty *op_dst,
358 IDOverrideLibraryProperty *op_src)
360 op_dst->rna_path = BLI_strdup(op_src->rna_path);
361 BLI_duplicatelist(&op_dst->operations, &op_src->operations);
363 for (IDOverrideLibraryPropertyOperation *opop_dst = op_dst->operations.first,
364 *opop_src = op_src->operations.first;
366 opop_dst = opop_dst->next, opop_src = opop_src->next) {
367 lib_override_library_property_operation_copy(opop_dst, opop_src);
371 void lib_override_library_property_clear(IDOverrideLibraryProperty *op)
373 BLI_assert(op->rna_path != NULL);
375 MEM_freeN(op->rna_path);
377 for (IDOverrideLibraryPropertyOperation *opop = op->operations.first; opop; opop = opop->next) {
378 lib_override_library_property_operation_clear(opop);
380 BLI_freelistN(&op->operations);
384 * Remove and free given \a override_property from given ID \a override.
386 void BKE_lib_override_library_property_delete(IDOverrideLibrary *override,
387 IDOverrideLibraryProperty *override_property)
389 lib_override_library_property_clear(override_property);
390 if (override->runtime != NULL) {
391 BLI_ghash_remove(override->runtime, override_property->rna_path, NULL, NULL);
393 BLI_freelinkN(&override->properties, override_property);
397 * Find override property operation from given sub-item(s), if it exists.
399 IDOverrideLibraryPropertyOperation *BKE_lib_override_library_property_operation_find(
400 IDOverrideLibraryProperty *override_property,
401 const char *subitem_refname,
402 const char *subitem_locname,
403 const int subitem_refindex,
404 const int subitem_locindex,
408 IDOverrideLibraryPropertyOperation *opop;
409 const int subitem_defindex = -1;
415 if (subitem_locname != NULL) {
416 opop = BLI_findstring_ptr(&override_property->operations,
418 offsetof(IDOverrideLibraryPropertyOperation, subitem_local_name));
424 if (subitem_refname == NULL || opop->subitem_reference_name == NULL) {
425 return subitem_refname == opop->subitem_reference_name ? opop : NULL;
427 return (subitem_refname != NULL && opop->subitem_reference_name != NULL &&
428 STREQ(subitem_refname, opop->subitem_reference_name)) ?
433 if (subitem_refname != NULL) {
434 opop = BLI_findstring_ptr(
435 &override_property->operations,
437 offsetof(IDOverrideLibraryPropertyOperation, subitem_reference_name));
443 if (subitem_locname == NULL || opop->subitem_local_name == NULL) {
444 return subitem_locname == opop->subitem_local_name ? opop : NULL;
446 return (subitem_locname != NULL && opop->subitem_local_name != NULL &&
447 STREQ(subitem_locname, opop->subitem_local_name)) ?
452 if ((opop = BLI_listbase_bytes_find(
453 &override_property->operations,
455 sizeof(subitem_locindex),
456 offsetof(IDOverrideLibraryPropertyOperation, subitem_local_index)))) {
457 return ELEM(subitem_refindex, -1, opop->subitem_reference_index) ? opop : NULL;
460 if ((opop = BLI_listbase_bytes_find(
461 &override_property->operations,
463 sizeof(subitem_refindex),
464 offsetof(IDOverrideLibraryPropertyOperation, subitem_reference_index)))) {
465 return ELEM(subitem_locindex, -1, opop->subitem_local_index) ? opop : NULL;
468 /* index == -1 means all indices, that is valid fallback in case we requested specific index. */
469 if (!strict && (subitem_locindex != subitem_defindex) &&
470 (opop = BLI_listbase_bytes_find(
471 &override_property->operations,
473 sizeof(subitem_defindex),
474 offsetof(IDOverrideLibraryPropertyOperation, subitem_local_index)))) {
485 * Find override property operation from given sub-item(s), or create it if it does not exist.
487 IDOverrideLibraryPropertyOperation *BKE_lib_override_library_property_operation_get(
488 IDOverrideLibraryProperty *override_property,
489 const short operation,
490 const char *subitem_refname,
491 const char *subitem_locname,
492 const int subitem_refindex,
493 const int subitem_locindex,
498 IDOverrideLibraryPropertyOperation *opop = BKE_lib_override_library_property_operation_find(
508 opop = MEM_callocN(sizeof(IDOverrideLibraryPropertyOperation), __func__);
509 opop->operation = operation;
510 if (subitem_locname) {
511 opop->subitem_local_name = BLI_strdup(subitem_locname);
513 if (subitem_refname) {
514 opop->subitem_reference_name = BLI_strdup(subitem_refname);
516 opop->subitem_local_index = subitem_locindex;
517 opop->subitem_reference_index = subitem_refindex;
519 BLI_addtail(&override_property->operations, opop);
525 else if (r_created) {
532 void lib_override_library_property_operation_copy(IDOverrideLibraryPropertyOperation *opop_dst,
533 IDOverrideLibraryPropertyOperation *opop_src)
535 if (opop_src->subitem_reference_name) {
536 opop_dst->subitem_reference_name = BLI_strdup(opop_src->subitem_reference_name);
538 if (opop_src->subitem_local_name) {
539 opop_dst->subitem_local_name = BLI_strdup(opop_src->subitem_local_name);
543 void lib_override_library_property_operation_clear(IDOverrideLibraryPropertyOperation *opop)
545 if (opop->subitem_reference_name) {
546 MEM_freeN(opop->subitem_reference_name);
548 if (opop->subitem_local_name) {
549 MEM_freeN(opop->subitem_local_name);
554 * Remove and free given \a override_property_operation from given ID \a override_property.
556 void BKE_lib_override_library_property_operation_delete(
557 IDOverrideLibraryProperty *override_property,
558 IDOverrideLibraryPropertyOperation *override_property_operation)
560 lib_override_library_property_operation_clear(override_property_operation);
561 BLI_freelinkN(&override_property->operations, override_property_operation);
565 * Check that status of local data-block is still valid against current reference one.
567 * It means that all overridable, but not overridden, properties' local values must be equal to
568 * reference ones. Clears #LIB_TAG_OVERRIDE_OK if they do not.
570 * This is typically used to detect whether some property has been changed in local and a new
571 * #IDOverrideProperty (of #IDOverridePropertyOperation) has to be added.
573 * \return true if status is OK, false otherwise. */
574 bool BKE_lib_override_library_status_check_local(Main *bmain, ID *local)
576 BLI_assert(local->override_library != NULL);
578 ID *reference = local->override_library->reference;
580 if (reference == NULL) {
581 /* This is an override template, local status is always OK! */
585 BLI_assert(GS(local->name) == GS(reference->name));
587 if (GS(local->name) == ID_OB) {
588 /* Our beloved pose's bone cross-data pointers... Usually, depsgraph evaluation would ensure
589 * this is valid, but in some cases (like hidden collections etc.) this won't be the case, so
590 * we need to take care of this ourselves. */
591 Object *ob_local = (Object *)local;
592 if (ob_local->data != NULL && ob_local->type == OB_ARMATURE && ob_local->pose != NULL &&
593 ob_local->pose->flag & POSE_RECALC) {
594 BKE_pose_rebuild(bmain, ob_local, ob_local->data, true);
598 /* Note that reference is assumed always valid, caller has to ensure that itself. */
600 PointerRNA rnaptr_local, rnaptr_reference;
601 RNA_id_pointer_create(local, &rnaptr_local);
602 RNA_id_pointer_create(reference, &rnaptr_reference);
604 if (!RNA_struct_override_matches(bmain,
608 local->override_library,
609 RNA_OVERRIDE_COMPARE_IGNORE_NON_OVERRIDABLE |
610 RNA_OVERRIDE_COMPARE_IGNORE_OVERRIDDEN,
612 local->tag &= ~LIB_TAG_OVERRIDE_LIBRARY_REFOK;
620 * Check that status of reference data-block is still valid against current local one.
622 * It means that all non-overridden properties' local values must be equal to reference ones.
623 * Clears LIB_TAG_OVERRIDE_OK if they do not.
625 * This is typically used to detect whether some reference has changed and local
626 * needs to be updated against it.
628 * \return true if status is OK, false otherwise. */
629 bool BKE_lib_override_library_status_check_reference(Main *bmain, ID *local)
631 BLI_assert(local->override_library != NULL);
633 ID *reference = local->override_library->reference;
635 if (reference == NULL) {
636 /* This is an override template, reference is virtual, so its status is always OK! */
640 BLI_assert(GS(local->name) == GS(reference->name));
642 if (reference->override_library && (reference->tag & LIB_TAG_OVERRIDE_LIBRARY_REFOK) == 0) {
643 if (!BKE_lib_override_library_status_check_reference(bmain, reference)) {
644 /* If reference is also override of another data-block, and its status is not OK,
645 * then this override is not OK either.
646 * Note that this should only happen when reloading libraries... */
647 local->tag &= ~LIB_TAG_OVERRIDE_LIBRARY_REFOK;
652 if (GS(local->name) == ID_OB) {
653 /* Our beloved pose's bone cross-data pointers... Usually, depsgraph evaluation would ensure
654 * this is valid, but in some cases (like hidden collections etc.) this won't be the case, so
655 * we need to take care of this ourselves. */
656 Object *ob_local = (Object *)local;
657 if (ob_local->data != NULL && ob_local->type == OB_ARMATURE && ob_local->pose != NULL &&
658 ob_local->pose->flag & POSE_RECALC) {
659 BKE_pose_rebuild(bmain, ob_local, ob_local->data, true);
663 PointerRNA rnaptr_local, rnaptr_reference;
664 RNA_id_pointer_create(local, &rnaptr_local);
665 RNA_id_pointer_create(reference, &rnaptr_reference);
667 if (!RNA_struct_override_matches(bmain,
671 local->override_library,
672 RNA_OVERRIDE_COMPARE_IGNORE_OVERRIDDEN,
674 local->tag &= ~LIB_TAG_OVERRIDE_LIBRARY_REFOK;
682 * Compares local and reference data-blocks and create new override operations as needed,
683 * or reset to reference values if overriding is not allowed.
685 * \note Defining override operations is only mandatory before saving a `.blend` file on disk
687 * Knowing that info at runtime is only useful for UI/UX feedback.
689 * \note This is by far the biggest operation (the more time-consuming) of the three so far,
690 * since it has to go over all properties in depth (all overridable ones at least).
691 * Generating diff values and applying overrides are much cheaper.
693 * \return true if new overriding op was created, or some local data was reset. */
694 bool BKE_lib_override_library_operations_create(Main *bmain, ID *local, const bool force_auto)
696 BLI_assert(local->override_library != NULL);
697 const bool is_template = (local->override_library->reference == NULL);
700 if (!is_template && (force_auto || local->override_library->flag & OVERRIDE_LIBRARY_AUTO)) {
701 /* Do not attempt to generate overriding rules from an empty place-holder generated by link
702 * code when it cannot find to actual library/ID. Much better to keep the local datablock as
703 * is in the file in that case, until broken lib is fixed. */
704 if (ID_MISSING(local->override_library->reference)) {
708 if (GS(local->name) == ID_OB) {
709 /* Our beloved pose's bone cross-data pointers... Usually, depsgraph evaluation would ensure
710 * this is valid, but in some cases (like hidden collections etc.) this won't be the case, so
711 * we need to take care of this ourselves. */
712 Object *ob_local = (Object *)local;
713 if (ob_local->data != NULL && ob_local->type == OB_ARMATURE && ob_local->pose != NULL &&
714 ob_local->pose->flag & POSE_RECALC) {
715 BKE_pose_rebuild(bmain, ob_local, ob_local->data, true);
719 PointerRNA rnaptr_local, rnaptr_reference;
720 RNA_id_pointer_create(local, &rnaptr_local);
721 RNA_id_pointer_create(local->override_library->reference, &rnaptr_reference);
723 eRNAOverrideMatchResult report_flags = 0;
724 RNA_struct_override_matches(bmain,
728 local->override_library,
729 RNA_OVERRIDE_COMPARE_CREATE | RNA_OVERRIDE_COMPARE_RESTORE,
731 if (report_flags & RNA_OVERRIDE_MATCH_RESULT_CREATED) {
735 if (report_flags & RNA_OVERRIDE_MATCH_RESULT_RESTORED) {
736 printf("We did restore some properties of %s from its reference.\n", local->name);
739 printf("We did generate library override rules for %s\n", local->name);
742 printf("No new library override rules for %s\n", local->name);
749 /** Check all overrides from given \a bmain and create/update overriding operations as needed. */
750 void BKE_lib_override_library_main_operations_create(Main *bmain, const bool force_auto)
754 FOREACH_MAIN_ID_BEGIN (bmain, id) {
755 if ((ID_IS_OVERRIDE_LIBRARY(id) && force_auto) ||
756 (ID_IS_OVERRIDE_LIBRARY_AUTO(id) && (id->tag & LIB_TAG_OVERRIDE_LIBRARY_AUTOREFRESH))) {
757 BKE_lib_override_library_operations_create(bmain, id, force_auto);
758 id->tag &= ~LIB_TAG_OVERRIDE_LIBRARY_AUTOREFRESH;
764 /** Update given override from its reference (re-applying overridden properties). */
765 void BKE_lib_override_library_update(Main *bmain, ID *local)
767 if (local->override_library == NULL || local->override_library->reference == NULL) {
771 /* Do not attempt to apply overriding rules over an empty place-holder generated by link code
772 * when it cannot find to actual library/ID. Much better to keep the local datablock as loaded
773 * from the file in that case, until broken lib is fixed. */
774 if (ID_MISSING(local->override_library->reference)) {
778 /* Recursively do 'ancestors' overrides first, if any. */
779 if (local->override_library->reference->override_library &&
780 (local->override_library->reference->tag & LIB_TAG_OVERRIDE_LIBRARY_REFOK) == 0) {
781 BKE_lib_override_library_update(bmain, local->override_library->reference);
784 /* We want to avoid having to remap here, however creating up-to-date override is much simpler
785 * if based on reference than on current override.
786 * So we work on temp copy of reference, and 'swap' its content with local. */
788 /* XXX We need a way to get off-Main copies of IDs (similar to localized mats/texts/ etc.)!
789 * However, this is whole bunch of code work in itself, so for now plain stupid ID copy will
790 * do, as inn-efficient as it is. :/
791 * Actually, maybe not! Since we are swapping with original ID's local content, we want to
792 * keep user-count in correct state when freeing tmp_id
793 * (and that user-counts of IDs used by 'new' local data also remain correct). */
794 /* This would imply change in handling of user-count all over RNA
795 * (and possibly all over Blender code).
796 * Not impossible to do, but would rather see first if extra useless usual user handling
797 * is actually a (performances) issue here. */
800 BKE_id_copy(bmain, local->override_library->reference, &tmp_id);
802 if (tmp_id == NULL) {
806 PointerRNA rnaptr_src, rnaptr_dst, rnaptr_storage_stack, *rnaptr_storage = NULL;
807 RNA_id_pointer_create(local, &rnaptr_src);
808 RNA_id_pointer_create(tmp_id, &rnaptr_dst);
809 if (local->override_library->storage) {
810 rnaptr_storage = &rnaptr_storage_stack;
811 RNA_id_pointer_create(local->override_library->storage, rnaptr_storage);
814 RNA_struct_override_apply(
815 bmain, &rnaptr_dst, &rnaptr_src, rnaptr_storage, local->override_library);
817 /* This also transfers all pointers (memory) owned by local to tmp_id, and vice-versa.
818 * So when we'll free tmp_id, we'll actually free old, outdated data from local. */
819 BKE_id_swap(bmain, local, tmp_id);
821 /* Again, horribly inn-efficient in our case, we need something off-Main
822 * (aka more generic nolib copy/free stuff)! */
823 /* XXX And crashing in complex cases (e.g. because depsgraph uses same data...). */
824 BKE_id_free_ex(bmain, tmp_id, LIB_ID_FREE_NO_UI_USER, true);
826 if (local->override_library->storage) {
827 /* We know this datablock is not used anywhere besides local->override->storage. */
828 /* XXX For until we get fully shadow copies, we still need to ensure storage releases
829 * its usage of any ID pointers it may have. */
830 BKE_id_free_ex(bmain, local->override_library->storage, LIB_ID_FREE_NO_UI_USER, true);
831 local->override_library->storage = NULL;
834 local->tag |= LIB_TAG_OVERRIDE_LIBRARY_REFOK;
836 /* Full rebuild of Depsgraph! */
838 /* XXX Is this actual valid replacement for old DAG_relations_tag_update(bmain) ? */
839 DEG_on_visible_update(bmain, true);
842 /** Update all overrides from given \a bmain. */
843 void BKE_lib_override_library_main_update(Main *bmain)
847 FOREACH_MAIN_ID_BEGIN (bmain, id) {
848 if (id->override_library != NULL && id->lib == NULL) {
849 BKE_lib_override_library_update(bmain, id);
856 * Storage (how to store overriding data into `.blend` files).
859 * 1) Only 'differential' storage needs special handling here. All others (replacing values or
860 * inserting/removing items from a collection) can be handled with simply storing current
861 * content of local data-block.
862 * 2) We store the differential value into a second 'ghost' data-block,
863 * which is an empty ID of same type as local one,
864 * where we only define values that need differential data.
866 * This avoids us having to modify 'real' data-block at write time (and restoring it afterwards),
867 * which is inefficient, and potentially dangerous (in case of concurrent access...), while not
868 * using much extra memory in typical cases. It also ensures stored data-block always contains
869 * exact same data as "desired" ones (kind of "baked" data-blocks).
872 /** Initialize an override storage. */
873 OverrideLibraryStorage *BKE_lib_override_library_operations_store_initialize(void)
875 return BKE_main_new();
879 * Generate suitable 'write' data (this only affects differential override operations).
881 * Note that \a local ID is no more modified by this call,
882 * all extra data are stored in its temp \a storage_id copy. */
883 ID *BKE_lib_override_library_operations_store_start(Main *bmain,
884 OverrideLibraryStorage *override_storage,
887 BLI_assert(local->override_library != NULL);
888 BLI_assert(override_storage != NULL);
889 const bool is_template = (local->override_library->reference == NULL);
892 /* This is actually purely local data with an override template, nothing to do here! */
896 /* Forcefully ensure we know about all needed override operations. */
897 BKE_lib_override_library_operations_create(bmain, local, false);
900 #ifdef DEBUG_OVERRIDE_TIMEIT
901 TIMEIT_START_AVERAGED(BKE_override_operations_store_start);
904 /* XXX TODO We may also want a specialized handling of things here too, to avoid copying heavy
905 * never-overridable data (like Mesh geometry etc.)? And also maybe avoid lib reference-counting
906 * completely (shallow copy...). */
907 /* This would imply change in handling of user-count all over RNA
908 * (and possibly all over Blender code).
909 * Not impossible to do, but would rather see first is extra useless usual user handling is
910 * actually a (performances) issue here, before doing it. */
911 BKE_id_copy((Main *)override_storage, local, &storage_id);
913 if (storage_id != NULL) {
914 PointerRNA rnaptr_reference, rnaptr_final, rnaptr_storage;
915 RNA_id_pointer_create(local->override_library->reference, &rnaptr_reference);
916 RNA_id_pointer_create(local, &rnaptr_final);
917 RNA_id_pointer_create(storage_id, &rnaptr_storage);
919 if (!RNA_struct_override_store(
920 bmain, &rnaptr_final, &rnaptr_reference, &rnaptr_storage, local->override_library)) {
921 BKE_id_free_ex(override_storage, storage_id, LIB_ID_FREE_NO_UI_USER, true);
926 local->override_library->storage = storage_id;
928 #ifdef DEBUG_OVERRIDE_TIMEIT
929 TIMEIT_END_AVERAGED(BKE_override_operations_store_start);
934 /** Restore given ID modified by \a BKE_override_operations_store_start, to its original state. */
935 void BKE_lib_override_library_operations_store_end(
936 OverrideLibraryStorage *UNUSED(override_storage), ID *local)
938 BLI_assert(local->override_library != NULL);
940 /* Nothing else to do here really, we need to keep all temp override storage data-blocks in
941 * memory until whole file is written anyway (otherwise we'd get mem pointers overlap...). */
942 local->override_library->storage = NULL;
945 void BKE_lib_override_library_operations_store_finalize(OverrideLibraryStorage *override_storage)
947 /* We cannot just call BKE_main_free(override_storage), not until we have option to make 'ghost'
948 * copies of IDs without increasing usercount of used data-blocks. */
951 FOREACH_MAIN_ID_BEGIN (override_storage, id) {
952 BKE_id_free_ex(override_storage, id, LIB_ID_FREE_NO_UI_USER, true);
956 BKE_main_free(override_storage);