2
* ***** BEGIN GPL LICENSE BLOCK *****
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software Foundation,
16
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
* The Original Code is Copyright (C) 2007 Blender Foundation.
19
* All rights reserved.
21
* The Original Code is: all of this file.
23
* Contributor(s): Lukas Toennne
25
* ***** END GPL LICENSE BLOCK *****
28
/** \file blender/nodes/intern/node_socket.c
34
#include "DNA_node_types.h"
36
#include "DNA_mesh_types.h"
37
#include "DNA_meshdata_types.h"
38
#include "DNA_object_types.h"
39
#include "DNA_scene_types.h"
41
#include "BLI_listbase.h"
43
#include "BLI_utildefines.h"
45
#include "BKE_DerivedMesh.h"
48
#include "RNA_access.h"
49
#include "RNA_types.h"
51
#include "MEM_guardedalloc.h"
53
#include "NOD_socket.h"
55
/****************** FLOAT ******************/
57
static bNodeSocketType node_socket_type_float = {
58
/* type */ SOCK_FLOAT,
59
/* ui_name */ "Float",
60
/* ui_description */ "Floating Point",
62
/* ui_color */ {160,160,160,255},
64
/* value_structname */ "bNodeSocketValueFloat",
65
/* value_structsize */ sizeof(bNodeSocketValueFloat),
67
/* buttonfunc */ NULL,
70
/****************** VECTOR ******************/
72
static bNodeSocketType node_socket_type_vector = {
73
/* type */ SOCK_VECTOR,
74
/* ui_name */ "Vector",
75
/* ui_description */ "3-dimensional floating point vector",
77
/* ui_color */ {100,100,200,255},
79
/* value_structname */ "bNodeSocketValueVector",
80
/* value_structsize */ sizeof(bNodeSocketValueVector),
82
/* buttonfunc */ NULL,
85
/****************** RGBA ******************/
87
static bNodeSocketType node_socket_type_rgba = {
90
/* ui_description */ "RGBA color",
92
/* ui_color */ {200,200,40,255},
94
/* value_structname */ "bNodeSocketValueRGBA",
95
/* value_structsize */ sizeof(bNodeSocketValueRGBA),
97
/* buttonfunc */ NULL,
100
/****************** INT ******************/
102
static bNodeSocketType node_socket_type_int = {
105
/* ui_description */ "Integer",
107
/* ui_color */ {17,133,37,255},
109
/* value_structname */ "bNodeSocketValueInt",
110
/* value_structsize */ sizeof(bNodeSocketValueInt),
112
/* buttonfunc */ NULL,
115
/****************** BOOLEAN ******************/
117
static bNodeSocketType node_socket_type_boolean = {
118
/* type */ SOCK_BOOLEAN,
119
/* ui_name */ "Boolean",
120
/* ui_description */ "Boolean",
122
/* ui_color */ {158,139,63,255},
124
/* value_structname */ "bNodeSocketValueBoolean",
125
/* value_structsize */ sizeof(bNodeSocketValueBoolean),
127
/* buttonfunc */ NULL,
130
/****************** SHADER ******************/
132
static bNodeSocketType node_socket_type_shader = {
133
/* type */ SOCK_SHADER,
134
/* ui_name */ "Shader",
135
/* ui_description */ "Shader",
137
/* ui_color */ {100,200,100,255},
139
/* value_structname */ NULL,
140
/* value_structsize */ 0,
142
/* buttonfunc */ NULL,
145
/****************** MESH ******************/
147
static bNodeSocketType node_socket_type_mesh = {
148
/* type */ SOCK_MESH,
149
/* ui_name */ "Mesh",
150
/* ui_description */ "Mesh geometry data",
152
/* ui_color */ {255,133,7,255},
154
/* value_structname */ NULL,
155
/* value_structsize */ 0,
157
/* buttonfunc */ NULL,
161
void node_socket_type_init(bNodeSocketType *types[])
163
#define INIT_TYPE(name) types[node_socket_type_##name.type] = &node_socket_type_##name
176
void *node_socket_make_default_value(int type)
178
/* XXX currently just allocates from stype->structsize.
179
* it might become necessary to do more complex allocations for later types.
181
bNodeSocketType *stype = ntreeGetSocketType(type);
182
if (stype->value_structsize > 0) {
183
void *default_value = MEM_callocN(stype->value_structsize, "default socket value");
184
return default_value;
190
void node_socket_free_default_value(int UNUSED(type), void *default_value)
192
/* XXX can just free the pointee for all current socket types. */
194
MEM_freeN(default_value);
197
void node_socket_init_default_value(int type, void *default_value)
201
node_socket_set_default_value_float(default_value, PROP_NONE, 0.0f, -FLT_MAX, FLT_MAX);
204
node_socket_set_default_value_int(default_value, PROP_NONE, 0, INT_MIN, INT_MAX);
207
node_socket_set_default_value_boolean(default_value, FALSE);
210
node_socket_set_default_value_vector(default_value, PROP_NONE, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX);
213
node_socket_set_default_value_rgba(default_value, 0.0f, 0.0f, 0.0f, 1.0f);
216
node_socket_set_default_value_shader(default_value);
219
node_socket_set_default_value_mesh(default_value);
224
void node_socket_set_default_value_int(void *default_value, PropertySubType subtype, int value, int min, int max)
226
bNodeSocketValueInt *val = default_value;
227
val->subtype = subtype;
233
void node_socket_set_default_value_float(void *default_value, PropertySubType subtype, float value, float min, float max)
235
bNodeSocketValueFloat *val = default_value;
236
val->subtype = subtype;
242
void node_socket_set_default_value_boolean(void *default_value, char value)
244
bNodeSocketValueBoolean *val = default_value;
248
void node_socket_set_default_value_vector(void *default_value, PropertySubType subtype, float x, float y, float z, float min, float max)
250
bNodeSocketValueVector *val = default_value;
251
val->subtype = subtype;
259
void node_socket_set_default_value_rgba(void *default_value, float r, float g, float b, float a)
261
bNodeSocketValueRGBA *val = default_value;
268
void node_socket_set_default_value_shader(void *UNUSED(default_value))
272
void node_socket_set_default_value_mesh(void *UNUSED(default_value))
277
void node_socket_copy_default_value(int type, void *to_default_value, void *from_default_value)
279
/* XXX only one of these pointers is valid! just putting them here for convenience */
280
bNodeSocketValueFloat *fromfloat= (bNodeSocketValueFloat*)from_default_value;
281
bNodeSocketValueInt *fromint= (bNodeSocketValueInt*)from_default_value;
282
bNodeSocketValueBoolean *frombool= (bNodeSocketValueBoolean*)from_default_value;
283
bNodeSocketValueVector *fromvector= (bNodeSocketValueVector*)from_default_value;
284
bNodeSocketValueRGBA *fromrgba= (bNodeSocketValueRGBA*)from_default_value;
286
bNodeSocketValueFloat *tofloat= (bNodeSocketValueFloat*)to_default_value;
287
bNodeSocketValueInt *toint= (bNodeSocketValueInt*)to_default_value;
288
bNodeSocketValueBoolean *tobool= (bNodeSocketValueBoolean*)to_default_value;
289
bNodeSocketValueVector *tovector= (bNodeSocketValueVector*)to_default_value;
290
bNodeSocketValueRGBA *torgba= (bNodeSocketValueRGBA*)to_default_value;
294
*tofloat = *fromfloat;
303
*tovector = *fromvector;
311
/* XXX This is a makeshift function to have useful initial group socket values.
312
* In the end this should be implemented by a flexible socket data conversion system,
313
* which is yet to be implemented. The idea is that beside default standard conversions,
314
* such as int-to-float, it should be possible to quickly select a conversion method or
315
* a chain of conversions for each input, whenever there is more than one option.
316
* E.g. a vector-to-float conversion could use either of the x/y/z components or
319
* In the interface this could be implemented by a pseudo-script textbox on linked inputs,
320
* with quick selection from a predefined list of conversion options. Some Examples:
321
* - vector component 'z' (vector->float): "z"
322
* - greyscale color (float->color): "grey"
323
* - color luminance (color->float): "lum"
324
* - matrix column 2 length (matrix->vector->float): "col[1].len"
325
* - mesh vertex coordinate 'y' (mesh->vertex->vector->float): "vertex.co.y"
327
* The actual conversion is then done by a series of conversion functions,
328
* which are defined in the socket type structs.
330
void node_socket_convert_default_value(int to_type, void *to_default_value, int from_type, void *from_default_value)
332
/* XXX only one of these pointers is valid! just putting them here for convenience */
333
bNodeSocketValueFloat *fromfloat= (bNodeSocketValueFloat*)from_default_value;
334
bNodeSocketValueInt *fromint= (bNodeSocketValueInt*)from_default_value;
335
bNodeSocketValueBoolean *frombool= (bNodeSocketValueBoolean*)from_default_value;
336
bNodeSocketValueVector *fromvector= (bNodeSocketValueVector*)from_default_value;
337
bNodeSocketValueRGBA *fromrgba= (bNodeSocketValueRGBA*)from_default_value;
339
bNodeSocketValueFloat *tofloat= (bNodeSocketValueFloat*)to_default_value;
340
bNodeSocketValueInt *toint= (bNodeSocketValueInt*)to_default_value;
341
bNodeSocketValueBoolean *tobool= (bNodeSocketValueBoolean*)to_default_value;
342
bNodeSocketValueVector *tovector= (bNodeSocketValueVector*)to_default_value;
343
bNodeSocketValueRGBA *torgba= (bNodeSocketValueRGBA*)to_default_value;
349
tofloat->value = fromfloat->value;
352
toint->value = (int)fromfloat->value;
355
tobool->value = (fromfloat->value > 0.0f);
358
tovector->value[0] = tovector->value[1] = tovector->value[2] = fromfloat->value;
361
torgba->value[0] = torgba->value[1] = torgba->value[2] = torgba->value[3] = fromfloat->value;
368
tofloat->value = (float)fromint->value;
371
toint->value = fromint->value;
374
tobool->value = (fromint->value > 0);
377
tovector->value[0] = tovector->value[1] = tovector->value[2] = (float)fromint->value;
380
torgba->value[0] = torgba->value[1] = torgba->value[2] = torgba->value[3] = (float)fromint->value;
387
tofloat->value = (float)frombool->value;
390
toint->value = (int)frombool->value;
393
tobool->value = frombool->value;
396
tovector->value[0] = tovector->value[1] = tovector->value[2] = (float)frombool->value;
399
torgba->value[0] = torgba->value[1] = torgba->value[2] = torgba->value[3] = (float)frombool->value;
406
tofloat->value = fromvector->value[0];
409
toint->value = (int)fromvector->value[0];
412
tobool->value = (fromvector->value[0] > 0.0f);
415
copy_v3_v3(tovector->value, fromvector->value);
418
copy_v3_v3(torgba->value, fromvector->value);
419
torgba->value[3] = 1.0f;
426
tofloat->value = fromrgba->value[0];
429
toint->value = (int)fromrgba->value[0];
432
tobool->value = (fromrgba->value[0] > 0.0f);
435
copy_v3_v3(tovector->value, fromrgba->value);
438
copy_v4_v4(torgba->value, fromrgba->value);
446
struct bNodeSocket *node_add_input_from_template(struct bNodeTree *ntree, struct bNode *node, struct bNodeSocketTemplate *stemp)
448
bNodeSocket *sock = nodeAddSocket(ntree, node, SOCK_IN, stemp->name, stemp->type);
449
sock->flag |= stemp->flag;
451
switch (stemp->type) {
453
node_socket_set_default_value_int(sock->default_value, stemp->subtype, (int)stemp->val1, (int)stemp->min, (int)stemp->max);
456
node_socket_set_default_value_float(sock->default_value, stemp->subtype, stemp->val1, stemp->min, stemp->max);
459
node_socket_set_default_value_boolean(sock->default_value, (char)stemp->val1);
462
node_socket_set_default_value_vector(sock->default_value, stemp->subtype, stemp->val1, stemp->val2, stemp->val3, stemp->min, stemp->max);
465
node_socket_set_default_value_rgba(sock->default_value, stemp->val1, stemp->val2, stemp->val3, stemp->val4);
468
node_socket_set_default_value_shader(sock->default_value);
471
node_socket_set_default_value_mesh(sock->default_value);
478
struct bNodeSocket *node_add_output_from_template(struct bNodeTree *ntree, struct bNode *node, struct bNodeSocketTemplate *stemp)
480
bNodeSocket *sock = nodeAddSocket(ntree, node, SOCK_OUT, stemp->name, stemp->type);
484
static bNodeSocket *verify_socket_template(bNodeTree *ntree, bNode *node, int in_out, ListBase *socklist, bNodeSocketTemplate *stemp)
488
for (sock= socklist->first; sock; sock= sock->next) {
489
if (!(sock->flag & SOCK_DYNAMIC) && strncmp(sock->name, stemp->name, NODE_MAXSTR)==0)
493
sock->type= stemp->type; /* in future, read this from tydefs! */
494
if (stemp->limit==0) sock->limit= 0xFFF;
495
else sock->limit= stemp->limit;
496
sock->flag |= stemp->flag;
498
/* Copy the property range and subtype parameters in case the template changed.
499
* NOT copying the actual value here, only button behavior changes!
501
switch (sock->type) {
504
bNodeSocketValueFloat *dval= sock->default_value;
505
dval->min = stemp->min;
506
dval->max = stemp->max;
507
dval->subtype = stemp->subtype;
512
bNodeSocketValueInt *dval= sock->default_value;
513
dval->min = stemp->min;
514
dval->max = stemp->max;
515
dval->subtype = stemp->subtype;
520
bNodeSocketValueVector *dval= sock->default_value;
521
dval->min = stemp->min;
522
dval->max = stemp->max;
523
dval->subtype = stemp->subtype;
528
BLI_remlink(socklist, sock);
533
/* no socket for this template found, make a new one */
535
sock = node_add_input_from_template(ntree, node, stemp);
537
sock = node_add_output_from_template(ntree, node, stemp);
538
/* remove the new socket from the node socket list first,
539
* will be added back after verification.
541
BLI_remlink(socklist, sock);
547
static void verify_socket_template_list(bNodeTree *ntree, bNode *node, int in_out, ListBase *socklist, bNodeSocketTemplate *stemp_first)
549
bNodeSocket *sock, *nextsock;
550
bNodeSocketTemplate *stemp;
552
/* no inputs anymore? */
553
if (stemp_first==NULL) {
554
for (sock = (bNodeSocket*)socklist->first; sock; sock=nextsock) {
555
nextsock = sock->next;
556
if (!(sock->flag & SOCK_DYNAMIC))
557
nodeRemoveSocket(ntree, node, sock);
561
/* step by step compare */
563
while (stemp->type != -1) {
564
stemp->sock= verify_socket_template(ntree, node, in_out, socklist, stemp);
567
/* leftovers are removed */
568
for (sock = (bNodeSocket*)socklist->first; sock; sock=nextsock) {
569
nextsock = sock->next;
570
if (!(sock->flag & SOCK_DYNAMIC))
571
nodeRemoveSocket(ntree, node, sock);
574
/* and we put back the verified sockets */
576
if (socklist->first) {
577
/* some dynamic sockets left, store the list start
578
* so we can add static sockets infront of it.
580
sock = socklist->first;
581
while (stemp->type != -1) {
582
/* put static sockets infront of dynamic */
583
BLI_insertlinkbefore(socklist, sock, stemp->sock);
588
while (stemp->type != -1) {
589
BLI_addtail(socklist, stemp->sock);
596
void node_verify_socket_templates(bNodeTree *ntree, bNode *node)
598
bNodeType *ntype= node->typeinfo;
599
/* XXX Small trick: don't try to match socket lists when there are no templates.
600
* This also prevents group node sockets from being removed, without the need to explicitly
601
* check the node type here.
603
if (ntype && ((ntype->inputs && ntype->inputs[0].type>=0) || (ntype->outputs && ntype->outputs[0].type>=0))) {
604
verify_socket_template_list(ntree, node, SOCK_IN, &node->inputs, ntype->inputs);
605
verify_socket_template_list(ntree, node, SOCK_OUT, &node->outputs, ntype->outputs);