[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [52924] trunk/blender/release/scripts/ startup/bl_operators/node.py: A few basic Python operators for adding nodes in the node editor tree.

Lukas Toenne lukas.toenne at googlemail.com
Wed Dec 12 13:50:43 CET 2012


Revision: 52924
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=52924
Author:   lukastoenne
Date:     2012-12-12 12:50:43 +0000 (Wed, 12 Dec 2012)
Log Message:
-----------
A few basic Python operators for adding nodes in the node editor tree. These operators basically have the same functionality as the 'Add' menu (which currently does not even use operators itself). They can be used in customized tools.

The node_add_move operator is an extended variant which starts the (modal) transform operator right after adding a node, as a quicker way of inserting nodes in a tree.

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

Modified: trunk/blender/release/scripts/startup/bl_operators/node.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_operators/node.py	2012-12-12 12:50:39 UTC (rev 52923)
+++ trunk/blender/release/scripts/startup/bl_operators/node.py	2012-12-12 12:50:43 UTC (rev 52924)
@@ -20,8 +20,82 @@
 
 import bpy
 from bpy.types import Operator
-from bpy.props import EnumProperty
+from bpy.props import EnumProperty, StringProperty
 
+# Base class for node 'Add' operators
+class NodeAddOperator():
+    @staticmethod
+    def store_mouse_cursor(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)
+
+    def create_node(self, context, node_type):
+        space = context.space_data
+        tree = space.edit_tree
+
+        node = tree.nodes.new(type=node_type)
+
+        # select only the new node
+        for n in tree.nodes:
+            n.select = (n == node)
+        tree.nodes.active = node
+        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)
+
+    # Default invoke stores the mouse position to place the node correctly
+    def invoke(self, context, event):
+        self.store_mouse_cursor(context, event)
+        return self.execute(context)
+
+
+# Simple basic operator for adding a node
+class NODE_OT_add_node(NodeAddOperator, Operator):
+    '''Add a node to the active tree'''
+    bl_idname = "node.add_node"
+    bl_label = "Add Node"
+
+    type = StringProperty(name="Node Type", description="Node type")
+    
+    # optional group tree parameter for group nodes
+    group_tree = StringProperty(name="Group tree", description="Group node tree name")
+
+    def execute(self, context):
+        node = self.create_node(context, self.type)
+
+        # set the node group tree of a group node
+        if self.properties.is_property_set('group_tree'):
+            node.node_tree = bpy.data.node_groups[self.group_tree]
+
+        return {'FINISHED'}
+
+
+# Adds a node and immediately starts the transform operator for inserting in a tree
+class NODE_OT_add_node_move(NODE_OT_add_node):
+    '''Add a node to the active tree and start transform'''
+    bl_idname = "node.add_node_move"
+    bl_label = "Add Node and Move"
+
+    type = StringProperty(name="Node Type", description="Node type")
+    
+    # optional group tree parameter for group nodes
+    group_tree = StringProperty(name="Group tree", description="Group node tree name")
+
+    def invoke(self, context, event):
+        self.store_mouse_cursor(context, event)
+        self.execute(context)
+        return bpy.ops.transform.translate('INVOKE_DEFAULT')
+
+
 # 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.




More information about the Bf-blender-cvs mailing list