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