Blender V5.3
node_geo_set_grease_pencil_color.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BKE_curves.hh"
7
9#include "UI_resources.hh"
10
11#include "NOD_rna_define.hh"
12
14
15#include "RNA_enum_types.hh"
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_input<decl::Geometry>("Grease Pencil"_ustr)
27 .description("Grease Pencil to change the color of");
28 b.add_output<decl::Geometry>("Grease Pencil"_ustr)
29 .propagate_all_geometry()
30 .align_with_previous();
31 b.add_input<decl::Bool>("Selection"_ustr)
32 .default_value(true)
33 .hide_value()
34 .evaluated_geometry_field();
35 b.add_input<decl::Menu>("Mode"_ustr)
38 b.add_input<decl::Color>("Color"_ustr)
39 .default_value(ColorGeometry4f(1.0f, 1.0f, 1.0f, 1.0f))
40 .evaluated_geometry_field()
42 b.add_input<decl::Float>("Opacity"_ustr)
43 .default_value(1.0f)
44 .min(0.0f)
45 .max(1.0f)
46 .evaluated_geometry_field();
47}
48
49static void node_init(bNodeTree * /*tree*/, bNode *node)
50{
51 node->custom1 = int(GEO_NODE_GREASE_PENCIL_STROKE);
52}
53
54static void set_grease_pencil_color(GeometrySet &geometry_set,
55 const AttrDomain domain,
56 const StringRef color_attr,
57 const StringRef opacity_attr,
58 const Field<bool> &selection,
59 const Field<ColorGeometry4f> &color_field,
60 const Field<float> &opacity_field)
61{
62 using namespace bke::greasepencil;
63
65 GreasePencil *grease_pencil = geometry.get_grease_pencil_for_write();
66 if (!grease_pencil) {
67 return;
68 }
69
70 for (const int layer_index : grease_pencil->layers().index_range()) {
71 Drawing *drawing = grease_pencil->get_eval_drawing(grease_pencil->layer(layer_index));
72 if (!drawing) {
73 continue;
74 }
75
77 bke::MutableAttributeAccessor attributes = curves.attributes_for_write();
78
79 /* FIXME: The default float value is 0, while the default opacity should be 1. So we have
80 * to initialize the attribute manually.
81 * TODO: Avoid doing this if the selection is false. */
82 if (!attributes.contains(opacity_attr)) {
83 attributes.add<float>(opacity_attr, domain, bke::AttributeInitValue(1.0f));
84 }
85
86 const bke::GreasePencilLayerFieldContext field_context(*grease_pencil, domain, layer_index);
87
89 field_context,
90 {color_attr, opacity_attr},
91 domain,
92 selection,
93 {color_field, opacity_field});
94 }
95 });
96}
97
99{
100 const auto mode = params.get_input<NodeGeometryGreasePencilStrokeType>("Mode"_ustr);
101
102 GeometrySet geometry_set = params.extract_input<GeometrySet>("Grease Pencil"_ustr);
103 const Field<bool> selection = params.extract_input<Field<bool>>("Selection"_ustr);
105 "Color"_ustr);
106 const Field<float> opacity_field = params.extract_input<Field<float>>("Opacity"_ustr);
107
109 set_grease_pencil_color(geometry_set,
110 AttrDomain::Point,
111 "vertex_color",
112 "opacity",
113 selection,
114 color_field,
115 opacity_field);
116 }
118 set_grease_pencil_color(geometry_set,
119 AttrDomain::Curve,
120 "fill_color",
121 "fill_opacity",
122 selection,
123 color_field,
124 opacity_field);
125 }
126
127 params.set_output("Grease Pencil"_ustr, std::move(geometry_set));
128}
129
130static void node_register()
131{
132 static bke::bNodeType ntype;
133
134 geo_node_type_base(&ntype, "GeometryNodeSetGreasePencilColor"_ustr);
135 ntype.ui_name = "Set Grease Pencil Color";
136 ntype.ui_description = "Set color and opacity attributes on Grease Pencil geometry";
139 ntype.declare = node_declare;
140 ntype.initfunc = node_init;
143}
145
146} // namespace blender::nodes::node_geo_set_grease_pencil_color_cc
Low-level operations for curves.
Low-level operations for grease pencil.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define ELEM(...)
bool contains(StringRef name) const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
bke::CurvesGeometry & strokes_for_write()
T extract_input(const UString identifier)
T get_input(const UString identifier) const
void set_output(const UString identifier, T &&value)
bool try_capture_fields_on_geometry(MutableAttributeAccessor attributes, const fn::FieldContext &field_context, Span< StringRef > names, AttrDomain domain, const fn::Field< bool > &selection, Span< fn::GField > fields)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
static void set_grease_pencil_color(GeometrySet &geometry_set, const AttrDomain domain, const StringRef color_attr, const StringRef opacity_attr, const Field< bool > &selection, const Field< ColorGeometry4f > &color_field, const Field< float > &opacity_field)
NodeGeometryGreasePencilStrokeType
@ GEO_NODE_GREASE_PENCIL_STROKE
@ GEO_NODE_GREASE_PENCIL_FILL
@ GEO_NODE_GREASE_PENCIL_BOTH
const EnumPropertyItem rna_enum_node_grease_pencil_stroke_type_items[]
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
static constexpr int _180
Definition BKE_node.hh:247
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
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
NodeDeclareFunction declare
Definition BKE_node.hh:393