Blender V4.3
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 "UI_interface.hh"
9#include "UI_resources.hh"
10
11#include "NOD_rna_define.hh"
12
13#include "RNA_enum_types.hh"
14
15#include "node_geometry_util.hh"
16
18
20{
21 b.add_input<decl::Geometry>("Curve").supported_type(
23 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
24 auto &normal = b.add_input<decl::Vector>("Normal")
25 .default_value({0.0f, 0.0f, 1.0f})
26 .subtype(PROP_XYZ)
27 .field_on_all();
28 b.add_output<decl::Geometry>("Curve").propagate_all();
29
30 const bNode *node = b.node_or_null();
31 if (node != nullptr) {
32 const NormalMode mode = NormalMode(node->custom1);
33 normal.available(mode == NORMAL_MODE_FREE);
34 }
35}
36
37static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
38{
39 uiItemR(layout, ptr, "mode", UI_ITEM_NONE, "", ICON_NONE);
40}
41
42static void node_init(bNodeTree * /*tree*/, bNode *node)
43{
44 node->custom1 = NORMAL_MODE_MINIMUM_TWIST;
45}
46
48 const NormalMode mode,
49 const fn::FieldContext &curve_context,
50 const fn::FieldContext &point_context,
51 const Field<bool> &selection_field,
52 const Field<float3> &custom_normal)
53{
54 bke::try_capture_field_on_geometry(curves.attributes_for_write(),
55 curve_context,
56 "normal_mode",
57 AttrDomain::Curve,
58 selection_field,
60
61 if (mode == NORMAL_MODE_FREE) {
62 bke::try_capture_field_on_geometry(curves.attributes_for_write(),
63 point_context,
64 "custom_normal",
65 AttrDomain::Point,
66 Field<bool>(std::make_shared<bke::EvaluateOnDomainInput>(
67 selection_field, AttrDomain::Curve)),
68 custom_normal);
69 }
70
71 curves.tag_normals_changed();
72}
73
74static void set_grease_pencil_normal(GreasePencil &grease_pencil,
75 const NormalMode mode,
76 const Field<bool> &selection_field,
77 const Field<float3> &custom_normal)
78{
79 using namespace blender::bke::greasepencil;
80 for (const int layer_index : grease_pencil.layers().index_range()) {
81 Drawing *drawing = grease_pencil.get_eval_drawing(grease_pencil.layer(layer_index));
82 if (drawing == nullptr) {
83 continue;
84 }
86 drawing->strokes_for_write(),
87 mode,
88 bke::GreasePencilLayerFieldContext(grease_pencil, AttrDomain::Curve, layer_index),
89 bke::GreasePencilLayerFieldContext(grease_pencil, AttrDomain::Point, layer_index),
90 selection_field,
91 custom_normal);
92 }
93}
94
96{
97 const NormalMode mode = static_cast<NormalMode>(params.node().custom1);
98
99 GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve");
100 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
101 Field<float3> custom_normal;
102 if (mode == NORMAL_MODE_FREE) {
103 custom_normal = params.extract_input<Field<float3>>("Normal");
104 }
105
106 geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
107 if (Curves *curves_id = geometry_set.get_curves_for_write()) {
108 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
109 set_curve_normal(curves,
110 mode,
111 bke::CurvesFieldContext(*curves_id, AttrDomain::Curve),
112 bke::CurvesFieldContext(*curves_id, AttrDomain::Point),
113 selection_field,
114 custom_normal);
115 }
116 if (GreasePencil *grease_pencil = geometry_set.get_grease_pencil_for_write()) {
117 set_grease_pencil_normal(*grease_pencil, mode, selection_field, custom_normal);
118 }
119 });
120
121 params.set_output("Curve", std::move(geometry_set));
122}
123
124static void node_rna(StructRNA *srna)
125{
127 "mode",
128 "Mode",
129 "Mode for curve normal evaluation",
132}
133
134static void node_register()
135{
136 static blender::bke::bNodeType ntype;
137 geo_node_type_base(&ntype, GEO_NODE_SET_CURVE_NORMAL, "Set Curve Normal", NODE_CLASS_GEOMETRY);
138 ntype.declare = node_declare;
140 ntype.initfunc = node_init;
142
144
145 node_rna(ntype.rna_ext.srna);
146}
148
149} // 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:418
NormalMode
@ NORMAL_MODE_MINIMUM_TWIST
@ NORMAL_MODE_FREE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
@ PROP_XYZ
Definition RNA_types.hh:172
#define UI_ITEM_NONE
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
bke::CurvesGeometry & strokes_for_write()
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
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 node_register_type(bNodeType *ntype)
Definition node.cc:1708
GField make_constant_field(const CPPType &type, const void *value)
Definition field.cc:533
static void node_init(bNodeTree *, bNode *node)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
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)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
void geo_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
const EnumPropertyItem rna_enum_curve_normal_mode_items[]
Definition rna_curves.cc:54
StructRNA * srna
Definition RNA_types.hh:780
void modify_geometry_sets(ForeachSubGeometryCallback callback)
GreasePencil * get_grease_pencil_for_write()
Defines a node type.
Definition BKE_node.hh:218
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:267
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:339
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:238
NodeDeclareFunction declare
Definition BKE_node.hh:347
PointerRNA * ptr
Definition wm_files.cc:4126