114
bNode *node_group_make_from_selected(bNodeTree *ntree)
116
bNodeLink *link, *linkn;
117
bNode *node, *gnode, *nextn;
120
ListBase anim_basepaths = {NULL, NULL};
121
float min[2], max[2];
125
INIT_MINMAX2(min, max);
127
/* is there something to group? also do some clearing */
128
for (node= ntree->nodes.first; node; node= node->next) {
129
if (node->flag & NODE_SELECT) {
130
/* no groups in groups */
131
if (node->type==NODE_GROUP)
133
DO_MINMAX2( (&node->locx), min, max);
138
if (totnode==0) return NULL;
140
/* check if all connections are OK, no unselected node has both
141
* inputs and outputs to a selection */
142
for (link= ntree->links.first; link; link= link->next) {
143
if (link->fromnode && link->tonode && link->fromnode->flag & NODE_SELECT)
144
link->tonode->done |= 1;
145
if (link->fromnode && link->tonode && link->tonode->flag & NODE_SELECT)
146
link->fromnode->done |= 2;
149
for (node= ntree->nodes.first; node; node= node->next) {
150
if ((node->flag & NODE_SELECT)==0)
157
/* OK! new nodetree */
158
ngroup= ntreeAddTree("NodeGroup", ntree->type, NODE_GROUP);
160
/* move nodes over */
161
for (node= ntree->nodes.first; node; node= nextn) {
163
if (node->flag & NODE_SELECT) {
164
/* keep track of this node's RNA "base" path (the part of the pat identifying the node)
165
* if the old nodetree has animation data which potentially covers this node
171
RNA_pointer_create(&ntree->id, &RNA_Node, node, &ptr);
172
path = RNA_path_from_ID_to_struct(&ptr);
175
BLI_addtail(&anim_basepaths, BLI_genericNodeN(path));
178
/* change node-collection membership */
179
BLI_remlink(&ntree->nodes, node);
180
BLI_addtail(&ngroup->nodes, node);
182
node->locx-= 0.5f*(min[0]+max[0]);
183
node->locy-= 0.5f*(min[1]+max[1]);
187
/* move animation data over */
189
LinkData *ld, *ldn=NULL;
191
BKE_animdata_separate_by_basepath(&ntree->id, &ngroup->id, &anim_basepaths);
193
/* paths + their wrappers need to be freed */
194
for (ld = anim_basepaths.first; ld; ld = ldn) {
198
BLI_freelinkN(&anim_basepaths, ld);
202
/* node groups don't use internal cached data */
203
ntreeFreeCache(ngroup);
205
/* make group node */
206
ntemp.type = NODE_GROUP;
207
ntemp.ngroup = ngroup;
208
gnode= nodeAddNode(ntree, &ntemp);
209
gnode->locx= 0.5f*(min[0]+max[0]);
210
gnode->locy= 0.5f*(min[1]+max[1]);
212
/* relink external sockets */
213
for (link= ntree->links.first; link; link= linkn) {
216
if (link->fromnode && link->tonode && (link->fromnode->flag & link->tonode->flag & NODE_SELECT)) {
217
BLI_remlink(&ntree->links, link);
218
BLI_addtail(&ngroup->links, link);
220
else if (link->tonode && (link->tonode->flag & NODE_SELECT)) {
221
gsock = node_group_expose_socket(ngroup, link->tosock, SOCK_IN);
222
link->tosock->link = nodeAddLink(ngroup, NULL, gsock, link->tonode, link->tosock);
223
link->tosock = node_group_add_extern_socket(ntree, &gnode->inputs, SOCK_IN, gsock);
224
link->tonode = gnode;
226
else if (link->fromnode && (link->fromnode->flag & NODE_SELECT)) {
227
/* search for existing group node socket */
228
for (gsock=ngroup->outputs.first; gsock; gsock=gsock->next)
229
if (gsock->link && gsock->link->fromsock==link->fromsock)
232
gsock = node_group_expose_socket(ngroup, link->fromsock, SOCK_OUT);
233
gsock->link = nodeAddLink(ngroup, link->fromnode, link->fromsock, NULL, gsock);
234
link->fromsock = node_group_add_extern_socket(ntree, &gnode->outputs, SOCK_OUT, gsock);
237
link->fromsock = node_group_find_output(gnode, gsock);
238
link->fromnode = gnode;
242
/* update of the group tree */
243
ngroup->update |= NTREE_UPDATE;
244
ntreeUpdateTree(ngroup);
245
/* update of the tree containing the group instance node */
246
ntree->update |= NTREE_UPDATE_NODES | NTREE_UPDATE_LINKS;
247
ntreeUpdateTree(ntree);
252
/* returns 1 if its OK */
253
int node_group_ungroup(bNodeTree *ntree, bNode *gnode)
255
bNodeLink *link, *linkn;
257
bNodeTree *ngroup, *wgroup;
258
ListBase anim_basepaths = {NULL, NULL};
260
ngroup= (bNodeTree *)gnode->id;
261
if (ngroup==NULL) return 0;
263
/* clear new pointers, set in copytree */
264
for (node= ntree->nodes.first; node; node= node->next)
265
node->new_node= NULL;
267
/* wgroup is a temporary copy of the NodeTree we're merging in
268
* - all of wgroup's nodes are transferred across to their new home
269
* - ngroup (i.e. the source NodeTree) is left unscathed
271
wgroup= ntreeCopyTree(ngroup);
273
/* add the nodes into the ntree */
274
for (node= wgroup->nodes.first; node; node= nextn) {
277
/* keep track of this node's RNA "base" path (the part of the pat identifying the node)
278
* if the old nodetree has animation data which potentially covers this node
284
RNA_pointer_create(&wgroup->id, &RNA_Node, node, &ptr);
285
path = RNA_path_from_ID_to_struct(&ptr);
288
BLI_addtail(&anim_basepaths, BLI_genericNodeN(path));
292
BLI_remlink(&wgroup->nodes, node);
293
BLI_addtail(&ntree->nodes, node);
295
node->locx+= gnode->locx;
296
node->locy+= gnode->locy;
298
node->flag |= NODE_SELECT;
301
/* restore external links to and from the gnode */
302
for (link= ntree->links.first; link; link= link->next) {
303
if (link->fromnode==gnode) {
304
if (link->fromsock->groupsock) {
305
bNodeSocket *gsock= link->fromsock->groupsock;
307
if (gsock->link->fromnode) {
308
/* NB: using the new internal copies here! the groupsock pointer still maps to the old tree */
309
link->fromnode = (gsock->link->fromnode ? gsock->link->fromnode->new_node : NULL);
310
link->fromsock = gsock->link->fromsock->new_sock;
313
/* group output directly maps to group input */
314
bNodeSocket *insock= node_group_find_input(gnode, gsock->link->fromsock);
316
link->fromnode = insock->link->fromnode;
317
link->fromsock = insock->link->fromsock;
322
/* copy the default input value from the group socket default to the external socket */
323
node_socket_convert_default_value(link->tosock->type, link->tosock->default_value, gsock->type, gsock->default_value);
328
/* remove internal output links, these are not used anymore */
329
for (link=wgroup->links.first; link; link= linkn) {
332
nodeRemLink(wgroup, link);
334
/* restore links from internal nodes */
335
for (link= wgroup->links.first; link; link= link->next) {
336
/* indicates link to group input */
337
if (!link->fromnode) {
338
/* NB: can't use find_group_node_input here,
339
* because gnode sockets still point to the old tree!
342
for (insock= gnode->inputs.first; insock; insock= insock->next)
343
if (insock->groupsock->new_sock == link->fromsock)
346
link->fromnode = insock->link->fromnode;
347
link->fromsock = insock->link->fromsock;
350
/* copy the default input value from the group node socket default to the internal socket */
351
node_socket_convert_default_value(link->tosock->type, link->tosock->default_value, insock->type, insock->default_value);
352
nodeRemLink(wgroup, link);
357
/* add internal links to the ntree */
358
for (link= wgroup->links.first; link; link= linkn) {
360
BLI_remlink(&wgroup->links, link);
361
BLI_addtail(&ntree->links, link);
364
/* and copy across the animation,
365
* note that the animation data's action can be NULL here */
367
LinkData *ld, *ldn=NULL;
370
/* firstly, wgroup needs to temporary dummy action that can be destroyed, as it shares copies */
371
waction = wgroup->adt->action = copy_action(wgroup->adt->action);
373
/* now perform the moving */
374
BKE_animdata_separate_by_basepath(&wgroup->id, &ntree->id, &anim_basepaths);
376
/* paths + their wrappers need to be freed */
377
for (ld = anim_basepaths.first; ld; ld = ldn) {
381
BLI_freelinkN(&anim_basepaths, ld);
384
/* free temp action too */
386
free_libblock(&G.main->action, waction);
390
/* delete the group instance. this also removes old input links! */
391
nodeFreeNode(ntree, gnode);
393
/* free the group tree (takes care of user count) */
394
free_libblock(&G.main->nodetree, wgroup);
396
ntree->update |= NTREE_UPDATE_NODES | NTREE_UPDATE_LINKS;
397
ntreeUpdateTree(ntree);
402
110
bNodeSocket *node_group_add_socket(bNodeTree *ngroup, const char *name, int type, int in_out)
404
112
bNodeSocketType *stype = ntreeGetSocketType(type);
641
349
nodeGroupEditClear(inode);
644
void node_group_link(bNodeTree *ntree, bNodeSocket *sock, int in_out)
352
static void UNUSED_FUNCTION(node_group_link)(bNodeTree *ntree, bNodeSocket *sock, int in_out)
646
354
node_group_expose_socket(ntree, sock, in_out);
651
/* Essentially a group node with slightly different behavior.
652
* The internal tree is executed several times, with each output being re-used
653
* as an input in the next iteration. For this purpose, input and output socket
654
* lists are kept identical!
657
bNodeTemplate node_forloop_template(bNode *node)
660
ntemp.type = NODE_FORLOOP;
661
ntemp.ngroup = (bNodeTree*)node->id;
665
void node_forloop_init(bNodeTree *ntree, bNode *node, bNodeTemplate *ntemp)
669
node->id = (ID*)ntemp->ngroup;
671
sock = nodeAddSocket(ntree, node, SOCK_IN, "Iterations", SOCK_FLOAT);
672
node_socket_set_default_value_float(sock->default_value, PROP_UNSIGNED, 1, 0, 10000);
674
/* NB: group socket input/output roles are inverted internally!
675
* Group "inputs" work as outputs in links and vice versa.
679
for (gsock=ntemp->ngroup->inputs.first; gsock; gsock=gsock->next)
680
node_group_add_extern_socket(ntree, &node->inputs, SOCK_IN, gsock);
681
for (gsock=ntemp->ngroup->outputs.first; gsock; gsock=gsock->next)
682
node_group_add_extern_socket(ntree, &node->outputs, SOCK_OUT, gsock);
686
void node_forloop_init_tree(bNodeTree *ntree)
689
sock = node_group_add_socket(ntree, "Iteration", SOCK_FLOAT, SOCK_IN);
690
sock->flag |= SOCK_INTERNAL;
693
static void loop_sync(bNodeTree *ntree, int sync_in_out)
695
bNodeSocket *sock, *sync, *nsync, *mirror;
698
if (sync_in_out==SOCK_IN) {
699
sock = ntree->outputs.first;
701
sync = ntree->inputs.first;
702
sync_lb = &ntree->inputs;
705
sock = ntree->inputs.first;
707
sync = ntree->outputs.first;
708
sync_lb = &ntree->outputs;
711
/* NB: the sock->storage pointer is used here directly to store the own_index int
712
* out the mirrored socket counterpart!
716
/* skip static and internal sockets on the sync side (preserves socket order!) */
717
while (sync && ((sync->flag & SOCK_INTERNAL) || !(sync->flag & SOCK_DYNAMIC)))
720
if (sync && !(sync->flag & SOCK_INTERNAL) && (sync->flag & SOCK_DYNAMIC)) {
721
if (sock->storage==NULL) {
722
/* if mirror index is 0, the sockets is newly added and a new mirror must be created. */
723
mirror = node_group_expose_socket(ntree, sock, sync_in_out);
724
/* store the mirror index */
725
sock->storage = SET_INT_IN_POINTER(mirror->own_index);
726
mirror->storage = SET_INT_IN_POINTER(sock->own_index);
727
/* move mirror to the right place */
728
BLI_remlink(sync_lb, mirror);
730
BLI_insertlinkbefore(sync_lb, sync, mirror);
732
BLI_addtail(sync_lb, mirror);
735
/* look up the mirror socket */
736
for (mirror=sync; mirror; mirror=mirror->next)
737
if (mirror->own_index == GET_INT_FROM_POINTER(sock->storage))
739
/* make sure the name is the same (only for identification by user, no deeper meaning) */
740
BLI_strncpy(mirror->name, sock->name, sizeof(mirror->name));
741
/* fix the socket order if necessary */
742
if (mirror != sync) {
743
BLI_remlink(sync_lb, mirror);
744
BLI_insertlinkbefore(sync_lb, sync, mirror);
754
/* remaining sockets in sync_lb are leftovers from deleted sockets, remove them */
757
if (!(sync->flag & SOCK_INTERNAL) && (sync->flag & SOCK_DYNAMIC))
758
node_group_remove_socket(ntree, sync, sync_in_out);
763
void node_loop_update_tree(bNodeTree *ngroup)
765
/* make sure inputs & outputs are identical */
766
if (ngroup->update & NTREE_UPDATE_GROUP_IN)
767
loop_sync(ngroup, SOCK_OUT);
768
if (ngroup->update & NTREE_UPDATE_GROUP_OUT)
769
loop_sync(ngroup, SOCK_IN);
772
void node_whileloop_init(bNodeTree *ntree, bNode *node, bNodeTemplate *ntemp)
776
node->id = (ID*)ntemp->ngroup;
778
sock = nodeAddSocket(ntree, node, SOCK_IN, "Condition", SOCK_FLOAT);
779
node_socket_set_default_value_float(sock->default_value, PROP_NONE, 1, 0, 1);
782
node->custom1 = 10000;
784
/* NB: group socket input/output roles are inverted internally!
785
* Group "inputs" work as outputs in links and vice versa.
789
for (gsock=ntemp->ngroup->inputs.first; gsock; gsock=gsock->next)
790
node_group_add_extern_socket(ntree, &node->inputs, SOCK_IN, gsock);
791
for (gsock=ntemp->ngroup->outputs.first; gsock; gsock=gsock->next)
792
node_group_add_extern_socket(ntree, &node->outputs, SOCK_OUT, gsock);
796
void node_whileloop_init_tree(bNodeTree *ntree)
799
sock = node_group_add_socket(ntree, "Condition", SOCK_FLOAT, SOCK_OUT);
800
sock->flag |= SOCK_INTERNAL;
803
bNodeTemplate node_whileloop_template(bNode *node)
806
ntemp.type = NODE_WHILELOOP;
807
ntemp.ngroup = (bNodeTree*)node->id;
811
357
/**** FRAME ****/
359
static void node_frame_init(bNodeTree *UNUSED(ntree), bNode *node, bNodeTemplate *UNUSED(ntemp))
361
NodeFrame *data = (NodeFrame *)MEM_callocN(sizeof(NodeFrame), "frame node storage");
362
node->storage = data;
364
data->flag |= NODE_FRAME_SHRINK;
366
data->label_size = 20;
813
369
void register_node_type_frame(bNodeTreeType *ttype)
815
371
/* frame type is used for all tree types, needs dynamic allocation */
816
372
bNodeType *ntype= MEM_callocN(sizeof(bNodeType), "frame node type");
818
node_type_base(ttype, ntype, NODE_FRAME, "Frame", NODE_CLASS_LAYOUT, NODE_BACKGROUND);
374
node_type_base(ttype, ntype, NODE_FRAME, "Frame", NODE_CLASS_LAYOUT, NODE_BACKGROUND|NODE_OPTIONS);
375
node_type_init(ntype, node_frame_init);
376
node_type_storage(ntype, "NodeFrame", node_free_standard_storage, node_copy_standard_storage);
819
377
node_type_size(ntype, 150, 100, 0);
820
378
node_type_compatibility(ntype, NODE_OLD_SHADING|NODE_NEW_SHADING);
822
380
ntype->needs_free = 1;
823
381
nodeRegisterType(ttype, ntype);
385
/* **************** REROUTE ******************** */
387
/* simple, only a single input and output here */
388
static void node_reroute_update_internal_links(bNodeTree *ntree, bNode *node)
392
/* Security check! */
396
link = MEM_callocN(sizeof(bNodeLink), "internal node link");
397
link->fromnode = node;
398
link->fromsock = node->inputs.first;
400
link->tosock = node->outputs.first;
401
/* internal link is always valid */
402
link->flag |= NODE_LINK_VALID;
403
BLI_addtail(&node->internal_links, link);
406
static void node_reroute_init(bNodeTree *ntree, bNode *node, bNodeTemplate *UNUSED(ntemp))
408
/* Note: Cannot use socket templates for this, since it would reset the socket type
409
* on each file read via the template verification procedure.
411
nodeAddSocket(ntree, node, SOCK_IN, "Input", SOCK_RGBA);
412
nodeAddSocket(ntree, node, SOCK_OUT, "Output", SOCK_RGBA);
415
void register_node_type_reroute(bNodeTreeType *ttype)
417
/* frame type is used for all tree types, needs dynamic allocation */
418
bNodeType *ntype= MEM_callocN(sizeof(bNodeType), "frame node type");
420
node_type_base(ttype, ntype, NODE_REROUTE, "Reroute", NODE_CLASS_LAYOUT, 0);
421
node_type_init(ntype, node_reroute_init);
422
node_type_internal_links(ntype, node_reroute_update_internal_links);
424
ntype->needs_free = 1;
425
nodeRegisterType(ttype, ntype);
428
static void node_reroute_inherit_type_recursive(bNodeTree *ntree, bNode *node)
430
bNodeSocket *input = node->inputs.first;
431
bNodeSocket *output = node->outputs.first;
432
int type = SOCK_FLOAT;
435
/* XXX it would be a little bit more efficient to restrict actual updates
436
* to rerout nodes connected to an updated node, but there's no reliable flag
437
* to indicate updated nodes (node->update is not set on linking).
442
/* recursive update */
443
for (link = ntree->links.first; link; link = link->next)
445
bNode *fromnode = link->fromnode;
446
bNode *tonode = link->tonode;
447
if (!tonode || !fromnode)
450
if (tonode == node && fromnode->type == NODE_REROUTE && !fromnode->done)
451
node_reroute_inherit_type_recursive(ntree, fromnode);
453
if (fromnode == node && tonode->type == NODE_REROUTE && !tonode->done)
454
node_reroute_inherit_type_recursive(ntree, tonode);
457
/* determine socket type from unambiguous input/output connection if possible */
458
if (input->limit==1 && input->link)
459
type = input->link->fromsock->type;
460
else if (output->limit==1 && output->link)
461
type = output->link->tosock->type;
463
/* arbitrary, could also test output->type, both are the same */
464
if (input->type != type) {
465
/* same type for input/output */
466
nodeSocketSetType(input, type);
467
nodeSocketSetType(output, type);
471
/* Global update function for Reroute node types.
472
* This depends on connected nodes, so must be done as a tree-wide update.
474
void ntree_update_reroute_nodes(bNodeTree *ntree)
479
for (node = ntree->nodes.first; node; node = node->next)
482
for (node = ntree->nodes.first; node; node = node->next)
483
if (node->type == NODE_REROUTE && !node->done)
484
node_reroute_inherit_type_recursive(ntree, node);
487
void BKE_node_tree_unlink_id_cb(void *calldata, struct ID *UNUSED(owner_id), struct bNodeTree *ntree)
489
ID *id = (ID *)calldata;
492
for (node = ntree->nodes.first; node; node = node->next) {
493
if (node->id == id) {