6b407daaccb2470058768bffb72a94504c49609b
[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         layout = self.layout
170         layout.menu("NODE_MT_view")
171         layout.menu("NODE_MT_select")
172         layout.menu("NODE_MT_add")
173         layout.menu("NODE_MT_node")
174
175
176 class NODE_MT_add(bpy.types.Menu):
177     bl_space_type = 'NODE_EDITOR'
178     bl_label = "Add"
179
180     def draw(self, context):
181         layout = self.layout
182
183         layout.operator_context = 'INVOKE_DEFAULT'
184         props = layout.operator("node.add_search", text="Search...", icon='VIEWZOOM')
185         props.use_transform = True
186
187         layout.separator()
188
189         # actual node submenus are defined by draw functions from node categories
190         nodeitems_utils.draw_node_categories_menu(self, context)
191
192
193 class NODE_MT_view(Menu):
194     bl_label = "View"
195
196     def draw(self, context):
197         layout = self.layout
198
199         snode = context.space_data
200
201         layout.operator("node.properties", icon='MENU_PANEL')
202         layout.operator("node.toolbar", icon='MENU_PANEL')
203
204         layout.separator()
205
206         # Auto-offset nodes (called "insert_offset" in code)
207         layout.prop(snode, "use_insert_offset")
208
209         layout.separator()
210
211         layout.operator("view2d.zoom_in")
212         layout.operator("view2d.zoom_out")
213
214         layout.separator()
215
216         layout.operator("node.view_selected")
217         layout.operator("node.view_all")
218
219         if context.space_data.show_backdrop:
220             layout.separator()
221
222             layout.operator("node.backimage_move", text="Backdrop Move")
223             layout.operator("node.backimage_zoom", text="Backdrop Zoom In").factor = 1.2
224             layout.operator("node.backimage_zoom", text="Backdrop Zoom Out").factor = 1.0 / 1.2
225             layout.operator("node.backimage_fit", text="Fit Backdrop to Available Space")
226
227         layout.separator()
228
229         layout.menu("INFO_MT_area")
230
231
232 class NODE_MT_select(Menu):
233     bl_label = "Select"
234
235     def draw(self, context):
236         layout = self.layout
237
238         layout.operator("node.select_box").tweak = False
239         layout.operator("node.select_circle")
240
241         layout.separator()
242         layout.operator("node.select_all").action = 'TOGGLE'
243         layout.operator("node.select_all", text="Inverse").action = 'INVERT'
244         layout.operator("node.select_linked_from")
245         layout.operator("node.select_linked_to")
246
247         layout.separator()
248
249         layout.operator("node.select_grouped").extend = False
250         layout.operator("node.select_same_type_step", text="Activate Same Type Previous").prev = True
251         layout.operator("node.select_same_type_step", text="Activate Same Type Next").prev = False
252
253         layout.separator()
254
255         layout.operator("node.find_node")
256
257
258 class NODE_MT_node(Menu):
259     bl_label = "Node"
260
261     def draw(self, context):
262         layout = self.layout
263
264         layout.operator("transform.translate")
265         layout.operator("transform.rotate")
266         layout.operator("transform.resize")
267
268         layout.separator()
269         layout.operator("node.clipboard_copy", text="Copy")
270         layout.operator("node.clipboard_paste", text="Paste")
271         layout.operator("node.duplicate_move")
272         layout.operator("node.delete")
273         layout.operator("node.delete_reconnect")
274
275         layout.separator()
276
277         layout.operator("node.join", text="Join in New Frame")
278         layout.operator("node.detach", text="Remove from Frame")
279
280         layout.separator()
281
282         layout.operator("node.link_make").replace = False
283         layout.operator("node.link_make", text="Make and Replace Links").replace = True
284         layout.operator("node.links_cut")
285         layout.operator("node.links_detach")
286
287         layout.separator()
288
289         layout.operator("node.group_edit").exit = False
290         layout.operator("node.group_ungroup")
291         layout.operator("node.group_make")
292         layout.operator("node.group_insert")
293
294         layout.separator()
295
296         layout.operator("node.hide_toggle")
297         layout.operator("node.mute_toggle")
298         layout.operator("node.preview_toggle")
299         layout.operator("node.hide_socket_toggle")
300         layout.operator("node.options_toggle")
301         layout.operator("node.collapse_hide_unused_toggle")
302
303         layout.separator()
304
305         layout.operator("node.read_viewlayers")
306
307
308 class NODE_PT_node_color_presets(PresetMenu):
309     """Predefined node color"""
310     bl_label = "Color Presets"
311     preset_subdir = "node_color"
312     preset_operator = "script.execute_preset"
313     preset_add_operator = "node.node_color_preset_add"
314
315
316 class NODE_MT_node_color_specials(Menu):
317     bl_label = "Node Color Specials"
318
319     def draw(self, context):
320         layout = self.layout
321
322         layout.operator("node.node_copy_color", icon='COPY_ID')
323
324
325 class NODE_MT_specials(Menu):
326     bl_label = "Node Context Menu"
327
328     def draw(self, context):
329         layout = self.layout
330
331         # If nothing is selected
332         selected_nodes_len = len(context.selected_nodes)
333         if selected_nodes_len == 0:
334             layout.operator_context = 'INVOKE_DEFAULT'
335             layout.menu("NODE_MT_add")
336             layout.operator("node.clipboard_paste", text="Paste")
337             return
338
339         # If something is selected
340         layout.operator_context = 'INVOKE_DEFAULT'
341         layout.operator("node.duplicate_move")
342         layout.operator("node.delete")
343         layout.operator("node.clipboard_copy", text="Copy")
344         layout.operator("node.clipboard_paste", text="Paste")
345         layout.operator_context = 'EXEC_DEFAULT'
346
347         layout.operator("node.delete_reconnect")
348
349         if selected_nodes_len > 1:
350             layout.separator()
351
352             layout.operator("node.link_make").replace = False
353             layout.operator("node.link_make", text="Make and Replace Links").replace = True
354             layout.operator("node.links_detach")
355
356             layout.separator()
357
358             layout.operator("node.group_make", text="Group")
359
360         layout.operator("node.group_ungroup", text="Ungroup")
361         layout.operator("node.group_edit").exit = False
362
363         layout.separator()
364
365         layout.operator("node.hide_toggle")
366         layout.operator("node.mute_toggle")
367         layout.operator("node.preview_toggle")
368         layout.operator("node.hide_socket_toggle")
369         layout.operator("node.options_toggle")
370         layout.operator("node.collapse_hide_unused_toggle")
371
372
373 class NODE_PT_active_node_generic(Panel):
374     bl_space_type = 'NODE_EDITOR'
375     bl_region_type = 'UI'
376     bl_category = "Node"
377     bl_label = "Node"
378
379     @classmethod
380     def poll(cls, context):
381         return context.active_node is not None
382
383     def draw(self, context):
384         layout = self.layout
385         node = context.active_node
386
387         layout.prop(node, "name", icon='NODE')
388         layout.prop(node, "label", icon='NODE')
389
390
391 class NODE_PT_active_node_color(Panel):
392     bl_space_type = 'NODE_EDITOR'
393     bl_region_type = 'UI'
394     bl_category = "Node"
395     bl_label = "Color"
396     bl_options = {'DEFAULT_CLOSED'}
397     bl_parent_id = 'NODE_PT_active_node_generic'
398
399     @classmethod
400     def poll(cls, context):
401         return context.active_node is not None
402
403     def draw_header(self, context):
404         node = context.active_node
405         self.layout.prop(node, "use_custom_color", text="")
406
407     def draw_header_preset(self, context):
408         NODE_PT_node_color_presets.draw_panel_header(self.layout)
409
410     def draw(self, context):
411         layout = self.layout
412         node = context.active_node
413
414         layout.enabled = node.use_custom_color
415
416         row = layout.row()
417         row.prop(node, "color", text="")
418         row.menu("NODE_MT_node_color_specials", text="", icon='DOWNARROW_HLT')
419
420
421 class NODE_PT_active_node_properties(Panel):
422     bl_space_type = 'NODE_EDITOR'
423     bl_region_type = 'UI'
424     bl_category = "Node"
425     bl_label = "Properties"
426     bl_options = {'DEFAULT_CLOSED'}
427     bl_parent_id = 'NODE_PT_active_node_generic'
428
429     @classmethod
430     def poll(cls, context):
431         return context.active_node is not None
432
433     def draw(self, context):
434         layout = self.layout
435         node = context.active_node
436         # set "node" context pointer for the panel layout
437         layout.context_pointer_set("node", node)
438
439         if hasattr(node, "draw_buttons_ext"):
440             node.draw_buttons_ext(context, layout)
441         elif hasattr(node, "draw_buttons"):
442             node.draw_buttons(context, layout)
443
444         # XXX this could be filtered further to exclude socket types which don't have meaningful input values (e.g. cycles shader)
445         value_inputs = [socket for socket in node.inputs if socket.enabled and not socket.is_linked]
446         if value_inputs:
447             layout.separator()
448             layout.label(text="Inputs:")
449             for socket in value_inputs:
450                 row = layout.row()
451                 socket.draw(context, row, node, iface_(socket.name, socket.bl_rna.translation_context))
452
453
454 # Node Backdrop options
455 class NODE_PT_backdrop(Panel):
456     bl_space_type = 'NODE_EDITOR'
457     bl_region_type = 'UI'
458     bl_category = "Node"
459     bl_label = "Backdrop"
460
461     @classmethod
462     def poll(cls, context):
463         snode = context.space_data
464         return snode.tree_type == 'CompositorNodeTree'
465
466     def draw_header(self, context):
467         snode = context.space_data
468         self.layout.prop(snode, "show_backdrop", text="")
469
470     def draw(self, context):
471         layout = self.layout
472         layout.use_property_split = True
473
474         snode = context.space_data
475         layout.active = snode.show_backdrop
476
477         col = layout.column()
478
479         col.prop(snode, "backdrop_channels", text="Channels")
480         col.prop(snode, "backdrop_zoom", text="Zoom")
481
482         col.prop(snode, "backdrop_offset", text="Offset")
483
484         col.separator()
485
486         col.operator("node.backimage_move", text="Move")
487         col.operator("node.backimage_fit", text="Fit")
488
489
490 class NODE_PT_quality(bpy.types.Panel):
491     bl_space_type = 'NODE_EDITOR'
492     bl_region_type = 'UI'
493     bl_category = "Node"
494     bl_label = "Performance"
495
496     @classmethod
497     def poll(cls, context):
498         snode = context.space_data
499         return snode.tree_type == 'CompositorNodeTree' and snode.node_tree is not None
500
501     def draw(self, context):
502         layout = self.layout
503         layout.use_property_split = True
504
505         snode = context.space_data
506         tree = snode.node_tree
507
508         col = layout.column()
509         col.prop(tree, "render_quality", text="Render")
510         col.prop(tree, "edit_quality", text="Edit")
511         col.prop(tree, "chunk_size")
512
513         col = layout.column()
514         col.prop(tree, "use_opencl")
515         col.prop(tree, "use_groupnode_buffer")
516         col.prop(tree, "use_two_pass")
517         col.prop(tree, "use_viewer_border")
518
519
520 class NODE_UL_interface_sockets(bpy.types.UIList):
521     def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
522         socket = item
523         color = socket.draw_color(context)
524
525         if self.layout_type in {'DEFAULT', 'COMPACT'}:
526             row = layout.row(align=True)
527
528             # inputs get icon on the left
529             if not socket.is_output:
530                 row.template_node_socket(color=color)
531
532             row.prop(socket, "name", text="", emboss=False, icon_value=icon)
533
534             # outputs get icon on the right
535             if socket.is_output:
536                 row.template_node_socket(color=color)
537
538         elif self.layout_type == 'GRID':
539             layout.alignment = 'CENTER'
540             layout.template_node_socket(color=color)
541
542
543 # Grease Pencil properties
544 class NODE_PT_grease_pencil(AnnotationDataPanel, Panel):
545     bl_space_type = 'NODE_EDITOR'
546     bl_region_type = 'UI'
547     bl_category = "Node"
548     bl_options = {'DEFAULT_CLOSED'}
549
550     # NOTE: this is just a wrapper around the generic GP Panel
551
552     @classmethod
553     def poll(cls, context):
554         snode = context.space_data
555         return snode is not None and snode.node_tree is not None
556
557
558 class NODE_PT_grease_pencil_tools(GreasePencilToolsPanel, Panel):
559     bl_space_type = 'NODE_EDITOR'
560     bl_region_type = 'UI'
561     bl_category = "Node"
562     bl_options = {'DEFAULT_CLOSED'}
563
564     # NOTE: this is just a wrapper around the generic GP tools panel
565     # It contains access to some essential tools usually found only in
566     # toolbar, but which may not necessarily be open
567
568
569 class EEVEE_NODE_PT_material_settings(Panel):
570     bl_space_type = 'NODE_EDITOR'
571     bl_region_type = 'UI'
572     bl_category = "Node"
573     bl_label = "Settings"
574     COMPAT_ENGINES = {'BLENDER_EEVEE'}
575
576     @classmethod
577     def poll(cls, context):
578         snode = context.space_data
579         return (context.engine in cls.COMPAT_ENGINES) and \
580                snode.tree_type == 'ShaderNodeTree' and snode.id and \
581                snode.id.bl_rna.identifier == 'Material'
582
583     def draw(self, context):
584         material = context.space_data.id
585         EEVEE_MATERIAL_PT_settings.draw_shared(self, material)
586
587
588 class NODE_PT_material_viewport(Panel):
589     bl_space_type = 'NODE_EDITOR'
590     bl_region_type = 'UI'
591     bl_category = "Node"
592     bl_label = "Viewport Display"
593     bl_options = {'DEFAULT_CLOSED'}
594
595     @classmethod
596     def poll(cls, context):
597         snode = context.space_data
598         return snode.tree_type == 'ShaderNodeTree' and snode.id and \
599                snode.id.bl_rna.identifier == 'Material'
600
601     def draw(self, context):
602         material = context.space_data.id
603         MATERIAL_PT_viewport.draw_shared(self, material)
604
605
606 def node_draw_tree_view(layout, context):
607     pass
608
609
610 classes = (
611     NODE_HT_header,
612     NODE_MT_editor_menus,
613     NODE_MT_add,
614     NODE_MT_view,
615     NODE_MT_select,
616     NODE_MT_node,
617     NODE_PT_node_color_presets,
618     NODE_MT_node_color_specials,
619     NODE_MT_specials,
620     NODE_PT_active_node_generic,
621     NODE_PT_active_node_color,
622     NODE_PT_active_node_properties,
623     NODE_PT_backdrop,
624     NODE_PT_quality,
625     NODE_UL_interface_sockets,
626     NODE_PT_grease_pencil,
627     NODE_PT_grease_pencil_tools,
628     EEVEE_NODE_PT_material_settings,
629     NODE_PT_material_viewport,
630 )
631
632
633 if __name__ == "__main__":  # only for live edit.
634     from bpy.utils import register_class
635     for cls in classes:
636         register_class(cls)