Search code examples
pythonmaya

Get Transform of selected Locator in Maya using CMDS


Using Maya's cmds (python) how can i check if the user has a Locator selected, and then get that locators Transform?

I tried using this, but im not to familiar with maya python...

nodes = cmds.filterExpand(sm=22) # check if user has a locator selected
if not len(nodes) == 1:
     return
# not sure if this is the proper method to use, can i pass in my nodes from the method above like i have written?
tm = cmds.ls(nodes[0], sl=True, transforms=True)
# ... from here i can work on TM assuming it's the transform ex. Locator1

Solution

  • You can use a simple list comprehension to collect all selected transforms that have locator shapes like so:

    [obj for obj in cmds.ls(sl=True) if cmds.listRelatives(obj, shapes=True, type="locator")]
    

    To break it down, cmds.ls(sl=True) will return a list of all selected objects. Then to filter that we can use cmds.listRelatives to check if the transform has any shapes as locators, and if so, collect it.