Blender V5.0
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")
15 .supported_type({GeometryComponent::Type::Curve, GeometryComponent::Type::GreasePencil})
16 .description("Curve to compute the length of");
17 b.add_output<decl::Float>("Length");
18}
19
20static float curves_total_length(const bke::CurvesGeometry &curves)
21{
22 const VArray<bool> cyclic = curves.cyclic();
24
25 float total_length = 0.0f;
26 for (const int i : curves.curves_range()) {
27 total_length += curves.evaluated_length_total_for_curve(i, cyclic[i]);
28 }
29 return total_length;
30}
31
33{
34 GeometrySet geometry_set = params.extract_input<GeometrySet>("Curve");
35 float length = 0.0f;
36 if (geometry_set.has_curves()) {
37 const Curves &curves_id = *geometry_set.get_curves();
38 const bke::CurvesGeometry &curves = curves_id.geometry.wrap();
39 length += curves_total_length(curves);
40 }
41 else if (geometry_set.has_grease_pencil()) {
42 using namespace bke::greasepencil;
43 const GreasePencil &grease_pencil = *geometry_set.get_grease_pencil();
44 for (const int layer_index : grease_pencil.layers().index_range()) {
45 const Drawing *drawing = grease_pencil.get_eval_drawing(grease_pencil.layer(layer_index));
46 if (drawing == nullptr) {
47 continue;
48 }
49 const bke::CurvesGeometry &curves = drawing->strokes();
50 length += curves_total_length(curves);
51 }
52 }
53 else {
54 params.set_default_remaining_outputs();
55 return;
56 }
57
58 params.set_output("Length", length);
59}
60
61static void node_register()
62{
63 static blender::bke::bNodeType ntype;
64
65 geo_node_type_base(&ntype, "GeometryNodeCurveLength", GEO_NODE_CURVE_LENGTH);
66 ntype.ui_name = "Curve Length";
67 ntype.ui_description = "Retrieve the length of all splines added together";
68 ntype.enum_name_legacy = "CURVE_LENGTH";
70 ntype.declare = node_declare;
73}
75
76} // 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:461
#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:2416
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:238
std::string ui_description
Definition BKE_node.hh:244
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362
i
Definition text_draw.cc:230