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