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