Blender V5.3
grease_pencil_attributes.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "BKE_grease_pencil.hh"
11
13
15
17
18static const auto &changed_tags()
19{
20 static Map<StringRef, AttrUpdateOnChange> attributes;
21 return attributes;
22}
23
24static const auto &builtin_attributes()
25{
26 static auto attributes = []() {
28 return map;
29 }();
30 return attributes;
31}
32
33static const auto &array_storage_required()
34{
35 static Set<StringRef> attributes{};
36 return attributes;
37}
38
39static int get_domain_size(const void *owner, const AttrDomain domain)
40{
41 const GreasePencil &grease_pencil = *static_cast<const GreasePencil *>(owner);
42 return domain == AttrDomain::Layer ? grease_pencil.layers().size() : 0;
43}
44
46{
48 fn.domain_supported = [](const void * /*owner*/, const AttrDomain domain) {
49 return domain == AttrDomain::Layer;
50 };
51 fn.domain_size = get_domain_size;
52 fn.builtin_domain_and_type = [](const void * /*owner*/, const StringRef /*name*/)
53 -> std::optional<AttributeDomainAndType> { return std::nullopt; };
54 fn.lookup = [](const void *owner, const StringRef name) -> GAttributeReader {
55 const GreasePencil &grease_pencil = *static_cast<const GreasePencil *>(owner);
56 const AttributeStorage &storage = grease_pencil.attribute_storage.wrap();
57 const Attribute *attribute = storage.lookup(name);
58 if (!attribute) {
59 return {};
60 }
61 const int domain_size = get_domain_size(owner, AttrDomain::Layer);
62 return attribute_to_reader(*attribute, AttrDomain::Layer, domain_size);
63 };
64 fn.get_builtin_default = [](const void * /*owner*/, StringRef name) -> GPointer {
65 const AttrBuiltinInfo &info = builtin_attributes().lookup(name);
66 return info.default_value;
67 };
68 fn.lookup_meta_data = [](const void *owner, StringRef name) -> std::optional<AttributeMetaData> {
69 const GreasePencil &grease_pencil = *static_cast<const GreasePencil *>(owner);
70 const AttributeStorage &storage = grease_pencil.attribute_storage.wrap();
71 const Attribute *attr = storage.lookup(name);
72 if (!attr) {
73 return std::nullopt;
74 }
75 return AttributeMetaData{attr->domain(), attr->data_type()};
76 };
77 fn.adapt_domain = [](const void * /*owner*/,
78 const GVArray &varray,
79 const AttrDomain from_domain,
80 const AttrDomain to_domain) {
81 if (from_domain == to_domain && from_domain == AttrDomain::Layer) {
82 return varray;
83 }
84 return GVArray{};
85 };
86 fn.foreach_attribute = [](const void *owner,
87 const FunctionRef<void(const AttributeIter &)> fn,
88 const AttributeAccessor &accessor) {
89 const GreasePencil &grease_pencil = *static_cast<const GreasePencil *>(owner);
90 const AttributeStorage &storage = grease_pencil.attribute_storage.wrap();
91 for (const Attribute &attribute : storage) {
92 const auto get_fn = [&]() {
93 const int domain_size = get_domain_size(owner, AttrDomain::Layer);
95 };
96 AttributeIter iter(attribute.name(), attribute.domain(), attribute.data_type(), get_fn);
97 iter.is_builtin = builtin_attributes().contains(attribute.name());
98 iter.storage_type = attribute.storage_type();
99 iter.accessor = &accessor;
100 fn(iter);
101 if (iter.is_stopped()) {
102 break;
103 }
104 }
105 };
106 fn.lookup_validator = [](const void * /*owner*/, const StringRef name) -> AttributeValidator {
107 const AttrBuiltinInfo *info = builtin_attributes().lookup_ptr(name);
108 if (!info) {
109 return {};
110 }
111 return info->validator;
112 };
113 fn.lookup_for_write = [](void *owner, const StringRef name) -> GAttributeWriter {
114 GreasePencil &grease_pencil = *static_cast<GreasePencil *>(owner);
115 AttributeStorage &storage = grease_pencil.attribute_storage.wrap();
116 Attribute *attribute = storage.lookup(name);
117 if (!attribute) {
118 return {};
119 }
120 const int domain_size = get_domain_size(owner, AttrDomain::Layer);
121 return attribute_to_writer(&grease_pencil, {}, domain_size, *attribute);
122 };
123 fn.remove = [](void *owner, const StringRef name) -> bool {
124 GreasePencil &grease_pencil = *static_cast<GreasePencil *>(owner);
125 AttributeStorage &storage = grease_pencil.attribute_storage.wrap();
126 if (const AttrBuiltinInfo *info = builtin_attributes().lookup_ptr(name)) {
127 if (!info->deletable) {
128 return false;
129 }
130 }
131 const std::optional<AttrUpdateOnChange> fn = changed_tags().lookup_try(name);
132 const bool removed = storage.remove(name);
133 if (!removed) {
134 return false;
135 }
136 if (fn) {
137 (*fn)(owner);
138 }
139 return true;
140 };
141 fn.add = [](void *owner,
142 const StringRef name,
143 const AttrDomain domain,
144 const bke::AttrType type,
145 const AttributeInit &initializer) {
146 GreasePencil &grease_pencil = *static_cast<GreasePencil *>(owner);
147 const int domain_size = get_domain_size(owner, domain);
148 AttributeStorage &storage = grease_pencil.attribute_storage.wrap();
149 if (const AttrBuiltinInfo *info = builtin_attributes().lookup_ptr(name)) {
150 if (info->domain != domain || info->type != type) {
151 return false;
152 }
153 }
154 if (storage.lookup(name)) {
155 return false;
156 }
157 const bool array = array_storage_required().contains(name);
158 Attribute::DataVariant data = attribute_init_to_data(type, domain_size, initializer, array);
159 storage.add(name, domain, type, std::move(data));
160 if (initializer.type != AttributeInit::Type::Construct) {
161 if (const std::optional<AttrUpdateOnChange> fn = changed_tags().lookup_try(name)) {
162 (*fn)(owner);
163 }
164 }
165 return true;
166 };
167 fn.rename = [](void *owner,
168 const Map<StringRef, StringRef> &name_map,
169 bool overwrite) -> Set<StringRef> {
170 GreasePencil &grease_pencil = *static_cast<GreasePencil *>(owner);
171 return rename_attributes(
172 grease_pencil.attribute_storage.wrap(),
173 name_map,
174 overwrite,
177 [&](const bke::AttrDomain domain) { return get_domain_size(owner, domain); },
178 std::nullopt,
179 {});
180 };
181 fn.assign_data = [](void *owner, StringRef name, const AttributeInit &initializer) {
182 GreasePencil &grease_pencil = *static_cast<GreasePencil *>(owner);
183 AttributeStorage &storage = grease_pencil.attribute_storage.wrap();
184 Attribute *attr = storage.lookup(name);
185 if (!attr) {
186 return false;
187 }
189 get_domain_size(owner, attr->domain()),
190 initializer,
191 array_storage_required().contains(name));
192 attr->assign_data(std::move(data));
193 if (initializer.type != AttributeInit::Type::Construct) {
194 if (const std::optional<AttrUpdateOnChange> fn = changed_tags().lookup_try(name)) {
195 (*fn)(owner);
196 }
197 }
198 return true;
199 };
200
201 return fn;
202}
203
209
210} // namespace blender::bke::greasepencil
Low-level operations for grease pencil.
std::optional< AttrStorageType > storage_type
const AttributeAccessor * accessor
std::variant< ArrayData, SingleData > DataVariant
void assign_data(DataVariant &&data)
#define storage(slot, qualifiers)
#define attribute(slot)
static const auto & array_storage_required()
static int get_domain_size(const void *owner, const AttrDomain domain)
const AttributeAccessorFunctions & get_attribute_accessor_functions()
static AttributeAccessorFunctions get_grease_pencil_accessor_functions()
static const auto & builtin_attributes()
GAttributeReader attribute_to_reader(const Attribute &attribute, const AttrDomain domain, const int64_t domain_size)
Set< StringRef > rename_attributes(AttributeStorage &storage, const Map< StringRef, StringRef > &name_map, const bool overwrite, const Map< StringRef, AttrBuiltinInfo > &builtin_attributes, const Set< StringRef > &array_storage_required, const FunctionRef< int(AttrDomain)> domain_size_fn, std::optional< ListBaseT< bDeformGroup > * > vertex_groups, FunctionRef< MutableSpan< MDeformVert >()> get_mutable_dverts)
GAttributeWriter attribute_to_writer(void *owner, const Map< StringRef, AttrUpdateOnChange > &changed_tags, const int64_t domain_size, Attribute &attribute)
Attribute::DataVariant attribute_init_to_data(const bke::AttrType data_type, const int64_t domain_size, const AttributeInit &initializer, const bool require_array_data)
BMesh const char void * data