Blender V5.3
curves_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"
16
17#include "WM_api.hh"
18#include "WM_types.hh"
19
20#include "ED_curves.hh"
21#include "ED_geometry.hh"
22#include "ED_object.hh"
23#include "ED_screen.hh"
24#include "ED_transform.hh"
25#include "ED_view3d.hh"
26
27#include "RNA_access.hh"
28
30#include "UI_resources.hh"
31
32#include "DNA_object_types.h"
33
34#include "DEG_depsgraph.hh"
35
36/* -------------------------------------------------------------------- */
39
40namespace blender::ed::curves {
41
43{
45 return false;
46 }
47 const Object *object = CTX_data_active_object(C);
48 const ID &object_data = *static_cast<const ID *>(object->data);
49 if (!geometry::attribute_set_poll(*C, object_data)) {
50 return false;
51 }
52 return true;
53}
54
56 const bke::AttrDomain domain,
57 IndexMaskMemory &memory)
58{
59 switch (domain) {
61 return retrieve_selected_points(curves_id, memory);
63 return retrieve_selected_curves(curves_id, memory);
64 default:
66 return {};
67 }
68}
69
70static void validate_value(const bke::AttributeAccessor attributes,
71 const StringRef name,
72 const CPPType &type,
73 void *buffer)
74{
75 const bke::AttributeValidator validator = attributes.lookup_validator(name);
76 if (!validator) {
77 return;
78 }
79 BUFFER_FOR_CPP_TYPE_VALUE(type, validated_buffer);
80 BLI_SCOPED_DEFER([&]() { type.destruct(validated_buffer); });
81
82 const IndexMask single_mask(1);
83 mf::ParamsBuilder params(*validator.function, &single_mask);
84 params.add_readonly_single_input(GPointer(type, buffer));
85 params.add_uninitialized_single_output({type, validated_buffer, 1});
86 mf::ContextBuilder context;
87 validator.function->call(single_mask, params, context);
88
89 type.copy_assign(validated_buffer, buffer);
90}
91
93{
94 Object *active_object = CTX_data_active_object(C);
95 Curves &active_curves_id = *id_cast<Curves *>(active_object->data);
96
97 AttributeOwner active_owner = AttributeOwner::from_id(&active_curves_id.id);
98 const StringRef name = *BKE_attributes_active_name_get(active_owner);
99 const bke::AttributeMetaData active_meta_data =
100 *active_curves_id.geometry.wrap().attributes().lookup_meta_data(name);
101 const bke::AttrType active_type = active_meta_data.data_type;
102 const CPPType &type = bke::attribute_type_to_cpp_type(active_type);
103
104 BUFFER_FOR_CPP_TYPE_VALUE(type, buffer);
105 BLI_SCOPED_DEFER([&]() { type.destruct(buffer); });
107 *op->ptr, active_type, buffer);
108
110
111 for (Curves *curves_id : get_unique_editable_curves(*C)) {
112 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
113 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
114 const std::optional<bke::AttributeMetaData> meta_data = attributes.lookup_meta_data(name);
115 if (!meta_data) {
116 continue;
117 }
118
119 IndexMaskMemory memory;
120 const IndexMask selection = retrieve_selected_elements(*curves_id, meta_data->domain, memory);
121 if (selection.is_empty()) {
122 continue;
123 }
124
125 /* Use implicit conversions to try to handle the case where the active attribute has a
126 * different type on multiple objects. */
127 const CPPType &dst_type = bke::attribute_type_to_cpp_type(meta_data->data_type);
128 if (&type != &dst_type && !conversions.is_convertible(type, dst_type)) {
129 continue;
130 }
131 BUFFER_FOR_CPP_TYPE_VALUE(dst_type, dst_buffer);
132 BLI_SCOPED_DEFER([&]() { dst_type.destruct(dst_buffer); });
133 conversions.convert_to_uninitialized(type, dst_type, value.get(), dst_buffer);
134
135 validate_value(attributes, name, dst_type, dst_buffer);
136 const GPointer dst_value(dst_type, dst_buffer);
137 if (selection.size() == attributes.domain_size(meta_data->domain)) {
138 if (attributes.assign_data(name, bke::AttributeInitValue(dst_value))) {
139 DEG_id_tag_update(&curves_id->id, ID_RECALC_GEOMETRY);
140 WM_event_add_notifier(C, NC_GEOM | ND_DATA, curves_id);
141 continue;
142 }
143 }
144
146 dst_type.fill_assign_indices(dst_value.get(), attribute.span.data(), selection);
147 attribute.finish();
148
149 DEG_id_tag_update(&curves_id->id, ID_RECALC_GEOMETRY);
150 WM_event_add_notifier(C, NC_GEOM | ND_DATA, curves_id);
151 }
152
153 return OPERATOR_FINISHED;
154}
155
157{
158 Object *active_object = CTX_data_active_object(C);
159 Curves &active_curves_id = *id_cast<Curves *>(active_object->data);
160
161 AttributeOwner owner = AttributeOwner::from_id(&active_curves_id.id);
162 const StringRef name = *BKE_attributes_active_name_get(owner);
163 const bke::CurvesGeometry &curves = active_curves_id.geometry.wrap();
164 const bke::AttributeAccessor attributes = curves.attributes();
165 const bke::GAttributeReader attribute = attributes.lookup(name);
166 const bke::AttrDomain domain = attribute.domain;
167
168 IndexMaskMemory memory;
169 const IndexMask selection = retrieve_selected_elements(active_curves_id, domain, memory);
170
171 const CPPType &type = attribute.varray.type();
172
175 if (RNA_property_is_set(op->ptr, prop)) {
176 return WM_operator_props_popup(C, op, event);
177 }
178
179 BUFFER_FOR_CPP_TYPE_VALUE(type, buffer);
180 BLI_SCOPED_DEFER([&]() { type.destruct(buffer); });
181
182 bke::attribute_math::to_static_type(type, [&]<typename T>() {
183 if constexpr (!std::is_void_v<bke::attribute_math::DefaultMixer<T>>) {
184 const VArray<T> values_typed = attribute.varray.typed<T>();
185 bke::attribute_math::DefaultMixer<T> mixer{MutableSpan(static_cast<T *>(buffer), 1)};
186 selection.foreach_index([&](const int i) { mixer.mix_in(0, values_typed[i]); });
187 mixer.finalize();
188 }
189 });
190
192
193 return WM_operator_props_popup(C, op, event);
194}
195
197{
198 ui::Layout &layout = op->layout->column(true);
199 layout.use_property_split_set(true);
200 layout.use_property_decorate_set(false);
201
203 Curves &curves_id = *id_cast<Curves *>(object->data);
204
205 AttributeOwner owner = AttributeOwner::from_id(&curves_id.id);
206 const StringRef name = *BKE_attributes_active_name_get(owner);
207 const bke::CurvesGeometry &curves = curves_id.geometry.wrap();
208 const bke::AttributeMetaData meta_data = *curves.attributes().lookup_meta_data(name);
210 layout.prop(op->ptr, prop_name, UI_ITEM_NONE, name, ICON_NONE);
211}
212
214{
215 using namespace blender::ed;
216 using namespace blender::ed::curves;
217 ot->name = "Set Attribute";
218 ot->description = "Set values of the active attribute for selected elements";
219 ot->idname = "CURVES_OT_attribute_set";
220
224 ot->ui = set_attribute_ui;
225
227
229}
230
231} // namespace blender::ed::curves
232
Generic geometry attributes built on CustomData.
#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
#define BLI_assert_unreachable()
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)
static IndexMask retrieve_selected_elements(const Curves &curves_id, const bke::AttrDomain domain, IndexMaskMemory &memory)
IndexMask retrieve_selected_points(const bke::CurvesGeometry &curves, LinearAllocator<> &memory)
VectorSet< Curves * > get_unique_editable_curves(const bContext &C)
static void validate_value(const bke::AttributeAccessor attributes, const StringRef name, const CPPType &type, void *buffer)
bool editable_curves_in_edit_mode_poll(bContext *C)
static bool active_attribute_poll(bContext *C)
IndexMask retrieve_selected_curves(const bke::CurvesGeometry &curves, LinearAllocator<> &memory)
static void set_attribute_ui(bContext *C, wmOperator *op)
static wmOperatorStatus set_attribute_exec(bContext *C, wmOperator *op)
void CURVES_OT_attribute_set(wmOperatorType *ot)
static wmOperatorStatus set_attribute_invoke(bContext *C, wmOperator *op, const wmEvent *event)
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)
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