Blender V5.3
editmesh_attribute.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.hh"
13#include "BKE_context.hh"
14#include "BKE_editmesh.hh"
15#include "BKE_layer.hh"
16#include "BKE_mesh.hh"
18
19#include "WM_api.hh"
20#include "WM_types.hh"
21
22#include "RNA_access.hh"
23#include "RNA_enum_types.hh"
24
25#include "ED_geometry.hh"
26#include "ED_mesh.hh"
27#include "ED_object.hh"
28#include "ED_screen.hh"
29#include "ED_transform.hh"
30#include "ED_view3d.hh"
31
32#include "DNA_object_types.h"
33
35#include "UI_resources.hh"
36
37#include "mesh_intern.hh"
38
39namespace blender {
40
41/* -------------------------------------------------------------------- */
44
45namespace ed::mesh {
46
47static char domain_to_htype(const bke::AttrDomain domain)
48{
49 switch (domain) {
51 return BM_VERT;
53 return BM_EDGE;
55 return BM_FACE;
57 return BM_LOOP;
58 default:
60 return BM_VERT;
61 }
62}
63
65{
66 if (!ED_operator_editmesh(C)) {
67 return false;
68 }
69 const Mesh *mesh = ED_mesh_context(C);
70 if (mesh == nullptr) {
71 return false;
72 }
73 if (!mesh->runtime->edit_mesh) {
74 return false;
75 }
77 return false;
78 }
79 return true;
80}
81
82namespace set_attribute {
83
85 const BMIterType iter_type,
86 const GPointer value,
87 const int offset)
88{
89 const CPPType &type = *value.type();
90 BMIter iter;
91 BMElem *elem;
92 BM_ITER_MESH (elem, &iter, &bm, iter_type) {
94 type.copy_assign(value.get(), POINTER_OFFSET(elem->head.data, offset));
95 }
96 }
97}
98
104 const short selectmode,
105 const GPointer value,
106 const int offset)
107{
108 /* In the separate select modes we may set the same loop values more than once.
109 * This is okay because we're always setting the same value. */
110 const CPPType &type = *value.type();
111 if (selectmode & SCE_SELECT_FACE) {
112 BMIter face_iter;
113 BMFace *face;
114 BM_ITER_MESH (face, &face_iter, &bm, BM_FACES_OF_MESH) {
116 BMIter loop_iter;
117 BMLoop *loop;
118 BM_ITER_ELEM (loop, &loop_iter, face, BM_LOOPS_OF_FACE) {
119 type.copy_assign(value.get(), POINTER_OFFSET(loop->head.data, offset));
120 }
121 }
122 }
123 }
124 if (selectmode & (SCE_SELECT_VERTEX | SCE_SELECT_EDGE)) {
125 BMIter vert_iter;
126 BMVert *vert;
127 BM_ITER_MESH (vert, &vert_iter, &bm, BM_VERTS_OF_MESH) {
129 BMIter loop_iter;
130 BMLoop *loop;
131 BM_ITER_ELEM (loop, &loop_iter, vert, BM_LOOPS_OF_VERT) {
132 type.copy_assign(value.get(), POINTER_OFFSET(loop->head.data, offset));
133 }
134 }
135 }
136 }
137}
138
140{
141 const Main *bmain = CTX_data_main(C);
142 const Scene *scene = CTX_data_scene(C);
143 ViewLayer *view_layer = CTX_data_view_layer(C);
144
146 *bmain, scene, view_layer, CTX_wm_view3d(C));
147
148 Mesh *active_mesh = ED_mesh_context(C);
149 BMesh *active_bm = BKE_editmesh_bmesh_get_for_write(active_mesh);
150 AttributeOwner active_owner = AttributeOwner::from_id(&active_mesh->id);
151 const StringRef name = *BKE_attributes_active_name_get(active_owner);
152 const BMDataLayerLookup active_attr = BM_data_layer_lookup(*active_bm, name);
153 const bke::AttrType active_type = active_attr.type;
154 const CPPType &type = bke::attribute_type_to_cpp_type(active_type);
155
156 BUFFER_FOR_CPP_TYPE_VALUE(type, buffer);
157 BLI_SCOPED_DEFER([&]() { type.destruct(buffer); });
159 *op->ptr, active_type, buffer);
160
162
163 bool changed = false;
164 for (Object *object : objects) {
165 Mesh *mesh = id_cast<Mesh *>(object->data);
169 if (!attr) {
170 continue;
171 }
172 /* Use implicit conversions to try to handle the case where the active attribute has a
173 * different type on multiple objects. */
174 const CPPType &dst_type = bke::attribute_type_to_cpp_type(attr.type);
175 if (&type != &dst_type && !conversions.is_convertible(type, dst_type)) {
176 continue;
177 }
178 BUFFER_FOR_CPP_TYPE_VALUE(dst_type, dst_buffer);
179 BLI_SCOPED_DEFER([&]() { dst_type.destruct(dst_buffer); });
180 conversions.convert_to_uninitialized(type, dst_type, value.get(), dst_buffer);
181 const GPointer dst_value(dst_type, dst_buffer);
182 switch (attr.domain) {
185 *bm, BM_VERTS_OF_MESH, dst_value, attr.offset);
186 break;
189 *bm, BM_EDGES_OF_MESH, dst_value, attr.offset);
190 break;
193 *bm, BM_FACES_OF_MESH, dst_value, attr.offset);
194 break;
197 break;
198 default:
200 break;
201 }
202
203 changed = true;
204 EDBMUpdate_Params update{};
205 update.calc_looptris = false;
206 update.calc_normals = false;
207 update.is_destructive = false;
208 EDBM_update(mesh, &update);
209 }
210
211 return changed ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
212}
213
215 wmOperator *op,
216 const wmEvent *event)
217{
221
222 const StringRef name = *BKE_attributes_active_name_get(owner);
223 const BMDataLayerLookup attr = BM_data_layer_lookup(*bm, name);
224 const bke::AttrType data_type = attr.type;
225 const bke::AttrDomain domain = attr.domain;
226 const BMElem *active_elem = BM_mesh_active_elem_get(bm);
227 if (!active_elem) {
228 return WM_operator_props_popup(C, op, event);
229 }
230
231 /* Only support filling the active data when the active selection mode matches the active
232 * attribute domain. NOTE: This doesn't work well for corner domain attributes. */
233 if (active_elem->head.htype != domain_to_htype(domain)) {
234 return WM_operator_props_popup(C, op, event);
235 }
236
237 const CPPType &type = bke::attribute_type_to_cpp_type(data_type);
238 const GPointer active_value(type, POINTER_OFFSET(active_elem->head.data, attr.offset));
239
240 PropertyRNA *prop = geometry::rna_property_for_type(*op->ptr, data_type);
241 if (!RNA_property_is_set(op->ptr, prop)) {
243 }
244
245 return WM_operator_props_popup(C, op, event);
246}
247
249{
250 ui::Layout &layout = op->layout->column(true);
251 layout.use_property_split_set(true);
252 layout.use_property_decorate_set(false);
253
257 const StringRef name = *BKE_attributes_active_name_get(owner);
258 const BMDataLayerLookup attr = BM_data_layer_lookup(*bm, name);
260 layout.prop(op->ptr, prop_name, UI_ITEM_NONE, name, ICON_NONE);
261}
262
263} // namespace set_attribute
264
265} // namespace ed::mesh
266
268{
269 using namespace blender::ed::mesh;
270 using namespace blender::ed::mesh::set_attribute;
271 ot->name = "Set Attribute";
272 ot->description = "Set values of the active attribute for selected elements";
273 ot->idname = "MESH_OT_attribute_set";
274
279
281
283}
284
286
287} // namespace blender
#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 BLI_assert_unreachable()
#define POINTER_OFFSET(v, ofs)
#define BM_elem_flag_test(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
static AttributeOwner from_id(ID *id)
static const CPPType & get()
void destruct(void *ptr) const
void copy_assign(const void *src, void *dst) const
const CPPType * type() const
const void * get() 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
const DataTypeConversions & get_implicit_type_conversions()
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)
static void mesh_set_attribute_ui(bContext *C, wmOperator *op)
static wmOperatorStatus mesh_set_attribute_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static wmOperatorStatus mesh_set_attribute_exec(bContext *C, wmOperator *op)
static void bmesh_loop_layer_selected_values_set(BMesh &bm, const short selectmode, const GPointer value, const int offset)
static void bmesh_vert_edge_face_layer_selected_values_set(BMesh &bm, const BMIterType iter_type, const GPointer value, const int offset)
static char domain_to_htype(const bke::AttrDomain domain)
static bool mesh_active_attribute_poll(bContext *C)
BMesh Iterators.
std::optional< StringRefNull > BKE_attributes_active_name_get(AttributeOwner &owner)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
void EDBM_update(Mesh *mesh, const EDBMUpdate_Params *params)
ViewLayer * CTX_data_view_layer(const bContext *C)
bool ED_operator_editmesh(bContext *C)
void MESH_OT_attribute_set(wmOperatorType *ot)
wmOperatorStatus WM_operator_props_popup(bContext *C, wmOperator *op, const wmEvent *)
BMDataLayerLookup BM_data_layer_lookup(const BMesh &bm, const StringRef name)
BMEditMesh * BKE_editmesh_from_object(Object *ob)
Return the BMEditMesh for a given object.
Definition editmesh.cc:59
Mesh * ED_mesh_context(bContext *C)
Main * CTX_data_main(const bContext *C)
BMElem * BM_mesh_active_elem_get(BMesh *bm)
Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode_unique_data(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
View3D * CTX_wm_view3d(const bContext *C)
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