Would be that a math bug? Or there is some reason for that?

Hi there, I am trying to do a math operation, which is returning the following error:

Error: ValueError: file line 50: math domain error

line 50 is: angle = math.acos(upRotated * P_V)

I don´t know why sometimes it happens when I change very very small values in an axis which doesn´t change the direction of the triangle built by the position of the 3 locators.

This is my code:

def getMDagPath(name):
        dag = om.MDagPath()
        sel = om.MSelectionList()
        sel.getDagPath(0, dag)
        return dag

    def aimConstr(lct_, lct_A, lct_U):

        vAim = om.MVector().xAxis
        vUp = om.MVector().yAxis

        P_dag = getMDagPath(lct_)
        A_dag = getMDagPath(lct_A)
        U_dag = getMDagPath(lct_U)

        transformFn = om.MFnTransform(P_dag)
        P_Pos = transformFn.rotatePivot(om.MSpace.kWorld)

        A_Pos = transformFn.rotatePivot(om.MSpace.kWorld)

        U_Pos = transformFn.rotatePivot(om.MSpace.kWorld)

        aimVector = (A_Pos - P_Pos)

        upVector = (U_Pos - P_Pos)

        P_U = aimVector.normal()
        P_V = upVector
        P_W = (P_U ^ P_V).normal()

        P_V = P_W ^ P_U

        quaternionU = om.MQuaternion(vAim, P_U)
        quaternion = quaternionU

        upRotated = vUp.rotateBy(quaternion)
        angle = math.acos(upRotated * P_V)

aimConstr("e", "t", "tup")

I have 3 locators: “e”, “t” and “tup”

the position of “e”: [-0.5, 0.0, 0.5]
the position of “t”: [-0.5, 0.5, 0.5]
the position of “tup”: [thisValueIsGettingTheErrorWhenIChange, 0.0, 0.5]

0.495 works!
0.497 doesn´t work… :frowning:
0.498 works!
0.499 doesn´t work… :frowning:
0.6180000000000001 work!
0.618 doesn´t work… :frowning:

Does somebody know how to explain this?

thanks a lot!

You’re doing a acos which I think does not work if upRotated * P_V > 1. Maybe if you normalize P_V?

1 Like

hi thodox!
thanks for replying! is exaclty what you said.
I did a clamp min max in the value, and worked fine!