Blender V5.3
blenkernel/intern/attribute.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cstring>
12#include <optional>
13
15#include "MEM_guardedalloc.h"
16
17#include "DNA_ID.h"
18#include "DNA_curves_types.h"
20#include "DNA_mesh_types.h"
22
23#include "BLI_index_range.hh"
24#include "BLI_string.hh"
25#include "BLI_string_utils.hh"
26
27#include "BLT_translation.hh"
28
29#include "BKE_attribute.hh"
31#include "BKE_curves.hh"
32#include "BKE_customdata.hh"
33#include "BKE_deform.hh"
34#include "BKE_editmesh.hh"
35#include "BKE_grease_pencil.hh"
36#include "BKE_mesh.hh"
37#include "BKE_pointcloud.hh"
38#include "BKE_report.hh"
39
40namespace blender {
41
42using bke::AttrDomain;
43
45{
46 if (id == nullptr) {
47 return {};
48 }
49 switch (GS(id->name)) {
50 case ID_ME:
52 case ID_PT:
54 case ID_CV:
56 case ID_GP:
58 default:
59 return {};
60 }
61}
62
64{
65 return type_;
66}
67
69{
70 return ptr_ != nullptr;
71}
72
74{
75 BLI_assert(this->is_valid());
77 return reinterpret_cast<Mesh *>(ptr_);
78}
79
81{
82 BLI_assert(this->is_valid());
84 return reinterpret_cast<PointCloud *>(ptr_);
85}
86
88{
89 BLI_assert(this->is_valid());
91 return reinterpret_cast<Curves *>(ptr_);
92}
93
95{
96 BLI_assert(this->is_valid());
98 return reinterpret_cast<GreasePencil *>(ptr_);
99}
100
107
109{
110 switch (type_) {
112 return &this->get_mesh()->attribute_storage.wrap();
114 return &this->get_pointcloud()->attribute_storage.wrap();
116 return &this->get_curves()->geometry.attribute_storage.wrap();
118 return &this->get_grease_pencil()->attribute_storage.wrap();
121 }
122 BLI_assert(false);
123 return nullptr;
124}
125
126std::optional<bke::MutableAttributeAccessor> AttributeOwner::get_accessor() const
127{
128 switch (type_) {
130 /* The attribute API isn't implemented for BMesh, so edit mode meshes are not supported. */
131 BLI_assert(this->get_mesh()->runtime->edit_mesh == nullptr);
132 return this->get_mesh()->attributes_for_write();
134 return this->get_pointcloud()->attributes_for_write();
136 return this->get_curves()->geometry.wrap().attributes_for_write();
138 return this->get_grease_pencil()->attributes_for_write();
140 return this->get_grease_pencil_drawing()->wrap().strokes_for_write().attributes_for_write();
141 }
142 BLI_assert(false);
143 return std::nullopt;
144}
145
148 int length = 0;
149};
150
151static std::array<DomainInfo, ATTR_DOMAIN_NUM> get_domains(BMesh *bm)
152{
153 std::array<DomainInfo, ATTR_DOMAIN_NUM> info;
154 info[int(AttrDomain::Point)].customdata = &bm->vdata;
155 info[int(AttrDomain::Point)].length = bm->totvert;
156 info[int(AttrDomain::Edge)].customdata = &bm->edata;
157 info[int(AttrDomain::Edge)].length = bm->totedge;
158 info[int(AttrDomain::Corner)].customdata = &bm->ldata;
159 info[int(AttrDomain::Corner)].length = bm->totloop;
160 info[int(AttrDomain::Face)].customdata = &bm->pdata;
161 info[int(AttrDomain::Face)].length = bm->totface;
162 return info;
163}
164
166 const StringRef old_name,
167 const StringRef new_name,
168 ReportList *reports)
169{
170 const bke::AttributeStorage &storage = *owner.get_storage();
171 if (!storage.lookup(old_name)) {
172 return false;
173 }
174 return BKE_attribute_rename(owner, old_name, new_name, reports);
175}
176
178 BMesh &bm,
179 const StringRef old_name,
180 const StringRef new_name,
181 ReportList *reports)
182{
183 BMDataLayerLookup attr = BM_data_layer_lookup(bm, old_name);
184 if (!attr) {
185 return false;
186 }
187 return BKE_attribute_rename(owner, old_name, new_name, reports);
188}
189
191 const StringRef name,
192 const AttrDomain domain,
193 const bke::AttrType data_type,
194 ReportList *reports)
195{
196 if (const std::optional metadata = attributes.get_builtin_domain_and_type(name)) {
197 if (domain != metadata->domain) {
198 BKE_reportf(reports,
199 RPT_ERROR,
200 "Domain unsupported for \"%s\" attribute",
201 std::string(name).c_str());
202 return false;
203 }
204 if (data_type != metadata->data_type) {
206 reports, RPT_ERROR, "Type unsupported for \"%s\" attribute", std::string(name).c_str());
207 return false;
208 }
209 }
210 return true;
211}
212
213static bool mesh_attribute_valid(const Mesh &mesh,
214 const StringRef name,
215 const AttrDomain domain,
216 const bke::AttrType data_type,
217 ReportList *reports)
218{
219 if (mesh.runtime->edit_mesh) {
221 BKE_report(reports, RPT_ERROR, "Unable to create attribute in edit mode");
222 return false;
223 }
224 }
225 if (!name_valid_for_builtin_domain_and_type(mesh.attributes(), name, domain, data_type, reports))
226 {
227 return false;
228 }
229 return true;
230}
231
233 const StringRef old_name,
234 const StringRef new_name,
235 ReportList *reports)
236{
237 if (BKE_attribute_required(owner, old_name)) {
238 BLI_assert_msg(0, "Required attribute name is not editable");
239 return false;
240 }
241 if (new_name.is_empty()) {
242 BKE_report(reports, RPT_ERROR, "Attribute name cannot be empty");
243 return false;
244 }
245
246 if (owner.type() == AttributeOwnerType::Mesh) {
247 Mesh *mesh = owner.get_mesh();
249 /* NOTE: Checking if the new name matches the old name only makes sense when the name
250 * is clamped to its maximum length, otherwise assigning an over-long name multiple times
251 * will add `.001` suffix unnecessarily. */
252 {
253 const int new_name_maxncpy = CustomData_name_maxncpy_calc(new_name);
254 /* NOTE: A function that performs a clamped comparison without copying would be handy. */
255 char new_name_clamped[MAX_CUSTOMDATA_LAYER_NAME];
256 new_name.copy_utf8_truncated(new_name_clamped, new_name_maxncpy);
257 if (old_name == new_name_clamped) {
258 return false;
259 }
260 }
261
262 BMDataLayerLookup attr = BM_data_layer_lookup(*bm, old_name);
263 if (!attr) {
264 BKE_report(reports, RPT_ERROR, "Attribute is not part of this geometry");
265 return false;
266 }
267
268 if (!mesh_attribute_valid(*mesh, new_name, attr.domain, attr.type, reports)) {
269 return false;
270 }
271
272 std::string result_name = BKE_attribute_calc_unique_name(owner, new_name);
273
274 if (attr.type == bke::AttrType::Float2) {
275 /* Rename UV sub-attributes. */
276 char buffer_src[MAX_CUSTOMDATA_LAYER_NAME];
277 char buffer_dst[MAX_CUSTOMDATA_LAYER_NAME];
279 *bm,
280 BKE_uv_map_pin_name_get(old_name, buffer_src),
281 BKE_uv_map_pin_name_get(result_name, buffer_dst),
282 reports);
283 }
284
285 if (old_name == BKE_id_attributes_active_color_name(&mesh->id)) {
286 BKE_id_attributes_active_color_set(&mesh->id, result_name);
287 }
288 if (old_name == BKE_id_attributes_default_color_name(&mesh->id)) {
289 BKE_id_attributes_default_color_set(&mesh->id, result_name);
290 }
291
292 StringRef(result_name).copy_utf8_truncated(const_cast<CustomDataLayer *>(attr.layer)->name);
293
294 return true;
295 }
296 }
297
298 bke::AttributeStorage &attributes = *owner.get_storage();
299 bke::Attribute *attr = attributes.lookup(old_name);
300 if (!attr) {
301 BKE_report(reports, RPT_ERROR, "Attribute is not part of this geometry");
302 return false;
303 }
304
305 if (owner.type() == AttributeOwnerType::Curves) {
306 Curves *curves = owner.get_curves();
307 if (!name_valid_for_builtin_domain_and_type(curves->geometry.wrap().attributes(),
308 new_name,
309 attr->domain(),
310 attr->data_type(),
311 reports))
312 {
313 return false;
314 }
315 }
316 else if (owner.type() == AttributeOwnerType::Mesh) {
317 Mesh *mesh = owner.get_mesh();
318 if (!mesh_attribute_valid(*mesh, new_name, attr->domain(), attr->data_type(), reports)) {
319 return false;
320 }
321 if (attr->data_type() == bke::AttrType::Float2) {
322 /* Rename UV sub-attributes. */
323 char buffer_src[MAX_CUSTOMDATA_LAYER_NAME];
324 char buffer_dst[MAX_CUSTOMDATA_LAYER_NAME];
326 BKE_uv_map_pin_name_get(attr->name(), buffer_src),
327 BKE_uv_map_pin_name_get(new_name, buffer_dst),
328 reports);
329 }
330
331 if (old_name == BKE_id_attributes_active_color_name(&mesh->id)) {
333 }
334 if (old_name == BKE_id_attributes_default_color_name(&mesh->id)) {
336 }
337 if (old_name == mesh->active_uv_map_name()) {
338 mesh->uv_maps_active_set(new_name);
339 }
340 if (old_name == mesh->default_uv_map_name()) {
341 mesh->uv_maps_default_set(new_name);
342 }
343 }
344
345 attributes.rename(old_name, new_name);
346 return true;
347}
348
349std::string BKE_attribute_calc_unique_name(const AttributeOwner &owner, const StringRef name)
350{
351 const StringRef name_final = name.is_empty() ? DATA_("Attribute") : name;
352 if (owner.type() == AttributeOwnerType::Mesh) {
353 const Mesh &mesh = *owner.get_mesh();
354 /* While attribute names may collide with vertex group names,
355 * it's important not to allow this when requesting a unique name
356 * because #MutableAttributeAccessor::add will consider the layer as "existing",
357 * and not add the new attribute as expected. See: #160029. */
358 const auto is_used_vertex_group = [&](const StringRef check_name) {
359 return BKE_defgroup_name_index(&mesh.vertex_group_names, check_name) != -1;
360 };
361 if (const BMesh *bm = BKE_editmesh_bmesh_get(&mesh)) {
362 Set<StringRef, 8> names;
363 const auto add_names = [&](const CustomData &data) {
364 for (const CustomDataLayer &layer : Span(data.layers, data.totlayer)) {
366 names.add(layer.name);
367 }
368 }
369 };
370 add_names(bm->vdata);
371 add_names(bm->edata);
372 add_names(bm->pdata);
373 add_names(bm->ldata);
374 const int name_maxncpy = CustomData_name_maxncpy_calc(name_final);
375 char name_buffer[MAX_CUSTOMDATA_LAYER_NAME];
376 name_final.copy_utf8_truncated(name_buffer, name_maxncpy);
377 BLI_assert(!name_final.is_empty());
379 [&](const StringRefNull new_name) {
380 return names.contains(new_name) || BM_attribute_stored_in_bmesh_builtin(new_name) ||
381 is_used_vertex_group(new_name);
382 },
383 /* Unused, `name_final` is never empty. */
384 "",
385 '.',
386 name_buffer,
387 name_maxncpy);
388 return name_buffer;
389 }
390 const bke::AttributeStorage &storage = *owner.get_storage();
391 return BLI_uniquename_cb(
392 [&](const StringRef check_name) {
393 return storage.lookup(check_name) != nullptr || is_used_vertex_group(check_name);
394 },
395 '.',
396 name_final);
397 }
398
400 return storage.unique_name_calc(name_final);
401}
402
404 BMesh &bm,
405 const StringRef name,
406 const eCustomDataType type,
407 const AttrDomain domain,
408 ReportList *reports)
409{
410 using namespace blender::bke;
411 BLI_assert(mesh.runtime->edit_mesh->bm == &bm);
412 const std::array<DomainInfo, ATTR_DOMAIN_NUM> info = get_domains(&bm);
413
414 CustomData *customdata = info[int(domain)].customdata;
415 if (customdata == nullptr) {
416 BKE_report(reports, RPT_ERROR, "Attribute domain not supported by this geometry type");
417 return nullptr;
418 }
419
420 std::string uniquename = BKE_attribute_calc_unique_name(AttributeOwner::from_id(&mesh.id), name);
421
422 if (!mesh_attribute_valid(mesh, name, domain, *custom_data_type_to_attr_type(type), reports)) {
423 return nullptr;
424 }
425 BM_data_layer_add_named(&bm, customdata, type, uniquename.c_str());
426 const int index = CustomData_get_named_layer_index(customdata, type, uniquename);
427 return (index == -1) ? nullptr : &(customdata->layers[index]);
428}
429
430static int color_name_to_index(AttributeOwner &owner, const StringRef name)
431{
433}
434
436{
438 return min_ii(index, length - 1);
439}
440
446
447static int uv_name_to_index(AttributeOwner &owner, const StringRef name)
448{
450}
451
452static int uv_clamp_index(AttributeOwner &owner, int index)
453{
455 return min_ii(index, length - 1);
456}
457
463
464bool BKE_attribute_remove(AttributeOwner &owner, const StringRef name, ReportList *reports)
465{
466 using namespace blender::bke;
467 if (name.is_empty()) {
468 BKE_report(reports, RPT_ERROR, "The attribute name must not be empty");
469 return false;
470 }
471 if (BKE_attribute_required(owner, name)) {
472 BKE_reportf(reports,
473 RPT_ERROR,
474 "Attribute '%s' is required and cannot be removed",
475 std::string(name).c_str());
476 return false;
477 }
478
479 if (owner.type() == AttributeOwnerType::Mesh) {
480 Mesh *mesh = owner.get_mesh();
482 const std::array<DomainInfo, ATTR_DOMAIN_NUM> info = get_domains(bm);
483 for (const int domain : IndexRange(ATTR_DOMAIN_NUM)) {
484 if (CustomData *data = info[domain].customdata) {
485 const std::string name_copy = name;
486 const int layer_index = CustomData_get_named_layer_index_notype(data, name_copy);
487 if (layer_index == -1) {
488 continue;
489 }
490
491 const eCustomDataType type = eCustomDataType(data->layers[layer_index].type);
492 const bool is_active_uv_attribute = name_copy == mesh->active_uv_map_name();
493 const bool is_default_uv_attribute = name_copy == mesh->default_uv_map_name();
494 const bool is_active_color_attribute = name_copy.c_str() ==
495 StringRef(mesh->active_color_attribute);
496 const bool is_default_color_attribute = name_copy.c_str() ==
497 StringRef(mesh->default_color_attribute);
498 const int active_color_index = color_name_to_index(owner, mesh->active_color_attribute);
499 const int default_color_index = color_name_to_index(owner,
500 mesh->default_color_attribute);
501 const int active_uv_index = uv_name_to_index(owner, mesh->active_uv_map_name());
502 const int default_uv_index = uv_name_to_index(owner, mesh->default_uv_map_name());
503
504 if (!BM_data_layer_free_named(bm, data, name_copy.c_str())) {
506 }
507
508 if (is_active_color_attribute) {
510 &mesh->id,
511 color_name_from_index(owner, color_clamp_index(owner, active_color_index)));
512 }
513 if (is_default_color_attribute) {
515 &mesh->id,
516 color_name_from_index(owner, color_clamp_index(owner, default_color_index)));
517 }
518 if (is_active_uv_attribute) {
519 mesh->uv_maps_active_set(
520 uv_name_from_index(owner, uv_clamp_index(owner, active_uv_index)));
521 }
522 if (is_default_uv_attribute) {
523 mesh->uv_maps_default_set(
524 uv_name_from_index(owner, uv_clamp_index(owner, default_uv_index)));
525 }
526
527 if (type == CD_PROP_FLOAT2 && domain == int(AttrDomain::Corner)) {
528 char buffer[MAX_CUSTOMDATA_LAYER_NAME];
530 }
531 return true;
532 }
533 }
534 return false;
535 }
536 }
537
538 std::optional<MutableAttributeAccessor> attributes = owner.get_accessor();
539 if (!attributes) {
540 return false;
541 }
542
543 if (owner.type() == AttributeOwnerType::Mesh) {
544 const std::string name_copy = name;
545 std::optional<bke::AttributeMetaData> metadata = attributes->lookup_meta_data(name_copy);
546 if (!metadata) {
547 return false;
548 }
549 /* Update active and default color attributes. */
550 Mesh *mesh = owner.get_mesh();
551 const bool is_active_color_attribute = name_copy == StringRef(mesh->active_color_attribute);
552 const bool is_default_color_attribute = name_copy == StringRef(mesh->default_color_attribute);
553 const bool is_active_uv_attribute = name_copy == mesh->active_uv_map_name();
554 const bool is_default_uv_attribute = name_copy == mesh->default_uv_map_name();
555 const int active_color_index = color_name_to_index(owner, mesh->active_color_attribute);
556 const int default_color_index = color_name_to_index(owner, mesh->default_color_attribute);
557 const int active_uv_index = uv_name_to_index(owner, mesh->active_uv_map_name());
558 const int default_uv_index = uv_name_to_index(owner, mesh->default_uv_map_name());
559
560 if (!attributes->remove(name_copy)) {
562 }
563
564 if (is_active_color_attribute) {
566 &mesh->id, color_name_from_index(owner, color_clamp_index(owner, active_color_index)));
567 }
568 if (is_default_color_attribute) {
570 &mesh->id, color_name_from_index(owner, color_clamp_index(owner, default_color_index)));
571 }
572 if (is_active_uv_attribute) {
573 mesh->uv_maps_active_set(uv_name_from_index(owner, uv_clamp_index(owner, active_uv_index)));
574 }
575 if (is_default_uv_attribute) {
576 mesh->uv_maps_default_set(
577 uv_name_from_index(owner, uv_clamp_index(owner, default_uv_index)));
578 }
579
581 char buffer[MAX_CUSTOMDATA_LAYER_NAME];
582 attributes->remove(BKE_uv_map_pin_name_get(name_copy, buffer));
583 }
584 return true;
585 }
586
587 return attributes->remove(name);
588}
589
591 const AttrDomainMask domain_mask,
592 const eCustomDataMask mask,
593 const bool include_anonymous)
594{
595 if (owner.type() == AttributeOwnerType::Mesh) {
596 const Mesh &mesh = *owner.get_mesh();
597 if (const BMesh *bm = BKE_editmesh_bmesh_get(&mesh)) {
598 const std::array<DomainInfo, ATTR_DOMAIN_NUM> info = get_domains(const_cast<BMesh *>(bm));
599 int length = 0;
600 for (const int domain : IndexRange(ATTR_DOMAIN_NUM)) {
601 const CustomData *customdata = info[domain].customdata;
602 if (customdata == nullptr) {
603 continue;
604 }
605 if (!customdata || !((1 << int(domain)) & domain_mask)) {
606 continue;
607 }
608 for (const CustomDataLayer &layer : Span(customdata->layers, customdata->totlayer)) {
609 if (!(mask & CD_TYPE_AS_MASK(eCustomDataType(layer.type)))) {
610 continue;
611 }
612 if (!include_anonymous && bke::attribute_name_is_anonymous(layer.name)) {
613 continue;
614 }
615 length++;
616 }
617 }
618 return length;
619 }
620 }
621 const bke::AttributeStorage &storage = *owner.get_storage();
622 if (include_anonymous && domain_mask == ATTR_DOMAIN_MASK_ALL && mask == CD_MASK_PROP_ALL) {
623 return storage.count();
624 }
625 return std::count_if(storage.begin(), storage.end(), [&](const bke::Attribute &attr) {
626 if (!(ATTR_DOMAIN_AS_MASK(attr.domain()) & domain_mask)) {
627 return false;
628 }
630 return false;
631 }
632 if (!include_anonymous && bke::attribute_name_is_anonymous(attr.name())) {
633 return false;
634 }
635 return true;
636 });
637}
638
640{
641 BLI_assert(mesh.runtime->edit_mesh->bm == &bm);
643 const std::array<DomainInfo, ATTR_DOMAIN_NUM> info = get_domains(&const_cast<BMesh &>(bm));
644
645 for (const int domain : IndexRange(ATTR_DOMAIN_NUM)) {
646 const CustomData *customdata = info[domain].customdata;
647 if (customdata == nullptr) {
648 continue;
649 }
650 if (Span(customdata->layers, customdata->totlayer).contains_ptr(layer)) {
651 return AttrDomain(domain);
652 }
653 }
654
655 BLI_assert_msg(0, "Custom data layer not found in geometry");
657}
658
659int BKE_attribute_domain_size(const AttributeOwner &owner, const int domain)
660{
661 if (owner.type() == AttributeOwnerType::Mesh) {
662 const Mesh &mesh = *owner.get_mesh();
663 if (const BMesh *bm = BKE_editmesh_bmesh_get(&mesh)) {
664 const std::array<DomainInfo, ATTR_DOMAIN_NUM> info = get_domains(const_cast<BMesh *>(bm));
665 return info[domain].length;
666 }
667 }
668 const bke::AttributeAccessor attributes = *owner.get_accessor();
669 return attributes.domain_size(bke::AttrDomain(domain));
670}
671
672bool BKE_attribute_required(const AttributeOwner &owner, const StringRef name)
673{
674 switch (owner.type()) {
678 return BKE_curves_attribute_required(owner.get_curves(), name);
680 return BKE_mesh_attribute_required(name);
682 return false;
685 }
686 return false;
687}
688
689std::optional<StringRefNull> BKE_attributes_active_name_get(AttributeOwner &owner)
690{
691 using namespace blender::bke;
692 int active_index = *BKE_attributes_active_index_p(owner);
693 if (active_index == -1) {
694 return std::nullopt;
695 }
696 if (owner.type() == AttributeOwnerType::Mesh) {
697 const Mesh *mesh = owner.get_mesh();
698 if (const BMesh *bm = BKE_editmesh_bmesh_get(mesh)) {
699 if (active_index > BKE_attributes_length(owner, ATTR_DOMAIN_MASK_ALL, CD_MASK_PROP_ALL)) {
700 active_index = 0;
701 }
702 const std::array<DomainInfo, ATTR_DOMAIN_NUM> info = get_domains(const_cast<BMesh *>(bm));
703 int index = 0;
704 for (const int domain : IndexRange(ATTR_DOMAIN_NUM)) {
705 CustomData *customdata = info[domain].customdata;
706 if (customdata == nullptr) {
707 continue;
708 }
709 for (int i = 0; i < customdata->totlayer; i++) {
710 CustomDataLayer *layer = &customdata->layers[i];
712 if (index == active_index) {
714 return layer->name;
715 }
716 return std::nullopt;
717 }
718 index++;
719 }
720 }
721 }
722 return std::nullopt;
723 }
724 }
725
727 if (!IndexRange(storage.count()).contains(active_index)) {
728 return std::nullopt;
729 }
730 return storage.at_index(active_index).name();
731}
732
734{
736 if (owner.type() == AttributeOwnerType::Mesh) {
737 const Mesh *mesh = owner.get_mesh();
738 if (mesh->runtime->edit_mesh) {
739 const int index = BKE_attribute_to_index(
742 return;
743 }
744 }
745
746 bke::AttributeStorage &attributes = *owner.get_storage();
747 *BKE_attributes_active_index_p(owner) = attributes.index_of(name);
748}
749
754
756{
757 switch (owner.type()) {
760 }
762 return &owner.get_mesh()->attributes_active_index;
763 }
765 return &owner.get_curves()->geometry.attributes_active_index;
766 }
768 return &owner.get_grease_pencil()->attributes_active_index;
769 }
772 }
773 }
774 return nullptr;
775}
776
778{
779 int *active_index = BKE_attributes_active_index_p(owner);
780 if (*active_index < 0) {
781 /* Mark none as active. */
782 *active_index = -1;
783 return;
784 }
785
786 const int attributes_num = BKE_attributes_length(owner, ATTR_DOMAIN_MASK_ALL, CD_MASK_PROP_ALL);
787 if (attributes_num == 0) {
788 /* Mark none as active. */
789 *active_index = -1;
790 return;
791 }
792
793 /* First try downwards. */
794 int index_check = *active_index;
795 while (index_check >= 0) {
796 if (index_check < attributes_num) {
797 std::optional<StringRef> attribute_name_check = BKE_attribute_from_index(
798 owner, index_check, ATTR_DOMAIN_MASK_ALL, CD_MASK_PROP_ALL);
799 if (attribute_name_check.has_value() &&
800 bke::allow_procedural_attribute_access(attribute_name_check.value()))
801 {
802 *active_index = index_check;
803 return;
804 }
805 }
806 index_check--;
807 }
808
809 /* Still not found? Try upwards. */
810 index_check = *active_index + 1;
811 while (index_check < attributes_num) {
812 std::optional<StringRef> attribute_name_check = BKE_attribute_from_index(
813 owner, index_check, ATTR_DOMAIN_MASK_ALL, CD_MASK_PROP_ALL);
814 if (attribute_name_check.has_value() &&
815 bke::allow_procedural_attribute_access(attribute_name_check.value()))
816 {
817 *active_index = index_check;
818 return;
819 }
820 index_check++;
821 }
822
823 /* Still not found? Mark none as active. */
824 *active_index = -1;
825}
826
827std::optional<StringRef> BKE_attribute_from_index(AttributeOwner &owner,
828 const int lookup_index,
829 const AttrDomainMask domain_mask,
830 const eCustomDataMask layer_mask,
831 const bool include_anonymous)
832{
833 if (owner.type() == AttributeOwnerType::Mesh) {
834 const Mesh &mesh = *owner.get_mesh();
835 if (const BMesh *bm = BKE_editmesh_bmesh_get(&mesh)) {
836 const std::array<DomainInfo, ATTR_DOMAIN_NUM> info = get_domains(const_cast<BMesh *>(bm));
837
838 int index = 0;
839 for (const int domain : IndexRange(ATTR_DOMAIN_NUM)) {
840 CustomData *customdata = info[domain].customdata;
841 if (!customdata || !((1 << int(domain)) & domain_mask)) {
842 continue;
843 }
844 for (const CustomDataLayer &layer : Span(customdata->layers, customdata->totlayer)) {
845 if (!(layer_mask & CD_TYPE_AS_MASK(eCustomDataType(layer.type)))) {
846 continue;
847 }
848 if (!include_anonymous && bke::attribute_name_is_anonymous(layer.name)) {
849 continue;
850 }
851 if (index == lookup_index) {
852 return layer.name;
853 }
854 index++;
855 }
856 }
857 return std::nullopt;
858 }
859 }
860
862 if (domain_mask == ATTR_DOMAIN_MASK_ALL && layer_mask == CD_MASK_PROP_ALL) {
863 return storage.at_index(lookup_index).name();
864 }
865 int index = 0;
866 for (const bke::Attribute &attr : storage) {
867 if (!(ATTR_DOMAIN_AS_MASK(attr.domain()) & domain_mask)) {
868 continue;
869 }
870 if (!(CD_TYPE_AS_MASK(*bke::attr_type_to_custom_data_type(attr.data_type())) & layer_mask)) {
871 continue;
872 }
873 if (!include_anonymous && bke::attribute_name_is_anonymous(attr.name())) {
874 continue;
875 }
876 if (index == lookup_index) {
877 return attr.name();
878 }
879 index++;
880 }
881 return std::nullopt;
882}
883
885 const StringRef name,
886 AttrDomainMask domain_mask,
887 eCustomDataMask layer_mask,
888 const bool include_anonymous)
889{
890 if (owner.type() == AttributeOwnerType::Mesh) {
891 const Mesh &mesh = *owner.get_mesh();
892 if (const BMesh *bm = BKE_editmesh_bmesh_get(&mesh)) {
893 const std::array<DomainInfo, ATTR_DOMAIN_NUM> info = get_domains(const_cast<BMesh *>(bm));
894 int index = 0;
895 for (const int domain : IndexRange(ATTR_DOMAIN_NUM)) {
896 const CustomData *customdata = info[domain].customdata;
897 if (!customdata || !((1 << int(domain)) & domain_mask)) {
898 continue;
899 }
900 for (const CustomDataLayer &layer : Span(customdata->layers, customdata->totlayer)) {
901 if (!(layer_mask & CD_TYPE_AS_MASK(eCustomDataType(layer.type)))) {
902 continue;
903 }
904 if (!include_anonymous && bke::attribute_name_is_anonymous(layer.name)) {
905 continue;
906 }
907 if (layer.name == name) {
908 return index;
909 }
910 index++;
911 }
912 }
913 return -1;
914 }
915 }
916
917 const bke::AttributeStorage &storage = *owner.get_storage();
918 if (domain_mask == ATTR_DOMAIN_MASK_ALL && layer_mask == CD_MASK_PROP_ALL) {
919 return storage.index_of(name);
920 }
921 int index = 0;
922 for (const bke::Attribute &attr : storage) {
923 if (!(ATTR_DOMAIN_AS_MASK(attr.domain()) & domain_mask)) {
924 continue;
925 }
926 if (!(CD_TYPE_AS_MASK(*bke::attr_type_to_custom_data_type(attr.data_type())) & layer_mask)) {
927 continue;
928 }
929 if (!include_anonymous && bke::attribute_name_is_anonymous(attr.name())) {
930 continue;
931 }
932 if (attr.name() == name) {
933 return index;
934 }
935 index++;
936 }
937 return -1;
938}
939
940std::optional<StringRef> BKE_id_attributes_active_color_name(const ID *id)
941{
942 if (GS(id->name) == ID_ME) {
943 return reinterpret_cast<const Mesh *>(id)->active_color_attribute;
944 }
945 return std::nullopt;
946}
947
948std::optional<StringRef> BKE_id_attributes_default_color_name(const ID *id)
949{
950 if (GS(id->name) == ID_ME) {
951 return reinterpret_cast<const Mesh *>(id)->default_color_attribute;
952 }
953 return std::nullopt;
954}
955
956void BKE_id_attributes_active_color_set(ID *id, const std::optional<StringRef> name)
957{
958 switch (GS(id->name)) {
959 case ID_ME: {
960 Mesh *mesh = reinterpret_cast<Mesh *>(id);
961 MEM_SAFE_DELETE(mesh->active_color_attribute);
962 if (name) {
963 mesh->active_color_attribute = BLI_strdupn(name->data(), name->size());
964 }
965 break;
966 }
967 default:
968 break;
969 }
970}
971
973{
974 switch (GS(id->name)) {
975 case ID_ME: {
976 Mesh *mesh = reinterpret_cast<Mesh *>(id);
977 MEM_SAFE_DELETE(mesh->active_color_attribute);
978 break;
979 }
980 default:
981 break;
982 }
983}
984
985void BKE_id_attributes_default_color_set(ID *id, const std::optional<StringRef> name)
986{
987 switch (GS(id->name)) {
988 case ID_ME: {
989 Mesh *mesh = reinterpret_cast<Mesh *>(id);
990 MEM_SAFE_DELETE(mesh->default_color_attribute);
991 if (name) {
992 mesh->default_color_attribute = BLI_strdupn(name->data(), name->size());
993 }
994 break;
995 }
996 default:
997 break;
998 }
999}
1000
1001bool BKE_id_attributes_color_find(const ID *id, const StringRef name)
1002{
1003 AttributeOwner owner = AttributeOwner::from_id(const_cast<ID *>(id));
1005}
1006
1007StringRef BKE_uv_map_pin_name_get(const StringRef uv_map_name, char *buffer)
1008{
1009 const StringRef prefix = "." UV_PINNED_NAME ".";
1010 prefix.copy_unsafe(buffer);
1011 uv_map_name.copy_utf8_truncated(buffer + prefix.size(),
1012 MAX_CUSTOMDATA_LAYER_NAME - prefix.size());
1013 return buffer;
1014}
1015
1016} // namespace blender
#define ATTR_DOMAIN_MASK_COLOR
#define ATTR_DOMAIN_AS_MASK(domain)
#define ATTR_DOMAIN_NUM
Low-level operations for curves.
CustomData interface, see also DNA_customdata_types.h.
#define UV_PINNED_NAME
support for deformation groups and hooks.
Low-level operations for grease pencil.
General operations for point clouds.
#define DATA_(msgid)
ID and Library types, which are fundamental for SDNA.
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert_msg(a, msg)
#define MAX_CUSTOMDATA_LAYER_NAME
#define CD_MASK_PROP_ALL
#define CD_MASK_PROP_FLOAT2
#define CD_MASK_COLOR_ALL
constexpr Span()=default
GreasePencilDrawing * get_grease_pencil_drawing() const
GreasePencil * get_grease_pencil() const
bke::AttributeStorage * get_storage() const
std::optional< bke::MutableAttributeAccessor > get_accessor() const
static AttributeOwner from_id(ID *id)
AttributeOwnerType type() const
constexpr IndexRange()=default
constexpr bool contains(int64_t value) const
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr Span()=default
constexpr StringRef()
std::optional< AttributeDomainAndType > get_builtin_domain_and_type(const StringRef name) const
int domain_size(const AttrDomain domain) const
void rename(StringRef old_name, std::string new_name)
Attribute * lookup(StringRef name)
int index_of(StringRef name) const
constexpr bool is_empty() const
constexpr int64_t size() const
void copy_unsafe(char *dst) const
void copy_utf8_truncated(char *dst, int64_t dst_size) const
Definition string_ref.cc:28
#define storage(slot, qualifiers)
#define layer
float length(VecOp< float, D >) RET
bool is_uv_map(const AttributeMetaData &meta_data)
std::optional< eCustomDataType > attr_type_to_custom_data_type(AttrType attr_type)
bool attribute_name_is_anonymous(const StringRef name)
bool allow_procedural_attribute_access(StringRef attribute_name)
std::optional< AttrType > custom_data_type_to_attr_type(eCustomDataType data_type)
BMesh Iterators.
std::optional< StringRefNull > BKE_attributes_active_name_get(AttributeOwner &owner)
bool BM_data_layer_free_named(BMesh *bm, CustomData *data, StringRef name)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int * BKE_attributes_active_index_p(AttributeOwner &owner)
bool BKE_attribute_remove(AttributeOwner &owner, StringRef name, struct ReportList *reports)
void BKE_attributes_active_index_validate(AttributeOwner &owner)
void BKE_id_attributes_default_color_set(struct ID *id, std::optional< StringRef > name)
void BKE_id_attributes_active_color_clear(struct ID *id)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
std::optional< StringRef > BKE_id_attributes_default_color_name(const struct ID *id)
std::optional< StringRef > BKE_id_attributes_active_color_name(const struct ID *id)
bool BKE_grease_pencil_drawing_attribute_required(const GreasePencilDrawing *, StringRef name)
static bool name_valid_for_builtin_domain_and_type(const bke::AttributeAccessor attributes, const StringRef name, const AttrDomain domain, const bke::AttrType data_type, ReportList *reports)
static StringRef color_name_from_index(AttributeOwner &owner, int index)
int BKE_attributes_length(const AttributeOwner &owner, AttrDomainMask domain_mask, eCustomDataMask mask, bool include_anonymous=true)
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BKE_attributes_active_set(AttributeOwner &owner, StringRef name)
bool BKE_mesh_attribute_required(StringRef name)
std::string BKE_attribute_calc_unique_name(const AttributeOwner &owner, StringRef name)
int BKE_defgroup_name_index(const ListBaseT< bDeformGroup > *defbase, StringRef name)
Definition deform.cc:544
bool BKE_attribute_rename(AttributeOwner &owner, StringRef old_name, StringRef new_name, struct ReportList *reports)
uint64_t eCustomDataMask
size_t void BLI_uniquename_cb(FunctionRef< bool(StringRefNull)> unique_check, const char *defname, char delim, char *name, size_t name_maxncpy) ATTR_NONNULL(2
static int color_name_to_index(AttributeOwner &owner, const StringRef name)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
static int color_clamp_index(AttributeOwner &owner, int index)
BMesh const char void * data
static int uv_name_to_index(AttributeOwner &owner, const StringRef name)
BMDataLayerLookup BM_data_layer_lookup(const BMesh &bm, const StringRef name)
eCustomDataMask CD_TYPE_AS_MASK(eCustomDataType type)
bool BKE_curves_attribute_required(const struct Curves *curves, StringRef name)
std::optional< StringRef > BKE_attribute_from_index(AttributeOwner &owner, int lookup_index, AttrDomainMask domain_mask, eCustomDataMask layer_mask, bool include_anonymous=true)
bool BKE_attribute_required(const AttributeOwner &owner, StringRef name)
int CustomData_get_named_layer_index(const CustomData *data, eCustomDataType type, StringRef name)
static StringRef uv_name_from_index(AttributeOwner &owner, int index)
bke::AttrDomain BKE_attribute_domain(const Mesh &mesh, const BMesh &bm, const struct CustomDataLayer *layer)
int BKE_attribute_domain_size(const AttributeOwner &owner, int domain)
int CustomData_name_maxncpy_calc(StringRef name)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
@ ATTR_DOMAIN_MASK_CORNER
@ ATTR_DOMAIN_MASK_ALL
int BKE_attribute_to_index(const AttributeOwner &owner, const StringRef name, AttrDomainMask domain_mask, eCustomDataMask layer_mask, bool include_anonymous=true)
void BKE_id_attributes_active_color_set(struct ID *id, std::optional< StringRef > name)
static int uv_clamp_index(AttributeOwner &owner, int index)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
void BM_data_layer_add_named(BMesh *bm, CustomData *data, int type, const StringRef name)
static bool bke_attribute_rename_if_exists(AttributeOwner &owner, const StringRef old_name, const StringRef new_name, ReportList *reports)
void BKE_attributes_active_clear(AttributeOwner &owner)
int CustomData_get_named_layer_index_notype(const CustomData *data, StringRef name)
StringRef BKE_uv_map_pin_name_get(StringRef uv_map_name, char *buffer)
MINLINE int min_ii(int a, int b)
static std::array< DomainInfo, ATTR_DOMAIN_NUM > get_domains(BMesh *bm)
bool BM_attribute_stored_in_bmesh_builtin(const StringRef name)
static bool mesh_attribute_valid(const Mesh &mesh, const StringRef name, const AttrDomain domain, const bke::AttrType data_type, ReportList *reports)
struct CustomDataLayer * BKE_attribute_new(Mesh &mesh, BMesh &bm, StringRef name, eCustomDataType type, bke::AttrDomain domain, struct ReportList *reports)
bool BKE_id_attributes_color_find(const struct ID *id, StringRef name)
bool BKE_pointcloud_attribute_required(const PointCloud *pointcloud, StringRef name)
const CustomDataLayer * layer
struct AttributeStorage attribute_storage
struct AttributeStorage attribute_storage
struct AttributeStorage attribute_storage