Blender V4.5
node_geo_curve_length.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"
7
9
11
13{
14 b.add_input<decl::Geometry>("Curve").supported_type(
15 {GeometryComponent::Type::Curve, GeometryComponent::Type::GreasePencil});
16 b.add_output<decl::Float>("Length");
17}
18
19static float curves_total_length(const bke::CurvesGeometry &curves)
20{
21 const VArray<bool> cyclic = curves.cyclic();
23
24 float total_length = 0.0f;
25 for (const int i : curves.curves_range()) {
26 total_length += curves.evaluated_length_total_for_curve(i, cyclic[i]);
27 }
28 return total_length;
29}
30
32{
33 GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve");
34 float length = 0.0f;
35 if (geometry_set.has_curves()) {
36 const Curves &curves_id = *geometry_set.get_curves();
37 const bke::CurvesGeometry &curves = curves_id.geometry.wrap();
38 length += curves_total_length(curves);
39 }
40 else if (geometry_set.has_grease_pencil()) {
41 using namespace bke::greasepencil;
42 const GreasePencil &grease_pencil = *geometry_set.get_grease_pencil();
43 for (const int layer_index : grease_pencil.layers().index_range()) {
44 const Drawing *drawing = grease_pencil.get_eval_drawing(grease_pencil.layer(layer_index));
45 if (drawing == nullptr) {
46 continue;
47 }
48 const bke::CurvesGeometry &curves = drawing->strokes();
49 length += curves_total_length(curves);
50 }
51 }
52 else {
53 params.set_default_remaining_outputs();
54 return;
55 }
56
57 params.set_output("Length", length);
58}
59
60static void node_register()
61{
62 static blender::bke::bNodeType ntype;
63
64 geo_node_type_base(&ntype, "GeometryNodeCurveLength", GEO_NODE_CURVE_LENGTH);
65 ntype.ui_name = "Curve Length";
66 ntype.ui_description = "Retrieve the length of all splines added together";
67 ntype.enum_name_legacy = "CURVE_LENGTH";
69 ntype.declare = node_declare;
72}
74
75} // namespace blender::nodes::node_geo_curve_length_cc
Low-level operations for curves.
Low-level operations for grease pencil.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
#define GEO_NODE_CURVE_LENGTH
#define NOD_REGISTER_NODE(REGISTER_FUNC)
IndexRange curves_range() const
float evaluated_length_total_for_curve(int curve_index, bool cyclic) const
VArray< bool > cyclic() const
const bke::CurvesGeometry & strokes() const
float length(VecOp< float, D >) RET
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
static void node_declare(NodeDeclarationBuilder &b)
static float curves_total_length(const bke::CurvesGeometry &curves)
static void node_geo_exec(GeoNodeExecParams params)
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
CurvesGeometry geometry
const GreasePencil * get_grease_pencil() const
const Curves * get_curves() const
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355
i
Definition text_draw.cc:230