# Creating a custom node in Cycles

My friend and I are trying to create a very simple custom node with OSL. The node we are making looks like this:

It has very simple code, and it's only switching input data to output according to the index:

shader mySwitch(
color map1 = 0,
color map2 = 0,
color map3 = 0,
int index = 0,
output closure color BSDF = diffuse(N)
)
{

if (index == 0)
BSDF = map1 * diffuse(N);
else if (index == 1)
BSDF = map2 * diffuse(N);
else
BSDF = map3 * diffuse(N);

}


However, we cannot see the maps (textures) in the 3Dview port (OSL is not supported by GPU), so it must be programmed with the Blender API to create a custom shader, which is what we did. This is what's in the file gpu_shader_material.glsl

void node_switch_shader(
int index,
)
{

switch (index) {

}

}

...


After that, we tried to create the node with C++ by changing 7 files and compiling Blender. However, this process is possible because my smart friend is a very good programmer and we did it only for Ios. For now, we don't know if it is fully functional but maybe it is. But creators and gurus will know.

So is it possible to create something like this in a simpler way? We only want to change the output color data depending on the index, nothing else. For the puppets it can be a game changer.

I am adding the .diff file to show the changes on files for C++. Or please can you add this for next day release?

diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index 8a736c9..8957791 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -792,6 +792,7 @@ struct ShadeResult;
#define SH_NODE_BEVEL                   197
#define SH_NODE_DISPLACEMENT            198
#define SH_NODE_VECTOR_DISPLACEMENT     199

/* custom defines options for Material node */
#define SH_NODE_MAT_DIFF   1
index 1f5ffbd..4f134a8 100644
}

+   int index,
+)
+{
+
+   switch (index) {
+
+
+
+   }
+
+}
+
/* fresnel */

void node_fresnel(float ior, vec3 N, vec3 I, out float result)
diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt
index 1acd1c4..3e4a9d3 100644
--- a/source/blender/nodes/CMakeLists.txt
+++ b/source/blender/nodes/CMakeLists.txt
@@ -181,6 +181,7 @@ set(SRC
index 6ed71e0..7734205 100644
@@ -117,6 +117,7 @@ void register_node_type_sh_volume_scatter(void);
void register_node_type_sh_bsdf_hair(void);
void register_node_type_sh_subsurface_scattering(void);
void register_node_type_sh_uvmap(void);
void register_node_type_sh_uvalongstroke(void);
diff --git a/source/blender/nodes/NOD_static_types.h b/source/blender/nodes/NOD_static_types.h
index 2db23c2..e7d3c03 100644
--- a/source/blender/nodes/NOD_static_types.h
+++ b/source/blender/nodes/NOD_static_types.h
@@ -73,6 +73,7 @@ DefNode( ShaderNode,     SH_NODE_OUTPUT_LINESTYLE,   def_sh_output_linestyle,"OU
DefNode( ShaderNode,     SH_NODE_FRESNEL,            0,                      "FRESNEL",            Fresnel,          "Fresnel",           ""       )
DefNode( ShaderNode,     SH_NODE_LAYER_WEIGHT,       0,                      "LAYER_WEIGHT",       LayerWeight,      "Layer Weight",      ""       )
DefNode( ShaderNode,     SH_NODE_ATTRIBUTE,          def_sh_attribute,       "ATTRIBUTE",          Attribute,        "Attribute",         ""       )
DefNode( ShaderNode,     SH_NODE_AMBIENT_OCCLUSION,  0,                      "AMBIENT_OCCLUSION",  AmbientOcclusion, "Ambient Occlusion", ""       )


eddited Diff 02/15/2018 15:30

diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp
node = new MixClosureNode();
}
+       node = new SwitchClosureNode();
+   }
AttributeNode *attr = new AttributeNode();
index 19b7769..e781017 100644
@@ -48,6 +48,7 @@ set(SRC_OSL
node_math.osl
node_mix.osl
node_mix_closure.osl
+   node_switch_closure.osl
node_musgrave_texture.osl
node_noise_texture.osl
node_normal.osl
diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp
index 48613a9..db75c59 100644
--- a/intern/cycles/render/nodes.cpp
+++ b/intern/cycles/render/nodes.cpp
@@ -3819,6 +3819,51 @@ void MixClosureNode::constant_fold(const ConstantFolder& folder)
}
}

+/* Switch Closure */
+
+NODE_DEFINE(SwitchClosureNode)
+{
+
+   SOCKET_IN_FLOAT(index, "Index", 1.0f);
+   SOCKET_IN_CLOSURE(closure1,  "Closure1");
+   SOCKET_IN_CLOSURE(closure2,  "Closure2");
+   SOCKET_IN_CLOSURE(closure3,  "Closure3");
+   SOCKET_IN_CLOSURE(closure4,  "Closure4");
+   SOCKET_IN_CLOSURE(closure5,  "Closure5");
+   SOCKET_IN_CLOSURE(closure6,  "Closure6");
+   SOCKET_IN_CLOSURE(closure7,  "Closure7");
+   SOCKET_IN_CLOSURE(closure8,  "Closure8");
+   SOCKET_IN_CLOSURE(closure9,  "Closure9");
+   SOCKET_IN_CLOSURE(closure10, "Closure10");
+
+   SOCKET_OUT_CLOSURE(closure, "Closure");
+
+   return type;
+}
+
+SwitchClosureNode::SwitchClosureNode()
+{
+   // Don't know which type to use
+}
+
+void SwitchClosureNode::compile(SVMCompiler& compiler)
+{
+   // How to achieve the same behaviour as in OSL node_switch_closure using SVM?
+}
+
+void SwitchClosureNode::compile(OSLCompiler& compiler)
+{
+}
+
+void SwitchClosureNode::constant_fold(const ConstantFolder& folder)
+{
+   // Do not need to modify folder, probably
+}
+
/* Mix Closure */

NODE_DEFINE(MixClosureWeightNode)
diff --git a/intern/cycles/render/nodes.h b/intern/cycles/render/nodes.h
index f664ebf..5650e2a 100644
--- a/intern/cycles/render/nodes.h
+++ b/intern/cycles/render/nodes.h
@@ -674,6 +674,14 @@ public:
float fac;
};

+class SwitchClosureNode : public ShaderNode {
+public:
+   void constant_fold(const ConstantFolder& folder);
+
+   float index;
+};
+
class MixClosureWeightNode : public ShaderNode {
public:
diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index 8a736c9..887062e 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -792,6 +792,7 @@ struct ShadeResult;
#define SH_NODE_BEVEL                   197
#define SH_NODE_DISPLACEMENT            198
#define SH_NODE_VECTOR_DISPLACEMENT     199

/* custom defines options for Material node */
#define SH_NODE_MAT_DIFF   1
diff --git a/source/blender/blenkernel/intern/node.c b/source/blender/blenkernel/intern/node.c
index 74766be..45478d9 100644
--- a/source/blender/blenkernel/intern/node.c
+++ b/source/blender/blenkernel/intern/node.c
@@ -3618,6 +3618,7 @@ static void registerShaderNodes(void)
register_node_type_sh_volume_scatter();
register_node_type_sh_subsurface_scattering();
register_node_type_sh_uvmap();
register_node_type_sh_uvalongstroke();
index 1f5ffbd..b7200d4 100644
}

+   float index,
+)
+{
+
+   vec4 _placeholder = vec4(0.0, 0.0, 0.0, 0.0);
+   float _index = ceil(index);
+
+   if(_index == 2)
+   else if(_index == 3)
+   else if(_index == 4)
+   else if(_index == 5)
+   else if(_index == 6)
+   else if(_index == 7)
+   else if(_index == 8)
+   else if(_index == 9)
+   else if(_index == 10)
+   else
+}
+
/* fresnel */

void node_fresnel(float ior, vec3 N, vec3 I, out float result)
diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt
index 1acd1c4..3e4a9d3 100644
--- a/source/blender/nodes/CMakeLists.txt
+++ b/source/blender/nodes/CMakeLists.txt
@@ -181,6 +181,7 @@ set(SRC
index 6ed71e0..7734205 100644
@@ -117,6 +117,7 @@ void register_node_type_sh_volume_scatter(void);
void register_node_type_sh_bsdf_hair(void);
void register_node_type_sh_subsurface_scattering(void);
void register_node_type_sh_uvmap(void);
void register_node_type_sh_uvalongstroke(void);
diff --git a/source/blender/nodes/NOD_static_types.h b/source/blender/nodes/NOD_static_types.h
index 2db23c2..e7d3c03 100644
--- a/source/blender/nodes/NOD_static_types.h
+++ b/source/blender/nodes/NOD_static_types.h
@@ -73,6 +73,7 @@ DefNode( ShaderNode,     SH_NODE_OUTPUT_LINESTYLE,   def_sh_output_linestyle,"OU
DefNode( ShaderNode,     SH_NODE_FRESNEL,            0,                      "FRESNEL",            Fresnel,          "Fresnel",           ""       )
DefNode( ShaderNode,     SH_NODE_LAYER_WEIGHT,       0,                      "LAYER_WEIGHT",       LayerWeight,      "Layer Weight",      ""       )
DefNode( ShaderNode,     SH_NODE_ATTRIBUTE,          def_sh_attribute,       "ATTRIBUTE",          Attribute,        "Attribute",         ""       )
DefNode( ShaderNode,     SH_NODE_AMBIENT_OCCLUSION,  0,                      "AMBIENT_OCCLUSION",  AmbientOcclusion, "Ambient Occlusion", ""       )

• This is not a Dev's site, you are better off directing questions about source code and development to the dev's IRC and mailing-list. This will be closed as off-topic. But to help you what you are searching for is probably here: blender.stackexchange.com/a/99003/7777 – Jaroslav Jerryno Novotny Feb 15 '18 at 0:14
• Possible duplicate of Cycles Materials Math node with more than two inputs – Jaroslav Jerryno Novotny Feb 15 '18 at 0:14
• @Jerryno, unfortunatelly, that method still lacks of gpu rendering in the viewport. I wrote a patch to fix it, but it didn't went to master. If I had time I could write a better patch for 2.79a, thought i'm aiming for 2.8. – Secrop Feb 15 '18 at 8:43
• @Secrop True, I completely overlooked the GPU rendering requirement.. – Jaroslav Jerryno Novotny Feb 15 '18 at 12:13
• I am sorry, i am quite new here, so would I delete or edit this question ? Because its about the Gpu rendering and viewport problem. – MRL Feb 15 '18 at 12:19

P.S.: Before you read the rest of this answer, check if the ColorRamp node with a constant interpolation suits your needs. It won't work if you need to input a texture, but it is enough for simple colors.

If you don't strictly need to do this in OSL, I suggest you use the Group feature. Basically, a group lets you create a node and define it's behavior using other nodes. It should be relatively simple for the goal that you're trying to achieve.

First, we'll create a bunch of nodes that do just what you need. Then, we'll convert these into a node group, so that you can use this functionality as many times as you want in that Blender file. You will even be able to export this to other .blend files.

Imagine for a moment that you want to switch between two colors. Index 0 = first color; index 1 = second color. You could do it with the following node setup:

Here, we have one number input (the index) and two color inputs. If the index is 0, the first color is output. If the index is anything greater, like 1, the second color is output.

We can also add another color:

Now, whenever the index is larger than 0, there is a choice between color 2 and 3, depending on whether the index is 1 or more.

Notice that we can repeat this as much as we like, to add more colors:

When you have setup this node tree, select all the nodes in it (but not the placeholder color input nodes*!). You can do this easily with circle select (C).

*If you have Node Wrangler installed, you can first add reroutes to all outputs of the index input, that will save you a few worries later. Select the reroute, too.

Now, click Group > Make Group. Your nodes will be packed into a group that you can use wherever you like.

You might have to clean the inputs up a bit, because you only need one index input that is inserted to all the greater than math nodes.

You should now have something like this:

In the tab on the right, you can edit the inputs and outputs, rename them, delete them, etc.

Now, click that arrow-up thingy at the bottom of the screen to get out of the node group.

You will see that the node setup that you selected is replaced by a single node. If you select it, you can rename the node group on the tab at the right side of the screen.

What's important is that you can create more nodes like this by going to Group and clicking [name of node]. In my example, I renamed the node group to "MySwitch":

• Thank you, I will try to script this solution, for my purpose, I would like to have it all automated. – MRL Feb 16 '18 at 12:11