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