Cleanup: replace dict /w list for module reload
[blender.git] / release / scripts / startup / nodeitems_builtins.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 nodeitems_utils import NodeCategory, NodeItem, NodeItemCustom
23
24
25 # Subclasses for standard node types
26
27 class CompositorNodeCategory(NodeCategory):
28     @classmethod
29     def poll(cls, context):
30         return (context.space_data.tree_type == 'CompositorNodeTree')
31
32
33 class ShaderNewNodeCategory(NodeCategory):
34     @classmethod
35     def poll(cls, context):
36         return (context.space_data.tree_type == 'ShaderNodeTree' and
37                 context.scene.render.use_shading_nodes)
38
39
40 class ShaderOldNodeCategory(NodeCategory):
41     @classmethod
42     def poll(cls, context):
43         return (context.space_data.tree_type == 'ShaderNodeTree' and
44                 not context.scene.render.use_shading_nodes)
45
46
47 class TextureNodeCategory(NodeCategory):
48     @classmethod
49     def poll(cls, context):
50         return context.space_data.tree_type == 'TextureNodeTree'
51
52
53 # menu entry for node group tools
54 def group_tools_draw(self, layout, context):
55     layout.operator("node.group_make")
56     layout.operator("node.group_ungroup")
57     layout.separator()
58
59 # maps node tree type to group node type
60 node_tree_group_type = {
61     'CompositorNodeTree': 'CompositorNodeGroup',
62     'ShaderNodeTree': 'ShaderNodeGroup',
63     'TextureNodeTree': 'TextureNodeGroup',
64     }
65
66
67 # generic node group items generator for shader, compositor and texture node groups
68 def node_group_items(context):
69     if context is None:
70         return
71     space = context.space_data
72     if not space:
73         return
74     ntree = space.edit_tree
75     if not ntree:
76         return
77
78     yield NodeItemCustom(draw=group_tools_draw)
79
80     def contains_group(nodetree, group):
81         if nodetree == group:
82             return True
83         else:
84             for node in nodetree.nodes:
85                 if node.bl_idname in node_tree_group_type.values() and node.node_tree is not None:
86                     if contains_group(node.node_tree, group):
87                         return True
88         return False
89
90     for group in context.blend_data.node_groups:
91         if group.bl_idname != ntree.bl_idname:
92             continue
93         # filter out recursive groups
94         if contains_group(group, ntree):
95             continue
96
97         yield NodeItem(node_tree_group_type[group.bl_idname],
98                        group.name,
99                        {"node_tree": "bpy.data.node_groups[%r]" % group.name})
100
101
102 # only show input/output nodes inside node groups
103 def group_input_output_item_poll(context):
104     space = context.space_data
105     if space.edit_tree in bpy.data.node_groups.values():
106         return True
107     return False
108
109
110 # only show input/output nodes when editing line style node trees
111 def line_style_shader_nodes_poll(context):
112     snode = context.space_data
113     return (snode.tree_type == 'ShaderNodeTree' and
114             snode.shader_type == 'LINESTYLE')
115
116
117 # only show nodes working in world node trees
118 def world_shader_nodes_poll(context):
119     snode = context.space_data
120     return (snode.tree_type == 'ShaderNodeTree' and
121             snode.shader_type == 'WORLD')
122
123
124 # only show nodes working in object node trees
125 def object_shader_nodes_poll(context):
126     snode = context.space_data
127     return (snode.tree_type == 'ShaderNodeTree' and
128             snode.shader_type == 'OBJECT')
129
130
131 # All standard node categories currently used in nodes.
132
133 shader_node_categories = [
134     # Shader Nodes
135     ShaderOldNodeCategory("SH_INPUT", "Input", items=[
136         NodeItem("ShaderNodeMaterial"),
137         NodeItem("ShaderNodeCameraData"),
138         NodeItem("ShaderNodeLampData"),
139         NodeItem("ShaderNodeValue"),
140         NodeItem("ShaderNodeRGB"),
141         NodeItem("ShaderNodeTexture"),
142         NodeItem("ShaderNodeGeometry"),
143         NodeItem("ShaderNodeExtendedMaterial"),
144         NodeItem("ShaderNodeParticleInfo"),
145         NodeItem("NodeGroupInput", poll=group_input_output_item_poll),
146         ]),
147     ShaderOldNodeCategory("SH_OUTPUT", "Output", items=[
148         NodeItem("ShaderNodeOutput"),
149         NodeItem("NodeGroupOutput", poll=group_input_output_item_poll),
150         ]),
151     ShaderOldNodeCategory("SH_OP_COLOR", "Color", items=[
152         NodeItem("ShaderNodeMixRGB"),
153         NodeItem("ShaderNodeRGBCurve"),
154         NodeItem("ShaderNodeInvert"),
155         NodeItem("ShaderNodeHueSaturation"),
156         NodeItem("ShaderNodeGamma"),
157         ]),
158     ShaderOldNodeCategory("SH_OP_VECTOR", "Vector", items=[
159         NodeItem("ShaderNodeNormal"),
160         NodeItem("ShaderNodeMapping"),
161         NodeItem("ShaderNodeVectorCurve"),
162         NodeItem("ShaderNodeVectorTransform"),
163         ]),
164     ShaderOldNodeCategory("SH_CONVERTOR", "Converter", items=[
165         NodeItem("ShaderNodeValToRGB"),
166         NodeItem("ShaderNodeRGBToBW"),
167         NodeItem("ShaderNodeMath"),
168         NodeItem("ShaderNodeVectorMath"),
169         NodeItem("ShaderNodeSqueeze"),
170         NodeItem("ShaderNodeSeparateRGB"),
171         NodeItem("ShaderNodeCombineRGB"),
172         NodeItem("ShaderNodeSeparateHSV"),
173         NodeItem("ShaderNodeCombineHSV"),
174         ]),
175     ShaderOldNodeCategory("SH_GROUP", "Group", items=node_group_items),
176     ShaderOldNodeCategory("SH_LAYOUT", "Layout", items=[
177         NodeItem("NodeFrame"),
178         NodeItem("NodeReroute"),
179         ]),
180
181     # New Shader Nodes (Cycles)
182     ShaderNewNodeCategory("SH_NEW_INPUT", "Input", items=[
183         NodeItem("ShaderNodeTexCoord"),
184         NodeItem("ShaderNodeAttribute"),
185         NodeItem("ShaderNodeLightPath"),
186         NodeItem("ShaderNodeFresnel"),
187         NodeItem("ShaderNodeLayerWeight"),
188         NodeItem("ShaderNodeRGB"),
189         NodeItem("ShaderNodeValue"),
190         NodeItem("ShaderNodeTangent"),
191         NodeItem("ShaderNodeNewGeometry"),
192         NodeItem("ShaderNodeWireframe"),
193         NodeItem("ShaderNodeObjectInfo"),
194         NodeItem("ShaderNodeHairInfo"),
195         NodeItem("ShaderNodeParticleInfo"),
196         NodeItem("ShaderNodeCameraData"),
197         NodeItem("ShaderNodeUVMap"),
198         NodeItem("ShaderNodeUVAlongStroke", poll=line_style_shader_nodes_poll),
199         NodeItem("NodeGroupInput", poll=group_input_output_item_poll),
200         ]),
201     ShaderNewNodeCategory("SH_NEW_OUTPUT", "Output", items=[
202         NodeItem("ShaderNodeOutputMaterial", poll=object_shader_nodes_poll),
203         NodeItem("ShaderNodeOutputLamp", poll=object_shader_nodes_poll),
204         NodeItem("ShaderNodeOutputWorld", poll=world_shader_nodes_poll),
205         NodeItem("ShaderNodeOutputLineStyle", poll=line_style_shader_nodes_poll),
206         NodeItem("NodeGroupOutput", poll=group_input_output_item_poll),
207         ]),
208     ShaderNewNodeCategory("SH_NEW_SHADER", "Shader", items=[
209         NodeItem("ShaderNodeMixShader"),
210         NodeItem("ShaderNodeAddShader"),
211         NodeItem("ShaderNodeBsdfDiffuse", poll=object_shader_nodes_poll),
212         NodeItem("ShaderNodeBsdfGlossy", poll=object_shader_nodes_poll),
213         NodeItem("ShaderNodeBsdfTransparent", poll=object_shader_nodes_poll),
214         NodeItem("ShaderNodeBsdfRefraction", poll=object_shader_nodes_poll),
215         NodeItem("ShaderNodeBsdfGlass", poll=object_shader_nodes_poll),
216         NodeItem("ShaderNodeBsdfTranslucent", poll=object_shader_nodes_poll),
217         NodeItem("ShaderNodeBsdfAnisotropic", poll=object_shader_nodes_poll),
218         NodeItem("ShaderNodeBsdfVelvet", poll=object_shader_nodes_poll),
219         NodeItem("ShaderNodeBsdfToon", poll=object_shader_nodes_poll),
220         NodeItem("ShaderNodeSubsurfaceScattering", poll=object_shader_nodes_poll),
221         NodeItem("ShaderNodeEmission", poll=object_shader_nodes_poll),
222         NodeItem("ShaderNodeBsdfHair", poll=object_shader_nodes_poll),
223         NodeItem("ShaderNodeBackground", poll=world_shader_nodes_poll),
224         NodeItem("ShaderNodeAmbientOcclusion", poll=object_shader_nodes_poll),
225         NodeItem("ShaderNodeHoldout", poll=object_shader_nodes_poll),
226         NodeItem("ShaderNodeVolumeAbsorption"),
227         NodeItem("ShaderNodeVolumeScatter"),
228         ]),
229     ShaderNewNodeCategory("SH_NEW_TEXTURE", "Texture", items=[
230         NodeItem("ShaderNodeTexImage"),
231         NodeItem("ShaderNodeTexEnvironment"),
232         NodeItem("ShaderNodeTexSky"),
233         NodeItem("ShaderNodeTexNoise"),
234         NodeItem("ShaderNodeTexWave"),
235         NodeItem("ShaderNodeTexVoronoi"),
236         NodeItem("ShaderNodeTexMusgrave"),
237         NodeItem("ShaderNodeTexGradient"),
238         NodeItem("ShaderNodeTexMagic"),
239         NodeItem("ShaderNodeTexChecker"),
240         NodeItem("ShaderNodeTexBrick"),
241         NodeItem("ShaderNodeTexPointDensity"),
242         ]),
243     ShaderNewNodeCategory("SH_NEW_OP_COLOR", "Color", items=[
244         NodeItem("ShaderNodeMixRGB"),
245         NodeItem("ShaderNodeRGBCurve"),
246         NodeItem("ShaderNodeInvert"),
247         NodeItem("ShaderNodeLightFalloff"),
248         NodeItem("ShaderNodeHueSaturation"),
249         NodeItem("ShaderNodeGamma"),
250         NodeItem("ShaderNodeBrightContrast"),
251         ]),
252     ShaderNewNodeCategory("SH_NEW_OP_VECTOR", "Vector", items=[
253         NodeItem("ShaderNodeMapping"),
254         NodeItem("ShaderNodeBump"),
255         NodeItem("ShaderNodeNormalMap"),
256         NodeItem("ShaderNodeNormal"),
257         NodeItem("ShaderNodeVectorCurve"),
258         NodeItem("ShaderNodeVectorTransform"),
259         ]),
260     ShaderNewNodeCategory("SH_NEW_CONVERTOR", "Converter", items=[
261         NodeItem("ShaderNodeMath"),
262         NodeItem("ShaderNodeValToRGB"),
263         NodeItem("ShaderNodeRGBToBW"),
264         NodeItem("ShaderNodeVectorMath"),
265         NodeItem("ShaderNodeSeparateRGB"),
266         NodeItem("ShaderNodeCombineRGB"),
267         NodeItem("ShaderNodeSeparateXYZ"),
268         NodeItem("ShaderNodeCombineXYZ"),
269         NodeItem("ShaderNodeSeparateHSV"),
270         NodeItem("ShaderNodeCombineHSV"),
271         NodeItem("ShaderNodeWavelength"),
272         NodeItem("ShaderNodeBlackbody"),
273         ]),
274     ShaderNewNodeCategory("SH_NEW_SCRIPT", "Script", items=[
275         NodeItem("ShaderNodeScript"),
276         ]),
277     ShaderNewNodeCategory("SH_NEW_GROUP", "Group", items=node_group_items),
278     ShaderNewNodeCategory("SH_NEW_LAYOUT", "Layout", items=[
279         NodeItem("NodeFrame"),
280         NodeItem("NodeReroute"),
281         ]),
282     ]
283
284 compositor_node_categories = [
285     # Compositor Nodes
286     CompositorNodeCategory("CMP_INPUT", "Input", items=[
287         NodeItem("CompositorNodeRLayers"),
288         NodeItem("CompositorNodeImage"),
289         NodeItem("CompositorNodeMovieClip"),
290         NodeItem("CompositorNodeMask"),
291         NodeItem("CompositorNodeRGB"),
292         NodeItem("CompositorNodeValue"),
293         NodeItem("CompositorNodeTexture"),
294         NodeItem("CompositorNodeBokehImage"),
295         NodeItem("CompositorNodeTime"),
296         NodeItem("CompositorNodeTrackPos"),
297         NodeItem("NodeGroupInput", poll=group_input_output_item_poll),
298         ]),
299     CompositorNodeCategory("CMP_OUTPUT", "Output", items=[
300         NodeItem("CompositorNodeComposite"),
301         NodeItem("CompositorNodeViewer"),
302         NodeItem("CompositorNodeSplitViewer"),
303         NodeItem("CompositorNodeOutputFile"),
304         NodeItem("CompositorNodeLevels"),
305         NodeItem("NodeGroupOutput", poll=group_input_output_item_poll),
306         ]),
307     CompositorNodeCategory("CMP_OP_COLOR", "Color", items=[
308         NodeItem("CompositorNodeMixRGB"),
309         NodeItem("CompositorNodeAlphaOver"),
310         NodeItem("CompositorNodeInvert"),
311         NodeItem("CompositorNodeCurveRGB"),
312         NodeItem("CompositorNodeHueSat"),
313         NodeItem("CompositorNodeColorBalance"),
314         NodeItem("CompositorNodeHueCorrect"),
315         NodeItem("CompositorNodeBrightContrast"),
316         NodeItem("CompositorNodeGamma"),
317         NodeItem("CompositorNodeColorCorrection"),
318         NodeItem("CompositorNodeTonemap"),
319         NodeItem("CompositorNodeZcombine"),
320         ]),
321     CompositorNodeCategory("CMP_CONVERTOR", "Converter", items=[
322         NodeItem("CompositorNodeMath"),
323         NodeItem("CompositorNodeValToRGB"),
324         NodeItem("CompositorNodeSetAlpha"),
325         NodeItem("CompositorNodePremulKey"),
326         NodeItem("CompositorNodeIDMask"),
327         NodeItem("CompositorNodeRGBToBW"),
328         NodeItem("CompositorNodeSepRGBA"),
329         NodeItem("CompositorNodeCombRGBA"),
330         NodeItem("CompositorNodeSepHSVA"),
331         NodeItem("CompositorNodeCombHSVA"),
332         NodeItem("CompositorNodeSepYUVA"),
333         NodeItem("CompositorNodeCombYUVA"),
334         NodeItem("CompositorNodeSepYCCA"),
335         NodeItem("CompositorNodeCombYCCA"),
336         NodeItem("CompositorNodeSwitchView"),
337         ]),
338     CompositorNodeCategory("CMP_OP_FILTER", "Filter", items=[
339         NodeItem("CompositorNodeBlur"),
340         NodeItem("CompositorNodeBilateralblur"),
341         NodeItem("CompositorNodeDilateErode"),
342         NodeItem("CompositorNodeDespeckle"),
343         NodeItem("CompositorNodeFilter"),
344         NodeItem("CompositorNodeBokehBlur"),
345         NodeItem("CompositorNodeVecBlur"),
346         NodeItem("CompositorNodeDefocus"),
347         NodeItem("CompositorNodeGlare"),
348         NodeItem("CompositorNodeInpaint"),
349         NodeItem("CompositorNodeDBlur"),
350         NodeItem("CompositorNodePixelate"),
351         NodeItem("CompositorNodeSunBeams"),
352         ]),
353     CompositorNodeCategory("CMP_OP_VECTOR", "Vector", items=[
354         NodeItem("CompositorNodeNormal"),
355         NodeItem("CompositorNodeMapValue"),
356         NodeItem("CompositorNodeMapRange"),
357         NodeItem("CompositorNodeNormalize"),
358         NodeItem("CompositorNodeCurveVec"),
359         ]),
360     CompositorNodeCategory("CMP_MATTE", "Matte", items=[
361         NodeItem("CompositorNodeKeying"),
362         NodeItem("CompositorNodeKeyingScreen"),
363         NodeItem("CompositorNodeChannelMatte"),
364         NodeItem("CompositorNodeColorSpill"),
365         NodeItem("CompositorNodeBoxMask"),
366         NodeItem("CompositorNodeEllipseMask"),
367         NodeItem("CompositorNodeLumaMatte"),
368         NodeItem("CompositorNodeDiffMatte"),
369         NodeItem("CompositorNodeDistanceMatte"),
370         NodeItem("CompositorNodeChromaMatte"),
371         NodeItem("CompositorNodeColorMatte"),
372         NodeItem("CompositorNodeDoubleEdgeMask"),
373         ]),
374     CompositorNodeCategory("CMP_DISTORT", "Distort", items=[
375         NodeItem("CompositorNodeScale"),
376         NodeItem("CompositorNodeLensdist"),
377         NodeItem("CompositorNodeMovieDistortion"),
378         NodeItem("CompositorNodeTranslate"),
379         NodeItem("CompositorNodeRotate"),
380         NodeItem("CompositorNodeFlip"),
381         NodeItem("CompositorNodeCrop"),
382         NodeItem("CompositorNodeDisplace"),
383         NodeItem("CompositorNodeMapUV"),
384         NodeItem("CompositorNodeTransform"),
385         NodeItem("CompositorNodeStabilize"),
386         NodeItem("CompositorNodePlaneTrackDeform"),
387         NodeItem("CompositorNodeCornerPin"),
388         ]),
389     CompositorNodeCategory("CMP_GROUP", "Group", items=node_group_items),
390     CompositorNodeCategory("CMP_LAYOUT", "Layout", items=[
391         NodeItem("NodeFrame"),
392         NodeItem("NodeReroute"),
393         NodeItem("CompositorNodeSwitch"),
394         ]),
395     ]
396
397 texture_node_categories = [
398     # Texture Nodes
399     TextureNodeCategory("TEX_INPUT", "Input", items=[
400         NodeItem("TextureNodeCurveTime"),
401         NodeItem("TextureNodeCoordinates"),
402         NodeItem("TextureNodeTexture"),
403         NodeItem("TextureNodeImage"),
404         NodeItem("NodeGroupInput", poll=group_input_output_item_poll),
405         ]),
406     TextureNodeCategory("TEX_OUTPUT", "Output", items=[
407         NodeItem("TextureNodeOutput"),
408         NodeItem("TextureNodeViewer"),
409         NodeItem("NodeGroupOutput", poll=group_input_output_item_poll),
410         ]),
411     TextureNodeCategory("TEX_OP_COLOR", "Color", items=[
412         NodeItem("TextureNodeMixRGB"),
413         NodeItem("TextureNodeCurveRGB"),
414         NodeItem("TextureNodeInvert"),
415         NodeItem("TextureNodeHueSaturation"),
416         NodeItem("TextureNodeCompose"),
417         NodeItem("TextureNodeDecompose"),
418         ]),
419     TextureNodeCategory("TEX_PATTERN", "Pattern", items=[
420         NodeItem("TextureNodeChecker"),
421         NodeItem("TextureNodeBricks"),
422         ]),
423     TextureNodeCategory("TEX_TEXTURE", "Textures", items=[
424         NodeItem("TextureNodeTexNoise"),
425         NodeItem("TextureNodeTexDistNoise"),
426         NodeItem("TextureNodeTexClouds"),
427         NodeItem("TextureNodeTexBlend"),
428         NodeItem("TextureNodeTexVoronoi"),
429         NodeItem("TextureNodeTexMagic"),
430         NodeItem("TextureNodeTexMarble"),
431         NodeItem("TextureNodeTexWood"),
432         NodeItem("TextureNodeTexMusgrave"),
433         NodeItem("TextureNodeTexStucci"),
434         ]),
435     TextureNodeCategory("TEX_CONVERTOR", "Converter", items=[
436         NodeItem("TextureNodeMath"),
437         NodeItem("TextureNodeValToRGB"),
438         NodeItem("TextureNodeRGBToBW"),
439         NodeItem("TextureNodeValToNor"),
440         NodeItem("TextureNodeDistance"),
441         ]),
442     TextureNodeCategory("TEX_DISTORT", "Distort", items=[
443         NodeItem("TextureNodeScale"),
444         NodeItem("TextureNodeTranslate"),
445         NodeItem("TextureNodeRotate"),
446         NodeItem("TextureNodeAt"),
447         ]),
448     TextureNodeCategory("TEX_GROUP", "Group", items=node_group_items),
449     TextureNodeCategory("TEX_LAYOUT", "Layout", items=[
450         NodeItem("NodeFrame"),
451         NodeItem("NodeReroute"),
452         ]),
453     ]
454
455
456 def register():
457     nodeitems_utils.register_node_categories('SHADER', shader_node_categories)
458     nodeitems_utils.register_node_categories('COMPOSITING', compositor_node_categories)
459     nodeitems_utils.register_node_categories('TEXTURE', texture_node_categories)
460
461
462 def unregister():
463     nodeitems_utils.unregister_node_categories('SHADER')
464     nodeitems_utils.unregister_node_categories('COMPOSITING')
465     nodeitems_utils.unregister_node_categories('TEXTURE')
466
467
468 if __name__ == "__main__":
469     register()