Blender V5.3
geometry_attributes.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MEM_guardedalloc.h"
10
11#include "DNA_mesh_types.h"
12#include "DNA_meshdata_types.h"
14
15#include "BLI_color.hh"
16#include "BLI_generic_array.hh"
18#include "BLI_listbase.hh"
19
20#include "BKE_attribute.hh"
22#include "BKE_context.hh"
23#include "BKE_curves.hh"
24#include "BKE_customdata.hh"
25#include "BKE_deform.hh"
26#include "BKE_editmesh.hh"
27#include "BKE_geometry_set.hh"
28#include "BKE_lib_id.hh"
29#include "BKE_mesh.hh"
30#include "BKE_object_deform.h"
31#include "BKE_paint.hh"
32#include "BKE_report.hh"
33
34#include "BLT_translation.hh"
35
36#include "RNA_access.hh"
37#include "RNA_define.hh"
38#include "RNA_enum_types.hh"
39
40#include "DEG_depsgraph.hh"
41
42#include "WM_api.hh"
43#include "WM_types.hh"
44
46#include "UI_resources.hh"
47
48#include "ED_geometry.hh"
49#include "ED_mesh.hh"
50#include "ED_object.hh"
51#include "ED_sculpt.hh"
52
53#include "geometry_intern.hh"
54
56
58{
59 switch (type) {
61 return "value_float";
63 return "value_float_vector_2d";
65 return "value_float_vector_3d";
67 return "value_float_vector_4d";
70 return "value_color";
72 return "value_bool";
75 return "value_int";
78 return "value_int_vector_2d";
79 default:
81 return "";
82 }
83}
84
89
91{
92 static float4 color_default(1);
93
94 RNA_def_float(&srna, "value_float", 0.0f, -FLT_MAX, FLT_MAX, "Value", "", -FLT_MAX, FLT_MAX);
96 "value_float_vector_2d",
97 2,
98 nullptr,
99 -FLT_MAX,
100 FLT_MAX,
101 "Value",
102 "",
103 -FLT_MAX,
104 FLT_MAX);
106 "value_float_vector_3d",
107 3,
108 nullptr,
109 -FLT_MAX,
110 FLT_MAX,
111 "Value",
112 "",
113 -FLT_MAX,
114 FLT_MAX);
116 "value_float_vector_4d",
117 4,
118 nullptr,
119 -FLT_MAX,
120 FLT_MAX,
121 "Value",
122 "",
123 -FLT_MAX,
124 FLT_MAX);
125 RNA_def_int(&srna, "value_int", 0, INT_MIN, INT_MAX, "Value", "", INT_MIN, INT_MAX);
127 &srna, "value_int_vector_2d", 2, nullptr, INT_MIN, INT_MAX, "Value", "", INT_MIN, INT_MAX);
129 &srna, "value_color", 4, color_default, -FLT_MAX, FLT_MAX, "Value", "", 0.0f, 1.0f);
130 RNA_def_boolean(&srna, "value_bool", false, "Value", "");
131}
132
134 const bke::AttrType type,
135 void *buffer)
136{
137 const StringRefNull prop_name = rna_property_name_for_type(type);
138 switch (type) {
140 *static_cast<float *>(buffer) = RNA_float_get(&ptr, prop_name.c_str());
141 break;
143 RNA_float_get_array(&ptr, prop_name.c_str(), static_cast<float *>(buffer));
144 break;
146 RNA_float_get_array(&ptr, prop_name.c_str(), static_cast<float *>(buffer));
147 break;
149 RNA_float_get_array(&ptr, prop_name.c_str(), static_cast<float *>(buffer));
150 break;
152 RNA_float_get_array(&ptr, prop_name.c_str(), static_cast<float *>(buffer));
153 break;
155 ColorGeometry4f value;
156 RNA_float_get_array(&ptr, prop_name.c_str(), value);
157 *static_cast<ColorGeometry4b *>(buffer) = color::encode(value);
158 break;
159 }
161 *static_cast<bool *>(buffer) = RNA_boolean_get(&ptr, prop_name.c_str());
162 break;
164 *static_cast<int8_t *>(buffer) = RNA_int_get(&ptr, prop_name.c_str());
165 break;
167 *static_cast<int32_t *>(buffer) = RNA_int_get(&ptr, prop_name.c_str());
168 break;
170 int2 value;
171 RNA_int_get_array(&ptr, prop_name.c_str(), value);
172 *static_cast<short2 *>(buffer) = short2(value);
173 break;
174 }
176 RNA_int_get_array(&ptr, prop_name.c_str(), static_cast<int *>(buffer));
177 break;
178 default:
180 return {};
181 }
182 return GPointer(bke::attribute_type_to_cpp_type(type), buffer);
183}
184
186 PropertyRNA &prop,
187 const GPointer value)
188{
189 switch (bke::cpp_type_to_attribute_type(*value.type())) {
191 RNA_property_float_set(&ptr, &prop, *value.get<float>());
192 break;
194 RNA_property_float_set_array(&ptr, &prop, *value.get<float2>());
195 break;
197 RNA_property_float_set_array(&ptr, &prop, *value.get<float3>());
198 break;
200 RNA_property_float_set_array(&ptr, &prop, *value.get<float4>());
201 break;
204 break;
206 RNA_property_float_set_array(&ptr, &prop, *value.get<ColorGeometry4f>());
207 break;
209 RNA_property_boolean_set(&ptr, &prop, *value.get<bool>());
210 break;
212 RNA_property_int_set(&ptr, &prop, *value.get<int8_t>());
213 break;
215 RNA_property_int_set(&ptr, &prop, *value.get<int32_t>());
216 break;
218 RNA_property_int_set_array(&ptr, &prop, int2(*value.get<short2>()));
219 break;
221 RNA_property_int_set_array(&ptr, &prop, *value.get<int2>());
222 break;
223 default:
225 }
226}
227
228bool attribute_set_poll(bContext &C, const ID &object_data)
229{
230 AttributeOwner owner = AttributeOwner::from_id(&const_cast<ID &>(object_data));
231 const std::optional<StringRef> name = BKE_attributes_active_name_get(owner);
232 if (!name) {
233 CTX_wm_operator_poll_msg_set(&C, "No active attribute");
234 return false;
235 }
236
237 if (owner.type() == AttributeOwnerType::Mesh) {
238 const Mesh *mesh = owner.get_mesh();
239 if (const BMesh *bm = BKE_editmesh_bmesh_get(mesh)) {
241 if (ELEM(attr.type,
245 {
246 CTX_wm_operator_poll_msg_set(&C, "The active attribute has an unsupported type");
247 return false;
248 }
249 return true;
250 }
251 }
252
253 bke::AttributeAccessor attributes = *owner.get_accessor();
254 std::optional<bke::AttributeMetaData> meta_data = attributes.lookup_meta_data(*name);
255 if (!meta_data) {
256 CTX_wm_operator_poll_msg_set(&C, "No active attribute");
257 return false;
258 }
259 if (ELEM(meta_data->data_type,
263 {
264 CTX_wm_operator_poll_msg_set(&C, "The active attribute has an unsupported type");
265 return false;
266 }
267 return true;
268}
269
270/*********************** Attribute Operators ************************/
271
273{
274 using namespace blender::bke;
275 const Main *bmain = CTX_data_main(C);
276 if (!ob || !BKE_id_is_editable(bmain, &ob->id)) {
277 return false;
278 }
279 const ID *data = (ob) ? static_cast<const ID *>(ob->data) : nullptr;
280 if (!data || !BKE_id_is_editable(bmain, data)) {
281 return false;
282 }
283 return AttributeAccessor::from_id(*data).has_value();
284}
285
287 const Object *ob,
288 const ID_Type obdata_type)
289{
290 if (!geometry_attributes_poll_ex(C, ob)) {
291 return false;
292 }
293 const ID *id = ob->data;
294 if (GS(id->name) != obdata_type) {
295 return false;
296 }
297
298 return true;
299}
300
305
311
313{
315 return false;
316 }
317
319 ID *data = (ob) ? ob->data : nullptr;
321 if (BKE_attributes_active_name_get(owner) != std::nullopt) {
322 return true;
323 }
324
325 return false;
326}
327
329 PointerRNA * /*ptr*/,
330 PropertyRNA * /*prop*/,
331 bool *r_free)
332{
333 if (C == nullptr) {
335 }
336
338 if (ob == nullptr) {
340 }
341
343 return rna_enum_attribute_domain_itemf(owner, false, r_free);
344}
345
347 const bke::AttrDomain domain,
348 const bke::AttrType type,
349 const StringRefNull name)
350{
351 if (bke::mesh::is_color_attribute({domain, type})) {
352 if (!BKE_id_attributes_color_find(&mesh.id, mesh.active_color_attribute)) {
354 }
355 if (!BKE_id_attributes_color_find(&mesh.id, mesh.default_color_attribute)) {
357 }
358 }
359 else if (bke::mesh::is_uv_map({domain, type})) {
360 const VectorSet<StringRefNull> uv_maps = mesh.uv_map_names();
361 if (!uv_maps.contains(mesh.active_uv_map_name())) {
362 mesh.uv_maps_active_set(name);
363 }
364 if (!uv_maps.contains(mesh.default_uv_map_name())) {
365 mesh.uv_maps_default_set(name);
366 }
367 }
368}
369
371{
373 ID *id = ob->data;
374
375 char name[MAX_NAME];
376 RNA_string_get(op->ptr, "name", name);
377 const eCustomDataType cd_type = eCustomDataType(RNA_enum_get(op->ptr, "data_type"));
379 const bke::AttrDomain domain = bke::AttrDomain(RNA_enum_get(op->ptr, "domain"));
381
382 if (owner.type() == AttributeOwnerType::Mesh) {
383 Mesh &mesh = *id_cast<Mesh *>(id);
385 CustomDataLayer *layer = BKE_attribute_new(mesh, *bm, name, cd_type, domain, op->reports);
386 if (layer == nullptr) {
387 return OPERATOR_CANCELLED;
388 }
389 const StringRefNull new_name = layer->name;
390 BKE_attributes_active_set(owner, new_name);
391
392 set_active_default_status_on_add(mesh, domain, type, new_name);
393
396
397 return OPERATOR_FINISHED;
398 }
399 }
400
402 if (!accessor.domain_supported(bke::AttrDomain(domain))) {
403 BKE_report(op->reports, RPT_ERROR, "Attribute domain not supported by this geometry type");
404 return OPERATOR_CANCELLED;
405 }
406 bke::AttributeStorage &attributes = *owner.get_storage();
407 const int domain_size = accessor.domain_size(bke::AttrDomain(domain));
408
409 const CPPType &cpp_type = bke::attribute_type_to_cpp_type(type);
410 bke::Attribute &attr = attributes.add(
412 bke::AttrDomain(domain),
413 type,
414 bke::Attribute::ArrayData::from_default_value(cpp_type, domain_size));
415
416 BKE_attributes_active_set(owner, attr.name());
417
418 if (owner.type() == AttributeOwnerType::Mesh) {
419 set_active_default_status_on_add(*owner.get_mesh(), domain, type, attr.name());
420 }
421
424
425 return OPERATOR_FINISHED;
426}
427
429 wmOperator *op,
430 const wmEvent *event)
431{
432 PropertyRNA *prop;
433 prop = RNA_struct_find_property(op->ptr, "name");
434 if (!RNA_property_is_set(op->ptr, prop)) {
435 RNA_property_string_set(op->ptr, prop, DATA_("Attribute"));
436 }
437 /* Set a valid default domain, in case Point domain is not supported. */
438 prop = RNA_struct_find_property(op->ptr, "domain");
439 if (!RNA_property_is_set(op->ptr, prop)) {
440 EnumPropertyItem *items;
441 int totitems;
442 bool free;
444 C, op->ptr, prop, const_cast<const EnumPropertyItem **>(&items), &totitems, &free);
445 if (totitems > 0) {
446 RNA_property_enum_set(op->ptr, prop, items[0].value);
447 }
448 if (free) {
449 MEM_delete(items);
450 }
451 }
453 C, op, event, IFACE_("Add Attribute"), CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Add"));
454}
455
457{
458 /* identifiers */
459 ot->name = "Add Attribute";
460 ot->description = "Add attribute to geometry";
461 ot->idname = "GEOMETRY_OT_attribute_add";
462
463 /* API callbacks. */
467
468 /* flags */
470
471 /* properties */
472 PropertyRNA *prop;
473
474 /* The default name of the new attribute can be translated if new data translation is enabled,
475 * but since the user can choose it at invoke time, the translation happens in the invoke
476 * callback instead of here. */
477 prop = RNA_def_string(ot->srna, "name", nullptr, MAX_NAME, "Name", "Name of new attribute");
479
480 prop = RNA_def_enum(ot->srna,
481 "domain",
484 "Domain",
485 "Type of element that attribute is stored on");
488
489 prop = RNA_def_enum(ot->srna,
490 "data_type",
493 "Data Type",
494 "Type of data stored in attribute");
496}
497
499{
501 ID *id = ob->data;
503 const StringRef name = *BKE_attributes_active_name_get(owner);
504
505 if (!BKE_attribute_remove(owner, name, op->reports)) {
506 return OPERATOR_CANCELLED;
507 }
508
509 int *active_index = BKE_attributes_active_index_p(owner);
510 if (*active_index > 0) {
511 *active_index -= 1;
512 }
514
517
518 return OPERATOR_FINISHED;
519}
520
522{
523 /* identifiers */
524 ot->name = "Remove Attribute";
525 ot->description = "Remove attribute from geometry";
526 ot->idname = "GEOMETRY_OT_attribute_remove";
527
528 /* API callbacks. */
531
532 /* flags */
534}
535
537{
539 ID *id = ob->data;
540
541 char name[MAX_NAME];
542 RNA_string_get(op->ptr, "name", name);
543 eCustomDataType type = eCustomDataType(RNA_enum_get(op->ptr, "data_type"));
544 bke::AttrDomain domain = bke::AttrDomain(RNA_enum_get(op->ptr, "domain"));
545
546 float color[4];
547 RNA_float_get_array(op->ptr, "color", color);
548
550 const std::string unique_name = BKE_attribute_calc_unique_name(owner, name);
551
552 if (owner.type() == AttributeOwnerType::Mesh) {
553 Mesh *mesh = owner.get_mesh();
555 CustomDataLayer *layer = BKE_attribute_new(*mesh, *bm, name, type, domain, op->reports);
556 if (layer == nullptr) {
557 return OPERATOR_CANCELLED;
558 }
559 BKE_id_attributes_active_color_set(id, unique_name);
561 {
563 }
567 return OPERATOR_FINISHED;
568 }
569 }
570
571 bke::MutableAttributeAccessor attributes = *owner.get_accessor();
572 attributes.add(unique_name,
573 domain,
576
577 BKE_id_attributes_active_color_set(id, unique_name);
580 }
584
585 return OPERATOR_FINISHED;
586}
587
589 wmOperator *op,
590 const wmEvent *event)
591{
592 PropertyRNA *prop;
593 prop = RNA_struct_find_property(op->ptr, "name");
594 if (!RNA_property_is_set(op->ptr, prop)) {
595 RNA_property_string_set(op->ptr, prop, DATA_("Color"));
596 }
598 op,
599 event,
600 IFACE_("Add Color Attribute"),
602}
603
606 VertexGroup = 1,
607};
608
610{
612 return false;
613 }
614
616 ID *data = ob->data;
618 if (ob->type == OB_MESH) {
619 if (CTX_data_edit_object(C) != nullptr) {
620 CTX_wm_operator_poll_msg_set(C, "Operation is not allowed in edit mode");
621 return false;
622 }
623 }
624 if (BKE_attributes_active_name_get(owner) == std::nullopt) {
625 return false;
626 }
627 return true;
628}
629
632 const StringRef name,
633 const bke::AttrDomain dst_domain,
634 const bke::AttrType dst_type,
635 ReportList *reports)
636{
637 BLI_assert(attributes.contains(name));
638 if (ELEM(dst_type, bke::AttrType::String)) {
639 if (reports) {
640 BKE_report(reports, RPT_ERROR, "Cannot convert to the selected type");
641 }
642 return false;
643 }
644
645 const bool was_active = BKE_attributes_active_name_get(owner) == name;
646
647 /* Support restoring names after removing, note that this could be a utility. */
648 Mesh *mesh = owner.type() == AttributeOwnerType::Mesh ? owner.get_mesh() : nullptr;
649 const bool was_active_color = mesh && name == StringRef(mesh->active_color_attribute);
650 const bool was_default_color = mesh && name == StringRef(mesh->default_color_attribute);
651 const bool was_active_uv = mesh && name == mesh->active_uv_map_name();
652 const bool was_default_uv = mesh && name == mesh->default_uv_map_name();
653
654 const std::string name_copy = name;
655 const GVArray varray = *attributes.lookup_or_default(name_copy, dst_domain, dst_type);
656
657 GArray<> new_data(varray.type(), varray.size(), NoInitialization());
658 varray.materialize_to_uninitialized(new_data.data());
659 if (!BKE_attribute_remove(owner, name_copy, reports)) {
660 return false;
661 }
662 auto *sharing_info = new ImplicitSharedValue<GArray<>>(std::move(new_data));
663 attributes.add(name_copy,
664 dst_domain,
665 dst_type,
666 bke::AttributeInitShared(sharing_info->data.data(), *sharing_info));
667 sharing_info->remove_user_and_delete_if_last();
668
669 if (was_active) {
670 /* The attribute active status is stored as an index. Changing the attribute's domain will
671 * change its index, so reassign the active attribute if necessary. */
672 BKE_attributes_active_set(owner, name_copy);
673 }
674 if (mesh) {
675 if (bke::mesh::is_color_attribute({dst_domain, dst_type})) {
676 if (was_active_color) {
678 }
679 if (was_default_color) {
681 }
682 }
683 else if (bke::mesh::is_uv_map({dst_domain, dst_type})) {
684 if (was_active_uv) {
685 mesh->uv_maps_active_set(name_copy);
686 }
687 if (was_default_uv) {
688 mesh->uv_maps_default_set(name_copy);
689 }
690 }
691 }
692
693 return true;
694}
695
697{
699 ID *ob_data = ob->data;
702 const eCustomDataType cd_type = eCustomDataType(RNA_enum_get(op->ptr, "data_type"));
704 const bke::AttrDomain domain = bke::AttrDomain(RNA_enum_get(op->ptr, "domain"));
705 const std::string name = *BKE_attributes_active_name_get(owner);
706
707 if (ob->type == OB_MESH) {
708 Mesh *mesh = reinterpret_cast<Mesh *>(ob_data);
709 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
710 switch (mode) {
712 if (!convert_attribute(owner, attributes, name, domain, type, op->reports)) {
713 return OPERATOR_CANCELLED;
714 }
715 BKE_attributes_active_set(owner, name);
716 break;
717 }
719 Array<float> src_weights(mesh->verts_num);
720 VArray<float> src_varray = *attributes.lookup_or_default<float>(
721 name, bke::AttrDomain::Point, 0.0f);
722 src_varray.materialize(src_weights);
723 if (!BKE_attribute_remove(owner, name, op->reports)) {
724 return OPERATOR_CANCELLED;
725 }
726
727 bDeformGroup *defgroup = BKE_object_defgroup_new(ob, name);
728 const int defgroup_index = BLI_findindex(BKE_id_defgroup_list_get(&mesh->id), defgroup);
730 for (const int i : IndexRange(mesh->verts_num)) {
731 const float weight = src_weights[i];
732 if (weight > 0.0f) {
733 BKE_defvert_add_index_notest(dverts + i, defgroup_index, weight);
734 }
735 }
736 BKE_object_defgroup_active_index_set(ob, defgroup_index + 1);
737
738 int *active_index = BKE_attributes_active_index_p(owner);
739 if (*active_index > 0) {
740 *active_index -= 1;
741 }
743 break;
744 }
745 }
748 return OPERATOR_FINISHED;
749 }
750
751 if (!convert_attribute(owner, *owner.get_accessor(), name, domain, type, op->reports)) {
752 return OPERATOR_CANCELLED;
753 }
754 BKE_attributes_active_set(owner, name);
757
758 return OPERATOR_FINISHED;
759}
760
762{
763 ui::Layout &layout = *op->layout;
764 layout.use_property_split_set(true);
765 layout.use_property_decorate_set(false);
766
767 layout.prop(op->ptr, "name", UI_ITEM_NONE, std::nullopt, ICON_NONE);
768 layout.prop(op->ptr, "domain", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
769 layout.prop(op->ptr, "data_type", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
770 layout.prop(op->ptr, "color", UI_ITEM_NONE, std::nullopt, ICON_NONE);
771}
772
774{
775 /* identifiers */
776 ot->name = "Add Color Attribute";
777 ot->description = "Add color attribute to geometry";
778 ot->idname = "GEOMETRY_OT_color_attribute_add";
779
780 /* API callbacks. */
785
786 /* flags */
788
789 /* properties */
790 PropertyRNA *prop;
791
792 /* The default name of the new attribute can be translated if new data translation is enabled,
793 * but since the user can choose it at invoke time, the translation happens in the invoke
794 * callback instead of here. */
795 prop = RNA_def_string(
796 ot->srna, "name", nullptr, MAX_NAME, "Name", "Name of new color attribute");
798
799 prop = RNA_def_enum(ot->srna,
800 "domain",
803 "Domain",
804 "Type of element that attribute is stored on");
805
806 prop = RNA_def_enum(ot->srna,
807 "data_type",
810 "Data Type",
811 "Type of data stored in attribute");
812
813 static float default_color[4] = {0.0f, 0.0f, 0.0f, 1.0f};
814
815 prop = RNA_def_float_color(
816 ot->srna, "color", 4, nullptr, 0.0f, FLT_MAX, "Color", "Default fill color", 0.0f, 1.0f);
818 RNA_def_property_float_array_default(prop, default_color);
819}
820
822{
824 ID *id = ob->data;
825
826 char name[MAX_NAME];
827 RNA_string_get(op->ptr, "name", name);
828 Mesh *mesh = id_cast<Mesh *>(id);
830 const BMDataLayerLookup attr = BM_data_layer_lookup(*bm, name);
831 if (!attr) {
832 return OPERATOR_CANCELLED;
833 }
834 if (!bke::mesh::is_color_attribute({attr.domain, attr.type})) {
835 return OPERATOR_CANCELLED;
836 }
840 }
841 else {
842 const bke::AttributeAccessor attributes = mesh->attributes();
843 if (!bke::mesh::is_color_attribute(attributes.lookup_meta_data(name))) {
844 return OPERATOR_CANCELLED;
845 }
849 }
850
851 return OPERATOR_FINISHED;
852}
853
855{
856 /* identifiers */
857 ot->name = "Set Render Color";
858 ot->description = "Set default color attribute used for rendering";
859 ot->idname = "GEOMETRY_OT_color_attribute_render_set";
860
861 /* API callbacks. */
864
865 /* flags */
867
868 /* properties */
869 PropertyRNA *prop;
870
871 prop = RNA_def_string(ot->srna, "name", "Color", MAX_NAME, "Name", "Name of color attribute");
873}
874
876{
878 ID *id = ob->data;
879 const std::string active_name = BKE_id_attributes_active_color_name(id).value_or("");
880 if (active_name.empty()) {
881 return OPERATOR_CANCELLED;
882 }
884 if (!BKE_attribute_remove(owner, active_name, op->reports)) {
885 return OPERATOR_CANCELLED;
886 }
887
890
891 return OPERATOR_FINISHED;
892}
893
895{
897 return false;
898 }
899
900 const Object *ob = object::context_object(C);
901 const ID *data = ob->data;
902
904 return true;
905 }
906
907 return false;
908}
909
911{
912 /* identifiers */
913 ot->name = "Remove Color Attribute";
914 ot->description = "Remove color attribute from geometry";
915 ot->idname = "GEOMETRY_OT_color_attribute_remove";
916
917 /* API callbacks. */
920
921 /* flags */
923}
924
926{
928 ID *id = ob->data;
929 const std::optional<StringRef> active_name = BKE_id_attributes_active_color_name(id);
930 if (!active_name) {
931 return OPERATOR_CANCELLED;
932 }
933 if (GS(id->name) != ID_ME) {
934 return OPERATOR_CANCELLED;
935 }
936 Mesh &mesh = *id_cast<Mesh *>(id);
937 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
938 bke::GAttributeReader src = attributes.lookup(*active_name);
940 std::string uniquename = BKE_attribute_calc_unique_name(owner, *active_name);
942 if (!attributes.add(uniquename, src.domain, type, bke::AttributeInitVArray(src.varray))) {
943 return OPERATOR_CANCELLED;
944 }
945
947
950
951 return OPERATOR_FINISHED;
952}
953
955{
957 return false;
958 }
959 if (CTX_data_edit_object(C) != nullptr) {
960 CTX_wm_operator_poll_msg_set(C, "Operation is not allowed in edit mode");
961 return false;
962 }
963
964 const Object *ob = object::context_object(C);
965 const ID *data = ob->data;
966
968 return true;
969 }
970
971 return false;
972}
973
975{
976 /* identifiers */
977 ot->name = "Duplicate Color Attribute";
978 ot->description = "Duplicate color attribute";
979 ot->idname = "GEOMETRY_OT_color_attribute_duplicate";
980
981 /* API callbacks. */
984
985 /* flags */
987}
988
990 wmOperator *op,
991 const wmEvent * /*event*/)
992{
994 ID *id = ob->data;
997 const bke::AttributeMetaData meta_data = *accessor.lookup_meta_data(
999
1000 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "domain");
1001 if (!RNA_property_is_set(op->ptr, prop)) {
1002 RNA_property_enum_set(op->ptr, prop, int(meta_data.domain));
1003 }
1004 prop = RNA_struct_find_property(op->ptr, "data_type");
1005 if (!RNA_property_is_set(op->ptr, prop)) {
1007 }
1008
1010 C, op, 300, IFACE_("Convert Attribute Domain"), IFACE_("Convert"));
1011}
1012
1014{
1015 ui::Layout &layout = *op->layout;
1016 layout.use_property_split_set(true);
1017 layout.use_property_decorate_set(false);
1018
1020 if (ob->type == OB_MESH) {
1021 layout.prop(op->ptr, "mode", UI_ITEM_NONE, std::nullopt, ICON_NONE);
1022 }
1023
1024 const ConvertAttributeMode mode = ob->type == OB_MESH ?
1025 ConvertAttributeMode(RNA_enum_get(op->ptr, "mode")) :
1027
1028 if (mode == ConvertAttributeMode::Generic) {
1029 if (ob->type != OB_POINTCLOUD) {
1030 layout.prop(op->ptr, "domain", UI_ITEM_NONE, std::nullopt, ICON_NONE);
1031 }
1032 layout.prop(op->ptr, "data_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
1033 }
1034}
1035
1037 {int(ConvertAttributeMode::Generic), "GENERIC", 0, "Generic", ""},
1038 {int(ConvertAttributeMode::VertexGroup), "VERTEX_GROUP", 0, "Vertex Group", ""},
1039 {0, nullptr, 0, nullptr, nullptr},
1040};
1041
1043 PointerRNA * /*ptr*/,
1044 PropertyRNA * /*prop*/,
1045 bool *r_free)
1046{
1047 if (C == nullptr) {
1049 }
1050
1052 if (ob == nullptr) {
1054 }
1055
1056 if (ob->type == OB_MESH) {
1057 *r_free = false;
1059 }
1060
1061 EnumPropertyItem *items = nullptr;
1062 int totitem = 0;
1064 if (item.value == int(ConvertAttributeMode::Generic)) {
1065 RNA_enum_item_add(&items, &totitem, &item);
1066 }
1067 }
1068 RNA_enum_item_end(&items, &totitem);
1069 *r_free = true;
1070 return items;
1071}
1072
1074{
1076 return false;
1077 }
1078
1079 if (CTX_data_edit_object(C) != nullptr) {
1080 CTX_wm_operator_poll_msg_set(C, "Operation is not allowed in edit mode");
1081 return false;
1082 }
1083
1085 ID *id = ob->data;
1086 if (GS(id->name) != ID_ME) {
1087 return false;
1088 }
1089 const Mesh *mesh = id_cast<const Mesh *>(ob->data);
1090 const char *name = mesh->active_color_attribute;
1091 const bke::AttributeAccessor attributes = mesh->attributes();
1092 if (!bke::mesh::is_color_attribute(attributes.lookup_meta_data(name))) {
1093 return false;
1094 }
1095
1096 return true;
1097}
1098
1100{
1102 Mesh *mesh = id_cast<Mesh *>(ob->data);
1105 owner,
1106 mesh->attributes_for_write(),
1107 mesh->active_color_attribute,
1108 bke::AttrDomain(RNA_enum_get(op->ptr, "domain")),
1110 op->reports);
1113 return OPERATOR_FINISHED;
1114}
1115
1117 wmOperator *op,
1118 const wmEvent * /*event*/)
1119{
1121 Mesh *mesh = id_cast<Mesh *>(ob->data);
1122 const char *name = mesh->active_color_attribute;
1123 const bke::AttributeMetaData meta_data = *mesh->attributes().lookup_meta_data(name);
1124
1125 PropertyRNA *prop = RNA_struct_find_property(op->ptr, "domain");
1126 if (!RNA_property_is_set(op->ptr, prop)) {
1127 RNA_property_enum_set(op->ptr, prop, int(meta_data.domain));
1128 }
1129 prop = RNA_struct_find_property(op->ptr, "data_type");
1130 if (!RNA_property_is_set(op->ptr, prop)) {
1132 }
1133
1135 C, op, 300, IFACE_("Convert Color Attribute Domain"), IFACE_("Convert"));
1136}
1137
1139{
1140 ui::Layout &layout = *op->layout;
1141 layout.use_property_split_set(true);
1142 layout.use_property_decorate_set(false);
1143
1144 layout.prop(op->ptr, "domain", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
1145 layout.prop(op->ptr, "data_type", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
1146}
1147
1149{
1150 ot->name = "Convert Color Attribute";
1151 ot->description = "Change how the color attribute is stored";
1152 ot->idname = "GEOMETRY_OT_color_attribute_convert";
1153
1158
1160
1161 PropertyRNA *prop;
1162
1163 prop = RNA_def_enum(ot->srna,
1164 "domain",
1167 "Domain",
1168 "Type of element that attribute is stored on");
1170
1171 prop = RNA_def_enum(ot->srna,
1172 "data_type",
1175 "Data Type",
1176 "Type of data stored in attribute");
1178}
1179
1181{
1182 ot->name = "Convert Attribute";
1183 ot->description = "Change how the attribute is stored";
1184 ot->idname = "GEOMETRY_OT_attribute_convert";
1185
1190
1192
1193 PropertyRNA *prop;
1194
1195 prop = RNA_def_enum(ot->srna,
1196 "mode",
1199 "Mode",
1200 "");
1202
1203 prop = RNA_def_enum(ot->srna,
1204 "domain",
1207 "Domain",
1208 "Which geometry element to move the attribute to");
1210
1212 ot->srna, "data_type", rna_enum_attribute_type_items, CD_PROP_FLOAT, "Data Type", "");
1213}
1214
1215} // namespace blender::ed::geometry
Low-level operations for curves.
CustomData interface, see also DNA_customdata_types.h.
support for deformation groups and hooks.
Functions for dealing with objects and deform verts, used by painting and tools.
#define CTX_IFACE_(context, msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define IFACE_(msgid)
#define DATA_(msgid)
#define MAX_NAME
Definition DNA_defs.h:63
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define BLI_assert_unreachable()
#define ELEM(...)
bke::AttributeStorage * get_storage() const
std::optional< bke::MutableAttributeAccessor > get_accessor() const
static AttributeOwner from_id(ID *id)
AttributeOwnerType type() const
const void * data() const
const CPPType * type() const
GPointer()=default
const void * get() const
void materialize_to_uninitialized(void *dst) const
constexpr IndexRange()=default
constexpr const char * c_str() const
constexpr StringRef()
void materialize(MutableSpan< T > r_span) const
bool contains(const Key &key) const
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
bool contains(StringRef name) const
static std::optional< AttributeAccessor > from_id(const ID &id)
std::optional< AttributeMetaData > lookup_meta_data(StringRef name) const
bool domain_supported(const AttrDomain domain) const
GAttributeReader lookup(const StringRef name) const
int domain_size(const AttrDomain domain) const
Attribute & add(std::string name, bke::AttrDomain domain, bke::AttrType data_type, Attribute::DataVariant data)
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
#define FLT_MAX
#define INT_MAX
#define layer
bool is_uv_map(const AttributeMetaData &meta_data)
bool is_color_attribute(const AttributeMetaData &meta_data)
void free(Subdiv *subdiv)
Definition subdiv.cc:192
std::optional< eCustomDataType > attr_type_to_custom_data_type(AttrType attr_type)
std::optional< AttrType > custom_data_type_to_attr_type(eCustomDataType data_type)
AttrType cpp_type_to_attribute_type(const CPPType &type)
const CPPType & attribute_type_to_cpp_type(AttrType type)
BLI_INLINE ColorSceneLinear4f< Alpha > decode(const ColorSceneLinearByteEncoded4b< Alpha > &color)
Definition BLI_color.hh:77
BLI_INLINE ColorSceneLinearByteEncoded4b< Alpha > encode(const ColorSceneLinear4f< Alpha > &color)
Definition BLI_color.hh:50
static bool geometry_attributes_poll_ex(bContext *C, const Object *ob)
static bool geometry_attributes_poll(bContext *C)
static void set_active_default_status_on_add(Mesh &mesh, const bke::AttrDomain domain, const bke::AttrType type, const StringRefNull name)
void GEOMETRY_OT_attribute_add(wmOperatorType *ot)
PropertyRNA * rna_property_for_type(PointerRNA &ptr, const bke::AttrType type)
static const EnumPropertyItem * geometry_attribute_domain_itemf(bContext *C, PointerRNA *, PropertyRNA *, bool *r_free)
static wmOperatorStatus geometry_color_attribute_add_exec(bContext *C, wmOperator *op)
static wmOperatorStatus geometry_color_attribute_set_render_exec(bContext *C, wmOperator *op)
static EnumPropertyItem convert_attribute_mode_items[]
void GEOMETRY_OT_color_attribute_add(wmOperatorType *ot)
GPointer rna_property_for_attribute_type_retrieve_value(PointerRNA &ptr, const bke::AttrType type, void *buffer)
void GEOMETRY_OT_attribute_convert(wmOperatorType *ot)
StringRefNull rna_property_name_for_type(const bke::AttrType type)
void GEOMETRY_OT_color_attribute_duplicate(wmOperatorType *ot)
static void geometry_attribute_convert_ui(bContext *C, wmOperator *op)
void GEOMETRY_OT_color_attribute_convert(wmOperatorType *ot)
static bool geometry_attributes_with_id_type_poll_ex(bContext *C, const Object *ob, const ID_Type obdata_type)
static bool geometry_attributes_remove_poll(bContext *C)
static bool geometry_attributes_for_mesh_poll(bContext *C)
static wmOperatorStatus geometry_color_attribute_remove_exec(bContext *C, wmOperator *op)
static wmOperatorStatus geometry_color_attribute_duplicate_exec(bContext *C, wmOperator *)
static void geometry_color_attribute_convert_ui(bContext *, wmOperator *op)
void register_rna_properties_for_attribute_types(StructRNA &srna)
void GEOMETRY_OT_attribute_remove(wmOperatorType *ot)
void GEOMETRY_OT_color_attribute_render_set(wmOperatorType *ot)
void rna_property_for_attribute_type_set_value(PointerRNA &ptr, PropertyRNA &prop, const GPointer value)
bool attribute_set_poll(bContext &C, const ID &object_data)
static bool geometry_color_attributes_remove_poll(bContext *C)
static bool geometry_attribute_convert_poll(bContext *C)
static const EnumPropertyItem * geometry_attribute_convert_mode_itemf(bContext *C, PointerRNA *, PropertyRNA *, bool *r_free)
static void geometry_color_attribute_add_ui(bContext *, wmOperator *op)
static wmOperatorStatus geometry_color_attribute_convert_invoke(bContext *C, wmOperator *op, const wmEvent *)
static bool geometry_color_attribute_convert_poll(bContext *C)
static wmOperatorStatus geometry_color_attribute_convert_exec(bContext *C, wmOperator *op)
static wmOperatorStatus geometry_attribute_remove_exec(bContext *C, wmOperator *op)
bool convert_attribute(AttributeOwner &owner, bke::MutableAttributeAccessor attributes, const StringRef name, const bke::AttrDomain dst_domain, const bke::AttrType dst_type, ReportList *reports)
static bool geometry_color_attributes_duplicate_poll(bContext *C)
static wmOperatorStatus geometry_attribute_add_exec(bContext *C, wmOperator *op)
static wmOperatorStatus geometry_attribute_convert_invoke(bContext *C, wmOperator *op, const wmEvent *)
void GEOMETRY_OT_color_attribute_remove(wmOperatorType *ot)
static wmOperatorStatus geometry_attribute_convert_exec(bContext *C, wmOperator *op)
static wmOperatorStatus geometry_color_attribute_add_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static wmOperatorStatus geometry_attribute_add_invoke(bContext *C, wmOperator *op, const wmEvent *event)
Object * context_object(const bContext *C)
bool object_active_color_init(Object &ob, const float fill_color[4])
void WM_main_add_notifier(uint type, void *reference)
std::optional< StringRefNull > BKE_attributes_active_name_get(AttributeOwner &owner)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
struct MDeformVert * BKE_object_defgroup_data_create(struct ID *id)
void RNA_def_enum_funcs(PropertyRNA *prop, EnumPropertyItemFunc itemfunc)
int * BKE_attributes_active_index_p(AttributeOwner &owner)
bool BKE_attribute_remove(AttributeOwner &owner, StringRef name, struct ReportList *reports)
void RNA_enum_item_add(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item)
int RNA_enum_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
void BKE_attributes_active_index_validate(AttributeOwner &owner)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void BKE_id_attributes_default_color_set(struct ID *id, std::optional< StringRef > name)
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)
PropertyRNA * RNA_def_float_array(StructOrFunctionRNA *cont_, const char *identifier, const int len, const float *default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
PropertyRNA * RNA_def_float_color(StructOrFunctionRNA *cont_, const char *identifier, const int len, const float *default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
std::optional< StringRef > BKE_id_attributes_active_color_name(const struct ID *id)
void RNA_property_enum_items(bContext *C, PointerRNA *ptr, PropertyRNA *prop, const EnumPropertyItem **r_item, int *r_totitem, bool *r_free)
const ListBaseT< bDeformGroup > * BKE_id_defgroup_list_get(const ID *id)
Definition deform.cc:472
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
int RNA_int_get(PointerRNA *ptr, const char *name)
void RNA_property_float_set_array(PointerRNA *ptr, PropertyRNA *prop, const float *values)
void BKE_attributes_active_set(AttributeOwner &owner, StringRef name)
std::string BKE_attribute_calc_unique_name(const AttributeOwner &owner, StringRef name)
void BKE_object_defgroup_active_index_set(Object *ob, int new_index)
Definition deform.cc:618
void RNA_def_property_float_array_default(PropertyRNA *prop, const float *array)
VecBase< float, 4 > float4
void RNA_property_int_set_array(PointerRNA *ptr, PropertyRNA *prop, const int *values)
void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
void RNA_int_get_array(PointerRNA *ptr, const char *name, int *values)
Object * CTX_data_edit_object(const bContext *C)
void RNA_def_property_subtype(PropertyRNA *prop, PropertySubType subtype)
const EnumPropertyItem rna_enum_attribute_type_items[]
BMDataLayerLookup BM_data_layer_lookup(const BMesh &bm, const StringRef name)
void RNA_enum_item_end(EnumPropertyItem **items, int *totitem)
wmOperatorStatus WM_operator_props_dialog_popup(bContext *C, wmOperator *op, int width, std::optional< std::string > title, std::optional< std::string > confirm_text, const bool cancel_default, std::optional< std::string > message, const bool show_icon)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
const EnumPropertyItem rna_enum_dummy_NULL_items[]
Definition rna_rna.cc:28
wmOperatorStatus WM_operator_props_popup_confirm_ex(bContext *C, wmOperator *op, const wmEvent *, std::optional< std::string > title, std::optional< std::string > confirm_text, const bool cancel_default, std::optional< std::string > message)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
Main * CTX_data_main(const bContext *C)
bDeformGroup * BKE_object_defgroup_new(Object *ob, StringRef name)
Definition deform.cc:51
VecBase< int16_t, 2 > short2
void BKE_id_attributes_active_color_set(struct ID *id, std::optional< StringRef > name)
void BKE_defvert_add_index_notest(MDeformVert *dv, int defgroup, float weight)
Definition deform.cc:861
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
ID_Type GS(const char *name)
Definition DNA_ID.h:410
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
const EnumPropertyItem rna_enum_color_attribute_domain_items[]
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
signed int int32_t
Definition BKE_text.h:78
PropertyRNA * RNA_def_int_array(StructOrFunctionRNA *cont_, const char *identifier, const int len, const int *default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, bool value)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_INTERNAL
Definition WM_types.hh:210
std::string RNA_string_get(PointerRNA *ptr, const char *name)
VecBase< float, 3 > float3
const EnumPropertyItem * rna_enum_attribute_domain_itemf(const AttributeOwner &owner, bool include_instances, bool *r_free)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
bool BKE_id_is_editable(const Main *bmain, const ID *id)
Definition lib_id.cc:2782
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
float RNA_float_get(PointerRNA *ptr, const char *name)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
const EnumPropertyItem rna_enum_attribute_domain_items[]
const EnumPropertyItem rna_enum_color_attribute_type_items[]
signed char int8_t
Definition BKE_text.h:76
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
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)
static ArrayData from_default_value(const CPPType &type, int64_t domain_size)
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) ui(bContext *C, wmOperator *op)
Definition WM_types.hh:1176
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports