Search code examples
pythonmayamelpymel

PyMel Maya Python - Set Joint location


I am very new to PyMel (Done Python before) and I found the documentation available very complicated, and in times simply being still MEL and not Python. Very frustrating to do anything here.

I am looking for a way to set the absolute location of one of my joints, as well as the orientation of its bone.

The joint I want to manipulate is found this way:

theJoint = ls("LeftArm")[0]

Now using

theJoint.setAngleX(45)

I am able to set its rotation relative to the parent. But what I need to do is set the position in world space absolute coordinates, as well as the rotation.

There arent any setPositionX() or setTransform(X) methods for me to use, so I am wondering if I am even in the right place.

How do I go about this task, and more importantly: Where would I actually start looking for correct and useful command libraries? Its very frustrating to do this without working examples or small demos always relying on the context selected object, which is something I cannot do.


Solution

  • When you set the attribute using pymels attribute objects (syntaxt like mynode.rotate.set() you're poking numbers directly into the attributes; but the numbers are just numbers, getting interpreteed downstream as if you had typed them into the channel box.

    You can use Pymel's versions of the xform, move and rotate commands to set absolute values. These commands work the same way as the ones in the regular Maya docs. This. for example, will rotate Joint2 to 0,0,10 in world space, regardless of what it's parent is doing:

    import pymel.core as pm
    example = pm.PyNode('joint2')
    pm.rotate(example, [0,0,10], a=True, ws=True) 
    

    The a flag is 'absolute' and the ws is 'worldspace' (docs here for rotate). move and xform have options for absolute and worldspace values as well. So to position a joint in worldspace it'd be

     pm.move(example, [1,2,3], a=True, ws=True)
    

    xform is a bit more complicated and has more options but does the same things as well as allowing you to set pivots.