Blender V5.0
node_geo_curve_primitive_line.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"
6
7#include "NOD_rna_define.hh"
8
10#include "UI_resources.hh"
11
12#include "node_geometry_util.hh"
13
15
17
19{
20 auto enable_direction = [](bNode &node) {
21 node_storage(node).mode = GEO_NODE_CURVE_PRIMITIVE_LINE_MODE_DIRECTION;
22 };
23
24 b.add_input<decl::Vector>("Start")
26 .description("Position of the first control point");
27 auto &end = b.add_input<decl::Vector>("End")
28 .default_value({0.0f, 0.0f, 1.0f})
29 .subtype(PROP_TRANSLATION)
30 .description("Position of the second control point")
31 .make_available([](bNode &node) {
32 node_storage(node).mode = GEO_NODE_CURVE_PRIMITIVE_LINE_MODE_POINTS;
33 });
34 auto &direction =
35 b.add_input<decl::Vector>("Direction")
36 .default_value({0.0f, 0.0f, 1.0f})
37 .description("Direction the line is going in. The length of this vector does not matter")
38 .make_available(enable_direction);
39 auto &length = b.add_input<decl::Float>("Length")
40 .default_value(1.0f)
41 .subtype(PROP_DISTANCE)
42 .description("Distance between the two points")
43 .make_available(enable_direction);
44 b.add_output<decl::Geometry>("Curve");
45
46 const bNode *node = b.node_or_null();
47 if (node != nullptr) {
48 const NodeGeometryCurvePrimitiveLine &storage = node_storage(*node);
50 storage.mode);
51
52 end.available(mode == GEO_NODE_CURVE_PRIMITIVE_LINE_MODE_POINTS);
53 direction.available(mode == GEO_NODE_CURVE_PRIMITIVE_LINE_MODE_DIRECTION);
55 }
56}
57
58static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
59{
60 layout->prop(ptr, "mode", UI_ITEM_R_EXPAND, std::nullopt, ICON_NONE);
61}
62
70
71static Curves *create_point_line_curve(const float3 start, const float3 end)
72{
74 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
75
76 curves.positions_for_write().first() = start;
77 curves.positions_for_write().last() = end;
78
79 return curves_id;
80}
81
83 const float3 direction,
84 const float length)
85{
87 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
88
89 curves.positions_for_write().first() = start;
90 curves.positions_for_write().last() = math::normalize(direction) * length + start;
91
92 return curves_id;
93}
94
96{
97 const NodeGeometryCurvePrimitiveLine &storage = node_storage(params.node());
99
100 Curves *curves = nullptr;
102 curves = create_point_line_curve(params.extract_input<float3>("Start"),
103 params.extract_input<float3>("End"));
104 }
106 curves = create_direction_line_curve(params.extract_input<float3>("Start"),
107 params.extract_input<float3>("Direction"),
108 params.extract_input<float>("Length"));
109 }
110
111 params.set_output("Curve", GeometrySet::from_curves(curves));
112}
113
114static void node_rna(StructRNA *srna)
115{
116 static const EnumPropertyItem mode_items[] = {
118 "POINTS",
119 ICON_NONE,
120 "Points",
121 "Define the start and end points of the line"},
123 "DIRECTION",
124 ICON_NONE,
125 "Direction",
126 "Define a line with a start point, direction and length"},
127 {0, nullptr, 0, nullptr, nullptr},
128 };
129
131 "mode",
132 "Mode",
133 "Method used to determine radius and placement",
134 mode_items,
137}
138
139static void node_register()
140{
141 static blender::bke::bNodeType ntype;
142 geo_node_type_base(&ntype, "GeometryNodeCurvePrimitiveLine", GEO_NODE_CURVE_PRIMITIVE_LINE);
143 ntype.ui_name = "Curve Line";
144 ntype.ui_description = "Generate a poly spline line with two points";
145 ntype.enum_name_legacy = "CURVE_PRIMITIVE_LINE";
147 ntype.initfunc = node_init;
149 "NodeGeometryCurvePrimitiveLine",
152 ntype.declare = node_declare;
156
157 node_rna(ntype.rna_ext.srna);
158}
160
161} // namespace blender::nodes::node_geo_curve_primitive_line_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_PRIMITIVE_LINE
GeometryNodeCurvePrimitiveLineMode
@ GEO_NODE_CURVE_PRIMITIVE_LINE_MODE_POINTS
@ GEO_NODE_CURVE_PRIMITIVE_LINE_MODE_DIRECTION
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
@ PROP_DISTANCE
Definition RNA_types.hh:256
@ PROP_TRANSLATION
Definition RNA_types.hh:261
@ UI_ITEM_R_EXPAND
BMesh const char void * data
MutableSpan< float3 > positions_for_write()
float length(VecOp< float, D >) RET
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
Curves * curves_new_nomain_single(int points_num, CurveType type)
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
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
static Curves * create_direction_line_curve(const float3 start, const float3 direction, const float length)
static Curves * create_point_line_curve(const float3 start, const float3 end)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
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)
VecBase< float, 3 > float3
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
StructRNA * srna
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
static GeometrySet from_curves(Curves *curves, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
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