Cleanup: pep8
[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 import nodeitems_utils
22 from bpy.types import Header, Menu, Panel
23 from bpy.app.translations import pgettext_iface as iface_
24 from bl_ui.properties_grease_pencil_common import (
25         GreasePencilDrawingToolsPanel,
26         GreasePencilStrokeEditPanel,
27         GreasePencilStrokeSculptPanel,
28         GreasePencilDataPanel,
29         GreasePencilToolsPanel,
30         )
31
32
33 class NODE_HT_header(Header):
34     bl_space_type = 'NODE_EDITOR'
35
36     def draw(self, context):
37         layout = self.layout
38
39         scene = context.scene
40         snode = context.space_data
41         snode_id = snode.id
42         id_from = snode.id_from
43         toolsettings = context.tool_settings
44
45         row = layout.row(align=True)
46         row.template_header()
47
48         NODE_MT_editor_menus.draw_collapsible(context, layout)
49
50         layout.prop(snode, "tree_type", text="", expand=True)
51
52         if snode.tree_type == 'ShaderNodeTree':
53             if scene.render.use_shading_nodes:
54                 layout.prop(snode, "shader_type", text="", expand=True)
55
56             ob = context.object
57             if (not scene.render.use_shading_nodes or snode.shader_type == 'OBJECT') and ob:
58                 row = layout.row()
59                 # disable material slot buttons when pinned, cannot find correct slot within id_from (#36589)
60                 row.enabled = not snode.pin
61                 # Show material.new when no active ID/slot exists
62                 if not id_from and ob.type in {'MESH', 'CURVE', 'SURFACE', 'FONT', 'METABALL'}:
63                     row.template_ID(ob, "active_material", new="material.new")
64                 # Material ID, but not for Lamps
65                 if id_from and ob.type != 'LAMP':
66                     row.template_ID(id_from, "active_material", new="material.new")
67
68                 # Don't show "Use Nodes" Button when Engine is BI for Lamps
69                 if snode_id and not (scene.render.use_shading_nodes == 0 and ob.type == 'LAMP'):
70                     layout.prop(snode_id, "use_nodes")
71
72             if scene.render.use_shading_nodes and snode.shader_type == 'WORLD':
73                 row = layout.row()
74                 row.enabled = not snode.pin
75                 row.template_ID(scene, "world", new="world.new")
76                 if snode_id:
77                     row.prop(snode_id, "use_nodes")
78
79             if scene.render.use_shading_nodes and snode.shader_type == 'LINESTYLE':
80                 rl = context.scene.render.layers.active
81                 lineset = rl.freestyle_settings.linesets.active
82                 if lineset is not None:
83                     row = layout.row()
84                     row.enabled = not snode.pin
85                     row.template_ID(lineset, "linestyle", new="scene.freestyle_linestyle_new")
86                     if snode_id:
87                         row.prop(snode_id, "use_nodes")
88
89         elif snode.tree_type == 'TextureNodeTree':
90             layout.prop(snode, "texture_type", text="", expand=True)
91
92             if id_from:
93                 if snode.texture_type == 'BRUSH':
94                     layout.template_ID(id_from, "texture", new="texture.new")
95                 else:
96                     layout.template_ID(id_from, "active_texture", new="texture.new")
97             if snode_id:
98                 layout.prop(snode_id, "use_nodes")
99
100         elif snode.tree_type == 'CompositorNodeTree':
101             if snode_id:
102                 layout.prop(snode_id, "use_nodes")
103                 layout.prop(snode_id.render, "use_free_unused_nodes", text="Free Unused")
104             layout.prop(snode, "show_backdrop")
105             if snode.show_backdrop:
106                 row = layout.row(align=True)
107                 row.prop(snode, "backdrop_channels", text="", expand=True)
108             layout.prop(snode, "use_auto_render")
109
110         else:
111             # Custom node tree is edited as independent ID block
112             layout.template_ID(snode, "node_tree", new="node.new_node_tree")
113
114         layout.prop(snode, "pin", text="")
115         layout.operator("node.tree_path_parent", text="", icon='FILE_PARENT')
116
117         layout.separator()
118
119         # Auto-offset nodes (called "insert_offset" in code)
120         layout.prop(snode, "use_insert_offset", text="")
121
122         # Snap
123         row = layout.row(align=True)
124         row.prop(toolsettings, "use_snap", text="")
125         row.prop(toolsettings, "snap_node_element", icon_only=True)
126         if toolsettings.snap_node_element != 'GRID':
127             row.prop(toolsettings, "snap_target", text="")
128
129         row = layout.row(align=True)
130         row.operator("node.clipboard_copy", text="", icon='COPYDOWN')
131         row.operator("node.clipboard_paste", text="", icon='PASTEDOWN')
132
133         layout.template_running_jobs()
134
135
136 class NODE_MT_editor_menus(Menu):
137     bl_idname = "NODE_MT_editor_menus"
138     bl_label = ""
139
140     def draw(self, context):
141         self.draw_menus(self.layout, context)
142
143     @staticmethod
144     def draw_menus(layout, context):
145         layout.menu("NODE_MT_view")
146         layout.menu("NODE_MT_select")
147         layout.menu("NODE_MT_add")
148         layout.menu("NODE_MT_node")
149
150
151 class NODE_MT_add(bpy.types.Menu):
152     bl_space_type = 'NODE_EDITOR'
153     bl_label = "Add"
154
155     def draw(self, context):
156         layout = self.layout
157
158         layout.operator_context = 'INVOKE_DEFAULT'
159         props = layout.operator("node.add_search", text="Search ...")
160         props.use_transform = True
161
162         # actual node submenus are defined by draw functions from node categories
163         nodeitems_utils.draw_node_categories_menu(self, context)
164
165
166 class NODE_MT_view(Menu):
167     bl_label = "View"
168
169     def draw(self, context):
170         layout = self.layout
171
172         layout.operator("node.properties", icon='MENU_PANEL')
173         layout.operator("node.toolbar", icon='MENU_PANEL')
174
175         layout.separator()
176
177         layout.operator("view2d.zoom_in")
178         layout.operator("view2d.zoom_out")
179
180         layout.separator()
181
182         layout.operator("node.view_selected")
183         layout.operator("node.view_all")
184
185         if context.space_data.show_backdrop:
186             layout.separator()
187
188             layout.operator("node.backimage_move", text="Backdrop move")
189             layout.operator("node.backimage_zoom", text="Backdrop zoom in").factor = 1.2
190             layout.operator("node.backimage_zoom", text="Backdrop zoom out").factor = 0.83333
191             layout.operator("node.backimage_fit", text="Fit backdrop to available space")
192
193         layout.separator()
194
195         layout.operator("screen.area_dupli")
196         layout.operator("screen.screen_full_area", text="Toggle Maximize Area")
197         layout.operator("screen.screen_full_area").use_hide_panels = True
198
199
200 class NODE_MT_select(Menu):
201     bl_label = "Select"
202
203     def draw(self, context):
204         layout = self.layout
205
206         layout.operator("node.select_border").tweak = False
207         layout.operator("node.select_circle")
208
209         layout.separator()
210         layout.operator("node.select_all").action = 'TOGGLE'
211         layout.operator("node.select_all", text="Inverse").action = 'INVERT'
212         layout.operator("node.select_linked_from")
213         layout.operator("node.select_linked_to")
214
215         layout.separator()
216
217         layout.operator("node.select_grouped").extend = False
218         layout.operator("node.select_same_type_step", text="Activate Same Type Previous").prev = True
219         layout.operator("node.select_same_type_step", text="Activate Same Type Next").prev = False
220
221         layout.separator()
222
223         layout.operator("node.find_node")
224
225
226 class NODE_MT_node(Menu):
227     bl_label = "Node"
228
229     def draw(self, context):
230         layout = self.layout
231
232         layout.operator("transform.translate")
233         layout.operator("transform.rotate")
234         layout.operator("transform.resize")
235
236         layout.separator()
237
238         layout.operator("node.duplicate_move")
239         layout.operator("node.delete")
240         layout.operator("node.delete_reconnect")
241
242         layout.separator()
243
244         layout.operator("node.join", text="Join in new Frame")
245         layout.operator("node.detach", text="Remove from Frame")
246
247         layout.separator()
248
249         layout.operator("node.link_make").replace = False
250         layout.operator("node.link_make", text="Make and Replace Links").replace = True
251         layout.operator("node.links_cut")
252         layout.operator("node.links_detach")
253
254         layout.separator()
255
256         layout.operator("node.group_edit").exit = False
257         layout.operator("node.group_ungroup")
258         layout.operator("node.group_make")
259         layout.operator("node.group_insert")
260
261         layout.separator()
262
263         layout.operator("node.hide_toggle")
264         layout.operator("node.mute_toggle")
265         layout.operator("node.preview_toggle")
266         layout.operator("node.hide_socket_toggle")
267         layout.operator("node.options_toggle")
268         layout.operator("node.collapse_hide_unused_toggle")
269
270         layout.separator()
271
272         layout.operator("node.read_renderlayers")
273         layout.operator("node.read_fullsamplelayers")
274
275
276 class NODE_MT_node_color_presets(Menu):
277     """Predefined node color"""
278     bl_label = "Color Presets"
279     preset_subdir = "node_color"
280     preset_operator = "script.execute_preset"
281     draw = Menu.draw_preset
282
283
284 class NODE_MT_node_color_specials(Menu):
285     bl_label = "Node Color Specials"
286
287     def draw(self, context):
288         layout = self.layout
289
290         layout.operator("node.node_copy_color", icon='COPY_ID')
291
292
293 class NODE_PT_active_node_generic(Panel):
294     bl_space_type = 'NODE_EDITOR'
295     bl_region_type = 'UI'
296     bl_label = "Node"
297 #    bl_options = {'HIDE_HEADER'}
298
299     @classmethod
300     def poll(cls, context):
301         return context.active_node is not None
302
303     def draw(self, context):
304         layout = self.layout
305         node = context.active_node
306
307         layout.prop(node, "name", icon='NODE')
308         layout.prop(node, "label", icon='NODE')
309
310
311 class NODE_PT_active_node_color(Panel):
312     bl_space_type = 'NODE_EDITOR'
313     bl_region_type = 'UI'
314     bl_label = "Color"
315     bl_options = {'DEFAULT_CLOSED'}
316
317     @classmethod
318     def poll(cls, context):
319         return context.active_node is not None
320
321     def draw_header(self, context):
322         node = context.active_node
323         self.layout.prop(node, "use_custom_color", text="")
324
325     def draw(self, context):
326         layout = self.layout
327         node = context.active_node
328
329         layout.enabled = node.use_custom_color
330
331         row = layout.row()
332         col = row.column()
333         col.menu("NODE_MT_node_color_presets")
334         col.prop(node, "color", text="")
335         col = row.column(align=True)
336         col.operator("node.node_color_preset_add", text="", icon='ZOOMIN').remove_active = False
337         col.operator("node.node_color_preset_add", text="", icon='ZOOMOUT').remove_active = True
338         col.menu("NODE_MT_node_color_specials", text="", icon='DOWNARROW_HLT')
339
340
341 class NODE_PT_active_node_properties(Panel):
342     bl_space_type = 'NODE_EDITOR'
343     bl_region_type = 'UI'
344     bl_label = "Properties"
345
346     @classmethod
347     def poll(cls, context):
348         return context.active_node is not None
349
350     def draw(self, context):
351         layout = self.layout
352         node = context.active_node
353         # set "node" context pointer for the panel layout
354         layout.context_pointer_set("node", node)
355
356         if hasattr(node, "draw_buttons_ext"):
357             node.draw_buttons_ext(context, layout)
358         elif hasattr(node, "draw_buttons"):
359             node.draw_buttons(context, layout)
360
361         # XXX this could be filtered further to exclude socket types which don't have meaningful input values (e.g. cycles shader)
362         value_inputs = [socket for socket in node.inputs if socket.enabled and not socket.is_linked]
363         if value_inputs:
364             layout.separator()
365             layout.label("Inputs:")
366             for socket in value_inputs:
367                 row = layout.row()
368                 socket.draw(context, row, node, iface_(socket.name, socket.bl_rna.translation_context))
369
370
371 # Node Backdrop options
372 class NODE_PT_backdrop(Panel):
373     bl_space_type = 'NODE_EDITOR'
374     bl_region_type = 'UI'
375     bl_label = "Backdrop"
376
377     @classmethod
378     def poll(cls, context):
379         snode = context.space_data
380         return snode.tree_type == 'CompositorNodeTree'
381
382     def draw_header(self, context):
383         snode = context.space_data
384         self.layout.prop(snode, "show_backdrop", text="")
385
386     def draw(self, context):
387         layout = self.layout
388
389         snode = context.space_data
390         layout.active = snode.show_backdrop
391         layout.prop(snode, "backdrop_channels", text="")
392         layout.prop(snode, "backdrop_zoom", text="Zoom")
393
394         col = layout.column(align=True)
395         col.label(text="Offset:")
396         col.prop(snode, "backdrop_x", text="X")
397         col.prop(snode, "backdrop_y", text="Y")
398         col.operator("node.backimage_move", text="Move")
399
400         layout.operator("node.backimage_fit", text="Fit")
401
402
403 class NODE_PT_quality(bpy.types.Panel):
404     bl_space_type = 'NODE_EDITOR'
405     bl_region_type = 'UI'
406     bl_label = "Performance"
407
408     @classmethod
409     def poll(cls, context):
410         snode = context.space_data
411         return snode.tree_type == 'CompositorNodeTree' and snode.node_tree is not None
412
413     def draw(self, context):
414         layout = self.layout
415
416         snode = context.space_data
417         tree = snode.node_tree
418
419         col = layout.column()
420         col.prop(tree, "render_quality", text="Render")
421         col.prop(tree, "edit_quality", text="Edit")
422         col.prop(tree, "chunk_size")
423
424         col = layout.column()
425         col.prop(tree, "use_opencl")
426         col.prop(tree, "use_groupnode_buffer")
427         col.prop(tree, "use_two_pass")
428         col.prop(tree, "use_viewer_border")
429         col.prop(snode, "show_highlight")
430
431
432 class NODE_UL_interface_sockets(bpy.types.UIList):
433     def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
434         socket = item
435         color = socket.draw_color(context)
436
437         if self.layout_type in {'DEFAULT', 'COMPACT'}:
438             row = layout.row(align=True)
439
440             # inputs get icon on the left
441             if not socket.is_output:
442                 row.template_node_socket(color)
443
444             row.prop(socket, "name", text="", emboss=False, icon_value=icon)
445
446             # outputs get icon on the right
447             if socket.is_output:
448                 row.template_node_socket(color)
449
450         elif self.layout_type == 'GRID':
451             layout.alignment = 'CENTER'
452             layout.template_node_socket(color)
453
454
455 # Grease Pencil properties
456 class NODE_PT_grease_pencil(GreasePencilDataPanel, Panel):
457     bl_space_type = 'NODE_EDITOR'
458     bl_region_type = 'UI'
459
460     # NOTE: this is just a wrapper around the generic GP Panel
461
462     @classmethod
463     def poll(cls, context):
464         snode = context.space_data
465         return snode is not None and snode.node_tree is not None
466
467
468 class NODE_PT_grease_pencil_tools(GreasePencilToolsPanel, Panel):
469     bl_space_type = 'NODE_EDITOR'
470     bl_region_type = 'UI'
471     bl_options = {'DEFAULT_CLOSED'}
472
473     # NOTE: this is just a wrapper around the generic GP tools panel
474     # It contains access to some essential tools usually found only in
475     # toolbar, but which may not necessarily be open
476
477
478 # Tool Shelf ------------------
479
480
481 # Grease Pencil drawing tools
482 class NODE_PT_tools_grease_pencil_draw(GreasePencilDrawingToolsPanel, Panel):
483     bl_space_type = 'NODE_EDITOR'
484     bl_region_type = 'TOOLS'
485
486
487 # Grease Pencil stroke editing tools
488 class NODE_PT_tools_grease_pencil_edit(GreasePencilStrokeEditPanel, Panel):
489     bl_space_type = 'NODE_EDITOR'
490     bl_region_type = 'TOOLS'
491
492
493 # Grease Pencil stroke sculpting tools
494 class NODE_PT_tools_grease_pencil_sculpt(GreasePencilStrokeSculptPanel, Panel):
495     bl_space_type = 'NODE_EDITOR'
496     bl_region_type = 'TOOLS'
497
498 # -----------------------------
499
500
501 def node_draw_tree_view(layout, context):
502     pass
503
504
505 if __name__ == "__main__":  # only for live edit.
506     bpy.utils.register_module(__name__)