Search code examples
pythonmaya

Querying a shading node Maya Python


I am currently having a problem where i want to query the 'inputX' of a multiplyDivide Node in maya and put the queried number into the 'inputX' of another multiplyDivide node.

The script currently makes an stretchy IK set up for an arm. Using a distanceBetween the shoulder and the wrist (at a certain point, which is what i want to query) the bones would then stretch. So obviously, I don't want to connect the two together.

def stretchyIK(firstJointStore, lastJointStore, side, limb):

    GlobalMoveRig = cmds.rename ('GlobalMove_Grp_01')
    locFirstJoint = cmds.spaceLocator (n='Loc_' + firstJointStore + '_0#')
    locLastJoint = cmds.spaceLocator (n='Loc_' + lastJointStore + '_0#')
    pointLoc1 = cmds.pointConstraint (side + '_Fk_' + firstJointStore + suffix, locFirstJoint)
    pointLoc2 = cmds.pointConstraint (side + '_Fk_' + lastJointStore  + suffix, locLastJoint)
    cmds.delete (pointLoc1, pointLoc2)
    cmds.pointConstraint (side + '_FK_' + firstJointStore + suffix, locFirstJoint)
    cmds.pointConstraint (ikCtr, locLastJoint)
    cmds.parent (locFirstJoint, locLastJoint, 'Do_Not_Touch')
    #Creating Nodes for Stretchy IK
    IkStretch_DisNode = cmds.shadingNode ('distanceBetween', asUtility=True, n='DistBet_IkStretch_' + side + limb + '_#')
    cmds.connectAttr (locFirstJoint[0] + '.translate', IkStretch_DisNode + '.point1')
    cmds.connectAttr (locLastJoint[0] + '.translate', IkStretch_DisNode + '.point2')
    IkStretch_DivNode = cmds.shadingNode ('multiplyDivide', asUtility=True, n='Div_IkStretch_' + side + limb + '_#')
    cmds.setAttr (IkStretch_DivNode + '.operation', 2)
    input = cmds.connectAttr (IkStretch_DisNode + '.distance', IkStretch_DivNode + '.input1.input1X')   ########HELP NEEDED HERE
    cmds.setAttr (ikCtr + '.translateX', 2)
    IkStretch_MultNode = cmds.shadingNode ('multiplyDivide', asUtility=True, n='Mult_IkStretch_' + side + limb + '_#')
    cmds.setAttr (IkStretch_MultNode + '.input1X', IkStretch_DivNode + '.input1.input1X')#WAIT FOR PAUL
    cmds.connectAttr (GlobalMoveRig + '.scaleY', IkStretch_MultNode + '.input2X')
    cmds.connectAttr (IkStretch_MultNode + '.outputX', IkStretch_DivNode + '.input2X')
    IkStretch_Cond_Equ = cmds.shadingNode ('condition',  asUtility=True, n='Cond_Equ_IkStretch_' + side + limb + '_#')
    IkStretch_Cond_GrtEqu = cmds.shadingNode ('condition',  asUtility=True, n='Cond_GrtEqu_IkStretch_' + side + limb + '_#')
    cmds.setAttr (IkStretch_Cond_GrtEqu + '.operation', 3)
    cmds.connectAttr (ikCtr + '.Enable', IkStretch_Cond_Equ + '.firstTerm')
    cmds.setAttr (IkStretch_Cond_Equ + '.secondTerm', 1)
    cmds.connectAttr (IkStretch_DisNode + '.distance', IkStretch_Cond_GrtEqu + '.firstTerm')
    cmds.connectAttr (IkStretch_MultNode + '.outputX', IkStretch_Cond_GrtEqu + '.secondTerm')
    cmds.connectAttr (IkStretch_DivNode + '.outputX', IkStretch_Cond_GrtEqu + '.colorIfTrueR')
    cmds.connectAttr (IkStretch_Cond_GrtEqu + '.outColorR', IkStretch_Cond_Equ + '.colorIfTrueR')
    cmds.connectAttr (IkStretch_Cond_GrtEqu + '.outColorR', side + '_Ik_' + secondJointStore + suffix + '.scaleX')
    cmds.connectAttr (IkStretch_Cond_GrtEqu + '.outColorR', side + '_Ik_' + firstJointStore + suffix + '.scaleX')

Solution

  • Yes, your error makes perfect sense... The attribute you're looking for is actually just '.input1X' rather than '.input1.input1X'.

    I know, that isn't very clear, but you'll know in the future. An easy way of figuring out stuff like this, by the way, is manually connecting stuff in Maya and seeing the MEL output in the script editor. You'll get the real deal every time, and translating that stuff to Python afterwards is quick.

    So:

    cmds.connectAttr(IkStretch_DisNode + '.distance', IkStretch_DivNode + '.input1X') 
    

    By the way, I'm not sure why you were assigning the result to input. I admit I'm not sure what that would return, but I don't see how it could be any useful!

    Additionally: To answer your direct question, you can use getattr to query the value.

    cmds.setAttr(
        IkStretch_MultNode + '.input1X', 
        cmds.getattr(IkStretch_DivNode + '.input1X')
    )