MEL for finding a prefix in hierarchy


#1

Hello everyone,
this should be a simple one, but I just can’t figure out.
my hierarchy looks like this image. 1025
what I want to do is when I select the bottom pSphere1 and run script
can select the parent locator which with layout_ prefix.
The top 2 locator won’t change but the geo may have different grouping by case.

my thought was maybe use listRelatives command and conditions but no luck…

I am total newbie in scripting, any tips would help, thanks!


#2

I’m not too familiar with MEL, but I can give it to you in Python and maybe you can convert the logic behind it into mel syntax (or just use the python script)

def selectLayout():
    hierarchy = cmds.ls(sl=1, l=1)[0].split('|')
    for item in hierarchy:
        if 'layout' in item:
            return item
        
cmds.select(selectLayout(), r=1)

First I list the long name of my selection, which gives a full path separated by |. I convert the string to a list by splitting the string up at the |
# Result: u'|layout_myObject_LOC|anim_myObject_LOC|group4|group3|pSphere1' #
Then I iterate through every item in the list and if it contains the text ‘layout’, it will select it.

Hope this helps!


#3

Hi al_ka,

Thank you very much for your reply! and I tested the code it works! however there is a small issue.
when my hierarchy look like this
1025_2
so there are multiple layout locators in this case, the script always select the top one, is there anyway to make sure the “layout_myObject_LOC” been selected?

thanks again for the help, much appreciated!


#4

Quick fix to this that should work, although it’s hard to say without knowing how all your hierarchies are set up.

def selectLayout():
    hierarchy = cmds.ls(sl=1, l=1)[0].split('|')
    for item in hierarchy[::-1]:
        if 'layout' in item:
            return item
        
cmds.select(selectLayout(), r=1)

Looks similar, but uses [::-1] for the loop which is short-hand for iterating through the list in reverse. This way, it grabs the last instance of ‘layout’ instead of the first.


#5

Hi al_ka,
Tested it and all works great! Thanks for the help and also a python lesson :slight_smile:

Thank you so much!