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