UI: Remember ID-Filter in-between File Browser calls
[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 bpy.app.translations import contexts as i18n_contexts
25 from bl_ui.utils import PresetPanel
26 from bl_ui.properties_grease_pencil_common import (
27     AnnotationDataPanel,
28 )
29 from bl_ui.space_toolsystem_common import (
30     ToolActivePanelHelper,
31 )
32 from bl_ui.properties_material import (
33     EEVEE_MATERIAL_PT_settings,
34     MATERIAL_PT_viewport
35 )
36 from bl_ui.properties_world import (
37     WORLD_PT_viewport_display
38 )
39 from bl_ui.properties_data_light import (
40     DATA_PT_light,
41     DATA_PT_EEVEE_light,
42 )
43
44
45 class NODE_HT_header(Header):
46     bl_space_type = 'NODE_EDITOR'
47
48     def draw(self, context):
49         layout = self.layout
50
51         scene = context.scene
52         snode = context.space_data
53         snode_id = snode.id
54         id_from = snode.id_from
55         tool_settings = context.tool_settings
56         is_compositor = snode.tree_type == 'CompositorNodeTree'
57
58         layout.template_header()
59
60         # Now expanded via the 'ui_type'
61         # layout.prop(snode, "tree_type", text="")
62
63         if snode.tree_type == 'ShaderNodeTree':
64             layout.prop(snode, "shader_type", text="")
65
66             ob = context.object
67             if snode.shader_type == 'OBJECT' and ob:
68                 ob_type = ob.type
69
70                 NODE_MT_editor_menus.draw_collapsible(context, layout)
71
72                 # No shader nodes for Eevee lights
73                 if snode_id and not (context.engine == 'BLENDER_EEVEE' and ob_type == 'LIGHT'):
74                     row = layout.row()
75                     row.prop(snode_id, "use_nodes")
76
77                 layout.separator_spacer()
78
79                 types_that_support_material = {'MESH', 'CURVE', 'SURFACE', 'FONT', 'META', 'GPENCIL'}
80                 # disable material slot buttons when pinned, cannot find correct slot within id_from (#36589)
81                 # disable also when the selected object does not support materials
82                 has_material_slots = not snode.pin and ob_type in types_that_support_material
83
84                 if ob_type != 'LIGHT':
85                     row = layout.row()
86                     row.enabled = has_material_slots
87                     row.ui_units_x = 4
88                     row.popover(panel="NODE_PT_material_slots")
89
90                 row = layout.row()
91                 row.enabled = has_material_slots
92
93                 # Show material.new when no active ID/slot exists
94                 if not id_from and ob_type in types_that_support_material:
95                     row.template_ID(ob, "active_material", new="material.new")
96                 # Material ID, but not for Lights
97                 if id_from and ob_type != 'LIGHT':
98                     row.template_ID(id_from, "active_material", new="material.new")
99
100             if snode.shader_type == 'WORLD':
101                 NODE_MT_editor_menus.draw_collapsible(context, layout)
102
103                 if snode_id:
104                     row = layout.row()
105                     row.prop(snode_id, "use_nodes")
106
107                 layout.separator_spacer()
108
109                 row = layout.row()
110                 row.enabled = not snode.pin
111                 row.template_ID(scene, "world", new="world.new")
112
113             if snode.shader_type == 'LINESTYLE':
114                 view_layer = context.view_layer
115                 lineset = view_layer.freestyle_settings.linesets.active
116
117                 if lineset is not None:
118                     NODE_MT_editor_menus.draw_collapsible(context, layout)
119
120                     if snode_id:
121                         row = layout.row()
122                         row.prop(snode_id, "use_nodes")
123
124                     layout.separator_spacer()
125
126                     row = layout.row()
127                     row.enabled = not snode.pin
128                     row.template_ID(lineset, "linestyle", new="scene.freestyle_linestyle_new")
129
130         elif snode.tree_type == 'TextureNodeTree':
131             layout.prop(snode, "texture_type", text="")
132
133             NODE_MT_editor_menus.draw_collapsible(context, layout)
134
135             if snode_id:
136                 layout.prop(snode_id, "use_nodes")
137
138             layout.separator_spacer()
139
140             if id_from:
141                 if snode.texture_type == 'BRUSH':
142                     layout.template_ID(id_from, "texture", new="texture.new")
143                 else:
144                     layout.template_ID(id_from, "active_texture", new="texture.new")
145
146         elif snode.tree_type == 'CompositorNodeTree':
147
148             NODE_MT_editor_menus.draw_collapsible(context, layout)
149
150             if snode_id:
151                 layout.prop(snode_id, "use_nodes")
152
153         else:
154             # Custom node tree is edited as independent ID block
155             NODE_MT_editor_menus.draw_collapsible(context, layout)
156
157             layout.separator_spacer()
158
159             layout.template_ID(snode, "node_tree", new="node.new_node_tree")
160
161         # Put pin next to ID block
162         if not is_compositor:
163             layout.prop(snode, "pin", text="", emboss=False)
164
165         layout.separator_spacer()
166
167         # Put pin on the right for Compositing
168         if is_compositor:
169             layout.prop(snode, "pin", text="", emboss=False)
170
171         layout.operator("node.tree_path_parent", text="", icon='FILE_PARENT')
172
173         # Backdrop
174         if is_compositor:
175             row = layout.row(align=True)
176             row.prop(snode, "show_backdrop", toggle=True)
177             sub = row.row(align=True)
178             sub.active = snode.show_backdrop
179             sub.prop(snode, "backdrop_channels", icon_only=True, text="", expand=True)
180
181         # Snap
182         row = layout.row(align=True)
183         row.prop(tool_settings, "use_snap", text="")
184         row.prop(tool_settings, "snap_node_element", icon_only=True)
185         if tool_settings.snap_node_element != 'GRID':
186             row.prop(tool_settings, "snap_target", text="")
187
188
189 class NODE_MT_editor_menus(Menu):
190     bl_idname = "NODE_MT_editor_menus"
191     bl_label = ""
192
193     def draw(self, _context):
194         layout = self.layout
195         layout.menu("NODE_MT_view")
196         layout.menu("NODE_MT_select")
197         layout.menu("NODE_MT_add")
198         layout.menu("NODE_MT_node")
199
200
201 class NODE_MT_add(bpy.types.Menu):
202     bl_space_type = 'NODE_EDITOR'
203     bl_label = "Add"
204     bl_translation_context = i18n_contexts.operator_default
205
206     def draw(self, context):
207         layout = self.layout
208
209         layout.operator_context = 'INVOKE_DEFAULT'
210         props = layout.operator("node.add_search", text="Search...", icon='VIEWZOOM')
211         props.use_transform = True
212
213         layout.separator()
214
215         # actual node submenus are defined by draw functions from node categories
216         nodeitems_utils.draw_node_categories_menu(self, context)
217
218
219 class NODE_MT_view(Menu):
220     bl_label = "View"
221
222     def draw(self, context):
223         layout = self.layout
224
225         snode = context.space_data
226
227         layout.prop(snode, "show_region_toolbar")
228         layout.prop(snode, "show_region_ui")
229
230         layout.separator()
231
232         # Auto-offset nodes (called "insert_offset" in code)
233         layout.prop(snode, "use_insert_offset")
234
235         layout.separator()
236
237         layout.operator("view2d.zoom_in")
238         layout.operator("view2d.zoom_out")
239
240         layout.separator()
241
242         layout.operator("node.view_selected")
243         layout.operator("node.view_all")
244
245         if context.space_data.show_backdrop:
246             layout.separator()
247
248             layout.operator("node.backimage_move", text="Backdrop Move")
249             layout.operator("node.backimage_zoom", text="Backdrop Zoom In").factor = 1.2
250             layout.operator("node.backimage_zoom", text="Backdrop Zoom Out").factor = 1.0 / 1.2
251             layout.operator("node.backimage_fit", text="Fit Backdrop to Available Space")
252
253         layout.separator()
254
255         layout.menu("INFO_MT_area")
256
257
258 class NODE_MT_select(Menu):
259     bl_label = "Select"
260
261     def draw(self, _context):
262         layout = self.layout
263
264         layout.operator("node.select_box").tweak = False
265         layout.operator("node.select_circle")
266
267         layout.separator()
268         layout.operator("node.select_all").action = 'TOGGLE'
269         layout.operator("node.select_all", text="Inverse").action = 'INVERT'
270         layout.operator("node.select_linked_from")
271         layout.operator("node.select_linked_to")
272
273         layout.separator()
274
275         layout.operator("node.select_grouped").extend = False
276         layout.operator("node.select_same_type_step", text="Activate Same Type Previous").prev = True
277         layout.operator("node.select_same_type_step", text="Activate Same Type Next").prev = False
278
279         layout.separator()
280
281         layout.operator("node.find_node")
282
283
284 class NODE_MT_node(Menu):
285     bl_label = "Node"
286
287     def draw(self, _context):
288         layout = self.layout
289
290         layout.operator("transform.translate")
291         layout.operator("transform.rotate")
292         layout.operator("transform.resize")
293
294         layout.separator()
295         layout.operator("node.clipboard_copy", text="Copy")
296         layout.operator("node.clipboard_paste", text="Paste")
297         layout.operator("node.duplicate_move")
298         layout.operator("node.delete")
299         layout.operator("node.delete_reconnect")
300
301         layout.separator()
302
303         layout.operator("node.join", text="Join in New Frame")
304         layout.operator("node.detach", text="Remove from Frame")
305
306         layout.separator()
307
308         layout.operator("node.link_make").replace = False
309         layout.operator("node.link_make", text="Make and Replace Links").replace = True
310         layout.operator("node.links_cut")
311         layout.operator("node.links_detach")
312
313         layout.separator()
314
315         layout.operator("node.group_edit").exit = False
316         layout.operator("node.group_ungroup")
317         layout.operator("node.group_make")
318         layout.operator("node.group_insert")
319
320         layout.separator()
321
322         layout.operator("node.hide_toggle")
323         layout.operator("node.mute_toggle")
324         layout.operator("node.preview_toggle")
325         layout.operator("node.hide_socket_toggle")
326         layout.operator("node.options_toggle")
327         layout.operator("node.collapse_hide_unused_toggle")
328
329         layout.separator()
330
331         layout.operator("node.read_viewlayers")
332
333
334 class NODE_PT_active_tool(ToolActivePanelHelper, Panel):
335     bl_space_type = 'NODE_EDITOR'
336     bl_region_type = 'UI'
337     bl_category = "Tool"
338
339
340 class NODE_PT_material_slots(Panel):
341     bl_space_type = 'NODE_EDITOR'
342     bl_region_type = 'HEADER'
343     bl_label = "Slot"
344     bl_ui_units_x = 12
345
346     def draw_header(self, context):
347         ob = context.object
348         self.bl_label = (
349             "Slot " + str(ob.active_material_index + 1) if ob.material_slots else
350             "Slot"
351         )
352
353     # Duplicate part of 'EEVEE_MATERIAL_PT_context_material'.
354     def draw(self, context):
355         layout = self.layout
356         row = layout.row()
357         col = row.column()
358
359         ob = context.object
360         col.template_list("MATERIAL_UL_matslots", "", ob, "material_slots", ob, "active_material_index")
361
362         col = row.column(align=True)
363         col.operator("object.material_slot_add", icon='ADD', text="")
364         col.operator("object.material_slot_remove", icon='REMOVE', text="")
365
366         col.separator()
367
368         col.menu("MATERIAL_MT_context_menu", icon='DOWNARROW_HLT', text="")
369
370         if len(ob.material_slots) > 1:
371             col.separator()
372
373             col.operator("object.material_slot_move", icon='TRIA_UP', text="").direction = 'UP'
374             col.operator("object.material_slot_move", icon='TRIA_DOWN', text="").direction = 'DOWN'
375
376         if ob.mode == 'EDIT':
377             row = layout.row(align=True)
378             row.operator("object.material_slot_assign", text="Assign")
379             row.operator("object.material_slot_select", text="Select")
380             row.operator("object.material_slot_deselect", text="Deselect")
381
382
383 class NODE_PT_node_color_presets(PresetPanel, Panel):
384     """Predefined node color"""
385     bl_label = "Color Presets"
386     preset_subdir = "node_color"
387     preset_operator = "script.execute_preset"
388     preset_add_operator = "node.node_color_preset_add"
389
390
391 class NODE_MT_node_color_context_menu(Menu):
392     bl_label = "Node Color Specials"
393
394     def draw(self, _context):
395         layout = self.layout
396
397         layout.operator("node.node_copy_color", icon='COPY_ID')
398
399
400 class NODE_MT_context_menu(Menu):
401     bl_label = "Node Context Menu"
402
403     def draw(self, context):
404         layout = self.layout
405
406         selected_nodes_len = len(context.selected_nodes)
407
408         # If nothing is selected
409         # (disabled for now until it can be made more useful).
410         '''
411         if selected_nodes_len == 0:
412             layout.operator_context = 'INVOKE_DEFAULT'
413             layout.menu("NODE_MT_add")
414             layout.operator("node.clipboard_paste", text="Paste")
415             return
416         '''
417
418         # If something is selected
419         layout.operator_context = 'INVOKE_DEFAULT'
420         layout.operator("node.duplicate_move")
421         props = layout.operator("wm.call_panel", text="Rename...")
422         props.name = "TOPBAR_PT_name"
423         props.keep_open = False
424         layout.operator("node.delete")
425         layout.operator("node.clipboard_copy", text="Copy")
426         layout.operator("node.clipboard_paste", text="Paste")
427         layout.operator_context = 'EXEC_DEFAULT'
428
429         layout.operator("node.delete_reconnect")
430
431         if selected_nodes_len > 1:
432             layout.separator()
433
434             layout.operator("node.link_make").replace = False
435             layout.operator("node.link_make", text="Make and Replace Links").replace = True
436             layout.operator("node.links_detach")
437
438             layout.separator()
439
440             layout.operator("node.group_make", text="Group")
441
442         layout.operator("node.group_ungroup", text="Ungroup")
443         layout.operator("node.group_edit").exit = False
444
445         layout.separator()
446
447         layout.operator("node.hide_toggle")
448         layout.operator("node.mute_toggle")
449         layout.operator("node.preview_toggle")
450         layout.operator("node.hide_socket_toggle")
451         layout.operator("node.options_toggle")
452         layout.operator("node.collapse_hide_unused_toggle")
453
454
455 class NODE_PT_active_node_generic(Panel):
456     bl_space_type = 'NODE_EDITOR'
457     bl_region_type = 'UI'
458     bl_category = "Item"
459     bl_label = "Node"
460
461     @classmethod
462     def poll(cls, context):
463         return context.active_node is not None
464
465     def draw(self, context):
466         layout = self.layout
467         node = context.active_node
468
469         layout.prop(node, "name", icon='NODE')
470         layout.prop(node, "label", icon='NODE')
471
472
473 class NODE_PT_active_node_color(Panel):
474     bl_space_type = 'NODE_EDITOR'
475     bl_region_type = 'UI'
476     bl_category = "Item"
477     bl_label = "Color"
478     bl_options = {'DEFAULT_CLOSED'}
479     bl_parent_id = 'NODE_PT_active_node_generic'
480
481     @classmethod
482     def poll(cls, context):
483         return context.active_node is not None
484
485     def draw_header(self, context):
486         node = context.active_node
487         self.layout.prop(node, "use_custom_color", text="")
488
489     def draw_header_preset(self, _context):
490         NODE_PT_node_color_presets.draw_panel_header(self.layout)
491
492     def draw(self, context):
493         layout = self.layout
494         node = context.active_node
495
496         layout.enabled = node.use_custom_color
497
498         row = layout.row()
499         row.prop(node, "color", text="")
500         row.menu("NODE_MT_node_color_context_menu", text="", icon='DOWNARROW_HLT')
501
502
503 class NODE_PT_active_node_properties(Panel):
504     bl_space_type = 'NODE_EDITOR'
505     bl_region_type = 'UI'
506     bl_category = "Item"
507     bl_label = "Properties"
508     bl_options = {'DEFAULT_CLOSED'}
509
510     @classmethod
511     def poll(cls, context):
512         return context.active_node is not None
513
514     def draw(self, context):
515         layout = self.layout
516         node = context.active_node
517         # set "node" context pointer for the panel layout
518         layout.context_pointer_set("node", node)
519
520         if hasattr(node, "draw_buttons_ext"):
521             node.draw_buttons_ext(context, layout)
522         elif hasattr(node, "draw_buttons"):
523             node.draw_buttons(context, layout)
524
525         # XXX this could be filtered further to exclude socket types
526         # which don't have meaningful input values (e.g. cycles shader)
527         value_inputs = [socket for socket in node.inputs if socket.enabled and not socket.is_linked]
528         if value_inputs:
529             layout.separator()
530             layout.label(text="Inputs:")
531             for socket in value_inputs:
532                 row = layout.row()
533                 socket.draw(context, row, node, iface_(socket.name, socket.bl_rna.translation_context))
534
535
536 class NODE_PT_texture_mapping(Panel):
537     bl_space_type = 'NODE_EDITOR'
538     bl_region_type = 'UI'
539     bl_category = "Item"
540     bl_label = "Texture Mapping"
541     bl_options = {'DEFAULT_CLOSED'}
542     COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_EEVEE', 'BLENDER_WORKBENCH'}
543
544     @classmethod
545     def poll(cls, context):
546         node = context.active_node
547         return node and hasattr(node, "texture_mapping") and (context.engine in cls.COMPAT_ENGINES)
548
549     def draw(self, context):
550         layout = self.layout
551         layout.use_property_split = True
552         layout.use_property_decorate = False  # No animation.
553
554         node = context.active_node
555         mapping = node.texture_mapping
556
557         layout.prop(mapping, "vector_type")
558
559         layout.separator()
560
561         col = layout.column(align=True)
562         col.prop(mapping, "mapping_x", text="Projection X")
563         col.prop(mapping, "mapping_y", text="Y")
564         col.prop(mapping, "mapping_z", text="Z")
565
566         layout.separator()
567
568         layout.prop(mapping, "translation")
569         layout.prop(mapping, "rotation")
570         layout.prop(mapping, "scale")
571
572
573 # Node Backdrop options
574 class NODE_PT_backdrop(Panel):
575     bl_space_type = 'NODE_EDITOR'
576     bl_region_type = 'UI'
577     bl_category = "View"
578     bl_label = "Backdrop"
579
580     @classmethod
581     def poll(cls, context):
582         snode = context.space_data
583         return snode.tree_type == 'CompositorNodeTree'
584
585     def draw_header(self, context):
586         snode = context.space_data
587         self.layout.prop(snode, "show_backdrop", text="")
588
589     def draw(self, context):
590         layout = self.layout
591         layout.use_property_split = True
592         layout.use_property_decorate = False
593
594         snode = context.space_data
595         layout.active = snode.show_backdrop
596
597         col = layout.column()
598
599         col.prop(snode, "backdrop_channels", text="Channels")
600         col.prop(snode, "backdrop_zoom", text="Zoom")
601
602         col.prop(snode, "backdrop_offset", text="Offset")
603
604         col.separator()
605
606         col.operator("node.backimage_move", text="Move")
607         col.operator("node.backimage_fit", text="Fit")
608
609
610 class NODE_PT_quality(bpy.types.Panel):
611     bl_space_type = 'NODE_EDITOR'
612     bl_region_type = 'UI'
613     bl_category = "Options"
614     bl_label = "Performance"
615
616     @classmethod
617     def poll(cls, context):
618         snode = context.space_data
619         return snode.tree_type == 'CompositorNodeTree' and snode.node_tree is not None
620
621     def draw(self, context):
622         layout = self.layout
623         layout.use_property_split = True
624         layout.use_property_decorate = False
625
626         snode = context.space_data
627         tree = snode.node_tree
628
629         col = layout.column()
630         col.prop(tree, "render_quality", text="Render")
631         col.prop(tree, "edit_quality", text="Edit")
632         col.prop(tree, "chunk_size")
633
634         col = layout.column()
635         col.prop(tree, "use_opencl")
636         col.prop(tree, "use_groupnode_buffer")
637         col.prop(tree, "use_two_pass")
638         col.prop(tree, "use_viewer_border")
639         col.separator()
640         col.prop(snode, "use_auto_render")
641
642
643 class NODE_UL_interface_sockets(bpy.types.UIList):
644     def draw_item(self, context, layout, _data, item, icon, _active_data, _active_propname, _index):
645         socket = item
646         color = socket.draw_color(context)
647
648         if self.layout_type in {'DEFAULT', 'COMPACT'}:
649             row = layout.row(align=True)
650
651             # inputs get icon on the left
652             if not socket.is_output:
653                 row.template_node_socket(color=color)
654
655             row.prop(socket, "name", text="", emboss=False, icon_value=icon)
656
657             # outputs get icon on the right
658             if socket.is_output:
659                 row.template_node_socket(color=color)
660
661         elif self.layout_type == 'GRID':
662             layout.alignment = 'CENTER'
663             layout.template_node_socket(color=color)
664
665
666 # Grease Pencil properties
667 class NODE_PT_annotation(AnnotationDataPanel, Panel):
668     bl_space_type = 'NODE_EDITOR'
669     bl_region_type = 'UI'
670     bl_category = "View"
671     bl_options = {'DEFAULT_CLOSED'}
672
673     # NOTE: this is just a wrapper around the generic GP Panel
674
675     @classmethod
676     def poll(cls, context):
677         snode = context.space_data
678         return snode is not None and snode.node_tree is not None
679
680
681 def node_draw_tree_view(_layout, _context):
682     pass
683
684
685 # Adapt properties editor panel to display in node editor. We have to
686 # copy the class rather than inherit due to the way bpy registration works.
687 def node_panel(cls):
688     node_cls_dict = cls.__dict__.copy()
689
690     # Needed for re-registration.
691     node_cls_dict.pop("bl_rna", None)
692
693     node_cls = type('NODE_' + cls.__name__, cls.__bases__, node_cls_dict)
694
695     node_cls.bl_space_type = 'NODE_EDITOR'
696     node_cls.bl_region_type = 'UI'
697     node_cls.bl_category = "Options"
698     if hasattr(node_cls, 'bl_parent_id'):
699         node_cls.bl_parent_id = 'NODE_' + node_cls.bl_parent_id
700
701     return node_cls
702
703
704 classes = (
705     NODE_HT_header,
706     NODE_MT_editor_menus,
707     NODE_MT_add,
708     NODE_MT_view,
709     NODE_MT_select,
710     NODE_MT_node,
711     NODE_MT_node_color_context_menu,
712     NODE_MT_context_menu,
713     NODE_PT_material_slots,
714     NODE_PT_node_color_presets,
715     NODE_PT_active_node_generic,
716     NODE_PT_active_node_color,
717     NODE_PT_active_node_properties,
718     NODE_PT_texture_mapping,
719     NODE_PT_active_tool,
720     NODE_PT_backdrop,
721     NODE_PT_quality,
722     NODE_PT_annotation,
723     NODE_UL_interface_sockets,
724
725     node_panel(EEVEE_MATERIAL_PT_settings),
726     node_panel(MATERIAL_PT_viewport),
727     node_panel(WORLD_PT_viewport_display),
728     node_panel(DATA_PT_light),
729     node_panel(DATA_PT_EEVEE_light),
730 )
731
732
733 if __name__ == "__main__":  # only for live edit.
734     from bpy.utils import register_class
735     for cls in classes:
736         register_class(cls)