Search code examples
pythonmaya

Why does my object not go to 0,0,0 when using the xform Maya command


I am using Maya and I would like to move my object to 0,0,0 in world space. I am using the xform command and my object does not move ( based on its centered pivot) to 0,0,0. Here is what I am running:

import maya.cmds as cmds

temp_xforms = [0,0,0]
cmds.xform('Bottle_new',translation = temp_xforms,worldSpace = True)

and here is the result:

enter image description here

and this is what I would expect, and want, to happen:

enter image description here


Solution

  • There's likely a pivot offset on your object and xform doesn't take that into account. You can verify this by selecting your object, going to the Attribute Editor and looking under the Local Space tab. If all values aren't 0 then it has an offset.

    You can get your pivot back to world origin by querying those pivot values then supplying a negative version of them in the xform command.

    It should be something like this:

    import maya.cmds as cmds
    
    pivs = cmds.xform("Bottle_new", q=True, piv=True)[:3]  # Get pivot offset values.
    
    # Multiply all pivot values with -1.
    for i in range(len(pivs)):
        pivs[i] *= -1
    
    cmds.xform("Bottle_new", ws=True, t=pivs)  # Instead of [0, 0, 0] use the pivot values to snap it back to world origin.
    

    Another very cheesy way would be to use matchTransform as it does take pivots into account, but you need another object that it can align to. So you can create a temporary transform at world origin, align to it, then blow away the transform. I believe matchTransform is only available > Maya 2016 though.

    import maya.cmds as cmds
    
    temp_transform = cmds.createNode("transform")  # Create a temporary transform at world origin.
    cmds.matchTransform("Bottle_new", temp_transform, position=True)  # Align your object to the transform.
    cmds.delete(temp_transform)  # Delete the transform.
    

    Alternatively you can avoid all of this completely if you remove any offsets on the object, then selecting the vertices and moving it to the pivot's center. With this there will be no offset, and you can use xform like you originally wanted to center it to the world origin.