Search code examples
pythonlistmayadeselect

Maya, PYTHON: how do i select all but one in a list?


how do i deselect all objects except for my last selection?

When I'm working with 2 objects there's no problem because all i have to do is toggle list[0] which would be first object that i selected (this is how i have it working below).

import maya.cmds as mc
sel_objs = mc.ls(sl = True)
mc.select(sel_objs[0], tgl = True)

thanks


Solution

  • You can replace the selection entirely with the last element of the list:

    mc.select(sel_objs[-1], replace=True)
    

    replace is the equivalent to clicking on it rather than ctrl-clicking or shift-clicking. The selection is cleared in the action of selecting the new object.

    sel_objs[-1] returns the last object in the list, just as sel_objs[0] returns the first object.