[Bf-blender-cvs] [a067dcc] object_nodes: Staged construction of node graphs from bNodeTrees through a "parser".

Lukas Tönne noreply at git.blender.org
Tue Nov 24 09:43:35 CET 2015


Commit: a067dccf65c0e34cad830266d3f3373faa16cb2f
Author: Lukas Tönne
Date:   Wed Oct 28 17:52:04 2015 +0100
Branches: object_nodes
https://developer.blender.org/rBa067dccf65c0e34cad830266d3f3373faa16cb2f

Staged construction of node graphs from bNodeTrees through a "parser".

This wraps mapping of sockets and links in a nice small API.

===================================================================

M	source/blender/blenvm/BVM_api.h
M	source/blender/blenvm/intern/bvm_api.cc

===================================================================

diff --git a/source/blender/blenvm/BVM_api.h b/source/blender/blenvm/BVM_api.h
index 1d16d66..0fe77d2 100644
--- a/source/blender/blenvm/BVM_api.h
+++ b/source/blender/blenvm/BVM_api.h
@@ -70,7 +70,6 @@ struct BVMNodeInstance *BVM_nodegraph_add_node(struct BVMNodeGraph *graph, const
 
 struct BVMEvalGlobals;
 struct BVMEvalContext;
-struct EffectedPoint;
 
 struct BVMEvalGlobals *BVM_globals_create(void);
 void BVM_globals_free(struct BVMEvalGlobals *globals);
@@ -80,15 +79,20 @@ void BVM_globals_add_object(struct BVMEvalGlobals *globals, struct Object *ob);
 struct BVMEvalContext *BVM_context_create(void);
 void BVM_context_free(struct BVMEvalContext *context);
 
+/* ------------------------------------------------------------------------- */
+
+struct bNodeTree;
+struct Object;
+struct EffectedPoint;
+
+struct BVMExpression *BVM_gen_forcefield_expression(const struct BVMEvalGlobals *globals, struct Object *effob, struct bNodeTree *btree);
+
 void BVM_eval_forcefield(struct BVMEvalGlobals *globals, struct BVMEvalContext *context, struct BVMExpression *expr,
                          const struct EffectedPoint *point, float force[3], float impulse[3]);
 
 /* ------------------------------------------------------------------------- */
 
-struct bNodeTree;
-struct Object;
 
-struct BVMExpression *BVM_gen_forcefield_expression(const struct BVMEvalGlobals *globals, struct Object *effob, struct bNodeTree *ntree);
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/blenvm/intern/bvm_api.cc b/source/blender/blenvm/intern/bvm_api.cc
index afa8ec9..f2a5bff 100644
--- a/source/blender/blenvm/intern/bvm_api.cc
+++ b/source/blender/blenvm/intern/bvm_api.cc
@@ -54,6 +54,7 @@ extern "C" {
 #include "bvm_function.h"
 #include "bvm_module.h"
 #include "bvm_nodegraph.h"
+#include "bvm_util_map.h"
 
 void BVM_init(void)
 {
@@ -123,306 +124,366 @@ struct BVMEvalContext *BVM_context_create(void)
 void BVM_context_free(struct BVMEvalContext *ctx)
 { delete _CTX(ctx); }
 
-void BVM_eval_forcefield(struct BVMEvalGlobals *globals, struct BVMEvalContext *ctx, struct BVMExpression *expr,
-                         const EffectedPoint *point, float force[3], float impulse[3])
-{
-	bvm::EvalData data;
-	data.effector.position = bvm::float3(point->loc[0], point->loc[1], point->loc[2]);
-	data.effector.velocity = bvm::float3(point->vel[0], point->vel[1], point->vel[2]);
-	void *results[] = { force, impulse };
-	
-	_CTX(ctx)->eval_expression(_GLOBALS(globals), &data, _EXPR(expr), results);
-}
-
 /* ------------------------------------------------------------------------- */
 
-typedef std::pair<bNode*, bNodeSocket*> bSocketPair;
-typedef std::pair<bvm::NodeInstance*, bvm::string> SocketPair;
-typedef std::set<SocketPair> SocketSet;
-typedef std::map<bSocketPair, SocketSet> InputMap;
-typedef std::map<bSocketPair, SocketPair> OutputMap;
-
-typedef std::map<struct Object *, int> ObjectPtrMap;
-
-static void map_input_socket(InputMap &input_map, bNode *bnode, int bindex, bvm::NodeInstance *node, const bvm::string &name)
-{
-	bNodeSocket *binput = (bNodeSocket *)BLI_findlink(&bnode->inputs, bindex);
+struct bNodeCompiler {
+	typedef std::pair<bNode*, bNodeSocket*> bSocketPair;
+	typedef std::pair<bvm::NodeInstance*, bvm::string> SocketPair;
+	typedef std::set<SocketPair> SocketSet;
+	typedef std::map<bSocketPair, SocketSet> InputMap;
+	typedef std::map<bSocketPair, SocketPair> OutputMap;
 	
-	input_map[bSocketPair(bnode, binput)].insert(SocketPair(node, name));
+	bNodeCompiler(bvm::NodeGraph *graph) :
+	    m_graph(graph)
+	{
+	}
 	
-	switch (binput->type) {
-		case SOCK_FLOAT: {
-			bNodeSocketValueFloat *bvalue = (bNodeSocketValueFloat *)binput->default_value;
-			node->set_input_value(name, bvalue->value);
-			break;
+	~bNodeCompiler()
+	{
+	}
+	
+	const InputMap &input_map() const { return m_input_map; }
+	const OutputMap &output_map() const { return m_output_map; }
+	
+	bvm::NodeInstance *add_node(const bvm::string &type,  const bvm::string &name)
+	{
+		return m_graph->add_node(type, name);
+	}
+	
+	void map_input_socket(bNode *bnode, int bindex, bvm::NodeInstance *node, const bvm::string &name)
+	{
+		bNodeSocket *binput = (bNodeSocket *)BLI_findlink(&bnode->inputs, bindex);
+		
+		m_input_map[bSocketPair(bnode, binput)].insert(SocketPair(node, name));
+		
+		switch (binput->type) {
+			case SOCK_FLOAT: {
+				bNodeSocketValueFloat *bvalue = (bNodeSocketValueFloat *)binput->default_value;
+				node->set_input_value(name, bvalue->value);
+				break;
+			}
+			case SOCK_VECTOR: {
+				bNodeSocketValueVector *bvalue = (bNodeSocketValueVector *)binput->default_value;
+				node->set_input_value(name, bvm::float3(bvalue->value[0], bvalue->value[1], bvalue->value[2]));
+				break;
+			}
 		}
-		case SOCK_VECTOR: {
-			bNodeSocketValueVector *bvalue = (bNodeSocketValueVector *)binput->default_value;
-			node->set_input_value(name, bvm::float3(bvalue->value[0], bvalue->value[1], bvalue->value[2]));
-			break;
+	}
+	
+	void map_output_socket(bNode *bnode, int bindex, bvm::NodeInstance *node, const bvm::string &name)
+	{
+		bNodeSocket *boutput = (bNodeSocket *)BLI_findlink(&bnode->outputs, bindex);
+		
+		m_output_map[bSocketPair(bnode, boutput)] = SocketPair(node, name);
+	}
+	
+	void set_graph_output(const bvm::string &graph_output_name, bvm::NodeInstance *node, const bvm::string &name)
+	{
+		m_graph->set_output_link(graph_output_name, node, name);
+	}
+	
+	void map_all_sockets(bNode *bnode, bvm::NodeInstance *node)
+	{
+		bNodeSocket *bsock;
+		int i;
+		for (bsock = (bNodeSocket *)bnode->inputs.first, i = 0; bsock; bsock = bsock->next, ++i) {
+			const bvm::NodeSocket *input = node->type->find_input(i);
+			map_input_socket(bnode, i, node, input->name);
+		}
+		for (bsock = (bNodeSocket *)bnode->outputs.first, i = 0; bsock; bsock = bsock->next, ++i) {
+			const bvm::NodeSocket *output = node->type->find_output(i);
+			map_output_socket(bnode, i, node, output->name);
 		}
 	}
-}
-
-static void map_output_socket(OutputMap &output_map,
-                              bNode *bnode, int bindex,
-                              bvm::NodeInstance *node, const bvm::string &name)
-{
-	bNodeSocket *boutput = (bNodeSocket *)BLI_findlink(&bnode->outputs, bindex);
 	
-	output_map[bSocketPair(bnode, boutput)] = SocketPair(node, name);
-}
+	void add_link(bNodeLink *blink)
+	{
+		OutputMap::const_iterator it_from = m_output_map.find(bSocketPair(blink->fromnode, blink->fromsock));
+		InputMap::const_iterator it_to_set = m_input_map.find(bSocketPair(blink->tonode, blink->tosock));
+		if (it_from != m_output_map.end() && it_to_set != m_input_map.end()) {
+			const SocketPair &from_pair = it_from->second;
+			const SocketSet &to_set = it_to_set->second;
+			
+			for (SocketSet::const_iterator it_to = to_set.begin(); it_to != to_set.end(); ++it_to) {
+				const SocketPair &to_pair = *it_to;
+				
+				m_graph->add_link(from_pair.first, from_pair.second,
+				                  to_pair.first, to_pair.second);
+			}
+		}
+		else {
+			// TODO better error handling, some exceptions here may be ok
+			printf("Cannot map link from %s:%s to %s:%s\n",
+			       blink->fromnode->name, blink->fromsock->name,
+			       blink->tonode->name, blink->tosock->name);
+		}
+	}
+	
+private:
+	bvm::NodeGraph *m_graph;
+	
+	InputMap m_input_map;
+	OutputMap m_output_map;
+};
+
+struct bNodeParser {
+	virtual void parse(bNodeCompiler *nodecomp, PointerRNA *bnode_ptr) const = 0;
+};
 
-static void map_all_sockets(InputMap &input_map, OutputMap &output_map,
-                            bNode *bnode, bvm::NodeInstance *node)
+static void gen_nodegraph(bNodeTree *btree, bvm::NodeGraph &graph, const bNodeParser &parser)
 {
-	bNodeSocket *bsock;
-	int i;
-	for (bsock = (bNodeSocket *)bnode->inputs.first, i = 0; bsock; bsock = bsock->next, ++i) {
-		const bvm::NodeSocket *input = node->type->find_input(i);
-		map_input_socket(input_map, bnode, i, node, input->name);
+	bNodeCompiler nodecomp(&graph);
+	
+	for (bNode *bnode = (bNode*)btree->nodes.first; bnode; bnode = bnode->next) {
+		PointerRNA ptr;
+		RNA_pointer_create((ID *)btree, &RNA_Node, bnode, &ptr);
+		
+		BLI_assert(bnode->typeinfo != NULL);
+		if (!nodeIsRegistered(bnode))
+			continue;
+		
+		parser.parse(&nodecomp, &ptr);
 	}
-	for (bsock = (bNodeSocket *)bnode->outputs.first, i = 0; bsock; bsock = bsock->next, ++i) {
-		const bvm::NodeSocket *output = node->type->find_output(i);
-		map_output_socket(output_map, bnode, i, node, output->name);
+	
+	for (bNodeLink *blink = (bNodeLink *)btree->links.first; blink; blink = blink->next) {
+		if (!(blink->flag & NODE_LINK_VALID))
+			continue;
+		
+		nodecomp.add_link(blink);
 	}
 }
 
-static void binary_math_node(bvm::NodeGraph &graph, InputMap &input_map, OutputMap &output_map,
-                             bNode *bnode, const bvm::string &type)
+/* ------------------------------------------------------------------------- */
+
+static void binary_math_node(bNodeCompiler *comp, bNode *bnode, const bvm::string &type)
 {
-	bvm::NodeInstance *node = graph.add_node(type, bnode->name);
-	map_input_socket(input_map, bnode, 0, node, "value_a");
-	map_input_socket(input_map, bnode, 1, node, "value_b");
-	map_output_socket(output_map, bnode, 0, node, "value");
+	bvm::NodeInstance *node = comp->add_node(type, bnode->name);
+	comp->map_input_socket(bnode, 0, node, "value_a");
+	comp->map_input_socket(bnode, 1, node, "value_b");
+	comp->map_output_socket(bnode, 0, node, "value");
 }
 
-static void unary_math_node(bvm::NodeGraph &graph, InputMap &input_map, OutputMap &output_map,
-                            bNode *bnode, const bvm::string &type)
+static void unary_math_node(bNodeCompiler *comp, bNode *bnode, const bvm::string &type)
 {
-	bvm::NodeInstance *node = graph.add_node(type, bnode->name);
+	bvm::NodeInstance *node = comp->add_node(type, bnode->name);
 	bNodeSocket *sock0 = (bNodeSocket *)BLI_findlink(&bnode->inputs, 0);
 	bNodeSocket *sock1 = (bNodeSocket *)BLI_findlink(&bnode->inputs, 1);
 	bool sock0_linked = !nodeSocketIsHidden(sock0) && (sock0->flag & SOCK_IN_USE);
 	bool sock1_linked = !nodeSocketIsHidden(sock1) && (sock1->flag & SOCK_IN_USE);
 	if (sock0_linked || !sock1_linked)
-		map_input_socket(input_map, bnode, 

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list