Search code examples
pythonmaya

Why does print with and without parentheses give __str__() and __repr__() in


I am trying to accomplish some rigging tasks in Autodesk's Maya (2015) using Python. I have run into something I think is odd:

for source, destination in SCENE.Left_BallFoot_orientConstraint.connections(c=1,p=1):
    print source, destination

Gives the following output, the __str__() of the Attributes:

Left_BallFoot_orientConstraint.constraintRotateX Left_BallFoot.rotateX
Left_BallFoot_orientConstraint.constraintRotateY Left_BallFoot.rotateY
Left_BallFoot_orientConstraint.constraintRotateZ Left_BallFoot.rotateZ
Left_BallFoot_orientConstraint.constraintRotateOrder Left_BallFoot.rotateOrder
Left_BallFoot_orientConstraint.constraintParentInverseMatrix Left_BallFoot.parentInverseMatrix[0]
Left_BallFoot_orientConstraint.constraintJointOrient Left_BallFoot.jointOrient
Left_BallFoot_orientConstraint.target[0].targetRotate Left_Toe_Control.rotate
Left_BallFoot_orientConstraint.target[0].targetRotateOrder Left_Toe_Control.rotateOrder
Left_BallFoot_orientConstraint.target[0].targetParentMatrix Left_Toe_Control.parentMatrix[0]
Left_BallFoot_orientConstraint.target[0].targetJointOrient Left_Toe_Control.jointOrient
Left_BallFoot_orientConstraint.target[0].targetWeight Left_BallFoot_orientConstraint.Left_ToeControlW0
Left_BallFoot_orientConstraint.Left_ToeControlW0 Left_BallFoot_orientConstraint.target[0].targetWeight

But with parentheses around the print statement's arguments:

for source, destination in SCENE.Left_BallFoot_orientConstraint.connections(c=1,p=1):
    print(source, destination)

The resulting output is this, the __repr__() return value:

(Attribute(u'Left_BallFoot_orientConstraint.constraintRotateX'), Attribute(u'Left_BallFoot.rotateX'))
(Attribute(u'Left_BallFoot_orientConstraint.constraintRotateY'), Attribute(u'Left_BallFoot.rotateY'))
(Attribute(u'Left_BallFoot_orientConstraint.constraintRotateZ'), Attribute(u'Left_BallFoot.rotateZ'))
(Attribute(u'Left_BallFoot_orientConstraint.constraintRotateOrder'), Attribute(u'Left_BallFoot.rotateOrder'))
(Attribute(u'Left_BallFoot_orientConstraint.constraintParentInverseMatrix'), Attribute(u'Left_BallFoot.parentInverseMatrix[0]'))
(Attribute(u'Left_BallFoot_orientConstraint.constraintJointOrient'), Attribute(u'Left_BallFoot.jointOrient'))
(Attribute(u'Left_BallFoot_orientConstraint.target[0].targetRotate'), Attribute(u'Left_Toe_Control.rotate'))
(Attribute(u'Left_BallFoot_orientConstraint.target[0].targetRotateOrder'), Attribute(u'Left_Toe_Control.rotateOrder'))
(Attribute(u'Left_BallFoot_orientConstraint.target[0].targetParentMatrix'), Attribute(u'Left_Toe_Control.parentMatrix[0]'))
(Attribute(u'Left_BallFoot_orientConstraint.target[0].targetJointOrient'), Attribute(u'Left_Toe_Control.jointOrient'))
(Attribute(u'Left_BallFoot_orientConstraint.target[0].targetWeight'), Attribute(u'Left_BallFoot_orientConstraint.Left_ToeControlW0'))
(Attribute(u'Left_BallFoot_orientConstraint.Left_ToeControlW0'), Attribute(u'Left_BallFoot_orientConstraint.target[0].targetWeight'))

Solution

  • Even though it looks like you're making a function call, you are not. In python 2, print is a keyword. So your 2nd example is really:

    print (source, destination)
    

    i.e. you are printing a tuple. So, what you are getting is actually the str of the tuple, which shows the repr of its parts.

    In recent versions of python, you can get the print function in python 2 for compatibility, by using

    from __future__ import print_function
    

    Then this will do what you expected (but your first example becomes invalid).