ls objs by part of name


#1

Heya

Annoying like hell but I forgot how to do it o.o.

How can I get this to work ?

oldSel = cmds.ls(sl=1)

objs = cmds.ls('cube*')

print objs

I have objs with name :
pCubexx
testcube
catCube
CubeCube
Cubecube12312

It should select all that has cube in the name…

Regards

Dariusz


#2
import re
objectList = ["pCubexx", "testcube", "catCube", "CubeCube", "Cubecube12312", "patate"]
p = re.compile('cube', re.IGNORECASE)
output = [i for i in objectList if p.search(i)]


#3

Thanks for quick and perfect help !

Actually that didn’t work… so I made this :

objectList = cmds.ls(geometry=True)
for a in objectList:
    if a.find("sva")!=-1:
            print a

#4

cmds.ls(‘cube’, r=True)

will find everything with “cube” in the name.
the “r” flag is to search recursively through namespaces as well.

case sensitivity matters, I believe you can also do:

cmds.ls([‘cube’, ‘Cube’])

but I haven’t tested that… so I could be wrong


#5

In his case, you have to decline all combinaisation of case sensitivity and you have to bypass all namespaces.

cmds.ls(['*cube*', '*Cube*'])

won’t be enough

Oddly my method is working at work and at home. But the trick with “find” may work, you should add lower() method :


objectList = cmds.ls(geometry=True)
for a in objectList:
    if a.lower().find("sva")!=-1:
            print a

#6

this worked just fine…
and its faster since you aren’t using a python for loop, you are calling maya’s internal ls command.


cmds.ls(['*cube*', '*Cube*'], r=True)
// Result: [u'NewNamespace1:pcube2',
 u'NewNamespace1:pcubeShape2',
 u'pCube1',
 u'pCubeShape1',
 u'polyCube1'] //


#7

You may have something that I haven’t :

select -r feather:pPlane1 ;
cmds.ls("*feather*")
# Result: [] # 
cmds.ls("*pPlane1*")
# Result: [] # 

I agree it should be faster but writing :
cube Cube CUBE CuBe etc… is really annoying (it could be another longer word…)
Looping throught string is not so long.


#8

nothing special, Maya has already been able to do this.

you can’t list namespaces with the “ls” command. you have to use the “namespaceInfo” command to find those.

if you want to find items in a namespace, you have to use the “r” flag to the “ls” command.

http://help.autodesk.com/cloudhelp/2016/ENU/Maya-Tech-Docs/CommandsPython/namespaceInfo.html

http://help.autodesk.com/cloudhelp/2016/ENU/Maya-Tech-Docs/CommandsPython/ls.html


#9

interesting, never noticed the ‘r’ flag. Thanks :slight_smile:

— EDIT –

I’ve done some test over 26784 pCube in maya

import re
import time

t = time.time()

objectList = cmds.ls(o=1)
p = re.compile('pCube')
output = [i for i in objectList if p.search(i)]

t = time.time() - t
# Result: 0.14499998092651367 # 
t = time.time()

cmds.ls('*pCube*', o=True)

t = time.time() - t
# Result: 0.2630000114440918 # 

Oddly, looping with regex is .1 faster than using ‘*’


#10

i could be wrong about this, but I believe a list comprehension uses a generator, which will consume less memory and might be faster than a proper “for” loop.