Blender V5.0
node_geo_set_curve_normal.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
5#include "BKE_curves.hh"
7
8#include "RNA_enum_types.hh"
9
11
12#include "node_geometry_util.hh"
13
15
17{
18 b.use_custom_socket_order();
19 b.allow_any_socket_order();
20 b.add_input<decl::Geometry>("Curve")
21 .supported_type({GeometryComponent::Type::Curve, GeometryComponent::Type::GreasePencil})
22 .description("Curves to change the normals on");
23 b.add_output<decl::Geometry>("Curve").propagate_all().align_with_previous();
24 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
25 b.add_input<decl::Menu>("Mode")
28 .description("Mode for curve normal evaluation");
29 b.add_input<decl::Vector>("Normal")
30 .default_value({0.0f, 0.0f, 1.0f})
31 .subtype(PROP_XYZ)
32 .field_on_all()
33 .usage_by_single_menu(NORMAL_MODE_FREE);
34}
35
37 const NormalMode mode,
38 const fn::FieldContext &curve_context,
39 const fn::FieldContext &point_context,
40 const Field<bool> &selection_field,
41 const Field<float3> &custom_normal)
42{
43 /* First evaluate the normal modes without changing the geometry, since that will influence the
44 * result of the "Normal" node if used in the input to the custom normal field evaluation. */
45 fn::FieldEvaluator evaluator(curve_context, curves.curves_num());
46 evaluator.set_selection(selection_field);
47 evaluator.evaluate();
48 const IndexMask curve_mask = evaluator.get_evaluated_selection_as_mask();
49
50 if (mode == NORMAL_MODE_FREE) {
52 point_context,
53 "custom_normal",
54 AttrDomain::Point,
55 Field<bool>(std::make_shared<bke::EvaluateOnDomainInput>(
56 selection_field, AttrDomain::Curve)),
57 custom_normal);
58 }
59
60 index_mask::masked_fill(curves.normal_mode_for_write(), int8_t(mode), curve_mask);
61
62 curves.tag_normals_changed();
63}
64
65static void set_grease_pencil_normal(GreasePencil &grease_pencil,
66 const NormalMode mode,
67 const Field<bool> &selection_field,
68 const Field<float3> &custom_normal)
69{
70 using namespace blender::bke::greasepencil;
71 for (const int layer_index : grease_pencil.layers().index_range()) {
72 Drawing *drawing = grease_pencil.get_eval_drawing(grease_pencil.layer(layer_index));
73 if (drawing == nullptr) {
74 continue;
75 }
77 drawing->strokes_for_write(),
78 mode,
79 bke::GreasePencilLayerFieldContext(grease_pencil, AttrDomain::Curve, layer_index),
80 bke::GreasePencilLayerFieldContext(grease_pencil, AttrDomain::Point, layer_index),
81 selection_field,
82 custom_normal);
83 }
84}
85
87{
88 GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve");
89 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
90 const NormalMode mode = params.get_input<NormalMode>("Mode");
91 Field<float3> custom_normal;
92 if (mode == NORMAL_MODE_FREE) {
93 custom_normal = params.extract_input<Field<float3>>("Normal");
94 }
95
96 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
97 if (Curves *curves_id = geometry_set.get_curves_for_write()) {
98 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
100 mode,
103 selection_field,
104 custom_normal);
105 }
106 if (GreasePencil *grease_pencil = geometry_set.get_grease_pencil_for_write()) {
107 set_grease_pencil_normal(*grease_pencil, mode, selection_field, custom_normal);
108 }
109 });
110
111 params.set_output("Curve", std::move(geometry_set));
112}
113
114static void node_register()
115{
116 static blender::bke::bNodeType ntype;
117 geo_node_type_base(&ntype, "GeometryNodeSetCurveNormal", GEO_NODE_SET_CURVE_NORMAL);
118 ntype.ui_name = "Set Curve Normal";
119 ntype.ui_description = "Set the evaluation mode for curve normals";
120 ntype.enum_name_legacy = "SET_CURVE_NORMAL";
122 ntype.declare = node_declare;
124
126}
128
129} // namespace blender::nodes::node_geo_set_curve_normal_cc
Low-level operations for curves.
Low-level operations for grease pencil.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define GEO_NODE_SET_CURVE_NORMAL
NormalMode
@ NORMAL_MODE_FREE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_XYZ
Definition RNA_types.hh:269
MutableAttributeAccessor attributes_for_write()
MutableSpan< int8_t > normal_mode_for_write()
bke::CurvesGeometry & strokes_for_write()
void set_selection(Field< bool > selection)
Definition FN_field.hh:383
IndexMask get_evaluated_selection_as_mask() const
Definition field.cc:817
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
bool try_capture_field_on_geometry(MutableAttributeAccessor attributes, const fn::FieldContext &field_context, const StringRef attribute_id, AttrDomain domain, const fn::Field< bool > &selection, const fn::GField &field)
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
void masked_fill(MutableSpan< T > data, const T &value, const IndexMask &mask)
static void set_curve_normal(bke::CurvesGeometry &curves, const NormalMode mode, const fn::FieldContext &curve_context, const fn::FieldContext &point_context, const Field< bool > &selection_field, const Field< float3 > &custom_normal)
static void node_declare(NodeDeclarationBuilder &b)
static void set_grease_pencil_normal(GreasePencil &grease_pencil, const NormalMode mode, const Field< bool > &selection_field, const Field< float3 > &custom_normal)
static void node_geo_exec(GeoNodeExecParams params)
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
const EnumPropertyItem rna_enum_curve_normal_mode_items[]
Definition rna_curves.cc:56
GreasePencil * get_grease_pencil_for_write()
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362