Condition Node (If/Else Statement) for Cycles?

Hi,

Is there a condition node (if/else statement) for Cycles?

What I have so far:

  1. There is an existing OSL node for it but OSL works only on CPU.
  2. There are a comprehensive reply on this thread, but correct me if I’m wrong there is no If/Else Statement solution
  3. There is a commercial product here which might be what I’m looking for but it requires a monthly subscription fee. I don’t mind paying but I prefer not to have monthly fees.

Anyone have better solution?

Thank you

MixRGB set to ‘Mix’. If ‘fac’=0, output color1; if ‘fac’=1, output color2.
The same is possible for floats, using Math nodes: output = val1 * fac + val0 * (1-fac).
And shaders go with MixShaders.

2 Likes

I now use map range to do floating point linear interpolation (fLerp). I think it’s odd that we don’t have a simple Lerp function in the math node, with nothing of the fancy stuff map range offers.

1 Like

what about the math compare function ?

it is a if test !

happy bl

1 Like

Thank you all for the response.

@Secrop

I am not sure if I’m doing it right. It gives me a syntax error.
You can check an attempt below: I tried it twice: one with the "fac" and the other only fac
dl.dropbox.com/s/v7qmv3sfb0d3qre/blen48_if_else_in_cycles_node_editor.mp4

@CarlG

I’m not entirely sure how to use map-range since I require a hard 0-1 as an output rather than having float values (i.e. 0.1, 0.2)

@RickyBlender

You are right. They are. Thanks for pointing them out.
They work as expected but do you know how to use a hard “compare”
If X = 5, output 1. Or something.
Currently, it only has Greater than or Less Than.
So I can only perform either If X > 5 or if X <5.

dl.dropbox.com/s/9cxpkvmnp4xq7zi/blen48_if_else_in_cycles_node_editor02.jpg

BIS library is absolutelly free. All materiasl are available for everyone. If you don’t want to support the project, you need to pay nothing. It’s free.

2 Likes

In Blender, you can insert certain python expressions directly in Float property boxes, but they must result in a (constant) numerical value.

Blender parses the input expression and turns the result into a value on the fly.
The expression, however, will be gone as soon you press enter, as the property type (float, integer, etc) is the only thing that remains.

You can, for example, type sqrt(2)+4*pi or even 3.0 if 2 >= 4 else 5.0 (python for “if (2>=4){output=3.0;} else {output=5.0;}”) and it’s ok because those evaluates to a number (respectively 13.981 and 5.0)… But you cannot type something that uses a variable (only known constants are allowed).

When I wrote output = val1 * fac + val0 * (1-fac) this is what I meant:

It’s possible to use expressions with variables that are updated on scene changes, but you need to use ‘Drivers’ for that.

What is the best solution depends in what you’re looking for, exactly…
What are your conditions? what are your variables? what is the desired output?

Edited: BTW, what RickyBlender refers to ‘Compare’, it’s a new operation in the Math Node, that is only available in latest builds of Blender (prior to 07/12/2019).

1 Like

@Korchy

Thanks for the clarification. I might have read it wrong

@Secrop

Thanks for the response. Yea I guess you are right. If I needed to have complicated conditions and variables, I should probably use Drivers.

=======

Thank you all. Will consider this one as closed.