[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [34850] branches/particles-2010/source/ blender: Core code cleanup: Two new files will contain all code related to group nodes and other common node types .

Lukas Toenne lukas.toenne at googlemail.com
Mon Feb 14 18:24:22 CET 2011


Revision: 34850
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=34850
Author:   lukastoenne
Date:     2011-02-14 17:24:17 +0000 (Mon, 14 Feb 2011)
Log Message:
-----------
Core code cleanup: Two new files will contain all code related to group nodes and other common node types.
Eventually the core code should be free of any type-specific functions and conditionals. New callbacks need to be added for abstracted group features as needed, with simple default implementations for standard nodes.
Node types are described by bNodeTemplate structs, which can have additional information like the node group tree for advanced node types. A validation callback is used for testing if a node type (bNodeTemplate) can be added inside a tree. This is used to prevent circular group dependencies (infinite recursion).

Modified Paths:
--------------
    branches/particles-2010/source/blender/blenkernel/BKE_node.h
    branches/particles-2010/source/blender/blenkernel/intern/node.c
    branches/particles-2010/source/blender/editors/space_node/node_draw.c
    branches/particles-2010/source/blender/editors/space_node/node_edit.c
    branches/particles-2010/source/blender/editors/space_node/node_header.c
    branches/particles-2010/source/blender/editors/space_node/node_intern.h
    branches/particles-2010/source/blender/makesrna/intern/rna_nodetree.c
    branches/particles-2010/source/blender/nodes/CMakeLists.txt
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_alphaOver.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_bilateralblur.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_blur.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_channelMatte.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_chromaMatte.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_colorMatte.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_colorSpill.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_colorbalance.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_crop.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_curves.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_defocus.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_diffMatte.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_directionalblur.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_distanceMatte.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_glare.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_hueSatVal.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_huecorrect.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_image.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_invert.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_lensdist.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_levels.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_lummaMatte.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_mapValue.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_outputFile.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_rotate.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_splitViewer.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_tonemap.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_valToRgb.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_vecBlur.c
    branches/particles-2010/source/blender/nodes/intern/CMP_nodes/CMP_viewer.c
    branches/particles-2010/source/blender/nodes/intern/SHD_nodes/SHD_curves.c
    branches/particles-2010/source/blender/nodes/intern/SHD_nodes/SHD_geom.c
    branches/particles-2010/source/blender/nodes/intern/SHD_nodes/SHD_mapping.c
    branches/particles-2010/source/blender/nodes/intern/SHD_nodes/SHD_material.c
    branches/particles-2010/source/blender/nodes/intern/SHD_nodes/SHD_valToRgb.c
    branches/particles-2010/source/blender/nodes/intern/TEX_nodes/TEX_bricks.c
    branches/particles-2010/source/blender/nodes/intern/TEX_nodes/TEX_curves.c
    branches/particles-2010/source/blender/nodes/intern/TEX_nodes/TEX_image.c
    branches/particles-2010/source/blender/nodes/intern/TEX_nodes/TEX_output.c
    branches/particles-2010/source/blender/nodes/intern/TEX_nodes/TEX_proc.c
    branches/particles-2010/source/blender/nodes/intern/TEX_nodes/TEX_valToRgb.c

Added Paths:
-----------
    branches/particles-2010/source/blender/nodes/intern/node_common.c
    branches/particles-2010/source/blender/nodes/intern/node_common.h

Modified: branches/particles-2010/source/blender/blenkernel/BKE_node.h
===================================================================
--- branches/particles-2010/source/blender/blenkernel/BKE_node.h	2011-02-14 17:17:05 UTC (rev 34849)
+++ branches/particles-2010/source/blender/blenkernel/BKE_node.h	2011-02-14 17:24:17 UTC (rev 34850)
@@ -87,6 +87,13 @@
 	SOCK_SQUARE
 } NodeSocketSymbol;
 
+typedef struct bNodeTemplate {
+	int type;
+	
+	/* group tree */
+	struct bNodeTree *ngroup;
+} bNodeTemplate;
+
 typedef struct bNodeType {
 	void *next,*prev;
 	int type;
@@ -110,10 +117,13 @@
 	/* called when the node is updated (e.g. linked) in the editor. */
 	void (*updatefunc)(struct bNodeTree *ntree, struct bNode *node);
 	
-	void (*initfunc)(struct bNode *);
+	void (*initfunc)(struct bNodeTree *ntree, struct bNode *node, struct bNodeTemplate *ntemp);
 	void (*freestoragefunc)(struct bNode *);
 	void (*copystoragefunc)(struct bNode *, struct bNode *);
+	struct bNodeTemplate (*templatefunc)(struct bNode *);
 	
+	int (*validfunc)(struct bNodeTree *ntree, struct bNodeTemplate *ntemp);
+	
 	/* gpu */
 	int (*gpufunc)(struct GPUMaterial *mat, struct bNode *node, struct GPUNodeStack *in, struct GPUNodeStack *out);
 } bNodeType;
@@ -227,9 +237,7 @@
 
 void			nodeAddToPreview(struct bNode *, float *, int, int);
 
-struct bNode	*nodeAddNode(struct bNodeTree *ntree, int type);
-struct bNode	*nodeAddGroupNode(struct bNodeTree *ntree, struct bNodeTree *ngroup);
-struct bNode	*nodeAddDynamicNode(struct bNodeTree *ntree, int type);
+struct bNode	*nodeAddNode(struct bNodeTree *ntree, struct bNodeTemplate *ntemp);
 void			nodeUnlinkNode(struct bNodeTree *ntree, struct bNode *node);
 void			nodeUniqueName(struct bNodeTree *ntree, struct bNode *node);
 struct bNodeSocket *nodeAddInputSocket(struct bNodeTree *ntree, struct bNode *node, const char *name, int type);
@@ -299,13 +307,18 @@
 struct bNodeSocket *nodeFindGroupNodeInput(struct bNode *gnode, struct bNodeSocket *gsock);
 struct bNodeSocket *nodeFindGroupNodeOutput(struct bNode *gnode, struct bNodeSocket *gsock);
 
-/* ************** COMMON NODES *************** */
+/* ************** NODE TYPE ACCESS *************** */
 
+struct bNodeTemplate nodeMakeTemplate(struct bNode *node);
+int				nodeValid(struct bNodeTree *ntree, struct bNodeTemplate *ntemp);
+const char*		nodeLabel(struct bNode *node);
+
 /* Init a new node type struct with default values and callbacks */
 void			node_type_base(struct bNodeType *ntype, int type, const char *name, short nclass, short flag,
 							   struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs);
 void			node_type_size(struct bNodeType *ntype, int width, int minwidth, int maxwidth);
-void			node_type_init(struct bNodeType *ntype, void (*initfunc)(struct bNode *));
+void			node_type_init(struct bNodeType *ntype, void (*initfunc)(struct bNodeTree *ntree, struct bNode *node, struct bNodeTemplate *ntemp));
+void			node_type_valid(struct bNodeType *ntype, int (*validfunc)(struct bNodeTree *ntree, struct bNodeTemplate *ntemp));
 void			node_type_storage(struct bNodeType *ntype,
 								  const char *storagename,
 								  void (*freestoragefunc)(struct bNode *),
@@ -313,7 +326,10 @@
 void			node_type_exec(struct bNodeType *ntype, void (*execfunc)(void *data, struct bNode *, struct bNodeStack **, struct bNodeStack **));
 void			node_type_gpu(struct bNodeType *ntype, int (*gpufunc)(struct GPUMaterial *mat, struct bNode *node, struct GPUNodeStack *in, struct GPUNodeStack *out));
 void			node_type_label(struct bNodeType *ntype, const char *(*labelfunc)(struct bNode *));
+void			node_type_template(struct bNodeType *ntype, struct bNodeTemplate (*templatefunc)(struct bNode *));
 
+/* ************** COMMON NODES *************** */
+
 #define NODE_GROUP		2
 #define NODE_GROUP_MENU		10000
 #define NODE_DYNAMIC_MENU	20000

Modified: branches/particles-2010/source/blender/blenkernel/intern/node.c
===================================================================
--- branches/particles-2010/source/blender/blenkernel/intern/node.c	2011-02-14 17:17:05 UTC (rev 34849)
+++ branches/particles-2010/source/blender/blenkernel/intern/node.c	2011-02-14 17:24:17 UTC (rev 34850)
@@ -124,8 +124,8 @@
 				node->custom1= 0;
 				node->custom1= BSET(node->custom1,NODE_DYNAMIC_ADDEXIST);
 			}
-			if(node->typeinfo)
-				node->typeinfo->initfunc(node);
+//			if(node->typeinfo)
+//				node->typeinfo->initfunc(node);
 		}
 
 		if(node->typeinfo==NULL) {
@@ -453,39 +453,6 @@
 
 /* ************** Group stuff ********** */
 
-/* groups display their internal tree name as label */
-static const char *group_label(bNode *node)
-{
-	return node->id->name+2;
-}
-
-void register_node_type_group(ListBase *lb)
-{
-	static bNodeType ntype;
-
-	node_type_base(&ntype, NODE_GROUP, "Group", NODE_CLASS_GROUP, NODE_OPTIONS, NULL, NULL);
-	node_type_size(&ntype, 120, 60, 200);
-	node_type_label(&ntype, group_label);
-	
-	nodeRegisterType(lb, &ntype);
-}
-
-int ntreeGroupAllowed(bNodeTree *ntree, int UNUSED(type), bNodeTree *ngroup)
-{
-	bNode *node;
-	
-	/* XXX make a node type callback for this! */
-	if (ntree == ngroup)
-		return 0;
-	
-	for (node=ngroup->nodes.first; node; node=node->next) {
-		if (node->type==NODE_GROUP && node->id) {
-			return ntreeGroupAllowed(ntree, node->type, (bNodeTree*)node->id);
-		}
-	}
-	return 1;
-}
-
 bNodeSocket *nodeFindGroupNodeInput(bNode *gnode, bNodeSocket *gsock)
 {
 	bNodeSocket *sock;
@@ -513,6 +480,7 @@
 	ListBase anim_basepaths = {NULL, NULL};
 	float min[2], max[2];
 	int totnode=0;
+	bNodeTemplate ntemp;
 	
 	INIT_MINMAX2(min, max);
 	
@@ -592,7 +560,9 @@
 	}
 	
 	/* make group node */
-	gnode= nodeAddGroupNode(ntree, ngroup);
+	ntemp.type = NODE_GROUP;
+	ntemp.ngroup = ngroup;
+	gnode= nodeAddNode(ntree, &ntemp);
 	gnode->locx= 0.5f*(min[0]+max[0]);
 	gnode->locy= 0.5f*(min[1]+max[1]);
 	
@@ -1065,97 +1035,37 @@
 	BLI_uniquename(&ntree->nodes, node, "Node", '.', offsetof(bNode, name), sizeof(node->name));
 }
 
-static bNode *make_node(bNodeTree *ntree, bNodeType *ntype, const char *name)
+bNode *nodeAddNode(bNodeTree *ntree, struct bNodeTemplate *ntemp)
 {
-	bNode *node= NULL;
-
-	node= MEM_callocN(sizeof(bNode), "new node");
+	bNode *node;
+	bNodeType *ntype;
 	
-	BLI_strncpy(node->name, name, NODE_MAXSTR);
-	nodeUniqueName(ntree, node);
+	ntype= node_get_type(ntree, ntemp->type);
+	/* validity check */
+	if (!nodeValid(ntree, ntemp))
+		return NULL;
+	
+	node= MEM_callocN(sizeof(bNode), "new node");
 	node->type= ntype->type;
 	node->typeinfo= ntype;
 	node->flag= NODE_SELECT|ntype->flag;
 	node->width= ntype->width;
 	node->miniwidth= 42.0f;		/* small value only, allows print of first chars */
 	
-	return node;
-}
-
-bNode *nodeAddNode(bNodeTree *ntree, int type)
-{
-	bNode *node;
-	bNodeType *ntype;
-	
-	ntype= node_get_type(ntree, type);
-	node= make_node(ntree, ntype, ntype->name);
-	BLI_addtail(&ntree->nodes, node);
-	
-	/* XXX need init handler later? */
 	if(ntype->initfunc!=NULL)
-		ntype->initfunc(node);
+		ntype->initfunc(ntree, node, ntemp);
 	
-	node_add_sockets_from_type(ntree, node, ntype);
+	/* initialize the node name with the node label */
+	BLI_strncpy(node->name, nodeLabel(node), NODE_MAXSTR);
+	nodeUniqueName(ntree, node);
 	
-	return node;
-}
-
-bNode *nodeAddGroupNode(bNodeTree *ntree, bNodeTree *ngroup)
-{
-	bNode *node;
-	bNodeType *ntype;
-	
-	if (BLI_findindex(&G.main->nodetree, ngroup)==-1) {
-//		printf("nodeAddNode() error: '%s' not in main->nodetree\n", ngroup->id.name);
-		return NULL;
-	}
-	
-	ntype= node_get_type(ntree, NODE_GROUP);
-	node= make_node(ntree, ntype, ngroup->id.name+2);
-	node->id= &ngroup->id;
 	BLI_addtail(&ntree->nodes, node);
-
-	/* XXX need init handler later? */
-	if(ntype->initfunc!=NULL)
-		ntype->initfunc(node);
 	
 	node_add_sockets_from_type(ntree, node, ntype);
 	
 	return node;
 }
 
-bNode *nodeAddDynamicNode(bNodeTree *ntree, int type)
-{
-	bNode *node;
-	bNodeType *ntype;
-	
-	int a=0, idx= type-NODE_DYNAMIC_MENU;
-	ntype= ntreeGetType(ntree->type)->node_types.first;
-	while(ntype) {
-		if(ntype->type==NODE_DYNAMIC) {
-			if(a==idx)
-				break;
-			a++;
-		}
-		ntype= ntype->next;
-	}
-	
-	ntype= node_get_type(ntree, type);
-	/* XXX todo */
-	node= make_node(ntree, ntype, ntype->name);
-//	node= make_node(ntree, ntype, ntype->id->name+2);
-	node->custom2= type; /* for node_dynamic_init */
-	BLI_addtail(&ntree->nodes, node);
-	
-	/* XXX need init handler later? */
-	if(ntype->initfunc!=NULL)
-		ntype->initfunc(node);
-	
-	node_add_sockets_from_type(ntree, node, ntype);
-	
-	return node;
-}
-
 void nodeMakeDynamicType(bNode *node)
 {
 	/* find SH_DYNAMIC_NODE ntype */
@@ -1523,7 +1433,6 @@
 	}
 }
 
-
 /* ************** Free stuff ********** */
 
 /* goes over entire tree */
@@ -2193,8 +2102,40 @@
 	}
 }
 
-/* ************* node definition init ********** */
+/* ************* node type access ********** */
 
+int nodeValid(bNodeTree *ntree, bNodeTemplate *ntemp)
+{
+	bNodeType *ntype= node_get_type(ntree, ntemp->type);
+	if (ntype) {
+		if (ntype->validfunc)
+			return ntype->validfunc(ntree, ntemp);
+		else
+			return 1;
+	}
+	else
+		return 0;
+}
+
+const char* nodeLabel(bNode *node)
+{
+	if (node->typeinfo->labelfunc)
+		return node->typeinfo->labelfunc(node);
+	else
+		return node->typeinfo->name;
+}
+
+struct bNodeTemplate nodeMakeTemplate(struct bNode *node)
+{
+	bNodeTemplate ntemp;
+	if (node->typeinfo->templatefunc)
+		return node->typeinfo->templatefunc(node);
+	else {
+		ntemp.type = node->type;
+		return ntemp;
+	}
+}
+
 void node_type_base(bNodeType *ntype, int type, const char *name, short nclass, short flag,
 					struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
 {
@@ -2214,11 +2155,16 @@
 	ntype->maxwidth = 320;
 }
 
-void node_type_init(bNodeType *ntype, void (*initfunc)(struct bNode *))
+void node_type_init(struct bNodeType *ntype, void (*initfunc)(struct bNodeTree *ntree, struct bNode *node, struct bNodeTemplate *ntemp))
 {
 	ntype->initfunc = initfunc;
 }
 
+void node_type_valid(struct bNodeType *ntype, int (*validfunc)(struct bNodeTree *ntree, struct bNodeTemplate *ntemp))
+{
+	ntype->validfunc = validfunc;
+}
+
 void node_type_size(struct bNodeType *ntype, int width, int minwidth, int maxwidth)
 {
 	ntype->width = width;
@@ -2251,6 +2197,11 @@
 	ntype->labelfunc = labelfunc;
 }
 
+void node_type_template(struct bNodeType *ntype, struct bNodeTemplate (*templatefunc)(struct bNode *))
+{
+	ntype->templatefunc = templatefunc;
+}
+

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list