Blender V5.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
8
9#include "CLG_log.h"
10
11#include "BLI_linklist.hh"
12#include "BLI_listbase.hh"
13
14/* Required for proxy to liboverrides conversion code. */
15#define DNA_DEPRECATED_ALLOW
16
17#include "DNA_ID.h"
19#include "DNA_object_types.h"
20#include "DNA_scene_types.h"
21
22#include "DEG_depsgraph.hh"
23
24#include "BKE_collection.hh"
25#include "BKE_idtype.hh"
26#include "BKE_lib_id.hh"
27#include "BKE_lib_override.hh"
28#include "BKE_main.hh"
29
30#include "BLO_readfile.hh"
31
32namespace blender {
33
34static CLG_LogRef LOG = {"lib.override.proxy_conversion"};
35
37 Scene *scene,
38 ViewLayer *view_layer,
39 Object *ob_proxy)
40{
41 /* `proxy_group`, if defined, is the empty instantiating the collection from which the proxy is
42 * coming. */
43 Object *ob_proxy_group = ob_proxy->proxy_group;
44 const bool is_override_instancing_object = (ob_proxy_group != nullptr) &&
45 (ob_proxy_group->instance_collection != nullptr);
46 ID *id_root = is_override_instancing_object ? &ob_proxy_group->instance_collection->id :
47 &ob_proxy->proxy->id;
48 ID *id_instance_hint = is_override_instancing_object ? &ob_proxy_group->id : &ob_proxy->id;
49
50 /* In some cases the instance collection of a proxy object may be local (see e.g. #83875). Not
51 * sure this is a valid state, but for now just abort the overriding process. */
53 if (ob_proxy->proxy != nullptr) {
54 ob_proxy->proxy->proxy_from = nullptr;
55 }
56 id_us_min(id_cast<ID *>(ob_proxy->proxy));
57 ob_proxy->proxy = ob_proxy->proxy_group = nullptr;
58 return false;
59 }
60
61 /* We manually convert the proxy object into a library override, further override handling will
62 * then be handled by `BKE_lib_override_library_create()` just as for a regular override
63 * creation.
64 */
65 ob_proxy->proxy->id.tag |= ID_TAG_DOIT;
66 ob_proxy->proxy->id.newid = &ob_proxy->id;
67 BKE_lib_override_library_init(&ob_proxy->id, &ob_proxy->proxy->id);
69
70 ob_proxy->proxy->proxy_from = nullptr;
71 ob_proxy->proxy = ob_proxy->proxy_group = nullptr;
72
74
75 /* In case of proxy conversion, remap all local ID usages to linked IDs to their newly created
76 * overrides. Also do that for the IDs from the same lib as the proxy in case it is linked.
77 * While this might not be 100% the desired behavior, it is likely to be the case most of the
78 * time. Ref: #91711. */
79 ID *id_iter;
80 FOREACH_MAIN_ID_BEGIN (bmain, id_iter) {
81 if (!ID_IS_LINKED(id_iter) || id_iter->lib == ob_proxy->id.lib) {
82 id_iter->tag |= ID_TAG_DOIT;
83 }
84 }
86
88 scene,
89 view_layer,
90 ob_proxy->id.lib,
91 id_root,
92 id_root,
93 id_instance_hint,
94 nullptr,
95 false);
96}
97
99 Scene *scene,
100 Object *ob_proxy,
101 BlendFileReadReport *reports)
102{
103 Object *ob_proxy_group = ob_proxy->proxy_group;
104 const bool is_override_instancing_object = ob_proxy_group != nullptr;
105
106 const bool success = BKE_lib_override_library_proxy_convert(bmain, scene, nullptr, ob_proxy);
107
108 if (success) {
109 CLOG_INFO(
110 &LOG, "Proxy object '%s' successfully converted to library overrides", ob_proxy->id.name);
111 /* Remove the instance empty from this scene, the items now have an overridden collection
112 * instead. */
113 if (is_override_instancing_object) {
114 BKE_scene_collections_object_remove(bmain, scene, ob_proxy_group, true);
115 }
116 reports->count.proxies_to_lib_overrides_success++;
117 }
118}
119
121{
122 for (Scene &scene : bmain->scenes) {
123 LinkNodePair proxy_objects = {nullptr};
124
125 FOREACH_SCENE_OBJECT_BEGIN (&scene, object) {
126 if (object->proxy_group != nullptr) {
127 BLI_linklist_append(&proxy_objects, object);
128 }
129 }
131
132 FOREACH_SCENE_OBJECT_BEGIN (&scene, object) {
133 if (object->proxy != nullptr && object->proxy_group == nullptr) {
134 BLI_linklist_append(&proxy_objects, object);
135 }
136 }
138
139 for (LinkNode *proxy_object_iter = proxy_objects.list; proxy_object_iter != nullptr;
140 proxy_object_iter = proxy_object_iter->next)
141 {
142 Object *proxy_object = static_cast<Object *>(proxy_object_iter->link);
143 lib_override_library_proxy_convert_do(bmain, &scene, proxy_object, reports);
144 }
145
146 BLI_linklist_free(proxy_objects.list, nullptr);
147 }
148
149 for (Object &object : bmain->objects) {
150 if (object.proxy_group != nullptr || object.proxy != nullptr) {
151 if (ID_IS_LINKED(&object)) {
152 CLOG_WARN(&LOG,
153 "Linked proxy object '%s' from '%s' failed to be converted to library override",
154 object.id.name + 2,
155 object.id.lib->filepath);
156 }
157 else {
158 CLOG_WARN(&LOG,
159 "Proxy object '%s' failed to be converted to library override",
160 object.id.name + 2);
161 }
162 reports->count.proxies_to_lib_overrides_failures++;
163 if (object.proxy != nullptr) {
164 object.proxy->proxy_from = nullptr;
165 }
166 id_us_min(id_cast<ID *>(object.proxy));
167 object.proxy = object.proxy_group = nullptr;
168 }
169 }
170}
171
172} // namespace blender
#define FOREACH_SCENE_OBJECT_END
#define FOREACH_SCENE_OBJECT_BEGIN(scene, _instance)
#define FOREACH_MAIN_ID_END
Definition BKE_main.hh:735
#define FOREACH_MAIN_ID_BEGIN(_bmain, _id)
Definition BKE_main.hh:729
external readfile function prototypes.
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
ID and Library types, which are fundamental for SDNA.
#define ID_IS_OVERRIDABLE_LIBRARY_HIERARCHY(_id)
Definition DNA_ID.h:715
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
BMesh Iterators.
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)
static void lib_override_library_proxy_convert_do(Main *bmain, Scene *scene, Object *ob_proxy, BlendFileReadReport *reports)
bool BKE_scene_collections_object_remove(Main *bmain, Scene *scene, Object *ob, bool free_us)
@ LIBOVERRIDE_FLAG_SYSTEM_DEFINED
Definition DNA_ID.h:308
void BKE_lib_override_library_main_proxy_convert(Main *bmain, BlendFileReadReport *reports)
void DEG_id_tag_update(ID *id, unsigned int flags)
IDOverrideLibrary * BKE_lib_override_library_init(ID *local_id, ID *reference_id)
void void void void BLI_linklist_append(LinkNodePair *list_pair, void *ptr) ATTR_NONNULL(1)
void BLI_linklist_free(LinkNode *list, LinkNodeFreeFP freefunc)
bool BKE_lib_override_library_proxy_convert(Main *bmain, Scene *scene, ViewLayer *view_layer, Object *ob_proxy)
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_TAG_DOIT
Definition DNA_ID.h:1029
static CLG_LogRef LOG
Definition logging.cpp:12
void id_us_min(ID *id)
Definition lib_id.cc:441
struct blender::BlendFileReadReport::@140003175311343306107257242277261221002217304000 count
eID_OverrideLib_Flag flag
Definition DNA_ID.h:356
char name[258]
Definition DNA_ID.h:433
struct ID * newid
Definition DNA_ID.h:419
struct Library * lib
Definition DNA_ID.h:421
IDOverrideLibrary * override_library
Definition DNA_ID.h:495
struct LinkNode * next
ListBaseT< Object > objects
Definition BKE_main.hh:388
ListBaseT< Scene > scenes
Definition BKE_main.hh:386
DNA_DEPRECATED struct Object * proxy_group
DNA_DEPRECATED struct Object * proxy
DNA_DEPRECATED struct Object * proxy_from
struct Collection * instance_collection