Amaranth Addon
[blender-addons-contrib.git] / amaranth / node_editor / simplify_nodes.py
1 #  This program is free software; you can redistribute it and/or
2 #  modify it under the terms of the GNU General Public License
3 #  as published by the Free Software Foundation; either version 2
4 #  of the License, or (at your option) any later version.
5 #
6 #  This program is distributed in the hope that it will be useful,
7 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
8 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9 #  GNU General Public License for more details.
10 #
11 #  You should have received a copy of the GNU General Public License
12 #  along with this program; if not, write to the Free Software Foundation,
13 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 """
15 Nodes Simplify Panel [WIP Feature]
16
17 Disable/Enable certain nodes at a time. Useful to quickly "simplify"
18 compositing.
19 This feature is a work in progress, the main issue now is when switching
20 many different kinds one after the other.
21
22 On the Nodes Editor Properties N panel.
23 """
24
25 import bpy
26
27
28 def init():
29     nodes_compo_types = (
30         ("ALL", "All Types", "", 0),
31         ("BLUR", "Blur", "", 1),
32         ("BOKEHBLUR", "Bokeh Blur", "", 2),
33         ("VECBLUR", "Vector Blur", "", 3),
34         ("DEFOCUS", "Defocus", "", 4),
35         ("R_LAYERS", "Render Layer", "", 5),
36     )
37     node = bpy.types.Node
38     nodes_compo = bpy.types.CompositorNodeTree
39     nodes_compo.types = bpy.props.EnumProperty(
40         items=nodes_compo_types, name="Types")
41     nodes_compo.toggle_mute = bpy.props.BoolProperty(default=False)
42     node.status = bpy.props.BoolProperty(default=False)
43
44
45 def clear():
46     wm = bpy.context.window_manager
47     for p in ("types", "toggle_mute", "status"):
48         if wm.get(p):
49             del wm[p]
50
51
52 class AMTH_NODE_PT_simplify(bpy.types.Panel):
53
54     bl_space_type = "NODE_EDITOR"
55     bl_region_type = "UI"
56     bl_label = "Simplify"
57     bl_options = {"DEFAULT_CLOSED"}
58
59     @classmethod
60     def poll(cls, context):
61         space = context.space_data
62         return space.type == "NODE_EDITOR" \
63             and space.node_tree is not None \
64             and space.tree_type == "CompositorNodeTree"
65
66     def draw(self, context):
67         layout = self.layout
68         node_tree = context.scene.node_tree
69
70         if node_tree is not None:
71             layout.prop(node_tree, "types")
72             layout.operator(AMTH_NODE_OT_toggle_mute.bl_idname,
73                             text="Turn On" if node_tree.toggle_mute else "Turn Off",
74                             icon="RESTRICT_VIEW_OFF" if node_tree.toggle_mute else "RESTRICT_VIEW_ON")
75
76             if node_tree.types == "VECBLUR":
77                 layout.label(text="This will also toggle the Vector pass {}".format(
78                     "on" if node_tree.toggle_mute else "off"), icon="INFO")
79
80
81 class AMTH_NODE_OT_toggle_mute(bpy.types.Operator):
82
83     bl_idname = "node.toggle_mute"
84     bl_label = "Toggle Mute"
85
86     def execute(self, context):
87         scene = context.scene
88         node_tree = scene.node_tree
89         node_type = node_tree.types
90         rlayers = scene.render
91
92         if "amaranth_pass_vector" not in scene.keys():
93             scene["amaranth_pass_vector"] = []
94
95         # can"t extend() the list, so make a dummy one
96         pass_vector = scene["amaranth_pass_vector"]
97
98         if not pass_vector:
99             pass_vector = []
100
101         if node_tree.toggle_mute:
102             for node in node_tree.nodes:
103                 if node_type == "ALL":
104                     node.mute = node.status
105                 if node.type == node_type:
106                     node.mute = node.status
107                 if node_type == "VECBLUR":
108                     for layer in rlayers.layers:
109                         if layer.name in pass_vector:
110                             layer.use_pass_vector = True
111                             pass_vector.remove(layer.name)
112
113                 node_tree.toggle_mute = False
114
115         else:
116             for node in node_tree.nodes:
117                 if node_type == "ALL":
118                     node.mute = True
119                 if node.type == node_type:
120                     node.status = node.mute
121                     node.mute = True
122                 if node_type == "VECBLUR":
123                     for layer in rlayers.layers:
124                         if layer.use_pass_vector:
125                             pass_vector.append(layer.name)
126                             layer.use_pass_vector = False
127                             pass
128
129                 node_tree.toggle_mute = True
130
131         # Write back to the custom prop
132         pass_vector = sorted(set(pass_vector))
133         scene["amaranth_pass_vector"] = pass_vector
134
135         return {"FINISHED"}
136
137
138 def register():
139     init()
140     bpy.utils.register_class(AMTH_NODE_PT_simplify)
141     bpy.utils.register_class(AMTH_NODE_OT_toggle_mute)
142
143
144 def unregister():
145     clear()
146     bpy.utils.unregister_class(AMTH_NODE_PT_simplify)
147     bpy.utils.unregister_class(AMTH_NODE_OT_toggle_mute)