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

COM_GroupNode.cpp « nodes « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7c0499dc04eac7ae5d8b4a6e18c1cdd782bdf2a4 (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
/*
 * Copyright 2011, Blender Foundation.
 *
 * 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.
 *
 * Contributor: 
 *		Jeroen Bakker 
 *		Monique Dewanchand
 */

#include "BKE_node.h"

#include "COM_GroupNode.h"
#include "COM_SocketProxyNode.h"
#include "COM_SetColorOperation.h"
#include "COM_ExecutionSystemHelper.h"
#include "COM_SetValueOperation.h"
#include "COM_SetVectorOperation.h"
#include "COM_SetColorOperation.h"

extern "C" {
#include "RNA_access.h"
}

GroupNode::GroupNode(bNode *editorNode) : Node(editorNode)
{
	/* pass */
}

void GroupNode::convertToOperations(ExecutionSystem *graph, CompositorContext *context)
{
	if (this->getbNode()->id == NULL) {
		convertToOperations_invalid(graph, context);
	}
}

static int find_group_input(GroupNode *gnode, const char *identifier, InputSocket **r_sock)
{
	int index;
	for (index = 0; index < gnode->getNumberOfInputSockets(); ++index) {
		InputSocket *sock = gnode->getInputSocket(index);
		if (STREQ(sock->getbNodeSocket()->identifier, identifier)) {
			*r_sock = sock;
			return index;
		}
	}
	*r_sock = NULL;
	return -1;
}

static int find_group_output(GroupNode *gnode, const char *identifier, OutputSocket **r_sock)
{
	int index;
	for (index = 0; index < gnode->getNumberOfOutputSockets(); ++index) {
		OutputSocket *sock = gnode->getOutputSocket(index);
		if (STREQ(sock->getbNodeSocket()->identifier, identifier)) {
			*r_sock = sock;
			return index;
		}
	}
	*r_sock = NULL;
	return -1;
}

void GroupNode::ungroup(ExecutionSystem &system)
{
	bNode *bnode = this->getbNode();
	bNodeTree *subtree = (bNodeTree *)bnode->id;

	/* get the node list size _before_ adding proxy nodes, so they are available for linking */
	int nodes_start = system.getNodes().size();

	/* missing node group datablock can happen with library linking */
	if (!subtree) {
		/* this error case its handled in convertToOperations() so we don't get un-convertred sockets */
		return;
	}

	const bool groupnodeBuffering = system.getContext().isGroupnodeBufferEnabled();

	bool has_output = false;
	/* create proxy nodes for group input/output nodes */
	for (bNode *bionode = (bNode *)subtree->nodes.first; bionode; bionode = bionode->next) {
		if (bionode->type == NODE_GROUP_INPUT) {
			for (bNodeSocket *bsock = (bNodeSocket *)bionode->outputs.first; bsock; bsock = bsock->next) {
				InputSocket *gsock;
				int gsock_index = find_group_input(this, bsock->identifier, &gsock);
				/* ignore virtual sockets */
				if (gsock) {
					SocketProxyNode *proxy = new SocketProxyNode(bionode, gsock->getbNodeSocket(), bsock, false);
					ExecutionSystemHelper::addNode(system.getNodes(), proxy);
					
					gsock->relinkConnectionsDuplicate(proxy->getInputSocket(0), gsock_index, &system);
				}
			}
		}
		
		if (bionode->type == NODE_GROUP_OUTPUT && (bionode->flag & NODE_DO_OUTPUT)) {
			has_output = true;
			for (bNodeSocket *bsock = (bNodeSocket *)bionode->inputs.first; bsock; bsock = bsock->next) {
				OutputSocket *gsock;
				find_group_output(this, bsock->identifier, &gsock);
				/* ignore virtual sockets */
				if (gsock) {
					SocketProxyNode *proxy = new SocketProxyNode(bionode, bsock, gsock->getbNodeSocket(), groupnodeBuffering);
					ExecutionSystemHelper::addNode(system.getNodes(), proxy);
					
					gsock->relinkConnections(proxy->getOutputSocket(0));
				}
			}
		}
	}
	
	/* in case no output node exists, add input value operations using defaults */
	if (!has_output) {
		for (int index = 0; index < getNumberOfOutputSockets(); ++index) {
			OutputSocket *output = getOutputSocket(index);
			addDefaultOutputOperation(system, output);
		}
	}
	
	/* unlink the group node itself, input links have been duplicated */
	for (int index = 0; index < this->getNumberOfInputSockets(); ++index) {
		InputSocket *sock = this->getInputSocket(index);
		sock->unlinkConnections(&system);
	}
	for (int index = 0; index < this->getNumberOfOutputSockets(); ++index) {
		OutputSocket *sock = this->getOutputSocket(index);
		sock->clearConnections();
	}
	
	ExecutionSystemHelper::addbNodeTree(system, nodes_start, subtree, this->getInstanceKey());
}

bNodeSocket *GroupNode::findInterfaceInput(InputSocket *socket)
{
	bNode *bnode = this->getbNode();
	bNodeTree *subtree = (bNodeTree *)bnode->id;
	if (!subtree)
		return NULL;
	
	const char *identifier = socket->getbNodeSocket()->identifier;
	for (bNodeSocket *iosock = (bNodeSocket *)subtree->inputs.first; iosock; iosock = iosock->next)
		if (STREQ(iosock->identifier, identifier))
			return iosock;
	return NULL;
}

bNodeSocket *GroupNode::findInterfaceOutput(OutputSocket *socket)
{
	bNode *bnode = this->getbNode();
	bNodeTree *subtree = (bNodeTree *)bnode->id;
	if (!subtree)
		return NULL;
	
	const char *identifier = socket->getbNodeSocket()->identifier;
	for (bNodeSocket *iosock = (bNodeSocket *)subtree->outputs.first; iosock; iosock = iosock->next)
		if (STREQ(iosock->identifier, identifier))
			return iosock;
	return NULL;
}

void GroupNode::addDefaultOutputOperation(ExecutionSystem &system, OutputSocket *outputsocket)
{
	bNodeSocket *iosock = findInterfaceOutput(outputsocket);
	if (!iosock)
		return;
	
	PointerRNA ptr;
	RNA_pointer_create(&getbNodeTree()->id, &RNA_NodeSocket, iosock, &ptr);
	
	NodeOperation *operation = NULL;
	switch (iosock->typeinfo->type) {
		case SOCK_FLOAT:
		{
			float value = RNA_float_get(&ptr, "default_value");
			SetValueOperation *value_op = new SetValueOperation();
			value_op->setValue(value);
			operation = value_op;
			break;
		}
		case SOCK_VECTOR:
		{
			float vector[3];
			RNA_float_get_array(&ptr, "default_value", vector);
			SetVectorOperation *vector_op = new SetVectorOperation();
			vector_op->setVector(vector);
			operation = vector_op;
			break;
		}
		case SOCK_RGBA:
		{
			float color[4];
			RNA_float_get_array(&ptr, "default_value", color);
			SetColorOperation *color_op = new SetColorOperation();
			color_op->setChannels(color);
			operation = color_op;
			break;
		}
	}
	
	outputsocket->relinkConnections(operation->getOutputSocket());
	system.addOperation(operation);
}