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