Hmm, from the docs, it definitely looks like you should be able to do that, but I can confirm that it’s not working how I expect.
That said, probably the two ways I see most often of getting an object’s position are: print mc.xform(sla, query=True, translation=True) print mc.getAttr(sla + '.translate')
I tend to prefer getAttr, but if I have to deal with space switching, or I’m doing other stuff with pivots, or if I’m feeling particularly contrary at the time I might use xform.
On a side note: Since you’re just starting, now is the perfect time to get into the habit of using the full flag names. I’m fairly certain that my dying breath will be to complain about someone using the short flags
Thanks Tfox,
Just wanted to make sure I wasn’t missing something obvious. Yeah I’ll be sure to use the full flag names next time, best to make it a habit.
The spaceLocator command expects a locator node when querying, ie. the shape node of the created locator. If you pass the locator node to the command, it works as expected.
sla = mc.spaceLocator()[0] # returns a list, get the first index
sla_shp = mc.listRelatives(sla, shapes=True, path=True)[0] # returns a list, get the first index
pos = mc.spaceLocator(sla_shp, query=True, position=True)
What is messed up is that this returns a string! If you need something useful, you’ll need to make this into floats.
pos_floats = [float(x) for x in pos.split()]
# or
x, y, z = [float(x) for x in pos.split()]
But if you have the shape node, you can always do as tfox_TD mentioned and just use getAttr. This returns a one item list that contains a 3 item tuple (the x, y, z values).
pos = mc.getAttr('{}.worldPosition'.format(sla_shp))[0] # returns a list, get the first index
# or
x, y, z = mc.getAttr('{}.worldPosition'.format(sla_shp))[0]
thank you!! yeah I’m just conceptually trying to understand the api and the cmds module in maya. Even though I too would normally use xform or getAttr… I wanted to know why I couldn’t get this specific command to work right