Blender V5.3
attribute_access.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 <utility>
10
13#include "BKE_attribute_math.hh"
14#include "BKE_curves.hh"
15#include "BKE_customdata.hh"
16#include "BKE_geometry_set.hh"
18
19#include "DNA_ID.h"
21#include "DNA_mesh_types.h"
22#include "DNA_meshdata_types.h"
24
25#include "BLI_array_utils.hh"
26#include "BLI_color_types.hh"
27#include "BLI_generic_array.hh"
30#include "BLI_span.hh"
31
32#include "PRF_profile.hh"
33
34#include "BLT_translation.hh"
35
36#include "FN_field.hh"
37
38#ifndef NDEBUG
39# include <iostream>
40#endif
41
42namespace blender::bke {
43
79
81{
82 if (type.is<float>()) {
83 return AttrType::Float;
84 }
85 if (type.is<float2>()) {
86 return AttrType::Float2;
87 }
88 if (type.is<float3>()) {
89 return AttrType::Float3;
90 }
91 if (type.is<float4>()) {
92 return AttrType::Float4;
93 }
94 if (type.is<int>()) {
95 return AttrType::Int32;
96 }
97 if (type.is<int2>()) {
98 return AttrType::Int32_2D;
99 }
100 if (type.is<ColorGeometry4f>()) {
102 }
103 if (type.is<bool>()) {
104 return AttrType::Bool;
105 }
106 if (type.is<int8_t>()) {
107 return AttrType::Int8;
108 }
109 if (type.is<ColorGeometry4b>()) {
110 return AttrType::ColorByte;
111 }
112 if (type.is<math::Quaternion>()) {
114 }
115 if (type.is<float4x4>()) {
116 return AttrType::Float4x4;
117 }
118 if (type.is<short2>()) {
119 return AttrType::Int16_2D;
120 }
121 if (type.is<MStringProperty>()) {
122 return AttrType::String;
123 }
125 return AttrType::Bool;
126}
127
129{
130 switch (type) {
131 case CD_PROP_FLOAT:
132 return &CPPType::get<float>();
133 case CD_PROP_FLOAT2:
134 return &CPPType::get<float2>();
135 case CD_PROP_FLOAT3:
136 return &CPPType::get<float3>();
137 case CD_PROP_FLOAT4:
138 return &CPPType::get<float4>();
139 case CD_PROP_INT32:
140 return &CPPType::get<int>();
141 case CD_PROP_INT32_2D:
142 return &CPPType::get<int2>();
143 case CD_PROP_COLOR:
145 case CD_PROP_BOOL:
146 return &CPPType::get<bool>();
147 case CD_PROP_INT8:
148 return &CPPType::get<int8_t>();
153 case CD_PROP_FLOAT4X4:
154 return &CPPType::get<float4x4>();
155 case CD_PROP_INT16_2D:
156 return &CPPType::get<short2>();
157 case CD_PROP_STRING:
159 default:
160 return nullptr;
161 }
162}
163
164std::optional<eCustomDataType> cpp_type_to_custom_data_type(const CPPType &type)
165{
166 if (type.is<float>()) {
167 return CD_PROP_FLOAT;
168 }
169 if (type.is<float2>()) {
170 return CD_PROP_FLOAT2;
171 }
172 if (type.is<float3>()) {
173 return CD_PROP_FLOAT3;
174 }
175 if (type.is<float4>()) {
176 return CD_PROP_FLOAT4;
177 }
178 if (type.is<int>()) {
179 return CD_PROP_INT32;
180 }
181 if (type.is<int2>()) {
182 return CD_PROP_INT32_2D;
183 }
184 if (type.is<ColorGeometry4f>()) {
185 return CD_PROP_COLOR;
186 }
187 if (type.is<bool>()) {
188 return CD_PROP_BOOL;
189 }
190 if (type.is<int8_t>()) {
191 return CD_PROP_INT8;
192 }
193 if (type.is<ColorGeometry4b>()) {
194 return CD_PROP_BYTE_COLOR;
195 }
196 if (type.is<math::Quaternion>()) {
197 return CD_PROP_QUATERNION;
198 }
199 if (type.is<float4x4>()) {
200 return CD_PROP_FLOAT4X4;
201 }
202 if (type.is<short2>()) {
203 return CD_PROP_INT16_2D;
204 }
205 if (type.is<MStringProperty>()) {
206 return CD_PROP_STRING;
207 }
208 return std::nullopt;
209}
210
212 "This attribute cannot be accessed in a procedural context");
213
215{
216 if (attribute_name.startswith(".corner")) {
217 return false;
218 }
219 if (attribute_name.startswith(".edge")) {
220 return false;
221 }
222 if (attribute_name.startswith(".select")) {
223 return false;
224 }
225 if (attribute_name.startswith(".sculpt")) {
226 return false;
227 }
228 if (attribute_name.startswith(".hide")) {
229 return false;
230 }
231 if (attribute_name.startswith(".uv")) {
232 return false;
233 }
234 if (attribute_name == ".reference_index") {
235 return false;
236 }
237 if (attribute_name.startswith("." UV_PINNED_NAME ".")) {
238 return false;
239 }
240 return true;
241}
242
243static int attribute_data_type_complexity(const AttrType data_type)
244{
245 switch (data_type) {
246 case AttrType::Bool:
247 return 0;
248 case AttrType::Int8:
249 return 1;
250 case AttrType::Int32:
251 return 2;
252 case AttrType::Float:
253 return 3;
255 return 4;
257 return 5;
258 case AttrType::Float2:
259 return 6;
260 case AttrType::Float3:
261 return 7;
262 case AttrType::Float4:
263 return 8;
265 return 9;
267 return 10;
269 return 11;
271 return 12;
272#if 0 /* These attribute types are not supported yet. */
273 case AttrType::String:
274 return 13;
275#endif
276 default:
277 /* Only accept "generic" custom data types used by the attribute system. */
279 return 0;
280 }
281}
282
284{
285 int highest_complexity = INT_MIN;
286 AttrType most_complex_type = AttrType::ColorFloat;
287
288 for (const AttrType data_type : data_types) {
289 const int complexity = attribute_data_type_complexity(data_type);
290 if (complexity > highest_complexity) {
291 highest_complexity = complexity;
292 most_complex_type = data_type;
293 }
294 }
295
296 return most_complex_type;
297}
298
303static int attribute_domain_priority(const AttrDomain domain)
304{
305 switch (domain) {
307 return 0;
309 return 1;
311 return 2;
312 case AttrDomain::Face:
313 return 3;
314 case AttrDomain::Edge:
315 return 4;
317 return 5;
319 return 6;
320 default:
321 /* Domain not supported in nodes yet. */
323 return 0;
324 }
325}
326
328{
329 int highest_priority = INT_MIN;
330 AttrDomain highest_priority_domain = AttrDomain::Corner;
331
332 for (const AttrDomain domain : domains) {
333 const int priority = attribute_domain_priority(domain);
334 if (priority > highest_priority) {
335 highest_priority = priority;
336 highest_priority_domain = domain;
337 }
338 }
339
340 return highest_priority_domain;
341}
342
343/* -------------------------------------------------------------------- */
346
348{
350 return conversions.try_convert(std::move(varray), to_type);
351}
352
353std::optional<AttributeAccessor> AttributeAccessor::from_id(const ID &id)
354{
355 switch (GS(id.name)) {
356 case ID_ME:
357 return reinterpret_cast<const Mesh &>(id).attributes();
358 case ID_PT:
359 return reinterpret_cast<const PointCloud &>(id).attributes();
360 case ID_CV:
361 return reinterpret_cast<const Curves &>(id).geometry.wrap().attributes();
362 case ID_GP:
363 return reinterpret_cast<const GreasePencil &>(id).attributes();
364 default:
365 return {};
366 }
367 return {};
368}
369
371 const std::optional<AttrDomain> domain,
372 const std::optional<AttrType> data_type,
373 const AttributeAccessor &accessor)
374{
375 if (!attribute) {
376 return {};
377 }
378 if (domain.has_value()) {
379 if (attribute.domain != domain) {
380 attribute.varray = accessor.adapt_domain(attribute.varray, attribute.domain, *domain);
381 attribute.domain = *domain;
382 attribute.sharing_info = nullptr;
383 if (!attribute.varray) {
384 return {};
385 }
386 }
387 }
388 if (data_type.has_value()) {
389 const CPPType &type = attribute_type_to_cpp_type(*data_type);
390 if (attribute.varray.type() != type) {
391 attribute.varray = try_adapt_data_type(std::move(attribute.varray), type);
392 attribute.sharing_info = nullptr;
393 if (!attribute.varray) {
394 return {};
395 }
396 }
397 }
398 return attribute;
399}
400
402 const std::optional<AttrDomain> domain,
403 const std::optional<AttrType> data_type) const
404{
405 return adapt_domain_and_type_if_necessary(this->lookup(name), domain, data_type, *this);
406}
407
409 std::optional<AttrType> data_type) const
410{
411 BLI_assert(this->accessor != nullptr);
413}
414
416 const AttrDomain domain,
417 const AttrType data_type,
418 const void *default_value) const
419{
420 GAttributeReader attribute = this->lookup(name, domain, data_type);
421 if (attribute) {
422 return attribute;
423 }
424 const CPPType &type = attribute_type_to_cpp_type(data_type);
425 const int64_t domain_size = this->domain_size(domain);
426 if (default_value == nullptr) {
427 return {GVArray::from_single_ref(type, domain_size, type.default_value()), domain, nullptr};
428 }
429 return {GVArray::from_single(type, domain_size, default_value), domain, nullptr};
430}
431
433{
434 Set<StringRefNull> names;
435 this->foreach_attribute([&](const AttributeIter &iter) { names.add(iter.name); });
436 return names;
437}
438
441{
442 bool found_anonymous = false;
443 this->foreach_attribute([&](const AttributeIter &iter) {
445 found_anonymous = true;
446 iter.stop();
447 }
448 });
449 return found_anonymous;
450}
451
453{
454 Vector<std::string> anonymous_ids;
455 for (const StringRef name : this->all_names()) {
456 if (attribute_name_is_anonymous(name)) {
457 anonymous_ids.append(name);
458 }
459 }
460
461 while (!anonymous_ids.is_empty()) {
462 this->remove(anonymous_ids.pop_last());
463 }
464}
465
469#ifndef NDEBUG
471 std::string name;
472 bool finish_called = false;
473 std::function<void()> real_finish_fn;
474
476 {
477 if (!this->finish_called) {
478 std::cerr << "Forgot to call `finish()` for '" << this->name << "'.\n";
480 }
481 }
482};
483#endif
484
486{
488 /* Check that the #finish method is called in debug builds. */
489#ifndef NDEBUG
490 if (attribute) {
491 auto checker = std::make_shared<FinishCallChecker>();
492 checker->name = name;
493 checker->real_finish_fn = attribute.tag_modified_fn;
494 attribute.tag_modified_fn = [checker]() {
495 if (checker->real_finish_fn) {
496 checker->real_finish_fn();
497 }
498 checker->finish_called = true;
499 };
500 }
501#endif
502 return attribute;
503}
504
506{
508 if (attribute) {
509 return GSpanAttributeWriter{std::move(attribute), true};
510 }
511 return {};
512}
513
515 const StringRef name,
516 const AttrDomain domain,
517 const AttrType data_type,
518 const AttributeInit &initializer)
519{
520 std::optional<AttributeMetaData> meta_data = this->lookup_meta_data(name);
521 if (meta_data.has_value()) {
522 if (meta_data->domain == domain && meta_data->data_type == data_type) {
523 return this->lookup_for_write(name);
524 }
525 return {};
526 }
527 if (this->add(name, domain, data_type, initializer)) {
528 return this->lookup_for_write(name);
529 }
530 return {};
531}
532
534 const StringRef name,
535 const AttrDomain domain,
536 const AttrType data_type,
537 const AttributeInit &initializer)
538{
539 GAttributeWriter attribute = this->lookup_or_add_for_write(name, domain, data_type, initializer);
540 if (attribute) {
541 return GSpanAttributeWriter{std::move(attribute), true};
542 }
543 return {};
544}
545
547 const StringRef name, const AttrDomain domain, const AttrType data_type)
548{
550 name, domain, data_type, AttributeInitConstruct());
551 if (attribute) {
552 return GSpanAttributeWriter{std::move(attribute), false};
553 }
554 return {};
555}
556
558 const StringRef name,
559 const AttrDomain domain,
560 const AttrType data_type,
561 const AttributeInit &initializer)
562{
563 std::optional<AttributeMetaData> meta_data = this->lookup_meta_data(name);
564 if (meta_data.has_value()) {
565 if (meta_data->domain == domain && meta_data->data_type == data_type) {
566 return this->lookup_for_write(name);
567 }
568 /* The attribute already exists, but with the wrong domain or type.
569 * Convert it. */
570 GVArray data_on_domain = *this->lookup(name, domain, data_type);
571 auto *converted_data = new ImplicitSharedValue<GArray<>>(
572 data_on_domain.type(), data_on_domain.size(), NoInitialization());
573 data_on_domain.materialize_to_uninitialized(converted_data->data.data());
574 const bool added = this->add_override(
575 name,
576 domain,
577 data_type,
578 AttributeInitShared(converted_data->data.data(), *converted_data));
579 converted_data->remove_user_and_delete_if_last();
580 if (added) {
581 return this->lookup_for_write(name);
582 }
583 /* Transfer failed, convert_or_add_for_write() should not be used for attributes
584 * that can't be converted. */
586 return {};
587 }
588 this->add(name, domain, data_type, initializer);
589 return this->lookup_for_write(name);
590}
591
593 const AttrDomain domain,
594 const AttrType data_type)
595{
596 std::optional<AttributeMetaData> meta_data = this->lookup_meta_data(name);
597 if (meta_data.has_value()) {
598 if (meta_data->domain == domain && meta_data->data_type == data_type) {
599 return this->lookup_for_write(name);
600 }
601 /* The attribute already exists, but with the wrong domain or type.
602 * Convert it. */
603 if (this->add_override(name, domain, data_type, AttributeInitConstruct())) {
604 return this->lookup_for_write(name);
605 }
606 return {};
607 }
608 this->add(name, domain, data_type, AttributeInitConstruct());
609 return this->lookup_for_write(name);
610}
611
613 const StringRef name,
614 const AttrDomain domain,
615 const AttrType data_type,
616 const AttributeInit &initializer)
617{
619 name, domain, data_type, initializer);
621 return GSpanAttributeWriter{std::move(attribute), true};
622}
623
625 const StringRef name, const AttrDomain domain, const AttrType data_type)
626{
627 GAttributeWriter attribute = this->convert_or_add_for_write_only(name, domain, data_type);
629 return GSpanAttributeWriter{std::move(attribute), false};
630}
631
633 const StringRef new_name,
634 const bool overwrite)
635{
637 map.add_new(old_name, new_name);
638 return fn_->rename(owner_, map, overwrite).is_empty();
639}
640
642 const bool overwrite)
643{
644 return fn_->rename(owner_, map, overwrite);
645}
646
648{
649 if (function) {
650 auto validate_op = fn::FieldOperation::from(*function, {field});
651 return fn::GField(validate_op);
652 }
653 return field;
654}
655
657 const AttributeAccessor src_attributes,
658 MutableAttributeAccessor dst_attributes,
659 Span<AttrDomain> domains,
660 const bke::AttributeFilter &attribute_filter)
661{
663 src_attributes.foreach_attribute([&](const AttributeIter &iter) {
664 if (!domains.contains(iter.domain)) {
665 return;
666 }
667 if (iter.data_type == AttrType::String) {
668 return;
669 }
670 if (attribute_filter.allow_skip(iter.name)) {
671 return;
672 }
673 GVArray src = *iter.get();
674 const CommonVArrayInfo info = src.common_info();
676 const GPointer value(src.type(), info.data);
677 if (dst_attributes.add(iter.name, iter.domain, iter.data_type, AttributeInitValue(value))) {
678 return;
679 }
680 }
682 iter.name, iter.domain, iter.data_type);
683 /* Skip unsupported attributes. */
684 if (!dst) {
685 return;
686 }
687 attributes.append({std::move(src), iter.name, {iter.domain, iter.data_type}, std::move(dst)});
688 });
689 return attributes;
690}
691
693
695 const StringRef name,
696 const AttrDomain dst_domain,
697 const AttrType data_type,
698 MutableAttributeAccessor &dst_attributes)
699{
700 const CommonVArrayInfo src_info = src.common_info();
701 if (src_info.type != CommonVArrayInfo::Type::Single) {
702 return false;
703 }
704 const GPointer value(src.type(), src_info.data);
705 return dst_attributes.add(name, dst_domain, data_type, AttributeInitValue(value));
706}
707
708void gather_attributes(const AttributeAccessor src_attributes,
709 const AttrDomain src_domain,
710 const AttrDomain dst_domain,
711 const AttributeFilter &attribute_filter,
712 const IndexMask &selection,
713 MutableAttributeAccessor dst_attributes)
714{
716 const int src_size = src_attributes.domain_size(src_domain);
717 src_attributes.foreach_attribute([&](const AttributeIter &iter) {
718 if (iter.domain != src_domain) {
719 return;
720 }
721 if (iter.data_type == AttrType::String) {
722 return;
723 }
724 if (attribute_filter.allow_skip(iter.name)) {
725 return;
726 }
727 const GAttributeReader src = iter.get(src_domain);
729 *src, iter.name, dst_domain, iter.data_type, dst_attributes))
730 {
731 return;
732 }
733 if (selection.size() == src_size && src.sharing_info && src.varray.is_span()) {
735 if (dst_attributes.add(iter.name, dst_domain, iter.data_type, init)) {
736 return;
737 }
738 }
740 iter.name, dst_domain, iter.data_type);
741 if (!dst) {
742 return;
743 }
744 array_utils::gather(src.varray, selection, dst.span);
745 dst.finish();
746 });
747}
748
749void gather_attributes(const AttributeAccessor src_attributes,
750 const AttrDomain src_domain,
751 const AttrDomain dst_domain,
752 const AttributeFilter &attribute_filter,
753 const Span<int> indices,
754 MutableAttributeAccessor dst_attributes)
755{
757 if (array_utils::indices_are_range(indices, IndexRange(src_attributes.domain_size(src_domain))))
758 {
759 copy_attributes(src_attributes, src_domain, dst_domain, attribute_filter, dst_attributes);
760 }
761 else {
762 src_attributes.foreach_attribute([&](const AttributeIter &iter) {
763 if (iter.domain != src_domain) {
764 return;
765 }
766 if (iter.data_type == AttrType::String) {
767 return;
768 }
769 if (attribute_filter.allow_skip(iter.name)) {
770 return;
771 }
772 const GAttributeReader src = iter.get(src_domain);
774 *src, iter.name, dst_domain, iter.data_type, dst_attributes))
775 {
776 return;
777 }
779 iter.name, dst_domain, iter.data_type);
780 if (!dst) {
781 return;
782 }
783 attribute_math::gather(src.varray, indices, dst.span);
784 dst.finish();
785 });
786 }
787}
788
790 const AttrDomain src_domain,
791 const AttrDomain dst_domain,
792 const AttributeFilter &attribute_filter,
793 const OffsetIndices<int> src_offsets,
794 const OffsetIndices<int> dst_offsets,
795 const IndexMask &selection,
796 MutableAttributeAccessor dst_attributes)
797{
799 if (selection.size() == src_offsets.size()) {
800 if (src_attributes.domain_size(src_domain) == dst_attributes.domain_size(src_domain)) {
801 /* When all groups are selected and the domains are the same size, all values are copied,
802 * because corresponding groups are required to be the same size. */
803 copy_attributes(src_attributes, src_domain, dst_domain, attribute_filter, dst_attributes);
804 return;
805 }
806 }
807 src_attributes.foreach_attribute([&](const AttributeIter &iter) {
808 if (iter.domain != src_domain) {
809 return;
810 }
811 if (iter.data_type == AttrType::String) {
812 return;
813 }
814 if (attribute_filter.allow_skip(iter.name)) {
815 return;
816 }
817 const GVArray src = *iter.get(src_domain);
818 if (try_add_single_value_attribute(src, iter.name, dst_domain, iter.data_type, dst_attributes))
819 {
820 return;
821 }
823 iter.name, dst_domain, iter.data_type);
824 if (!dst) {
825 return;
826 }
827 const GVArraySpan src_span = src;
828 attribute_math::gather_group_to_group(src_offsets, dst_offsets, selection, src_span, dst.span);
829 dst.finish();
830 });
831}
832
834 const AttrDomain src_domain,
835 const AttrDomain dst_domain,
836 const AttributeFilter &attribute_filter,
837 const OffsetIndices<int> dst_offsets,
838 const IndexMask &src_selection,
839 MutableAttributeAccessor dst_attributes)
840{
842 src_attributes.foreach_attribute([&](const AttributeIter &iter) {
843 if (iter.domain != src_domain) {
844 return;
845 }
846 if (iter.data_type == AttrType::String) {
847 return;
848 }
849 if (attribute_filter.allow_skip(iter.name)) {
850 return;
851 }
852 const GVArray src = *iter.get(src_domain);
853 if (try_add_single_value_attribute(src, iter.name, dst_domain, iter.data_type, dst_attributes))
854 {
855 return;
856 }
858 iter.name, dst_domain, iter.data_type);
859 if (!dst) {
860 return;
861 }
862 const GVArraySpan src_span = src;
863 attribute_math::gather_to_groups(dst_offsets, src_selection, src_span, dst.span);
864 dst.finish();
865 });
866}
867
868void copy_attributes(const AttributeAccessor src_attributes,
869 const AttrDomain src_domain,
870 const AttrDomain dst_domain,
871 const AttributeFilter &attribute_filter,
872 MutableAttributeAccessor dst_attributes)
873{
875 BLI_assert(src_attributes.domain_size(src_domain) == dst_attributes.domain_size(dst_domain));
876 gather_attributes(src_attributes,
877 src_domain,
878 dst_domain,
879 attribute_filter,
880 IndexMask(src_attributes.domain_size(src_domain)),
881 dst_attributes);
882}
883
885 const CPPType &type,
886 const StringRef name)
887{
888 if (attributes.is_builtin(name)) {
889 if (const GPointer value = attributes.get_builtin_default(name)) {
890 return value;
891 }
892 }
893 return GPointer(type, type.default_value());
894}
895
897 const AttrDomain src_domain,
898 const AttrDomain dst_domain,
899 const AttributeFilter &attribute_filter,
900 const OffsetIndices<int> src_offsets,
901 const OffsetIndices<int> dst_offsets,
902 const IndexMask &selection,
903 MutableAttributeAccessor dst_attributes)
904{
906 if (selection.is_empty()) {
907 return;
908 }
909 src_attributes.foreach_attribute([&](const AttributeIter &iter) {
910 if (iter.domain != src_domain) {
911 return;
912 }
913 if (iter.data_type == AttrType::String) {
914 return;
915 }
916 if (attribute_filter.allow_skip(iter.name)) {
917 return;
918 }
919 const GVArray src = *iter.get(src_domain);
920 if (try_add_single_value_attribute(src, iter.name, dst_domain, iter.data_type, dst_attributes))
921 {
922 return;
923 }
924 const bool dst_already_exists = dst_attributes.contains(iter.name);
926 iter.name, dst_domain, iter.data_type);
927 if (!dst) {
928 return;
929 }
930 if (!dst_already_exists) {
931 /* Skip filling with the default value if all of the data is going to be filled. */
932 if (!(dst_offsets.total_size() == dst.span.size() && selection.size() == dst_offsets.size()))
933 {
934 const CPPType &type = attribute_type_to_cpp_type(iter.data_type);
935 const GPointer value = get_default_for_fill(dst_attributes, type, iter.name);
936 type.fill_construct_n(value.get(), dst.span.data(), dst.span.size());
937 }
938 }
939 const GVArraySpan src_span = src;
940 array_utils::copy_group_to_group(src_offsets, dst_offsets, selection, src_span, dst.span);
941 dst.finish();
942 });
943}
944
946 const AttrDomain domain,
947 const AttributeFilter &attribute_filter,
948 const IndexRange range)
949{
951 /* While it is valid to call this function for any valid range which can be placed in target
952 * domain, it is computationally costly to perform this loop. This check is COW elision and not
953 * just loop skip. */
954 if (range.is_empty()) {
955 return;
956 }
957
958 attributes.foreach_attribute([&](const AttributeIter &iter) {
959 if (iter.domain != domain) {
960 return;
961 }
962 if (attribute_filter.allow_skip(iter.name)) {
963 return;
964 }
965 if (iter.data_type == AttrType::String) {
966 return;
967 }
968 const GVArray varray = *iter.get();
969 const CPPType &type = varray.type();
970 const GPointer value = get_default_for_fill(attributes, type, iter.name);
971 const CommonVArrayInfo info = varray.common_info();
973 if (type.is_equal(value.get(), info.data)) {
974 return;
975 }
976 }
978 GMutableSpan data = attribute.span.slice(range);
979 type.fill_assign_n(value.get(), data.data(), data.size());
980 attribute.finish();
981 });
982}
983
985 MutableAttributeAccessor &attributes)
986{
988 const GAttributeReader normals = attributes.lookup("custom_normal");
989 if (!normals) {
990 return;
991 }
992 if (!normals.varray.type().is<float3>()) {
993 return;
994 }
995 if (normals.sharing_info->is_mutable()) {
997 "custom_normal");
999 normals.finish();
1000 }
1001 else {
1002 /* It's a bit faster to combine transforming and copying the attribute if it's shared. */
1003 auto *new_data = new ImplicitSharedValue<Array<float3>>(normals.varray.size());
1005 VArraySpan(normals.varray.typed<float3>()), float3x3(transform), new_data->data);
1006 const AttrDomain domain = normals.domain;
1007 attributes.remove("custom_normal");
1008 attributes.add<float3>(
1009 "custom_normal", domain, AttributeInitShared(new_data->data.data(), *new_data));
1010 new_data->remove_user_and_delete_if_last();
1011 }
1012}
1013
1014} // namespace blender::bke
Low-level operations for curves.
CustomData interface, see also DNA_customdata_types.h.
#define UV_PINNED_NAME
ID and Library types, which are fundamental for SDNA.
#define PRF_scope(category)
#define BLI_assert_unreachable()
static const CPPType & get()
void fill_assign_n(const void *value, void *dst, int64_t n) const
bool is_equal(const void *a, const void *b) const
void fill_construct_n(const void *value, void *dst, int64_t n) const
bool is() const
const void * default_value() const
GPointer()=default
const void * get() const
const void * data() const
CommonVArrayInfo common_info() const
void materialize_to_uninitialized(void *dst) const
static GVArray from_single(const CPPType &type, int64_t size, const void *value)
static GVArray from_single_ref(const CPPType &type, int64_t size, const void *value)
constexpr bool is_empty() const
constexpr IndexRange()=default
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr bool contains(const T &value) const
Definition BLI_span.hh:281
void append(const T &value)
bool is_empty() const
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
Set< StringRefNull > all_names() const
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
bool contains(StringRef name) const
static std::optional< AttributeAccessor > from_id(const ID &id)
GVArray adapt_domain(const GVArray &varray, const AttrDomain from_domain, const AttrDomain to_domain) const
std::optional< AttributeMetaData > lookup_meta_data(StringRef name) const
GAttributeReader lookup(const StringRef name) const
int domain_size(const AttrDomain domain) const
GPointer get_builtin_default(const StringRef name) const
const AttributeAccessorFunctions * fn_
bool is_builtin(const StringRef name) const
GAttributeReader get() const
const AttributeAccessor * accessor
GVArray try_convert(GVArray varray, const CPPType &to_type) const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GAttributeWriter lookup_or_add_for_write(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GSpanAttributeWriter convert_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GAttributeWriter convert_or_add_for_write(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
GAttributeWriter lookup_for_write(StringRef name)
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
GSpanAttributeWriter convert_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
GAttributeWriter convert_or_add_for_write_only(StringRef name, AttrDomain domain, AttrType data_type)
bool add_override(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_for_write_span(StringRef name)
bool rename(StringRef old_name, StringRef new_name, bool overwrite=false)
constexpr bool startswith(StringRef prefix) const
static FieldOperationPtr from(std::shared_ptr< const mf::MultiFunction > fn, Vector< GField > inputs)
Definition field.cc:252
#define attribute(slot)
uint to_type(bool)
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
bool indices_are_range(Span< int > indices, IndexRange range)
void copy_group_to_group(OffsetIndices< int > src_offsets, OffsetIndices< int > dst_offsets, const IndexMask &selection, GSpan src, GMutableSpan dst)
void gather(GSpan src, Span< int > map, GMutableSpan dst)
void gather_to_groups(OffsetIndices< int > dst_offsets, const IndexMask &src_selection, GSpan src, GMutableSpan dst)
void gather_group_to_group(OffsetIndices< int > src_offsets, OffsetIndices< int > dst_offsets, const IndexMask &selection, GSpan src, GMutableSpan dst)
bool attribute_name_is_anonymous(const StringRef name)
static GPointer get_default_for_fill(AttributeAccessor attributes, const CPPType &type, const StringRef name)
std::optional< eCustomDataType > cpp_type_to_custom_data_type(const CPPType &type)
void fill_attribute_range_default(MutableAttributeAccessor dst_attributes, AttrDomain domain, const AttributeFilter &attribute_filter, IndexRange range)
const CPPType * custom_data_type_to_cpp_type(eCustomDataType type)
void gather_attributes_to_groups(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, OffsetIndices< int > dst_offsets, const IndexMask &src_selection, MutableAttributeAccessor dst_attributes)
static GVArray try_adapt_data_type(GVArray varray, const CPPType &to_type)
const DataTypeConversions & get_implicit_type_conversions()
static int attribute_domain_priority(const AttrDomain domain)
bool allow_procedural_attribute_access(StringRef attribute_name)
void copy_attributes(const AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, MutableAttributeAccessor dst_attributes)
static bool try_add_single_value_attribute(const GVArray &src, const StringRef name, const AttrDomain dst_domain, const AttrType data_type, MutableAttributeAccessor &dst_attributes)
const char * no_procedural_access_message
Vector< AttributeTransferData > retrieve_attributes_for_transfer(const AttributeAccessor src_attributes, MutableAttributeAccessor dst_attributes, Span< AttrDomain > domains, const AttributeFilter &attribute_filter={})
void gather_attributes(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
static int attribute_data_type_complexity(const AttrType data_type)
AttrType attribute_data_type_highest_complexity(Span< AttrType > data_types)
AttrDomain attribute_domain_highest_priority(Span< AttrDomain > domains)
void transform_custom_normal_attribute(const float4x4 &transform, MutableAttributeAccessor &attributes)
void gather_attributes_group_to_group(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, OffsetIndices< int > src_offsets, OffsetIndices< int > dst_offsets, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
void copy_attributes_group_to_group(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, OffsetIndices< int > src_offsets, OffsetIndices< int > dst_offsets, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
static GAttributeReader adapt_domain_and_type_if_necessary(GAttributeReader attribute, const std::optional< AttrDomain > domain, const std::optional< AttrType > data_type, const AttributeAccessor &accessor)
AttrType cpp_type_to_attribute_type(const CPPType &type)
const CPPType & attribute_type_to_cpp_type(AttrType type)
QuaternionBase< float > Quaternion
void transform_normals(const float3x3 &transform, MutableSpan< float3 > normals)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
BMesh const char void * data
MatBase< float, 3, 3 > float3x3
VecBase< int16_t, 2 > short2
ID_Type GS(const char *name)
Definition DNA_ID.h:410
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
signed char int8_t
Definition BKE_text.h:76
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
GAttributeWriter(*) lookup_for_write(void *owner, StringRef name)
Set< StringRef >(*) rename(void *owner, const Map< StringRef, StringRef > &map, bool overwrite)
bool allow_skip(const StringRef name) const
AttributeInitShared(const void *data, const ImplicitSharingInfo &sharing_info)
const fn::multi_function::MultiFunction * function
fn::GField validate_field_if_necessary(const fn::GField &field) const
std::function< void()> real_finish_fn
const ImplicitSharingInfo * sharing_info
#define N_(msgid)