3
$\begingroup$

When outputting values for a mathutils.Vector and mathutils.Matrix, there's normally only 4 decimals printed out. Is there a way to output more decimals or would I have to do it manually like this?

print("%.7f, %.7f, %.7f" % (myVector.x, myVector.y, myVector.z))
$\endgroup$
4
$\begingroup$

str and repr

What you see in a print statement is the classes __str__ method, or how to represent the instance as a string, which as mentioned prints to 4 decimal places.

>>> str(C.object.location)
'<Vector (0.0000, 1.6400, 0.0000)>'

The representation, defined on class as def __repr__(self) result

>>> repr(C.object.location)
'Vector((0.0, 1.6399999856948853, 0.0))'

Gives us the values we would get from printing individuals.

>>> C.object.location.y
1.6399999856948853

and is what you see for result echoed in the python console.

>>> C.object.location
Vector((0.0, 1.6399999856948853, 0.0))

Similarly for matrix

>>> str(C.object.matrix_world)
'<Matrix 4x4 ( 0.9893, 0.0000, 0.0000, 0.0000)\n            ( 0.0000, 1.0000, 0.0000, 1.6400)\n            (-0.1461, 0.0000, 0.0000, 0.0000)\n            ( 0.0000, 0.0000, 0.0000, 1.0000)>'

>>> repr(C.object.matrix_world)
'Matrix(((0.9892723560333252, 0.0, 0.0, 0.0),\n        (0.0, 1.0, 0.0, 1.6399999856948853),\n        (-0.1460830420255661, 0.0, 0.0, 0.0),\n        (0.0, 0.0, 0.0, 1.0)))'

>>> print(repr(C.object.matrix_world))
Matrix(((0.9892723560333252, 0.0, 0.0, 0.0),
        (0.0, 1.0, 0.0, 1.6399999856948853),
        (-0.1460830420255661, 0.0, 0.0, 0.0),
        (0.0, 0.0, 0.0, 1.0)))

To have 7 decimal places for each the suggestion in question is a good as any.

>>> print("%.7f, %.7f, %.7f" % C.object.location[:])
0.0000000, 1.6400000, 0.0000000
$\endgroup$
1
  • 1
    $\begingroup$ Thanks a lot @batFINGER! $\endgroup$ Mar 27 '20 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.