Welcome to mirror list, hosted at ThFree Co, Russian Federation.

node_socket.c « intern « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2ac1a2c85f38f5f4f6aa8be6e783e52db64ad1ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2007 Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Lukas Toennne
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/nodes/intern/node_socket.c
 *  \ingroup nodes
 */

#include <limits.h>

#include "DNA_node_types.h"

#include "BLI_listbase.h"
#include "BLI_math.h"
#include "BLI_utildefines.h"
#include "BLI_string.h"

#include "BKE_node.h"

#include "RNA_access.h"
#include "RNA_define.h"
#include "RNA_types.h"

#include "MEM_guardedalloc.h"

#include "NOD_socket.h"

struct Main;

struct bNodeSocket *node_add_socket_from_template(struct bNodeTree *ntree, struct bNode *node, struct bNodeSocketTemplate *stemp, int in_out)
{
	bNodeSocket *sock = nodeAddStaticSocket(ntree, node, in_out, stemp->type, stemp->subtype, stemp->identifier, stemp->name);
	
	sock->flag |= stemp->flag;
	
	/* initialize default_value */
	switch (stemp->type) {
		case SOCK_FLOAT:
		{
			bNodeSocketValueFloat *dval = sock->default_value;
			dval->value = stemp->val1;
			dval->min = stemp->min;
			dval->max = stemp->max;
			break;
		}
		case SOCK_INT:
		{
			bNodeSocketValueInt *dval = sock->default_value;
			dval->value = (int)stemp->val1;
			dval->min = (int)stemp->min;
			dval->max = (int)stemp->max;
			break;
		}
		case SOCK_BOOLEAN:
		{
			bNodeSocketValueBoolean *dval = sock->default_value;
			dval->value = (int)stemp->val1;
			break;
		}
		case SOCK_VECTOR:
		{
			bNodeSocketValueVector *dval = sock->default_value;
			dval->value[0] = stemp->val1;
			dval->value[1] = stemp->val2;
			dval->value[2] = stemp->val3;
			dval->min = stemp->min;
			dval->max = stemp->max;
			break;
		}
		case SOCK_RGBA:
		{
			bNodeSocketValueRGBA *dval = sock->default_value;
			dval->value[0] = stemp->val1;
			dval->value[1] = stemp->val2;
			dval->value[2] = stemp->val3;
			dval->value[3] = stemp->val4;
			break;
		}
	}
	
	return sock;
}

static bNodeSocket *verify_socket_template(bNodeTree *ntree, bNode *node, int in_out, ListBase *socklist, bNodeSocketTemplate *stemp)
{
	bNodeSocket *sock;
	
	for (sock = socklist->first; sock; sock = sock->next) {
		if (strncmp(sock->name, stemp->name, NODE_MAXSTR) == 0)
			break;
	}
	if (sock) {
		sock->type = stemp->type;
		sock->limit = (stemp->limit == 0 ? 0xFFF : stemp->limit);
		sock->flag |= stemp->flag;
		
		BLI_remlink(socklist, sock);
		
		return sock;
	}
	else {
		/* no socket for this template found, make a new one */
		sock = node_add_socket_from_template(ntree, node, stemp, in_out);
		/* remove the new socket from the node socket list first,
		 * will be added back after verification.
		 */
		BLI_remlink(socklist, sock);
	}
	
	return sock;
}

static void verify_socket_template_list(bNodeTree *ntree, bNode *node, int in_out, ListBase *socklist, bNodeSocketTemplate *stemp_first)
{
	bNodeSocket *sock, *nextsock;
	bNodeSocketTemplate *stemp;
	
	/* no inputs anymore? */
	if (stemp_first == NULL) {
		for (sock = (bNodeSocket *)socklist->first; sock; sock = nextsock) {
			nextsock = sock->next;
			nodeRemoveSocket(ntree, node, sock);
		}
	}
	else {
		/* step by step compare */
		stemp = stemp_first;
		while (stemp->type != -1) {
			stemp->sock = verify_socket_template(ntree, node, in_out, socklist, stemp);
			stemp++;
		}
		/* leftovers are removed */
		for (sock = (bNodeSocket *)socklist->first; sock; sock = nextsock) {
			nextsock = sock->next;
			nodeRemoveSocket(ntree, node, sock);
		}
		
		/* and we put back the verified sockets */
		stemp = stemp_first;
		if (socklist->first) {
			/* some dynamic sockets left, store the list start
			 * so we can add static sockets infront of it.
			 */
			sock = socklist->first;
			while (stemp->type != -1) {
				/* put static sockets infront of dynamic */
				BLI_insertlinkbefore(socklist, sock, stemp->sock);
				stemp++;
			}
		}
		else {
			while (stemp->type != -1) {
				BLI_addtail(socklist, stemp->sock);
				stemp++;
			}
		}
	}
}

void node_verify_socket_templates(bNodeTree *ntree, bNode *node)
{
	bNodeType *ntype = node->typeinfo;
	/* Don't try to match socket lists when there are no templates.
	 * This prevents group node sockets from being removed, without the need to explicitly
	 * check the node type here.
	 */
	if (ntype) {
		if (ntype->inputs && ntype->inputs[0].type >= 0)
			verify_socket_template_list(ntree, node, SOCK_IN, &node->inputs, ntype->inputs);
		if (ntype->outputs && ntype->outputs[0].type >= 0)
			verify_socket_template_list(ntree, node, SOCK_OUT, &node->outputs, ntype->outputs);
	}
}


void node_socket_init_default_value(bNodeSocket *sock)
{
	int type = sock->typeinfo->type;
	int subtype = sock->typeinfo->subtype;
	
	if (sock->default_value)
		return; /* already initialized */
	
	switch (type) {
		case SOCK_FLOAT:
		{
			bNodeSocketValueFloat *dval = MEM_callocN(sizeof(bNodeSocketValueFloat), "node socket value float");
			dval->subtype = subtype;
			dval->value = 0.0f;
			dval->min = -FLT_MAX;
			dval->max = FLT_MAX;
		
			sock->default_value = dval;
			break;
		}
		case SOCK_INT:
		{
			bNodeSocketValueInt *dval = MEM_callocN(sizeof(bNodeSocketValueInt), "node socket value int");
			dval->subtype = subtype;
			dval->value = 0;
			dval->min = INT_MIN;
			dval->max = INT_MAX;
		
			sock->default_value = dval;
			break;
		}
		case SOCK_BOOLEAN:
		{
			bNodeSocketValueBoolean *dval = MEM_callocN(sizeof(bNodeSocketValueBoolean), "node socket value bool");
			dval->value = false;
		
			sock->default_value = dval;
			break;
		}
		case SOCK_VECTOR:
		{
			static float default_value[] = { 0.0f, 0.0f, 0.0f };
			bNodeSocketValueVector *dval = MEM_callocN(sizeof(bNodeSocketValueVector), "node socket value vector");
			dval->subtype = subtype;
			copy_v3_v3(dval->value, default_value);
			dval->min = -FLT_MAX;
			dval->max = FLT_MAX;
		
			sock->default_value = dval;
			break;
		}
		case SOCK_RGBA:
		{
			static float default_value[] = { 0.0f, 0.0f, 0.0f, 1.0f };
			bNodeSocketValueRGBA *dval = MEM_callocN(sizeof(bNodeSocketValueRGBA), "node socket value color");
			copy_v4_v4(dval->value, default_value);
		
			sock->default_value = dval;
			break;
		}
		case SOCK_STRING:
		{
			bNodeSocketValueString *dval = MEM_callocN(sizeof(bNodeSocketValueString), "node socket value string");
			dval->subtype = subtype;
			dval->value[0] = '\0';
		
			sock->default_value = dval;
			break;
		}
	}
}

void node_socket_copy_default_value(bNodeSocket *to, bNodeSocket *from)
{
	/* sanity check */
	if (to->type != from->type)
		return;
	
	/* make sure both exist */
	if (!from->default_value)
		return;
	node_socket_init_default_value(to);
	
	switch (from->typeinfo->type) {
		case SOCK_FLOAT:
		{
			bNodeSocketValueFloat *toval = to->default_value;
			bNodeSocketValueFloat *fromval = from->default_value;
			*toval = *fromval;
			break;
		}
		case SOCK_INT:
		{
			bNodeSocketValueInt *toval = to->default_value;
			bNodeSocketValueInt *fromval = from->default_value;
			*toval = *fromval;
			break;
		}
		case SOCK_BOOLEAN:
		{
			bNodeSocketValueBoolean *toval = to->default_value;
			bNodeSocketValueBoolean *fromval = from->default_value;
			*toval = *fromval;
			break;
		}
		case SOCK_VECTOR:
		{
			bNodeSocketValueVector *toval = to->default_value;
			bNodeSocketValueVector *fromval = from->default_value;
			*toval = *fromval;
			break;
		}
		case SOCK_RGBA:
		{
			bNodeSocketValueRGBA *toval = to->default_value;
			bNodeSocketValueRGBA *fromval = from->default_value;
			*toval = *fromval;
			break;
		}
		case SOCK_STRING:
		{
			bNodeSocketValueString *toval = to->default_value;
			bNodeSocketValueString *fromval = from->default_value;
			*toval = *fromval;
			break;
		}
	}

	to->flag |= (from->flag & SOCK_HIDE_VALUE);
}

static void standard_node_socket_interface_init_socket(bNodeTree *UNUSED(ntree), bNodeSocket *stemp, bNode *UNUSED(node), bNodeSocket *sock, const char *UNUSED(data_path))
{
	/* initialize the type value */
	sock->type = sock->typeinfo->type;
	
	/* XXX socket interface 'type' value is not used really,
	 * but has to match or the copy function will bail out
	 */
	stemp->type = stemp->typeinfo->type;
	/* copy default_value settings */
	node_socket_copy_default_value(sock, stemp);
}

/* copies settings that are not changed for each socket instance */
static void standard_node_socket_interface_verify_socket(bNodeTree *UNUSED(ntree), bNodeSocket *stemp, bNode *UNUSED(node), bNodeSocket *sock, const char *UNUSED(data_path))
{
	/* sanity check */
	if (sock->type != stemp->typeinfo->type)
		return;
	
	/* make sure both exist */
	if (!stemp->default_value)
		return;
	node_socket_init_default_value(sock);
	
	switch (stemp->typeinfo->type) {
		case SOCK_FLOAT:
		{
			bNodeSocketValueFloat *toval = sock->default_value;
			bNodeSocketValueFloat *fromval = stemp->default_value;
			toval->min = fromval->min;
			toval->max = fromval->max;
			break;
		}
		case SOCK_INT:
		{
			bNodeSocketValueInt *toval = sock->default_value;
			bNodeSocketValueInt *fromval = stemp->default_value;
			toval->min = fromval->min;
			toval->max = fromval->max;
			break;
		}
		case SOCK_VECTOR:
		{
			bNodeSocketValueVector *toval = sock->default_value;
			bNodeSocketValueVector *fromval = stemp->default_value;
			toval->min = fromval->min;
			toval->max = fromval->max;
			break;
		}
	}
}

static void standard_node_socket_interface_from_socket(bNodeTree *UNUSED(ntree), bNodeSocket *stemp, bNode *UNUSED(node), bNodeSocket *sock)
{
	/* initialize settings */
	stemp->type = stemp->typeinfo->type;
	node_socket_copy_default_value(stemp, sock);
}

static bNodeSocketType *make_standard_socket_type(int type, int subtype)
{
	extern void ED_init_standard_node_socket_type(bNodeSocketType *);
	
	const char *socket_idname = nodeStaticSocketType(type, subtype);
	const char *interface_idname = nodeStaticSocketInterfaceType(type, subtype);
	bNodeSocketType *stype;
	StructRNA *srna;
	
	stype = MEM_callocN(sizeof(bNodeSocketType), "node socket C type");
	BLI_strncpy(stype->idname, socket_idname, sizeof(stype->idname));
	
	/* set the RNA type
	 * uses the exact same identifier as the socket type idname */
	srna = stype->ext_socket.srna = RNA_struct_find(socket_idname);
	BLI_assert(srna != NULL);
	/* associate the RNA type with the socket type */
	RNA_struct_blender_type_set(srna, stype);
	
	/* set the interface RNA type */
	srna = stype->ext_interface.srna = RNA_struct_find(interface_idname);
	BLI_assert(srna != NULL);
	/* associate the RNA type with the socket type */
	RNA_struct_blender_type_set(srna, stype);
	
	/* extra type info for standard socket types */
	stype->type = type;
	stype->subtype = subtype;
	
	/* XXX bad-level call! needed for setting draw callbacks */
	ED_init_standard_node_socket_type(stype);
	
	stype->interface_init_socket = standard_node_socket_interface_init_socket;
	stype->interface_from_socket = standard_node_socket_interface_from_socket;
	stype->interface_verify_socket = standard_node_socket_interface_verify_socket;
	
	return stype;
}

static bNodeSocketType *make_socket_type_virtual(void)
{
	extern void ED_init_node_socket_type_virtual(bNodeSocketType *);
	
	const char *socket_idname = "NodeSocketVirtual";
	bNodeSocketType *stype;
	StructRNA *srna;
	
	stype = MEM_callocN(sizeof(bNodeSocketType), "node socket C type");
	BLI_strncpy(stype->idname, socket_idname, sizeof(stype->idname));
	
	/* set the RNA type
	 * uses the exact same identifier as the socket type idname */
	srna = stype->ext_socket.srna = RNA_struct_find(socket_idname);
	BLI_assert(srna != NULL);
	/* associate the RNA type with the socket type */
	RNA_struct_blender_type_set(srna, stype);
	
	/* extra type info for standard socket types */
	stype->type = SOCK_CUSTOM;
	
	ED_init_node_socket_type_virtual(stype);
	
	return stype;
}


void register_standard_node_socket_types(void)
{
	/* draw callbacks are set in drawnode.c to avoid bad-level calls */
	
	nodeRegisterSocketType(make_standard_socket_type(SOCK_FLOAT, PROP_NONE));
	nodeRegisterSocketType(make_standard_socket_type(SOCK_FLOAT, PROP_UNSIGNED));
	nodeRegisterSocketType(make_standard_socket_type(SOCK_FLOAT, PROP_PERCENTAGE));
	nodeRegisterSocketType(make_standard_socket_type(SOCK_FLOAT, PROP_FACTOR));
	nodeRegisterSocketType(make_standard_socket_type(SOCK_FLOAT, PROP_ANGLE));
	nodeRegisterSocketType(make_standard_socket_type(SOCK_FLOAT, PROP_TIME));
	
	nodeRegisterSocketType(make_standard_socket_type(SOCK_INT, PROP_NONE));
	nodeRegisterSocketType(make_standard_socket_type(SOCK_INT, PROP_UNSIGNED));
	nodeRegisterSocketType(make_standard_socket_type(SOCK_INT, PROP_PERCENTAGE));
	nodeRegisterSocketType(make_standard_socket_type(SOCK_INT, PROP_FACTOR));
	
	nodeRegisterSocketType(make_standard_socket_type(SOCK_BOOLEAN, PROP_NONE));
	
	nodeRegisterSocketType(make_standard_socket_type(SOCK_VECTOR, PROP_NONE));
	nodeRegisterSocketType(make_standard_socket_type(SOCK_VECTOR, PROP_TRANSLATION));
	nodeRegisterSocketType(make_standard_socket_type(SOCK_VECTOR, PROP_DIRECTION));
	nodeRegisterSocketType(make_standard_socket_type(SOCK_VECTOR, PROP_VELOCITY));
	nodeRegisterSocketType(make_standard_socket_type(SOCK_VECTOR, PROP_ACCELERATION));
	nodeRegisterSocketType(make_standard_socket_type(SOCK_VECTOR, PROP_EULER));
	nodeRegisterSocketType(make_standard_socket_type(SOCK_VECTOR, PROP_XYZ));
	
	nodeRegisterSocketType(make_standard_socket_type(SOCK_RGBA, PROP_NONE));
	
	nodeRegisterSocketType(make_standard_socket_type(SOCK_STRING, PROP_NONE));
	
	nodeRegisterSocketType(make_standard_socket_type(SOCK_SHADER, PROP_NONE));
	
	nodeRegisterSocketType(make_socket_type_virtual());
}