 # Math Node Maya with Python script

Hi,
i am using a math node from here https://apps.autodesk.com/MAYA/en/Detail/Index?id=212304226072872821&appLang=en&os=Win64

what i am trying to achieve is to implement this Python script with this math node,
I put here the parenthesis of (a) , (b), (c ) and (result), so these are the equivelant to MathNodes variables and result

``````import maya.cmds as mc
import math

previous_driver_angle(a) = 0.
previous_driven_angle(b) = 0.

def testFunction():
global previous_driver_angle, previous_driven_angle

next_driver_angle(c) = mc.getAttr('driver.rotateX')
next_driven_angle(result) = max(math.floor(previous_driven_angle / 60. + 10 ** -6) * 60.,
previous_driven_angle + (next_driver_angle - previous_driver_angle) * 60. / 55.)
mc.setAttr('driven.rotateZ', next_driven_angle)
previous_driver_angle = next_driver_angle
previous_driven_angle = next_driven_angle
``````

So in the math node i am writing in the Expression tab this:

``````[(b / 60. + 10 ** -6) * 60., b + (c - a) * 60. / 55.)]
a = 0
b = 0
c =  (driver.rotateX)
``````

I connect driver’s rotate X with c and then i connect the result of MathNode to driven.rotateZ.

And this is the error i get:

// Error: SyntaxError: file
C:/ProgramData/Autodesk/ApplicationPlugins/MathNode/Contents/plug-ins/asdkMathNode.py line 82: invalid syntax //
Expression [(b / 60. + 10 ** -6) * 60., b + (c - a) * 60. / 55.)] failed// Error: SyntaxError: file C:/ProgramData/Autodesk/ApplicationPlugins/MathNode/Contents/plug-ins/asdkMathNode.py line 82: invalid syntax //
Expression [(b / 60. + 10 ** -6) * 60., b + (c - a) * 60. / 55.)] failed

Any idea would be appreciated thank you

The trailing closing parentheses doesn’t have a pair (no paired open parentheses).

Using a good IDE will help identify issues like these straight away (PyCharm for example).

Thank you for your reply i remove the xtra parenthesis now the expression is like this:

`````` (b / 60. + 10 ** -6) * 60., b + (c - a) * 60. / 55.
``````

but i have this erros now:

`// Error: TypeError: file C:/ProgramData/Autodesk/ApplicationPlugins/MathNode/Contents/plug-ins/asdkMathNode.py line 88: in method 'MDataHandle_setFloat', argument 2 of type 'float' //`

Also when i am trying to add this: max(math.floor to expression it pops an error that math is not defined

If you’re getting a NameError that math is not defined then you need to import it.

`import math`

Yes i know
now i get this:
// Error: SyntaxError: file C:/ProgramData/Autodesk/ApplicationPlugins/MathNode/Contents/plug-
ins/asdkMathNode.py line 82: invalid syntax //
Expression import math max(math.floor(b / 60. + 10 ** -6) * 60., b + (c - a) * 60. / 55.) failed

Is the import on a separate line?
Without formatting it’s difficult to see what the formatting issue could be…

No its not on another line,
it doesnt allow me to press enter in the math node expression slot.
everything is in one line
`import math max(math.floor(b / 60. + 10 ** -6) * 60., b + (c - a) * 60. / 55.)`

``````import math
max(
math.floor(b / 60.0 + 10 ** -6) * 60.0,
b + (c - a) * 60.0 / 55.0
)
``````

If that’s the case you can use semi-colon.
`import math;max(math.floor(b / 60. + 10 ** -6) * 60., b + (c - a) * 60. / 55.)`
Although I’m not sure this is a good idea. Not at a computer ATM so I can’t suggest an alternative.

No worries,
although i still have this error:
`Expression import math;max(math.floor(b / 60. + 10 ** -6) * 60., b + (c - a) * 60. / 55.) failed// Error: SyntaxError: file C:/ProgramData/Autodesk/ApplicationPlugins/MathNode/Contents/plug-ins/asdkMathNode.py line 82: invalid syntax //`

even with the semicolons

Last ditch effort, try with trailing numbers for the floats.
60.0

`Expression import math;max(math.floor(b / 60.0 + 10 ** -6) * 60.0, b + (c - a) * 60.0 / 55.0) failed// Error: SyntaxError: file C:/ProgramData/Autodesk/ApplicationPlugins/MathNode/Contents/plug-ins/asdkMathNode.py line 82: invalid syntax //`

No luck unfortunatelly `max(((b / 60.0 + 10 ** -6) // 1) * 60.0, b + (c - a) * 60. 0 / 55.0)`

Try with the floordiv operator //

At the beginning with a fresh node everything works fine.
i even put manually numbers on c attr, (no error).
But, when i connect the result output to a rotate Z of a group lets say, then i have the error syntax.

Expression max(((b / 60.0 + 10 ** -6) // 1) * 60.0, b + (c - a) * 60. 0 / 55.0) failedExpression max(((b / 60.0 + 10 ** -6) // 1) * 60.0, b + (c - a) * 60. 0 / 55.0) failed// Error: SyntaxError: file C:/ProgramData/Autodesk/ApplicationPlugins/MathNode/Contents/plug-ins/asdkMathNode.py line 82: invalid syntax //

My mistake, this line is working:
`max(((b / 60.+ 10 ** -6) // 1) * 60., b + (c - a) * 60. / 55.)`

but when i want to complete the first python script by adding these two lines:
previous_driver_angle = next_driver_angle
previous_driven_angle = next_driven_angle

`max(((b / 60.+ 10 ** -6) // 1) * 60., b + (c - a) * 60. / 55.);(a = c); (b= result);`

then i have again error syntax

The python script and a mel expression are very different things, so you can’t often have a direct translation.
I didn’t pay as much attention to what you were actually trying to solve as I should have, leading to this long thread.

Writing it as an expression should be possible.
I would suggest taking a look at some learning material for writing expressions in Maya otherwise I imagine a significantly longer thread unless the answer is posted You should be able to find the previous value through caching, or perhaps getAttr frame -1 (not recommended during expressions as it’ll affect performance).
You should also be able to use the frameCache node to get attr value from a previous frame.

Should i use the frame cache node and the Expressions?

Using the frameCache node, you should be able to implement with nodes instead of an expression I think?

Once I’m at a computer I’ll be in a much better position to provide something other than semi-thought out advice.
That being said, there are generally much smarter and more experienced people than myself around who might point you in a better direction whilst you wait 