Blender V5.3
node_geo_mesh_bevel.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "DNA_curves_types.h"
6#include "DNA_mesh_types.h"
7
8#include "BKE_curves.hh"
9#include "BKE_geometry_set.hh"
10
11#include "NOD_rna_define.hh"
12
14#include "GEO_mesh_bevel.hh"
15
16#include "UI_interface.hh"
17#include "UI_resources.hh"
18
19#include "node_geometry_util.hh"
20
22
25 "VERTICES",
26 0,
27 N_("Vertices"),
28 N_("Bevel affects vertices")},
29 {int(geometry::BevelAffect::Edges), "EDGES", 0, N_("Edges"), N_("Bevel affects edges")},
30 {0, nullptr, 0, nullptr, nullptr}};
31
33{
34 b.use_custom_socket_order();
35 b.allow_any_socket_order();
36
37 b.add_input<decl::Geometry>("Mesh"_ustr).supported_type(GeometryComponent::Type::Mesh);
38 b.add_output<decl::Geometry>("Mesh"_ustr).propagate_all_geometry().align_with_previous();
39 b.add_input<decl::Bool>("Selection"_ustr)
40 .default_value(true)
41 .hide_value()
42 .evaluated_geometry_field()
43 .description("Selects elements of 'Affect Kind' for beveling");
44 b.add_input<decl::Menu>("Affect Kind"_ustr)
45 .default_value(geometry::BevelAffect::Edges)
46 .static_items(affect_items)
48 b.add_input<decl::Float>("Start Left Offset"_ustr)
49 .default_value(0.1f)
50 .min(0.0f)
52 .evaluated_geometry_field()
53 .usage_by_single_menu(int(geometry::BevelAffect::Edges))
54 .description("Offset for left side of source of edge, viewed from source");
55 b.add_input<decl::Float>("Start Right Offset"_ustr)
56 .default_value(0.1f)
57 .min(0.0f)
59 .evaluated_geometry_field()
60 .usage_by_single_menu(int(geometry::BevelAffect::Edges))
61 .description("Offset for right side of source of edge, viewed from source");
62 b.add_input<decl::Float>("End Left Offset"_ustr)
63 .default_value(0.1f)
64 .min(0.0f)
66 .evaluated_geometry_field()
67 .usage_by_single_menu(int(geometry::BevelAffect::Edges))
68 .description("Offset for left side of destination of edge, viewed from source");
69 b.add_input<decl::Float>("End Right Offset"_ustr)
70 .default_value(0.1f)
71 .min(0.0f)
73 .evaluated_geometry_field()
74 .usage_by_single_menu(int(geometry::BevelAffect::Edges))
75 .description("Offset for right side of destination of edge, viewed from source");
76 b.add_input<decl::Float>("Offset"_ustr)
77 .default_value(0.1f)
78 .min(0.0f)
80 .evaluated_geometry_field()
81 .usage_by_single_menu(int(geometry::BevelAffect::Vertices))
82 .description("Offset per vertex for vertex bevel (all edges)");
83 b.add_input<decl::Bool>("Miter"_ustr)
84 .default_value(false)
85 .evaluated_geometry_field()
86 .description("Use a miter for corner");
87 b.add_input<decl::Float>("Spread"_ustr)
88 .min(0.0f)
89 .default_value(0.1f)
90 .subtype(PROP_DISTANCE)
91 .evaluated_geometry_field()
92 .description("Per corner specification of 'spread' for arc miters")
93 .usage_by_bool("Miter"_ustr, true);
94 b.add_input<decl::Int>("Segments"_ustr)
95 .min(1)
96 .default_value(1)
97 .description(
98 "How many pieces is an edge beveled into, "
99 "or, for vertex bevels, how many segments on the arcs between the edges.");
100 b.add_input<decl::Float>("Shape"_ustr)
101 .default_value(0.5f)
102 .min(0.0f)
103 .max(1.0f)
105 .description(
106 "Superellipse shape parameter, used when there is no Profile, "
107 "and also used for Arc and Patch miters");
108 b.add_input<decl::Geometry>("Profile"_ustr)
109 .supported_type(GeometryComponent::Type::Curve)
111 "If present, the first curve will be sampled to give a custom profile on edges. "
112 "The curve should be in the XY plane, going from (0,1,0) to (1,0,0)");
113
114 PanelDeclarationBuilder &selections_panel = b.add_panel("Selections"_ustr);
115 selections_panel.default_closed(true);
116 selections_panel.add_output<decl::Bool>("Vertex Face"_ustr)
117 .anonymous_attribute_output()
118 .no_muted_links()
119 .description("Identifies output faces that are in the new mesh parts for vertices");
120 selections_panel.add_output<decl::Bool>("Edge Face"_ustr)
121 .anonymous_attribute_output()
122 .no_muted_links()
123 .description("Identifies output faces that are in the new mesh parts for edges");
124 selections_panel.add_output<decl::Bool>("Outer Edge"_ustr)
125 .anonymous_attribute_output()
126 .no_muted_links()
127 .description("Identifies output edges that are on the outsides of new mesh parts for edges");
128 selections_panel.add_output<decl::Bool>("Mid Edge"_ustr)
129 .anonymous_attribute_output()
130 .no_muted_links()
132 "Identifies output edges that are in the middle of new mesh parts of edges "
133 "and continued through vertices (round down if odd number of segments)");
134}
135
136/* -------------------------------------------------------------------- */
146
152{
153 if (curves.curves_num() == 0) {
154 return {};
155 }
156
157 /* Use the evaluator to get dense positions that correctly represent the curve shape
158 * regardless of type (Bezier, Catmull-Rom, poly, NURBS). */
159 const OffsetIndices<int> eval_by_curve = curves.evaluated_points_by_curve();
160 const Span<float3> eval_positions = curves.evaluated_positions();
161 const IndexRange eval_pts = eval_by_curve[0];
162
163 if (eval_pts.size() < 2) {
164 return {};
165 }
166
167 Array<float2> samples(eval_pts.size());
168 for (const int i : eval_pts.index_range()) {
169 const float3 &p = eval_positions[eval_pts[i]];
170 samples[i] = float2(p.x, p.y);
171 }
172 return samples;
173}
174
176
178{
179 GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh"_ustr);
180 const Field<bool> selection_field = params.extract_input<Field<bool>>("Selection"_ustr);
181 const AttributeFilter &attribute_filter = params.get_attribute_filter("Mesh"_ustr);
182 const int segments = params.extract_input<int>("Segments"_ustr);
183 if (segments < 1) {
184 params.error_message_add(NodeWarningType::Error, "Segments must be at least 1");
185 params.set_output("Mesh"_ustr, std::move(geometry_set));
187 return;
188 }
189 const auto affect = params.extract_input<geometry::BevelAffect>("Affect Kind"_ustr);
190
191 const Field<float> offset0_field = affect == geometry::BevelAffect::Edges ?
193 "Start Left Offset"_ustr) :
194 params.extract_input<Field<float>>("Offset"_ustr);
195 const Field<float> offset1_field = params.extract_input<Field<float>>("Start Right Offset"_ustr);
196 const Field<float> offset2_field = params.extract_input<Field<float>>("End Left Offset"_ustr);
197 const Field<float> offset3_field = params.extract_input<Field<float>>("End Right Offset"_ustr);
198
199 const Field<bool> miter_field = params.extract_input<Field<bool>>("Miter"_ustr);
200 const Field<float> spread_field = params.extract_input<Field<float>>("Spread"_ustr);
201 const float shape = params.extract_input<float>("Shape"_ustr);
202
203 /* Sample the Profile input curve into a flat float2 array.
204 * The array is built once here and shared across all instances in the geometry loop;
205 * BevelParameters holds it by const reference (non-owning Span). */
206 const GeometrySet profile_geometry = params.extract_input<GeometrySet>("Profile"_ustr);
207 Array<float2> profile_samples;
208 if (const Curves *profile_curves_id = profile_geometry.get_curves()) {
209 const bke::CurvesGeometry &profile_geom = profile_curves_id->geometry.wrap();
210 profile_samples = sample_profile_curve(profile_geom);
211 }
212
213 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
214 const Mesh *src_mesh = geometry_set.get_mesh();
215 if (!src_mesh) {
216 return;
217 }
218 geometry::BevelParameters bevel_params;
219 bevel_params.affect_type = affect;
220 bevel_params.segments = segments;
221 bevel_params.shape = shape;
222 /* Move the samples into bevel_params (zero-copy; profile_samples stays valid
223 * through the geometry loop because it is declared in this outer scope). */
224 bevel_params.custom_profile_samples = profile_samples;
225 const int ne = src_mesh->edges_num;
226
227 /* Shared logic executed after the selection/offset evaluator (and its lifetime) is set up.
228 * The `selection` reference must not outlive `evaluator`. */
229 auto run_bevel = [&](const IndexMask &selection) {
230 if (selection.is_empty()) {
231 return;
232 }
233
234 const bke::MeshFieldContext corner_context(*src_mesh, AttrDomain::Corner);
235 FieldEvaluator corner_evaluator{corner_context, src_mesh->corners_num};
236 /* TODO: make this more efficient in usual case of no miters. */
237 bevel_params.miter = Array<bool>(src_mesh->corners_num);
238 bevel_params.spread = Array<float>(src_mesh->corners_num);
239 corner_evaluator.add_with_destination(miter_field, bevel_params.miter.as_mutable_span());
240 corner_evaluator.add_with_destination(spread_field, bevel_params.spread.as_mutable_span());
241 corner_evaluator.evaluate();
242
243 bevel_params.attribute_outputs.vertex_face_id =
244 params.get_output_anonymous_attribute_id_if_needed("Vertex Face"_ustr);
245 bevel_params.attribute_outputs.edge_face_id =
246 params.get_output_anonymous_attribute_id_if_needed("Edge Face"_ustr);
247 bevel_params.attribute_outputs.outer_edge_id =
248 params.get_output_anonymous_attribute_id_if_needed("Outer Edge"_ustr);
249 bevel_params.attribute_outputs.mid_edge_id =
250 params.get_output_anonymous_attribute_id_if_needed("Mid Edge"_ustr);
251
252 std::optional<Mesh *> mesh = geometry::mesh_bevel(
253 *src_mesh, selection, bevel_params, attribute_filter);
254 if (!mesh) {
255 return;
256 }
257 geometry_set.replace_mesh(*mesh);
258 };
259
260 if (affect == geometry::BevelAffect::Vertices) {
261 /* Vertex bevel: selection and offset0 are per-vertex.
262 * offsets[0][v] is the slide distance at vertex v; offsets[1..3] are unused. */
263 const int nv = src_mesh->verts_num;
264 bevel_params.offsets = {Array<float>(nv), Array<float>(0), Array<float>(0), Array<float>(0)};
265
266 const bke::MeshFieldContext vert_context(*src_mesh, AttrDomain::Point);
267 FieldEvaluator vert_evaluator{vert_context, nv};
268 vert_evaluator.add(selection_field);
269 vert_evaluator.add_with_destination(offset0_field,
270 bevel_params.offsets[0].as_mutable_span());
271 vert_evaluator.evaluate();
272
273 /* Pass to run_bevel while vert_evaluator is still alive. */
274 run_bevel(vert_evaluator.get_evaluated_as_mask(0));
275 }
276 else {
277 /* Edge bevel: selection and all four offsets are per-edge. */
278 bevel_params.offsets = {
280
281 const bke::MeshFieldContext edge_context(*src_mesh, AttrDomain::Edge);
282 FieldEvaluator edge_evaluator{edge_context, ne};
283 edge_evaluator.add(selection_field);
284 edge_evaluator.add_with_destination(offset0_field,
285 bevel_params.offsets[0].as_mutable_span());
286 edge_evaluator.add_with_destination(offset1_field,
287 bevel_params.offsets[1].as_mutable_span());
288 edge_evaluator.add_with_destination(offset2_field,
289 bevel_params.offsets[2].as_mutable_span());
290 edge_evaluator.add_with_destination(offset3_field,
291 bevel_params.offsets[3].as_mutable_span());
292 edge_evaluator.evaluate();
293
294 /* Pass to run_bevel while edge_evaluator is still alive. */
295 run_bevel(edge_evaluator.get_evaluated_as_mask(0));
296 }
297 });
298
299 params.set_output("Mesh"_ustr, std::move(geometry_set));
300}
301
302static void node_register()
303{
304 static blender::bke::bNodeType ntype;
305
306 geo_node_type_base(&ntype, "GeometryNodeMeshBevel"_ustr, std::nullopt);
307 ntype.ui_name = "Mesh Bevel";
308 ntype.ui_description = "Bevel selected edges or vertices";
310 ntype.declare = node_declare;
314}
316
317} // namespace blender::nodes::node_geo_bevel_cc
Low-level operations for curves.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define NOD_REGISTER_NODE(REGISTER_FUNC)
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
constexpr int64_t size() const
constexpr IndexRange index_range() const
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_as_mask(int field_index)
int add_with_destination(GField field, GVMutableArray dst)
DeclType::Builder & add_output(UString name, UString identifier=""_ustr)
void error_message_add(const NodeWarningType type, StringRef message) const
NodeAttributeFilter get_attribute_filter(const UString output_identifier) const
std::optional< std::string > get_output_anonymous_attribute_id_if_needed(const UString output_identifier, const bool force_create=false)
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
std::optional< Mesh * > mesh_bevel(const Mesh &src_mesh, const IndexMask &selection, const BevelParameters &params, const bke::AttributeFilter &attribute_filter)
static Array< float2 > sample_profile_curve(const bke::CurvesGeometry &curves)
static void node_declare(NodeDeclarationBuilder &b)
static const EnumPropertyItem affect_items[]
static void node_geo_exec(GeoNodeExecParams params)
VecBase< float, 2 > float2
VecBase< float, 3 > float3
@ PROP_FACTOR
Definition RNA_types.hh:308
@ PROP_DISTANCE
Definition RNA_types.hh:313
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define min(a, b)
Definition sort.cc:38
const Mesh * get_mesh() const
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
static constexpr int _180
Definition BKE_node.hh:247
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
NodeDeclareFunction declare
Definition BKE_node.hh:393
std::optional< std::string > edge_face_id
std::optional< std::string > vertex_face_id
std::optional< std::string > mid_edge_id
std::optional< std::string > outer_edge_id
std::array< Array< float >, 4 > offsets
geometry::BevelAttributeOutputs attribute_outputs
#define N_(msgid)