[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [49700] trunk/blender: Search option for adding nodes.

Lukas Toenne lukas.toenne at googlemail.com
Wed Aug 8 18:44:17 CEST 2012


Revision: 49700
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=49700
Author:   lukastoenne
Date:     2012-08-08 16:44:16 +0000 (Wed, 08 Aug 2012)
Log Message:
-----------
Search option for adding nodes.

The 'Add' menu in the node editor now has an option 'Search' at the top, which opens a separate popup for searching node types by name.

The operator for this is implemented completely in Python (this could also be done for the regular menu-based Add options in the future). There are a few necessary extensions to the RNA as well:

* The View2D struct in regions is now exposed. Currently only contains converter functions for coordinates from the region to the view (i.e. scrolled and zoomed view space). Used for converting mouse location to node space.

* The SpaceNode exposes the existing 'cursor_location' for operators to store mouse position beyond invoke calls. Not used for anything else (transforms) so far.

* The edit_tree in SpaceNode is also exposed, this is needed for operators to work correctly inside node groups.

Modified Paths:
--------------
    trunk/blender/release/scripts/startup/bl_operators/__init__.py
    trunk/blender/source/blender/editors/space_node/node_header.c
    trunk/blender/source/blender/makesrna/intern/rna_screen.c
    trunk/blender/source/blender/makesrna/intern/rna_space.c
    trunk/blender/source/blenderplayer/bad_level_call_stubs/stubs.c

Added Paths:
-----------
    trunk/blender/release/scripts/startup/bl_operators/node.py

Modified: trunk/blender/release/scripts/startup/bl_operators/__init__.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_operators/__init__.py	2012-08-08 16:14:56 UTC (rev 49699)
+++ trunk/blender/release/scripts/startup/bl_operators/__init__.py	2012-08-08 16:44:16 UTC (rev 49700)
@@ -29,6 +29,7 @@
     "console",
     "image",
     "mesh",
+    "node",
     "object_align",
     "object",
     "object_randomize_transform",

Added: trunk/blender/release/scripts/startup/bl_operators/node.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_operators/node.py	                        (rev 0)
+++ trunk/blender/release/scripts/startup/bl_operators/node.py	2012-08-08 16:44:16 UTC (rev 49700)
@@ -0,0 +1,100 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+#  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.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8-80 compliant>
+
+import bpy
+from bpy.types import Operator
+from bpy.props import (EnumProperty,
+                       FloatVectorProperty,
+                       StringProperty,
+                       CollectionProperty
+                       )
+
+# XXX These node item lists should actually be generated by a callback at operator execution time (see node_type_items below),
+# using the active node tree from the context. Due to a difficult bug in bpy this is not possible (item list memory gets freed too early),
+# so for now just copy the static item lists to these global variables.
+#
+# In the custom_nodes branch, the static per-tree-type node items are replaced by a single independent type list anyway (with a poll function
+# to limit node types to the respective trees). So this workaround is only temporary.
+
+node_type_items_dict = {}
+node_type_items_dict['SHADER'] = [(item.identifier, item.name, item.description, item.value) for item in bpy.types.ShaderNode.bl_rna.properties['type'].enum_items]
+node_type_items_dict['COMPOSITING'] = [(item.identifier, item.name, item.description, item.value) for item in bpy.types.CompositorNode.bl_rna.properties['type'].enum_items]
+node_type_items_dict['TEXTURE'] = [(item.identifier, item.name, item.description, item.value) for item in bpy.types.TextureNode.bl_rna.properties['type'].enum_items]
+
+# Returns the enum item list for the edited tree in the context
+def node_type_items(self, context):
+    snode = context.space_data
+    if not snode:
+        return []
+    tree = snode.edit_tree
+    if not tree:
+        return []
+    
+    # XXX Does not work correctly, see comment above
+    #return [(item.identifier, item.name, item.description, item.value) for item in tree.nodes.bl_rna.functions['new'].parameters['type'].enum_items]
+    
+    if tree.type in node_type_items_dict:
+        return node_type_items_dict[tree.type]
+    else:
+        return []
+
+class NODE_OT_add_search(bpy.types.Operator):
+    '''Add a node to the active tree'''
+    bl_idname = "node.add_search"
+    bl_label = "Search and Add Node"
+    bl_options = {'REGISTER', 'UNDO'}
+
+    # XXX this should be called 'node_type' but the operator search property is hardcoded to 'type' by a hack in bpy_operator_wrap.c ...
+    type = EnumProperty(items=node_type_items, name="Node Type", description="Node type")
+
+    def create_node(self, context):
+        space = context.space_data
+        tree = space.edit_tree
+        
+        node = tree.nodes.new(type=self.type)
+        for n in tree.nodes:
+            if n==node:
+                node.select = True
+                tree.nodes.active = node
+            else:
+                node.select = False
+        node.location = space.cursor_location
+        return node
+    
+    @classmethod
+    def poll(cls, context):
+        space = context.space_data
+        # needs active node editor and a tree to add nodes to
+        return space.type == 'NODE_EDITOR' and space.edit_tree
+
+    def execute(self, context):
+        self.create_node(context)
+        return {'FINISHED'}
+
+    def invoke(self, context, event):
+        space = context.space_data
+        v2d = context.region.view2d
+
+        # convert mouse position to the View2D for later node placement
+        space.cursor_location = v2d.region_to_view(event.mouse_region_x, event.mouse_region_y)
+        
+        context.window_manager.invoke_search_popup(self)
+        return {'CANCELLED'}
+

Modified: trunk/blender/source/blender/editors/space_node/node_header.c
===================================================================
--- trunk/blender/source/blender/editors/space_node/node_header.c	2012-08-08 16:14:56 UTC (rev 49699)
+++ trunk/blender/source/blender/editors/space_node/node_header.c	2012-08-08 16:44:16 UTC (rev 49700)
@@ -235,6 +235,9 @@
 	if (!snode->nodetree)
 		uiLayoutSetActive(layout, FALSE);
 	
+	uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_DEFAULT);
+	uiItemO(layout, "Search ...", 0, "NODE_OT_add_search");
+	
 	if (ntreetype && ntreetype->foreach_nodeclass)
 		ntreetype->foreach_nodeclass(scene, layout, node_menu_add_foreach_cb);
 }

Modified: trunk/blender/source/blender/makesrna/intern/rna_screen.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_screen.c	2012-08-08 16:14:56 UTC (rev 49699)
+++ trunk/blender/source/blender/makesrna/intern/rna_screen.c	2012-08-08 16:44:16 UTC (rev 49700)
@@ -57,6 +57,8 @@
 
 #include "BKE_global.h"
 
+#include "UI_view2d.h"
+
 static void rna_Screen_scene_set(PointerRNA *ptr, PointerRNA value)
 {
 	bScreen *sc = (bScreen *)ptr->data;
@@ -138,6 +140,19 @@
 	}
 }
 
+void rna_View2D_region_to_view(struct View2D *v2d, int x, int y, float result[2])
+{
+	UI_view2d_region_to_view(v2d, x, y, &result[0], &result[1]);
+}
+
+void rna_View2D_view_to_region(struct View2D *v2d, float x, float y, int clip, int result[2])
+{
+	if (clip)
+		UI_view2d_view_to_region(v2d, x, y, &result[0], &result[1]);
+	else
+		UI_view2d_to_region_no_clip(v2d, x, y, &result[0], &result[1]);
+}
+
 #else
 
 /* Area.spaces */
@@ -220,6 +235,50 @@
 	RNA_def_string(func, "text", NULL, 0, "Text", "New string for the header, no argument clears the text");
 }
 
+static void rna_def_view2d_api(StructRNA *srna)
+{
+	FunctionRNA *func;
+	PropertyRNA *parm;
+	
+	static const float view_default[2] = {0.0f, 0.0f};
+	static const int region_default[2] = {0.0f, 0.0f};
+	
+	func = RNA_def_function(srna, "region_to_view", "rna_View2D_region_to_view");
+	RNA_def_function_ui_description(func, "Transform region coordinates to 2D view");
+	parm = RNA_def_int(func, "x", 0, INT_MIN, INT_MAX, "x", "Region x coordinate", -10000, 10000);
+	RNA_def_property_flag(parm, PROP_REQUIRED);
+	parm = RNA_def_int(func, "y", 0, INT_MIN, INT_MAX, "y", "Region y coordinate", -10000, 10000);
+	RNA_def_property_flag(parm, PROP_REQUIRED);
+	parm = RNA_def_float_array(func, "result", 2, view_default, -FLT_MAX, FLT_MAX, "Result", "View coordinates", -10000.0f, 10000.0f);
+	RNA_def_property_flag(parm, PROP_THICK_WRAP);
+	RNA_def_function_output(func, parm);
+
+	func = RNA_def_function(srna, "view_to_region", "rna_View2D_view_to_region");
+	RNA_def_function_ui_description(func, "Transform 2D view coordinates to region");
+	parm = RNA_def_float(func, "x", 0.0f, -FLT_MAX, FLT_MAX, "x", "2D View x coordinate", -10000.0f, 10000.0f);
+	RNA_def_property_flag(parm, PROP_REQUIRED);
+	parm = RNA_def_float(func, "y", 0.0f, -FLT_MAX, FLT_MAX, "y", "2D View y coordinate", -10000.0f, 10000.0f);
+	RNA_def_property_flag(parm, PROP_REQUIRED);
+	RNA_def_boolean(func, "clip", 1, "Clip", "Clip coordinates to the visible region");
+	parm = RNA_def_int_array(func, "result", 2, region_default, INT_MIN, INT_MAX, "Result", "Region coordinates", -10000, 10000);
+	RNA_def_property_flag(parm, PROP_THICK_WRAP);
+	RNA_def_function_output(func, parm);
+}
+
+static void rna_def_view2d(BlenderRNA *brna)
+{
+	StructRNA *srna;
+	/* PropertyRNA *prop; */
+
+	srna = RNA_def_struct(brna, "View2D", NULL);
+	RNA_def_struct_ui_text(srna, "View2D", "Scroll and zoom for a 2D region");
+	RNA_def_struct_sdna(srna, "View2D");
+	
+	/* TODO more View2D properties could be exposed here (read-only) */
+	
+	rna_def_view2d_api(srna);
+}
+
 static void rna_def_region(BlenderRNA *brna)
 {
 	StructRNA *srna;
@@ -260,6 +319,12 @@
 	RNA_def_property_clear_flag(prop, PROP_EDITABLE);
 	RNA_def_property_ui_text(prop, "Height", "Region height");
 
+	prop = RNA_def_property(srna, "view2d", PROP_POINTER, PROP_NONE);
+	RNA_def_property_pointer_sdna(prop, NULL, "v2d");
+	RNA_def_property_clear_flag(prop, PROP_EDITABLE);
+	RNA_def_property_flag(prop, PROP_NEVER_NULL);
+	RNA_def_property_ui_text(prop, "View2D", "2D view of the region");
+
 	RNA_def_function(srna, "tag_redraw", "ED_region_tag_redraw");
 }
 
@@ -345,6 +410,7 @@
 	rna_def_screen(brna);
 	rna_def_area(brna);
 	rna_def_region(brna);
+	rna_def_view2d(brna);
 }
 
 #endif

Modified: trunk/blender/source/blender/makesrna/intern/rna_space.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_space.c	2012-08-08 16:14:56 UTC (rev 49699)
+++ trunk/blender/source/blender/makesrna/intern/rna_space.c	2012-08-08 16:44:16 UTC (rev 49700)
@@ -2961,9 +2961,14 @@
 	RNA_def_property_pointer_sdna(prop, NULL, "nodetree");
 	RNA_def_property_pointer_funcs(prop, NULL, NULL, NULL, "rna_SpaceNodeEditor_node_tree_poll");
 	RNA_def_property_flag(prop, PROP_EDITABLE);
-	RNA_def_property_ui_text(prop, "Node Tree", "Node tree being displayed and edited");
+	RNA_def_property_ui_text(prop, "Node Tree", "Node tree being displayed");
 	RNA_def_property_update(prop, NC_SPACE | ND_SPACE_NODE, "rna_SpaceNodeEditor_node_tree_update");
 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list