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 ShaderNewNodeCategory(SortedNodeCategory):
47     @classmethod
48     def poll(cls, context):
49         return (context.space_data.tree_type == 'ShaderNodeTree' and
50                 context.view_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.view_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 def cycles_shader_nodes_poll(context):
145     return context.view_render.engine == 'CYCLES'
146
147
148 def eevee_shader_nodes_poll(context):
149     return context.view_render.engine == 'BLENDER_EEVEE'
150
151
152 def eevee_cycles_shader_nodes_poll(context):
153     return (cycles_shader_nodes_poll(context) or
154             eevee_shader_nodes_poll(context))
155
156
157 def object_cycles_shader_nodes_poll(context):
158     return (object_shader_nodes_poll(context) and
159             cycles_shader_nodes_poll(context))
160
161
162 def object_eevee_shader_nodes_poll(context):
163     return (object_shader_nodes_poll(context) and
164             eevee_shader_nodes_poll(context))
165
166
167 def object_eevee_cycles_shader_nodes_poll(context):
168     return (object_shader_nodes_poll(context) and
169             eevee_cycles_shader_nodes_poll(context))
170
171
172 # All standard node categories currently used in nodes.
173
174 shader_node_categories = [
175     # Shader Nodes
176     ShaderOldNodeCategory("SH_INPUT", "Input", items=[
177         NodeItem("ShaderNodeMaterial"),
178         NodeItem("ShaderNodeCameraData"),
179         NodeItem("ShaderNodeFresnel"),
180         NodeItem("ShaderNodeLayerWeight"),
181         NodeItem("ShaderNodeLampData"),
182         NodeItem("ShaderNodeValue"),
183         NodeItem("ShaderNodeRGB"),
184         NodeItem("ShaderNodeTexture"),
185         NodeItem("ShaderNodeGeometry"),
186         NodeItem("ShaderNodeExtendedMaterial"),
187         NodeItem("ShaderNodeParticleInfo"),
188         NodeItem("ShaderNodeObjectInfo"),
189         NodeItem("NodeGroupInput", poll=group_input_output_item_poll),
190         ]),
191     ShaderOldNodeCategory("SH_OUTPUT", "Output", items=[
192         NodeItem("ShaderNodeOutput"),
193         NodeItem("NodeGroupOutput", poll=group_input_output_item_poll),
194         ]),
195     ShaderOldNodeCategory("SH_OP_COLOR", "Color", items=[
196         NodeItem("ShaderNodeMixRGB"),
197         NodeItem("ShaderNodeRGBCurve"),
198         NodeItem("ShaderNodeInvert"),
199         NodeItem("ShaderNodeHueSaturation"),
200         NodeItem("ShaderNodeGamma"),
201         ]),
202     ShaderOldNodeCategory("SH_OP_VECTOR", "Vector", items=[
203         NodeItem("ShaderNodeNormal"),
204         NodeItem("ShaderNodeMapping"),
205         NodeItem("ShaderNodeVectorCurve"),
206         NodeItem("ShaderNodeVectorTransform"),
207         NodeItem("ShaderNodeNormalMap"),
208         ]),
209     ShaderOldNodeCategory("SH_CONVERTOR", "Converter", items=[
210         NodeItem("ShaderNodeValToRGB"),
211         NodeItem("ShaderNodeRGBToBW"),
212         NodeItem("ShaderNodeMath"),
213         NodeItem("ShaderNodeVectorMath"),
214         NodeItem("ShaderNodeSqueeze"),
215         NodeItem("ShaderNodeSeparateRGB"),
216         NodeItem("ShaderNodeCombineRGB"),
217         NodeItem("ShaderNodeSeparateHSV"),
218         NodeItem("ShaderNodeCombineHSV"),
219         ]),
220     ShaderOldNodeCategory("SH_GROUP", "Group", items=node_group_items),
221     ShaderOldNodeCategory("SH_LAYOUT", "Layout", items=[
222         NodeItem("NodeFrame"),
223         NodeItem("NodeReroute"),
224         ]),
225
226     # New Shader Nodes (Cycles)
227     ShaderNewNodeCategory("SH_NEW_INPUT", "Input", items=[
228         NodeItem("ShaderNodeTexCoord"),
229         NodeItem("ShaderNodeAttribute"),
230         NodeItem("ShaderNodeLightPath"),
231         NodeItem("ShaderNodeFresnel"),
232         NodeItem("ShaderNodeLayerWeight"),
233         NodeItem("ShaderNodeRGB"),
234         NodeItem("ShaderNodeValue"),
235         NodeItem("ShaderNodeTangent"),
236         NodeItem("ShaderNodeNewGeometry"),
237         NodeItem("ShaderNodeWireframe"),
238         NodeItem("ShaderNodeBevel"),
239         NodeItem("ShaderNodeObjectInfo"),
240         NodeItem("ShaderNodeHairInfo"),
241         NodeItem("ShaderNodeParticleInfo"),
242         NodeItem("ShaderNodeCameraData"),
243         NodeItem("ShaderNodeUVMap"),
244         NodeItem("ShaderNodeUVAlongStroke", poll=line_style_shader_nodes_poll),
245         NodeItem("NodeGroupInput", poll=group_input_output_item_poll),
246         ]),
247     ShaderNewNodeCategory("SH_NEW_OUTPUT", "Output", items=[
248         NodeItem("ShaderNodeOutputMaterial", poll=object_eevee_cycles_shader_nodes_poll),
249         NodeItem("ShaderNodeOutputLamp", poll=object_cycles_shader_nodes_poll),
250         NodeItem("ShaderNodeOutputWorld", poll=world_shader_nodes_poll),
251         NodeItem("ShaderNodeOutputLineStyle", poll=line_style_shader_nodes_poll),
252         NodeItem("NodeGroupOutput", poll=group_input_output_item_poll),
253         ]),
254     ShaderNewNodeCategory("SH_NEW_SHADER", "Shader", items=[
255         NodeItem("ShaderNodeMixShader", poll=eevee_cycles_shader_nodes_poll),
256         NodeItem("ShaderNodeAddShader", poll=eevee_cycles_shader_nodes_poll),
257         NodeItem("ShaderNodeBsdfDiffuse", poll=object_eevee_cycles_shader_nodes_poll),
258         NodeItem("ShaderNodeBsdfPrincipled", poll=object_eevee_cycles_shader_nodes_poll),
259         NodeItem("ShaderNodeBsdfGlossy", poll=object_eevee_cycles_shader_nodes_poll),
260         NodeItem("ShaderNodeBsdfTransparent", poll=object_eevee_cycles_shader_nodes_poll),
261         NodeItem("ShaderNodeBsdfRefraction", poll=object_eevee_cycles_shader_nodes_poll),
262         NodeItem("ShaderNodeBsdfGlass", poll=object_eevee_cycles_shader_nodes_poll),
263         NodeItem("ShaderNodeBsdfTranslucent", poll=object_cycles_shader_nodes_poll),
264         NodeItem("ShaderNodeBsdfAnisotropic", poll=object_cycles_shader_nodes_poll),
265         NodeItem("ShaderNodeBsdfVelvet", poll=object_cycles_shader_nodes_poll),
266         NodeItem("ShaderNodeBsdfToon", poll=object_cycles_shader_nodes_poll),
267         NodeItem("ShaderNodeSubsurfaceScattering", poll=object_eevee_cycles_shader_nodes_poll),
268         NodeItem("ShaderNodeEmission", poll=object_eevee_cycles_shader_nodes_poll),
269         NodeItem("ShaderNodeBsdfHair", poll=object_cycles_shader_nodes_poll),
270         NodeItem("ShaderNodeBackground", poll=world_shader_nodes_poll),
271         NodeItem("ShaderNodeAmbientOcclusion", poll=object_cycles_shader_nodes_poll),
272         NodeItem("ShaderNodeHoldout", poll=object_cycles_shader_nodes_poll),
273         NodeItem("ShaderNodeVolumeAbsorption", poll=eevee_cycles_shader_nodes_poll),
274         NodeItem("ShaderNodeVolumeScatter", poll=eevee_cycles_shader_nodes_poll),
275         NodeItem("ShaderNodeEeveeSpecular", poll=object_eevee_shader_nodes_poll),
276         ]),
277     ShaderNewNodeCategory("SH_NEW_TEXTURE", "Texture", items=[
278         NodeItem("ShaderNodeTexImage"),
279         NodeItem("ShaderNodeTexEnvironment"),
280         NodeItem("ShaderNodeTexSky"),
281         NodeItem("ShaderNodeTexNoise"),
282         NodeItem("ShaderNodeTexWave"),
283         NodeItem("ShaderNodeTexVoronoi"),
284         NodeItem("ShaderNodeTexMusgrave"),
285         NodeItem("ShaderNodeTexGradient"),
286         NodeItem("ShaderNodeTexMagic"),
287         NodeItem("ShaderNodeTexChecker"),
288         NodeItem("ShaderNodeTexBrick"),
289         NodeItem("ShaderNodeTexPointDensity"),
290         ]),
291     ShaderNewNodeCategory("SH_NEW_OP_COLOR", "Color", items=[
292         NodeItem("ShaderNodeMixRGB"),
293         NodeItem("ShaderNodeRGBCurve"),
294         NodeItem("ShaderNodeInvert"),
295         NodeItem("ShaderNodeLightFalloff"),
296         NodeItem("ShaderNodeHueSaturation"),
297         NodeItem("ShaderNodeGamma"),
298         NodeItem("ShaderNodeBrightContrast"),
299         ]),
300     ShaderNewNodeCategory("SH_NEW_OP_VECTOR", "Vector", items=[
301         NodeItem("ShaderNodeMapping"),
302         NodeItem("ShaderNodeBump"),
303         NodeItem("ShaderNodeNormalMap"),
304         NodeItem("ShaderNodeNormal"),
305         NodeItem("ShaderNodeVectorCurve"),
306         NodeItem("ShaderNodeVectorTransform"),
307         ]),
308     ShaderNewNodeCategory("SH_NEW_CONVERTOR", "Converter", items=[
309         NodeItem("ShaderNodeMath"),
310         NodeItem("ShaderNodeValToRGB"),
311         NodeItem("ShaderNodeRGBToBW"),
312         NodeItem("ShaderNodeVectorMath"),
313         NodeItem("ShaderNodeSeparateRGB"),
314         NodeItem("ShaderNodeCombineRGB"),
315         NodeItem("ShaderNodeSeparateXYZ"),
316         NodeItem("ShaderNodeCombineXYZ"),
317         NodeItem("ShaderNodeSeparateHSV"),
318         NodeItem("ShaderNodeCombineHSV"),
319         NodeItem("ShaderNodeWavelength"),
320         NodeItem("ShaderNodeBlackbody"),
321         ]),
322     ShaderNewNodeCategory("SH_NEW_SCRIPT", "Script", items=[
323         NodeItem("ShaderNodeScript"),
324         ]),
325     ShaderNewNodeCategory("SH_NEW_GROUP", "Group", items=node_group_items),
326     ShaderNewNodeCategory("SH_NEW_LAYOUT", "Layout", items=[
327         NodeItem("NodeFrame"),
328         NodeItem("NodeReroute"),
329         ]),
330     ]
331
332 compositor_node_categories = [
333     # Compositor Nodes
334     CompositorNodeCategory("CMP_INPUT", "Input", items=[
335         NodeItem("CompositorNodeRLayers"),
336         NodeItem("CompositorNodeImage"),
337         NodeItem("CompositorNodeMovieClip"),
338         NodeItem("CompositorNodeMask"),
339         NodeItem("CompositorNodeRGB"),
340         NodeItem("CompositorNodeValue"),
341         NodeItem("CompositorNodeTexture"),
342         NodeItem("CompositorNodeBokehImage"),
343         NodeItem("CompositorNodeTime"),
344         NodeItem("CompositorNodeTrackPos"),
345         NodeItem("NodeGroupInput", poll=group_input_output_item_poll),
346         ]),
347     CompositorNodeCategory("CMP_OUTPUT", "Output", items=[
348         NodeItem("CompositorNodeComposite"),
349         NodeItem("CompositorNodeViewer"),
350         NodeItem("CompositorNodeSplitViewer"),
351         NodeItem("CompositorNodeOutputFile"),
352         NodeItem("CompositorNodeLevels"),
353         NodeItem("NodeGroupOutput", poll=group_input_output_item_poll),
354         ]),
355     CompositorNodeCategory("CMP_OP_COLOR", "Color", items=[
356         NodeItem("CompositorNodeMixRGB"),
357         NodeItem("CompositorNodeAlphaOver"),
358         NodeItem("CompositorNodeInvert"),
359         NodeItem("CompositorNodeCurveRGB"),
360         NodeItem("CompositorNodeHueSat"),
361         NodeItem("CompositorNodeColorBalance"),
362         NodeItem("CompositorNodeHueCorrect"),
363         NodeItem("CompositorNodeBrightContrast"),
364         NodeItem("CompositorNodeGamma"),
365         NodeItem("CompositorNodeColorCorrection"),
366         NodeItem("CompositorNodeTonemap"),
367         NodeItem("CompositorNodeZcombine"),
368         ]),
369     CompositorNodeCategory("CMP_CONVERTOR", "Converter", items=[
370         NodeItem("CompositorNodeMath"),
371         NodeItem("CompositorNodeValToRGB"),
372         NodeItem("CompositorNodeSetAlpha"),
373         NodeItem("CompositorNodePremulKey"),
374         NodeItem("CompositorNodeIDMask"),
375         NodeItem("CompositorNodeRGBToBW"),
376         NodeItem("CompositorNodeSepRGBA"),
377         NodeItem("CompositorNodeCombRGBA"),
378         NodeItem("CompositorNodeSepHSVA"),
379         NodeItem("CompositorNodeCombHSVA"),
380         NodeItem("CompositorNodeSepYUVA"),
381         NodeItem("CompositorNodeCombYUVA"),
382         NodeItem("CompositorNodeSepYCCA"),
383         NodeItem("CompositorNodeCombYCCA"),
384         NodeItem("CompositorNodeSwitchView"),
385         ]),
386     CompositorNodeCategory("CMP_OP_FILTER", "Filter", items=[
387         NodeItem("CompositorNodeBlur"),
388         NodeItem("CompositorNodeBilateralblur"),
389         NodeItem("CompositorNodeDilateErode"),
390         NodeItem("CompositorNodeDespeckle"),
391         NodeItem("CompositorNodeFilter"),
392         NodeItem("CompositorNodeBokehBlur"),
393         NodeItem("CompositorNodeVecBlur"),
394         NodeItem("CompositorNodeDefocus"),
395         NodeItem("CompositorNodeGlare"),
396         NodeItem("CompositorNodeInpaint"),
397         NodeItem("CompositorNodeDBlur"),
398         NodeItem("CompositorNodePixelate"),
399         NodeItem("CompositorNodeSunBeams"),
400         ]),
401     CompositorNodeCategory("CMP_OP_VECTOR", "Vector", items=[
402         NodeItem("CompositorNodeNormal"),
403         NodeItem("CompositorNodeMapValue"),
404         NodeItem("CompositorNodeMapRange"),
405         NodeItem("CompositorNodeNormalize"),
406         NodeItem("CompositorNodeCurveVec"),
407         ]),
408     CompositorNodeCategory("CMP_MATTE", "Matte", items=[
409         NodeItem("CompositorNodeKeying"),
410         NodeItem("CompositorNodeKeyingScreen"),
411         NodeItem("CompositorNodeChannelMatte"),
412         NodeItem("CompositorNodeColorSpill"),
413         NodeItem("CompositorNodeBoxMask"),
414         NodeItem("CompositorNodeEllipseMask"),
415         NodeItem("CompositorNodeLumaMatte"),
416         NodeItem("CompositorNodeDiffMatte"),
417         NodeItem("CompositorNodeDistanceMatte"),
418         NodeItem("CompositorNodeChromaMatte"),
419         NodeItem("CompositorNodeColorMatte"),
420         NodeItem("CompositorNodeDoubleEdgeMask"),
421         ]),
422     CompositorNodeCategory("CMP_DISTORT", "Distort", items=[
423         NodeItem("CompositorNodeScale"),
424         NodeItem("CompositorNodeLensdist"),
425         NodeItem("CompositorNodeMovieDistortion"),
426         NodeItem("CompositorNodeTranslate"),
427         NodeItem("CompositorNodeRotate"),
428         NodeItem("CompositorNodeFlip"),
429         NodeItem("CompositorNodeCrop"),
430         NodeItem("CompositorNodeDisplace"),
431         NodeItem("CompositorNodeMapUV"),
432         NodeItem("CompositorNodeTransform"),
433         NodeItem("CompositorNodeStabilize"),
434         NodeItem("CompositorNodePlaneTrackDeform"),
435         NodeItem("CompositorNodeCornerPin"),
436         ]),
437     CompositorNodeCategory("CMP_GROUP", "Group", items=node_group_items),
438     CompositorNodeCategory("CMP_LAYOUT", "Layout", items=[
439         NodeItem("NodeFrame"),
440         NodeItem("NodeReroute"),
441         NodeItem("CompositorNodeSwitch"),
442         ]),
443     ]
444
445 texture_node_categories = [
446     # Texture Nodes
447     TextureNodeCategory("TEX_INPUT", "Input", items=[
448         NodeItem("TextureNodeCurveTime"),
449         NodeItem("TextureNodeCoordinates"),
450         NodeItem("TextureNodeTexture"),
451         NodeItem("TextureNodeImage"),
452         NodeItem("NodeGroupInput", poll=group_input_output_item_poll),
453         ]),
454     TextureNodeCategory("TEX_OUTPUT", "Output", items=[
455         NodeItem("TextureNodeOutput"),
456         NodeItem("TextureNodeViewer"),
457         NodeItem("NodeGroupOutput", poll=group_input_output_item_poll),
458         ]),
459     TextureNodeCategory("TEX_OP_COLOR", "Color", items=[
460         NodeItem("TextureNodeMixRGB"),
461         NodeItem("TextureNodeCurveRGB"),
462         NodeItem("TextureNodeInvert"),
463         NodeItem("TextureNodeHueSaturation"),
464         NodeItem("TextureNodeCompose"),
465         NodeItem("TextureNodeDecompose"),
466         ]),
467     TextureNodeCategory("TEX_PATTERN", "Pattern", items=[
468         NodeItem("TextureNodeChecker"),
469         NodeItem("TextureNodeBricks"),
470         ]),
471     TextureNodeCategory("TEX_TEXTURE", "Textures", items=[
472         NodeItem("TextureNodeTexNoise"),
473         NodeItem("TextureNodeTexDistNoise"),
474         NodeItem("TextureNodeTexClouds"),
475         NodeItem("TextureNodeTexBlend"),
476         NodeItem("TextureNodeTexVoronoi"),
477         NodeItem("TextureNodeTexMagic"),
478         NodeItem("TextureNodeTexMarble"),
479         NodeItem("TextureNodeTexWood"),
480         NodeItem("TextureNodeTexMusgrave"),
481         NodeItem("TextureNodeTexStucci"),
482         ]),
483     TextureNodeCategory("TEX_CONVERTOR", "Converter", items=[
484         NodeItem("TextureNodeMath"),
485         NodeItem("TextureNodeValToRGB"),
486         NodeItem("TextureNodeRGBToBW"),
487         NodeItem("TextureNodeValToNor"),
488         NodeItem("TextureNodeDistance"),
489         ]),
490     TextureNodeCategory("TEX_DISTORT", "Distort", items=[
491         NodeItem("TextureNodeScale"),
492         NodeItem("TextureNodeTranslate"),
493         NodeItem("TextureNodeRotate"),
494         NodeItem("TextureNodeAt"),
495         ]),
496     TextureNodeCategory("TEX_GROUP", "Group", items=node_group_items),
497     TextureNodeCategory("TEX_LAYOUT", "Layout", items=[
498         NodeItem("NodeFrame"),
499         NodeItem("NodeReroute"),
500         ]),
501     ]
502
503
504 def register():
505     nodeitems_utils.register_node_categories('SHADER', shader_node_categories)
506     nodeitems_utils.register_node_categories('COMPOSITING', compositor_node_categories)
507     nodeitems_utils.register_node_categories('TEXTURE', texture_node_categories)
508
509
510 def unregister():
511     nodeitems_utils.unregister_node_categories('SHADER')
512     nodeitems_utils.unregister_node_categories('COMPOSITING')
513     nodeitems_utils.unregister_node_categories('TEXTURE')
514
515
516 if __name__ == "__main__":
517     register()