Blender V4.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
9#include "UI_interface.hh"
10#include "UI_resources.hh"
11
12#include "node_geometry_util.hh"
13
15
17
19{
20 b.add_input<decl::Geometry>("Curve").supported_type(GeometryComponent::Type::Curve);
21 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
22 b.add_output<decl::Geometry>("Curve").propagate_all();
23}
24
25static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
26{
27 uiItemR(layout, ptr, "mode", UI_ITEM_R_EXPAND, nullptr, ICON_NONE);
28 uiItemR(layout, ptr, "handle_type", UI_ITEM_NONE, "", ICON_NONE);
29}
30
31static void node_init(bNodeTree * /*tree*/, bNode *node)
32{
33 NodeGeometryCurveSetHandles *data = MEM_cnew<NodeGeometryCurveSetHandles>(__func__);
34
35 data->handle_type = GEO_NODE_CURVE_HANDLE_AUTO;
37 node->storage = data;
38}
39
55
56static void set_handle_type(Curves &curves_id,
58 const HandleType new_handle_type,
59 const Field<bool> &selection_field)
60{
61 bke::CurvesGeometry &curves = curves_id.geometry.wrap();
62 const bke::CurvesFieldContext field_context{curves_id, AttrDomain::Point};
63 fn::FieldEvaluator evaluator{field_context, curves.points_num()};
64 evaluator.set_selection(selection_field);
65 evaluator.evaluate();
66 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
67
68 if (mode & GEO_NODE_CURVE_HANDLE_LEFT) {
70 curves.handle_types_left_for_write(), new_handle_type, selection);
71 }
72 if (mode & GEO_NODE_CURVE_HANDLE_RIGHT) {
74 curves.handle_types_right_for_write(), new_handle_type, selection);
75 }
76
77 curves.tag_topology_changed();
78
79 /* Eagerly calculate automatically derived handle positions if necessary. */
81 curves.calculate_bezier_auto_handles();
82 }
83}
84
86{
87 const NodeGeometryCurveSetHandles &storage = node_storage(params.node());
90
91 GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve");
92 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
93
94 const HandleType new_handle_type = handle_type_from_input_type(type);
95
96 std::atomic<bool> has_curves = false;
97 std::atomic<bool> has_bezier = false;
98
99 geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
100 if (Curves *curves_id = geometry_set.get_curves_for_write()) {
101 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
102 has_curves = true;
103 const AttributeAccessor attributes = curves.attributes();
104 if (!attributes.contains("handle_type_left") || !attributes.contains("handle_type_right")) {
105 return;
106 }
107 has_bezier = true;
108
109 set_handle_type(*curves_id, mode, new_handle_type, selection_field);
110 }
111 });
112
113 if (has_curves && !has_bezier) {
114 params.error_message_add(NodeWarningType::Info, TIP_("Input curves do not have Bézier type"));
115 }
116
117 params.set_output("Curve", std::move(geometry_set));
118}
119
120static void node_register()
121{
122 static blender::bke::bNodeType ntype;
124 &ntype, GEO_NODE_CURVE_SET_HANDLE_TYPE, "Set Handle Type", NODE_CLASS_GEOMETRY);
125 ntype.declare = node_declare;
127 ntype.initfunc = node_init;
129 "NodeGeometryCurveSetHandles",
133
135}
136NOD_REGISTER_NODE(node_register)
137
138} // namespace blender::nodes::node_geo_curve_set_handle_type_cc
Low-level operations for curves.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1799
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:418
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
#define ELEM(...)
#define TIP_(msgid)
HandleType
@ BEZIER_HANDLE_FREE
@ BEZIER_HANDLE_ALIGN
@ BEZIER_HANDLE_VECTOR
@ BEZIER_HANDLE_AUTO
GeometryNodeCurveHandleMode
@ GEO_NODE_CURVE_HANDLE_RIGHT
@ GEO_NODE_CURVE_HANDLE_LEFT
GeometryNodeCurveHandleType
@ GEO_NODE_CURVE_HANDLE_ALIGN
@ GEO_NODE_CURVE_HANDLE_AUTO
@ GEO_NODE_CURVE_HANDLE_FREE
@ GEO_NODE_CURVE_HANDLE_VECTOR
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define UI_ITEM_NONE
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
@ UI_ITEM_R_EXPAND
void set_selection(Field< bool > selection)
Definition FN_field.hh:385
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_type_storage(bNodeType *ntype, const char *storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:4632
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
void masked_fill(MutableSpan< T > data, const T &value, const IndexMask &mask)
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)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
void geo_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:46
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:58
CurvesGeometry geometry
void modify_geometry_sets(ForeachSubGeometryCallback callback)
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