Code Cleanup: style, spelling and pep8 edits
[blender-staging.git] / release / scripts / startup / bl_ui / space_node.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 # <pep8 compliant>
20 import bpy
21 from bpy.types import Header, Menu, Panel
22
23
24 class NODE_HT_header(Header):
25     bl_space_type = 'NODE_EDITOR'
26
27     def draw(self, context):
28         layout = self.layout
29
30         scene = context.scene
31         snode = context.space_data
32         snode_id = snode.id
33         id_from = snode.id_from
34         toolsettings = context.tool_settings
35
36         row = layout.row(align=True)
37         row.template_header()
38
39         if context.area.show_menus:
40             row.menu("NODE_MT_view")
41             row.menu("NODE_MT_select")
42             row.menu("NODE_MT_add")
43             row.menu("NODE_MT_node")
44
45         layout.prop(snode, "tree_type", text="", expand=True)
46
47         if snode.tree_type == 'ShaderNodeTree':
48             if scene.render.use_shading_nodes:
49                 layout.prop(snode, "shader_type", text="", expand=True)
50
51             ob = context.object
52             if (not scene.render.use_shading_nodes or snode.shader_type == 'OBJECT') and ob:
53                 row = layout.row()
54                 # disable material slot buttons when pinned, cannot find correct slot within id_from (#36589)
55                 row.enabled = not snode.pin
56                 # Show material.new when no active ID/slot exists
57                 if not id_from and ob.type in {'MESH', 'CURVE', 'SURFACE', 'FONT', 'METABALL'}:
58                     row.template_ID(ob, "active_material", new="material.new")
59                 # Material ID, but not for Lamps
60                 if id_from and ob.type != 'LAMP':
61                     row.template_ID(id_from, "active_material", new="material.new")
62
63                 # Don't show "Use Nodes" Button when Engine is BI for Lamps
64                 if snode_id and not (scene.render.use_shading_nodes == 0 and ob.type == 'LAMP'):
65                     layout.prop(snode_id, "use_nodes")
66
67             if snode.shader_type == 'WORLD':
68                 row = layout.row()
69                 row.enabled = not snode.pin
70                 row.template_ID(scene, "world", new="world.new")
71                 if snode_id:
72                     row.prop(snode_id, "use_nodes")
73
74         elif snode.tree_type == 'TextureNodeTree':
75             layout.prop(snode, "texture_type", text="", expand=True)
76
77             if id_from:
78                 if snode.texture_type == 'BRUSH':
79                     layout.template_ID(id_from, "texture", new="texture.new")
80                 else:
81                     layout.template_ID(id_from, "active_texture", new="texture.new")
82             if snode_id:
83                 layout.prop(snode_id, "use_nodes")
84
85         elif snode.tree_type == 'CompositorNodeTree':
86             if snode_id:
87                 layout.prop(snode_id, "use_nodes")
88                 layout.prop(snode_id.render, "use_free_unused_nodes", text="Free Unused")
89             layout.prop(snode, "show_backdrop")
90             if snode.show_backdrop:
91                 row = layout.row(align=True)
92                 row.prop(snode, "backdrop_channels", text="", expand=True)
93             layout.prop(snode, "use_auto_render")
94
95         else:
96             # Custom node tree is edited as independent ID block
97             layout.template_ID(snode, "node_tree", new="node.new_node_tree")
98
99         layout.prop(snode, "pin", text="")
100         layout.operator("node.tree_path_parent", text="", icon='FILE_PARENT')
101
102         layout.separator()
103
104         # Snap
105         row = layout.row(align=True)
106         row.prop(toolsettings, "use_snap", text="")
107         row.prop(toolsettings, "snap_node_element", text="", icon_only=True)
108         if toolsettings.snap_node_element != 'GRID':
109             row.prop(toolsettings, "snap_target", text="")
110
111         row = layout.row(align=True)
112         row.operator("node.clipboard_copy", text="", icon='COPYDOWN')
113         row.operator("node.clipboard_paste", text="", icon='PASTEDOWN')
114
115         layout.template_running_jobs()
116
117
118 class NODE_MT_add(bpy.types.Menu):
119     bl_space_type = 'NODE_EDITOR'
120     bl_label = "Add"
121
122     def draw(self, context):
123         layout = self.layout
124
125         layout.operator_context = 'INVOKE_DEFAULT'
126         props = layout.operator("node.add_search", text="Search ...")
127         props.use_transform = True
128
129         # actual node submenus are added by draw functions from node categories
130
131
132 class NODE_MT_view(Menu):
133     bl_label = "View"
134
135     def draw(self, context):
136         layout = self.layout
137
138         layout.operator("node.properties", icon='MENU_PANEL')
139         layout.operator("node.toolbar", icon='MENU_PANEL')
140
141         layout.separator()
142
143         layout.operator("view2d.zoom_in")
144         layout.operator("view2d.zoom_out")
145
146         layout.separator()
147
148         layout.operator("node.view_selected")
149         layout.operator("node.view_all")
150
151         if context.space_data.show_backdrop:
152             layout.separator()
153
154             layout.operator("node.backimage_move", text="Backdrop move")
155             layout.operator("node.backimage_zoom", text="Backdrop zoom in").factor = 1.2
156             layout.operator("node.backimage_zoom", text="Backdrop zoom out").factor = 0.833
157             layout.operator("node.backimage_fit", text="Fit backdrop to available space")
158
159         layout.separator()
160
161         layout.operator("screen.area_dupli")
162         layout.operator("screen.screen_full_area")
163
164
165 class NODE_MT_select(Menu):
166     bl_label = "Select"
167
168     def draw(self, context):
169         layout = self.layout
170
171         layout.operator("node.select_border")
172         layout.operator("node.select_circle")
173
174         layout.separator()
175         layout.operator("node.select_all").action = 'TOGGLE'
176         layout.operator("node.select_all", text="Inverse").action = 'INVERT'
177         layout.operator("node.select_linked_from")
178         layout.operator("node.select_linked_to")
179
180         layout.separator()
181
182         layout.operator("node.select_same_type")
183         layout.operator("node.select_same_type_step").prev = True
184         layout.operator("node.select_same_type_step").prev = False
185
186         layout.separator()
187
188         layout.operator("node.find_node")
189
190
191 class NODE_MT_node(Menu):
192     bl_label = "Node"
193
194     def draw(self, context):
195         layout = self.layout
196
197         layout.operator("transform.translate")
198         layout.operator("transform.rotate")
199         layout.operator("transform.resize")
200
201         layout.separator()
202
203         layout.operator("node.duplicate_move")
204         layout.operator("node.delete")
205         layout.operator("node.delete_reconnect")
206
207         layout.separator()
208
209         layout.operator("node.join", text="Join in new Frame")
210         layout.operator("node.detach", text="Remove from Frame")
211
212         layout.separator()
213
214         layout.operator("node.link_make")
215         layout.operator("node.link_make", text="Make and Replace Links").replace = True
216         layout.operator("node.links_cut")
217         layout.operator("node.links_detach")
218
219         layout.separator()
220
221         layout.operator("node.group_edit")
222         layout.operator("node.group_ungroup")
223         layout.operator("node.group_make")
224         layout.operator("node.group_insert")
225
226         layout.separator()
227
228         layout.operator("node.hide_toggle")
229         layout.operator("node.mute_toggle")
230         layout.operator("node.preview_toggle")
231         layout.operator("node.hide_socket_toggle")
232         layout.operator("node.options_toggle")
233         layout.operator("node.collapse_hide_unused_toggle")
234
235         layout.separator()
236
237         layout.operator("node.read_renderlayers")
238         layout.operator("node.read_fullsamplelayers")
239
240
241 class NODE_MT_node_color_presets(Menu):
242     """Predefined node color"""
243     bl_label = "Color Presets"
244     preset_subdir = "node_color"
245     preset_operator = "script.execute_preset"
246     draw = Menu.draw_preset
247
248
249 class NODE_MT_node_color_specials(Menu):
250     bl_label = "Node Color Specials"
251
252     def draw(self, context):
253         layout = self.layout
254
255         layout.operator("node.node_copy_color", icon='COPY_ID')
256
257
258 class NODE_PT_active_node_generic(Panel):
259     bl_space_type = 'NODE_EDITOR'
260     bl_region_type = 'UI'
261     bl_label = "Node"
262 #    bl_options = {'HIDE_HEADER'}
263
264     @classmethod
265     def poll(cls, context):
266         space = context.space_data
267         return context.active_node is not None
268
269     def draw(self, context):
270         layout = self.layout
271         node = context.active_node
272
273         layout.prop(node, "name", icon='NODE')
274         layout.prop(node, "label", icon='NODE')
275
276
277 class NODE_PT_active_node_color(Panel):
278     bl_space_type = 'NODE_EDITOR'
279     bl_region_type = 'UI'
280     bl_label = "Color"
281     bl_options = {'DEFAULT_CLOSED'}
282
283     @classmethod
284     def poll(cls, context):
285         space = context.space_data
286         return context.active_node is not None
287
288     def draw_header(self, context):
289         node = context.active_node
290         self.layout.prop(node, "use_custom_color", text="")
291
292     def draw(self, context):
293         layout = self.layout
294         node = context.active_node
295
296         layout.enabled = node.use_custom_color
297
298         row = layout.row()
299         col = row.column()
300         col.menu("NODE_MT_node_color_presets")
301         col.prop(node, "color", text="")
302         col = row.column(align=True)
303         col.operator("node.node_color_preset_add", text="", icon='ZOOMIN').remove_active = False
304         col.operator("node.node_color_preset_add", text="", icon='ZOOMOUT').remove_active = True
305         col.menu("NODE_MT_node_color_specials", text="", icon='DOWNARROW_HLT')
306
307
308 class NODE_PT_active_node_properties(Panel):
309     bl_space_type = 'NODE_EDITOR'
310     bl_region_type = 'UI'
311     bl_label = "Properties"
312
313     @classmethod
314     def poll(cls, context):
315         space = context.space_data
316         return context.active_node is not None
317
318     def draw(self, context):
319         layout = self.layout
320         node = context.active_node
321         # set "node" context pointer for the panel layout
322         layout.context_pointer_set("node", node)
323
324         if hasattr(node, "draw_buttons_ext"):
325             node.draw_buttons_ext(context, layout)
326         elif hasattr(node, "draw_buttons"):
327             node.draw_buttons(context, layout)
328
329         # XXX this could be filtered further to exclude socket types which don't have meaningful input values (e.g. cycles shader)
330         value_inputs = [socket for socket in node.inputs if socket.enabled and not socket.is_linked]
331         if value_inputs:
332             layout.separator()
333             layout.label("Inputs:")
334             for socket in value_inputs:
335                 row = layout.row()
336                 socket.draw(context, row, node, socket.name)
337
338
339 # Node Backdrop options
340 class NODE_PT_backdrop(Panel):
341     bl_space_type = 'NODE_EDITOR'
342     bl_region_type = 'UI'
343     bl_label = "Backdrop"
344
345     @classmethod
346     def poll(cls, context):
347         snode = context.space_data
348         return snode.tree_type == 'CompositorNodeTree'
349
350     def draw_header(self, context):
351         snode = context.space_data
352         self.layout.prop(snode, "show_backdrop", text="")
353
354     def draw(self, context):
355         layout = self.layout
356
357         snode = context.space_data
358         layout.active = snode.show_backdrop
359         layout.prop(snode, "backdrop_channels", text="")
360         layout.prop(snode, "backdrop_zoom", text="Zoom")
361
362         col = layout.column(align=True)
363         col.label(text="Offset:")
364         col.prop(snode, "backdrop_x", text="X")
365         col.prop(snode, "backdrop_y", text="Y")
366         col.operator("node.backimage_move", text="Move")
367
368         layout.operator("node.backimage_fit", text="Fit")
369
370
371 class NODE_PT_quality(bpy.types.Panel):
372     bl_space_type = 'NODE_EDITOR'
373     bl_region_type = 'UI'
374     bl_label = "Performance"
375
376     @classmethod
377     def poll(cls, context):
378         snode = context.space_data
379         return snode.tree_type == 'CompositorNodeTree' and snode.node_tree is not None
380
381     def draw(self, context):
382         layout = self.layout
383
384         snode = context.space_data
385         tree = snode.node_tree
386
387         col = layout.column()
388         col.prop(tree, "render_quality", text="Render")
389         col.prop(tree, "edit_quality", text="Edit")
390         col.prop(tree, "chunk_size")
391
392         col = layout.column()
393         col.prop(tree, "use_opencl")
394         col.prop(tree, "use_groupnode_buffer")
395         col.prop(tree, "use_two_pass")
396         col.prop(tree, "use_viewer_border")
397         col.prop(snode, "show_highlight")
398         col.prop(snode, "use_hidden_preview")
399
400
401 class NODE_UL_interface_sockets(bpy.types.UIList):
402     def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
403         socket = item
404         color = socket.draw_color(context)
405
406         if self.layout_type in {'DEFAULT', 'COMPACT'}:
407             row = layout.row(align=True)
408
409             # inputs get icon on the left
410             if not socket.is_output:
411                 row.template_node_socket(color)
412
413             row.label(text=socket.name, icon_value=icon)
414
415             # outputs get icon on the right
416             if socket.is_output:
417                 row.template_node_socket(color)
418
419         elif self.layout_type in {'GRID'}:
420             layout.alignment = 'CENTER'
421             layout.template_node_socket(color)
422
423
424 def node_draw_tree_view(layout, context):
425     pass
426
427
428 if __name__ == "__main__":  # only for live edit.
429     bpy.utils.register_module(__name__)