Blender V4.3
node_geo_mesh_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_material.h"
6
7#include "NOD_rna_define.hh"
8
9#include "RNA_access.hh"
10
11#include "UI_interface.hh"
12#include "UI_resources.hh"
13
15
17
18#include "node_geometry_util.hh"
19
21
23
25{
26 b.add_input<decl::Int>("Count").default_value(10).min(1).max(10000).description(
27 "Number of vertices on the line");
28 b.add_input<decl::Float>("Resolution")
29 .default_value(1.0f)
30 .min(0.1f)
32 .description("Length of each individual edge");
33 b.add_input<decl::Vector>("Start Location")
34 .subtype(PROP_TRANSLATION)
35 .description("Position of the first vertex");
36 b.add_input<decl::Vector>("Offset")
37 .default_value({0.0f, 0.0f, 1.0f})
38 .subtype(PROP_TRANSLATION)
40 "In offset mode, the distance between each socket on each axis. In end points mode, the "
41 "position of the final vertex");
42 b.add_output<decl::Geometry>("Mesh");
43}
44
45static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
46{
47 uiLayoutSetPropSep(layout, true);
48 uiLayoutSetPropDecorate(layout, false);
49 uiItemR(layout, ptr, "mode", UI_ITEM_NONE, "", ICON_NONE);
51 uiItemR(layout, ptr, "count_mode", UI_ITEM_NONE, "", ICON_NONE);
52 }
53}
54
55static void node_init(bNodeTree * /*tree*/, bNode *node)
56{
57 NodeGeometryMeshLine *node_storage = MEM_cnew<NodeGeometryMeshLine>(__func__);
58
61
62 node->storage = node_storage;
63}
64
65static void node_update(bNodeTree *ntree, bNode *node)
66{
67 bNodeSocket *count_socket = static_cast<bNodeSocket *>(node->inputs.first);
68 bNodeSocket *resolution_socket = count_socket->next;
69 bNodeSocket *start_socket = resolution_socket->next;
70 bNodeSocket *end_and_offset_socket = start_socket->next;
71
72 const NodeGeometryMeshLine &storage = node_storage(*node);
75 storage.count_mode;
76
77 node_sock_label(end_and_offset_socket,
78 (mode == GEO_NODE_MESH_LINE_MODE_END_POINTS) ? N_("End Location") :
79 N_("Offset"));
80
82 resolution_socket,
86 count_socket,
88 count_mode == GEO_NODE_MESH_LINE_COUNT_TOTAL);
89}
90
92{
93 const NodeDeclaration &declaration = *params.node_type().static_declaration;
94 if (params.in_out() == SOCK_OUT) {
96 return;
97 }
98 else if (params.node_tree().typeinfo->validate_link(
99 eNodeSocketDatatype(params.other_socket().type), SOCK_FLOAT))
100 {
101 params.add_item(IFACE_("Count"), [](LinkSearchOpParams &params) {
102 bNode &node = params.add_node("GeometryNodeMeshLine");
103 node_storage(node).mode = GEO_NODE_MESH_LINE_MODE_OFFSET;
104 params.connect_available_socket(node, "Count");
105 });
106 params.add_item(IFACE_("Resolution"), [](LinkSearchOpParams &params) {
107 bNode &node = params.add_node("GeometryNodeMeshLine");
108 node_storage(node).mode = GEO_NODE_MESH_LINE_MODE_OFFSET;
109 node_storage(node).count_mode = GEO_NODE_MESH_LINE_COUNT_RESOLUTION;
110 params.connect_available_socket(node, "Resolution");
111 });
112 params.add_item(IFACE_("Start Location"), [](LinkSearchOpParams &params) {
113 bNode &node = params.add_node("GeometryNodeMeshLine");
114 params.connect_available_socket(node, "Start Location");
115 });
116 params.add_item(IFACE_("Offset"), [](LinkSearchOpParams &params) {
117 bNode &node = params.add_node("GeometryNodeMeshLine");
118 params.connect_available_socket(node, "Offset");
119 });
120 /* The last socket is reused in end points mode. */
121 params.add_item(IFACE_("End Location"), [](LinkSearchOpParams &params) {
122 bNode &node = params.add_node("GeometryNodeMeshLine");
123 node_storage(node).mode = GEO_NODE_MESH_LINE_MODE_END_POINTS;
124 params.connect_available_socket(node, "Offset");
125 });
126 }
127}
128
130{
131 const NodeGeometryMeshLine &storage = node_storage(params.node());
134 storage.count_mode;
135
136 Mesh *mesh = nullptr;
137 const float3 start = params.extract_input<float3>("Start Location");
139 /* The label switches to "End Location", but the same socket is used. */
140 const float3 end = params.extract_input<float3>("Offset");
141 const float3 total_delta = end - start;
142
143 if (count_mode == GEO_NODE_MESH_LINE_COUNT_RESOLUTION) {
144 /* Don't allow asymptotic count increase for low resolution values. */
145 const float resolution = std::max(params.extract_input<float>("Resolution"), 0.0001f);
146 const int count = math::length(total_delta) / resolution + 1;
147 const float3 delta = math::normalize(total_delta) * resolution;
148 mesh = geometry::create_line_mesh(start, delta, count);
149 }
150 else if (count_mode == GEO_NODE_MESH_LINE_COUNT_TOTAL) {
151 const int count = params.extract_input<int>("Count");
152 if (count == 1) {
153 mesh = geometry::create_line_mesh(start, float3(0), count);
154 }
155 else {
156 const float3 delta = total_delta / float(count - 1);
157 mesh = geometry::create_line_mesh(start, delta, count);
158 }
159 }
160 }
161 else if (mode == GEO_NODE_MESH_LINE_MODE_OFFSET) {
162 const float3 delta = params.extract_input<float3>("Offset");
163 const int count = params.extract_input<int>("Count");
164 mesh = geometry::create_line_mesh(start, delta, count);
165 }
166
167 if (mesh) {
168 BKE_id_material_eval_ensure_default_slot(reinterpret_cast<ID *>(mesh));
169 }
170
171 params.set_output("Mesh", GeometrySet::from_mesh(mesh));
172}
173
174static void node_rna(StructRNA *srna)
175{
176 static EnumPropertyItem mode_items[] = {
178 "OFFSET",
179 0,
180 "Offset",
181 "Specify the offset from one vertex to the next"},
183 "END_POINTS",
184 0,
185 "End Points",
186 "Specify the line's start and end points"},
187 {0, nullptr, 0, nullptr, nullptr},
188 };
189
190 static EnumPropertyItem count_mode_items[] = {
192 "TOTAL",
193 0,
194 "Count",
195 "Specify the total number of vertices"},
197 "RESOLUTION",
198 0,
199 "Resolution",
200 "Specify the distance between vertices"},
201 {0, nullptr, 0, nullptr, nullptr},
202 };
203
205 "mode",
206 "Mode",
207 "",
208 mode_items,
211
213 "count_mode",
214 "Count Mode",
215 "",
216 count_mode_items,
217 NOD_storage_enum_accessors(count_mode),
219}
220
221static void node_register()
222{
223 static blender::bke::bNodeType ntype;
224
225 geo_node_type_base(&ntype, GEO_NODE_MESH_PRIMITIVE_LINE, "Mesh Line", NODE_CLASS_GEOMETRY);
226 ntype.declare = node_declare;
227 ntype.initfunc = node_init;
229 &ntype, "NodeGeometryMeshLine", node_free_standard_storage, node_copy_standard_storage);
232 ntype.updatefunc = node_update;
235
236 node_rna(ntype.rna_ext.srna);
237}
239
240} // namespace blender::nodes::node_geo_mesh_primitive_line_cc
General operations, lookup, etc. for materials.
void BKE_id_material_eval_ensure_default_slot(struct ID *id)
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1799
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:418
#define IFACE_(msgid)
GeometryNodeMeshLineCountMode
@ GEO_NODE_MESH_LINE_COUNT_RESOLUTION
@ GEO_NODE_MESH_LINE_COUNT_TOTAL
@ SOCK_OUT
GeometryNodeMeshLineMode
@ GEO_NODE_MESH_LINE_MODE_END_POINTS
@ GEO_NODE_MESH_LINE_MODE_OFFSET
eNodeSocketDatatype
@ SOCK_FLOAT
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
@ PROP_DISTANCE
Definition RNA_types.hh:159
@ PROP_TRANSLATION
Definition RNA_types.hh:164
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
#define UI_ITEM_NONE
void uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
Vector< SocketDeclaration * > outputs
local_group_size(16, 16) .push_constant(Type b
draw_view in_light_buf[] float
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
int count
void node_set_socket_availability(bNodeTree *ntree, bNodeSocket *sock, bool is_available)
Definition node.cc:3911
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
Mesh * create_line_mesh(float3 start, float3 delta, int count)
T length(const VecBase< T, Size > &a)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
static void node_update(bNodeTree *ntree, bNode *node)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
void search_link_ops_for_declarations(GatherLinkSearchOpParams &params, Span< SocketDeclaration * > declarations)
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, int type, const char *name, short nclass)
void node_sock_label(bNodeSocket *sock, const char *name)
Definition node_util.cc:77
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
int RNA_enum_get(PointerRNA *ptr, const char *name)
StructRNA * srna
Definition RNA_types.hh:780
Definition DNA_ID.h:413
struct bNodeSocket * next
static GeometrySet from_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
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
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:363
NodeDeclareFunction declare
Definition BKE_node.hh:347
void(* updatefunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:257
#define N_(msgid)
PointerRNA * ptr
Definition wm_files.cc:4126