Fix #35376: node editor throwing python error on some files saved with 2.66 test...
[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         ob = context.object
32         snode = context.space_data
33         snode_id = snode.id
34         id_from = snode.id_from
35         toolsettings = context.tool_settings
36
37         row = layout.row(align=True)
38         row.template_header()
39
40         if context.area.show_menus:
41             row.menu("NODE_MT_view")
42             row.menu("NODE_MT_select")
43             row.menu("NODE_MT_add")
44             row.menu("NODE_MT_node")
45
46         layout.prop(snode, "tree_type", text="", expand=True)
47
48         if snode.tree_type == 'ShaderNodeTree':
49             if scene.render.use_shading_nodes:
50                 layout.prop(snode, "shader_type", text="", expand=True)
51
52             if (not scene.render.use_shading_nodes or snode.shader_type == 'OBJECT') and ob:
53                 # Show material.new when no active ID/slot exists
54                 if not id_from and ob.type in {'MESH', 'CURVE', 'SURFACE', 'FONT', 'METABALL'}:
55                     layout.template_ID(ob, "active_material", new="material.new")
56                 # Material ID, but not for Lamps
57                 if id_from and ob.type != 'LAMP':
58                     layout.template_ID(id_from, "active_material", new="material.new")
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                 layout.template_ID(scene, "world", new="world.new")
65                 if snode_id:
66                     layout.prop(snode_id, "use_nodes")
67
68         elif snode.tree_type == 'TextureNodeTree':
69             layout.prop(snode, "texture_type", text="", expand=True)
70
71             if id_from:
72                 if snode.texture_type == 'BRUSH':
73                     layout.template_ID(id_from, "texture", new="texture.new")
74                 else:
75                     layout.template_ID(id_from, "active_texture", new="texture.new")
76             if snode_id:
77                 layout.prop(snode_id, "use_nodes")
78
79         elif snode.tree_type == 'CompositorNodeTree':
80             if snode_id:
81                 layout.prop(snode_id, "use_nodes")
82                 layout.prop(snode_id.render, "use_free_unused_nodes", text="Free Unused")
83             layout.prop(snode, "show_backdrop")
84             if snode.show_backdrop:
85                 row = layout.row(align=True)
86                 row.prop(snode, "backdrop_channels", text="", expand=True)
87             layout.prop(snode, "use_auto_render")
88
89         else:
90             # Custom node tree is edited as independent ID block
91             layout.template_ID(snode, "node_tree", new="node.new_node_tree")
92
93         layout.prop(snode, "pin", text="")
94         layout.operator("node.tree_path_parent", text="", icon='FILE_PARENT')
95
96         layout.separator()
97
98         # Snap
99         row = layout.row(align=True)
100         row.prop(toolsettings, "use_snap", text="")
101         row.prop(toolsettings, "snap_node_element", text="", icon_only=True)
102         if toolsettings.snap_node_element != 'INCREMENT':
103             row.prop(toolsettings, "snap_target", text="")
104
105         row = layout.row(align=True)
106         row.operator("node.clipboard_copy", text="", icon='COPYDOWN')
107         row.operator("node.clipboard_paste", text="", icon='PASTEDOWN')
108
109         layout.template_running_jobs()
110
111
112 class NODE_MT_add(bpy.types.Menu):
113     bl_space_type = 'NODE_EDITOR'
114     bl_label = "Add"
115
116     def draw(self, context):
117         layout = self.layout
118
119         layout.operator_context = 'INVOKE_DEFAULT'
120         props = layout.operator("node.add_search", text="Search ...")
121         props.use_transform = True
122
123         # actual node submenus are added by draw functions from node categories
124
125
126 class NODE_MT_view(Menu):
127     bl_label = "View"
128
129     def draw(self, context):
130         layout = self.layout
131
132         layout.operator("node.properties", icon='MENU_PANEL')
133         layout.operator("node.toolbar", icon='MENU_PANEL')
134         
135         layout.separator()
136
137         layout.operator("view2d.zoom_in")
138         layout.operator("view2d.zoom_out")
139
140         layout.separator()
141
142         layout.operator("node.view_selected")
143         layout.operator("node.view_all")
144
145         if context.space_data.show_backdrop:
146             layout.separator()
147
148             layout.operator("node.backimage_move", text="Backdrop move")
149             layout.operator("node.backimage_zoom", text="Backdrop zoom in").factor = 1.2
150             layout.operator("node.backimage_zoom", text="Backdrop zoom out").factor = 0.833
151
152         layout.separator()
153
154         layout.operator("screen.area_dupli")
155         layout.operator("screen.screen_full_area")
156
157
158 class NODE_MT_select(Menu):
159     bl_label = "Select"
160
161     def draw(self, context):
162         layout = self.layout
163
164         layout.operator("node.select_border")
165
166         layout.separator()
167         layout.operator("node.select_all").action = 'TOGGLE'
168         layout.operator("node.select_all", text="Inverse").action = 'INVERT'
169         layout.operator("node.select_linked_from")
170         layout.operator("node.select_linked_to")
171
172         layout.separator()
173
174         layout.operator("node.select_same_type")
175         layout.operator("node.select_same_type_step").prev = True
176         layout.operator("node.select_same_type_step").prev = False
177
178         layout.separator()
179
180         layout.operator("node.find_node")
181
182
183 class NODE_MT_node(Menu):
184     bl_label = "Node"
185
186     def draw(self, context):
187         layout = self.layout
188
189         layout.operator("transform.translate")
190         layout.operator("transform.rotate")
191         layout.operator("transform.resize")
192
193         layout.separator()
194
195         layout.operator("node.duplicate_move")
196         layout.operator("node.delete")
197         layout.operator("node.delete_reconnect")
198
199         layout.separator()
200
201         layout.operator("node.join", text="Join in new Frame")
202         layout.operator("node.detach", text="Remove from Frame")
203
204         layout.separator()
205
206         layout.operator("node.link_make")
207         layout.operator("node.link_make", text="Make and Replace Links").replace = True
208         layout.operator("node.links_cut")
209         layout.operator("node.links_detach")
210
211         layout.separator()
212
213         layout.operator("node.group_edit")
214         layout.operator("node.group_ungroup")
215         layout.operator("node.group_make")
216         layout.operator("node.group_insert")
217
218         layout.separator()
219
220         layout.operator("node.hide_toggle")
221         layout.operator("node.mute_toggle")
222         layout.operator("node.preview_toggle")
223         layout.operator("node.hide_socket_toggle")
224         layout.operator("node.options_toggle")
225         layout.operator("node.collapse_hide_unused_toggle")
226
227         layout.separator()
228
229         layout.operator("node.show_cyclic_dependencies")
230         layout.operator("node.read_renderlayers")
231         layout.operator("node.read_fullsamplelayers")
232
233
234 # Node Backdrop options
235 class NODE_PT_properties(Panel):
236     bl_space_type = 'NODE_EDITOR'
237     bl_region_type = 'UI'
238     bl_label = "Backdrop"
239
240     @classmethod
241     def poll(cls, context):
242         snode = context.space_data
243         return snode.tree_type == 'CompositorNodeTree'
244
245     def draw_header(self, context):
246         snode = context.space_data
247         self.layout.prop(snode, "show_backdrop", text="")
248
249     def draw(self, context):
250         layout = self.layout
251
252         snode = context.space_data
253         layout.active = snode.show_backdrop
254         layout.prop(snode, "backdrop_channels", text="")
255         layout.prop(snode, "backdrop_zoom", text="Zoom")
256
257         col = layout.column(align=True)
258         col.label(text="Offset:")
259         col.prop(snode, "backdrop_x", text="X")
260         col.prop(snode, "backdrop_y", text="Y")
261         col.operator("node.backimage_move", text="Move")
262
263
264 class NODE_PT_quality(bpy.types.Panel):
265     bl_space_type = 'NODE_EDITOR'
266     bl_region_type = 'UI'
267     bl_label = "Performance"
268
269     @classmethod
270     def poll(cls, context):
271         snode = context.space_data
272         return snode.tree_type == 'CompositorNodeTree' and snode.node_tree is not None
273
274     def draw(self, context):
275         layout = self.layout
276
277         snode = context.space_data
278         tree = snode.node_tree
279
280         col = layout.column()
281         col.prop(tree, "render_quality", text="Render")
282         col.prop(tree, "edit_quality", text="Edit")
283         col.prop(tree, "chunk_size")
284
285         col = layout.column()
286         col.prop(tree, "use_opencl")
287         col.prop(tree, "use_groupnode_buffer")
288         col.prop(tree, "use_two_pass")
289         col.prop(tree, "use_viewer_border")
290         col.prop(snode, "show_highlight")
291         col.prop(snode, "use_hidden_preview")
292
293
294 class NODE_MT_node_color_presets(Menu):
295     """Predefined node color"""
296     bl_label = "Color Presets"
297     preset_subdir = "node_color"
298     preset_operator = "script.execute_preset"
299     draw = Menu.draw_preset
300
301
302 class NODE_MT_node_color_specials(Menu):
303     bl_label = "Node Color Specials"
304
305     def draw(self, context):
306         layout = self.layout
307
308         layout.operator("node.node_copy_color", icon='COPY_ID')
309
310
311 class NODE_UL_interface_sockets(bpy.types.UIList):
312     def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
313         socket = item
314         color = socket.draw_color(context)
315
316         if self.layout_type in {'DEFAULT', 'COMPACT'}:
317             row = layout.row(align=True)
318
319             # inputs get icon on the left
320             if socket.in_out == 'IN':
321                 row.template_node_socket(color)
322
323             row.label(text=socket.name, icon_value=icon)
324
325             # outputs get icon on the right
326             if socket.in_out == 'OUT':
327                 row.template_node_socket(color)
328
329         elif self.layout_type in {'GRID'}:
330             layout.alignment = 'CENTER'
331             layout.template_node_socket(color)
332
333
334 def node_draw_tree_view(layout, context):
335     pass
336
337
338 if __name__ == "__main__":  # only for live edit.
339     bpy.utils.register_module(__name__)