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