Blender V5.3
tree_element_overrides.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_lib_override.hh"
10
11#include "BLI_function_ref.hh"
13#include "BLI_map.hh"
14#include "BLI_utildefines.hh"
15
16#include "BLT_translation.hh"
17
18#include "DNA_space_types.h"
19
20#include "RNA_access.hh"
21#include "RNA_path.hh"
22
23#include "../outliner_intern.hh"
24
25#include "tree_display.hh"
26#include "tree_element_label.hh"
28
29#include <stdexcept>
30
31namespace blender::ed::outliner {
32
34 AbstractTreeDisplay &tree_display_;
36
37 public:
39 void build_path(TreeElement &parent, TreeElementOverridesData &override_data, short &index);
40
41 private:
42 TreeElement &ensure_label_element_for_prop(
43 TreeElement &parent, StringRef elem_path, PointerRNA &ptr, PropertyRNA &prop, short &index);
44 TreeElement &ensure_label_element_for_ptr(TreeElement &parent,
45 StringRef elem_path,
46 PointerRNA &ptr,
47 short &index);
48 void ensure_entire_collection(TreeElement &te_to_expand,
49 const TreeElementOverridesData &override_data,
50 const char *coll_prop_path,
51 short &index);
52};
53
54/* -------------------------------------------------------------------- */
61
63 : AbstractTreeElement(legacy_te), id(id)
64{
66 if (legacy_te.parent != nullptr &&
68 {
69 legacy_te.name = IFACE_("Library Overrides");
70 }
71 else {
72 legacy_te.name = id.name + 2;
73 }
74}
75
77{
79 return RPT_("This override data-block is not needed anymore, but was detected as user-edited");
80 }
81
82 if (ID_IS_OVERRIDE_LIBRARY_REAL(&id) && ID_REAL_USERS(&id) == 0) {
83 return RPT_("This override data-block is unused");
84 }
85
86 return {};
87}
88
90 const bool show_system_overrides,
92{
93 PointerRNA override_rna_ptr;
94 PropertyRNA *override_rna_prop;
95
97
98 for (IDOverrideLibraryProperty *override_prop :
99 ListBaseWrapper<IDOverrideLibraryProperty>(id.override_library->properties))
100 {
101 int rnaprop_index = 0;
102 const bool is_rna_path_valid = BKE_lib_override_rna_property_find(
103 &idpoin, override_prop, &override_rna_ptr, &override_rna_prop, &rnaprop_index);
104
105 /* Check for conditions where the liboverride property should be considered as a system
106 * override, if needed. */
107 if (is_rna_path_valid && !show_system_overrides) {
108 bool do_skip = true;
109 bool is_system_override = false;
110
111 /* Matching ID pointers are considered as system overrides. */
112 if (ELEM(override_prop->rna_prop_type, PROP_POINTER, PROP_COLLECTION) &&
113 RNA_struct_is_ID(RNA_property_pointer_type(&override_rna_ptr, override_rna_prop)))
114 {
115 for (IDOverrideLibraryPropertyOperation *override_prop_op :
117 {
118 if ((override_prop_op->flag & LIBOVERRIDE_OP_FLAG_IDPOINTER_MATCH_REFERENCE) == 0) {
119 do_skip = false;
120 break;
121 }
122 is_system_override = true;
123 }
124 }
125
126 /* Animated/driven properties are considered as system overrides. */
127 if (!is_system_override && !BKE_lib_override_library_property_is_animated(
128 &id, override_prop, override_rna_prop, rnaprop_index))
129 {
130 do_skip = false;
131 }
132
133 if (do_skip) {
134 continue;
135 }
136 }
137
139 id, *override_prop, override_rna_ptr, *override_rna_prop, is_rna_path_valid};
140
141 fn(data);
142 }
143}
144
146{
147 BLI_assert(id.override_library != nullptr);
148
149 const bool show_system_overrides = (SUPPORT_FILTER_OUTLINER(&space_outliner) &&
150 (space_outliner.filter & SO_FILTER_SHOW_SYSTEM_OVERRIDES) !=
151 0);
152
154 short index = 0;
155
156 iterate_properties_to_display(id, show_system_overrides, [&](TreeElementOverridesData &data) {
157 path_builder.build_path(legacy_te_, data, index);
158 });
159}
160
162
163/* -------------------------------------------------------------------- */
169
171{
172 return &override_data.id;
173}
174
176{
177 return &override_data.id;
178}
179
181 TreeElementOverridesData &override_data)
182 : AbstractTreeElement(legacy_te),
183 override_rna_ptr(override_data.override_rna_ptr),
185 rna_path(override_data.override_property.rna_path),
187{
190
191 legacy_te.name = RNA_property_ui_name(&override_data.override_rna_prop);
192}
193
195{
196 if (!is_rna_path_valid) {
197 return RPT_(
198 "This override property does not exist in current data, it will be removed on "
199 "next .blend file save");
200 }
201
202 return {};
203}
204
211
213
214/* -------------------------------------------------------------------- */
219
221 TreeElement &legacy_te, TreeElementOverridesData &override_data)
222 : TreeElementOverridesProperty(legacy_te, override_data)
223{
226 "Override operations are only supported for collections right now");
227 /* Quiet Clang Static Analyzer warning by throwing instead of asserting (possible
228 * null-dereference). */
229 if (!override_data.operation) {
230 throw std::invalid_argument("missing operation");
231 }
232
233 operation_ = std::make_unique<IDOverrideLibraryPropertyOperation>(*override_data.operation);
234 /* Just for extra sanity. */
235 operation_->next = operation_->prev = nullptr;
236
237 if (std::optional<PointerRNA> col_item_ptr = get_collection_ptr()) {
238 const char *dyn_name = RNA_struct_name_get_alloc(&*col_item_ptr, nullptr, 0, nullptr);
239 if (dyn_name) {
240 legacy_te.name = dyn_name;
241 legacy_te.flag |= TE_FREE_NAME;
242 }
243 else {
244 legacy_te.name = RNA_struct_ui_name(col_item_ptr->type);
245 }
246 }
247}
248
250{
251 if (operation_->label) {
252 return operation_->label;
253 }
254 switch (eID_OverrideLib_Op(operation_->operation)) {
257 return RPT_("Added through override");
259 /* Returning nothing so that drawing code shows actual RNA button instead. */
260 return {};
261 /* Following cases are not expected in regular situation, but could be found in experimental
262 * files. */
264 return RPT_("Protected from override");
266 return RPT_("Additive override");
268 return RPT_("Subtractive override");
270 return RPT_("Multiplicative override");
272 return RPT_("Custom override");
273 }
274 return RPT_("Unknown override");
275}
276
278{
279 if (operation_->tooltip) {
280 return operation_->tooltip;
281 }
282 return {};
283}
284
286{
287 if (const std::optional<PointerRNA> col_item_ptr = get_collection_ptr()) {
288 return RNA_struct_ui_icon(col_item_ptr->type);
289 }
290
291 return {};
292}
293
295{
296 return operation_->operation;
297}
298
301{
304 for (IDOverrideLibraryPropertyOperation &opop : override_property.operations) {
305 if (*operation_ == opop) {
306 return &opop;
307 }
308 }
309 return nullptr;
310}
311
312std::optional<PointerRNA> TreeElementOverridesPropertyOperation::get_collection_ptr() const
313{
314 PointerRNA col_item_ptr;
317 operation_->subitem_local_index,
318 &col_item_ptr))
319 {
320 return col_item_ptr;
321 }
322
323 return {};
324}
325
327
328/* -------------------------------------------------------------------- */
350
352 : tree_display_(tree_display)
353{
354}
355
357 TreeElementOverridesData &override_data,
358 short &index)
359{
360 PointerRNA idpoin = RNA_id_pointer_create(&override_data.id);
361
362 Vector<PropertyElemRNA> path_elems;
363 if (!RNA_path_resolve_elements(&idpoin, override_data.override_property.rna_path, &path_elems)) {
364 return;
365 }
366
367 const char *elem_path = nullptr;
368 TreeElement *te_to_expand = &parent;
369 char name_buf[128], *name;
370
371 /* The last element is added as #TSE_LIBRARY_OVERRIDE below. */
372 for (const int i : path_elems.index_range().drop_back(1)) {
373 PropertyElemRNA &elem = path_elems[i];
374 PropertyElemRNA &next_elem = path_elems[i + 1];
375 const char *previous_path = elem_path;
376 const char *new_path = RNA_path_append(previous_path, &elem.ptr, elem.prop, -1, nullptr);
377
378 te_to_expand = &ensure_label_element_for_prop(
379 *te_to_expand, new_path, elem.ptr, *elem.prop, index);
380
381 /* Above the collection property was added (e.g. "Modifiers"), to get the actual collection
382 * item the path refers to, we have to peek at the following path element and add a tree
383 * element for its pointer (e.g. "My Subdiv Modifier"). */
385 const int coll_item_idx = RNA_property_collection_lookup_index(
386 &elem.ptr, elem.prop, &next_elem.ptr);
387 name = RNA_struct_name_get_alloc(&next_elem.ptr, name_buf, sizeof(name_buf), nullptr);
388 const char *coll_item_path = RNA_path_append(
389 previous_path, &elem.ptr, elem.prop, coll_item_idx, name);
390 if (name && (name != name_buf)) {
391 MEM_delete(name);
392 }
393
394 te_to_expand = &ensure_label_element_for_ptr(
395 *te_to_expand, coll_item_path, next_elem.ptr, index);
396
397 MEM_delete(new_path);
398 new_path = coll_item_path;
399 }
400
401 if (new_path) {
402 MEM_delete(elem_path);
403 elem_path = new_path;
404 }
405 }
406
407 /* Special case: Overriding collections, e.g. adding or removing items. In this case we add
408 * elements for all collection items to show full context, and indicate which ones were
409 * added/removed (currently added only). Note that a single collection override may add/remove
410 * multiple items. */
411 if (RNA_property_type(&override_data.override_rna_prop) == PROP_COLLECTION) {
412 /* Tree element for the actual collection item (e.g. "Modifiers"). Can just use the override
413 * ptr & prop here, since they point to the collection property (e.g. `modifiers`). */
414 te_to_expand = &ensure_label_element_for_prop(*te_to_expand,
415 override_data.override_property.rna_path,
416 override_data.override_rna_ptr,
417 override_data.override_rna_prop,
418 index);
419
420 ensure_entire_collection(*te_to_expand, override_data, elem_path, index);
421 }
422 /* Some properties have multiple operations (e.g. an array property with multiple changed
423 * values), so the element may already be present. At this point they are displayed as a single
424 * property in the tree, so don't add it multiple times here. */
425 else if (!path_te_map.contains(override_data.override_property.rna_path)) {
426 tree_display_.add_element<TreeElementOverridesProperty>(
427 {.parent = te_to_expand, .index = index++}, override_data);
428 }
429
430 MEM_delete(elem_path);
431}
432
433void OverrideRNAPathTreeBuilder::ensure_entire_collection(
434 TreeElement &te_to_expand,
435 const TreeElementOverridesData &override_data,
436 /* The path of the owning collection property. */
437 const char *coll_prop_path,
438 short &index)
439{
440 BLI_assert(tree_element_cast<AbstractTreeElement>(&te_to_expand) != nullptr);
441
442 TreeElement *previous_te = nullptr;
443 int item_idx = 0;
444 char name_buf[128], *name;
445 RNA_PROP_BEGIN (&override_data.override_rna_ptr, itemptr, &override_data.override_rna_prop) {
446 name = RNA_struct_name_get_alloc(&itemptr, name_buf, sizeof(name_buf), nullptr);
447 const char *coll_item_path = RNA_path_append(coll_prop_path,
448 &override_data.override_rna_ptr,
449 &override_data.override_rna_prop,
450 item_idx,
451 name);
452 if (name && (name != name_buf)) {
453 MEM_delete(name);
454 }
455 IDOverrideLibraryPropertyOperation *item_operation =
457 nullptr,
458 nullptr,
459 {},
460 {},
461 -1,
462 item_idx,
463 false,
464 nullptr);
465 TreeElement *current_te = nullptr;
466
467 TreeElement *existing_te = path_te_map.lookup_default(coll_item_path, nullptr);
468
469 if (existing_te) {
470 /* Reinsert the element to make sure the order is right. It may have been inserted by a
471 * previous override. */
472 BLI_remlink(&te_to_expand.subtree, existing_te);
473 BLI_insertlinkafter(&te_to_expand.subtree, previous_te, existing_te);
474 current_te = existing_te;
475 }
476 /* Is there an operation for this item (added or removed the item to/from the collection)? If
477 * so indicate it as override using #TSE_LIBRARY_OVERRIDE_OPERATION. Otherwise it's just a
478 * regular collection we display for context. */
479 else if (item_operation) {
480 TreeElementOverridesData override_op_data = override_data;
481 override_op_data.operation = item_operation;
482
483 /* Element will store a copy. */
484 current_te = tree_display_.add_element<TreeElementOverridesPropertyOperation>(
485 {.parent = &te_to_expand, .index = index++}, override_op_data);
486 }
487 else {
488 /* NOTE: Do not generate entries for collection items which are not affected by liboverride,
489 * this is more disturbing than useful. */
490 }
491
492 MEM_delete(coll_item_path);
493 item_idx++;
494 if (current_te) {
495 previous_te = current_te;
496 }
497 }
499}
500
502{
503 BIFIconID icon = RNA_property_ui_icon(&prop);
504 if (icon) {
505 return icon;
506 }
507
508 /* Try if the collection item type has a dedicated icon (e.g. #ICON_MODIFIER for the
509 * #Object.modifiers property). */
510 if (RNA_property_type(&prop) == PROP_COLLECTION) {
511 const StructRNA *coll_ptr_type = RNA_property_pointer_type(&ptr, &prop);
512 icon = RNA_struct_ui_icon(coll_ptr_type);
513 if (icon != ICON_DOT) {
514 return icon;
515 }
516 }
517
518 return ICON_NONE;
519}
520
521TreeElement &OverrideRNAPathTreeBuilder::ensure_label_element_for_prop(
522 TreeElement &parent, StringRef elem_path, PointerRNA &ptr, PropertyRNA &prop, short &index)
523{
524 return *path_te_map.lookup_or_add_cb(elem_path, [&]() {
525 TreeElement *new_te = tree_display_.add_element<TreeElementLabel>(
526 {.parent = &parent, .index = index++, .expand = false}, RNA_property_ui_name(&prop));
528
529 te_label->set_icon(get_property_icon(ptr, prop));
530 return new_te;
531 });
532}
533
534TreeElement &OverrideRNAPathTreeBuilder::ensure_label_element_for_ptr(TreeElement &parent,
535 StringRef elem_path,
536 PointerRNA &ptr,
537 short &index)
538{
539 return *path_te_map.lookup_or_add_cb(elem_path, [&]() {
540 const char *dyn_name = RNA_struct_name_get_alloc(&ptr, nullptr, 0, nullptr);
541
542 TreeElement *new_te = tree_display_.add_element<TreeElementLabel>(
543 {.parent = &parent, .index = index++}, dyn_name ? dyn_name : RNA_struct_ui_name(ptr.type));
544 TreeElementLabel *te_label = tree_element_cast<TreeElementLabel>(new_te);
545 te_label->set_icon(RNA_struct_ui_icon(ptr.type));
546
547 MEM_delete(dyn_name);
548
549 return new_te;
550 });
551}
552
554
555} // namespace blender::ed::outliner
#define RPT_(msgid)
#define IFACE_(msgid)
#define ID_IS_OVERRIDE_LIBRARY_REAL(_id)
Definition DNA_ID.h:729
#define ID_REAL_USERS(id)
Definition DNA_ID.h:679
#define RNA_PROP_END
#define RNA_PROP_BEGIN(sptr, itemptr, prop)
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
constexpr IndexRange drop_back(int64_t n) const
constexpr const char * c_str() const
IndexRange index_range() const
TreeElement * add_element(const TreeElementAddParams &params, Args &&...args)
void build_path(TreeElement &parent, TreeElementOverridesData &override_data, short &index)
OverrideRNAPathTreeBuilder(AbstractTreeDisplay &tree_display)
TreeElementOverridesPropertyOperation(TreeElement &legacy_te, TreeElementOverridesData &override_data)
static ID * owner_id(TreeElementOverridesData &override_data)
IDOverrideLibraryPropertyOperation * get_override_operation_from_id(ID &id, IDOverrideLibraryProperty &override_property) const
TreeElementOverridesProperty(TreeElement &legacy_te, TreeElementOverridesData &override_data)
IDOverrideLibraryProperty * get_override_property_from_id(ID &id) const
static ID * owner_id(TreeElementOverridesData &override_data)
#define index(i)
static void iterate_properties_to_display(ID &id, const bool show_system_overrides, FunctionRef< void(TreeElementOverridesData &data)> fn)
TreeElementT * tree_element_cast(const TreeElement *te)
static BIFIconID get_property_icon(PointerRNA &ptr, PropertyRNA &prop)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ SO_FILTER_SHOW_SYSTEM_OVERRIDES
IDOverrideLibraryPropertyOperation * BKE_lib_override_library_property_operation_find(IDOverrideLibraryProperty *liboverride_property, const char *subitem_refname, const char *subitem_locname, const std::optional< const ID * > &subitem_refid, const std::optional< const ID * > &subitem_locid, int subitem_refindex, int subitem_locindex, bool strict, bool *r_strict)
PointerRNA RNA_id_pointer_create(ID *id)
int RNA_property_collection_lookup_index(PointerRNA *ptr, PropertyRNA *prop, const PointerRNA *t_ptr)
char * RNA_struct_name_get_alloc(PointerRNA *ptr, char *fixedbuf, int fixedlen, int *r_len)
const char * RNA_struct_ui_name(const StructRNA *type)
eID_OverrideLib_Op
Definition DNA_ID.h:182
@ LIBOVERRIDE_OP_ADD
Definition DNA_ID.h:192
@ LIBOVERRIDE_OP_INSERT_AFTER
Definition DNA_ID.h:200
@ LIBOVERRIDE_OP_SUBTRACT
Definition DNA_ID.h:194
@ LIBOVERRIDE_OP_MULTIPLY
Definition DNA_ID.h:196
@ LIBOVERRIDE_OP_NOOP
Definition DNA_ID.h:185
@ LIBOVERRIDE_OP_CUSTOM
Definition DNA_ID.h:212
@ LIBOVERRIDE_OP_REPLACE
Definition DNA_ID.h:188
@ LIBOVERRIDE_OP_INSERT_BEFORE
Definition DNA_ID.h:202
@ TSE_LIBRARY_OVERRIDE_BASE
@ TSE_LIBRARY_OVERRIDE_OPERATION
PropertyType RNA_property_type(PropertyRNA *prop)
StructRNA * RNA_property_pointer_type(PointerRNA *ptr, PropertyRNA *prop)
@ PROP_POINTER
Definition RNA_types.hh:215
@ PROP_COLLECTION
Definition RNA_types.hh:216
ListBaseWrapperTemplate< ListBase, T > ListBaseWrapper
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
BMesh const char void * data
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:339
bool RNA_path_resolve_elements(PointerRNA *ptr, const char *path, Vector< PropertyElemRNA > *r_elements)
Definition rna_path.cc:666
IDOverrideLibraryProperty * BKE_lib_override_library_property_find(IDOverrideLibrary *liboverride, const char *rna_path)
int RNA_struct_ui_icon(const StructRNA *type)
bool RNA_struct_is_ID(const StructRNA *type)
int BIFIconID
Definition ED_asset.hh:30
bool BKE_lib_override_rna_property_find(PointerRNA *idpoin, const IDOverrideLibraryProperty *library_prop, PointerRNA *r_override_poin, PropertyRNA **r_override_prop, int *r_index)
char * RNA_path_append(const char *path, const PointerRNA *, PropertyRNA *prop, int intkey, const char *strkey)
Definition rna_path.cc:680
bool RNA_property_collection_lookup_int(PointerRNA *ptr, PropertyRNA *prop, int key, PointerRNA *r_ptr)
@ ID_FLAG_LIB_OVERRIDE_RESYNC_LEFTOVER
Definition DNA_ID.h:791
int RNA_property_ui_icon(const PropertyRNA *prop)
bool BKE_lib_override_library_property_is_animated(const ID *id, const IDOverrideLibraryProperty *liboverride_prop, const PropertyRNA *override_rna_prop, const int rnaprop_index)
@ LIBOVERRIDE_OP_FLAG_IDPOINTER_MATCH_REFERENCE
Definition DNA_ID.h:226
const char * RNA_property_ui_name(const PropertyRNA *prop, const PointerRNA *ptr)
#define SUPPORT_FILTER_OUTLINER(space_outliner_)
ListBaseT< IDOverrideLibraryPropertyOperation > operations
Definition DNA_ID.h:326
eSpaceOutliner_Filter filter
Establish and manage Outliner trees for different display modes.