Blender V5.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
6
7#include "BKE_material.hh"
8
9#include "NOD_rna_define.hh"
10
11#include "RNA_access.hh"
12
14#include "UI_resources.hh"
15
17
19
20#include "node_geometry_util.hh"
21
23
25
27{
28 b.add_input<decl::Int>("Count"_ustr)
29 .default_value(10)
30 .min(1)
31 .max(10000)
32 .description("Number of vertices on the line");
33 b.add_input<decl::Float>("Resolution"_ustr)
34 .default_value(1.0f)
35 .min(0.1f)
36 .subtype(PROP_DISTANCE)
37 .description("Length of each individual edge");
38 b.add_input<decl::Vector>("Start Location"_ustr)
40 .description("Position of the first vertex");
41 b.add_input<decl::Vector>("Offset"_ustr)
42 .default_value({0.0f, 0.0f, 1.0f})
43 .subtype(PROP_TRANSLATION)
44 .description(
45 "In offset mode, the distance between each socket on each axis. In end points mode, the "
46 "position of the final vertex")
47 .label_fn([](const bNode &node) {
48 return (node_storage(node).mode == GEO_NODE_MESH_LINE_MODE_END_POINTS) ?
49 IFACE_("End Location") :
50 IFACE_("Offset");
51 });
52 ;
53 b.add_output<decl::Geometry>("Mesh"_ustr);
54}
55
56static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
57{
58 layout.use_property_split_set(true);
59 layout.use_property_decorate_set(false);
60 layout.prop(ptr, "mode", UI_ITEM_NONE, "", ICON_NONE);
62 layout.prop(ptr, "count_mode", UI_ITEM_NONE, "", ICON_NONE);
63 }
64}
65
66static void node_init(bNodeTree * /*tree*/, bNode *node)
67{
68 NodeGeometryMeshLine *node_storage = MEM_new<NodeGeometryMeshLine>(__func__);
69
72
73 node->storage = node_storage;
74}
75
76static void node_update(bNodeTree *ntree, bNode *node)
77{
78 bNodeSocket *count_socket = node->inputs.first();
79 bNodeSocket *resolution_socket = count_socket->next;
80
81 const NodeGeometryMeshLine &storage = node_storage(*node);
84 storage.count_mode);
85
87 *resolution_socket,
91 *count_socket,
93 count_mode == GEO_NODE_MESH_LINE_COUNT_TOTAL);
94}
95
97{
98 const NodeDeclaration &declaration = *params.node_type().static_declaration;
99 if (params.in_out() == SOCK_OUT) {
100 search_link_ops_for_declarations(params, declaration.outputs);
101 return;
102 }
103 if (params.node_tree().typeinfo->validate_link(params.other_socket().type, SOCK_FLOAT)) {
104 params.add_item(IFACE_("Count"), [](LinkSearchOpParams &params) {
105 bNode &node = params.add_node("GeometryNodeMeshLine"_ustr);
106 node_storage(node).mode = GEO_NODE_MESH_LINE_MODE_OFFSET;
107 params.connect_available_socket(node, "Count"_ustr);
108 });
109 params.add_item(IFACE_("Resolution"), [](LinkSearchOpParams &params) {
110 bNode &node = params.add_node("GeometryNodeMeshLine"_ustr);
111 node_storage(node).mode = GEO_NODE_MESH_LINE_MODE_OFFSET;
112 node_storage(node).count_mode = GEO_NODE_MESH_LINE_COUNT_RESOLUTION;
113 params.connect_available_socket(node, "Resolution"_ustr);
114 });
115 params.add_item(IFACE_("Start Location"), [](LinkSearchOpParams &params) {
116 bNode &node = params.add_node("GeometryNodeMeshLine"_ustr);
117 params.connect_available_socket(node, "Start Location"_ustr);
118 });
119 params.add_item(IFACE_("Offset"), [](LinkSearchOpParams &params) {
120 bNode &node = params.add_node("GeometryNodeMeshLine"_ustr);
121 params.connect_available_socket(node, "Offset"_ustr);
122 });
123 /* The last socket is reused in end points mode. */
124 params.add_item(IFACE_("End Location"), [](LinkSearchOpParams &params) {
125 bNode &node = params.add_node("GeometryNodeMeshLine"_ustr);
126 node_storage(node).mode = GEO_NODE_MESH_LINE_MODE_END_POINTS;
127 params.connect_available_socket(node, "Offset"_ustr);
128 });
129 }
130}
131
133{
134 const NodeGeometryMeshLine &storage = node_storage(params.node());
137 storage.count_mode);
138
139 Mesh *mesh = nullptr;
140 const float3 start = params.extract_input<float3>("Start Location"_ustr);
142 /* The label switches to "End Location", but the same socket is used. */
143 const float3 end = params.extract_input<float3>("Offset"_ustr);
144 const float3 total_delta = end - start;
145
146 if (count_mode == GEO_NODE_MESH_LINE_COUNT_RESOLUTION) {
147 /* Don't allow asymptotic count increase for low resolution values. */
148 const float resolution = std::max(params.extract_input<float>("Resolution"_ustr), 0.0001f);
149 const int count = math::length(total_delta) / resolution + 1;
150 const float3 delta = math::normalize(total_delta) * resolution;
151 mesh = geometry::create_line_mesh(start, delta, count);
152 }
153 else if (count_mode == GEO_NODE_MESH_LINE_COUNT_TOTAL) {
154 const int count = params.extract_input<int>("Count"_ustr);
155 if (count == 1) {
156 mesh = geometry::create_line_mesh(start, float3(0), count);
157 }
158 else {
159 const float3 delta = total_delta / float(count - 1);
160 mesh = geometry::create_line_mesh(start, delta, count);
161 }
162 }
163 }
164 else if (mode == GEO_NODE_MESH_LINE_MODE_OFFSET) {
165 const float3 delta = params.extract_input<float3>("Offset"_ustr);
166 const int count = params.extract_input<int>("Count"_ustr);
167 mesh = geometry::create_line_mesh(start, delta, count);
168 }
169
170 if (mesh) {
172 }
173
174 params.set_output("Mesh"_ustr, GeometrySet::from_mesh(mesh));
175}
176
177static void node_rna(StructRNA *srna)
178{
179 static EnumPropertyItem mode_items[] = {
181 "OFFSET",
182 0,
183 "Offset",
184 "Specify the offset from one vertex to the next"},
186 "END_POINTS",
187 0,
188 "End Points",
189 "Specify the line's start and end points"},
190 {0, nullptr, 0, nullptr, nullptr},
191 };
192
193 static EnumPropertyItem count_mode_items[] = {
195 "TOTAL",
196 0,
197 "Count",
198 "Specify the total number of vertices"},
200 "RESOLUTION",
201 0,
202 "Resolution",
203 "Specify the distance between vertices"},
204 {0, nullptr, 0, nullptr, nullptr},
205 };
206
208 "mode",
209 "Mode",
210 "",
211 mode_items,
214
216 "count_mode",
217 "Count Mode",
218 "",
219 count_mode_items,
220 NOD_storage_enum_accessors(count_mode),
222}
223
224static void node_register()
225{
226 static bke::bNodeType ntype;
227
228 geo_node_type_base(&ntype, "GeometryNodeMeshLine"_ustr, GEO_NODE_MESH_PRIMITIVE_LINE);
229 ntype.ui_name = "Mesh Line";
230 ntype.ui_description = "Generate vertices in a line and connect them with edges";
231 ntype.enum_name_legacy = "MESH_PRIMITIVE_LINE";
233 ntype.declare = node_declare;
234 ntype.initfunc = node_init;
236 ntype, "NodeGeometryMeshLine", node_free_standard_storage, node_copy_standard_storage);
239 ntype.updatefunc = node_update;
242
243 node_rna(ntype.rna_ext.srna);
244}
246
247} // namespace blender::nodes::node_geo_mesh_primitive_line_cc
General operations, lookup, etc. for materials.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_MESH_PRIMITIVE_LINE
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
#define UI_ITEM_NONE
btScalar max
const bke::bNodeType & node_type() const
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
void connect_available_socket(bNode &new_node, UString socket_name)
Vector< SocketDeclaration * > outputs
#define storage(slot, qualifiers)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_set_socket_availability(bNodeTree &ntree, bNodeSocket &sock, bool is_available)
Definition node.cc:5499
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:6166
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(ui::Layout &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)
int RNA_enum_get(PointerRNA *ptr, const char *name)
@ GEO_NODE_MESH_LINE_MODE_END_POINTS
@ GEO_NODE_MESH_LINE_MODE_OFFSET
void BKE_id_material_eval_ensure_default_slot(ID *id)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
VecBase< float, 3 > float3
@ PROP_TRANSLATION
Definition RNA_types.hh:318
@ PROP_DISTANCE
Definition RNA_types.hh:313
GeometryNodeMeshLineCountMode
@ GEO_NODE_MESH_LINE_COUNT_RESOLUTION
@ GEO_NODE_MESH_LINE_COUNT_TOTAL
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
GeometryNodeMeshLineCountMode count_mode
GeometryNodeMeshLineMode mode
eNodeSocketDatatype type
struct bNodeSocket * next
bke::bNodeTreeType * typeinfo
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
void(*) updatefunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:312
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
nodes::NodeDeclaration * static_declaration
Definition BKE_node.hh:402
static GeometrySet from_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)