atan2 equivalent in Cycles nodes?

The Cycle's math node has an "Inverse tangent" option. This function however cannot discriminate between two opposite points on the circle, as they have the same tangent. Usually, this is solved with a function called atan2, which receives two inputs, the x and y of the vector, and returns the angle. Can I construct something similar in Cycle's nodes?

EDIT July 2018

If you are using Blender 2.8 or a recent build of 2.79, the math node now has the atan2 operation since this commit by Lukas Stockner.

This is in no way the most efficient way of doing it, but just a reminder that knowing a bit of math, you can painstakingly craft functions using NodeGroups.

Things to know:

• The Maximum Math node acts as an OR boolean operation.
• The Minimum Math node acts as an AND boolean operation.
• The Substract Math node acts as a NOT boolean operation with 1 as first input and your value as second.
• The Mix Colors node acts as a switch with a boolean as first input, which returns second input if condition == False, or third input if condition == True.

http://en.wikipedia.org/wiki/Atan2

http://en.wikipedia.org/wiki/De_Morgan%27s_law

• Thanks! I was aware of this method, but couldn't imagine it's the simplest way... If no one else answers in a few days I'll accept it as the answer. – olamundo Jul 4 '14 at 13:11
• From what I gather, this is the only method, except modifying the source code... – Pisurquatre Jul 4 '14 at 15:11
• Good job, but I have tried this setup and it fails to identify that y = 0 (the greater than 0 OR less than 0, negated). I replaced it with a more pragmatic "less than 0.00001" after an absolute node and it worked. Also, the switch should produce PI, not 0 (black). – Watcom Jul 30 '15 at 0:27

The simplest way would be an OSL script. Drawback is no GPU suport.

#include "stdosl.h"

float X = 0.0,
float Y = 0.0,

output float result = 0.0 )
{
result = atan2(X,Y);
}


Paste that into a text block and select it in a script node.

• I like this, but when I try it, the node puts out only a constant. Is this source correct? – DarenW May 23 '15 at 21:54
• Found the trouble! There's a secret checkbox in the render settings which one must turn on, and is never mentioned in any tutorial/intro that I've seen so far. Thanks to Blender S.E. blender.stackexchange.com/a/20876/606 – DarenW May 24 '15 at 1:28

Very old question, I know. However, here's an alternative (and simpler) node-based solution to produce the atan2(x,y) function.

The difference between 'arctangent' and 'atan2' is that the 'arctangent' only provides unique values for one half of the rotation - the other have returns duplicate results. In order to extract the second half of the rotation (as is available from atan2(X,Y)) we need to use the sign of one of the input values (in this case X) to determine when we are in the second half of the full rotation.

The Less Than node detects the change in sign while the Multiply changes the 0.0/1.0 output of the Less Than into either 0.0 or 'pi' (which represents a 'half turn' (180 degrees) since we're working in radians rather than degrees). This is then added into the result of arctangent(Y/X) and, finally, adjusted by a quarter of a turn (pi/2 radians) to match the output from 'atan2(...)'.

In order to test this I compared it to the OSL example provided by @sambler to prove that it produces the same result as the OSL 'atan2(X,Y)' function.

• It is indeed much simpler! – Pisurquatre Jul 24 '18 at 21:11
• @Pisurquatre I notice it’s now even easier in the latest build of Blender (2.79.5) as there is an actual atan2 operation on the Maths node! – Rich Sedman Jul 24 '18 at 21:27
• That's actually why I came back to this question ;) (see my other answer below). I didn't know however that is was backported to 2.79. – Pisurquatre Jul 24 '18 at 21:29