Blender V5.3
node_geo_curve_to_mesh.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
9#include "BKE_instances.hh"
10
13#include "GEO_randomize.hh"
14
15#include "DNA_mesh_types.h"
16
17#include "node_geometry_util.hh"
18
20
22{
23 b.use_custom_socket_order();
24 b.allow_any_socket_order();
25 b.add_output<decl::Geometry>("Mesh"_ustr).propagate_all_geometry();
26 b.add_input<decl::Geometry>("Curve"_ustr)
28 .description("Curve to convert to a mesh using the given profile");
29 b.add_input<decl::Geometry>("Profile Curve"_ustr)
30 .only_realized_data()
31 .supported_type(GeometryComponent::Type::Curve)
32 .description("Curves that are swept along the main curve");
33 b.add_input<decl::Float>("Scale"_ustr)
34 .default_value(1.0f)
35 .min(0.0f)
36 .evaluated_geometry_field({0})
37 .description("Scale of the profile at each point");
38 b.add_input<decl::Bool>("Fill Caps"_ustr)
39 .description(
40 "If the profile spline is cyclic, fill the ends of the generated mesh with N-gons");
41 {
42 auto &p = b.add_panel("Miter Scale"_ustr).default_closed(true);
43 p.add_input<decl::Bool>("Miter Scale"_ustr)
44 .panel_toggle()
46 "Scale the profile along the axis of the corner's turn to maintain a constant visual "
47 "width");
48 p.add_input<decl::Float>("Miter Scale Limit"_ustr)
49 .min(0.0f)
50 .max(math::AngleRadian::from_degree(180.0f).radian())
51 .default_value(math::AngleRadian::from_degree(135.0f).radian())
52 .subtype(PROP_ANGLE)
53 .description("Angle at which the miter scale stops increasing");
54 }
55}
56
58 const GeometrySet &profile_set,
59 const fn::FieldContext &context,
60 const Field<float> &scale_field,
61 const bool fill_caps,
62 const std::optional<float> miter_limit_angle,
63 const AttributeFilter &attribute_filter)
64{
65 Mesh *mesh;
66 if (profile_set.has_curves()) {
67 const Curves *profile_curves = profile_set.get_curves();
68
69 FieldEvaluator evaluator{context, curves.points_num()};
70 evaluator.add(scale_field);
71 evaluator.evaluate();
72
73 const VArray<float> profile_scales = evaluator.get_evaluated<float>(0);
75 profile_curves->geometry.wrap(),
76 profile_scales,
77 fill_caps,
78 miter_limit_angle,
79 attribute_filter);
80 }
81 else {
82 mesh = bke::curve_to_wire_mesh(curves, attribute_filter);
83 }
85 return mesh;
86}
87
88static void grease_pencil_to_mesh(GeometrySet &geometry_set,
89 const GeometrySet &profile_set,
90 const Field<float> &scale_field,
91 const bool fill_caps,
92 const std::optional<float> miter_limit_angle,
93 const AttributeFilter &attribute_filter)
94{
95 using namespace blender::bke::greasepencil;
96
97 const GreasePencil &grease_pencil = *geometry_set.get_grease_pencil();
98 Array<Mesh *> mesh_by_layer(grease_pencil.layers().size(), nullptr);
99
100 for (const int layer_index : grease_pencil.layers().index_range()) {
101 const Drawing *drawing = grease_pencil.get_eval_drawing(grease_pencil.layer(layer_index));
102 if (drawing == nullptr) {
103 continue;
104 }
105 const bke::CurvesGeometry &curves = drawing->strokes();
108 mesh_by_layer[layer_index] = curve_to_mesh(
109 curves, profile_set, context, scale_field, fill_caps, miter_limit_angle, attribute_filter);
110 }
111
112 if (mesh_by_layer.is_empty()) {
113 return;
114 }
115
116 auto instances = std::make_unique<bke::Instances>(mesh_by_layer.size());
117 MutableSpan<int> handles = instances->reference_handles_for_write();
118 instances->transforms_for_write().fill(float4x4::identity());
119 for (const int i : mesh_by_layer.index_range()) {
120 Mesh *mesh = mesh_by_layer[i];
121 if (!mesh) {
122 /* Add an empty reference so the number of layers and instances match.
123 * This makes it easy to reconstruct the layers afterwards and keep their attributes.
124 * Although in this particular case we don't propagate the attributes. */
125 handles[i] = instances->add_reference(bke::InstanceReference());
126 continue;
127 }
129 handles[i] = instances->add_reference(bke::InstanceReference{std::move(temp_set)});
130 }
131
132 bke::copy_attributes(geometry_set.get_grease_pencil()->attributes(),
135 attribute_filter,
136 instances->attributes_for_write());
137 InstancesComponent &dst_component = geometry_set.get_component_for_write<InstancesComponent>();
139 {GeometrySet::from_instances(dst_component.release()),
140 GeometrySet::from_instances(std::move(instances))},
141 attribute_filter);
142 dst_component.replace(new_instances.get_component_for_write<InstancesComponent>().release());
143 geometry_set.replace_grease_pencil(nullptr);
144}
145
147{
148 GeometrySet curve_set = params.extract_input<GeometrySet>("Curve"_ustr);
149 GeometrySet profile_set = params.extract_input<GeometrySet>("Profile Curve"_ustr);
150 const Field<float> scale_field = params.extract_input<Field<float>>("Scale"_ustr);
151 const bool fill_caps = params.extract_input<bool>("Fill Caps"_ustr);
152 const bool use_miter_limit_angle = params.extract_input<bool>("Miter Scale"_ustr);
153 const std::optional<float> miter_limit_angle = use_miter_limit_angle ?
154 std::make_optional<float>(
155 std::abs(params.extract_input<float>(
156 "Miter Scale Limit"_ustr))) :
157 std::nullopt;
158
160 const AttributeFilter &attribute_filter = params.get_attribute_filter("Mesh"_ustr);
161
162 geometry::foreach_real_geometry(curve_set, [&](GeometrySet &geometry_set) {
163 if (geometry_set.has_curves()) {
164 const Curves &curves = *geometry_set.get_curves();
165
167 Mesh *mesh = curve_to_mesh(curves.geometry.wrap(),
168 profile_set,
169 context,
170 scale_field,
171 fill_caps,
172 miter_limit_angle,
173 attribute_filter);
174 if (mesh != nullptr) {
175 mesh->mat = MEM_dupalloc(curves.mat);
176 mesh->totcol = curves.totcol;
177 }
178 geometry_set.replace_mesh(mesh);
179 }
180 if (geometry_set.has_grease_pencil()) {
182 geometry_set, profile_set, scale_field, fill_caps, miter_limit_angle, attribute_filter);
183 }
187 });
188
189 params.set_output("Mesh"_ustr, std::move(curve_set));
190}
191
192static void node_register()
193{
194 static bke::bNodeType ntype;
195
196 geo_node_type_base(&ntype, "GeometryNodeCurveToMesh"_ustr, GEO_NODE_CURVE_TO_MESH);
197 ntype.ui_name = "Curve to Mesh";
198 ntype.ui_description =
199 "Convert curves into a mesh, optionally with a custom profile shape defined by curves";
200 ntype.enum_name_legacy = "CURVE_TO_MESH";
202 ntype.declare = node_declare;
205}
207
208} // namespace blender::nodes::node_geo_curve_to_mesh_cc
Low-level operations for curves.
Low-level operations for grease pencil.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_CURVE_TO_MESH
#define NOD_REGISTER_NODE(REGISTER_FUNC)
int64_t size() const
Definition BLI_array.hh:267
IndexRange index_range() const
Definition BLI_array.hh:371
bool is_empty() const
Definition BLI_array.hh:275
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
static void remember_deformed_positions_if_necessary(GeometrySet &geometry)
void replace(Instances *instances, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const bke::CurvesGeometry & strokes() const
int add(GField field, GVArray *varray_ptr)
const GVArray & get_evaluated(const int field_index) const
NodeAttributeFilter get_attribute_filter(const UString output_identifier) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
Mesh * curve_to_mesh_sweep(const CurvesGeometry &main, const CurvesGeometry &profile, const VArray< float > &scales, bool fill_caps, std::optional< float > miter_limit_angle, const bke::AttributeFilter &attribute_filter={})
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void copy_attributes(const AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, MutableAttributeAccessor dst_attributes)
Mesh * curve_to_wire_mesh(const CurvesGeometry &curve, const bke::AttributeFilter &attribute_filter={})
bke::GeometrySet join_geometries(Span< bke::GeometrySet > geometries, const bke::AttributeFilter &attribute_filter, const std::optional< Span< bke::GeometryComponent::Type > > &component_types_to_join=std::nullopt, bool allow_merging_instance_references=true)
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
void debug_randomize_mesh_order(Mesh *mesh)
Definition randomize.cc:305
static void node_declare(NodeDeclarationBuilder &b)
static void grease_pencil_to_mesh(GeometrySet &geometry_set, const GeometrySet &profile_set, const Field< float > &scale_field, const bool fill_caps, const std::optional< float > miter_limit_angle, const AttributeFilter &attribute_filter)
static void node_geo_exec(GeoNodeExecParams params)
static Mesh * curve_to_mesh(const bke::CurvesGeometry &curves, const GeometrySet &profile_set, const fn::FieldContext &context, const Field< float > &scale_field, const bool fill_caps, const std::optional< float > miter_limit_angle, const AttributeFilter &attribute_filter)
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define min(a, b)
Definition sort.cc:38
void keep_only(Span< GeometryComponent::Type > component_types)
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
const GreasePencil * get_grease_pencil() const
const Curves * get_curves() const
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void replace_grease_pencil(GreasePencil *grease_pencil, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
static AngleRadianBase from_degree(const float &degrees)
static GeometrySet from_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
static GeometrySet from_instances(std::unique_ptr< Instances > instances)