3
$\begingroup$

I've looked through documentation and many questions here, but couldn't find an answer. Is it possible to create a custom node for cycles with python, which will include inside itself already existing nodes, like Diffuse BSDF and which will return shader as well? It is possible to make a node which handles and returns values or colours, but shader... So the simplest thing I want is to replicate some existing shader, like Diffuse, so that I have a custom node, which takes some values from inputs, do some math, puts new values into Diffuse (like roughness and colour) and outputs it as a usual green socket

$\endgroup$
4
  • 2
    $\begingroup$ if you are doing simple math then use node groups , else go for OSL nodes $\endgroup$
    – Chebhou
    Nov 20, 2016 at 11:15
  • $\begingroup$ Nah..that's the thing. I don't need neither. There're many things involved in shader, so node group will have to many properties. And with python you can exclude some nodes from computation, when you don't need them and thus dencrease render time (if you have multiple Mix Shaders, but in some occasionts you want to set them exactly to 0 or 1 and do not compute other brach of mix). OSL doesn't suit, because it's CPU only $\endgroup$ Nov 20, 2016 at 11:22
  • $\begingroup$ vimeo.com/90065481 this is an old one but I don't know what is the situation today though $\endgroup$
    – Chebhou
    Nov 20, 2016 at 11:27
  • $\begingroup$ It's still aplicable, but as I've said, there's no such problem with values and colors. The problem is that shader socket has no default_value and I can't find documentation about what syntax to use to transfer shader within custom node $\endgroup$ Nov 20, 2016 at 12:22

1 Answer 1

2
$\begingroup$

As far as I understand your question, node groups should work just fine. You can define exactly what parameters you want as input, and if a node has a 0 weight in a mix shader, then it will not be evaluated.

EDIT The manual says clearly that you don't need to worry about Mix Shader nodes performance.

When executing shaders, a special optimization is applied to Mix Shader nodes. If Factor evaluates to 0 or 1, any nodes that are only reachable via the unused branch of the mix are not evaluated.

This can substantially reduce the performance cost of combining multiple materials in one shader with vertex color, texture, or other input used as a switch.

Also Python code can control the material properties inside the node tree, and for this Python code you can create your own Python panel: Control Cycles material nodes and material properties in Python

$\endgroup$
10
  • $\begingroup$ Node groups will do, that's true. What I'm looking for is UI improvement. Custom node groups provide exactly that. Imagine that you have 3 main node groups, each has inputs and three shader outputs (For that you can exclude some features from computation). To get final shader you have to connect them which results in 27! possible shaders. Now imagine how inconvinient it would look if you'd wanted to comdine them all in one master group While using python's logic you can have one outut for master shader, which is one of those 27, depending on inputs $\endgroup$ Nov 20, 2016 at 14:15
  • $\begingroup$ I have a feeling that if a node was running Python code, then it would slow down the material evaluation much more than mix shaders, and I also don't think that Python code can run on GPU in Blender. $\endgroup$
    – lbalazscs
    Nov 20, 2016 at 15:18
  • $\begingroup$ I would assume exactly opposite:) Shader is constructed through python anyway, while nodes just serve as a visual representation of code, thus using python math would be faster, then using math nodes in Cycles, as an example As for GPU, there's nothing CPU specific in code I'm writing, it's just interface. I don't want to write new shader, which actually changes traced rays. I just want to use already existing shader, like Diffuse, but with more complex interface $\endgroup$ Nov 20, 2016 at 15:37
  • $\begingroup$ I edited the answer to explain why you should not worry about Mix Shader nodes performance. Basically you can do everything with node groups, and the performance will be fine. Also you can express any logic using nodes only. BTW certainly no Python is running during render time, because that would be very slow indeed. $\endgroup$
    – lbalazscs
    Nov 20, 2016 at 16:43
  • 1
    $\begingroup$ Blender could be improved in many ways, so this is a great opportunity for you to fight for your dreams and contribute the necessary code to improve node groups... But - as far as I understand - this would have to be C++ code. $\endgroup$
    – lbalazscs
    Nov 20, 2016 at 17:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .