[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [56586] trunk/blender: Workaround for C nodes: In order to make registerable RNA methods of the standard C nodes (e.g.

Lukas Toenne lukas.toenne at googlemail.com
Wed May 8 17:40:41 CEST 2013


Revision: 56586
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=56586
Author:   lukastoenne
Date:     2013-05-08 15:40:40 +0000 (Wed, 08 May 2013)
Log Message:
-----------
Workaround for C nodes: In order to make registerable RNA methods of the standard C nodes (e.g. poll or draw_buttons) available in python scripts, they need a specialized Node subtype (called NodeInternal). This is necessary because bpy omits any registerable functions of RNA types in the generated python classes, relying instead on using the supposed native implementation in a registered python class. Since the standard shader/compositor/texture nodes in Blender are not registered but directly created in makesrna they lack all registerable function in the associated python types. The NodeInternal RNA subtype replaces the registerable functions of the base Node type to solve this issue.

Modified Paths:
--------------
    trunk/blender/release/scripts/modules/bpy_types.py
    trunk/blender/source/blender/makesrna/intern/rna_nodetree.c

Modified: trunk/blender/release/scripts/modules/bpy_types.py
===================================================================
--- trunk/blender/release/scripts/modules/bpy_types.py	2013-05-08 14:58:41 UTC (rev 56585)
+++ trunk/blender/release/scripts/modules/bpy_types.py	2013-05-08 15:40:40 UTC (rev 56586)
@@ -853,6 +853,10 @@
         return True
 
 
+class NodeInternal(Node):
+    __slots__ = ()
+
+
 class NodeSocket(StructRNA, metaclass=RNAMetaPropGroup):
     __slots__ = ()
 
@@ -869,7 +873,7 @@
 
 
 # These are intermediate subclasses, need a bpy type too
-class CompositorNode(Node):
+class CompositorNode(NodeInternal):
     __slots__ = ()
 
     @classmethod
@@ -880,7 +884,7 @@
         self.tag_need_exec()
 
 
-class ShaderNode(Node):
+class ShaderNode(NodeInternal):
     __slots__ = ()
 
     @classmethod
@@ -888,7 +892,7 @@
         return ntree.bl_idname == 'ShaderNodeTree'
 
 
-class TextureNode(Node):
+class TextureNode(NodeInternal):
     __slots__ = ()
 
     @classmethod

Modified: trunk/blender/source/blender/makesrna/intern/rna_nodetree.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_nodetree.c	2013-05-08 14:58:41 UTC (rev 56585)
+++ trunk/blender/source/blender/makesrna/intern/rna_nodetree.c	2013-05-08 15:40:40 UTC (rev 56586)
@@ -2218,6 +2218,49 @@
 
 /* ******** Node Types ******** */
 
+static int rna_NodeInternal_poll(StructRNA *srna, bNodeTree *ntree)
+{
+	bNodeType *ntype = RNA_struct_blender_type_get(srna);
+	return ntype && (!ntype->poll || ntype->poll(ntype, ntree));
+}
+
+static int rna_NodeInternal_poll_instance(bNode *node, bNodeTree *ntree)
+{
+	bNodeType *ntype = node->typeinfo;
+	if (ntype->poll_instance) {
+		return ntype->poll_instance(node, ntree);
+	}
+	else {
+		/* fall back to basic poll function */
+		return !ntype->poll || ntype->poll(ntype, ntree);
+	}
+}
+
+static void rna_NodeInternal_update(ID *id, bNode *node)
+{
+	bNodeTree *ntree = (bNodeTree *)id;
+	if (node->typeinfo->updatefunc)
+		node->typeinfo->updatefunc(ntree, node);
+}
+
+static void rna_NodeInternal_draw_buttons(ID *id, bNode *node, struct bContext *C, struct uiLayout *layout)
+{
+	if (node->typeinfo->uifunc) {
+		PointerRNA ptr;
+		RNA_pointer_create(id, &RNA_Node, node, &ptr);
+		node->typeinfo->uifunc(layout, C, &ptr);
+	}
+}
+
+static void rna_NodeInternal_draw_buttons_ext(ID *id, bNode *node, struct bContext *C, struct uiLayout *layout)
+{
+	if (node->typeinfo->uifuncbut) {
+		PointerRNA ptr;
+		RNA_pointer_create(id, &RNA_Node, node, &ptr);
+		node->typeinfo->uifuncbut(layout, C, &ptr);
+	}
+}
+
 static void rna_CompositorNode_tag_need_exec(bNode *node)
 {
 	node->need_exec = TRUE;
@@ -5676,7 +5719,7 @@
 {
 	StructRNA *srna;
 	
-	srna = RNA_def_struct(brna, "ShaderNode", "Node");
+	srna = RNA_def_struct(brna, "ShaderNode", "NodeInternal");
 	RNA_def_struct_ui_text(srna, "Shader Node", "Material shader node");
 	RNA_def_struct_sdna(srna, "bNode");
 	RNA_def_struct_register_funcs(srna, "rna_ShaderNode_register", "rna_Node_unregister", NULL);
@@ -5687,7 +5730,7 @@
 	StructRNA *srna;
 	FunctionRNA *func;
 	
-	srna = RNA_def_struct(brna, "CompositorNode", "Node");
+	srna = RNA_def_struct(brna, "CompositorNode", "NodeInternal");
 	RNA_def_struct_ui_text(srna, "Compositor Node", "");
 	RNA_def_struct_sdna(srna, "bNode");
 	RNA_def_struct_register_funcs(srna, "rna_CompositorNode_register", "rna_Node_unregister", NULL);
@@ -5701,7 +5744,7 @@
 {
 	StructRNA *srna;
 	
-	srna = RNA_def_struct(brna, "TextureNode", "Node");
+	srna = RNA_def_struct(brna, "TextureNode", "NodeInternal");
 	RNA_def_struct_ui_text(srna, "Texture Node", "");
 	RNA_def_struct_sdna(srna, "bNode");
 	RNA_def_struct_register_funcs(srna, "rna_TextureNode_register", "rna_Node_unregister", NULL);
@@ -6286,6 +6329,64 @@
 	rna_def_node_socket_virtual(brna, "NodeSocketVirtual");
 }
 
+static void rna_def_internal_node(BlenderRNA *brna)
+{
+	/* XXX Workaround: Registered functions are not exposed in python by bpy,
+	 * it expects them to be registered from python and use the native implementation.
+	 * However, the standard node types are not registering these functions from python,
+	 * so in order to call them in py scripts we need to overload and replace them with plain C callbacks.
+	 * This type provides a usable basis for node types defined in C.
+	 */
+	
+	StructRNA *srna;
+	PropertyRNA *parm;
+	FunctionRNA *func;
+	
+	srna = RNA_def_struct(brna, "NodeInternal", "Node");
+	RNA_def_struct_sdna(srna, "bNode");
+	
+	/* poll */
+	func = RNA_def_function(srna, "poll", "rna_NodeInternal_poll");
+	RNA_def_function_ui_description(func, "If non-null output is returned, the node type can be added to the tree");
+	RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_USE_SELF_TYPE);
+	RNA_def_function_return(func, RNA_def_boolean(func, "visible", FALSE, "", ""));
+	parm = RNA_def_pointer(func, "node_tree", "NodeTree", "Node Tree", "");
+	RNA_def_property_flag(parm, PROP_REQUIRED);
+	
+	func = RNA_def_function(srna, "poll_instance", "rna_NodeInternal_poll_instance");
+	RNA_def_function_ui_description(func, "If non-null output is returned, the node can be added to the tree");
+	RNA_def_function_return(func, RNA_def_boolean(func, "visible", FALSE, "", ""));
+	parm = RNA_def_pointer(func, "node_tree", "NodeTree", "Node Tree", "");
+	RNA_def_property_flag(parm, PROP_REQUIRED);
+	
+	/* update */
+	func = RNA_def_function(srna, "update", "rna_NodeInternal_update");
+	RNA_def_function_ui_description(func, "Update on editor changes");
+	RNA_def_function_flag(func, FUNC_USE_SELF_ID | FUNC_ALLOW_WRITE);
+	
+	/* draw buttons */
+	func = RNA_def_function(srna, "draw_buttons", "rna_NodeInternal_draw_buttons");
+	RNA_def_function_ui_description(func, "Draw node buttons");
+	RNA_def_function_flag(func, FUNC_USE_SELF_ID);
+	parm = RNA_def_pointer(func, "context", "Context", "", "");
+	RNA_def_property_flag(parm, PROP_REQUIRED | PROP_NEVER_NULL);
+	parm = RNA_def_property(func, "layout", PROP_POINTER, PROP_NONE);
+	RNA_def_property_struct_type(parm, "UILayout");
+	RNA_def_property_ui_text(parm, "Layout", "Layout in the UI");
+	RNA_def_property_flag(parm, PROP_REQUIRED | PROP_NEVER_NULL);
+
+	/* draw buttons extended */
+	func = RNA_def_function(srna, "draw_buttons_ext", "rna_NodeInternal_draw_buttons_ext");
+	RNA_def_function_ui_description(func, "Draw node buttons in the sidebar");
+	RNA_def_function_flag(func, FUNC_USE_SELF_ID);
+	parm = RNA_def_pointer(func, "context", "Context", "", "");
+	RNA_def_property_flag(parm, PROP_REQUIRED | PROP_NEVER_NULL);
+	parm = RNA_def_property(func, "layout", PROP_POINTER, PROP_NONE);
+	RNA_def_property_struct_type(parm, "UILayout");
+	RNA_def_property_ui_text(parm, "Layout", "Layout in the UI");
+	RNA_def_property_flag(parm, PROP_REQUIRED | PROP_NEVER_NULL);
+}
+
 static void rna_def_node_sockets_api(BlenderRNA *brna, PropertyRNA *cprop, int in_out)
 {
 	StructRNA *srna;
@@ -7001,6 +7102,8 @@
 	
 	rna_def_node(brna);
 	rna_def_node_link(brna);
+	
+	rna_def_internal_node(brna);
 	rna_def_shader_node(brna);
 	rna_def_compositor_node(brna);
 	rna_def_texture_node(brna);




More information about the Bf-blender-cvs mailing list