0

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

winteralfs
  • 459
  • 6
  • 25

1 Answers1

2

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.

Green Cell
  • 4,677
  • 2
  • 18
  • 49
  • So when I “center pivot’ on the object, and the transform widget cross hair is where I would expect, object center, and then I do a python xform, shouldn’t the object move as expected? I noticed this problem happens after I “freeze transforms” on the object. before running that, xform moves the object as expected in world space, after running it, the object wont move as expected. Why would the pivot being at 0,0,0 fix the issue, wouldnt you want the pivot at the center of the object, thus having world space offsets, and then you just tell it coords in world space and the object moves there? – winteralfs Jul 11 '19 at 05:48
  • 1
    Like I said, the crux of the problem is that `xform` doesn't take pivot offsets into account so you get that offset when trying to snap back to 0, 0, 0. The thing about freeze transforms is that it doesn't straight up zero out your translation values, instead it makes them 0, 0, 0 but also stuffs the values into the Local Space attributes. Essentially it zeros your values by making them offsets. Center pivot does the same. You don't have to remove these pivots though, the first script should work with your offsets to snap the pivot to 0, 0, 0. – Green Cell Jul 11 '19 at 07:12
  • 1
    You can also run this: `cmds.xform("Bottle_new", q=True, piv=True)[:3]`, if it returns any value that isn't 0 it means the object's pivot was moved! 'Freeze transforms' and 'Center pivot' will cause this. – Green Cell Jul 11 '19 at 07:17
  • Thank you for all the help, out of curiosity, what does the Maya command, 'makeIdentity' do, and how would you use it? I saw it come up in searches when I was Googling my issue? Thanks again! – winteralfs Jul 11 '19 at 13:03
  • 1
    `makeIdentity` is the command that is called when you click 'Freeze transformations'. The documentation gives some pretty good examples at the end: http://help.autodesk.com/cloudhelp/2018/ENU/Maya-Tech-Docs/CommandsPython/makeIdentity.html – Green Cell Jul 11 '19 at 13:39
  • lastly, what if I wanted to move one object to another’s position, not world space 0,0,0. I believe the concepts you explained would be the same, but maybe slightly more complex math? I would still have to negate any offsets on obj 1 before xforming it to the world space coords of obj 2? – winteralfs Jul 11 '19 at 13:42
  • 1
    I believe you would get the target's world position with `xform` then negate it with the obj1's pivots. Though if you have 2016 or higher just use `matchTransform` and it will handle the alignment properly, even if they both have pivot offsets. Unfortunately Maya does an awful job freezing transforms unlike other softwares. – Green Cell Jul 11 '19 at 15:38