Blender V4.3
lib_override_proxy_conversion.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2016 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include "CLG_log.h"
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_linklist.h"
14
15/* Required for proxy to liboverrides conversion code. */
16#define DNA_DEPRECATED_ALLOW
17
18#include "DNA_ID.h"
20#include "DNA_object_types.h"
21#include "DNA_scene_types.h"
22
23#include "DEG_depsgraph.hh"
24
25#include "BKE_collection.hh"
26#include "BKE_idtype.hh"
27#include "BKE_lib_id.hh"
28#include "BKE_lib_override.hh"
29#include "BKE_main.hh"
30
31#include "BLO_readfile.hh"
32
33static CLG_LogRef LOG = {"bke.liboverride_proxy_conversion"};
34
36 Scene *scene,
37 ViewLayer *view_layer,
38 Object *ob_proxy)
39{
40 /* `proxy_group`, if defined, is the empty instantiating the collection from which the proxy is
41 * coming. */
42 Object *ob_proxy_group = ob_proxy->proxy_group;
43 const bool is_override_instancing_object = (ob_proxy_group != nullptr) &&
44 (ob_proxy_group->instance_collection != nullptr);
45 ID *id_root = is_override_instancing_object ? &ob_proxy_group->instance_collection->id :
46 &ob_proxy->proxy->id;
47 ID *id_instance_hint = is_override_instancing_object ? &ob_proxy_group->id : &ob_proxy->id;
48
49 /* In some cases the instance collection of a proxy object may be local (see e.g. #83875). Not
50 * sure this is a valid state, but for now just abort the overriding process. */
52 if (ob_proxy->proxy != nullptr) {
53 ob_proxy->proxy->proxy_from = nullptr;
54 }
55 id_us_min((ID *)ob_proxy->proxy);
56 ob_proxy->proxy = ob_proxy->proxy_group = nullptr;
57 return false;
58 }
59
60 /* We manually convert the proxy object into a library override, further override handling will
61 * then be handled by `BKE_lib_override_library_create()` just as for a regular override
62 * creation.
63 */
64 ob_proxy->proxy->id.tag |= ID_TAG_DOIT;
65 ob_proxy->proxy->id.newid = &ob_proxy->id;
66 BKE_lib_override_library_init(&ob_proxy->id, &ob_proxy->proxy->id);
67 ob_proxy->id.override_library->flag &= ~LIBOVERRIDE_FLAG_SYSTEM_DEFINED;
68
69 ob_proxy->proxy->proxy_from = nullptr;
70 ob_proxy->proxy = ob_proxy->proxy_group = nullptr;
71
73
74 /* In case of proxy conversion, remap all local ID usages to linked IDs to their newly created
75 * overrides. Also do that for the IDs from the same lib as the proxy in case it is linked.
76 * While this might not be 100% the desired behavior, it is likely to be the case most of the
77 * time. Ref: #91711. */
78 ID *id_iter;
79 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
80 if (!ID_IS_LINKED(id_iter) || id_iter->lib == ob_proxy->id.lib) {
81 id_iter->tag |= ID_TAG_DOIT;
82 }
83 }
85
87 scene,
88 view_layer,
89 ob_proxy->id.lib,
90 id_root,
91 id_root,
92 id_instance_hint,
93 nullptr,
94 false);
95}
96
98 Scene *scene,
99 Object *ob_proxy,
100 BlendFileReadReport *reports)
101{
102 Object *ob_proxy_group = ob_proxy->proxy_group;
103 const bool is_override_instancing_object = ob_proxy_group != nullptr;
104
105 const bool success = BKE_lib_override_library_proxy_convert(bmain, scene, nullptr, ob_proxy);
106
107 if (success) {
108 CLOG_INFO(&LOG,
109 4,
110 "Proxy object '%s' successfully converted to library overrides",
111 ob_proxy->id.name);
112 /* Remove the instance empty from this scene, the items now have an overridden collection
113 * instead. */
114 if (is_override_instancing_object) {
115 BKE_scene_collections_object_remove(bmain, scene, ob_proxy_group, true);
116 }
118 }
119}
120
122{
123 LISTBASE_FOREACH (Scene *, scene, &bmain->scenes) {
124 LinkNodePair proxy_objects = {nullptr};
125
126 FOREACH_SCENE_OBJECT_BEGIN (scene, object) {
127 if (object->proxy_group != nullptr) {
128 BLI_linklist_append(&proxy_objects, object);
129 }
130 }
132
133 FOREACH_SCENE_OBJECT_BEGIN (scene, object) {
134 if (object->proxy != nullptr && object->proxy_group == nullptr) {
135 BLI_linklist_append(&proxy_objects, object);
136 }
137 }
139
140 for (LinkNode *proxy_object_iter = proxy_objects.list; proxy_object_iter != nullptr;
141 proxy_object_iter = proxy_object_iter->next)
142 {
143 Object *proxy_object = static_cast<Object *>(proxy_object_iter->link);
144 lib_override_library_proxy_convert_do(bmain, scene, proxy_object, reports);
145 }
146
147 BLI_linklist_free(proxy_objects.list, nullptr);
148 }
149
150 LISTBASE_FOREACH (Object *, object, &bmain->objects) {
151 if (object->proxy_group != nullptr || object->proxy != nullptr) {
152 if (ID_IS_LINKED(object)) {
153 CLOG_WARN(&LOG,
154 "Linked proxy object '%s' from '%s' failed to be converted to library override",
155 object->id.name + 2,
156 object->id.lib->filepath);
157 }
158 else {
159 CLOG_WARN(&LOG,
160 "Proxy object '%s' failed to be converted to library override",
161 object->id.name + 2);
162 }
164 if (object->proxy != nullptr) {
165 object->proxy->proxy_from = nullptr;
166 }
167 id_us_min((ID *)object->proxy);
168 object->proxy = object->proxy_group = nullptr;
169 }
170 }
171}
#define FOREACH_SCENE_OBJECT_END
bool BKE_scene_collections_object_remove(Main *bmain, Scene *scene, Object *ob, bool free_us)
#define FOREACH_SCENE_OBJECT_BEGIN(scene, _instance)
void id_us_min(ID *id)
Definition lib_id.cc:359
bool BKE_lib_override_library_create(Main *bmain, Scene *scene, ViewLayer *view_layer, Library *owner_library, ID *id_root_reference, ID *id_hierarchy_root_reference, ID *id_instance_hint, ID **r_id_root_override, const bool do_fully_editable)
IDOverrideLibrary * BKE_lib_override_library_init(ID *local_id, ID *reference_id)
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:500
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:494
#define LISTBASE_FOREACH(type, var, list)
external readfile function prototypes.
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:181
#define CLOG_INFO(clg_ref, level,...)
Definition CLG_log.h:179
void DEG_id_tag_update(ID *id, unsigned int flags)
ID and Library types, which are fundamental for SDNA.
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1085
#define ID_IS_OVERRIDABLE_LIBRARY_HIERARCHY(_id)
Definition DNA_ID.h:665
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:654
@ ID_TAG_DOIT
Definition DNA_ID.h:1003
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
Read Guarded memory(de)allocation.
bool BKE_lib_override_library_proxy_convert(Main *bmain, Scene *scene, ViewLayer *view_layer, Object *ob_proxy)
void BKE_lib_override_library_main_proxy_convert(Main *bmain, BlendFileReadReport *reports)
static CLG_LogRef LOG
static void lib_override_library_proxy_convert_do(Main *bmain, Scene *scene, Object *ob_proxy, BlendFileReadReport *reports)
struct BlendFileReadReport::@128 count
unsigned int flag
Definition DNA_ID.h:352
Definition DNA_ID.h:413
int tag
Definition DNA_ID.h:434
struct Library * lib
Definition DNA_ID.h:419
struct ID * newid
Definition DNA_ID.h:417
IDOverrideLibrary * override_library
Definition DNA_ID.h:459
char name[66]
Definition DNA_ID.h:425
LinkNode * list
ListBase scenes
Definition BKE_main.hh:210
ListBase objects
Definition BKE_main.hh:212
struct Collection * instance_collection