UI: Replace +/- menus with collapsible ones
[blender-staging.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 from bpy.types import Header, Menu, Panel
22
23
24 class NODE_HT_header(Header):
25     bl_space_type = 'NODE_EDITOR'
26
27     def draw(self, context):
28         layout = self.layout
29
30         scene = context.scene
31         snode = context.space_data
32         snode_id = snode.id
33         id_from = snode.id_from
34         toolsettings = context.tool_settings
35
36         row = layout.row(align=True)
37         row.template_header()
38
39         NODE_MT_editor_menus.draw_collapsible(context, layout)
40
41         layout.prop(snode, "tree_type", text="", expand=True)
42
43         if snode.tree_type == 'ShaderNodeTree':
44             if scene.render.use_shading_nodes:
45                 layout.prop(snode, "shader_type", text="", expand=True)
46
47             ob = context.object
48             if (not scene.render.use_shading_nodes or snode.shader_type == 'OBJECT') and ob:
49                 row = layout.row()
50                 # disable material slot buttons when pinned, cannot find correct slot within id_from (#36589)
51                 row.enabled = not snode.pin
52                 # Show material.new when no active ID/slot exists
53                 if not id_from and ob.type in {'MESH', 'CURVE', 'SURFACE', 'FONT', 'METABALL'}:
54                     row.template_ID(ob, "active_material", new="material.new")
55                 # Material ID, but not for Lamps
56                 if id_from and ob.type != 'LAMP':
57                     row.template_ID(id_from, "active_material", new="material.new")
58
59                 # Don't show "Use Nodes" Button when Engine is BI for Lamps
60                 if snode_id and not (scene.render.use_shading_nodes == 0 and ob.type == 'LAMP'):
61                     layout.prop(snode_id, "use_nodes")
62
63             if snode.shader_type == 'WORLD':
64                 row = layout.row()
65                 row.enabled = not snode.pin
66                 row.template_ID(scene, "world", new="world.new")
67                 if snode_id:
68                     row.prop(snode_id, "use_nodes")
69
70         elif snode.tree_type == 'TextureNodeTree':
71             layout.prop(snode, "texture_type", text="", expand=True)
72
73             if id_from:
74                 if snode.texture_type == 'BRUSH':
75                     layout.template_ID(id_from, "texture", new="texture.new")
76                 else:
77                     layout.template_ID(id_from, "active_texture", new="texture.new")
78             if snode_id:
79                 layout.prop(snode_id, "use_nodes")
80
81         elif snode.tree_type == 'CompositorNodeTree':
82             if snode_id:
83                 layout.prop(snode_id, "use_nodes")
84                 layout.prop(snode_id.render, "use_free_unused_nodes", text="Free Unused")
85             layout.prop(snode, "show_backdrop")
86             if snode.show_backdrop:
87                 row = layout.row(align=True)
88                 row.prop(snode, "backdrop_channels", text="", expand=True)
89             layout.prop(snode, "use_auto_render")
90
91         else:
92             # Custom node tree is edited as independent ID block
93             layout.template_ID(snode, "node_tree", new="node.new_node_tree")
94
95         layout.prop(snode, "pin", text="")
96         layout.operator("node.tree_path_parent", text="", icon='FILE_PARENT')
97
98         layout.separator()
99
100         # Snap
101         row = layout.row(align=True)
102         row.prop(toolsettings, "use_snap", text="")
103         row.prop(toolsettings, "snap_node_element", text="", icon_only=True)
104         if toolsettings.snap_node_element != 'GRID':
105             row.prop(toolsettings, "snap_target", text="")
106
107         row = layout.row(align=True)
108         row.operator("node.clipboard_copy", text="", icon='COPYDOWN')
109         row.operator("node.clipboard_paste", text="", icon='PASTEDOWN')
110
111         layout.template_running_jobs()
112
113
114 class NODE_MT_editor_menus(Menu):
115     bl_idname = "NODE_MT_editor_menus"
116     bl_label = ""
117
118     def draw(self, context):
119         self.draw_menus(self.layout, context)
120
121     @staticmethod
122     def draw_menus(layout, context):
123         layout.menu("NODE_MT_view")
124         layout.menu("NODE_MT_select")
125         layout.menu("NODE_MT_add")
126         layout.menu("NODE_MT_node")
127
128
129 class NODE_MT_add(bpy.types.Menu):
130     bl_space_type = 'NODE_EDITOR'
131     bl_label = "Add"
132
133     def draw(self, context):
134         layout = self.layout
135
136         layout.operator_context = 'INVOKE_DEFAULT'
137         props = layout.operator("node.add_search", text="Search ...")
138         props.use_transform = True
139
140         # actual node submenus are added by draw functions from node categories
141
142
143 class NODE_MT_view(Menu):
144     bl_label = "View"
145
146     def draw(self, context):
147         layout = self.layout
148
149         layout.operator("node.properties", icon='MENU_PANEL')
150         layout.operator("node.toolbar", icon='MENU_PANEL')
151
152         layout.separator()
153
154         layout.operator("view2d.zoom_in")
155         layout.operator("view2d.zoom_out")
156
157         layout.separator()
158
159         layout.operator("node.view_selected")
160         layout.operator("node.view_all")
161
162         if context.space_data.show_backdrop:
163             layout.separator()
164
165             layout.operator("node.backimage_move", text="Backdrop move")
166             layout.operator("node.backimage_zoom", text="Backdrop zoom in").factor = 1.2
167             layout.operator("node.backimage_zoom", text="Backdrop zoom out").factor = 0.833
168             layout.operator("node.backimage_fit", text="Fit backdrop to available space")
169
170         layout.separator()
171
172         layout.operator("screen.area_dupli")
173         layout.operator("screen.screen_full_area")
174
175
176 class NODE_MT_select(Menu):
177     bl_label = "Select"
178
179     def draw(self, context):
180         layout = self.layout
181
182         layout.operator("node.select_border")
183         layout.operator("node.select_circle")
184
185         layout.separator()
186         layout.operator("node.select_all").action = 'TOGGLE'
187         layout.operator("node.select_all", text="Inverse").action = 'INVERT'
188         layout.operator("node.select_linked_from")
189         layout.operator("node.select_linked_to")
190
191         layout.separator()
192
193         layout.operator("node.select_same_type")
194         layout.operator("node.select_same_type_step").prev = True
195         layout.operator("node.select_same_type_step").prev = False
196
197         layout.separator()
198
199         layout.operator("node.find_node")
200
201
202 class NODE_MT_node(Menu):
203     bl_label = "Node"
204
205     def draw(self, context):
206         layout = self.layout
207
208         layout.operator("transform.translate")
209         layout.operator("transform.rotate")
210         layout.operator("transform.resize")
211
212         layout.separator()
213
214         layout.operator("node.duplicate_move")
215         layout.operator("node.delete")
216         layout.operator("node.delete_reconnect")
217
218         layout.separator()
219
220         layout.operator("node.join", text="Join in new Frame")
221         layout.operator("node.detach", text="Remove from Frame")
222
223         layout.separator()
224
225         layout.operator("node.link_make")
226         layout.operator("node.link_make", text="Make and Replace Links").replace = True
227         layout.operator("node.links_cut")
228         layout.operator("node.links_detach")
229
230         layout.separator()
231
232         layout.operator("node.group_edit")
233         layout.operator("node.group_ungroup")
234         layout.operator("node.group_make")
235         layout.operator("node.group_insert")
236
237         layout.separator()
238
239         layout.operator("node.hide_toggle")
240         layout.operator("node.mute_toggle")
241         layout.operator("node.preview_toggle")
242         layout.operator("node.hide_socket_toggle")
243         layout.operator("node.options_toggle")
244         layout.operator("node.collapse_hide_unused_toggle")
245
246         layout.separator()
247
248         layout.operator("node.read_renderlayers")
249         layout.operator("node.read_fullsamplelayers")
250
251
252 class NODE_MT_node_color_presets(Menu):
253     """Predefined node color"""
254     bl_label = "Color Presets"
255     preset_subdir = "node_color"
256     preset_operator = "script.execute_preset"
257     draw = Menu.draw_preset
258
259
260 class NODE_MT_node_color_specials(Menu):
261     bl_label = "Node Color Specials"
262
263     def draw(self, context):
264         layout = self.layout
265
266         layout.operator("node.node_copy_color", icon='COPY_ID')
267
268
269 class NODE_PT_active_node_generic(Panel):
270     bl_space_type = 'NODE_EDITOR'
271     bl_region_type = 'UI'
272     bl_label = "Node"
273 #    bl_options = {'HIDE_HEADER'}
274
275     @classmethod
276     def poll(cls, context):
277         space = context.space_data
278         return context.active_node is not None
279
280     def draw(self, context):
281         layout = self.layout
282         node = context.active_node
283
284         layout.prop(node, "name", icon='NODE')
285         layout.prop(node, "label", icon='NODE')
286
287
288 class NODE_PT_active_node_color(Panel):
289     bl_space_type = 'NODE_EDITOR'
290     bl_region_type = 'UI'
291     bl_label = "Color"
292     bl_options = {'DEFAULT_CLOSED'}
293
294     @classmethod
295     def poll(cls, context):
296         space = context.space_data
297         return context.active_node is not None
298
299     def draw_header(self, context):
300         node = context.active_node
301         self.layout.prop(node, "use_custom_color", text="")
302
303     def draw(self, context):
304         layout = self.layout
305         node = context.active_node
306
307         layout.enabled = node.use_custom_color
308
309         row = layout.row()
310         col = row.column()
311         col.menu("NODE_MT_node_color_presets")
312         col.prop(node, "color", text="")
313         col = row.column(align=True)
314         col.operator("node.node_color_preset_add", text="", icon='ZOOMIN').remove_active = False
315         col.operator("node.node_color_preset_add", text="", icon='ZOOMOUT').remove_active = True
316         col.menu("NODE_MT_node_color_specials", text="", icon='DOWNARROW_HLT')
317
318
319 class NODE_PT_active_node_properties(Panel):
320     bl_space_type = 'NODE_EDITOR'
321     bl_region_type = 'UI'
322     bl_label = "Properties"
323
324     @classmethod
325     def poll(cls, context):
326         space = context.space_data
327         return context.active_node is not None
328
329     def draw(self, context):
330         layout = self.layout
331         node = context.active_node
332         # set "node" context pointer for the panel layout
333         layout.context_pointer_set("node", node)
334
335         if hasattr(node, "draw_buttons_ext"):
336             node.draw_buttons_ext(context, layout)
337         elif hasattr(node, "draw_buttons"):
338             node.draw_buttons(context, layout)
339
340         # XXX this could be filtered further to exclude socket types which don't have meaningful input values (e.g. cycles shader)
341         value_inputs = [socket for socket in node.inputs if socket.enabled and not socket.is_linked]
342         if value_inputs:
343             layout.separator()
344             layout.label("Inputs:")
345             for socket in value_inputs:
346                 row = layout.row()
347                 socket.draw(context, row, node, socket.name)
348
349
350 # Node Backdrop options
351 class NODE_PT_backdrop(Panel):
352     bl_space_type = 'NODE_EDITOR'
353     bl_region_type = 'UI'
354     bl_label = "Backdrop"
355
356     @classmethod
357     def poll(cls, context):
358         snode = context.space_data
359         return snode.tree_type == 'CompositorNodeTree'
360
361     def draw_header(self, context):
362         snode = context.space_data
363         self.layout.prop(snode, "show_backdrop", text="")
364
365     def draw(self, context):
366         layout = self.layout
367
368         snode = context.space_data
369         layout.active = snode.show_backdrop
370         layout.prop(snode, "backdrop_channels", text="")
371         layout.prop(snode, "backdrop_zoom", text="Zoom")
372
373         col = layout.column(align=True)
374         col.label(text="Offset:")
375         col.prop(snode, "backdrop_x", text="X")
376         col.prop(snode, "backdrop_y", text="Y")
377         col.operator("node.backimage_move", text="Move")
378
379         layout.operator("node.backimage_fit", text="Fit")
380
381
382 class NODE_PT_quality(bpy.types.Panel):
383     bl_space_type = 'NODE_EDITOR'
384     bl_region_type = 'UI'
385     bl_label = "Performance"
386
387     @classmethod
388     def poll(cls, context):
389         snode = context.space_data
390         return snode.tree_type == 'CompositorNodeTree' and snode.node_tree is not None
391
392     def draw(self, context):
393         layout = self.layout
394
395         snode = context.space_data
396         tree = snode.node_tree
397
398         col = layout.column()
399         col.prop(tree, "render_quality", text="Render")
400         col.prop(tree, "edit_quality", text="Edit")
401         col.prop(tree, "chunk_size")
402
403         col = layout.column()
404         col.prop(tree, "use_opencl")
405         col.prop(tree, "use_groupnode_buffer")
406         col.prop(tree, "use_two_pass")
407         col.prop(tree, "use_viewer_border")
408         col.prop(snode, "show_highlight")
409
410
411 class NODE_UL_interface_sockets(bpy.types.UIList):
412     def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
413         socket = item
414         color = socket.draw_color(context)
415
416         if self.layout_type in {'DEFAULT', 'COMPACT'}:
417             row = layout.row(align=True)
418
419             # inputs get icon on the left
420             if not socket.is_output:
421                 row.template_node_socket(color)
422
423             row.prop(socket, "name", text="", emboss=False, icon_value=icon)
424
425             # outputs get icon on the right
426             if socket.is_output:
427                 row.template_node_socket(color)
428
429         elif self.layout_type in {'GRID'}:
430             layout.alignment = 'CENTER'
431             layout.template_node_socket(color)
432
433
434 def node_draw_tree_view(layout, context):
435     pass
436
437
438 if __name__ == "__main__":  # only for live edit.
439     bpy.utils.register_module(__name__)