Blender V5.0
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
21enum class Mode : int8_t {
22 Stroke = 0,
23 Fill = 1,
24};
25
27{
28 b.use_custom_socket_order();
29 b.allow_any_socket_order();
30 b.add_default_layout();
31 b.add_input<decl::Geometry>("Grease Pencil")
32 .supported_type(GeometryComponent::Type::GreasePencil)
33 .align_with_previous()
34 .description("Grease Pencil to change the color of");
35 b.add_output<decl::Geometry>("Grease Pencil").propagate_all().align_with_previous();
36 b.add_input<decl::Bool>("Selection").default_value(true).hide_value().field_on_all();
37 b.add_input<decl::Color>("Color")
38 .default_value(ColorGeometry4f(1.0f, 1.0f, 1.0f, 1.0f))
39 .field_on_all()
41 b.add_input<decl::Float>("Opacity").default_value(1.0f).min(0.0f).max(1.0f).field_on_all();
42}
43
44static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
45{
46 layout->prop(ptr, "mode", UI_ITEM_NONE, "", ICON_NONE);
47}
48static void node_init(bNodeTree * /*tree*/, bNode *node)
49{
50 node->custom1 = int(Mode::Stroke);
51}
52
54{
55 const bNode &node = params.node();
56 const AttrDomain domain = Mode(node.custom1) == Mode::Stroke ? AttrDomain::Point :
57 AttrDomain::Curve;
58
59 GeometrySet geometry_set = params.extract_input<GeometrySet>("Grease Pencil");
60 const Field<bool> selection = params.extract_input<Field<bool>>("Selection");
61 const Field<ColorGeometry4f> color_field = params.extract_input<Field<ColorGeometry4f>>("Color");
62 const Field<float> opacity_field = params.extract_input<Field<float>>("Opacity");
63
64 const StringRef color_attr_name = domain == AttrDomain::Point ? "vertex_color" : "fill_color";
65 const StringRef opacity_attr_name = domain == AttrDomain::Point ? "opacity" : "fill_opacity";
66
68 if (GreasePencil *grease_pencil = geometry.get_grease_pencil_for_write()) {
69 using namespace bke::greasepencil;
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 == nullptr) {
73 continue;
74 }
75 bke::CurvesGeometry &curves = drawing->strokes_for_write();
76 const int64_t domain_size = curves.attributes().domain_size(domain);
77
78 const bke::GreasePencilLayerFieldContext layer_field_context(
79 *grease_pencil, domain, layer_index);
80
81 /* FIXME: The default float value is 0, while the default opacity should be 1. So we have
82 * to initialize the attribute manually.
83 * TODO: Avoid doing this if the selection is false. */
84 if (!curves.attributes().contains(opacity_attr_name)) {
85 curves.attributes_for_write().add<float>(
86 opacity_attr_name,
87 domain,
89 }
91 layer_field_context,
92 {color_attr_name, opacity_attr_name},
93 domain,
94 selection,
95 {color_field, opacity_field});
96 }
97 }
98 });
99
100 params.set_output("Grease Pencil", std::move(geometry_set));
101}
102
103static void node_rna(StructRNA *srna)
104{
105 static const EnumPropertyItem mode_items[] = {
106 {int(Mode::Stroke),
107 "STROKE",
108 ICON_NONE,
109 "Stroke",
110 "Set the color and opacity for the points of the stroke"},
111 {int(Mode::Fill),
112 "FILL",
113 ICON_NONE,
114 "Fill",
115 "Set the color and opacity for the stroke fills"},
116 {0, nullptr, 0, nullptr, nullptr},
117 };
118
119 RNA_def_node_enum(srna, "mode", "Mode", "", mode_items, NOD_inline_enum_accessors(custom1));
120}
121
122static void node_register()
123{
124 static blender::bke::bNodeType ntype;
125
126 geo_node_type_base(&ntype, "GeometryNodeSetGreasePencilColor");
127 ntype.ui_name = "Set Grease Pencil Color";
128 ntype.ui_description = "Set color and opacity attributes on Grease Pencil geometry";
131 ntype.declare = node_declare;
132 ntype.initfunc = node_init;
136
137 node_rna(ntype.rna_ext.srna);
138}
140
141} // 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:461
constexpr int NODE_DEFAULT_MAX_WIDTH
Definition BKE_node.hh:1250
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
long long int int64_t
static VArray from_single(T value, const int64_t size)
bool contains(StringRef attribute_id) const
int domain_size(const AttrDomain domain) const
MutableAttributeAccessor attributes_for_write()
AttributeAccessor attributes() const
bool add(const StringRef attribute_id, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
bke::CurvesGeometry & strokes_for_write()
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_type_size(bNodeType &ntype, int width, int minwidth, int maxwidth)
Definition node.cc:5384
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
bool try_capture_fields_on_geometry(MutableAttributeAccessor attributes, const fn::FieldContext &field_context, Span< StringRef > attribute_ids, AttrDomain domain, const fn::Field< bool > &selection, Span< fn::GField > fields)
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
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)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
StructRNA * srna
int16_t custom1
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeDeclareFunction declare
Definition BKE_node.hh:362
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4238