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