Get locator position using spaceLocator query flag in maya.cmds?

Dumb question guys,
in the documentation it says the position flag is queryable for the spaceLocator() function in maya.cmds

I’m trying this:
print(mc.spaceLocator(a=True, n=sla, p=True, q=True))
variable sla is just a locator that exists in the scene

but I get the error:
TypeError: Flag ‘n’ must be passed a boolean argument when query flag is set #

I don’t get it, do I basically have to use getAttr every time? (or get point position?)

Sorry Im new to cmds and getting a hang of the query flag.

I know its kind of a dumb question ha.

its been a while but try mc.spaceLocator(sla, q=True, p=True) most maya.cmds commands take the thing it acts on as the first argument.

Yeah that gives me no error but does not output anything to stdout, hm. Weird.

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 :slight_smile:


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 :slight_smile: