Blender V5.3
node_geo_curve_set_handle_type.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 <atomic>
6
7#include "BKE_curves.hh"
8
10#include "UI_resources.hh"
11
13
14#include "node_geometry_util.hh"
15
17
19
21{
22 b.use_custom_socket_order();
23 b.allow_any_socket_order();
24 b.add_default_layout();
25 b.add_input<decl::Geometry>("Curve"_ustr)
26 .supported_type(GeometryComponent::Type::Curve)
27 .description("Curves to set handles of control points on");
28 b.add_output<decl::Geometry>("Curve"_ustr).propagate_all_geometry().align_with_previous();
29 b.add_input<decl::Bool>("Selection"_ustr)
30 .default_value(true)
31 .hide_value()
32 .evaluated_geometry_field();
33}
34
35static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
36{
37 layout.prop(ptr, "mode", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
38 layout.prop(ptr, "handle_type", UI_ITEM_NONE, "", ICON_NONE);
39}
40
41static void node_init(bNodeTree * /*tree*/, bNode *node)
42{
43 NodeGeometryCurveSetHandles *data = MEM_new<NodeGeometryCurveSetHandles>(__func__);
44
45 data->handle_type = GEO_NODE_CURVE_HANDLE_AUTO;
47 node->storage = data;
48}
49
65
66static void set_handle_type(Curves &curves_id,
68 const HandleType new_handle_type,
69 const Field<bool> &selection_field)
70{
71 bke::CurvesGeometry &curves = curves_id.geometry.wrap();
72 const bke::CurvesFieldContext field_context{curves_id, AttrDomain::Point};
73 fn::FieldEvaluator evaluator{field_context, curves.points_num()};
74 evaluator.set_selection(selection_field);
75 evaluator.evaluate();
76 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
77
78 if (mode & GEO_NODE_CURVE_HANDLE_LEFT) {
80 curves.handle_types_left_for_write(), new_handle_type, selection);
81 }
82 if (mode & GEO_NODE_CURVE_HANDLE_RIGHT) {
84 curves.handle_types_right_for_write(), new_handle_type, selection);
85 }
86
87 curves.tag_topology_changed();
88
89 /* Eagerly calculate automatically derived handle positions if necessary. */
91 curves.calculate_bezier_auto_handles();
92 }
93}
94
96{
97 const NodeGeometryCurveSetHandles &storage = node_storage(params.node());
100
101 GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve"_ustr);
102 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection"_ustr);
103
104 const HandleType new_handle_type = handle_type_from_input_type(type);
105
106 std::atomic<bool> has_curves = false;
107 std::atomic<bool> has_bezier = false;
108
109 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
110 if (Curves *curves_id = geometry_set.get_curves_for_write()) {
111 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
112 has_curves = true;
113 const AttributeAccessor attributes = curves.attributes();
114 if (!attributes.contains("handle_type_left") || !attributes.contains("handle_type_right")) {
115 return;
116 }
117 has_bezier = true;
118
119 set_handle_type(*curves_id, mode, new_handle_type, selection_field);
120 }
121 });
122
123 if (has_curves && !has_bezier) {
124 params.error_message_add(NodeWarningType::Info, TIP_("Input curves do not have Bézier type"));
125 }
126
127 params.set_output("Curve"_ustr, std::move(geometry_set));
128}
129
130static void node_register()
131{
132 static bke::bNodeType ntype;
133 geo_node_type_base(&ntype, "GeometryNodeCurveSetHandles"_ustr, GEO_NODE_CURVE_SET_HANDLE_TYPE);
134 ntype.ui_name = "Set Handle Type";
135 ntype.ui_description = "Set the handle type for the control points of a Bézier curve";
136 ntype.enum_name_legacy = "CURVE_SET_HANDLES";
138 ntype.declare = node_declare;
140 ntype.initfunc = node_init;
142 "NodeGeometryCurveSetHandles",
146
148}
150
151} // namespace blender::nodes::node_geo_curve_set_handle_type_cc
Low-level operations for curves.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_CURVE_SET_HANDLE_TYPE
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
#define ELEM(...)
bool contains(StringRef name) const
void set_selection(Field< bool > selection)
IndexMask get_evaluated_selection_as_mask() const
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
#define storage(slot, qualifiers)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
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 node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void set_handle_type(Curves &curves_id, const GeometryNodeCurveHandleMode mode, const HandleType new_handle_type, const Field< bool > &selection_field)
static HandleType handle_type_from_input_type(GeometryNodeCurveHandleType type)
GeometryNodeCurveHandleType
@ GEO_NODE_CURVE_HANDLE_FREE
@ GEO_NODE_CURVE_HANDLE_AUTO
@ GEO_NODE_CURVE_HANDLE_VECTOR
@ GEO_NODE_CURVE_HANDLE_ALIGN
BMesh const char void * data
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
GeometryNodeCurveHandleMode
@ GEO_NODE_CURVE_HANDLE_LEFT
@ GEO_NODE_CURVE_HANDLE_RIGHT
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
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)