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