Question

I am trying to create 2 buttons, prev and next, but instead of selecting the item before or after it in the Maya outliner, I want to select the next or previous item which has children.

To make this clearer: In the image below, if I have selected grpA_ctrl02, while executing these 2 buttons it will either select grpA_ctrl01 or grpA_ctrl03, since both of these have child items

How can this be done?

enter image description here

Was it helpful?

Solution

You want to use the pickWalk command.

while True:
    cmds.pickWalk(d='left')
    children = cmds.listRelatives(cmds.ls(sl=True), children=True, typ='transform')
    if children:
        break

All of this is inside a while True, to keep it looping until it finds a valid node.

  • First, it walks one node to the left.
  • Then it lists child transforms
  • If there are children, it matches the requirement, so break
  • Otherwise, do all this again, walking one further to the left

For the other direction, do the same thing but with pickWalk(d='right')

Documentation:
pickWalk
listRelatives

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top