Blender V5.3
attribute_set.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
10
11#include "BKE_attribute.h"
12#include "BKE_attribute.hh"
13#include "BKE_attribute_math.hh"
14#include "BKE_context.hh"
15#include "BKE_pointcloud.hh"
17
18#include "WM_api.hh"
19#include "WM_types.hh"
20
21#include "ED_geometry.hh"
22#include "ED_object.hh"
23#include "ED_pointcloud.hh"
24#include "ED_screen.hh"
25#include "ED_transform.hh"
26#include "ED_view3d.hh"
27
28#include "RNA_access.hh"
29
31#include "UI_resources.hh"
32
33#include "DNA_object_types.h"
34
35#include "FN_multi_function.hh"
36
37#include "DEG_depsgraph.hh"
38
39/* -------------------------------------------------------------------- */
42
44
46{
48 return false;
49 }
50 const Object *object = CTX_data_active_object(C);
51 const ID &object_data = *static_cast<const ID *>(object->data);
52 if (!geometry::attribute_set_poll(*C, object_data)) {
53 return false;
54 }
55 return true;
56}
57
58static void validate_value(const bke::AttributeAccessor attributes,
59 const StringRef name,
60 const CPPType &type,
61 void *buffer)
62{
63 const bke::AttributeValidator validator = attributes.lookup_validator(name);
64 if (!validator) {
65 return;
66 }
67 BUFFER_FOR_CPP_TYPE_VALUE(type, validated_buffer);
68 BLI_SCOPED_DEFER([&]() { type.destruct(validated_buffer); });
69
70 const IndexMask single_mask(1);
71 mf::ParamsBuilder params(*validator.function, &single_mask);
72 params.add_readonly_single_input(GPointer(type, buffer));
73 params.add_uninitialized_single_output({type, validated_buffer, 1});
74 mf::ContextBuilder context;
75 validator.function->call(single_mask, params, context);
76
77 type.copy_assign(validated_buffer, buffer);
78}
79
81{
82 Object *active_object = CTX_data_active_object(C);
83 PointCloud &active_pointcloud = *id_cast<PointCloud *>(active_object->data);
84
85 AttributeOwner active_owner = AttributeOwner::from_id(&active_pointcloud.id);
86 const StringRef name = *BKE_attributes_active_name_get(active_owner);
87 const bke::AttributeMetaData meta_data = *active_pointcloud.attributes().lookup_meta_data(name);
88 const bke::AttrType active_type = meta_data.data_type;
89 const CPPType &type = bke::attribute_type_to_cpp_type(active_type);
90
91 BUFFER_FOR_CPP_TYPE_VALUE(type, buffer);
92 BLI_SCOPED_DEFER([&]() { type.destruct(buffer); });
94 *op->ptr, active_type, buffer);
95
97
99 bke::MutableAttributeAccessor attributes = pointcloud->attributes_for_write();
100 const std::optional<bke::AttributeMetaData> meta_data = attributes.lookup_meta_data(name);
101 if (!meta_data) {
102 continue;
103 }
104
105 IndexMaskMemory memory;
106 const IndexMask selection = retrieve_selected_points(*pointcloud, memory);
107 if (selection.is_empty()) {
108 continue;
109 }
110
111 /* Use implicit conversions to try to handle the case where the active attribute has a
112 * different type on multiple objects. */
113 const CPPType &dst_type = bke::attribute_type_to_cpp_type(meta_data->data_type);
114 if (&type != &dst_type && !conversions.is_convertible(type, dst_type)) {
115 continue;
116 }
117 BUFFER_FOR_CPP_TYPE_VALUE(dst_type, dst_buffer);
118 BLI_SCOPED_DEFER([&]() { dst_type.destruct(dst_buffer); });
119 conversions.convert_to_uninitialized(type, dst_type, value.get(), dst_buffer);
120
121 validate_value(attributes, name, dst_type, dst_buffer);
122 const GPointer dst_value(dst_type, dst_buffer);
123 if (selection.size() == attributes.domain_size(meta_data->domain)) {
124 if (attributes.assign_data(name, bke::AttributeInitValue(dst_value))) {
127 continue;
128 }
129 }
130
132 dst_type.fill_assign_indices(dst_value.get(), attribute.span.data(), selection);
133 attribute.finish();
134
137 }
138
139 return OPERATOR_FINISHED;
140}
141
143{
144 Object *active_object = CTX_data_active_object(C);
145 PointCloud &active_pointcloud = *id_cast<PointCloud *>(active_object->data);
146
147 AttributeOwner owner = AttributeOwner::from_id(&active_pointcloud.id);
148 const bke::AttributeAccessor attributes = active_pointcloud.attributes();
149 const StringRef name = *BKE_attributes_active_name_get(owner);
150 const bke::GAttributeReader attribute = attributes.lookup(name);
151
152 IndexMaskMemory memory;
153 const IndexMask selection = retrieve_selected_points(active_pointcloud, memory);
154
155 const CPPType &type = attribute.varray.type();
156
159 if (RNA_property_is_set(op->ptr, prop)) {
160 return WM_operator_props_popup(C, op, event);
161 }
162
163 BUFFER_FOR_CPP_TYPE_VALUE(type, buffer);
164 BLI_SCOPED_DEFER([&]() { type.destruct(buffer); });
165
166 bke::attribute_math::to_static_type(type, [&]<typename T>() {
167 if constexpr (!std::is_void_v<bke::attribute_math::DefaultMixer<T>>) {
168 const VArray<T> values_typed = attribute.varray.typed<T>();
169 bke::attribute_math::DefaultMixer<T> mixer{MutableSpan(static_cast<T *>(buffer), 1)};
170 selection.foreach_index([&](const int i) { mixer.mix_in(0, values_typed[i]); });
171 mixer.finalize();
172 }
173 });
174
176
177 return WM_operator_props_popup(C, op, event);
178}
179
181{
182 ui::Layout &layout = op->layout->column(true);
183 layout.use_property_split_set(true);
184 layout.use_property_decorate_set(false);
185
187 PointCloud &pointcloud = *id_cast<PointCloud *>(object->data);
188
190 const StringRef name = *BKE_attributes_active_name_get(owner);
191 const bke::AttributeMetaData meta_data = *pointcloud.attributes().lookup_meta_data(name);
193 layout.prop(op->ptr, prop_name, UI_ITEM_NONE, name, ICON_NONE);
194}
195
197{
198 ot->name = "Set Attribute";
199 ot->description = "Set values of the active attribute for selected elements";
200 ot->idname = "POINTCLOUD_OT_attribute_set";
201
205 ot->ui = set_attribute_ui;
206
208
210}
211
212} // namespace blender::ed::pointcloud
213
Generic geometry attributes built on CustomData.
General operations for point clouds.
#define BUFFER_FOR_CPP_TYPE_VALUE(type, variable_name)
#define BLI_SCOPED_DEFER(function_to_defer)
Object is a sort of wrapper for general info.
#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
static AttributeOwner from_id(ID *id)
static const CPPType & get()
void fill_assign_indices(const void *value, void *dst, const IndexMask &mask) const
void destruct(void *ptr) const
void copy_assign(const void *src, void *dst) const
GPointer()=default
const void * get() const
constexpr MutableSpan()=default
AttributeValidator lookup_validator(const StringRef name) const
std::optional< AttributeMetaData > lookup_meta_data(StringRef name) const
GAttributeReader lookup(const StringRef name) const
int domain_size(const AttrDomain domain) const
void convert_to_uninitialized(const CPPType &from_type, const CPPType &to_type, const void *from_value, void *to_value) const
bool is_convertible(const CPPType &from_type, const CPPType &to_type) const
bool assign_data(const StringRef name, const AttributeInit &initializer)
GSpanAttributeWriter lookup_for_write_span(StringRef name)
virtual void call(const IndexMask &mask, Params params, Context context) const =0
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
#define attribute(slot)
#define T
typename DefaultMixerStruct< T >::type DefaultMixer
void to_static_type(const CPPType &cpp_type, Fn &&fn)
const DataTypeConversions & get_implicit_type_conversions()
AttrType cpp_type_to_attribute_type(const CPPType &type)
const CPPType & attribute_type_to_cpp_type(AttrType type)
PropertyRNA * rna_property_for_type(PointerRNA &ptr, const bke::AttrType type)
GPointer rna_property_for_attribute_type_retrieve_value(PointerRNA &ptr, const bke::AttrType type, void *buffer)
StringRefNull rna_property_name_for_type(const bke::AttrType type)
void register_rna_properties_for_attribute_types(StructRNA &srna)
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)
bool editable_pointcloud_in_edit_mode_poll(bContext *C)
Definition operators.cc:73
static void validate_value(const bke::AttributeAccessor attributes, const StringRef name, const CPPType &type, void *buffer)
static bool active_attribute_poll(bContext *C)
static void set_attribute_ui(bContext *C, wmOperator *op)
static wmOperatorStatus set_attribute_exec(bContext *C, wmOperator *op)
static wmOperatorStatus set_attribute_invoke(bContext *C, wmOperator *op, const wmEvent *event)
IndexMask retrieve_selected_points(const PointCloud &pointcloud, IndexMaskMemory &memory)
Definition selection.cc:274
void POINTCLOUD_OT_attribute_set(wmOperatorType *ot)
VectorSet< PointCloud * > get_unique_editable_pointclouds(const bContext &C)
Definition operators.cc:78
std::optional< StringRefNull > BKE_attributes_active_name_get(AttributeOwner &owner)
void DEG_id_tag_update(ID *id, unsigned int flags)
wmOperatorStatus WM_operator_props_popup(bContext *C, wmOperator *op, const wmEvent *)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
Object * CTX_data_active_object(const bContext *C)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
const fn::multi_function::MultiFunction * function
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)
Layout & column(bool align)
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