Blender V4.3
node_util.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2007 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <cctype>
10#include <climits>
11#include <cstring>
12
13#include "DNA_node_types.h"
14
15#include "BLI_listbase.h"
16#include "BLI_string.h"
17#include "BLI_string_utf8.h"
18#include "BLI_utildefines.h"
19
20#include "BLT_translation.hh"
21
22#include "BKE_colortools.hh"
23#include "BKE_node.hh"
24#include "BKE_node_runtime.hh"
26
27#include "RNA_access.hh"
28#include "RNA_enum_types.hh"
29#include "RNA_prototypes.hh"
30
31#include "MEM_guardedalloc.h"
32
33#include "NOD_common.h"
34
35#include "node_util.hh"
36
37/* -------------------------------------------------------------------- */
42{
43 BKE_curvemapping_free(static_cast<CurveMapping *>(node->storage));
44}
45
47{
48 if (node->storage) {
49 MEM_freeN(node->storage);
50 }
51}
52
53void node_copy_curves(bNodeTree * /*dest_ntree*/, bNode *dest_node, const bNode *src_node)
54{
55 dest_node->storage = BKE_curvemapping_copy(static_cast<CurveMapping *>(src_node->storage));
56}
57
59 bNode *dest_node,
60 const bNode *src_node)
61{
62 dest_node->storage = MEM_dupallocN(src_node->storage);
63}
64
65void *node_initexec_curves(bNodeExecContext * /*context*/, bNode *node, bNodeInstanceKey /*key*/)
66{
67 BKE_curvemapping_init(static_cast<CurveMapping *>(node->storage));
68 return nullptr; /* unused return */
69}
70
73/* -------------------------------------------------------------------- */
77void node_sock_label(bNodeSocket *sock, const char *name)
78{
79 STRNCPY(sock->label, name);
80}
81
83{
84 if (sock->label[0] != '\0') {
85 sock->label[0] = '\0';
86 }
87}
88
89void node_math_update(bNodeTree *ntree, bNode *node)
90{
91 bNodeSocket *sock1 = static_cast<bNodeSocket *>(BLI_findlink(&node->inputs, 0));
92 bNodeSocket *sock2 = static_cast<bNodeSocket *>(BLI_findlink(&node->inputs, 1));
93 bNodeSocket *sock3 = static_cast<bNodeSocket *>(BLI_findlink(&node->inputs, 2));
95 sock2,
96 !ELEM(node->custom1,
112 !ELEM(node->custom1,
120 sock3,
121 ELEM(node->custom1,
127
131
132 switch (node->custom1) {
133 case NODE_MATH_WRAP:
134 node_sock_label(sock2, "Max");
135 node_sock_label(sock3, "Min");
136 break;
138 node_sock_label(sock2, "Multiplier");
139 node_sock_label(sock3, "Addend");
140 break;
143 node_sock_label(sock2, "Threshold");
144 break;
146 node_sock_label(sock2, "Scale");
147 break;
148 case NODE_MATH_SNAP:
149 node_sock_label(sock2, "Increment");
150 break;
151 case NODE_MATH_POWER:
152 node_sock_label(sock1, "Base");
153 node_sock_label(sock2, "Exponent");
154 break;
156 node_sock_label(sock2, "Base");
157 break;
159 node_sock_label(sock1, "Radians");
160 break;
162 node_sock_label(sock1, "Degrees");
163 break;
165 node_sock_label(sock3, "Epsilon");
166 break;
169 node_sock_label(sock3, "Distance");
170 break;
171 }
172}
173
176/* -------------------------------------------------------------------- */
180void node_blend_label(const bNodeTree * /*ntree*/,
181 const bNode *node,
182 char *label,
183 int label_maxncpy)
184{
185 const char *name;
186 bool enum_label = RNA_enum_name(rna_enum_ramp_blend_items, node->custom1, &name);
187 if (!enum_label) {
188 name = IFACE_("Unknown");
189 }
190 BLI_strncpy_utf8(label, IFACE_(name), label_maxncpy);
191}
192
193void node_image_label(const bNodeTree * /*ntree*/,
194 const bNode *node,
195 char *label,
196 int label_maxncpy)
197{
198 /* If there is no loaded image, return an empty string,
199 * and let blender::bke::nodeLabel() fill in the proper type translation. */
200 BLI_strncpy(label, (node->id) ? node->id->name + 2 : "", label_maxncpy);
201}
202
203void node_math_label(const bNodeTree * /*ntree*/,
204 const bNode *node,
205 char *label,
206 int label_maxncpy)
207{
208 const char *name;
209 bool enum_label = RNA_enum_name(rna_enum_node_math_items, node->custom1, &name);
210 if (!enum_label) {
211 name = IFACE_("Unknown");
212 }
214}
215
216void node_vector_math_label(const bNodeTree * /*ntree*/,
217 const bNode *node,
218 char *label,
219 int label_maxncpy)
220{
221 const char *name;
222 bool enum_label = RNA_enum_name(rna_enum_node_vec_math_items, node->custom1, &name);
223 if (!enum_label) {
224 name = IFACE_("Unknown");
225 }
227}
228
229void node_filter_label(const bNodeTree * /*ntree*/,
230 const bNode *node,
231 char *label,
232 int label_maxncpy)
233{
234 const char *name;
235 bool enum_label = RNA_enum_name(rna_enum_node_filter_items, node->custom1, &name);
236 if (!enum_label) {
237 name = IFACE_("Unknown");
238 }
239 BLI_strncpy_utf8(label, IFACE_(name), label_maxncpy);
240}
241
243{
244 bNodeSocket *sock1 = (bNodeSocket *)sockets->first;
245 bNodeSocket *sock2 = sock1->next;
246 bNodeSocket *sock3 = sock2->next;
247
251
252 switch (mode) {
254 node_sock_label(sock1, "Red");
255 node_sock_label(sock2, "Green");
256 node_sock_label(sock3, "Blue");
257 break;
259 node_sock_label(sock1, "Hue");
260 node_sock_label(sock2, "Saturation");
261 node_sock_label(sock3, "Lightness");
262 break;
264 node_sock_label(sock1, "Hue");
265 node_sock_label(sock2, "Saturation");
266 node_sock_label(sock3, "Value");
267 break;
268 default: {
270 break;
271 }
272 }
273}
274
277/* -------------------------------------------------------------------- */
282 bNode * /*node*/,
283 bNodeLink * /*inserted_link*/)
284{
285 return true;
286}
287
290/* -------------------------------------------------------------------- */
294float node_socket_get_float(bNodeTree *ntree, bNode * /*node*/, bNodeSocket *sock)
295{
296 PointerRNA ptr = RNA_pointer_create((ID *)ntree, &RNA_NodeSocket, sock);
297 return RNA_float_get(&ptr, "default_value");
298}
299
300void node_socket_set_float(bNodeTree *ntree, bNode * /*node*/, bNodeSocket *sock, float value)
301{
302 PointerRNA ptr = RNA_pointer_create((ID *)ntree, &RNA_NodeSocket, sock);
303 RNA_float_set(&ptr, "default_value", value);
304}
305
306void node_socket_get_color(bNodeTree *ntree, bNode * /*node*/, bNodeSocket *sock, float *value)
307{
308 PointerRNA ptr = RNA_pointer_create((ID *)ntree, &RNA_NodeSocket, sock);
309 RNA_float_get_array(&ptr, "default_value", value);
310}
311
313 bNode * /*node*/,
314 bNodeSocket *sock,
315 const float *value)
316{
317 PointerRNA ptr = RNA_pointer_create((ID *)ntree, &RNA_NodeSocket, sock);
318 RNA_float_set_array(&ptr, "default_value", value);
319}
320
321void node_socket_get_vector(bNodeTree *ntree, bNode * /*node*/, bNodeSocket *sock, float *value)
322{
323 PointerRNA ptr = RNA_pointer_create((ID *)ntree, &RNA_NodeSocket, sock);
324 RNA_float_get_array(&ptr, "default_value", value);
325}
326
328 bNode * /*node*/,
329 bNodeSocket *sock,
330 const float *value)
331{
332 PointerRNA ptr = RNA_pointer_create((ID *)ntree, &RNA_NodeSocket, sock);
333 RNA_float_set_array(&ptr, "default_value", value);
334}
335
CurveMapping * BKE_curvemapping_copy(const CurveMapping *cumap)
void BKE_curvemapping_init(CurveMapping *cumap)
void BKE_curvemapping_free(CurveMapping *cumap)
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
void * BLI_findlink(const struct ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
#define STRNCPY(dst, src)
Definition BLI_string.h:593
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
#define ELEM(...)
#define BLT_I18NCONTEXT_ID_NODETREE
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
@ NODE_MATH_SINH
@ NODE_MATH_SMOOTH_MIN
@ NODE_MATH_TRUNC
@ NODE_MATH_COSH
@ NODE_MATH_SIGN
@ NODE_MATH_DEGREES
@ NODE_MATH_ABSOLUTE
@ NODE_MATH_SINE
@ NODE_MATH_ARCCOSINE
@ NODE_MATH_MULTIPLY_ADD
@ NODE_MATH_POWER
@ NODE_MATH_WRAP
@ NODE_MATH_ARCTANGENT
@ NODE_MATH_SQRT
@ NODE_MATH_CEIL
@ NODE_MATH_TANH
@ NODE_MATH_GREATER_THAN
@ NODE_MATH_FRACTION
@ NODE_MATH_EXPONENT
@ NODE_MATH_LESS_THAN
@ NODE_MATH_ARCSINE
@ NODE_MATH_LOGARITHM
@ NODE_MATH_COMPARE
@ NODE_MATH_INV_SQRT
@ NODE_MATH_PINGPONG
@ NODE_MATH_ROUND
@ NODE_MATH_FLOOR
@ NODE_MATH_COSINE
@ NODE_MATH_SNAP
@ NODE_MATH_TANGENT
@ NODE_MATH_SMOOTH_MAX
@ NODE_MATH_RADIANS
NodeCombSepColorMode
@ NODE_COMBSEP_COLOR_RGB
@ NODE_COMBSEP_COLOR_HSV
@ NODE_COMBSEP_COLOR_HSL
Read Guarded memory(de)allocation.
const char * label
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_dupallocN)(const void *vmemh)
Definition mallocn.cc:39
void node_set_socket_availability(bNodeTree *ntree, bNodeSocket *sock, bool is_available)
Definition node.cc:3911
void node_sock_label_clear(bNodeSocket *sock)
Definition node_util.cc:82
void node_sock_label(bNodeSocket *sock, const char *name)
Definition node_util.cc:77
void node_filter_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:229
void * node_initexec_curves(bNodeExecContext *, bNode *node, bNodeInstanceKey)
Definition node_util.cc:65
void node_vector_math_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:216
void node_free_standard_storage(bNode *node)
Definition node_util.cc:46
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:58
void node_math_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:203
void node_copy_curves(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:53
bool node_insert_link_default(bNodeTree *, bNode *, bNodeLink *)
Definition node_util.cc:281
void node_blend_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:180
void node_free_curves(bNode *node)
Definition node_util.cc:41
void node_math_update(bNodeTree *ntree, bNode *node)
Definition node_util.cc:89
void node_socket_get_color(bNodeTree *ntree, bNode *, bNodeSocket *sock, float *value)
Definition node_util.cc:306
float node_socket_get_float(bNodeTree *ntree, bNode *, bNodeSocket *sock)
Definition node_util.cc:294
void node_image_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:193
void node_socket_set_vector(bNodeTree *ntree, bNode *, bNodeSocket *sock, const float *value)
Definition node_util.cc:327
void node_socket_set_float(bNodeTree *ntree, bNode *, bNodeSocket *sock, float value)
Definition node_util.cc:300
void node_combsep_color_label(const ListBase *sockets, NodeCombSepColorMode mode)
Definition node_util.cc:242
void node_socket_set_color(bNodeTree *ntree, bNode *, bNodeSocket *sock, const float *value)
Definition node_util.cc:312
void node_socket_get_vector(bNodeTree *ntree, bNode *, bNodeSocket *sock, float *value)
Definition node_util.cc:321
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
float RNA_float_get(PointerRNA *ptr, const char *name)
void RNA_float_set(PointerRNA *ptr, const char *name, float value)
bool RNA_enum_name(const EnumPropertyItem *item, const int value, const char **r_name)
void RNA_float_set_array(PointerRNA *ptr, const char *name, const float *values)
PointerRNA RNA_pointer_create(ID *id, StructRNA *type, void *data)
const EnumPropertyItem rna_enum_ramp_blend_items[]
const EnumPropertyItem rna_enum_node_math_items[]
const EnumPropertyItem rna_enum_node_vec_math_items[]
const EnumPropertyItem rna_enum_node_filter_items[]
Definition DNA_ID.h:413
void * first
struct bNodeSocket * next
void * storage
PointerRNA * ptr
Definition wm_files.cc:4126