[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [4434] contrib/py/scripts/addons/ node_efficiency_tools.py: Code clean up after code review.

Bartek Skorupa bartekskorupa at bartekskorupa.com
Wed Mar 27 08:50:48 CET 2013


Revision: 4434
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=4434
Author:   bartekskorupa
Date:     2013-03-27 07:50:47 +0000 (Wed, 27 Mar 2013)
Log Message:
-----------
Code clean up after code review.
https://codereview.appspot.com/7651047/diff/1/node_efficiency_tools.py#skiplinks-148
This review has been done using not the latest version of the script and many of the suggestions have been implemented earlier.
This commit addredded the rest of the suggestions from this review.

Modified Paths:
--------------
    contrib/py/scripts/addons/node_efficiency_tools.py

Modified: contrib/py/scripts/addons/node_efficiency_tools.py
===================================================================
--- contrib/py/scripts/addons/node_efficiency_tools.py	2013-03-26 19:59:10 UTC (rev 4433)
+++ contrib/py/scripts/addons/node_efficiency_tools.py	2013-03-27 07:50:47 UTC (rev 4434)
@@ -19,7 +19,7 @@
 bl_info = {
     'name': "Nodes Efficiency Tools",
     'author': "Bartek Skorupa",
-    'version': (2, 1.3),
+    'version': (2, 1.4),
     'blender': (2, 6, 6),
     'location': "Node Editor Properties Panel (Ctrl-SPACE)",
     'description': "Nodes Efficiency Tools",
@@ -133,10 +133,7 @@
     ('ShaderNodeHoldout', 'HOLDOUT', 'Holdout'),
     )
 
-def set_convenience_variables(context):
-    global nodes
-    global links
-
+def get_nodes_links(context):
     space = context.space_data
     tree = space.node_tree
     nodes = tree.nodes
@@ -156,10 +153,17 @@
         nodes = tree.nodes
         links = tree.links
     
-    return
+    return nodes, links
 
 
-class MergeNodes(bpy.types.Operator):
+class NodeToolBase:
+    @classmethod
+    def poll(cls, context):
+        space = context.space_data
+        return space.type == 'NODE_EDITOR' and space.node_tree is not None
+
+
+class MergeNodes(bpy.types.Operator, NodeToolBase):
     bl_idname = "node.merge_nodes"
     bl_label = "Merge Nodes"
     bl_description = "Merge Selected Nodes"
@@ -181,18 +185,13 @@
             ],
         )
     
-    @classmethod
-    def poll(cls, context):
-        space = context.space_data
-        return space.type == 'NODE_EDITOR' and space.node_tree is not None
-    
     def execute(self, context):
         tree_type = context.space_data.node_tree.type
         if tree_type == 'COMPOSITING':
             node_type = 'CompositorNode'
         elif tree_type == 'SHADER':
             node_type = 'ShaderNode'
-        set_convenience_variables(context)
+        nodes, links = get_nodes_links(context)
         mode = self.mode
         merge_type = self.merge_type
         selected_mix = []  # entry = [index, loc]
@@ -202,13 +201,13 @@
         for i, node in enumerate(nodes):
             if node.select and node.outputs:
                 if merge_type == 'AUTO':
-                    for (type, the_list, dst) in (
+                    for (type, types_list, dst) in (
                         ('SHADER', merge_shaders, selected_shader),
                         ('RGBA', [t[0] for t in blend_types], selected_mix),
                         ('VALUE', [t[0] for t in operations], selected_math),
                         ):
                         output_type = node.outputs[0].type
-                        valid_mode = mode in the_list
+                        valid_mode = mode in types_list
                         # When mode is 'MIX' use mix node for both 'RGBA' and 'VALUE' output types.
                         # Cheat that output type is 'RGBA',
                         # and that 'MIX' exists in math operations list.
@@ -220,12 +219,12 @@
                         if output_type == type and valid_mode:
                             dst.append([i, node.location.x, node.location.y])
                 else:
-                    for (type, the_list, dst) in (
+                    for (type, types_list, dst) in (
                         ('SHADER', merge_shaders, selected_shader),
                         ('MIX', [t[0] for t in blend_types], selected_mix),
                         ('MATH', [t[0] for t in operations], selected_math),
                         ):
-                        if merge_type == type and mode in the_list:
+                        if merge_type == type and mode in types_list:
                             dst.append([i, node.location.x, node.location.y])
         # When nodes with output kinds 'RGBA' and 'VALUE' are selected at the same time
         # use only 'Mix' nodes for merging.
@@ -234,25 +233,25 @@
             selected_mix += selected_math
             selected_math = []
         
-        for the_list in [selected_mix, selected_shader, selected_math]:
-            if the_list:
+        for nodes_list in [selected_mix, selected_shader, selected_math]:
+            if nodes_list:
                 count_before = len(nodes)
                 # sort list by loc_x - reversed
-                the_list.sort(key = lambda k: k[1], reverse = True)
+                nodes_list.sort(key = lambda k: k[1], reverse = True)
                 # get maximum loc_x
-                loc_x = the_list[0][1] + 350.0
-                the_list.sort(key = lambda k: k[2], reverse = True)
-                loc_y = the_list[len(the_list) - 1][2]
+                loc_x = nodes_list[0][1] + 350.0
+                nodes_list.sort(key = lambda k: k[2], reverse = True)
+                loc_y = nodes_list[len(nodes_list) - 1][2]
                 offset_y = 40.0
-                if the_list == selected_shader:
+                if nodes_list == selected_shader:
                     offset_y = 150.0
-                the_range = len(the_list)-1
+                the_range = len(nodes_list)-1
                 do_hide = True
-                if len(the_list) == 1:
+                if len(nodes_list) == 1:
                     the_range = 1
                     do_hide = False
                 for i in range(the_range):
-                    if the_list == selected_mix:
+                    if nodes_list == selected_mix:
                         add_type = node_type + 'MixRGB'
                         add = nodes.new(add_type)
                         add.blend_type = mode
@@ -261,7 +260,7 @@
                         first = 1
                         second = 2
                         add.width_hidden = 100.0
-                    elif the_list == selected_math:
+                    elif nodes_list == selected_math:
                         add_type = node_type + 'Math'
                         add = nodes.new(add_type)
                         add.operation = mode
@@ -269,7 +268,7 @@
                         first = 0
                         second = 1
                         add.width_hidden = 100.0
-                    elif the_list == selected_shader:
+                    elif nodes_list == selected_shader:
                         if mode == 'MIX':
                             add_type = node_type + 'MixShader'
                             add = nodes.new(add_type)
@@ -282,31 +281,30 @@
                             first = 0
                             second = 1
                             add.width_hidden = 100.0
-                    add.location.x = loc_x
-                    add.location.y = loc_y
+                    add.location = loc_x, loc_y
                     loc_y += offset_y
                     add.select = True
                 count_adds = i + 1
                 count_after = len(nodes)
                 index = count_after - 1
                 # add link from "first" selected and "first" add node
-                links.new(nodes[the_list[0][0]].outputs[0], nodes[count_after - 1].inputs[first])
+                links.new(nodes[nodes_list[0][0]].outputs[0], nodes[count_after - 1].inputs[first])
                 # add links between added ADD nodes and between selected and ADD nodes
                 for i in range(count_adds):
                     if i < count_adds - 1:
                         links.new(nodes[index-1].inputs[first], nodes[index].outputs[0])
-                    if len(the_list) > 1:
-                        links.new(nodes[index].inputs[second], nodes[the_list[i+1][0]].outputs[0])
+                    if len(nodes_list) > 1:
+                        links.new(nodes[index].inputs[second], nodes[nodes_list[i+1][0]].outputs[0])
                     index -= 1
                 # set "last" of added nodes as active    
                 nodes.active = nodes[count_before]
-                for i, x, y in the_list:
+                for i, x, y in nodes_list:
                     nodes[i].select = False
                     
         return {'FINISHED'}
 
 
-class BatchChangeNodes(bpy.types.Operator):
+class BatchChangeNodes(bpy.types.Operator, NodeToolBase):
     bl_idname = "node.batch_change"
     bl_label = "Batch Change"
     bl_description = "Batch Change Blend Type and Math Operation"
@@ -321,13 +319,8 @@
         items = operations + navs,
         )
         
-    @classmethod
-    def poll(cls, context):
-        space = context.space_data
-        return space.type == 'NODE_EDITOR' and space.node_tree is not None
-    
     def execute(self, context):
-        set_convenience_variables(context)
+        nodes, links = get_nodes_links(context)
         blend_type = self.blend_type
         operation = self.operation
         for node in context.selected_nodes:
@@ -373,7 +366,7 @@
         return {'FINISHED'}
 
 
-class ChangeMixFactor(bpy.types.Operator):
+class ChangeMixFactor(bpy.types.Operator, NodeToolBase):
     bl_idname = "node.factor"
     bl_label = "Change Factor"
     bl_description = "Change Factors of Mix Nodes and Mix Shader Nodes"
@@ -384,13 +377,8 @@
     # Else - change factor by option value.
     option = FloatProperty()
     
-    @classmethod
-    def poll(cls, context):
-        space = context.space_data
-        return space.type == 'NODE_EDITOR' and space.node_tree is not None
-    
     def execute(self, context):
-        set_convenience_variables(context)
+        nodes, links = get_nodes_links(context)
         option = self.option
         selected = []  # entry = index
         for si, node in enumerate(nodes):
@@ -428,7 +416,7 @@
         return valid
     
     def execute(self, context):
-        set_convenience_variables(context)
+        nodes, links = get_nodes_links(context)
         selected = [n for n in nodes if n.select]
         reselect = []  # duplicated nodes will be selected after execution
         active = nodes.active
@@ -487,7 +475,7 @@
         return {'FINISHED'}
 
 
-class NodesCopyLabel(bpy.types.Operator):
+class NodesCopyLabel(bpy.types.Operator, NodeToolBase):
     bl_idname = "node.copy_label"
     bl_label = "Copy Label"
     bl_options = {'REGISTER', 'UNDO'}
@@ -502,13 +490,8 @@
             ]
         )
     
-    @classmethod
-    def poll(cls, context):
-        space = context.space_data
-        return space.type == 'NODE_EDITOR' and space.node_tree is not None
-    
     def execute(self, context):
-        set_convenience_variables(context)
+        nodes, links = get_nodes_links(context)

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list