[Maya] Querying `xform -objectSpace` translation for an object with rotation values

To obtain the objects position and rotation I’m using [B]xform[/B] MEL command.

Everything is clear and accurate when I’m trying to obtain the rotation of an object with xform in both world and object space.
Also everything is all right when I’m trying to obtain the position (xform -q -ws -t) of an object in world space.

But when I’m trying to query the local position (xform -q -os -t) of an object with rotation values
that are different from zero it (xform) gives me weird result.

Maya help documentation has an extremly brief description about all with regard to space transformations and
nearly zero explanation of how maya actually performs coordinate evaluations (except crookedly drawn transformation matrix).

So I’ve wrote the script in order to understand maya objectSpace realization better:

string $selNodes[] = `ls -sl`;

for ($sel in $selNodes) {
    
    float $wsSelPos[] = `xform -q -ws -t $sel`;
    float $osSelPos[] = `xform -q -os -t $sel`;
      
    print ("ws position: " + $wsSelPos[0]+ " - " + $wsSelPos[1]+ " - " + $wsSelPos[2] + "
");
    print ("os position: " + $osSelPos[0]+ " - " + $osSelPos[1]+ " - " + $osSelPos[2] + "
");
}

I have a parent dummy group (xyz: 2-0-0 world coordinates)
with a poly cube inside it (xyz: 8-0-0 world coordinates).

When cube has zero Y-rotation I can obtain a local position relative to the parent group without any problems
otherwise I’m getting weird local position values for the cube.

It would be a real salvation for me if you will give a detailed explanation for this situation:

Those numbers are consistent with both objects being rotated 45 in the configuration you are showing. Are you sure the parent is zeroed?

You can easily reconstruct this situation:

  1. Create empty group. Move it to x=2, y=0, z=0 (world coordinates)
  2. Create poly cube. Move it to x=8, y=0, z=0 (world coordinates)
  3. Parent the cube to this group
  4. Rotate the cube by +45 degrees along Y axis
  5. Select the cube and type in [I]xform -q -os -t[/I]

You will get not a distance between the cube and the parentGroup which is equal to 6 units
but some weird values. Can you explain what these values are representing?

Because if the cube is not rotated and you will skip the step №4 you will get from [I]xform -q -os -t[/I]
the number 6 which is the distance between this cube and its parent I suppose.

if an object is 6 units from its parent at an angle 45 degrees in the parent’s space, you’d expect it to be at [4.24, 0, 4.24] since the square root of (4.24 squared + 4.24 squared) is 6. That’s what I meant – the numbers represent an object six units from it’s parent rotated 45 degrees in the XZ plane.

That is a bit confusing. I recommend extracting the translation components from the object space matrix (component 41, 42, and 43 in a right handed matrix represent x, y, and z respectively)

So this should yield the result you’re looking for:

cmds.xform('pCube1', matrix=True, os=True, q=True)[12:15]

As for what Theodox is saying, remember that matrix multiplication is NOT communicative. It’s the matrix multiplication order that determines if the transformation is happening in world space or object space.

Men, you are awesome! Now all a bit clearer, but I need time to think about it.
Thanks a lot!