Blender V4.5
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.use_custom_socket_order();
21 b.allow_any_socket_order();
22 b.add_default_layout();
23 b.add_input<decl::Geometry>("Curve").supported_type(GeometryComponent::Type::Curve);
24 b.add_output<decl::Geometry>("Curve").propagate_all().align_with_previous();
25 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
26}
27
28static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
29{
30 layout->prop(ptr, "mode", UI_ITEM_R_EXPAND, std::nullopt, ICON_NONE);
31 layout->prop(ptr, "handle_type", UI_ITEM_NONE, "", ICON_NONE);
32}
33
42
58
59static void set_handle_type(Curves &curves_id,
61 const HandleType new_handle_type,
62 const Field<bool> &selection_field)
63{
64 bke::CurvesGeometry &curves = curves_id.geometry.wrap();
65 const bke::CurvesFieldContext field_context{curves_id, AttrDomain::Point};
66 fn::FieldEvaluator evaluator{field_context, curves.points_num()};
67 evaluator.set_selection(selection_field);
68 evaluator.evaluate();
69 const IndexMask selection = evaluator.get_evaluated_selection_as_mask();
70
71 if (mode & GEO_NODE_CURVE_HANDLE_LEFT) {
73 curves.handle_types_left_for_write(), new_handle_type, selection);
74 }
75 if (mode & GEO_NODE_CURVE_HANDLE_RIGHT) {
77 curves.handle_types_right_for_write(), new_handle_type, selection);
78 }
79
80 curves.tag_topology_changed();
81
82 /* Eagerly calculate automatically derived handle positions if necessary. */
85 }
86}
87
89{
90 const NodeGeometryCurveSetHandles &storage = node_storage(params.node());
93
94 GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve");
95 Field<bool> selection_field = params.extract_input<Field<bool>>("Selection");
96
97 const HandleType new_handle_type = handle_type_from_input_type(type);
98
99 std::atomic<bool> has_curves = false;
100 std::atomic<bool> has_bezier = false;
101
102 geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
103 if (Curves *curves_id = geometry_set.get_curves_for_write()) {
104 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
105 has_curves = true;
106 const AttributeAccessor attributes = curves.attributes();
107 if (!attributes.contains("handle_type_left") || !attributes.contains("handle_type_right")) {
108 return;
109 }
110 has_bezier = true;
111
112 set_handle_type(*curves_id, mode, new_handle_type, selection_field);
113 }
114 });
115
116 if (has_curves && !has_bezier) {
117 params.error_message_add(NodeWarningType::Info, TIP_("Input curves do not have Bézier type"));
118 }
119
120 params.set_output("Curve", std::move(geometry_set));
121}
122
123static void node_register()
124{
125 static blender::bke::bNodeType ntype;
126 geo_node_type_base(&ntype, "GeometryNodeCurveSetHandles", GEO_NODE_CURVE_SET_HANDLE_TYPE);
127 ntype.ui_name = "Set Handle Type";
128 ntype.ui_description = "Set the handle type for the control points of a Bézier curve";
129 ntype.enum_name_legacy = "CURVE_SET_HANDLES";
131 ntype.declare = node_declare;
133 ntype.initfunc = node_init;
135 "NodeGeometryCurveSetHandles",
139
141}
143
144} // namespace blender::nodes::node_geo_curve_set_handle_type_cc
Low-level operations for curves.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1215
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
#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:2748
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:5603
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
void modify_geometry_sets(ForeachSubGeometryCallback callback)
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:277
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:355
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:4227