Blender V5.0
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")
26 .supported_type(GeometryComponent::Type::Curve)
27 .description("Curves to set handles of control points on");
28 b.add_output<decl::Geometry>("Curve").propagate_all().align_with_previous();
29 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
30}
31
32static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
33{
34 layout->prop(ptr, "mode", UI_ITEM_R_EXPAND, std::nullopt, ICON_NONE);
35 layout->prop(ptr, "handle_type", UI_ITEM_NONE, "", ICON_NONE);
36}
37
46
62
63static void set_handle_type(Curves &curves_id,
65 const HandleType new_handle_type,
66 const Field<bool> &selection_field)
67{
68 bke::CurvesGeometry &curves = curves_id.geometry.wrap();
69 const bke::CurvesFieldContext field_context{curves_id, AttrDomain::Point};
70 fn::FieldEvaluator evaluator{field_context, curves.points_num()};
71 evaluator.set_selection(selection_field);
72 evaluator.evaluate();
73 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
74
75 if (mode & GEO_NODE_CURVE_HANDLE_LEFT) {
77 curves.handle_types_left_for_write(), new_handle_type, selection);
78 }
79 if (mode & GEO_NODE_CURVE_HANDLE_RIGHT) {
81 curves.handle_types_right_for_write(), new_handle_type, selection);
82 }
83
84 curves.tag_topology_changed();
85
86 /* Eagerly calculate automatically derived handle positions if necessary. */
89 }
90}
91
93{
94 const NodeGeometryCurveSetHandles &storage = node_storage(params.node());
97
98 GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve");
99 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
100
101 const HandleType new_handle_type = handle_type_from_input_type(type);
102
103 std::atomic<bool> has_curves = false;
104 std::atomic<bool> has_bezier = false;
105
106 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
107 if (Curves *curves_id = geometry_set.get_curves_for_write()) {
108 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
109 has_curves = true;
110 const AttributeAccessor attributes = curves.attributes();
111 if (!attributes.contains("handle_type_left") || !attributes.contains("handle_type_right")) {
112 return;
113 }
114 has_bezier = true;
115
116 set_handle_type(*curves_id, mode, new_handle_type, selection_field);
117 }
118 });
119
120 if (has_curves && !has_bezier) {
121 params.error_message_add(NodeWarningType::Info, TIP_("Input curves do not have Bézier type"));
122 }
123
124 params.set_output("Curve", std::move(geometry_set));
125}
126
127static void node_register()
128{
129 static blender::bke::bNodeType ntype;
130 geo_node_type_base(&ntype, "GeometryNodeCurveSetHandles", GEO_NODE_CURVE_SET_HANDLE_TYPE);
131 ntype.ui_name = "Set Handle Type";
132 ntype.ui_description = "Set the handle type for the control points of a Bézier curve";
133 ntype.enum_name_legacy = "CURVE_SET_HANDLES";
135 ntype.declare = node_declare;
137 ntype.initfunc = node_init;
139 "NodeGeometryCurveSetHandles",
143
145}
147
148} // namespace blender::nodes::node_geo_curve_set_handle_type_cc
Low-level operations for curves.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1240
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define GEO_NODE_CURVE_SET_HANDLE_TYPE
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#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)
@ UI_ITEM_R_EXPAND
#define UI_ITEM_NONE
BMesh const char void * data
bool contains(StringRef attribute_id) const
MutableSpan< int8_t > handle_types_right_for_write()
AttributeAccessor attributes() const
MutableSpan< int8_t > handle_types_left_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 * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
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:5414
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_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, std::string idname, const std::optional< int16_t > legacy_type)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:42
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:54
CurvesGeometry geometry
void * storage
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeDeclareFunction declare
Definition BKE_node.hh:362
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4238